some more SVP work
[picodrive.git] / Pico / carthw / svp / ssp16.c
index 7ac1467..26c4cac 100644 (file)
  * mpys (rj), (ri), b
  *   name: multiply and subtract?
  *   notes: not used by VR code.
+ *
+ *
+ * Assumptions in this code
+ *   P is not directly writeable
  */
 
 #include "../../PicoInt.h"
 
-#define rX     ssp->gr[SSP_X].l
-#define rY     ssp->gr[SSP_Y].l
-#define rA     ssp->gr[SSP_A]          // 4
-#define rST    ssp->gr[SSP_ST].l
-#define rSTACK ssp->gr[SSP_STACK].l
-#define rPC    ssp->gr[SSP_PC].l
-#define rP     ssp->gr[SSP_P]          // 8
-#define rPM0   ssp->gr[SSP_PM0].l
-#define rPM1   ssp->gr[SSP_PM1].l
-#define rPM2   ssp->gr[SSP_PM2].l
-#define rXST   ssp->gr[SSP_XST].l      // 12
-#define rPM4   ssp->gr[SSP_PM4].l      // 14
-#define rPMC   ssp->gr[SSP_PMC].l
+#define u32 unsigned int
+
+// 0
+#define rX     ssp->gr[SSP_X].h
+#define rY     ssp->gr[SSP_Y].h
+#define rA     ssp->gr[SSP_A].h
+#define rST    ssp->gr[SSP_ST].h       // 4
+#define rSTACK ssp->gr[SSP_STACK].h
+#define rPC    ssp->gr[SSP_PC].h
+#define rP     ssp->gr[SSP_P]
+#define rPM0   ssp->gr[SSP_PM0].h      // 8
+#define rPM1   ssp->gr[SSP_PM1].h
+#define rPM2   ssp->gr[SSP_PM2].h
+#define rXST   ssp->gr[SSP_XST].h
+#define rPM4   ssp->gr[SSP_PM4].h      // 12
+// 13
+#define rPMC   ssp->gr[SSP_PMC]                // will keep addr in .h, mode in .l
 #define rAL    ssp->gr[SSP_A].l
 
 #define GET_PC() (PC - (unsigned short *)Pico.rom)
