Mappers 252/253: rewrite to use modular VRC2/4.

This commit is contained in:
NewRisingSun
2025-04-10 19:16:27 +02:00
committed by LibretroAdmin
parent 98d567bae1
commit 5d398fbb5d
3 changed files with 103 additions and 287 deletions

View File

@@ -1,135 +0,0 @@
/* FCE Ultra - NES/Famicom Emulator
*
* Copyright notice for this file:
* Copyright (C) 2009 CaH4e3
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "mapinc.h"
static uint8 creg[8], preg[2];
static int32 IRQa, IRQCount, IRQClock, IRQLatch;
static uint8 *WRAM = NULL;
static uint32 WRAMSIZE;
static uint8 *CHRRAM = NULL;
static uint32 CHRRAMSIZE;
static SFORMAT StateRegs[] =
{
{ creg, 8, "CREG" },
{ preg, 2, "PREG" },
{ &IRQa, 4, "IRQA" },
{ &IRQCount, 4, "IRQC" },
{ &IRQLatch, 4, "IRQL" },
{ &IRQClock, 4, "IRQK" },
{ 0 }
};
static void Sync(void) {
uint8 i;
setprg8r(0x10, 0x6000, 0);
setprg8(0x8000, preg[0]);
setprg8(0xa000, preg[1]);
setprg8(0xc000, ~1);
setprg8(0xe000, ~0);
for (i = 0; i < 8; i++)
if ((creg[i] == 6) || (creg[i] == 7))
setchr1r(0x10, i << 10, creg[i] & 1);
else
setchr1(i << 10, creg[i]);
}
static DECLFW(M252Write) {
if ((A >= 0xB000) && (A <= 0xEFFF)) {
uint8 ind = ((((A & 8) | (A >> 8)) >> 3) + 2) & 7;
uint8 sar = A & 4;
creg[ind] = (creg[ind] & (0xF0 >> sar)) | ((V & 0x0F) << sar);
Sync();
} else
switch (A & 0xF00C) {
case 0x8000:
case 0x8004:
case 0x8008:
case 0x800C: preg[0] = V; Sync(); break;
case 0xA000:
case 0xA004:
case 0xA008:
case 0xA00C: preg[1] = V; Sync(); break;
case 0xF000: X6502_IRQEnd(FCEU_IQEXT); IRQLatch &= 0xF0; IRQLatch |= V & 0xF; break;
case 0xF004: X6502_IRQEnd(FCEU_IQEXT); IRQLatch &= 0x0F; IRQLatch |= V << 4; break;
case 0xF008: X6502_IRQEnd(FCEU_IQEXT); IRQClock = 0; IRQCount = IRQLatch; IRQa = V & 2; break;
}
}
static void M252Power(void) {
Sync();
SetReadHandler(0x6000, 0x7FFF, CartBR);
SetWriteHandler(0x6000, 0x7FFF, CartBW);
SetReadHandler(0x8000, 0xFFFF, CartBR);
SetWriteHandler(0x8000, 0xFFFF, M252Write);
FCEU_CheatAddRAM(WRAMSIZE >> 10, 0x6000, WRAM);
}
static void M252IRQ(int a) {
#define LCYCS 341
if (IRQa) {
IRQClock += a * 3;
if (IRQClock >= LCYCS) {
while (IRQClock >= LCYCS) {
IRQClock -= LCYCS;
IRQCount++;
if (IRQCount & 0x100) {
X6502_IRQBegin(FCEU_IQEXT);
IRQCount = IRQLatch;
}
}
}
}
}
static void M252Close(void) {
if (WRAM)
FCEU_gfree(WRAM);
if (CHRRAM)
FCEU_gfree(CHRRAM);
WRAM = CHRRAM = NULL;
}
static void StateRestore(int version) {
Sync();
}
void Mapper252_Init(CartInfo *info) {
info->Power = M252Power;
info->Close = M252Close;
MapIRQHook = M252IRQ;
CHRRAMSIZE = 2048;
CHRRAM = (uint8*)FCEU_gmalloc(CHRRAMSIZE);
SetupCartCHRMapping(0x10, CHRRAM, CHRRAMSIZE, 1);
AddExState(CHRRAM, CHRRAMSIZE, 0, "CRAM");
WRAMSIZE = 8192;
WRAM = (uint8*)FCEU_gmalloc(WRAMSIZE);
SetupCartPRGMapping(0x10, WRAM, WRAMSIZE, 1);
AddExState(WRAM, WRAMSIZE, 0, "WRAM");
if (info->battery) {
info->SaveGame[0] = WRAM;
info->SaveGameLen[0] = WRAMSIZE;
}
GameStateRestore = StateRestore;
AddExState(&StateRegs, ~0, 0, 0);
}

103
src/boards/252_253.c Normal file
View File

@@ -0,0 +1,103 @@
/* FCE Ultra - NES/Famicom Emulator
*
* Copyright notice for this file:
* Copyright (C) 2025 NewRisingSun
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "mapinc.h"
#include "vrc2and4.h"
static uint8 *CHRRAM;
static uint32 CHRRAMSize;
static uint8 mask;
static uint8 compare;
extern uint32 RefreshAddr;
writefunc writePPU;
static SFORMAT stateRegs[] ={
{ &mask, 1, "CHRM" },
{ &compare, 1, "CHRC" },
{ 0 }
};
static void sync () {
int bank;
VRC24_syncWRAM(0);
VRC24_syncPRG(0x01F, 0x000);
for (bank =0; bank <8; bank++) setchr1r((VRC24_chr[bank] &mask) ==compare? 0x10: 0x00, bank <<10, VRC24_chr[bank]);
VRC24_syncMirror();
}
static DECLFW(Mapper252_253_interceptPPUWrite) {
if (~RefreshAddr &0x2000) {
int bank =VRC24_chr[RefreshAddr >>10 &7];
switch(bank) {
case 0x88: mask =0xFC; compare =0x4C; VRC24_Sync(); break;
case 0xC2: mask =0xFE; compare =0x7C; VRC24_Sync(); break;
case 0xC8: mask =0xFE; compare =0x04; VRC24_Sync(); break;
}
}
writePPU(A, V);
}
void Mapper252_power (void) {
mask =0xFE;
compare =0x06;
VRC24_power();
writePPU =GetWriteHandler(0x2007);
SetWriteHandler(0x2007, 0x2007, Mapper252_253_interceptPPUWrite);
}
void Mapper253_power (void) {
mask =0xFE; /* There are two board revisions, the earlier one with a non-switchable mask/compare FE/04 and a later switchable one that starts with FC/28 */
compare =0x04;
VRC24_power();
writePPU =GetWriteHandler(0x2007);
SetWriteHandler(0x2007, 0x2007, Mapper252_253_interceptPPUWrite);
}
void Mapper252_253_close(void) {
if (CHRRAM) {
FCEU_gfree(CHRRAM);
CHRRAM =NULL;
}
}
void Mapper252_Init (CartInfo *info) {
VRC24_init(info, sync, 0x4, 0x8, 1, 1, 0);
info->Power =Mapper252_power;
info->Close =Mapper252_253_close;
AddExState(stateRegs, ~0, 0, 0);
CHRRAMSize =info->iNES2? (info->CHRRamSize +info->CHRRamSaveSize): 2048;
CHRRAM =(uint8*)FCEU_gmalloc(CHRRAMSize);
AddExState(CHRRAM, CHRRAMSize, 0, "CRAM");
SetupCartCHRMapping(0x10, CHRRAM, CHRRAMSize, 1);
}
void Mapper253_Init (CartInfo *info) {
VRC24_init(info, sync, 0x4, 0x8, 1, 1, 0);
info->Power =Mapper253_power;
info->Close =Mapper252_253_close;
AddExState(stateRegs, ~0, 0, 0);
CHRRAMSize =info->iNES2? (info->CHRRamSize +info->CHRRamSaveSize): 2048;
CHRRAM =(uint8*)FCEU_gmalloc(CHRRAMSize);
AddExState(CHRRAM, CHRRAMSize, 0, "CRAM");
SetupCartCHRMapping(0x10, CHRRAM, CHRRAMSize, 1);
}

