From a7aabca6180b7d60bf49023dfb88526951bc2b39 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Tue, 3 Jul 2018 21:43:46 +0300 Subject: [PATCH] Starting to add CGB-C support --- Core/gb.c | 15 +++++++++++++++ Core/gb.h | 3 ++- Core/memory.c | 27 +++++++++++++++++++++------ 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/Core/gb.c b/Core/gb.c index 2c3dfbb..c3fa4cc 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -473,6 +473,21 @@ static void reset_ram(GB_gameboy_t *gb) } break; #endif + + 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] = (random() | random() | random() | random()) & 0xFF; + } + } + break; + } + + for (unsigned i = 0; i < sizeof(gb->extra_oam); i++) { + gb->extra_oam[i] = (random() & 0xFF); } } diff --git a/Core/gb.h b/Core/gb.h index 31840d9..3254624 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -43,7 +43,7 @@ typedef enum { // GB_MODEL_CGB_0 = 0x200, // GB_MODEL_CGB_A = 0x201, // GB_MODEL_CGB_B = 0x202, - // GB_MODEL_CGB_C = 0x203, + GB_MODEL_CGB_C = 0x203, // GB_MODEL_CGB_D = 0x204, GB_MODEL_CGB_E = 0x205, GB_MODEL_AGB = 0x206, @@ -301,6 +301,7 @@ struct GB_gameboy_internal_s { /* Misc state */ bool infrared_input; GB_printer_t printer; + uint8_t extra_oam[0xff00 - 0xfea0]; ); /* DMA and HDMA */ diff --git a/Core/memory.c b/Core/memory.c index ee9ad18..5f6a89a 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -244,13 +244,21 @@ static uint8_t read_high_memory(GB_gameboy_t *gb, uint16_t addr) return gb->oam[addr & 0xFF]; } - /* Unusable. CGB results are verified, but DMG results were tested on a SGB2 */ - /* Also, writes to this area are not emulated */ - if ((gb->io_registers[GB_IO_STAT] & 0x3) >= 2) { /* Seems to be disabled in Modes 2 and 3 */ + if (gb->oam_read_blocked) { return 0xFF; } - if (GB_is_cgb(gb)) { - return (addr & 0xF0) | ((addr >> 4) & 0xF); + + switch (gb->model) { + case GB_MODEL_CGB_E: + case GB_MODEL_AGB: + return (addr & 0xF0) | ((addr >> 4) & 0xF); + + case GB_MODEL_CGB_C: + addr &= ~0x18; + return gb->extra_oam[addr - 0xfea0]; + + default: + ; } } @@ -535,10 +543,17 @@ static void write_high_memory(GB_gameboy_t *gb, uint16_t addr, uint8_t value) if (addr < 0xFEA0) { gb->oam[addr & 0xFF] = value; } + switch (gb->model) { + case GB_MODEL_CGB_C: + addr &= ~0x18; + gb->extra_oam[addr - 0xfea0] = value; + break; + default: + break; + } return; } - /* Todo: This is writable, but glitchy, on CGB-B and CGB-D. */ if (addr < 0xFEA0) { if (gb->accessed_oam_row == 0xa0) { for (unsigned i = 0; i < 8; i++) {