| | 1 | /* FCE Ultra - NES/Famicom Emulator\r |
| | 2 | *\r |
| | 3 | * Copyright notice for this file:\r |
| | 4 | * Copyright (C) 2006 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\r |
| | 19 | *\r |
| | 20 | */\r |
| | 21 | \r |
| | 22 | #include "mapinc.h"\r |
| | 23 | \r |
| | 24 | static uint16 latchea;\r |
| | 25 | static uint8 latched;\r |
| | 26 | static uint8 *WRAM=NULL;\r |
| | 27 | static uint32 WRAMSIZE;\r |
| | 28 | static SFORMAT StateRegs[]=\r |
| | 29 | {\r |
| | 30 | {&latchea, 2, "AREG"},\r |
| | 31 | {&latched, 1, "DREG"},\r |
| | 32 | {0}\r |
| | 33 | };\r |
| | 34 | \r |
| | 35 | static void Sync(void)\r |
| | 36 | {\r |
| | 37 | int i;\r |
| | 38 | setmirror(((latched>>6)&1)^1);\r |
| | 39 | switch(latchea)\r |
| | 40 | {\r |
| | 41 | case 0x8000:\r |
| | 42 | for(i=0;i<4;i++)\r |
| | 43 | setprg8(0x8000+(i<<13),(((latched&0x7F)<<1)+i)^(latched>>7));\r |
| | 44 | break;\r |
| | 45 | case 0x8002:\r |
| | 46 | for(i=0;i<4;i++)\r |
| | 47 | setprg8(0x8000+(i<<13),((latched&0x7F)<<1)+(latched>>7));\r |
| | 48 | break;\r |
| | 49 | case 0x8001:\r |
| | 50 | case 0x8003:\r |
| | 51 | for(i=0;i<4;i++)\r |
| | 52 | {\r |
| | 53 | unsigned int b;\r |
| | 54 | b=latched&0x7F;\r |
| | 55 | if(i>=2 && !(latchea&0x2))\r |
| | 56 | i=0x7F;\r |
| | 57 | setprg8(0x8000+(i<<13),(i&1)+((b<<1)^(latched>>7)));\r |
| | 58 | }\r |
| | 59 | break;\r |
| | 60 | }\r |
| | 61 | }\r |
| | 62 | \r |
| | 63 | static DECLFW(M15Write)\r |
| | 64 | {\r |
| | 65 | latchea=A;\r |
| | 66 | latched=V;\r |
| | 67 | Sync();\r |
| | 68 | }\r |
| | 69 | \r |
| | 70 | static void StateRestore(int version)\r |
| | 71 | {\r |
| | 72 | Sync();\r |
| | 73 | }\r |
| | 74 | \r |
| | 75 | static void M15Power(void)\r |
| | 76 | {\r |
| | 77 | latchea=0x8000;\r |
| | 78 | latched=0;\r |
| | 79 | setchr8(0);\r |
| | 80 | setprg8r(0x10,0x6000,0);\r |
| | 81 | SetReadHandler(0x6000,0x7FFF,CartBR);\r |
| | 82 | SetWriteHandler(0x6000,0x7FFF,CartBW);\r |
| | 83 | SetWriteHandler(0x8000,0xFFFF,M15Write);\r |
| | 84 | SetReadHandler(0x8000,0xFFFF,CartBR);\r |
| | 85 | Sync();\r |
| | 86 | }\r |
| | 87 | \r |
| | 88 | static void M15Reset(void)\r |
| | 89 | {\r |
| | 90 | latchea=0x8000;\r |
| | 91 | latched=0;\r |
| | 92 | Sync();\r |
| | 93 | }\r |
| | 94 | \r |
| | 95 | static void M15Close(void)\r |
| | 96 | {\r |
| | 97 | if(WRAM)\r |
| | 98 | FCEU_gfree(WRAM);\r |
| | 99 | WRAM=NULL;\r |
| | 100 | }\r |
| | 101 | \r |
| | 102 | void Mapper15_Init(CartInfo *info)\r |
| | 103 | {\r |
| | 104 | info->Power=M15Power;\r |
| | 105 | info->Reset=M15Reset;\r |
| | 106 | info->Close=M15Close;\r |
| | 107 | GameStateRestore=StateRestore;\r |
| | 108 | WRAMSIZE=8192;\r |
| | 109 | WRAM=(uint8*)FCEU_gmalloc(WRAMSIZE);\r |
| | 110 | SetupCartPRGMapping(0x10,WRAM,WRAMSIZE,1);\r |
| | 111 | if(info->battery)\r |
| | 112 | {\r |
| | 113 | info->SaveGame[0]=WRAM;\r |
| | 114 | info->SaveGameLen[0]=WRAMSIZE;\r |
| | 115 | }\r |
| | 116 | AddExState(WRAM, WRAMSIZE, 0, "WRAM");\r |
| | 117 | AddExState(&StateRegs, ~0, 0, 0);\r |
| | 118 | }\r |
| | 119 | \r |