1 /* FCE Ultra - NES/Famicom Emulator
2  *
3  * Copyright notice for this file:
4  *  Copyright (C) 2012
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * Pegasus 5-in-1 (Golden Five) (Unl)
21  */
22 
23 #include "mapinc.h"
24 
25 static uint8 preg[2];
26 static uint8 *WRAM = NULL;
27 static uint32 WRAMSIZE;
28 
29 static SFORMAT StateRegs[] =
30 {
31 	{ preg, 2, "PREG" },
32 	{ 0 }
33 };
34 
Sync(void)35 static void Sync(void) {
36 	setprg8r(0x10, 0x6000, 0);
37 	setprg16(0x8000, preg[0]);
38 	setprg16(0xC000, preg[1]);
39 	setchr8(0);
40 }
41 
DECLFW(M104WriteBank)42 static DECLFW(M104WriteBank) {
43 	if ((V & 8) > 0) {
44 		preg[0]  = ((V << 4) & 0x70) | (preg[0] & 0x0F);
45 		preg[1]  = ((V << 4) & 0x70) | 0x0F;
46 		Sync();
47 	}
48 }
49 
DECLFW(M104WritePreg)50 static DECLFW(M104WritePreg) {
51 	preg[0] = (preg[0] & 0x70) | (V & 0x0F);
52 	Sync();
53 }
54 
M104Close(void)55 static void M104Close(void) {
56 	if (WRAM)
57 		FCEU_gfree(WRAM);
58 	WRAM = NULL;
59 }
60 
M104Power(void)61 static void M104Power(void) {
62 	preg[1] = 0x0F;
63 	Sync();
64 	SetReadHandler(0x6000, 0x7fff, CartBR);
65 	SetWriteHandler(0x6000, 0x7fff, CartBW);
66 	SetWriteHandler(0x8000, 0x9FFF, M104WriteBank);
67 	SetWriteHandler(0xC000, 0xFFFF, M104WritePreg);
68 	SetReadHandler(0x8000, 0xFFFF, CartBR);
69 	setmirror(MI_V);
70 	FCEU_CheatAddRAM(WRAMSIZE >> 10, 0x6000, WRAM);
71 }
72 
StateRestore(int version)73 static void StateRestore(int version) {
74 	Sync();
75 }
76 
Mapper104_Init(CartInfo * info)77 void Mapper104_Init(CartInfo *info) {
78 	info->Power = M104Power;
79 	info->Close = M104Close;
80 	AddExState(&StateRegs, ~0, 0, 0);
81 
82 	WRAMSIZE = 8192;
83 	WRAM = (uint8*)FCEU_gmalloc(WRAMSIZE);
84 	SetupCartPRGMapping(0x10, WRAM, WRAMSIZE, 1);
85 	AddExState(WRAM, WRAMSIZE, 0, "WRAM");
86 	if (info->battery) {
87 		info->SaveGame[0] = WRAM;
88 		info->SaveGameLen[0] = WRAMSIZE;
89 	}
90 
91 	GameStateRestore = StateRestore;
92 }
93