| 1 | /* FCE Ultra - NES/Famicom Emulator\r |
| 2 | *\r |
| 3 | * Copyright notice for this file:\r |
| 4 | * Copyright (C) 2005 CaH4e3\r |
| 5 | *\r |
| 6 | * This program is free software; you can redistribute it and/or modify\r |
| 7 | * it under the terms of the GNU General Public License as published by\r |
| 8 | * the Free Software Foundation; either version 2 of the License, or\r |
| 9 | * (at your option) any later version.\r |
| 10 | *\r |
| 11 | * This program is distributed in the hope that it will be useful,\r |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of\r |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r |
| 14 | * GNU General Public License for more details.\r |
| 15 | *\r |
| 16 | * You should have received a copy of the GNU General Public License\r |
| 17 | * along with this program; if not, write to the Free Software\r |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\r |
| 19 | */\r |
| 20 | \r |
| 21 | #include "mapinc.h"\r |
| 22 | #include "mmc3.h"\r |
| 23 | \r |
| 24 | // brk is a system call in *nix, and is an illegal variable name - soules\r |
| 25 | static uint8 chrcmd[8], prg0, prg1, bbrk, mirr, swap;\r |
| 26 | static SFORMAT StateRegs[]=\r |
| 27 | {\r |
| 28 | {chrcmd, 8, "CHRCMD"},\r |
| 29 | {&prg0, 1, "PRG0"},\r |
| 30 | {&prg1, 1, "PRG1"},\r |
| 31 | {&bbrk, 1, "BRK"},\r |
| 32 | {&mirr, 1, "MIRR"},\r |
| 33 | {&swap, 1, "SWAP"},\r |
| 34 | {0}\r |
| 35 | };\r |
| 36 | \r |
| 37 | static void Sync(void)\r |
| 38 | {\r |
| 39 | int i;\r |
| 40 | setprg8(0x8000,prg0);\r |
| 41 | setprg8(0xA000,prg1);\r |
| 42 | setprg8(0xC000,~1);\r |
| 43 | setprg8(0xE000,~0);\r |
| 44 | for(i=0; i<8; i++)\r |
| 45 | setchr1(i<<10,chrcmd[i]);\r |
| 46 | setmirror(mirr^1);\r |
| 47 | }\r |
| 48 | \r |
| 49 | static void UNLSL1632CW(uint32 A, uint8 V)\r |
| 50 | {\r |
| 51 | int cbase=(MMC3_cmd&0x80)<<5;\r |
| 52 | int page0=(bbrk&0x08)<<5;\r |
| 53 | int page1=(bbrk&0x20)<<3;\r |
| 54 | int page2=(bbrk&0x80)<<1;\r |
| 55 | setchr1(cbase^0x0000,page0|(DRegBuf[0]&(~1)));\r |
| 56 | setchr1(cbase^0x0400,page0|DRegBuf[0]|1);\r |
| 57 | setchr1(cbase^0x0800,page0|(DRegBuf[1]&(~1)));\r |
| 58 | setchr1(cbase^0x0C00,page0|DRegBuf[1]|1);\r |
| 59 | setchr1(cbase^0x1000,page1|DRegBuf[2]);\r |
| 60 | setchr1(cbase^0x1400,page1|DRegBuf[3]);\r |
| 61 | setchr1(cbase^0x1800,page2|DRegBuf[4]);\r |
| 62 | setchr1(cbase^0x1c00,page2|DRegBuf[5]);\r |
| 63 | }\r |
| 64 | \r |
| 65 | static DECLFW(UNLSL1632CMDWrite)\r |
| 66 | {\r |
| 67 | if(A==0xA131)\r |
| 68 | {\r |
| 69 | bbrk=V;\r |
| 70 | }\r |
| 71 | if(bbrk&2)\r |
| 72 | {\r |
| 73 | FixMMC3PRG(MMC3_cmd);\r |
| 74 | FixMMC3CHR(MMC3_cmd);\r |
| 75 | if(A<0xC000)\r |
| 76 | MMC3_CMDWrite(A,V);\r |
| 77 | else\r |
| 78 | MMC3_IRQWrite(A,V);\r |
| 79 | }\r |
| 80 | else\r |
| 81 | {\r |
| 82 | if((A>=0xB000)&&(A<=0xE003))\r |
| 83 | {\r |
| 84 | int ind=((((A&2)|(A>>10))>>1)+2)&7;\r |
| 85 | int sar=((A&1)<<2);\r |
| 86 | chrcmd[ind]=(chrcmd[ind]&(0xF0>>sar))|((V&0x0F)<<sar);\r |
| 87 | }\r |
| 88 | else\r |
| 89 | switch(A&0xF003)\r |
| 90 | {\r |
| 91 | case 0x8000: prg0=V; break;\r |
| 92 | case 0xA000: prg1=V; break;\r |
| 93 | case 0x9000: mirr=V&1; break;\r |
| 94 | }\r |
| 95 | Sync();\r |
| 96 | }\r |
| 97 | }\r |
| 98 | \r |
| 99 | static void StateRestore(int version)\r |
| 100 | {\r |
| 101 | if(bbrk&2)\r |
| 102 | {\r |
| 103 | FixMMC3PRG(MMC3_cmd);\r |
| 104 | FixMMC3CHR(MMC3_cmd);\r |
| 105 | }\r |
| 106 | else\r |
| 107 | Sync();\r |
| 108 | }\r |
| 109 | \r |
| 110 | static void UNLSL1632Power(void)\r |
| 111 | {\r |
| 112 | GenMMC3Power();\r |
| 113 | SetReadHandler(0x8000,0xFFFF,CartBR);\r |
| 114 | SetWriteHandler(0x4100,0xFFFF,UNLSL1632CMDWrite);\r |
| 115 | }\r |
| 116 | \r |
| 117 | void UNLSL1632_Init(CartInfo *info)\r |
| 118 | {\r |
| 119 | GenMMC3_Init(info, 256, 512, 0, 0);\r |
| 120 | cwrap=UNLSL1632CW;\r |
| 121 | info->Power=UNLSL1632Power;\r |
| 122 | GameStateRestore=StateRestore;\r |
| 123 | AddExState(&StateRegs, ~0, 0, 0);\r |
| 124 | }\r |