1 /* FCE Ultra - NES/Famicom Emulator
3 * Copyright notice for this file:
4 * Copyright (C) 2002 Xodnizel 2006 CaH4e3
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.
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.
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
20 * It seems that 162/163/164 mappers are the same mapper with just different
21 * mapper modes enabled or disabled in software or hardware, need more nanjing
27 static uint8 laststrobe, trigger;
29 static uint8 *WRAM=NULL;
30 static uint32 WRAMSIZE;
32 static void(*WSync)(void);
34 static SFORMAT StateRegs[]=
36 {&laststrobe, 1, "STB"},
43 const EEPROM_interface eeprom_interface. =
47 "*110", // read 1 10 aaaaaaaaa
48 "*101", // write 1 01 aaaaaaaaa dddddddd
49 . "*10000xxxxxxx", // lock 1 00 00xxxxxxx
50 "*10011xxxxxxx", // unlock 1 00 11xxxxxxx
55 static const EEPROM_interface *intf;
57 static int serial_count = 0;
58 static u8 serial_buffer[SERIAL_BUFFER_LENGTH];
60 static int eeprom_data_bits;
61 static int eeprom_clock_count;
62 static int eeprom_read_address;
63 static u8 *eeprom_data;
66 static int locked = 1;
67 static int sending = 0;
68 static int reset_line = ASSERT_LINE;
69 static int clock_line = ASSERT_LINE;
70 static int reset_delay;
72 void EEPROM_Init(u8 *data, u8 bit)
76 intf = &eeprom_interface_93C46_8;
78 intf = &eeprom_interface_93C46_16;
86 static int EEPROM_command_match(const char *buf, const char *cmd, int len)
88 if ( cmd == 0 ) return 0;
89 if ( len == 0 ) return 0;
103 if (b != c) return 0;
117 if (b == c) { cmd++; }
118 else { buf++; len--; }
127 static void EEPROM_write(int bit)
129 if (serial_count >= SERIAL_BUFFER_LENGTH-1)
134 serial_buffer[serial_count++] = (bit ? '1' : '0');
135 serial_buffer[serial_count] = 0;
137 if ( (serial_count > intf->address_bits) &&
138 EEPROM_command_match((char*)serial_buffer,intf->cmd_read,(int)strlen((char*)serial_buffer)-intf->address_bits) )
143 for (i = serial_count-intf->address_bits;i < serial_count;i++)
146 if (serial_buffer[i] == '1') address |= 1;
148 if (intf->data_bits == 16)
149 eeprom_data_bits = (eeprom_data[2*address+0] << 8) + eeprom_data[2*address+1];
151 eeprom_data_bits = eeprom_data[address];
152 eeprom_read_address = address;
153 eeprom_clock_count = 0;
157 else if ( (serial_count > intf->address_bits) &&
158 EEPROM_command_match((char*)serial_buffer,intf->cmd_erase,(int)strlen((char*)serial_buffer)-intf->address_bits) )
163 for (i = serial_count-intf->address_bits;i < serial_count;i++)
166 if (serial_buffer[i] == '1') address |= 1;
171 if (intf->data_bits == 16)
173 eeprom_data[2*address+0] = 0x00;
174 eeprom_data[2*address+1] = 0x00;
177 eeprom_data[address] = 0x00;
182 else if ( (serial_count > (intf->address_bits + intf->data_bits)) &&
183 EEPROM_command_match((char*)serial_buffer,intf->cmd_write,(int)strlen((char*)serial_buffer)-(intf->address_bits + intf->data_bits)) )
188 for (i = serial_count-intf->data_bits-intf->address_bits;i < (serial_count-intf->data_bits);i++)
191 if (serial_buffer[i] == '1') address |= 1;
194 for (i = serial_count-intf->data_bits;i < serial_count;i++)
197 if (serial_buffer[i] == '1') data |= 1;
201 if (intf->data_bits == 16)
203 eeprom_data[2*address+0] = data >> 8;
204 eeprom_data[2*address+1] = data & 0xff;
207 eeprom_data[address] = data;
212 else if ( EEPROM_command_match((char*)serial_buffer,intf->cmd_lock,(int)strlen((char*)serial_buffer)) )
217 else if ( EEPROM_command_match((char*)serial_buffer,intf->cmd_unlock,(int)strlen((char*)serial_buffer)) )
224 static void EEPROM_reset()
228 reset_delay = intf->reset_delay;
231 void EEPROM_set_cs_line(int state)
235 if (reset_line != CLEAR_LINE)
239 void EEPROM_set_clock_line(int state)
241 if (state == PULSE_LINE || (clock_line == CLEAR_LINE && state != CLEAR_LINE))
243 if (reset_line == CLEAR_LINE)
247 if (eeprom_clock_count == intf->data_bits)
249 if(intf->enable_multi_read)
251 eeprom_read_address = (eeprom_read_address + 1) & ((1 << intf->address_bits) - 1);
252 if (intf->data_bits == 16)
253 eeprom_data_bits = (eeprom_data[2*eeprom_read_address+0] << 8) + eeprom_data[2*eeprom_read_address+1];
255 eeprom_data_bits = eeprom_data[eeprom_read_address];
256 eeprom_clock_count = 0;
263 eeprom_data_bits = (eeprom_data_bits << 1) | 1;
264 eeprom_clock_count++;
275 void EEPROM_write_bit(int bit)
280 int EEPROM_read_bit(void)
285 res = (eeprom_data_bits >> intf->data_bits) & 1;
301 static void Sync(void)
303 setprg8r(0x10,0x6000,0);
304 setprg32(0x8000,(reg[0]<<4)|(reg[1]&0xF));
308 static void StateRestore(int version)
313 static DECLFR(ReadLow)
317 case 0x5100: return reg[2]|reg[0]|reg[1]|reg[3]^0xff; break;
318 case 0x5500: if(trigger)
319 return reg[2]|reg[1]; // Lei Dian Huang Bi Ka Qiu Chuan Shuo (NJ046) may broke other games
326 static void M163HB(void)
335 else if(scanline==127)
341 if(scanline>=127) // Hu Lu Jin Gang (NJ039) (Ch) [!] don't like it
359 case 0x5100: reg[0]=V; WSync(); break;
360 case 0x5000: reg[1]=V; WSync(); break;
361 case 0x5300: reg[2]=V; break;
362 case 0x5200: reg[3]=V; WSync(); break;
366 static void Power(void)
370 SetWriteHandler(0x5000,0x5FFF,Write);
371 SetReadHandler(0x6000,0xFFFF,CartBR);
372 SetWriteHandler(0x6000,0x7FFF,CartBW);
376 static void Close(void)
383 void Mapper164_Init(CartInfo *info)
390 WRAM=(uint8*)FCEU_gmalloc(WRAMSIZE);
391 SetupCartPRGMapping(0x10,WRAM,WRAMSIZE,1);
392 AddExState(WRAM, WRAMSIZE, 0, "WRAM");
396 info->SaveGame[0]=WRAM;
397 info->SaveGameLen[0]=WRAMSIZE;
400 GameStateRestore=StateRestore;
401 AddExState(&StateRegs, ~0, 0, 0);
404 static DECLFW(Write2)
413 }else if(A==0x5100&&V==6) //damn thoose protected games
418 case 0x5200: reg[0]=V; WSync(); break;
419 case 0x5000: reg[1]=V; WSync(); if(!(reg[1]&0x80)&&(scanline<128)) setchr8(0); /* setchr8(0); */ break;
420 case 0x5300: reg[2]=V; break;
421 case 0x5100: reg[3]=V; WSync(); break;
425 static void Power2(void)
429 SetReadHandler(0x5000,0x5FFF,ReadLow);
430 SetWriteHandler(0x5000,0x5FFF,Write2);
431 SetReadHandler(0x6000,0xFFFF,CartBR);
432 SetWriteHandler(0x6000,0x7FFF,CartBW);
436 void Mapper163_Init(CartInfo *info)
441 GameHBIRQHook=M163HB;
444 WRAM=(uint8*)FCEU_gmalloc(WRAMSIZE);
445 SetupCartPRGMapping(0x10,WRAM,WRAMSIZE,1);
446 AddExState(WRAM, WRAMSIZE, 0, "WRAM");
450 info->SaveGame[0]=WRAM;
451 info->SaveGameLen[0]=WRAMSIZE;
453 GameStateRestore=StateRestore;
454 AddExState(&StateRegs, ~0, 0, 0);
457 static void Sync3(void)
460 setprg8r(0x10,0x6000,0);
463 case 2: setprg32(0x8000,(reg[0]&0xc)|(reg[1]&2)|((reg[2]&0xf)<<4)); break;
465 case 3: setprg32(0x8000,(reg[0]&0xc)|(reg[2]&0xf)<<4); break;
467 case 6: setprg32(0x8000,(reg[0]&0xe)|((reg[1]>>1)&1)|((reg[2]&0xf)<<4)); break;
469 case 7: setprg32(0x8000,(reg[0]&0xf)|((reg[2]&0xf)<<4)); break;
473 static DECLFW(Write3)
475 // FCEU_printf("bs %04x %02x\n",A,V);
480 static void Power3(void)
486 SetWriteHandler(0x5000,0x5FFF,Write3);
487 SetReadHandler(0x6000,0xFFFF,CartBR);
488 SetWriteHandler(0x6000,0x7FFF,CartBW);
492 void UNLFS304_Init(CartInfo *info)
499 WRAM=(uint8*)FCEU_gmalloc(WRAMSIZE);
500 SetupCartPRGMapping(0x10,WRAM,WRAMSIZE,1);
501 AddExState(WRAM, WRAMSIZE, 0, "WRAM");
505 info->SaveGame[0]=WRAM;
506 info->SaveGameLen[0]=WRAMSIZE;
509 GameStateRestore=StateRestore;
510 AddExState(&StateRegs, ~0, 0, 0);