32x: implement standard/ssf2 mapper
[picodrive.git] / pico / carthw / carthw.c
index 06936fb..41dc6bd 100644 (file)
@@ -8,67 +8,89 @@
 
 #include "../pico_int.h"
 #include "../memory.h"
+#include "eeprom_spi.h"
 
 
-/* The SSFII mapper */
-static unsigned char ssf2_banks[8];
+static int have_bank(u32 base)
+{
+  // the loader allocs in 512K quantities
+  if (base >= Pico.romsize) {
+    elprintf(EL_ANOMALY|EL_STATUS, "carthw: missing bank @ %06x", base);
+    return 0;
+  }
+  return 1;
+}
+
+/* standard/ssf2 mapper */
+int carthw_ssf2_active;
+unsigned char carthw_ssf2_banks[8];
 
 static carthw_state_chunk carthw_ssf2_state[] =
 {
-       { CHUNK_CARTHW, sizeof(ssf2_banks), &ssf2_banks },
-       { 0,            0,                  NULL }
+  { CHUNK_CARTHW, sizeof(carthw_ssf2_banks), &carthw_ssf2_banks },
+  { 0,            0,                         NULL }
 };
 
 static void carthw_ssf2_write8(u32 a, u32 d)
 {
-       u32 target, base;
+  u32 target, base;
 
-       if ((a & 0xfffff0) != 0xa130f0) {
-               PicoWrite8_io(a, d);
-               return;
-       }
+  if ((a & 0xfffff0) != 0xa130f0) {
+    PicoWrite8_io(a, d);
+    return;
+  }
 
-       a &= 0x0e;
-       if (a == 0)
-               return;
+  a &= 0x0e;
+  if (a == 0)
+    return;
+  if (carthw_ssf2_banks[a >> 1] == d)
+    return;
 
-       ssf2_banks[a >> 1] = d;
-       base = d << 19;
-       target = a << 18;
-       if (base + 0x80000 > Pico.romsize) {
-               elprintf(EL_ANOMALY|EL_STATUS, "ssf2: missing bank @ %06x", base);
-               return;
-       }
+  base = d << 19;
+  target = a << 18;
+  if (!have_bank(base))
+    return;
+  carthw_ssf2_banks[a >> 1] = d;
 
-       cpu68k_map_set(m68k_read8_map,  target, target + 0x80000 - 1, Pico.rom + base, 0);
-       cpu68k_map_set(m68k_read16_map, target, target + 0x80000 - 1, Pico.rom + base, 0);
+  cpu68k_map_set(m68k_read8_map,  target, target + 0x80000 - 1, Pico.rom + base, 0);
+  cpu68k_map_set(m68k_read16_map, target, target + 0x80000 - 1, Pico.rom + base, 0);
+  if (PicoIn.AHW & PAHW_32X)
+    p32x_update_banks();
 }
 
 static void carthw_ssf2_mem_setup(void)
 {
-       cpu68k_map_set(m68k_write8_map, 0xa10000, 0xa1ffff, carthw_ssf2_write8, 1);
+  cpu68k_map_set(m68k_write8_map, 0xa10000, 0xa1ffff, carthw_ssf2_write8, 1);
 }
 
 static void carthw_ssf2_statef(void)
 {
-       int i;
-       for (i = 1; i < 8; i++)
-               carthw_ssf2_write8(0xa130f0 | (i << 1), ssf2_banks[i]);
+  int i;
+  for (i = 1; i < 8; i++)
+    carthw_ssf2_write8(0xa130f0 | (i << 1), carthw_ssf2_banks[i]);
+}
+
+static void carthw_ssf2_unload(void)
+{
+  memset(carthw_ssf2_banks, 0, sizeof(carthw_ssf2_banks));
+  carthw_ssf2_active = 0;
 }
 
 void carthw_ssf2_startup(void)
 {
-       int i;
+  int i;
 
-       elprintf(EL_STATUS, "SSF2 mapper startup");
+  elprintf(EL_STATUS, "SSF2 mapper startup");
 
-       // default map
-       for (i = 0; i < 8; i++)
-               ssf2_banks[i] = i;
+  // default map
+  for (i = 0; i < 8; i++)
+    carthw_ssf2_banks[i] = i;
 
-       PicoCartMemSetup  = carthw_ssf2_mem_setup;
-       PicoLoadStateHook = carthw_ssf2_statef;
-       carthw_chunks     = carthw_ssf2_state;
+  PicoCartMemSetup   = carthw_ssf2_mem_setup;
+  PicoLoadStateHook  = carthw_ssf2_statef;
+  PicoCartUnloadHook = carthw_ssf2_unload;
+  carthw_chunks      = carthw_ssf2_state;
+  carthw_ssf2_active = 1;
 }
 
 
