sms: do psg like md does
[picodrive.git] / pico / sms.c
index 5e32832..6955a7d 100644 (file)
@@ -1,11 +1,15 @@
+/*
+ * SMS emulation
+ * (C) notaz, 2009-2010
+ *
+ * This work is licensed under the terms of MAME license.
+ * See COPYING file in the top-level directory.
+ */
 /*
  * TODO:
  * - start in a state as if BIOS ran
- * - remaining status flags (OVR/COL)
  * - RAM support in mapper
  * - region support
- * - Pause button (NMI)
- * - SN76496 DAC-like usage
  * - H counter
  */
 #include "pico_int.h"
@@ -17,7 +21,7 @@ static unsigned char vdp_data_read(void)
   struct PicoVideo *pv = &Pico.video;
   unsigned char d;
 
-  d = Pico.vramb[pv->addr];
+  d = PicoMem.vramb[pv->addr];
   pv->addr = (pv->addr + 1) & 0x3fff;
   pv->pending = 0;
   return d;
@@ -25,9 +29,12 @@ static unsigned char vdp_data_read(void)
 
 static unsigned char vdp_ctl_read(void)
 {
-  unsigned char d = Pico.video.pending_ints << 7;
-  Pico.video.pending = 0;
-  Pico.video.pending_ints = 0;
+  struct PicoVideo *pv = &Pico.video;
+  unsigned char d;
+
+  d = pv->status | (pv->pending_ints << 7);
+  pv->pending = pv->pending_ints = 0;
+  pv->status = 0;
 
   elprintf(EL_SR, "VDP sr: %02x", d);
   return d;
@@ -38,10 +45,10 @@ static void vdp_data_write(unsigned char d)
   struct PicoVideo *pv = &Pico.video;
 
   if (pv->type == 3) {
-    Pico.cram[pv->addr & 0x1f] = d;
+    PicoMem.cram[pv->addr & 0x1f] = d;
     Pico.m.dirtyPal = 1;
   } else {
-    Pico.vramb[pv->addr] = d;
+    PicoMem.vramb[pv->addr] = d;
   }
   pv->addr = (pv->addr + 1) & 0x3fff;
 
@@ -99,12 +106,12 @@ static unsigned char z80_sms_in(unsigned short a)
       break;
 
     case 0xc0: /* I/O port A and B */
-      d = ~((PicoPad[0] & 0x3f) | (PicoPad[1] << 6));
+      d = ~((PicoIn.pad[0] & 0x3f) | (PicoIn.pad[1] << 6));
       break;
 
     case 0xc1: /* I/O port B and miscellaneous */
-      d = (Pico.sms_io_ctl & 0x80) | ((Pico.sms_io_ctl << 1) & 0x40) | 0x30;
-      d |= ~(PicoPad[1] >> 2) & 0x0f;
+      d = (Pico.ms.io_ctl & 0x80) | ((Pico.ms.io_ctl << 1) & 0x40) | 0x30;
+      d |= ~(PicoIn.pad[1] >> 2) & 0x0f;
       break;
   }
 
@@ -119,13 +126,14 @@ static void z80_sms_out(unsigned short a, unsigned char d)
   switch (a)
   {
     case 0x01:
-      Pico.sms_io_ctl = d;
+      Pico.ms.io_ctl = d;
       break;
 
     case 0x40:
     case 0x41:
-      if (PicoOpt & POPT_EN_PSG)
-        SN76496Write(d);
+      if ((d & 0x90) == 0x90 && PsndPsgLine < Pico.m.scanline)
+        PsndDoPSG(Pico.m.scanline);
+      SN76496Write(d);
       break;
 
     case 0x80:
@@ -156,25 +164,26 @@ static void write_bank(unsigned short a, unsigned char d)
       d &= bank_mask;
       z80_map_set(z80_read_map, 0x4000, 0x7fff, Pico.rom + (d << 14), 0);
 #ifdef _USE_CZ80
-      Cz80_Set_Fetch(&CZ80, 0x4000, 0x7fff, (UINT32)Pico.rom + (d << 14));
+      Cz80_Set_Fetch(&CZ80, 0x4000, 0x7fff, (FPTR)Pico.rom + (d << 14));
 #endif
       break;
     case 0x0f:
       d &= bank_mask;
       z80_map_set(z80_read_map, 0x8000, 0xbfff, Pico.rom + (d << 14), 0);
 #ifdef _USE_CZ80
-      Cz80_Set_Fetch(&CZ80, 0x8000, 0xbfff, (UINT32)Pico.rom + (d << 14));
+      Cz80_Set_Fetch(&CZ80, 0x8000, 0xbfff, (FPTR)Pico.rom + (d << 14));
 #endif
       break;
   }
+  Pico.ms.carthw[a & 0x0f] = d;
 }
 
 static void xwrite(unsigned int a, unsigned char d)
 {
   elprintf(EL_IO, "z80 write [%04x] %02x", a, d);
   if (a >= 0xc000)
-    Pico.zram[a & 0x1fff] = d;
-  if (a >= 0xfff0)
+    PicoMem.zram[a & 0x1fff] = d;
+  if (a >= 0xfff8)
     write_bank(a, d);
 }
 
