Class: N65::MemorySpace

Inherits:
Object
  • Object
show all
Defined in:
lib/n65/memory_space.rb

Overview

Let’s use this to simulate a virtual address space

Either a 16kb prog rom or 8kb char rom space.
It can also be used to create arbitrary sized spaces
for example to build the final binary ROM in.

Defined Under Namespace

Classes: AccessOutOfBounds, AccessOutsideCharRom, AccessOutsideProgRom

Constant Summary collapse

BankSizes =

Some constants, the size of PROG and CHAR ROM

{
  :ines => 0x10,     #  16b
  :prog => 0x4000,   #  16kb
  :char => 0x2000,   #   8kb
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size, type) ⇒ MemorySpace

Create a completely zeroed memory space



48
49
50
51
# File 'lib/n65/memory_space.rb', line 48

def initialize(size, type)
  @type = type
  @memory = Array.new(size, 0x0)
end

Class Method Details

.create_bank(type) ⇒ Object

Create a new bank



41
42
43
# File 'lib/n65/memory_space.rb', line 41

def self.create_bank(type)
  self.new(BankSizes[type], type)
end

.create_char_romObject

Create a new CHAR ROM



34
35
36
# File 'lib/n65/memory_space.rb', line 34

def self.create_char_rom
  self.create_bank(:char)
end

.create_prog_romObject

Create a new PROG ROM



27
28
29
# File 'lib/n65/memory_space.rb', line 27

def self.create_prog_rom
  self.create_bank(:prog)
end

Instance Method Details

#emit_bytesObject

Return the memory as an array of bytes to write to disk



81
82
83
# File 'lib/n65/memory_space.rb', line 81

def emit_bytes
  @memory
end

#read(address, count) ⇒ Object

Normalized read from memory



56
57
58
59
60
61
62
# File 'lib/n65/memory_space.rb', line 56

def read(address, count)
  from_normalized = normalize_address(address)
  to_normalized = normalize_address(address + (count - 1))
  ensure_addresses_in_bounds!([from_normalized, to_normalized])

  @memory[from_normalized..to_normalized]
end

#write(address, bytes) ⇒ Object

Normalized write to memory



67
68
69
70
71
72
73
74
75
76
# File 'lib/n65/memory_space.rb', line 67

def write(address, bytes)
  from_normalized = normalize_address(address)
  to_normalized = normalize_address(address + bytes.size - 1)
  ensure_addresses_in_bounds!([from_normalized, to_normalized])

  bytes.each_with_index do |byte, index|
    @memory[from_normalized + index] = byte
  end
  bytes.size
end