merge mappers from FCEU-mm
[fceu.git] / boards / 164.c
index 22589ec..26cb8bc 100644 (file)
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * It seems that 162/163/164 mappers are the same mapper with just different
+ * mapper modes enabled or disabled in software or hardware, need more nanjing
+ * carts
  */
 
 #include "mapinc.h"
 
-static uint8 cmd, laststrobe, trigger;
-static uint8 DRegs[8];
+static uint8 laststrobe, trigger;
+static uint8 reg[8];
+static uint8 *WRAM=NULL;
+static uint32 WRAMSIZE;
+
+static void(*WSync)(void);
+
 static SFORMAT StateRegs[]=
 {
-  {&cmd, 1, "CMD"},
   {&laststrobe, 1, "STB"},
   {&trigger, 1, "TRG"},
-  {DRegs, 8, "DREG"},
+  {reg, 8, "REGS"},
   {0}
 };
 
+/*
+const EEPROM_interface eeprom_interface. =
+{
+       9,                                 // address bits 9
+       8,                                 // data bits    8
+       "*110",                    // read         1 10 aaaaaaaaa
+       "*101",                    // write        1 01 aaaaaaaaa dddddddd
+.      "*10000xxxxxxx",   // lock         1 00 00xxxxxxx
+       "*10011xxxxxxx",   // unlock       1 00 11xxxxxxx
+       1,
+       5
+};
+
+static const EEPROM_interface *intf;
+
+static int serial_count = 0;
+static u8  serial_buffer[SERIAL_BUFFER_LENGTH];
+
+static int eeprom_data_bits;
+static int eeprom_clock_count;
+static int eeprom_read_address;
+static u8  *eeprom_data;
+
+static int latch = 0;
+static int locked = 1;
+static int sending = 0;
+static int reset_line = ASSERT_LINE;
+static int clock_line = ASSERT_LINE;
+static int reset_delay;
+
+void EEPROM_Init(u8 *data, u8 bit)
+{
+       eeprom_data = data;
+       if(bit == 8)
+               intf = &eeprom_interface_93C46_8;
+       else
+               intf = &eeprom_interface_93C46_16;
+}
+
+u8 *EEPROM_GetData()
+{
+       return eeprom_data;
+}
+
+static int EEPROM_command_match(const char *buf, const char *cmd, int len)
+{
+       if ( cmd == 0 ) return 0;
+       if ( len == 0 ) return 0;
+
+       for (;len>0;)
+       {
+               char b = *buf;
+               char c = *cmd;
+
+               if ((b==0) || (c==0))
+                       return (b==c);
+
+               switch ( c )
+               {
+               case '0':
+               case '1':
+                       if (b != c)     return 0;
+               case 'X':
+               case 'x':
+                       buf++;
+                       len--;
+                       cmd++;
+                       break;
+
+               case '*':
+                       c = cmd[1];
+                       switch( c )
+                       {
+                       case '0':
+                       case '1':
+                               if (b == c)     {       cmd++;                  }
+                               else            {       buf++;  len--;  }
+                               break;
+                       default:        return 0;
+                       }
+               }
+       }
+       return (*cmd==0);
+}
+
+static void EEPROM_write(int bit)
+{
+       if (serial_count >= SERIAL_BUFFER_LENGTH-1)
+       {
+               return;
+       }
+
+       serial_buffer[serial_count++] = (bit ? '1' : '0');
+       serial_buffer[serial_count] = 0;
+
+       if ( (serial_count > intf->address_bits) &&
+             EEPROM_command_match((char*)serial_buffer,intf->cmd_read,(int)strlen((char*)serial_buffer)-intf->address_bits) )
+       {
+               int i,address;
+
+               address = 0;
+               for (i = serial_count-intf->address_bits;i < serial_count;i++)
+               {
+                       address <<= 1;
+                       if (serial_buffer[i] == '1') address |= 1;
+               }
+               if (intf->data_bits == 16)
+                       eeprom_data_bits = (eeprom_data[2*address+0] << 8) + eeprom_data[2*address+1];
+               else
+                       eeprom_data_bits = eeprom_data[address];
+               eeprom_read_address = address;
+               eeprom_clock_count = 0;
+               sending = 1;
+               serial_count = 0;
+       }
+       else if ( (serial_count > intf->address_bits) &&
+                  EEPROM_command_match((char*)serial_buffer,intf->cmd_erase,(int)strlen((char*)serial_buffer)-intf->address_bits) )
+       {
+               int i,address;
+
+               address = 0;
+               for (i = serial_count-intf->address_bits;i < serial_count;i++)
+               {
+                       address <<= 1;
+                       if (serial_buffer[i] == '1') address |= 1;
+               }
+
+               if (locked == 0)
+               {
+                       if (intf->data_bits == 16)
+                       {
+                               eeprom_data[2*address+0] = 0x00;
+                               eeprom_data[2*address+1] = 0x00;
+                       }
+                       else
+                               eeprom_data[address] = 0x00;
+               }
+               else
+               serial_count = 0;
+       }
+       else if ( (serial_count > (intf->address_bits + intf->data_bits)) &&
+                  EEPROM_command_match((char*)serial_buffer,intf->cmd_write,(int)strlen((char*)serial_buffer)-(intf->address_bits + intf->data_bits)) )
+       {
+               int i,address,data;
+
+               address = 0;
+               for (i = serial_count-intf->data_bits-intf->address_bits;i < (serial_count-intf->data_bits);i++)
+               {
+                       address <<= 1;
+                       if (serial_buffer[i] == '1') address |= 1;
+               }
+               data = 0;
+               for (i = serial_count-intf->data_bits;i < serial_count;i++)
+               {
+                       data <<= 1;
+                       if (serial_buffer[i] == '1') data |= 1;
+               }
+               if (locked == 0)
+               {
+                       if (intf->data_bits == 16)
+                       {
+                               eeprom_data[2*address+0] = data >> 8;
+                               eeprom_data[2*address+1] = data & 0xff;
+                       }
+                       else
+                               eeprom_data[address] = data;
+               }
+               else
+               serial_count = 0;
+       }
+       else if ( EEPROM_command_match((char*)serial_buffer,intf->cmd_lock,(int)strlen((char*)serial_buffer)) )
+       {
+               locked = 1;
+               serial_count = 0;
+       }
+       else if ( EEPROM_command_match((char*)serial_buffer,intf->cmd_unlock,(int)strlen((char*)serial_buffer)) )
+       {
+               locked = 0;
+               serial_count = 0;
+       }
+}
+
+static void EEPROM_reset()
+{
+       serial_count = 0;
+       sending = 0;
+       reset_delay = intf->reset_delay;
+}
+
+void EEPROM_set_cs_line(int state)
+{
+       reset_line = state;
+
+       if (reset_line != CLEAR_LINE)
+               EEPROM_reset();
+}
+
+void EEPROM_set_clock_line(int state)
+{
+       if (state == PULSE_LINE || (clock_line == CLEAR_LINE && state != CLEAR_LINE))
+       {
+               if (reset_line == CLEAR_LINE)
+               {
+                       if (sending)
+                       {
+                               if (eeprom_clock_count == intf->data_bits)
+                               {
+                                       if(intf->enable_multi_read)
+                                       {
+                                               eeprom_read_address = (eeprom_read_address + 1) & ((1 << intf->address_bits) - 1);
+                                               if (intf->data_bits == 16)
+                                                       eeprom_data_bits = (eeprom_data[2*eeprom_read_address+0] << 8) + eeprom_data[2*eeprom_read_address+1];
+                                               else
+                                                       eeprom_data_bits = eeprom_data[eeprom_read_address];
+                                               eeprom_clock_count = 0;
+                                       }
+                                       else
+                                       {
+                                               sending = 0;
+                                       }
+                               }
+                               eeprom_data_bits = (eeprom_data_bits << 1) | 1;
+                               eeprom_clock_count++;
+                       }
+                       else
+                               EEPROM_write(latch);
+               }
+       }
+
+       clock_line = state;
+}
+
+
+void EEPROM_write_bit(int bit)
+{
+       latch = bit;
+}
+
+int EEPROM_read_bit(void)
+{
+       int res;
+
+       if (sending)
+               res = (eeprom_data_bits >> intf->data_bits) & 1;
+       else
+       {
+               if (reset_delay > 0)
+               {
+                       reset_delay--;
+                       res = 0;
+               }
+               else
+                       res = 1;
+       }
+
+       return res;
+}
+*/
+
 static void Sync(void)
 {
-  setprg32(0x8000,(DRegs[0]<<4)|(DRegs[1]&0xF));
+  setprg8r(0x10,0x6000,0);
+  setprg32(0x8000,(reg[0]<<4)|(reg[1]&0xF));
+  setchr8(0);
 }
 
 static void StateRestore(int version)
 {
-  Sync();
+  WSync();
 }
 
 static DECLFR(ReadLow)
 {
   switch (A&0x7700)
   {
-    case 0x5100: return DRegs[2]; break;
+    case 0x5100: return reg[2]|reg[0]|reg[1]|reg[3]^0xff; break;
     case 0x5500: if(trigger)
-                   return DRegs[2];
+                   return reg[2]|reg[1]; // Lei Dian Huang Bi Ka Qiu Chuan Shuo (NJ046) may broke other games
                  else
                    return 0;
   }
   return 4;
 }
 