View File

@@ -1,152 +0,0 @@
/* FCE Ultra - NES/Famicom Emulator
*
* Copyright notice for this file:
* Copyright (C) 2009 CaH4e3
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "mapinc.h"
static uint8 chrlo[8], chrhi[8], prg[2], mirr, vlock;
static int32 IRQa, IRQCount, IRQLatch, IRQClock;
static uint8 *WRAM = NULL;
static uint32 WRAMSIZE;
static uint8 *CHRRAM = NULL;
static uint32 CHRRAMSIZE;
static SFORMAT StateRegs[] =
{
{ chrlo, 8, "CHRL" },
{ chrhi, 8, "CHRH" },
{ prg, 2, "PRGR" },
{ &mirr, 1, "MIRR" },
{ &vlock, 1, "VLCK" },
{ &IRQa, 4, "IRQA" },
{ &IRQCount, 4, "IRQC" },
{ &IRQLatch, 4, "IRQL" },
{ &IRQClock, 4, "IRQK" },
{ 0 }
};
static void Sync(void) {
uint8 i;
setprg8r(0x10, 0x6000, 0);
setprg8(0x8000, prg[0]);
setprg8(0xa000, prg[1]);
setprg8(0xc000, ~1);
setprg8(0xe000, ~0);
for (i = 0; i < 8; i++) {
uint32 chr = chrlo[i] | (chrhi[i] << 8);
if (((chrlo[i] == 4) || (chrlo[i] == 5)) && !vlock)
setchr1r(0x10, i << 10, chr & 1);
else
setchr1(i << 10, chr);
}
switch (mirr) {
case 0: setmirror(MI_V); break;
case 1: setmirror(MI_H); break;
case 2: setmirror(MI_0); break;
case 3: setmirror(MI_1); break;
}
}
static DECLFW(M253Write) {
if ((A >= 0xB000) && (A <= 0xE00C)) {
uint8 ind = ((((A & 8) | (A >> 8)) >> 3) + 2) & 7;
uint8 sar = A & 4;
uint8 clo = (chrlo[ind] & (0xF0 >> sar)) | ((V & 0x0F) << sar);
chrlo[ind] = clo;
if (ind == 0) {
if (clo == 0xc8)
vlock = 0;
else if (clo == 0x88)
vlock = 1;
}
if (sar)
chrhi[ind] = V >> 4;
Sync();
} else
switch (A) {
case 0x8010: prg[0] = V; Sync(); break;
case 0xA010: prg[1] = V; Sync(); break;
case 0x9400: mirr = V & 3; Sync(); break;
case 0xF000: X6502_IRQEnd(FCEU_IQEXT); IRQLatch &= 0xF0; IRQLatch |= V & 0xF; break;
case 0xF004: X6502_IRQEnd(FCEU_IQEXT); IRQLatch &= 0x0F; IRQLatch |= V << 4; break;
case 0xF008: X6502_IRQEnd(FCEU_IQEXT); IRQClock = 0; IRQCount = IRQLatch; IRQa = V & 2; break;
}
}
static void M253Power(void) {
vlock = 0;
Sync();
SetReadHandler(0x6000, 0x7FFF, CartBR);
SetWriteHandler(0x6000, 0x7FFF, CartBW);
SetReadHandler(0x8000, 0xFFFF, CartBR);
SetWriteHandler(0x8000, 0xFFFF, M253Write);
FCEU_CheatAddRAM(WRAMSIZE >> 10, 0x6000, WRAM);
}
static void M253Close(void) {
if (WRAM)
FCEU_gfree(WRAM);
if (CHRRAM)
FCEU_gfree(CHRRAM);
WRAM = CHRRAM = NULL;
}
static void M253IRQ(int a) {
#define LCYCS 341
if (IRQa) {
IRQClock += a * 3;
if (IRQClock >= LCYCS) {
while (IRQClock >= LCYCS) {
IRQClock -= LCYCS;
IRQCount++;
if (IRQCount & 0x100) {
X6502_IRQBegin(FCEU_IQEXT);
IRQCount = IRQLatch;
}
}
}
}
}
static void StateRestore(int version) {
Sync();
}
void Mapper253_Init(CartInfo *info) {
info->Power = M253Power;
info->Close = M253Close;
MapIRQHook = M253IRQ;
GameStateRestore = StateRestore;
CHRRAMSIZE = 2048;
CHRRAM = (uint8*)FCEU_gmalloc(CHRRAMSIZE);
SetupCartCHRMapping(0x10, CHRRAM, CHRRAMSIZE, 1);
AddExState(CHRRAM, CHRRAMSIZE, 0, "CRAM");
WRAMSIZE = 8192;
WRAM = (uint8*)FCEU_gmalloc(WRAMSIZE);
SetupCartPRGMapping(0x10, WRAM, WRAMSIZE, 1);
AddExState(WRAM, WRAMSIZE, 0, "WRAM");
if (info->battery) {
info->SaveGame[0] = WRAM;
info->SaveGameLen[0] = WRAMSIZE;
}
AddExState(&StateRegs, ~0, 0, 0);
}