Class: Amaterasu::GameBoy::Ram

Inherits:
Object
  • Object
show all
Defined in:
lib/amaterasu/game_boy/ram.rb

Overview

Models the RAM chip within the Game Boy.

Direct Known Subclasses

Oam, Vram

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size:, offset:) ⇒ Ram

Creates a new Ram object.

Parameters:

  • size (Integer)

    Memory size in bytes.

  • offset (Integer)

    Address offset based on the Bus memory map.



14
15
16
17
18
19
20
21
# File 'lib/amaterasu/game_boy/ram.rb', line 14

def initialize(size:, offset:)
  @size = size
  @offset = offset

  @data = Array.new(size, 0x00)
  @backup = nil
  @backed_up = false
end

Instance Attribute Details

#sizeObject (readonly)

Memory size in bytes.



8
9
10
# File 'lib/amaterasu/game_boy/ram.rb', line 8

def size
  @size
end

Instance Method Details

#disk_sizeObject

Returns a readable string in B or KiB based on memory size.



41
42
43
44
45
# File 'lib/amaterasu/game_boy/ram.rb', line 41

def disk_size
  return "#{@size} B" if @size < 1024

  "#{@size / 1024} KiB"
end

#read_backup(address:) ⇒ Object

Provides an interface to read backup data.



34
35
36
37
38
# File 'lib/amaterasu/game_boy/ram.rb', line 34

def read_backup(address:)
  return 0xFF unless @backed_up

  @backup[address - @offset]
end

#read_byte(address:) ⇒ Object

Returns a 8-bit value from a given address in memory.



24
25
26
# File 'lib/amaterasu/game_boy/ram.rb', line 24

def read_byte(address:)
  @data[address - @offset]
end

#restore_dataObject

Restores a previously backed up memory state.



54
55
56
# File 'lib/amaterasu/game_boy/ram.rb', line 54

def restore_data
  @data = @backup.dup
end

#save_dataObject

Creates a copy of the current state of memory.



48
49
50
51
# File 'lib/amaterasu/game_boy/ram.rb', line 48

def save_data
  @backup = @data.dup
  @backed_up = true
end

#wipe_backupObject

Clears all previously set backup values.



64
65
66
67
# File 'lib/amaterasu/game_boy/ram.rb', line 64

def wipe_backup
  @backup = Array.new
  @backed_up = false
end

#wipe_dataObject

Clears all previously set memory values.



59
60
61
# File 'lib/amaterasu/game_boy/ram.rb', line 59

def wipe_data
  @data = Array.new(@size, 0x00)
end

#write_byte(address:, value:) ⇒ Object

Stores a 8-bit value into a given address in memory.



29
30
31
# File 'lib/amaterasu/game_boy/ram.rb', line 29

def write_byte(address:, value:)
  @data[address - @offset] = value & 0xFF
end