Class: Zemu::Config::SerialPort

Inherits:
BusDevice show all
Defined in:
lib/zemu/config.rb

Overview

Serial Input/Output object

Represents a serial connection between the emulated CPU and the host machine, with input and output mapped to Z80 I/O ports.

Instance Method Summary collapse

Methods inherited from BusDevice

#clock, #functions, #interrupt, #interrupt?, #mem_read, #mem_write, #memory, #nmi, #nmi?, #when_setup

Methods inherited from Zemu::ConfigObject

#method_missing

Constructor Details

#initializeSerialPort

Constructor.

Takes a block in which the parameters of the serial port can be initialized.

All parameters can be set within this block. They become readonly as soon as the block completes.

Examples:


Zemu::Config::SerialPort.new do
    name "serial"
    in_port 0x00
    out_port 0x01
end


360
361
362
363
364
365
# File 'lib/zemu/config.rb', line 360

def initialize
    super

    @buffer_tx = []
    @buffer_rx = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Zemu::ConfigObject

Instance Method Details

#get_byteObject

Gets a byte transmitted by the CPU, or nil if transmit buffer is empty.



409
410
411
# File 'lib/zemu/config.rb', line 409

def get_byte
    @buffer_tx.shift()
end

#gets(n = nil) ⇒ Object

Gets a string from the transmit buffer of this device. String length will be no more than n, but may be less if fewer characters exist in buffer.

entire buffer will be returned.

Parameters:

  • n (defaults to: nil)

    Length of string to retrieve. If omitted the



429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# File 'lib/zemu/config.rb', line 429

def gets(n=nil)
    s = ""

    if n.nil?
        until (c = get_byte()).nil?
            s += c.chr
        end
    else
        n.times do
            c = get_byte()
            break if c.nil?

            s += c.chr
        end
    end

    s
end

#io_read(port) ⇒ Object

IO bus read handler.

Handles read access via the IO bus to this device.

Returns the value read, or nil if the port does not correspond to this device.

Parameters:

  • port

    The IO port being accessed.



375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/zemu/config.rb', line 375

def io_read(port)
    if port == in_port
        return @buffer_rx.shift()
    elsif port == ready_port
        if @buffer_rx.empty?
            return 0
        else
            return 1
        end
    end

    nil
end

#io_write(port, value) ⇒ Object

IO bus write handler.

Handles write access via the IO bus to this device.

Parameters:

  • port

    The IO port being accessed.

  • value

    The value being written.



395
396
397
398
399
# File 'lib/zemu/config.rb', line 395

def io_write(port, value)
    if port == out_port
        @buffer_tx << value
    end
end

#paramsObject

Valid parameters for a SerialPort, along with those defined in [Zemu::Config::BusDevice].



450
451
452
# File 'lib/zemu/config.rb', line 450

def params
    super + %w(in_port out_port ready_port)
end

#put_byte(b) ⇒ Object

Puts a byte in the receive buffer of this device.



414
415
416
# File 'lib/zemu/config.rb', line 414

def put_byte(b)
    @buffer_rx << b
end

#puts(s) ⇒ Object

Puts a string in the receive buffer of this device.



419
420
421
# File 'lib/zemu/config.rb', line 419

def puts(s)
    s.each_byte { |b| put_byte(b) }
end

#transmitted_countObject

Gets number of bytes transmitted by the CPU, but not yet read from this device.



403
404
405
# File 'lib/zemu/config.rb', line 403

def transmitted_count
    @buffer_tx.size
end