-#define SET_PC() PC = (unsigned short *)Pico.rom + rPC
+#define GET_PC_OFFS() ((unsigned int)PC - (unsigned int)Pico.rom)
+#define SET_PC(d) PC = (unsigned short *)Pico.rom + d
+
+#define REG_READ(r) (((r) <= 4) ? ssp->gr[r].h : read_handlers[r]())
+#define REG_WRITE(r,d) { \
+       int r1 = r; \
+       if (r1 > 4) write_handlers[r1](d); \
+       else if (r1 > 0) ssp->gr[r1].h = d; \
+}
+
+static ssp1601_t *ssp = NULL;
+static unsigned short *PC;
+static int g_cycles;
+
+// -----------------------------------------------------
+// register i/o handlers
+
+// 0-4, 13
+static u32 read_unknown(void)
+{
+       elprintf(EL_ANOMALY|EL_SVP, "ssp16: unknown read @ %04x", GET_PC_OFFS());
+       return 0;
+}
+
+static void write_unknown(u32 d)
+{
+       elprintf(EL_ANOMALY|EL_SVP, "ssp16: unknown write @ %04x", GET_PC_OFFS());
+}
+
+// 5
+static u32 read_STACK(void)
+{
+       u32 d = 0;
+       if (rSTACK < 6) {
+               d = ssp->stack[rSTACK];
+               rSTACK++;
+       } else
+               elprintf(EL_ANOMALY|EL_SVP, "ssp16: stack underflow! (%i) @ %04x", rSTACK, GET_PC_OFFS());
+       return d;
+}
+
+static void write_STACK(u32 d)
+{
+       if (rSTACK > 0) {
+               rSTACK--;
+               ssp->stack[rSTACK] = d;
+       } else
+               elprintf(EL_ANOMALY|EL_SVP, "ssp16: stack overflow! (%i) @ %04x", rSTACK, GET_PC_OFFS());
+}
+
+// 6
+static u32 read_PC(void)
+{
+       return GET_PC();
+}
+
+static void write_PC(u32 d)
+{
+       SET_PC(d);
+       g_cycles--;
+}
+
+// 7
+static u32 read_P(void)
+{
+       rP.v = (u32)rX * rY * 2;
+       return rP.h;
+}
+
+static u32 pm_io(int reg, int write, u32 d)
+{
+       if (ssp->emu_status & SSP_PMC_SET) {
+               elprintf(EL_SVP, "PM%i (%c) set to %08x @ %04x", reg, write ? 'w' : 'r', rPMC.v, GET_PC_OFFS());
+               ssp->pmac_read[write ? reg + 6 : reg] = rPMC.v;
+               ssp->emu_status &= ~SSP_PMC_SET;
+               return 0;
+       }
+
+       if (ssp->pmac_read[reg] != 0) {
+               elprintf(EL_SVP, "PM%i %c @ %04x", reg, write ? 'w' : 'r', GET_PC_OFFS());
+               // do something depending on mode
+               return 0;
+       }
+
+       return (u32)-1;
+}
+
+// 8
+static u32 read_PM0(void)
+{
+       u32 d = pm_io(0, 0, 0);
+       if (d != (u32)-1) return d;
+       elprintf(EL_SVP, "PM0 raw r %04x @ %04x", rPM0, GET_PC_OFFS());
+       return rPM0;
+}
+
+static void write_PM0(u32 d)
+{
+       u32 r = pm_io(0, 1, d);
+       if (r != (u32)-1) return;
+       elprintf(EL_SVP, "PM0 raw w %04x @ %04x", d, GET_PC_OFFS());
+       rPM0 = d;
+}
+
+// 9
+static u32 read_PM1(void)
+{
+       u32 d = pm_io(1, 0, 0);
+       if (d != (u32)-1) return d;
+       // can be removed?
+       elprintf(EL_SVP, "PM1 raw r %04x @ %04x", rPM1, GET_PC_OFFS());
+       return rPM0;
+}
+
+static void write_PM1(u32 d)
+{
+       u32 r = pm_io(1, 1, d);
+       if (r != (u32)-1) return;
+       // can be removed?
+       elprintf(EL_SVP, "PM1 raw w %04x @ %04x", d, GET_PC_OFFS());
+       rPM0 = d;
+}
+
+// 10
+static u32 read_PM2(void)
+{
+       u32 d = pm_io(2, 0, 0);
+       if (d != (u32)-1) return d;
+       // can be removed?
+       elprintf(EL_SVP, "PM2 raw r %04x @ %04x", rPM2, GET_PC_OFFS());
+       return rPM0;
+}
+
+static void write_PM2(u32 d)
+{
+       u32 r = pm_io(2, 1, d);
+       if (r != (u32)-1) return;
+       // can be removed?
+       elprintf(EL_SVP, "PM2 raw w %04x @ %04x", d, GET_PC_OFFS());
+       rPM0 = d;
+}
+
+// 11
+static u32 read_XST(void)
+{
+       // can be removed?
+       u32 d = pm_io(3, 0, 0);
+       if (d != (u32)-1) return d;
+
+       elprintf(EL_SVP, "XST raw r %04x @ %04x", rXST, GET_PC_OFFS());
+       return rPM0;
+}
 
-void ssp1601_reset(ssp1601_t *ssp)
+static void write_XST(u32 d)
 {
+       // can be removed?
+       u32 r = pm_io(3, 1, d);
+       if (r != (u32)-1) return;
+
+       elprintf(EL_SVP, "XST raw w %04x @ %04x", d, GET_PC_OFFS());
+       rPM0 = d;
+}
+
+// 12
+static u32 read_PM4(void)
+{
+       u32 d = pm_io(4, 0, 0);
+       if (d != (u32)-1) return d;
+       // can be removed?
+       elprintf(EL_SVP, "PM4 raw r %04x @ %04x", rPM4, GET_PC_OFFS());
+       return rPM0;
+}
+
+static void write_PM4(u32 d)
+{
+       u32 r = pm_io(4, 1, d);
+       if (r != (u32)-1) return;
+       // can be removed?
+       elprintf(EL_SVP, "PM4 raw w %04x @ %04x", d, GET_PC_OFFS());
+       rPM0 = d;
+}
+
+// 14
+static u32 read_PMC(void)
+{
+       if (ssp->emu_status & SSP_PMC_HAVE_ADDR) {
+               if (ssp->emu_status & SSP_PMC_SET)
+                       elprintf(EL_ANOMALY|EL_SVP, "prev PMC not used @ %04x", GET_PC_OFFS());
+               ssp->emu_status |= SSP_PMC_SET;
+               return rPMC.l;
+       } else {
+               ssp->emu_status |= SSP_PMC_HAVE_ADDR;
+               return rPMC.h;
+       }
+}
+
+static void write_PMC(u32 d)
+{
+       if (ssp->emu_status & SSP_PMC_HAVE_ADDR) {
+               if (ssp->emu_status & SSP_PMC_SET)
+                       elprintf(EL_ANOMALY|EL_SVP, "prev PMC not used @ %04x", GET_PC_OFFS());
+               ssp->emu_status |= SSP_PMC_SET;
+               rPMC.l = d;
+       } else {
+               ssp->emu_status |= SSP_PMC_HAVE_ADDR;
+               rPMC.h = d;
+       }
+}
+
+// 15
+static u32 read_AL(void)
+{
+       // TODO: figure out what's up with those blind reads..
+       return rAL;
+}
+
+static void write_AL(u32 d)
+{
+       rAL = d;
+}
+
+
+typedef u32 (*read_func_t)(void);
+typedef void (*write_func_t)(u32 d);
+
+static read_func_t read_handlers[16] =
+{
+       read_unknown, read_unknown, read_unknown, read_unknown, // -, X, Y, A
+       read_unknown,   // 4 ST
+       read_STACK,
+       read_PC,
+       read_P,
+       read_PM0,       // 8
+       read_PM1,
+       read_PM2,
+       read_XST,
+       read_PM4,       // 12
+       read_unknown,   // 13 gr13
+       read_PMC,
+       read_AL
+};
+
+static write_func_t write_handlers[16] =
+{
+       write_unknown, write_unknown, write_unknown, write_unknown, // -, X, Y, A
+       write_unknown,  // 4 ST
+       write_STACK,
+       write_PC,
+       write_unknown,  // 7 P
+       write_PM0,      // 8
+       write_PM1,
+       write_PM2,
+       write_XST,
+       write_PM4,      // 12
+       write_unknown,  // 13 gr13
+       write_PMC,
+       write_AL
+};
+
+void ssp1601_reset(ssp1601_t *l_ssp)
+{
+       ssp = l_ssp;
        ssp->emu_status = 0;
-       ssp->gr[SSP_GR0].v = 0xffff;
+       ssp->gr[SSP_GR0].v = 0xffff0000;
        rPC = 0x400;
-       rSTACK = 5; // ?
+       rSTACK = 6; // ? using descending stack
 }
 
 
