1 /* FCEUmm - NES/Famicom Emulator
2  *
3  * Copyright notice for this file:
4  * Copyright (C) 2020 negativeExponent
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (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 
21 /* NES 2.0 Mapper 390 - Realtec 8031 */
22 
23 #include "mapinc.h"
24 
25 static uint8 regs[2];
26 static uint8 dipswitch;
27 
28 static SFORMAT StateRegs[] =
29 {
30 	{ &regs, 2, "REG" },
31 	{ &dipswitch, 1, "DPSW" },
32 	{ 0 }
33 };
34 
Sync(void)35 static void Sync(void) {
36 	switch ((regs[1] >> 4) & 3) {
37 	case 0:
38 	case 1:
39 		/* UNROM */
40 		setprg16(0x8000, regs[1]);
41 		setprg16(0xC000, regs[1] | 7);
42 		break;
43 	case 2:
44 		/* Maybe unused, NROM-256? */
45 		setprg32(0x8000, regs[1] >> 1);
46 		break;
47 	case 3:
48 		/* NROM-128 */
49 		setprg16(0x8000, regs[1]);
50 		setprg16(0xC000, regs[1]);
51 		break;
52 	}
53 	setchr8(regs[0]);
54 	setmirror(((regs[0] & 0x20) >> 5) ^ 1);
55 }
56 
DECLFR(M390Read)57 static DECLFR(M390Read) {
58 	uint8 ret = CartBR(A);
59 	if ((regs[1] & 0x30) == 0x10)
60 		ret |= dipswitch;
61 	return ret;
62 }
63 
DECLFW(M390Write)64 static DECLFW(M390Write) {
65 	regs[(A >> 14) & 1] = A & 0x3F;
66 	Sync();
67 }
68 
M390Power(void)69 static void M390Power(void) {
70 	regs[0] = 0;
71 	regs[1] = 0;
72 	dipswitch = 11; /* hard-coded 150-in-1 menu */
73 	Sync();
74 	SetReadHandler(0x8000, 0xffff, M390Read);
75 	SetWriteHandler(0x8000, 0xffff, M390Write);
76 }
77 
M390Reset(void)78 static void M390Reset(void) {
79 	dipswitch = 11; /* hard-coded 150-in-1 menu */
80 	Sync();
81 }
82 
StateRestore(int version)83 static void StateRestore(int version) {
84 	Sync();
85 }
86 
Mapper390_Init(CartInfo * info)87 void Mapper390_Init(CartInfo *info) {
88 	info->Reset = M390Reset;
89 	info->Power = M390Power;
90 	GameStateRestore = StateRestore;
91 	AddExState(&StateRegs, ~0, 0, 0);
92 }
93