+static void M163HB(void)
+{
+    if(reg[1]&0x80)
+    {
+      if(scanline==239)
+      {
+        setchr4(0x0000,0);
+        setchr4(0x1000,0);
+      }
+      else if(scanline==127)
+      {
+        setchr4(0x0000,1);
+        setchr4(0x1000,1);
+      }
+/*
+      if(scanline>=127)     // Hu Lu Jin Gang (NJ039) (Ch) [!] don't like it
+      {
+        setchr4(0x0000,1);
+        setchr4(0x1000,1);
+      }
+      else
+      {
+        setchr4(0x0000,0);
+        setchr4(0x1000,0);
+      }
+*/      
+    }
+}
+
 static DECLFW(Write)
 {
   switch (A&0x7300)
   {
-    case 0x5100: DRegs[0]=V; Sync(); break;
-    case 0x5000: DRegs[1]=V; Sync(); break;
-    case 0x5300: DRegs[2]=V; break;
+    case 0x5100: reg[0]=V; WSync(); break;
+    case 0x5000: reg[1]=V; WSync(); break;
+    case 0x5300: reg[2]=V; break;
+    case 0x5200: reg[3]=V; WSync(); break;
   }
 }
 
+static void Power(void)
+{
+  memset(reg,0,8);
+  reg[1]=0xFF;
+  SetWriteHandler(0x5000,0x5FFF,Write);
+  SetReadHandler(0x6000,0xFFFF,CartBR);
+  SetWriteHandler(0x6000,0x7FFF,CartBW);
+  WSync();
+}
+
+static void Close(void)
+{
+  if(WRAM)
+    FCEU_gfree(WRAM);
+  WRAM=NULL;
+}
+
+void Mapper164_Init(CartInfo *info)
+{
+  info->Power=Power;
+  info->Close=Close;
+  WSync = Sync;
+
+  WRAMSIZE = 8192;
+  WRAM=(uint8*)FCEU_gmalloc(WRAMSIZE);
+  SetupCartPRGMapping(0x10,WRAM,WRAMSIZE,1);
+  AddExState(WRAM, WRAMSIZE, 0, "WRAM");
+
+  if(info->battery)
+  {
+    info->SaveGame[0]=WRAM;
+    info->SaveGameLen[0]=WRAMSIZE;
+  }
+
+  GameStateRestore=StateRestore;
+  AddExState(&StateRegs, ~0, 0, 0);
+}
+
 static DECLFW(Write2)
 {
   if(A==0x5101)
@@ -78,83 +415,97 @@ static DECLFW(Write2)
   else
   switch (A&0x7300)
   {
-    case 0x5200: DRegs[0]=V; Sync(); break;
-    case 0x5000: DRegs[1]=V; Sync(); if(!(DRegs[1]&0x80)&&(scanline<128)) setchr8(0); break;
-    case 0x5300: DRegs[2]=V; break;
+    case 0x5200: reg[0]=V; WSync(); break;
+    case 0x5000: reg[1]=V; WSync(); if(!(reg[1]&0x80)&&(scanline<128)) setchr8(0); /* setchr8(0); */ break;
+    case 0x5300: reg[2]=V; break;
+    case 0x5100: reg[3]=V; WSync(); break;
   }
 }
 
