| 1 | /* FCE Ultra - NES/Famicom Emulator |
| 2 | * |
| 3 | * Copyright notice for this file: |
| 4 | * Copyright (C) 2002 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 | |
| 23 | static uint8 prg_reg; |
| 24 | static uint8 chr_reg; |
| 25 | |
| 26 | static uint8 sim0reg, sim0bit, sim0byte, sim0parity, sim0bcnt; |
| 27 | static uint16 sim0data; |
| 28 | static uint8 sim0array[128] = |
| 29 | { |
| 30 | 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, |
| 31 | 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, |
| 32 | 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, |
| 33 | 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, |
| 34 | 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, |
| 35 | 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, |
| 36 | 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, |
| 37 | 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0xAA, |
| 38 | }; |
| 39 | |
| 40 | static SFORMAT StateRegs[]= |
| 41 | { |
| 42 | {&prg_reg, 1, "PREG"}, |
| 43 | {&chr_reg, 1, "CREG"}, |
| 44 | {0} |
| 45 | }; |
| 46 | |
| 47 | static void Sync(void) |
| 48 | { |
| 49 | setprg32(0x8000, prg_reg); |
| 50 | setchr8(chr_reg); |
| 51 | } |
| 52 | |
| 53 | static void StateRestore(int version) |
| 54 | { |
| 55 | Sync(); |
| 56 | } |
| 57 | |
| 58 | static DECLFW(M216WriteHi) |
| 59 | { |
| 60 | // FCEU_printf("%04x:%04x\n",A,V); |
| 61 | prg_reg=A&1; |
| 62 | chr_reg=(A&0x0E)>>1; |
| 63 | Sync(); |
| 64 | } |
| 65 | |
| 66 | static DECLFW(M216Write5000) |
| 67 | { |
| 68 | // FCEU_printf("WRITE: %04x:%04x\n",A,V); |
| 69 | sim0reg=V; |
| 70 | if(!sim0reg) |
| 71 | { |
| 72 | sim0bit=sim0byte=sim0parity=0; |
| 73 | sim0data=sim0array[0]; |
| 74 | sim0bcnt=0x80; |
| 75 | } |
| 76 | else if(sim0reg&0x20) |
| 77 | { |
| 78 | sim0bcnt=0x20; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | static DECLFR(M216Read5000) |
| 83 | { |
| 84 | if(sim0reg&0x60) |
| 85 | { |
| 86 | sim0reg=(sim0reg^(sim0reg<<1))&0x40; |
| 87 | return sim0reg; |
| 88 | } |
| 89 | else |
| 90 | { |
| 91 | uint8 sim0out=0; |
| 92 | if(sim0bit<8) |
| 93 | { |
| 94 | // sim0data=((sim0array[sim0byte]<<(sim0bit))&0x80)>>1; |
| 95 | sim0out=(sim0data&1)<<6; |
| 96 | sim0data>>=1; |
| 97 | sim0bit++; |
| 98 | sim0parity+=sim0data; |
| 99 | } |
| 100 | else if(sim0bit==8) |
| 101 | { |
| 102 | sim0bit++; |
| 103 | sim0out=sim0parity&1; |
| 104 | } |
| 105 | else if(sim0bit==9) |
| 106 | { |
| 107 | if(sim0byte==sim0bcnt) |
| 108 | sim0out=0x60; |
| 109 | else |
| 110 | { |
| 111 | sim0bit=0; |
| 112 | sim0byte++; |
| 113 | sim0data=sim0array[sim0byte]; |
| 114 | sim0out=0; |
| 115 | } |
| 116 | } |
| 117 | // FCEU_printf("READ: %04x (%04x-%02x,%04x)\n",A,X.PC,sim0out,sim0byte); |
| 118 | return sim0out; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | static void Power(void) |
| 123 | { |
| 124 | prg_reg = 0; |
| 125 | chr_reg = 0; |
| 126 | Sync(); |
| 127 | SetReadHandler(0x8000,0xFFFF,CartBR); |
| 128 | SetWriteHandler(0x8000,0xFFFF,M216WriteHi); |
| 129 | SetWriteHandler(0x5000,0x5000,M216Write5000); |
| 130 | SetReadHandler(0x5000,0x5000,M216Read5000); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | void Mapper216_Init(CartInfo *info) |
| 135 | { |
| 136 | info->Power=Power; |
| 137 | GameStateRestore=StateRestore; |
| 138 | AddExState(&StateRegs, ~0, 0, 0); |
| 139 | } |