-void ssp1601_run(ssp1601_t *ssp, int cycles)
+void ssp1601_run(int cycles)
 {
-       unsigned short *PC;
        int op;
 
-       SET_PC();
+       SET_PC(rPC);
+       g_cycles = cycles;
 
-       while (cycles > 0)
+       while (g_cycles > 0)
        {
                op = *PC;
                switch (op >> 9)
                {
                        // ld d, s
                        case 0:
-                       {
-                               int s, d, opdata = 0;
                                if (op == 0) break; // nop
-                               s =  op & 0x0f;
-                               d = (op & 0xf0) >> 4;
-                               if (s == SSP_A || s == SSP_P) opdata |= 1; // src is 32bit
-                               if (d == SSP_A || d == SSP_P) opdata |= 2; // dst is 32bit
-                               if (s == SSP_STACK) opdata |= 4; // src is stack
-                               if (d == SSP_STACK) opdata |= 8; // dst is stack
-                               switch (opdata)
-                               {
-                                       case 0x0: ssp->gr[d].l = ssp->gr[s].l; break; // 16 <- 16
-                                       case 0x1: ssp->gr[d].l = ssp->gr[s].h; break; // 16 <- 32
-                                       case 0x2: ssp->gr[d].h = ssp->gr[s].l; break; // 32 <- 16
-                                                 // TODO: MAME claims that only hi word is transfered. Go figure.
-                                       case 0x3: ssp->gr[d].v = ssp->gr[s].v; break; // 32 <- 32
-                                       case 0x4: ; // TODO
+                               if (op == ((SSP_A<<4)|SSP_P)) { // A <- P
+                                       // not sure. MAME claims that only hi word is transfered.
+                                       read_P(); // update P
+                                       ssp->gr[SSP_A].v = ssp->gr[SSP_P].v;
+                                       break;
                                }
-                               if (d == SSP_PC)
                                {
-                                       SET_PC();
-                                       cycles--;
+                                       u32 d = REG_READ(op & 0x0f);
+                                       REG_WRITE((op & 0xf0) >> 4, d);
                                }
+                               // flags?
                                break;
-                       }
 
                        default:
-                       elprintf(0xffff, "ssp: unhandled op %04x @ %04x", op, GET_PC()<<1);
-                       break;
+                               elprintf(EL_ANOMALY|EL_SVP, "ssp16: unhandled op %04x @ %04x", op, GET_PC_OFFS());
+                               break;
                }
-               cycles--;
+               g_cycles--;
                PC++;
        }
 
+       read_P(); // update P
        rPC = GET_PC();
+
+       if (ssp->gr[SSP_GR0].v != 0xffff0000)
+               elprintf(EL_ANOMALY|EL_SVP, "ssp16: REG 0 corruption! %08x", ssp->gr[SSP_GR0].v);
 }