From 6cd13be6249b2285264af8c1c55ef662f9553153 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 30 Oct 2021 20:58:57 +0300 Subject: [PATCH] Add CGB-B support --- Cocoa/Preferences.xib | 4 ++-- Core/apu.c | 7 ++----- Core/gb.c | 11 ++++++++--- Core/gb.h | 2 +- Core/memory.c | 4 ++-- Core/save_state.c | 3 ++- 6 files changed, 17 insertions(+), 14 deletions(-) diff --git a/Cocoa/Preferences.xib b/Cocoa/Preferences.xib index 41c1cda..5cb44ae 100644 --- a/Cocoa/Preferences.xib +++ b/Cocoa/Preferences.xib @@ -399,7 +399,7 @@ - + @@ -608,7 +608,7 @@ - + diff --git a/Core/apu.c b/Core/apu.c index e049c06..e0b75f9 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -834,7 +834,6 @@ static inline uint16_t effective_channel4_counter(GB_gameboy_t *gb) switch (gb->model) { /* Pre CGB revisions are assumed to be like CGB-C, A and 0 for the lack of a better guess. TODO: It could be verified with audio based test ROMs. */ -#if 0 case GB_MODEL_CGB_B: if (effective_counter & 8) { effective_counter |= 0xE; // Seems to me F under some circumstances? @@ -861,7 +860,6 @@ static inline uint16_t effective_channel4_counter(GB_gameboy_t *gb) effective_counter |= 0x20; } break; -#endif case GB_MODEL_DMG_B: case GB_MODEL_MGB: case GB_MODEL_SGB_NTSC: @@ -1056,7 +1054,6 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) case GB_IO_NR14: case GB_IO_NR24: { - /* TODO: GB_MODEL_CGB_D fails channel_1_sweep_restart_2, don't forget when adding support for this revision! */ unsigned index = reg == GB_IO_NR24? GB_SQUARE_2: GB_SQUARE_1; bool was_active = gb->apu.is_active[index]; /* TODO: When the sample length changes right before being updated, the countdown should change to the @@ -1162,7 +1159,7 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) } /* APU glitch - if length is enabled while the DIV-divider's LSB is 1, tick the length once. */ - if ((value & 0x40) && + if (((value & 0x40) || (GB_is_cgb(gb) && gb->model <= GB_MODEL_CGB_B)) && // Current value is irrelevant on CGB-B and older !gb->apu.square_channels[index].length_enabled && (gb->apu.div_divider & 1) && gb->apu.square_channels[index].pulse_length) { @@ -1262,7 +1259,7 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) } /* APU glitch - if length is enabled while the DIV-divider's LSB is 1, tick the length once. */ - if ((value & 0x40) && + if (((value & 0x40) || (GB_is_cgb(gb) && gb->model <= GB_MODEL_CGB_B)) && // Current value is irrelevant on CGB-B and older !gb->apu.wave_channel.length_enabled && (gb->apu.div_divider & 1) && gb->apu.wave_channel.pulse_length) { diff --git a/Core/gb.c b/Core/gb.c index 8d2545d..86e012f 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -1463,13 +1463,14 @@ static void reset_ram(GB_gameboy_t *gb) } break; + case GB_MODEL_CGB_B: case GB_MODEL_CGB_C: for (unsigned i = 0; i < gb->ram_size; i++) { if ((i & 0x808) == 0x800 || (i & 0x808) == 0x008) { gb->ram[i] = 0; } else { - gb->ram[i] = GB_random() | GB_random() | GB_random() | GB_random(); + gb->ram[i] = GB_random() | GB_random() | GB_random() | GB_random() | GB_random(); } } break; @@ -1488,6 +1489,7 @@ static void reset_ram(GB_gameboy_t *gb) /* HRAM */ switch (gb->model) { + case GB_MODEL_CGB_B: case GB_MODEL_CGB_C: case GB_MODEL_CGB_D: case GB_MODEL_CGB_E: @@ -1518,11 +1520,12 @@ static void reset_ram(GB_gameboy_t *gb) /* OAM */ switch (gb->model) { + case GB_MODEL_CGB_B: case GB_MODEL_CGB_C: - case GB_MODEL_CGB_D: + case GB_MODEL_CGB_D: case GB_MODEL_CGB_E: case GB_MODEL_AGB: - /* Zero'd out by boot ROM anyway*/ + /* Zero'd out by boot ROM anyway, extra OAM no accessible */ break; case GB_MODEL_DMG_B: @@ -1549,6 +1552,7 @@ static void reset_ram(GB_gameboy_t *gb) /* Wave RAM */ switch (gb->model) { + case GB_MODEL_CGB_B: case GB_MODEL_CGB_C: case GB_MODEL_CGB_D: case GB_MODEL_CGB_E: @@ -1622,6 +1626,7 @@ 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_B: case GB_MODEL_CGB_C: case GB_MODEL_CGB_D: case GB_MODEL_CGB_E: diff --git a/Core/gb.h b/Core/gb.h index d00d438..67ad95e 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -133,7 +133,7 @@ typedef enum { GB_MODEL_SGB2_NO_SFC = GB_MODEL_SGB2 | GB_MODEL_NO_SFC_BIT, // GB_MODEL_CGB_0 = 0x200, // GB_MODEL_CGB_A = 0x201, - // GB_MODEL_CGB_B = 0x202, + GB_MODEL_CGB_B = 0x202, GB_MODEL_CGB_C = 0x203, GB_MODEL_CGB_D = 0x204, GB_MODEL_CGB_E = 0x205, diff --git a/Core/memory.c b/Core/memory.c index 22926be..a3647c2 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -503,8 +503,8 @@ static uint8_t read_high_memory(GB_gameboy_t *gb, uint16_t addr) return gb->extra_oam[addr - 0xfea0]; case GB_MODEL_CGB_C: + case GB_MODEL_CGB_B: /* - case GB_MODEL_CGB_B: case GB_MODEL_CGB_A: case GB_MODEL_CGB_0: */ @@ -1009,8 +1009,8 @@ static void write_high_memory(GB_gameboy_t *gb, uint16_t addr, uint8_t value) gb->extra_oam[addr - 0xfea0] = value; break; case GB_MODEL_CGB_C: + case GB_MODEL_CGB_B: /* - case GB_MODEL_CGB_B: case GB_MODEL_CGB_A: case GB_MODEL_CGB_0: */ diff --git a/Core/save_state.c b/Core/save_state.c index 6befa2d..e33dbf1 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_B: return true; case GB_MODEL_CGB_C: return true; case GB_MODEL_CGB_D: return true; case GB_MODEL_CGB_E: return true; @@ -647,7 +648,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_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; case GB_MODEL_CGB_E: bess_core.full_model = BE32('CCE '); break;