From fe4b519b23a2783ec62a2b4608ba5f5577a95cc9 Mon Sep 17 00:00:00 2001 From: MooglyGuy Date: Wed, 24 Mar 2021 16:52:02 +0100 Subject: bus/megadrive: Improved Sonic & Knuckles "lock on" slot handling. [Ryan Holtz] * Fixed saving in Sonic 3 & Knuckles. * Made all MD cartridge types able to be locked on. --- src/devices/bus/megadrive/sk.cpp | 60 ++++++++++++++++++++++++++++++---------- src/devices/bus/megadrive/sk.h | 4 +++ 2 files changed, 49 insertions(+), 15 deletions(-) diff --git a/src/devices/bus/megadrive/sk.cpp b/src/devices/bus/megadrive/sk.cpp index 02cf46b0151..5e0a970c320 100644 --- a/src/devices/bus/megadrive/sk.cpp +++ b/src/devices/bus/megadrive/sk.cpp @@ -5,19 +5,15 @@ Sonic & Knuckles pass-thorugh cart emulation - - TODO: currently we only support loading of base carts with no bankswitch or protection... - shall we support other as well? + TODO: This could potentially make use of memory views. Investigate. ***********************************************************************************************************/ - - - #include "emu.h" #include "sk.h" #include "rom.h" +#include "md_carts.h" //------------------------------------------------- @@ -31,6 +27,7 @@ md_rom_sk_device::md_rom_sk_device(const machine_config &mconfig, device_type ty : device_t(mconfig, type, tag, owner, clock) , device_md_cart_interface(mconfig, *this) , m_exp(*this, "subslot") + , m_map_upper(false) { } @@ -42,16 +39,33 @@ md_rom_sk_device::md_rom_sk_device(const machine_config &mconfig, const char *ta void md_rom_sk_device::device_start() { + save_item(NAME(m_map_upper)); } + +void md_rom_sk_device::device_reset() +{ + m_map_upper = false; +} + + /*------------------------------------------------- mapper specific handlers -------------------------------------------------*/ uint16_t md_rom_sk_device::read(offs_t offset) { - if (m_exp->m_cart != nullptr && m_exp->m_cart->get_rom_base() != nullptr && offset >= 0x200000/2 && offset < (0x200000 + m_exp->m_cart->get_rom_size())/2) - return m_exp->m_cart->m_rom[offset - 0x200000/2]; + if (m_map_upper) + { + if (offset >= 0x300000/2) + return m_rom[MD_ADDR(offset)]; // Sonic 2 patch ROM + else if (m_exp->m_cart != nullptr && m_exp->m_cart->get_rom_base() != nullptr && offset >= 0x200000/2) + return m_exp->m_cart->read(offset); // Sonic 3 ROM pass-through or FRAM + } + + if (m_exp->m_cart != nullptr && m_exp->m_cart->get_rom_base() != nullptr && offset >= 0x200000/2) + return m_exp->m_cart->read(offset - 0x200000/2); + if (offset < 0x400000/2) return m_rom[MD_ADDR(offset)]; else @@ -60,18 +74,34 @@ uint16_t md_rom_sk_device::read(offs_t offset) void md_rom_sk_device::write(offs_t offset, uint16_t data, uint16_t mem_mask) { - // TODO: implement pass-through NVRAM accessing for Sonic 3 cart, for Sonic 3 & Knuckles saving feature + if (m_exp->m_cart == nullptr || m_exp->m_cart->get_rom_base() == nullptr) + return; + + if (m_map_upper) + m_exp->m_cart->write(offset, data, mem_mask); + else if (offset >= 0x200000/2) + m_exp->m_cart->write(offset - 0x200000/2, data, mem_mask); +} + +uint16_t md_rom_sk_device::read_a13(offs_t offset) +{ + if (m_exp->m_cart != nullptr && m_exp->m_cart->get_rom_base() != nullptr) + return m_exp->m_cart->read_a13(offset); + return 0xffff; } +void md_rom_sk_device::write_a13(offs_t offset, uint16_t data) +{ + if (m_exp->m_cart != nullptr && m_exp->m_cart->get_rom_base() != nullptr) + m_exp->m_cart->write_a13(offset, data); + if (offset == 0xf0/2) + m_map_upper = BIT(data, 0); +} static void sk_sub_cart(device_slot_interface &device) { - device.option_add_internal("rom", MD_STD_ROM); - device.option_add_internal("rom_svp", MD_STD_ROM); - device.option_add_internal("rom_sram", MD_ROM_SRAM); - device.option_add_internal("rom_sramsafe", MD_ROM_SRAM); - device.option_add_internal("rom_fram", MD_ROM_FRAM); -// add all types?? + // Inherit all cartridge types; cartridges <= 2 megabytes in size are mirrored into the upper 2 megabytes by the Sonic & Knuckles cartridge. + md_cart(device); } diff --git a/src/devices/bus/megadrive/sk.h b/src/devices/bus/megadrive/sk.h index cd297c41a7f..c51abce9430 100644 --- a/src/devices/bus/megadrive/sk.h +++ b/src/devices/bus/megadrive/sk.h @@ -21,6 +21,7 @@ protected: md_rom_sk_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, uint32_t clock); virtual void device_start() override; + virtual void device_reset() override; // device-level overrides virtual void device_add_mconfig(machine_config &config) override; @@ -28,9 +29,12 @@ protected: // reading and writing virtual uint16_t read(offs_t offset) override; virtual void write(offs_t offset, uint16_t data, uint16_t mem_mask = ~0) override; + virtual uint16_t read_a13(offs_t offset) override; + virtual void write_a13(offs_t offset, uint16_t data) override; private: required_device m_exp; + bool m_map_upper; }; -- cgit v1.2.3