From f237b1e9b9ebec2089239c10b8efbb228f9ae26f Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Thu, 4 Nov 2021 00:32:15 +0200 Subject: [PATCH] CGB-0 support --- BootROMs/cgb0_boot.asm | 2 ++ BootROMs/cgb_boot.asm | 2 ++ Cocoa/Document.m | 4 ++-- Cocoa/Preferences.xib | 2 +- Core/apu.c | 2 +- Core/display.c | 3 +++ Core/gb.c | 11 +++++++++-- Core/gb.h | 6 +++--- Core/memory.c | 10 +++------- Core/save_state.c | 2 ++ Makefile | 9 +++++---- SDL/main.c | 4 ++-- libretro/libretro.c | 12 ++++++------ 13 files changed, 41 insertions(+), 28 deletions(-) create mode 100644 BootROMs/cgb0_boot.asm diff --git a/BootROMs/cgb0_boot.asm b/BootROMs/cgb0_boot.asm new file mode 100644 index 0000000..d49166d --- /dev/null +++ b/BootROMs/cgb0_boot.asm @@ -0,0 +1,2 @@ +CGB0 EQU 1 +include "cgb_boot.asm" \ No newline at end of file diff --git a/BootROMs/cgb_boot.asm b/BootROMs/cgb_boot.asm index 0bc2b17..ca3b57f 100644 --- a/BootROMs/cgb_boot.asm +++ b/BootROMs/cgb_boot.asm @@ -23,6 +23,7 @@ Start: dec c jr nz, .clearOAMLoop +IF !DEF(CGB0) ; Init waveform ld c, $10 ld hl, $FF30 @@ -31,6 +32,7 @@ Start: cpl dec c jr nz, .waveformLoop +ENDC ; Clear chosen input palette ldh [InputPalette], a diff --git a/Cocoa/Document.m b/Cocoa/Document.m index 74fc98e..2a77259 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -576,12 +576,12 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency) - (void) loadBootROM: (GB_boot_rom_t)type { static NSString *const names[] = { - [GB_BOOT_ROM_DMG0] = @"dmg0_boot", + [GB_BOOT_ROM_DMG_0] = @"dmg0_boot", [GB_BOOT_ROM_DMG] = @"dmg_boot", [GB_BOOT_ROM_MGB] = @"mgb_boot", [GB_BOOT_ROM_SGB] = @"sgb_boot", [GB_BOOT_ROM_SGB2] = @"sgb2_boot", - [GB_BOOT_ROM_CGB0] = @"cgb0_boot", + [GB_BOOT_ROM_CGB_0] = @"cgb0_boot", [GB_BOOT_ROM_CGB] = @"cgb_boot", [GB_BOOT_ROM_AGB] = @"agb_boot", }; diff --git a/Cocoa/Preferences.xib b/Cocoa/Preferences.xib index 5cb44ae..67e57b1 100644 --- a/Cocoa/Preferences.xib +++ b/Cocoa/Preferences.xib @@ -397,7 +397,7 @@ - + diff --git a/Core/apu.c b/Core/apu.c index e0b75f9..aac87b5 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -868,7 +868,7 @@ static inline uint16_t effective_channel4_counter(GB_gameboy_t *gb) case GB_MODEL_SGB_PAL_NO_SFC: case GB_MODEL_SGB2: case GB_MODEL_SGB2_NO_SFC: - // case GB_MODEL_CGB_0: + case GB_MODEL_CGB_0: // case GB_MODEL_CGB_A: case GB_MODEL_CGB_C: if (effective_counter & 8) { diff --git a/Core/display.c b/Core/display.c index e8b0006..83a499f 100644 --- a/Core/display.c +++ b/Core/display.c @@ -164,6 +164,9 @@ static void display_vblank(GB_gameboy_t *gb) 0x1050, 0x3C84, 0x0E07, 0x0E18, 0x2964, }; unsigned index = gb->rom? gb->rom[0x14e] % 5 : 0; + if (gb->model == GB_MODEL_CGB_0) { + index = 1; // CGB 0 was only available in Indigo! + } gb->borrowed_border.palette[0] = LE16(colors[index]); gb->borrowed_border.palette[10] = LE16(colors[5 + index]); gb->borrowed_border.palette[14] = LE16(colors[10 + index]); diff --git a/Core/gb.c b/Core/gb.c index 86e012f..0c47ce9 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -1462,7 +1462,8 @@ static void reset_ram(GB_gameboy_t *gb) gb->ram[i] ^= GB_random() & GB_random() & GB_random(); } break; - + + case GB_MODEL_CGB_0: case GB_MODEL_CGB_B: case GB_MODEL_CGB_C: for (unsigned i = 0; i < gb->ram_size; i++) { @@ -1489,6 +1490,7 @@ static void reset_ram(GB_gameboy_t *gb) /* HRAM */ switch (gb->model) { + case GB_MODEL_CGB_0: case GB_MODEL_CGB_B: case GB_MODEL_CGB_C: case GB_MODEL_CGB_D: @@ -1520,6 +1522,7 @@ static void reset_ram(GB_gameboy_t *gb) /* OAM */ switch (gb->model) { + case GB_MODEL_CGB_0: case GB_MODEL_CGB_B: case GB_MODEL_CGB_C: case GB_MODEL_CGB_D: @@ -1552,12 +1555,13 @@ static void reset_ram(GB_gameboy_t *gb) /* Wave RAM */ switch (gb->model) { + case GB_MODEL_CGB_0: case GB_MODEL_CGB_B: case GB_MODEL_CGB_C: case GB_MODEL_CGB_D: case GB_MODEL_CGB_E: case GB_MODEL_AGB: - /* Initialized by CGB-A and newer, 0s in CGB-0*/ + /* Initialized by CGB-A and newer, 0s in CGB-0 */ break; case GB_MODEL_MGB: { for (unsigned i = 0; i < GB_IO_WAV_END - GB_IO_WAV_START; i++) { @@ -1626,6 +1630,9 @@ static void request_boot_rom(GB_gameboy_t *gb) case GB_MODEL_SGB2_NO_SFC: type = GB_BOOT_ROM_SGB2; break; + case GB_MODEL_CGB_0: + type = GB_BOOT_ROM_CGB_0; + break; case GB_MODEL_CGB_B: case GB_MODEL_CGB_C: case GB_MODEL_CGB_D: diff --git a/Core/gb.h b/Core/gb.h index 67ad95e..bd0fa83 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -131,7 +131,7 @@ typedef enum { GB_MODEL_MGB = 0x100, GB_MODEL_SGB2 = 0x101, GB_MODEL_SGB2_NO_SFC = GB_MODEL_SGB2 | GB_MODEL_NO_SFC_BIT, - // GB_MODEL_CGB_0 = 0x200, + GB_MODEL_CGB_0 = 0x200, // GB_MODEL_CGB_A = 0x201, GB_MODEL_CGB_B = 0x202, GB_MODEL_CGB_C = 0x203, @@ -277,12 +277,12 @@ typedef enum { } GB_log_attributes; typedef enum { - GB_BOOT_ROM_DMG0, + GB_BOOT_ROM_DMG_0, GB_BOOT_ROM_DMG, GB_BOOT_ROM_MGB, GB_BOOT_ROM_SGB, GB_BOOT_ROM_SGB2, - GB_BOOT_ROM_CGB0, + GB_BOOT_ROM_CGB_0, GB_BOOT_ROM_CGB, GB_BOOT_ROM_AGB, } GB_boot_rom_t; diff --git a/Core/memory.c b/Core/memory.c index a3647c2..c221352 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -504,10 +504,8 @@ static uint8_t read_high_memory(GB_gameboy_t *gb, uint16_t addr) case GB_MODEL_CGB_C: case GB_MODEL_CGB_B: - /* - case GB_MODEL_CGB_A: + // case GB_MODEL_CGB_A: case GB_MODEL_CGB_0: - */ addr &= ~0x18; return gb->extra_oam[addr - 0xfea0]; @@ -1010,10 +1008,8 @@ static void write_high_memory(GB_gameboy_t *gb, uint16_t addr, uint8_t value) break; case GB_MODEL_CGB_C: case GB_MODEL_CGB_B: - /* - case GB_MODEL_CGB_A: - case GB_MODEL_CGB_0: - */ + // case GB_MODEL_CGB_A: + case GB_MODEL_CGB_0: addr &= ~0x18; gb->extra_oam[addr - 0xfea0] = value; break; diff --git a/Core/save_state.c b/Core/save_state.c index e33dbf1..e472995 100644 --- a/Core/save_state.c +++ b/Core/save_state.c @@ -347,6 +347,7 @@ static bool verify_and_update_state_compatibility(GB_gameboy_t *gb, GB_gameboy_t case GB_MODEL_MGB: return true; case GB_MODEL_SGB2: return true; case GB_MODEL_SGB2_NO_SFC: return true; + case GB_MODEL_CGB_0: return true; case GB_MODEL_CGB_B: return true; case GB_MODEL_CGB_C: return true; case GB_MODEL_CGB_D: return true; @@ -648,6 +649,7 @@ static int save_state_internal(GB_gameboy_t *gb, virtual_file_t *file, bool appe case GB_MODEL_SGB2: bess_core.full_model = BE32('S2 '); break; + case GB_MODEL_CGB_0: bess_core.full_model = BE32('CC0 '); break; case GB_MODEL_CGB_B: bess_core.full_model = BE32('CCB '); break; case GB_MODEL_CGB_C: bess_core.full_model = BE32('CCC '); break; case GB_MODEL_CGB_D: bess_core.full_model = BE32('CCD '); break; diff --git a/Makefile b/Makefile index fadc025..7016ac3 100644 --- a/Makefile +++ b/Makefile @@ -199,9 +199,9 @@ endif cocoa: $(BIN)/SameBoy.app quicklook: $(BIN)/SameBoy.qlgenerator -sdl: $(SDL_TARGET) $(BIN)/SDL/dmg_boot.bin $(BIN)/SDL/mgb_boot.bin $(BIN)/SDL/cgb_boot.bin $(BIN)/SDL/agb_boot.bin $(BIN)/SDL/sgb_boot.bin $(BIN)/SDL/sgb2_boot.bin $(BIN)/SDL/LICENSE $(BIN)/SDL/registers.sym $(BIN)/SDL/background.bmp $(BIN)/SDL/Shaders -bootroms: $(BIN)/BootROMs/agb_boot.bin $(BIN)/BootROMs/mgb_boot.bin $(BIN)/BootROMs/cgb_boot.bin $(BIN)/BootROMs/dmg_boot.bin $(BIN)/BootROMs/sgb_boot.bin $(BIN)/BootROMs/sgb2_boot.bin -tester: $(TESTER_TARGET) $(BIN)/tester/dmg_boot.bin $(BIN)/tester/mgb_boot.bin $(BIN)/tester/cgb_boot.bin $(BIN)/tester/agb_boot.bin $(BIN)/tester/sgb_boot.bin $(BIN)/tester/sgb2_boot.bin +sdl: $(SDL_TARGET) $(BIN)/SDL/dmg_boot.bin $(BIN)/SDL/cgb0_boot.bin $(BIN)/SDL/cgb_boot.bin $(BIN)/SDL/agb_boot.bin $(BIN)/SDL/sgb_boot.bin $(BIN)/SDL/sgb2_boot.bin $(BIN)/SDL/LICENSE $(BIN)/SDL/registers.sym $(BIN)/SDL/background.bmp $(BIN)/SDL/Shaders +bootroms: $(BIN)/BootROMs/agb_boot.bin $(BIN)/BootROMs/cgb_boot.bin $(BIN)/BootROMs/cgb0_boot.bin $(BIN)/BootROMs/dmg_boot.bin $(BIN)/BootROMs/sgb_boot.bin $(BIN)/BootROMs/sgb2_boot.bin +tester: $(TESTER_TARGET) $(BIN)/tester/dmg_boot.bin $(BIN)/tester/cgb_boot.bin $(BIN)/tester/agb_boot.bin $(BIN)/tester/sgb_boot.bin $(BIN)/tester/sgb2_boot.bin all: cocoa sdl tester libretro # Get a list of our source files and their respective object file targets @@ -280,6 +280,7 @@ $(BIN)/SameBoy.app: $(BIN)/SameBoy.app/Contents/MacOS/SameBoy \ Misc/registers.sym \ $(BIN)/SameBoy.app/Contents/Resources/dmg_boot.bin \ $(BIN)/SameBoy.app/Contents/Resources/mgb_boot.bin \ + $(BIN)/SameBoy.app/Contents/Resources/cgb0_boot.bin \ $(BIN)/SameBoy.app/Contents/Resources/cgb_boot.bin \ $(BIN)/SameBoy.app/Contents/Resources/agb_boot.bin \ $(BIN)/SameBoy.app/Contents/Resources/sgb_boot.bin \ @@ -418,7 +419,7 @@ $(OBJ)/BootROMs/SameBoyLogo.pb12: $(OBJ)/BootROMs/SameBoyLogo.2bpp $(PB12_COMPRE $(PB12_COMPRESS): BootROMs/pb12.c $(NATIVE_CC) -std=c99 -Wall -Werror $< -o $@ -$(BIN)/BootROMs/mgb_boot.bin: BootROMs/mgb_boot.asm +$(BIN)/BootROMs/cgb0_boot.bin: BootROMs/cgb_boot.asm $(BIN)/BootROMs/agb_boot.bin: BootROMs/cgb_boot.asm $(BIN)/BootROMs/cgb_boot_fast.bin: BootROMs/cgb_boot.asm $(BIN)/BootROMs/sgb2_boot: BootROMs/sgb_boot.asm diff --git a/SDL/main.c b/SDL/main.c index 999f143..b6607f5 100644 --- a/SDL/main.c +++ b/SDL/main.c @@ -590,12 +590,12 @@ static bool handle_pending_command(void) static void load_boot_rom(GB_gameboy_t *gb, GB_boot_rom_t type) { static const char *const names[] = { - [GB_BOOT_ROM_DMG0] = "dmg0_boot.bin", + [GB_BOOT_ROM_DMG_0] = "dmg0_boot.bin", [GB_BOOT_ROM_DMG] = "dmg_boot.bin", [GB_BOOT_ROM_MGB] = "mgb_boot.bin", [GB_BOOT_ROM_SGB] = "sgb_boot.bin", [GB_BOOT_ROM_SGB2] = "sgb2_boot.bin", - [GB_BOOT_ROM_CGB0] = "cgb0_boot.bin", + [GB_BOOT_ROM_CGB_0] = "cgb0_boot.bin", [GB_BOOT_ROM_CGB] = "cgb_boot.bin", [GB_BOOT_ROM_AGB] = "agb_boot.bin", }; diff --git a/libretro/libretro.c b/libretro/libretro.c index 5a559c4..94b82a6 100644 --- a/libretro/libretro.c +++ b/libretro/libretro.c @@ -418,34 +418,34 @@ static void set_link_cable_state(bool state) static void boot_rom_load(GB_gameboy_t *gb, GB_boot_rom_t type) { const char *model_name = (char *[]) { - [GB_BOOT_ROM_DMG0] = "dmg0", + [GB_BOOT_ROM_DMG_0] = "dmg0", [GB_BOOT_ROM_DMG] = "dmg", [GB_BOOT_ROM_MGB] = "mgb", [GB_BOOT_ROM_SGB] = "sgb", [GB_BOOT_ROM_SGB2] = "sgb2", - [GB_BOOT_ROM_CGB0] = "cgb0", + [GB_BOOT_ROM_CGB_0] = "cgb0", [GB_BOOT_ROM_CGB] = "cgb", [GB_BOOT_ROM_AGB] = "agb", }[type]; const uint8_t *boot_code = (const unsigned char *[]) { - [GB_BOOT_ROM_DMG0] = dmg_boot, // dmg0 not implemented yet + [GB_BOOT_ROM_DMG_0] = dmg_boot, // DMG_0 not implemented yet [GB_BOOT_ROM_DMG] = dmg_boot, [GB_BOOT_ROM_MGB] = dmg_boot, // mgb not implemented yet [GB_BOOT_ROM_SGB] = sgb_boot, [GB_BOOT_ROM_SGB2] = sgb2_boot, - [GB_BOOT_ROM_CGB0] = cgb_boot, // cgb0 not implemented yet + [GB_BOOT_ROM_CGB_0] = cgb_boot, // CGB_0 not implemented yet [GB_BOOT_ROM_CGB] = cgb_boot, [GB_BOOT_ROM_AGB] = agb_boot, }[type]; unsigned boot_length = (unsigned []) { - [GB_BOOT_ROM_DMG0] = dmg_boot_length, // dmg0 not implemented yet + [GB_BOOT_ROM_DMG_0] = dmg_boot_length, // DMG_0 not implemented yet [GB_BOOT_ROM_DMG] = dmg_boot_length, [GB_BOOT_ROM_MGB] = dmg_boot_length, // mgb not implemented yet [GB_BOOT_ROM_SGB] = sgb_boot_length, [GB_BOOT_ROM_SGB2] = sgb2_boot_length, - [GB_BOOT_ROM_CGB0] = cgb_boot_length, // cgb0 not implemented yet + [GB_BOOT_ROM_CGB_0] = cgb_boot_length, // CGB_0 not implemented yet [GB_BOOT_ROM_CGB] = cgb_boot_length, [GB_BOOT_ROM_AGB] = agb_boot_length, }[type];