diff --git a/builds/genesis_plus_gx_libretro.dll b/builds/genesis_plus_gx_libretro.dll index 41091f1..8cd0738 100644 Binary files a/builds/genesis_plus_gx_libretro.dll and b/builds/genesis_plus_gx_libretro.dll differ diff --git a/builds/genplus_cube.dol b/builds/genplus_cube.dol index 654635d..5cdf633 100644 Binary files a/builds/genplus_cube.dol and b/builds/genplus_cube.dol differ diff --git a/builds/genplus_wii.dol b/builds/genplus_wii.dol index 635c7bb..1e0fb8d 100644 Binary files a/builds/genplus_wii.dol and b/builds/genplus_wii.dol differ diff --git a/gx/gui/cheats.c b/gx/gui/cheats.c index 15cb365..722e04e 100644 --- a/gx/gui/cheats.c +++ b/gx/gui/cheats.c @@ -3,7 +3,7 @@ * * Genesis Plus GX Cheats menu * - * Copyright Eke-Eke (2010-2019) + * Copyright Eke-Eke (2010-2022) * * Redistribution and use of this code or any derivative works are permitted * provided that the following conditions are met: @@ -456,7 +456,33 @@ static void apply_cheats(void) { if (cheatlist[i].enable) { - if (cheatlist[i].address < cart.romsize) + /* detect Work RAM patch */ + if (cheatlist[i].address >= 0xFF0000) + { + /* add RAM patch */ + cheatIndexes[maxRAMcheats++] = i; + } + + /* check if Mega-CD game is running */ + else if ((system_hw == SYSTEM_MCD) && !scd.cartridge.boot) + { + /* detect PRG-RAM patch (Sub-CPU side) */ + if (cheatlist[i].address < 0x80000) + { + /* add RAM patch */ + cheatIndexes[maxRAMcheats++] = i; + } + + /* detect Word-RAM patch (Main-CPU side)*/ + else if ((cheatlist[i].address >= 0x200000) && (cheatlist[i].address < 0x240000)) + { + /* add RAM patch */ + cheatIndexes[maxRAMcheats++] = i; + } + } + + /* detect cartridge ROM patch */ + else if (cheatlist[i].address < cart.romsize) { if ((system_hw & SYSTEM_PBC) == SYSTEM_MD) { @@ -489,20 +515,20 @@ static void apply_cheats(void) } } } - else if (cheatlist[i].address >= 0xFF0000) - { - /* add RAM patch */ - cheatIndexes[maxRAMcheats++] = i; - } } } } static void clear_cheats(void) { - int i = maxcheats; + int i; - /* disable cheats in reversed order in case the same address is used by multiple patches */ + /* no ROM patches with Mega-CD games */ + if ((system_hw == SYSTEM_MCD) && !scd.cartridge.boot) + return; + + /* disable cheats in reversed order in case the same address is used by multiple ROM patches */ + i = maxcheats; while (i > 0) { if (cheatlist[i-1].enable) @@ -1501,6 +1527,8 @@ void CheatLoad(void) ****************************************************************************/ void RAMCheatUpdate(void) { + uint8 *base; + uint32 mask; int index, cnt = maxRAMcheats; while (cnt) @@ -1508,16 +1536,36 @@ void RAMCheatUpdate(void) /* get cheat index */ index = cheatIndexes[--cnt]; + /* detect destination RAM */ + switch ((cheatlist[index].address >> 20) & 0xf) + { + case 0x0: /* Mega-CD PRG-RAM (512 KB) */ + base = scd.prg_ram; + mask = 0x7fffe; + break; + + case 0x2: /* Mega-CD 2M Word-RAM (256 KB) */ + base = scd.word_ram_2M; + mask = 0x3fffe; + break; + + default: /* Work-RAM (64 KB) */ + base = work_ram; + mask = 0xfffe; + break; + } + /* apply RAM patch */ if (cheatlist[index].data & 0xFF00) { /* word patch */ - *(u16 *)(work_ram + (cheatlist[index].address & 0xFFFE)) = cheatlist[index].data; + *(u16 *)(base + (cheatlist[index].address & mask)) = cheatlist[index].data; } else { /* byte patch */ - work_ram[cheatlist[index].address & 0xFFFF] = cheatlist[index].data; + mask |= 1; + base[cheatlist[index].address & mask] = cheatlist[index].data; } } } diff --git a/gx/gui/cheats.h b/gx/gui/cheats.h index 56cba90..66ab147 100644 --- a/gx/gui/cheats.h +++ b/gx/gui/cheats.h @@ -3,7 +3,7 @@ * * Genesis Plus GX Cheats menu * - * Copyright Eke-Eke (2010-2019) + * Copyright Eke-Eke (2010-2022) * * Redistribution and use of this code or any derivative works are permitted * provided that the following conditions are met: diff --git a/libretro/libretro.c b/libretro/libretro.c index 3b547d4..8bbd1f4 100644 --- a/libretro/libretro.c +++ b/libretro/libretro.c @@ -2245,12 +2245,32 @@ static void apply_cheats(void) { if (cheatlist[i].address < cart.romsize) { - if ((system_hw & SYSTEM_PBC) == SYSTEM_MD) + /* detect Work RAM patch */ + if (cheatlist[i].address >= 0xFF0000) { - /* patch ROM data */ - cheatlist[i].old = *(uint16_t *)(cart.rom + (cheatlist[i].address & 0xFFFFFE)); - *(uint16_t *)(cart.rom + (cheatlist[i].address & 0xFFFFFE)) = cheatlist[i].data; + /* add RAM patch */ + cheatIndexes[maxRAMcheats++] = i; } + + /* check if Mega-CD game is running */ + else if ((system_hw == SYSTEM_MCD) && !scd.cartridge.boot) + { + /* detect PRG-RAM patch (Sub-CPU side) */ + if (cheatlist[i].address < 0x80000) + { + /* add RAM patch */ + cheatIndexes[maxRAMcheats++] = i; + } + + /* detect Word-RAM patch (Main-CPU side)*/ + else if ((cheatlist[i].address >= 0x200000) && (cheatlist[i].address < 0x240000)) + { + /* add RAM patch */ + cheatIndexes[maxRAMcheats++] = i; + } + } + + /* detect cartridge ROM patch */ else { /* add ROM patch */ @@ -2273,19 +2293,20 @@ static void apply_cheats(void) } } } - else if (cheatlist[i].address >= 0xFF0000) - { - /* add RAM patch */ - cheatIndexes[maxRAMcheats++] = i; - } } } } static void clear_cheats(void) { - int i = maxcheats; - /* disable cheats in reversed order in case the same address is used by multiple patches */ + int i; + + /* no ROM patches with Mega-CD games */ + if ((system_hw == SYSTEM_MCD) && !scd.cartridge.boot) + return; + + /* disable cheats in reversed order in case the same address is used by multiple ROM patches */ + i = maxcheats; while (i > 0) { if (cheatlist[i-1].enable) @@ -2322,21 +2343,45 @@ static void clear_cheats(void) ****************************************************************************/ static void RAMCheatUpdate(void) { + uint8_t *base; + uint32_t mask; int index, cnt = maxRAMcheats; + while (cnt) { /* get cheat index */ index = cheatIndexes[--cnt]; + + /* detect destination RAM */ + switch ((cheatlist[index].address >> 20) & 0xf) + { + case 0x0: /* Mega-CD PRG-RAM (512 KB) */ + base = scd.prg_ram; + mask = 0x7fffe; + break; + + case 0x2: /* Mega-CD 2M Word-RAM (256 KB) */ + base = scd.word_ram_2M; + mask = 0x3fffe; + break; + + default: /* Work-RAM (64 KB) */ + base = work_ram; + mask = 0xfffe; + break; + } + /* apply RAM patch */ if (cheatlist[index].data & 0xFF00) { - /* 16-bit patch */ - *(uint16_t *)(work_ram + (cheatlist[index].address & 0xFFFE)) = cheatlist[index].data; + /* word patch */ + *(uint16_t *)(base + (cheatlist[index].address & mask)) = cheatlist[index].data; } else { - /* 8-bit patch */ - work_ram[cheatlist[index].address & 0xFFFF] = cheatlist[index].data; + /* byte patch */ + mask |= 1; + base[cheatlist[index].address & mask] = cheatlist[index].data; } } }