parse cartridge type to enum

This commit is contained in:
Matthew Berry 2021-08-06 21:31:39 -07:00
parent dc6b2915dc
commit cf323648a5
7 changed files with 80 additions and 14 deletions

View file

@ -49,16 +49,14 @@ module GB
bytes
end
cartridge_type = rom[0x0147]
cartridge_type = CartridgeType.new(rom[0x0147].to_i)
cartridge = case cartridge_type
when 0x00, 0x08, 0x09 then ROM.new rom
when 0x01, 0x02, 0x03 then MBC1.new rom
when 0x05, 0x06 then MBC2.new rom
when 0x0F, 0x10, 0x11,
0x12, 0x13 then MBC3.new rom
when 0x19, 0x1A, 0x1B,
0x1C, 0x1D, 0x1E then MBC5.new rom
else raise "Unimplemented cartridge type: #{hex_str cartridge_type}"
when .is_rom? then ROM.new rom, cartridge_type
when .is_mbc1? then MBC1.new rom, cartridge_type
when .is_mbc2? then MBC2.new rom, cartridge_type
when .is_mbc3? then MBC3.new rom, cartridge_type
when .is_mbc5? then MBC5.new rom, cartridge_type
else abort "Unimplemented cartridge type: #{hex_str cartridge_type.value.to_u8!}"
end
cartridge.sav_file_path = rom_path.rpartition('.')[0] + ".sav"

68
src/crab/gb/mbc/enums.cr Normal file
View file

@ -0,0 +1,68 @@
module GB
enum CartridgeType
ROM = 0x00
ROM_RAM = 0x08
ROM_RAM_BATTERY = 0x09
MBC1 = 0x01
MBC1_RAM = 0x02
MBC1_RAM_BATTERY = 0x03
MBC2 = 0x05
MBC2_BATTERY = 0x06
MBC3_TIMER_BATTERY = 0x0F
MBC3_TIMER_RAM_BATTERY = 0x10
MBC3 = 0x11
MBC3_RAM = 0x12
MBC3_RAM_BATTERY = 0x13
MBC5 = 0x19
MBC5_RAM = 0x1A
MBC5_RAM_BATTERY = 0x1B
MBC5_RUMBLE = 0x1C
MBC5_RUMBLE_RAM = 0x1D
MBC5_RUMBLE_RAM_BATTERY = 0x1E
MBC6 = 0x20
MBC7_SENSOR_RUMBLE_RAM_BATTERY = 0x22
MMM01 = 0x0B
MMM01_RAM = 0x0C
MMM01_RAM_BATTERY = 0x0D
POCKET_CAMERA = 0xFC
BANDAI_TAMA5 = 0xFD
HuC3 = 0xFE
HuC1_RAM_BATTERY = 0xFF
def is_rom? : Bool
to_s.starts_with? "ROM"
end
def is_mbc1? : Bool
to_s.starts_with? "MBC1"
end
def is_mbc2? : Bool
to_s.starts_with? "MBC2"
end
def is_mbc3? : Bool
to_s.starts_with? "MBC3"
end
def is_mbc5? : Bool
to_s.starts_with? "MBC5"
end
def has_ram? : Bool
to_s.includes? "RAM"
end
def has_battery? : Bool
to_s.includes? "BATTERY"
end
def has_timer? : Bool
to_s.includes? "TIMER"
end
def has_rumble? : Bool
to_s.includes? "RUMBLE"
end
end
end

View file

@ -1,6 +1,6 @@
module GB
class MBC1 < Cartridge
def initialize(@rom : Bytes)
def initialize(@rom : Bytes, @cartridge_type : CartridgeType)
if ram_size == 0 && (@rom[0x0147] == 0x02 || @rom[0x0147] == 0x03)
STDERR.puts "MBC1 cartridge has ram, but `ram_size` was reported as 0. Ignoring `ram_size` and using 8kB of ram."
@ram = Bytes.new 0x2000

View file

@ -2,7 +2,7 @@ module GB
class MBC2 < Cartridge
getter ram_size : Int32 { 0x0200 }
def initialize(@rom : Bytes)
def initialize(@rom : Bytes, @cartridge_type : CartridgeType)
@ram = Bytes.new ram_size
@ram_enabled = false
@rom_bank = 1_u8

View file

@ -1,6 +1,6 @@
module GB
class MBC3 < Cartridge
def initialize(@rom : Bytes)
def initialize(@rom : Bytes, @cartridge_type : CartridgeType)
@ram = Bytes.new ram_size
@ram_enabled = false
@rom_bank_number = 1_u8 # 7-bit register

View file

@ -1,6 +1,6 @@
module GB
class MBC5 < Cartridge
def initialize(@rom : Bytes)
def initialize(@rom : Bytes, @cartridge_type : CartridgeType)
@ram = Bytes.new ram_size
@ram_enabled = false
@rom_bank_number = 1_u16 # 9-bit register

View file

@ -1,6 +1,6 @@
module GB
class ROM < Cartridge
def initialize(@rom : Bytes)
def initialize(@rom : Bytes, @cartridge_type : CartridgeType)
@ram = Bytes.new ram_size
end