@@ -188,7 +197,7 @@ void PicoPowerMS(void)
 {
   int s, tmp;
 
-  memset(&Pico.ram,0,(unsigned int)&Pico.rom-(unsigned int)&Pico.ram);
+  memset(&PicoMem,0,sizeof(PicoMem));
   memset(&Pico.video,0,sizeof(Pico.video));
   memset(&Pico.m,0,sizeof(Pico.m));
   Pico.m.pal = 0;
@@ -203,17 +212,20 @@ void PicoPowerMS(void)
   tmp = 1 << s;
   bank_mask = (tmp - 1) >> 14;
 
+  Pico.ms.carthw[0x0e] = 1;
+  Pico.ms.carthw[0x0f] = 2;
+
   PicoReset();
 }
 
 void PicoMemSetupMS(void)
 {
   z80_map_set(z80_read_map, 0x0000, 0xbfff, Pico.rom, 0);
-  z80_map_set(z80_read_map, 0xc000, 0xdfff, Pico.zram, 0);
-  z80_map_set(z80_read_map, 0xe000, 0xffff, Pico.zram, 0);
+  z80_map_set(z80_read_map, 0xc000, 0xdfff, PicoMem.zram, 0);
+  z80_map_set(z80_read_map, 0xe000, 0xffff, PicoMem.zram, 0);
 
   z80_map_set(z80_write_map, 0x0000, 0xbfff, xwrite, 1);
-  z80_map_set(z80_write_map, 0xc000, 0xdfff, Pico.zram, 0);
+  z80_map_set(z80_write_map, 0xc000, 0xdfff, PicoMem.zram, 0);
   z80_map_set(z80_write_map, 0xe000, 0xffff, xwrite, 1);
  
 #ifdef _USE_DRZ80
@@ -221,14 +233,20 @@ void PicoMemSetupMS(void)
   drZ80.z80_out = z80_sms_out;
 #endif
 #ifdef _USE_CZ80
-  Cz80_Set_Fetch(&CZ80, 0x0000, 0xbfff, (UINT32)Pico.rom);
-  Cz80_Set_Fetch(&CZ80, 0xc000, 0xdfff, (UINT32)Pico.zram);
-  Cz80_Set_Fetch(&CZ80, 0xe000, 0xffff, (UINT32)Pico.zram);
+  Cz80_Set_Fetch(&CZ80, 0x0000, 0xbfff, (FPTR)Pico.rom);
+  Cz80_Set_Fetch(&CZ80, 0xc000, 0xdfff, (FPTR)PicoMem.zram);
+  Cz80_Set_Fetch(&CZ80, 0xe000, 0xffff, (FPTR)PicoMem.zram);
   Cz80_Set_INPort(&CZ80, z80_sms_in);
   Cz80_Set_OUTPort(&CZ80, z80_sms_out);
 #endif
 }
 
+void PicoStateLoadedMS(void)
+{
+  write_bank(0xfffe, Pico.ms.carthw[0x0e]);
+  write_bank(0xffff, Pico.ms.carthw[0x0f]);
+}
+
 void PicoFrameMS(void)
 {
   struct PicoVideo *pv = &Pico.video;
@@ -236,17 +254,27 @@ void PicoFrameMS(void)
   int lines = is_pal ? 313 : 262;
   int cycles_line = is_pal ? 58020 : 58293; /* (226.6 : 227.7) * 256 */
   int cycles_done = 0, cycles_aim = 0;
-  int skip = PicoSkipFrame;
+  int skip = PicoIn.skipFrame;
   int lines_vis = 192;
   int hint; // Hint counter
+  int nmi;
   int y;
 
+  PsndStartFrame();
+
+  nmi = (PicoIn.pad[0] >> 7) & 1;
+  if (!Pico.ms.nmi_state && nmi)
+    z80_nmi();
+  Pico.ms.nmi_state = nmi;
+
   PicoFrameStartMode4();
   hint = pv->reg[0x0a];
 
   for (y = 0; y < lines; y++)
   {
     pv->v_counter = Pico.m.scanline = y;
+    if (y > 218)
+      pv->v_counter = y - 6;
 
     if (y < lines_vis && !skip)
       PicoLineMode4(y);
@@ -271,12 +299,16 @@ void PicoFrameMS(void)
       }
     }
 
+    // 224 because of how it's done for MD...
+    if (y == 224 && PsndOut)
+      PsndGetSamplesMS();
+
     cycles_aim += cycles_line;
     cycles_done += z80_run((cycles_aim - cycles_done) >> 8) << 8;
   }
 
-  if (PsndOut)
-    PsndGetSamplesMS();
+  if (PsndOut && PsndPsgLine < lines)
+    PsndDoPSG(lines - 1);
 }
 
 void PicoFrameDrawOnlyMS(void)
@@ -290,3 +322,4 @@ void PicoFrameDrawOnlyMS(void)
     PicoLineMode4(y);
 }
 
+// vim:ts=2:sw=2:expandtab