+ sh2->test_irq = 1;
+}
+
+#ifdef DRC_CMP
+
+/* trace/compare */
+#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 unsigned int mem_val;
+static FILE *f;
+
+enum ctl_byte {
+ CTL_MASTERSLAVE = 0x80,
+ CTL_EA = 0x82,
+ CTL_EAVAL = 0x83,
+ CTL_M68KPC = 0x84,
+ CTL_CYCLES = 0x85,
+};
+
+static unsigned int local_read32(SH2 *sh2, u32 a)
+{
+ const sh2_memmap *sh2_map = sh2->read16_map;
+ u16 *pd;
+ uptr p;
+
+ sh2_map += (a >> 25);
+ p = sh2_map->addr;
+ if (!map_flag_set(p)) {
+ pd = (u16 *)((p << 1) + ((a & sh2_map->mask) & ~1));
+ return (pd[0] << 16) | pd[1];
+ }
+
+ 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];
+ }
+
+ return 0;
+}
+
+static void write_uint(unsigned char ctl, unsigned int v)
+{
+ fwrite(&ctl, 1, 1, f);
+ fwrite(&v, sizeof(v), 1, f);
+}
+
+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;
+ unsigned char v;
+ u32 val;
+ int i;
+
+ if (f == NULL)
+ f = fopen("tracelog", "wb");
+
+ if (SekPc != current_m68k_pc) {
+ current_m68k_pc = SekPc;
+ write_uint(CTL_M68KPC, current_m68k_pc);
+ }
+
+ if (current->is_slave != current_slave) {
+ current_slave = current->is_slave;
+ v = CTL_MASTERSLAVE | current->is_slave;
+ fwrite(&v, 1, 1, f);
+ }
+
+ 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]);
+ regs_o[i] = regs_a[i];
+ }
+ }
+
+ if (current->ea != sh2o->ea) {
+ write_uint(CTL_EA, current->ea);
+ sh2o->ea = current->ea;
+ }
+ val = local_read32(current, current->ea);
+ if (mem_val != val) {
+ write_uint(CTL_EAVAL, val);
+ mem_val = val;
+ }
+ write_uint(CTL_CYCLES, cycles);
+}
+
+static const char *regnames[] = {
+ "r0", "r1", "r2", "r3",
+ "r4", "r5", "r6", "r7",
+ "r8", "r9", "r10", "r11",
+ "r12", "r13", "r14", "r15",
+ "pc", "ppc", "pr", "sr",
+ "gbr", "vbr", "mach","macl",
+};
+
+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 do_sh2_cmp(SH2 *current)
+{
+ static int current_slave;
+ static u32 current_val;
+ SH2 *sh2o = &sh2ref[current->is_slave];
+ u32 *regs_a = (void *)current;
+ u32 *regs_o = (void *)sh2o;
+ unsigned char code;
+ int cycles_o = 666;
+ u32 sr, val;
+ int bad = 0;
+ int cycles;
+ int i, ret;
+
+ if (f == NULL) {
+ f = fopen("tracelog", "rb");
+ sh2ref[1].is_slave = 1;
+ }
+
+ while (1) {
+ ret = fread(&code, 1, 1, f);
+ if (ret <= 0)
+ break;
+ if (code == CTL_CYCLES) {
+ fread(&cycles_o, 1, 4, f);
+ break;
+ }
+
+ switch (code) {
+ case CTL_MASTERSLAVE:
+ case CTL_MASTERSLAVE + 1:
+ current_slave = code & 1;
+ break;
+ case CTL_EA:
+ fread(&sh2o->ea, 4, 1, f);
+ break;
+ case CTL_EAVAL:
+ fread(¤t_val, 4, 1, f);
+ break;
+ case CTL_M68KPC:
+ fread(&val, 4, 1, f);
+ 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);
+ goto end;
+ }
+ break;
+ }
+ }
+
+ if (ret <= 0) {
+ printf("EOF?\n");
+ goto end;
+ }
+
+ if (current->is_slave != current_slave) {
+ printf("bad slave: %d %d\n", current->is_slave,
+ current_slave);
+ bad = 1;
+ }
+
+ for (i = 0; i < offsetof(SH2, read8_map) / 4; i++) {
+ if (i == 17 || i == 19) // ppc, sr
+ continue;
+ if (regs_a[i] != regs_o[i]) {
+ printf("bad %4s: %08x %08x\n",
+ regnames[i], regs_a[i], regs_o[i]);
+ bad = 1;
+ }
+ }
+
+ sr = current->sr & 0x3f3;
+ cycles = (signed int)current->sr >> 12;
+
+ if (sr != sh2o->sr) {
+ printf("bad SR: %03x %03x\n", sr, sh2o->sr);
+ bad = 1;
+ }
+
+ if (cycles != cycles_o) {
+ printf("bad cycles: %d %d\n", cycles, cycles_o);
+ bad = 1;
+ }
+
+ val = local_read32(current, sh2o->ea);
+ if (val != current_val) {
+ printf("bad val @%08x: %08x %08x\n", sh2o->ea, val, current_val);
+ bad = 1;
+ }
+
+ if (!bad) {
+ sh2o->ppc = current->pc;
+ return;
+ }
+
+end:
+ printf("--\n");
+ dump_regs(sh2o);
+ if (current->is_slave != current_slave)
+ dump_regs(&sh2ref[current->is_slave ^ 1]);
+ PDebugDumpMem();
+ exit(1);