@@ -270,6 +292,7 @@ static carthw_state_chunk carthw_pier_state[] =
 {
   { CHUNK_CARTHW,     sizeof(pier_regs),      pier_regs },
   { CHUNK_CARTHW + 1, sizeof(pier_dump_prot), &pier_dump_prot },
+  { CHUNK_CARTHW + 2, 0,                      NULL }, // filled later
   { 0,                0,                      NULL }
 };
 
@@ -308,7 +331,8 @@ static void carthw_pier_write8(u32 a, u32 d)
       base = d << 19;
       goto do_map;
     case 0x09:
-      // TODO
+      Pico.sv.changed = 1;
+      eeprom_spi_write(d);
       break;
     case 0x0b:
       // eeprom read
@@ -321,10 +345,9 @@ static void carthw_pier_write8(u32 a, u32 d)
   return;
 
 do_map:
-  if (base + 0x80000 > Pico.romsize) {
-    elprintf(EL_ANOMALY|EL_STATUS, "pier: missing bank @ %06x", base);
+  if (!have_bank(base))
     return;
-  }
+
   cpu68k_map_set(m68k_read8_map,  target, target + 0x80000 - 1, Pico.rom + base, 0);
   cpu68k_map_set(m68k_read16_map, target, target + 0x80000 - 1, Pico.rom + base, 0);
 }
@@ -346,7 +369,7 @@ static u32 carthw_pier_read8(u32 a)
     return PicoRead8_io(a);
 
   if (a == 0xa1300b)
-    return 0; // TODO
+    return eeprom_spi_read(a);
 
   elprintf(EL_UIO, "pier r8  [%06x] @%06x", a, SekPc);
   return 0;
@@ -413,12 +436,14 @@ static void carthw_pier_reset(void)
 {
   pier_regs[0] = 1;
   pier_regs[1] = pier_regs[2] = pier_regs[3] = 0;
-  pier_dump_prot = 3;
   carthw_pier_statef();
+  eeprom_spi_init(NULL);
 }
 
 void carthw_pier_startup(void)
 {
+  void *eeprom_state;
+  int eeprom_size = 0;
   int i;
 
   elprintf(EL_STATUS, "Pier Solar mapper startup");
@@ -430,10 +455,22 @@ void carthw_pier_startup(void)
     return;
   }
 
+  pier_dump_prot = 3;
+
   // create dump protection bank
   for (i = 0; i < M68K_BANK_SIZE; i += 0x8000)
     memcpy(Pico.rom + Pico.romsize + i, Pico.rom, 0x8000);
 
+  // save EEPROM
+  eeprom_state = eeprom_spi_init(&eeprom_size);
+  Pico.sv.flags = 0;
+  Pico.sv.size = 0x10000;
+  Pico.sv.data = calloc(1, Pico.sv.size);
+  if (!Pico.sv.data)
+    Pico.sv.size = 0;
+  carthw_pier_state[2].ptr = eeprom_state;
+  carthw_pier_state[2].size = eeprom_size;
+
   PicoCartMemSetup  = carthw_pier_mem_setup;
   PicoResetHook     = carthw_pier_reset;
   PicoLoadStateHook = carthw_pier_statef;
@@ -679,3 +716,4 @@ void carthw_prot_lk3_startup(void)
   PicoCartMemSetup = carthw_prot_lk3_mem_setup;
 }
 
+// vim:ts=2:sw=2:expandtab