Class: Amaterasu::Cartridge

Inherits:
Object
  • Object
show all
Defined in:
lib/amaterasu/cartridge.rb,
lib/amaterasu/cartridge/rom.rb,
lib/amaterasu/cartridge/mbc1.rb

Overview

Models a Game Boy cartridge.

Defined Under Namespace

Classes: Mbc1, Rom

Constant Summary collapse

ROM_BANK_SIZE =
16 * 1024
RAM_BANK_SIZE =
8 * 1024

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rom:, mbc: nil, ram: nil) ⇒ Cartridge

Creates a cartridge object based on the cartridge type.



26
27
28
29
30
31
32
33
# File 'lib/amaterasu/cartridge.rb', line 26

def initialize(rom:, mbc: nil, ram: nil)
  @rom = rom
  @ram = ram
  @mbc = mbc

  add_ram if rom.cartridge_type.include?('RAM')
  add_mbc(Mbc1.new(@rom, @ram)) if rom.cartridge_type.include?('MBC1')
end

Class Method Details

.load_rom(file_path, trace_rom:) ⇒ Object

Loads a Rom from a file path.

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/amaterasu/cartridge.rb', line 10

def self.load_rom(file_path, trace_rom:)
  rom = Rom.from_file(file_path)

  raise ArgumentError, 'Invalid ROM' unless rom.valid_checksum?

  if trace_rom
    puts rom.title
    puts rom.cartridge_type
    puts rom.rom_size
    puts rom.ram_size
  end

  new(rom: rom)
end

Instance Method Details

#add_mbc(mbc) ⇒ Object



35
36
37
# File 'lib/amaterasu/cartridge.rb', line 35

def add_mbc(mbc)
  @mbc = mbc
end

#add_ramObject



39
40
41
# File 'lib/amaterasu/cartridge.rb', line 39

def add_ram
  @ram = GameBoy::Ram.new(size: @rom.ram_size, offset: 0xA000)
end

#read_ram(address) ⇒ Object



55
56
57
58
59
# File 'lib/amaterasu/cartridge.rb', line 55

def read_ram(address)
  return 0xFF if @ram.nil?

  @mbc.nil? ? @ram.read_byte(address) : @mbc.read_byte(address)
end

#read_rom(address) ⇒ Object

Delegates the read byte to either the ROM or the MBC (Not implemented yet).



44
45
46
# File 'lib/amaterasu/cartridge.rb', line 44

def read_rom(address)
  @mbc.nil? ? @rom.read_byte(address) : @mbc.read_byte(address)
end

#write_ram(address, value) ⇒ Object

Delegates the write byte to the MBC (Not implemented yet).



62
63
64
65
66
# File 'lib/amaterasu/cartridge.rb', line 62

def write_ram(address, value)
  return unless @mbc

  @mbc.write_byte(address, value)
end

#write_rom(address, value) ⇒ Object

Delegates the read byte to the MBC (Not implemented yet).



49
50
51
52
53
# File 'lib/amaterasu/cartridge.rb', line 49

def write_rom(address, value)
  return unless @mbc

  @mbc.write_byte(address, value)
end