From 5c3e5258197bda4015cac50f74145cf804095be1 Mon Sep 17 00:00:00 2001 From: roee toledano Date: Fri, 22 Mar 2024 03:46:25 +0200 Subject: [PATCH] fixed some MMC3 open bus error just returned 0 instead of open bus --- src/bus.cpp | 2 +- src/main.cpp | 2 +- src/mapper_n_cart.cpp | 8 ++++---- src/mappers/mmc3_4.cpp | 22 ++++++++++------------ src/ppu.cpp | 4 ++-- 5 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/bus.cpp b/src/bus.cpp index 637088d..6e6dee1 100644 --- a/src/bus.cpp +++ b/src/bus.cpp @@ -13,7 +13,7 @@ namespace roee_nes { this->controller_1 = controller_1; this->controller_2 = controller_2; init_palette(palette_path); - + cpu->bus = this; ppu->bus = this; diff --git a/src/main.cpp b/src/main.cpp index cdcbf0c..0954b45 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -26,7 +26,7 @@ uint16_t emulator_tick(Bus* bus) { } int main() { - const std::string rom_path = "roms/METROID.nes"; + const std::string rom_path = "roms/SMB2.nes"; const std::string palette_path = "ntscpalette.pal"; Controller* const controller_1 = new Controller(); diff --git a/src/mapper_n_cart.cpp b/src/mapper_n_cart.cpp index 7632587..b242fd9 100644 --- a/src/mapper_n_cart.cpp +++ b/src/mapper_n_cart.cpp @@ -106,8 +106,8 @@ namespace roee_nes { return; // do nothing! } - Save_RAM::Save_RAM(const std::string& save_file_path) - : save_data({ 0 }), path(save_file_path){ + Save_RAM::Save_RAM(const std::string& save_file_path) + : save_data({ 0 }), path(save_file_path) { if (std::filesystem::exists(path)) { std::cout << "USER INFO: Save game data found. Reading from save data\n"; @@ -130,11 +130,11 @@ namespace roee_nes { } uint8_t Save_RAM::mapper_read(const uint16_t addr) { - return save_data[addr % 0x2000]; + return save_data[addr % 0x6000]; } void Save_RAM::mapper_write(const uint16_t addr, const uint8_t data) { - save_data[addr % 0x2000] = data; + save_data[addr % 0x6000] = data; } diff --git a/src/mappers/mmc3_4.cpp b/src/mappers/mmc3_4.cpp index a077e20..6a5c0b8 100644 --- a/src/mappers/mmc3_4.cpp +++ b/src/mappers/mmc3_4.cpp @@ -21,10 +21,12 @@ namespace roee_nes { chr_bank_num = (chr_read_mem->size() / (1 * KILOBYTE)) - 2; prg_bank_num = (cart->prg_rom.size() / (8 * KILOBYTE)); - if (cart->header.flag_6.parsed.prg_ram == 1) + if (cart->header.flag_6.parsed.prg_ram == 1) { + std::cout << "USER INFO: Game has PRG RAM\n"; save_ram = new Save_RAM(cart->rom_path + ".sav"); - else + } else { save_ram = nullptr; + } prg_bank[0] = 0; prg_bank[1] = 0; @@ -38,7 +40,8 @@ namespace roee_nes { void MMC3_4::cpu_write(uint16_t addr, uint8_t data) { if ((0x6000 <= addr) && (addr <= 0x7fff)) { - save_ram->mapper_write(addr, data); + if (save_ram != nullptr) + save_ram->mapper_write(addr, data); } else if ((0x8000 <= addr) && (addr <= 0x9ffe)) { if ((addr % 2) == 0) { // write to low reg bank_select.comp.even.select = data & 0b0000'0111; @@ -114,18 +117,13 @@ namespace roee_nes { // << " irq reload: " << (int)irq_reload << " \n" // << " set irq: " << (int)set_irq << " \n" // << "-------------------\n"; - if ((0x6000 <= addr) && (addr <= 0x7fff)) { - return save_ram->mapper_read(addr); - } else if ((0x6000 <= addr) && (addr <= 0x7fff)) { - if ((prg_ram_protect & 0b1000'0000) == 0) - return open_bus_data; - else - return save_data[addr % 0x6000]; // handle prg_ram + if ((0x6000 <= addr) && (addr <= 0x7fff) && (!(prg_ram_protect & 0b1000'0000)) && (save_ram != nullptr)) { + return save_data[addr % 0x6000]; // handle prg_ram } else if ((0x8000 <= addr) && (addr <= 0xffff)) { update_prg(addr); return cart->prg_rom[(final_bank * 8 * KILOBYTE) + final_addr]; - } else { - return open_bus_data; + } else { // otherwise return open bus + return 0; // should return open bus but fuck it, no MMC3 game uses this anyway } } diff --git a/src/ppu.cpp b/src/ppu.cpp index f4f9ede..069b042 100644 --- a/src/ppu.cpp +++ b/src/ppu.cpp @@ -23,9 +23,9 @@ namespace roee_nes { vblank_scanline(); if ((bus->mapper->mapper_number == 4) - // && (!checked) + && (!checked) ) - ((MMC3_4*)bus->mapper)->clock_irq(); + check_mmc3_irq_conditions(); increment_cycle(1); }