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

BANK_SIZES =

Some constants, the size of PROG and CHAR ROM

{
  ines: 0x10,
  prog: 0x4000,
  char: 0x2000
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size, type) ⇒ MemorySpace

Create a completely zeroed memory space



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

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

Class Method Details

.create_bank(type) ⇒ Object



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

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

.create_char_romObject



24
25
26
# File 'lib/n65/memory_space.rb', line 24

def self.create_char_rom
  create_bank(:char)
end

.create_prog_romObject



20
21
22
# File 'lib/n65/memory_space.rb', line 20

def self.create_prog_rom
  create_bank(:prog)
end

Instance Method Details

#emit_bytesObject

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



62
63
64
# File 'lib/n65/memory_space.rb', line 62

def emit_bytes
  @memory
end

#read(address, count) ⇒ Object

Normalized read from memory



40
41
42
43
44
45
46
# File 'lib/n65/memory_space.rb', line 40

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

#usage_infoObject

Bank Usage information



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

def usage_info
  percent_used = @bytes_written / @memory.size.to_f * 100
  percent_string = format('%0.2f', percent_used)
  bytes_written_hex = format('$%04x', @bytes_written)
  memory_size_hex = format('$%04x', @memory.size)
  "(#{bytes_written_hex} / #{memory_size_hex}) #{percent_string}%"
end

#write(address, bytes) ⇒ Object

Normalized write to memory



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/n65/memory_space.rb', line 49

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
    @bytes_written += 1
  end
  bytes.size
end