-static uint8 WRAM[8192];
-static DECLFR(AWRAM)
+static void Power2(void)
 {
-  return(WRAM[A-0x6000]);
+  memset(reg,0,8);
+  laststrobe=1;
+  SetReadHandler(0x5000,0x5FFF,ReadLow);
+  SetWriteHandler(0x5000,0x5FFF,Write2);
+  SetReadHandler(0x6000,0xFFFF,CartBR);
+  SetWriteHandler(0x6000,0x7FFF,CartBW);
+  WSync();
 }
 
-static DECLFW(BWRAM)
+void Mapper163_Init(CartInfo *info)
 {
-  WRAM[A-0x6000]=V;
+  info->Power=Power2;
+  info->Close=Close;
+  WSync = Sync;
+  GameHBIRQHook=M163HB;
+
+  WRAMSIZE = 8192;
+  WRAM=(uint8*)FCEU_gmalloc(WRAMSIZE);
+  SetupCartPRGMapping(0x10,WRAM,WRAMSIZE,1);
+  AddExState(WRAM, WRAMSIZE, 0, "WRAM");
+
+  if(info->battery)
+  {
+    info->SaveGame[0]=WRAM;
+    info->SaveGameLen[0]=WRAMSIZE;
+  }
+  GameStateRestore=StateRestore;
+  AddExState(&StateRegs, ~0, 0, 0);
 }
 
