merge mapper code from FCEUX
[fceu.git] / mappers / 83.c
CommitLineData
c62d2810 1/* FCE Ultra - NES/Famicom Emulator
2 *
3 * Copyright notice for this file:
d97315ac 4 * Copyright (C) 2002 Xodnizel
c62d2810 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
386f5371 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
c62d2810 19 */
20
21#include "mapinc.h"
22
386f5371 23static uint8 is2kbank, isnot2kbank, dipreg;
d97315ac 24
25/*void Mapper83_init(void)
26{
27
28}
29*/
386f5371 30static void m83IRQHook(int a)
c62d2810 31{
32 if(IRQa)
33 {
34 IRQCount-=a;
35 if(IRQCount<0)
36 {
d97315ac 37 X6502_IRQBegin(FCEU_IQEXT);
c62d2810 38 IRQa=0;
39 IRQCount=0xFFFF;
40 }
41 }
42}
43
44static DECLFW(wrlow)
45{
46 mapbyte4[A&3]=V;
47}
48
49static DECLFR(rdlow)
50{
51 return mapbyte4[A&3];
52}
53
386f5371 54static DECLFR(rd5000)
55{
56 return dipreg;
57}
58
c62d2810 59static void m83prg(void)
60{
d97315ac 61 ROM_BANK16(0x8000,(mapbyte1[0]&0x3F));
c62d2810 62 ROM_BANK16(0xC000,(mapbyte1[0]&0x30)|0xF);
63}
64
65static void m83chr(void)
66{
386f5371 67 if(is2kbank&&!isnot2kbank)
68 {
69 VROM_BANK2(0x0000,mapbyte2[0]);
70 VROM_BANK2(0x0800,mapbyte2[1]);
71 VROM_BANK2(0x1000,mapbyte2[6]);
72 VROM_BANK2(0x1800,mapbyte2[7]);
73 }
74 else
75 {
e2d0dd92 76 int x;
77 for(x=0;x<8;x++)
78 VROM_BANK1(x*0x400,mapbyte2[x]|((mapbyte1[0]&0x30)<<4));
386f5371 79 }
c62d2810 80}
81
82static DECLFW(Mapper83_write)
83{
84 //printf("$%04x:$%02x\n",A,V);
85 switch(A)
86 {
386f5371 87 case 0x8000: is2kbank = 1;
c62d2810 88 case 0xB000:
89 case 0xB0FF:
90 case 0xB1FF:
91 {
92 mapbyte1[0]=V;
93 m83prg();
94 m83chr();
95 }
96 break;
97 case 0x8100:
e2d0dd92 98 mapbyte3[0]=V&0x80;
c62d2810 99 switch(V&0x3)
100 {
101 case 0x00:MIRROR_SET2(1);break;
102 case 0x01:MIRROR_SET2(0);break;
103 case 0x02:onemir(0);break;
104 case 0x03:onemir(1);break;
105 }
106 break;
d97315ac 107 case 0x8200:IRQCount&=0xFF00;IRQCount|=V;X6502_IRQEnd(FCEU_IQEXT);break;
e2d0dd92 108 case 0x8201:IRQa=mapbyte3[0];IRQCount&=0xFF;IRQCount|=V<<8;break;
d97315ac 109 case 0x8300:ROM_BANK8(0x8000,V);break;
110 case 0x8301:ROM_BANK8(0xA000,V);break;
111 case 0x8302:ROM_BANK8(0xC000,V);break;
c62d2810 112 case 0x8310:mapbyte2[0]=V;m83chr();break;
113 case 0x8311:mapbyte2[1]=V;m83chr();break;
386f5371 114 case 0x8312:mapbyte2[2]=V;isnot2kbank = 1;m83chr();break; // 2 mappers in one :)
115 case 0x8313:mapbyte2[3]=V;isnot2kbank = 1;m83chr();break;
116 case 0x8314:mapbyte2[4]=V;isnot2kbank = 1;m83chr();break;
117 case 0x8315:mapbyte2[5]=V;isnot2kbank = 1;m83chr();break;
c62d2810 118 case 0x8316:mapbyte2[6]=V;m83chr();break;
119 case 0x8317:mapbyte2[7]=V;m83chr();break;
d97315ac 120 case 0x8318:mapbyte1[1]=V;m83prg();break;
c62d2810 121 }
122// printf("$%04x:$%02x, $%04x\n",A,V,X.PC.W);
d97315ac 123
c62d2810 124}
125
386f5371 126static void m83Reset(void)
127{
128 dipreg^=1;
129}
130
c62d2810 131void Mapper83_init(void)
132{
386f5371 133 is2kbank = 0;
134 isnot2kbank = 0;
135 dipreg = 0;
c62d2810 136 ROM_BANK8(0xc000,0x1e);
137 ROM_BANK8(0xe000,0x1f);
138
139 MapIRQHook=m83IRQHook;
386f5371 140 MapperReset=m83Reset;
c62d2810 141
386f5371 142 SetReadHandler(0x5000,0x5000,rd5000); // title scren dip switch
c62d2810 143 SetReadHandler(0x5100,0x5103,rdlow);
144 SetWriteHandler(0x5100,0x5103,wrlow);
145 SetWriteHandler(0x8000,0xffff,Mapper83_write);
d97315ac 146 mapbyte1[1]=0xF;
c62d2810 147}