updated bords/mappers/stuff to 0.98.15, lots of them got broken, asmcore support...
[fceu.git] / boards / 8237.c
1 /* FCE Ultra - NES/Famicom Emulator
2  *
3  * Copyright notice for this file:
4  *  Copyright (C) 2005 CaH4e3
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "mapinc.h"
22 #include "mmc3.h"
23
24 static uint8 cmdin;
25 static uint8 UNL8237_perm[8] = {0, 2, 6, 1, 7, 3, 4, 5};
26
27 static void UNL8237CW(uint32 A, uint8 V)
28 {
29   setchr1(A,((EXPREGS[1]&4)<<6)|V);
30 }
31
32 static void UNL8237PW(uint32 A, uint8 V)
33 {
34   if(EXPREGS[0]&0x80)
35   {
36     if(EXPREGS[0]&0x20)
37       setprg32(0x8000,(EXPREGS[0]&0xF)>>1);
38     else
39     {
40       setprg16(0x8000,(EXPREGS[0]&0x1F));
41       setprg16(0xC000,(EXPREGS[0]&0x1F));
42     }
43   }
44   else
45     setprg8(A,V&0x3F);
46 }
47
48 static DECLFW(UNL8237Write)
49 {
50   if((A&0xF000)==0xF000)
51     IRQCount=V;
52   else if((A&0xF000)==0xE000)
53     X6502_IRQEnd(FCEU_IQEXT);
54   else switch(A&0xE001)
55   {
56     case 0x8000: setmirror(((V|(V>>7))&1)^1); break;
57     case 0xA000: MMC3_CMDWrite(0x8000,(V&0xC0)|(UNL8237_perm[V&7])); cmdin=1; break;
58     case 0xC000: if(cmdin)
59                  {
60                    MMC3_CMDWrite(0x8001,V);
61                    cmdin=0;
62                  }
63                  break;
64   }
65 }
66
67 static DECLFW(UNL8237ExWrite)
68 {
69   switch(A)
70   {
71     case 0x5000: EXPREGS[0]=V; FixMMC3PRG(MMC3_cmd); break;
72     case 0x5001: EXPREGS[1]=V; FixMMC3CHR(MMC3_cmd); break;
73   }
74 }
75
76 static void UNL8237Power(void)
77 {
78   IRQa=1;
79   EXPREGS[0]=EXPREGS[1]=0;
80   GenMMC3Power();
81   SetWriteHandler(0x8000,0xFFFF,UNL8237Write);
82   SetWriteHandler(0x5000,0x7FFF,UNL8237ExWrite);
83 }
84
85 void UNL8237_Init(CartInfo *info)
86 {
87   GenMMC3_Init(info, 256, 256, 0, 0);
88   cwrap=UNL8237CW;
89   pwrap=UNL8237PW;
90   info->Power=UNL8237Power;
91   AddExState(EXPREGS, 3, 0, "EXPR");
92   AddExState(&cmdin, 1, 0, "CMDIN");
93 }