-static void Power(void)
+static void Sync3(void)
 {
-  memset(DRegs,0,8);
-  DRegs[1]=0xFF;
-  cmd=0;
-  SetReadHandler(0x8000,0xFFFF,CartBR);
-  SetWriteHandler(0x4020,0x5FFF,Write);
-  SetReadHandler(0x6000,0x7FFF,AWRAM);
-  SetWriteHandler(0x6000,0x7FFF,BWRAM);
   setchr8(0);
-  Sync();
+  setprg8r(0x10,0x6000,0);
+  switch(reg[3]&7){
+    case 0:
+    case 2: setprg32(0x8000,(reg[0]&0xc)|(reg[1]&2)|((reg[2]&0xf)<<4)); break;
+    case 1:
+    case 3: setprg32(0x8000,(reg[0]&0xc)|(reg[2]&0xf)<<4); break;
+    case 4: 
+    case 6: setprg32(0x8000,(reg[0]&0xe)|((reg[1]>>1)&1)|((reg[2]&0xf)<<4)); break;
+    case 5:
+    case 7: setprg32(0x8000,(reg[0]&0xf)|((reg[2]&0xf)<<4)); break;
+  }
 }
 
-static void M163HB(void)
+static DECLFW(Write3)
 {
-    if(DRegs[1]&0x80)
-    {
-      if(scanline==239)
-      {
-        setchr4(0x0000,0);
-        setchr4(0x1000,0);
-      }
-      else if(scanline==127)
-      {
-        setchr4(0x0000,1);
-        setchr4(0x1000,1);
-      }
-    }
+//  FCEU_printf("bs %04x %02x\n",A,V);
+  reg[(A>>8)&3]=V;
+  WSync();
 }
 
-static void Power2(void)
+static void Power3(void)
 {
-  memset(DRegs,0,8);
-  DRegs[1]=0xFF;
-  laststrobe=1;
-  cmd=0;
-  SetReadHandler(0x8000,0xFFFF,CartBR);
-  SetWriteHandler(0x4020,0x5FFF,Write2);
-  SetReadHandler(0x6000,0x7FFF,AWRAM);
-  SetWriteHandler(0x6000,0x7FFF,BWRAM);
-  SetReadHandler(0x5000,0x5FFF,ReadLow);
-  setchr8(0);
-  Sync();
+  reg[0]=3;
+  reg[1]=0;
+  reg[2]=0;
+  reg[3]=7;
+  SetWriteHandler(0x5000,0x5FFF,Write3);
+  SetReadHandler(0x6000,0xFFFF,CartBR);
+  SetWriteHandler(0x6000,0x7FFF,CartBW);
+  WSync();
 }
 
-void Mapper164_Init(CartInfo *info)
+void UNLFS304_Init(CartInfo *info)
 {
-  info->Power=Power;
-  GameStateRestore=StateRestore;
-  AddExState(&StateRegs, ~0, 0, 0);
-  AddExState(WRAM, 8192, 0, "WRAM");
-  info->SaveGame[0]=WRAM;\r
-  info->SaveGameLen[0]=8192;
-}
+  info->Power=Power3;
+  info->Close=Close;
+  WSync = Sync3;
+  
+  WRAMSIZE = 8192;
+  WRAM=(uint8*)FCEU_gmalloc(WRAMSIZE);
+  SetupCartPRGMapping(0x10,WRAM,WRAMSIZE,1);
+  AddExState(WRAM, WRAMSIZE, 0, "WRAM");
 
-void Mapper163_Init(CartInfo *info)
-{
-  info->Power=Power2;
-  GameHBIRQHook=M163HB;
+  if(info->battery)
+  {
+    info->SaveGame[0]=WRAM;
+    info->SaveGameLen[0]=WRAMSIZE;
+  }
+  
   GameStateRestore=StateRestore;
   AddExState(&StateRegs, ~0, 0, 0);
-  AddExState(WRAM, 8192, 0, "WRAM");
 }