Class: Amaterasu::GameBoy::Ram
- Inherits:
-
Object
- Object
- Amaterasu::GameBoy::Ram
- Defined in:
- lib/amaterasu/game_boy/ram.rb
Overview
Models the RAM chip within the Game Boy.
Instance Attribute Summary collapse
-
#size ⇒ Object
readonly
Memory size in bytes.
Instance Method Summary collapse
-
#disk_size ⇒ Object
Returns a readable string in B or KiB based on memory size.
-
#initialize(size:, offset:) ⇒ Ram
constructor
Creates a new Ram object.
-
#read_backup(address:) ⇒ Object
Provides an interface to read backup data.
-
#read_byte(address:) ⇒ Object
Returns a 8-bit value from a given address in memory.
-
#restore_data ⇒ Object
Restores a previously backed up memory state.
-
#save_data ⇒ Object
Creates a copy of the current state of memory.
-
#wipe_backup ⇒ Object
Clears all previously set backup values.
-
#wipe_data ⇒ Object
Clears all previously set memory values.
-
#write_byte(address:, value:) ⇒ Object
Stores a 8-bit value into a given address in memory.
Constructor Details
#initialize(size:, offset:) ⇒ Ram
Creates a new Ram object.
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
#size ⇒ Object (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_size ⇒ Object
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_data ⇒ Object
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_data ⇒ Object
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_backup ⇒ Object
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_data ⇒ Object
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 |