mcd, some fixes for msu/md+
authorkub <derkub@gmail.com>
Fri, 20 Dec 2024 11:04:54 +0000 (12:04 +0100)
committerkub <derkub@gmail.com>
Fri, 20 Dec 2024 11:04:54 +0000 (12:04 +0100)
pico/cd/megasd.c
pico/cd/megasd.h
pico/cd/memory.c
pico/cd/memory_arm.S
pico/media.c
pico/memory.c

index 6f1ec7e..ec31acb 100644 (file)
@@ -47,23 +47,23 @@ static void cdd_play(s32 lba)
   Pico_msd.currentlba = lba;
 
   cdd_play_audio(Pico_msd.index, Pico_msd.currentlba);
-  Pico_msd.state = 3;
+  Pico_msd.state |= MSD_ST_PLAY;
 }
 
 static void cdd_pause(void)
 {
-  Pico_msd.state |= 4;
+  Pico_msd.state |= MSD_ST_PAUSE;
 }
 
 static void cdd_resume(void)
 {
-  Pico_msd.state &= ~4;
+  Pico_msd.state &= ~MSD_ST_PAUSE;
 }
 
 static void cdd_stop(void)
 {
   Pico_msd.index = -1;
-  Pico_msd.state &= ~6;
+  Pico_msd.state &= ~(MSD_ST_PAUSE | MSD_ST_PLAY);
 }
 
 // play a track, looping from offset if enabled
