add a hack for Decap Attack
[picodrive.git] / cpu / sh2 / sh2.c
index 5736908..403c4c7 100644 (file)
 
 #define I 0xf0
 
-int sh2_init(SH2 *sh2, int is_slave)
+int sh2_init(SH2 *sh2, int is_slave, SH2 *other_sh2)
 {
        int ret = 0;
+       unsigned int mult_m68k_to_sh2 = sh2->mult_m68k_to_sh2;
+       unsigned int mult_sh2_to_m68k = sh2->mult_sh2_to_m68k;
 
-       memset(sh2, 0, offsetof(SH2, mult_m68k_to_sh2));
+       memset(sh2, 0, sizeof(*sh2));
        sh2->is_slave = is_slave;
+       sh2->other_sh2 = other_sh2;
+       sh2->mult_m68k_to_sh2 = mult_m68k_to_sh2;
+       sh2->mult_sh2_to_m68k = mult_sh2_to_m68k;
+
        pdb_register_cpu(sh2, PDBCT_SH2, is_slave ? "ssh2" : "msh2");
 #ifdef DRC_SH2
        ret = sh2_drc_init(sh2);
@@ -45,6 +51,8 @@ void sh2_reset(SH2 *sh2)
 
 void sh2_do_irq(SH2 *sh2, int level, int vector)
 {
+       sh2->sr &= 0x3f3;
+
        sh2->r[15] -= 4;
        p32x_sh2_write32(sh2->r[15], sh2->sr, sh2);     /* push SR onto stack */
        sh2->r[15] -= 4;
@@ -127,38 +135,47 @@ void sh2_unpack(SH2 *sh2, const unsigned char *buff)
 #include <stdio.h>
 #include <stdlib.h>
 #include <pico/memory.h>
+#undef _USE_CZ80 // HACK
+#include <pico/pico_int.h>
+#include <pico/debug.h>
 
 static SH2 sh2ref[2];
-static int current_slave = -1;
 static unsigned int mem_val;
-static FILE *f;
-
-#define SH2MAP_ADDR2OFFS_R(a) \
-  ((((a) >> 25) & 3) | (((a) >> 27) & 0x1c))
 
 static unsigned int local_read32(SH2 *sh2, u32 a)
 {
        const sh2_memmap *sh2_map = sh2->read16_map;
+       u16 *pd;
        uptr p;
 
-       sh2_map += SH2MAP_ADDR2OFFS_R(a);
+       sh2_map += (a >> 25);
        p = sh2_map->addr;
        if (!map_flag_set(p)) {
-               u16 *pd = (u16 *)((p << 1) + ((a & sh2_map->mask) & ~3));
+               pd = (u16 *)((p << 1) + ((a & sh2_map->mask) & ~1));
                return (pd[0] << 16) | pd[1];
        }
 
-       return 0;
-}
+       if ((a & 0xfffff000) == 0xc0000000) {
+               // data array
+               pd = (u16 *)sh2->data_array + (a & 0xfff) / 2;
+               return (pd[0] << 16) | pd[1];
+       }
+       if ((a & 0xdfffffc0) == 0x4000) {
+               pd = &Pico32x.regs[(a & 0x3f) / 2];
+               return (pd[0] << 16) | pd[1];
+       }
+       if ((a & 0xdffffe00) == 0x4200) {
+               pd = &Pico32xMem->pal[(a & 0x1ff) / 2];
+               return (pd[0] << 16) | pd[1];
+       }
 
-static void write_uint(unsigned char ctl, unsigned int v)
-{
-       fwrite(&ctl, 1, 1, f);
-       fwrite(&v, sizeof(v), 1, f);
+       return 0;
 }
 
 void do_sh2_trace(SH2 *current, int cycles)
 {
+       static int current_slave = -1;
+       static u32 current_m68k_pc;
        SH2 *sh2o = &sh2ref[current->is_slave];
        u32 *regs_a = (void *)current;
        u32 *regs_o = (void *)sh2o;
@@ -166,34 +183,36 @@ void do_sh2_trace(SH2 *current, int cycles)
        u32 val;
        int i;
 
-       if (f == NULL)
-               f = fopen("tracelog", "wb");
+       if (SekPc != current_m68k_pc) {
+               current_m68k_pc = SekPc;
+               tl_write_uint(CTL_M68KPC, current_m68k_pc);
+       }
 
        if (current->is_slave != current_slave) {
                current_slave = current->is_slave;
-               v = 0x80 | current->is_slave;
-               fwrite(&v, 1, 1, f);
+               v = CTL_MASTERSLAVE | current->is_slave;
+               tl_write(&v, sizeof(v));
        }
 
        for (i = 0; i < offsetof(SH2, read8_map) / 4; i++) {
                if (i == 17) // ppc
                        continue;
                if (regs_a[i] != regs_o[i]) {
-                       write_uint(i, regs_a[i]);
+                       tl_write_uint(CTL_SH2_R + i, regs_a[i]);
                        regs_o[i] = regs_a[i];
                }
        }
 
        if (current->ea != sh2o->ea) {
-               write_uint(0x82, current->ea);
+               tl_write_uint(CTL_EA, current->ea);
                sh2o->ea = current->ea;
        }
        val = local_read32(current, current->ea);
        if (mem_val != val) {
-               write_uint(0x83, val);
+               tl_write_uint(CTL_EAVAL, val);
                mem_val = val;
        }
-       write_uint(0x84, cycles);
+       tl_write_uint(CTL_CYCLES, cycles);
 }
 
 static const char *regnames[] = {
@@ -205,7 +224,20 @@ static const char *regnames[] = {
        "gbr", "vbr", "mach","macl",
 };
 
-void do_sh2_cmp(SH2 *current)
+static void dump_regs(SH2 *sh2)
+{
+       char csh2;
+       int i;
+
+       csh2 = sh2->is_slave ? 's' : 'm';
+       for (i = 0; i < 16/2; i++)
+               printf("%csh2 r%d: %08x r%02d: %08x\n", csh2,
+                       i, sh2->r[i], i+8, sh2->r[i+8]);
+       printf("%csh2 PC: %08x  ,   %08x\n", csh2, sh2->pc, sh2->ppc);
+       printf("%csh2 SR:      %03x  PR: %08x\n", csh2, sh2->sr, sh2->pr);
+}
+
+void REGPARM(1) do_sh2_cmp(SH2 *current)
 {
        static int current_slave;
        static u32 current_val;
@@ -218,36 +250,52 @@ void do_sh2_cmp(SH2 *current)
        int bad = 0;
        int cycles;
        int i, ret;
-       char csh2;
 
-       if (f == NULL)
-               f = fopen("tracelog", "rb");
+#if 0
+       sr = current->sr;
+       current->sr &= 0x3f3;
+       do_sh2_trace(current, (signed int)sr >> 12);
+       current->sr = sr;
+       return;
+#endif
+       sh2ref[1].is_slave = 1;
 
        while (1) {
-               ret = fread(&code, 1, 1, f);
+               ret = tl_read(&code, 1);
                if (ret <= 0)
                        break;
-               if (code == 0x84) {
-                       fread(&cycles_o, 1, 4, f);
+               if (code == CTL_CYCLES) {
+                       tl_read(&cycles_o, 4);
                        break;
                }
 
                switch (code) {
-               case 0x80:
-               case 0x81:
+               case CTL_MASTERSLAVE:
+               case CTL_MASTERSLAVE + 1:
                        current_slave = code & 1;
                        break;
-               case 0x82:
-                       fread(&sh2o->ea, 4, 1, f);
+               case CTL_EA:
+                       tl_read_uint(&sh2o->ea);
                        break;
-               case 0x83:
-                       fread(&current_val, 4, 1, f);
+               case CTL_EAVAL:
+                       tl_read_uint(&current_val);
+                       break;
+               case CTL_M68KPC:
+                       tl_read_uint(&val);
+                       if (SekPc != val) {
+                               printf("m68k: %08x %08x\n", SekPc, val);
+                               bad = 1;
+                       }
                        break;
                default:
-                       if (code < offsetof(SH2, read8_map) / 4)
-                               fread(regs_o + code, 4, 1, f);
-                       else {
-                               printf("invalid code: %02x\n", code);
+                       if (CTL_SH2_R <= code && code < CTL_SH2_R +
+                           offsetof(SH2, read8_map) / 4)
+                       {
+                               tl_read_uint(regs_o + code - CTL_SH2_R);
+                       }
+                       else
+                       {
+                               printf("wrong code: %02x\n", code);
                                goto end;
                        }
                        break;
@@ -301,12 +349,10 @@ void do_sh2_cmp(SH2 *current)
 
 end:
        printf("--\n");
-       csh2 = current->is_slave ? 's' : 'm';
-       for (i = 0; i < 16/2; i++)
-               printf("%csh2 r%d: %08x r%02d: %08x\n", csh2,
-                       i, sh2o->r[i], i+8, sh2o->r[i+8]);
-       printf("%csh2 PC: %08x  ,   %08x\n", csh2, sh2o->pc, sh2o->ppc);
-       printf("%csh2 SR:      %03x  PR: %08x\n", csh2, sh2o->sr, sh2o->pr);
+       dump_regs(sh2o);
+       if (current->is_slave != current_slave)
+               dump_regs(&sh2ref[current->is_slave ^ 1]);
+       PDebugDumpMem();
        exit(1);
 }