@@ -121,19 +121,15 @@ static void msd_playsectors(s32 startlba, s32 endlba, s32 looplba, int loop)
 void msd_update()
 {
   if (Pico_msd.state && Pico_msd.index >= 0) {
-    // CD LEDs
-
-    if (Pico_msd.state & 2) {
-      s68k_write8(0xff8000, 0x3);
+    if (Pico_msd.state & MSD_ST_PLAY) {
       Pico_msd.command = 0;
-      if (!(Pico_msd.state & 4))
+      if (!(Pico_msd.state & MSD_ST_PAUSE))
         Pico_msd.currentlba ++;
       if (Pico_msd.currentlba >= Pico_msd.endlba-1) {
         if (!Pico_msd.loop || Pico_msd.index < 0) {
-          Pico_msd.state = 1;
+          Pico_msd.state &= MSD_ST_INIT;
           // audio done
           Pico_msd.index = -1;
-          s68k_write8(0xff8000, 0x2);
         } else
           cdd_play(Pico_msd.looplba);
       }
@@ -159,7 +155,7 @@ void msd_process(u16 d)
   case 0x14: cdd_resume();
              Pico_msd.command = 0; break;
 
-  case 0x16: Pico_msd.result = !!(Pico_msd.state & 2) << 8;
+  case 0x16: Pico_msd.result = !!(Pico_msd.state & MSD_ST_PLAY) << 8;
              Pico_msd.command = 0; break;
 
   case 0x27: Pico_msd.result = cdd.toc.last << 8;
@@ -173,13 +169,10 @@ void msd_process(u16 d)
 // initialize MEGASD
 static void msd_init(void)
 {
-  if (!(Pico_msd.state & 1)) {
-    Pico_msd.state = 1;
+  if (!(Pico_msd.state & MSD_ST_INIT)) {
+    Pico_msd.state = MSD_ST_INIT;
     Pico_msd.index = -1;
 
-    // CD LEDs
-    s68k_write8(0xff8000, 0x2);
-
     PicoResetHook = msd_reset;
   }
 }
@@ -187,11 +180,9 @@ static void msd_init(void)
 void msd_reset(void)
 {
   if (Pico_msd.state) {
-    Pico_msd.state = Pico_msd.command = 0;
+    Pico_msd.state = Pico_msd.command = Pico_msd.result = 0;
     cdd_stop();
 
-    s68k_write8(0xff8000, 0x0);
-
     PicoResetHook = NULL;
   }
 }
@@ -201,27 +192,29 @@ static u32 msd_read16(u32 a)
 {
   u16 d = 0;
 
-  if ((u16)a >= 0x0f800) {
+  a = (u16)a;
+  if (a >= 0x0f800) {
     d = Pico_msd.data[(a&0x7ff)>>1];
-  } else if ((u16)a >= 0xf7f0) {
+  } else if (a >= 0xf7f6) {
     switch (a&0xe) {
       case 0x6: d = 0x5241; break; // RA
       case 0x8: d = 0x5445; break; // TE
+      case 0xa: d = 0xcd54; break;
       case 0xc: d = Pico_msd.result; break;
       case 0xe: d = Pico_msd.command; break;
     }
-  } else if (a < Pico.romsize)
-    d = *(u16 *)(Pico.rom + a);
+  } else if (Pico.romsize > 0x30000)
+    d = *(u16 *)(Pico.rom + 0x30000 + a);
 
   return d;
 }
 
 static u32 msd_read8(u32 a)
 {
-  u16 d = msd_read16(a);
+  u16 d = msd_read16(a&~1);
 
   if (!(a&1)) d >>= 8;
-  return d;
+  return (u8)d;
 }
 
 void msd_write16(u32 a, u32 d)
@@ -237,16 +230,20 @@ void msd_write16(u32 a, u32 d)
       base += 0x800000; // mirror
       cpu68k_map_set(m68k_read8_map,  base, 0x0bffff, msd_read8, 1);
       cpu68k_map_set(m68k_read16_map, base, 0x0bffff, msd_read16, 1);
+      if (Pico.romsize > base)
+        memcpy(Pico_msd.data, Pico.rom + 0x3f800, 0x800);
     } else if (Pico.romsize > base) {
       cpu68k_map_set(m68k_read8_map,  base, 0x03ffff, Pico.rom+base, 0);
       cpu68k_map_set(m68k_read16_map, base, 0x03ffff, Pico.rom+base, 0);
     }
   } else if (a == 0xf7fe) {
     // command port
-    msd_process(d);
+    if (Pico_msd.state & MSD_ST_INIT)
+      msd_process(d);
   } else if (a >= 0xf800) {
     // data area
-    Pico_msd.data[(a&0x7ff) >> 1] = d;
+    if (Pico_msd.state & MSD_ST_INIT)
+      Pico_msd.data[(a&0x7ff) >> 1] = d;
   }
 }
 
@@ -254,6 +251,7 @@ void msd_write8(u32 a, u32 d)
 {
   if ((u16)a >= 0xf800) {
     // data area
-    ((u8 *)Pico_msd.data)[MEM_BE2(a&0x7ff)] = d;
+    if (Pico_msd.state & MSD_ST_INIT)
+      ((u8 *)Pico_msd.data)[MEM_BE2(a&0x7ff)] = d;
   }
 }
index 63846e1..7c31158 100644 (file)
@@ -25,6 +25,10 @@ struct megasd {
   s32 pad[7];
 };
 
+#define MSD_ST_INIT     1
+#define MSD_ST_PLAY     2
+#define MSD_ST_PAUSE    4
+
 extern struct megasd Pico_msd;
 
 
index 72a64e4..d00f5b9 100644 (file)
@@ -1237,10 +1237,11 @@ PICO_INTERNAL void PicoMemSetupCD(void)
   cpu68k_map_set(m68k_read16_map,  BASE, BASE+0x01ffff, Pico_mcd->bios, 0);\r
   if (pcd_base_address != 0) { // cartridge (for MSU/MD+)\r
     // MD+ on MEGASD plus mirror\r
-    cpu68k_map_set(m68k_write8_map,  0x040000-(1<<M68K_MEM_SHIFT), 0x03ffff, msd_write8, 1);\r
-    cpu68k_map_set(m68k_write16_map, 0x040000-(1<<M68K_MEM_SHIFT), 0x03ffff, msd_write16, 1);\r
-    cpu68k_map_set(m68k_write8_map,  0x0c0000-(1<<M68K_MEM_SHIFT), 0x0bffff, msd_write8, 1);\r
-    cpu68k_map_set(m68k_write16_map, 0x0c0000-(1<<M68K_MEM_SHIFT), 0x0bffff, msd_write16, 1);\r
+    u32 base = 0x040000-(1<<M68K_MEM_SHIFT);\r
+    cpu68k_map_set(m68k_write8_map,  base, 0x03ffff, msd_write8, 1);\r
+    cpu68k_map_set(m68k_write16_map, base, 0x03ffff, msd_write16, 1);\r
+    cpu68k_map_set(m68k_write8_map,  base+0x080000, 0x0bffff, msd_write8, 1);\r
+    cpu68k_map_set(m68k_write16_map, base+0x080000, 0x0bffff, msd_write16, 1);\r
     msd_reset();\r
   } else { // no cartridge\r
     // RAM cart\r
index 1496462..2077a73 100644 (file)
@@ -199,8 +199,6 @@ m_m68k_read8_r06:
     ldrb    r0, [r1, #0x73]           @ IRQ vector
     bx      lr
 m_m68k_read8_r07:
-    PIC_LDR(r1, r2, Pico_mcd)
-    ldr     r1, [r1]
     ldrb    r0, [r1, #0x72]
     bx      lr
 m_m68k_read8_r08:
@@ -298,8 +296,6 @@ m_m68k_read16_r04:
     mov     r0, r0, lsl #8
     bx      lr
 m_m68k_read16_r06:
-    PIC_LDR(r1, r2, Pico_mcd)
-    ldr     r1, [r1]
     ldrh    r0, [r1, #0x72]           @ IRQ vector
     bx      lr
 m_m68k_read16_r08:
@@ -352,6 +348,7 @@ PicoWrite8_mcd_io:
     beq     m68k_reg_write8
 
     PIC_LDR(r2, r3, carthw_ssf2_active)
+    ldr     r2, [r2]
     tst     r2, r2
     bne     carthw_ssf2_write8
     b       PicoWrite8_io
@@ -383,6 +380,7 @@ PicoWrite16_mcd_io:
     beq     m_m68k_write16_regs
 
     PIC_LDR(r2, r3, carthw_ssf2_active)
+    ldr     r2, [r2]
     tst     r2, r2
     bne     carthw_ssf2_write16
     b       PicoWrite16_io
index ccad09a..ff7a338 100644 (file)
@@ -320,7 +320,7 @@ enum media_type_e PicoLoadMedia(const char *filename,
         rom_size = 0;
       }
 
-      // if there is an MSU ROM, it's name is now in rom_fname for loading
+      // if there is an MSU ROM, its name is now in rom_fname for loading
       PicoIn.AHW |= PAHW_MCD;
     }
     else {
index b0ee21c..c79018d 100644 (file)
@@ -203,7 +203,7 @@ void cpu68k_map_all_funcs(u32 start_addr, u32 end_addr, u32 (*r8)(u32), u32 (*r1
 u32 PicoRead16_floating(u32 a)\r
 {\r
   // faking open bus\r
-  u32 d = (Pico.m.rotate += 0x41);\r
+  u16 d = (Pico.m.rotate += 0x41);\r
   d ^= (d << 5) ^ (d << 8);\r
   if ((a & 0xff0000) == 0xa10000) return d; // MegaCD pulldowns don't work here curiously\r
   return (PicoIn.AHW & PAHW_MCD) ? 0x00 : d; // pulldown if MegaCD2 attached\r
@@ -775,9 +775,9 @@ u32 PicoRead8_io(u32 a)
     goto end;\r
   }\r
 \r
-  d = PicoRead16_floating(a);\r
-\r
   if ((a & 0xfc00) == 0x1000) {\r
+    d = (u8)PicoRead16_floating(a);\r
+\r
     if ((a & 0xff01) == 0x1100) { // z80 busreq (verified)\r
       // bit8 seems to be readable in this range\r
       if (!(a & 1)) {\r
@@ -807,10 +807,10 @@ u32 PicoRead16_io(u32 a)
     goto end;\r
   }\r
 \r
-  d = PicoRead16_floating(a);\r
-\r
   // bit8 seems to be readable in this range\r
   if ((a & 0xfc00) == 0x1000) {\r
+    d = PicoRead16_floating(a);\r
+\r
     if ((a & 0xff00) == 0x1100) { // z80 busreq\r
       d &= ~0x0100;\r
       d |= (z80_cycles_from_68k() < Pico.t.z80c_cnt) << 8;\r