Blaster Master + other fixes
[fceu.git] / ncpu_debug.c
index ebb32fa..ce1b3cc 100644 (file)
@@ -8,22 +8,30 @@
 // asm core state
 extern uint32 nes_registers[0x10];
 extern uint32 pc_base;
-uint32 PC_prev = 0, g_cnt = 0;
+extern uint8  nes_internal_ram[0x800];
+extern uint32 timestamp_a;
+uint32 PC_prev = 0xcccccc, OP_prev = 0xcccccc;
+int32  g_cnt = 0;
 
-int cpu_repeat;
-int cpu_lastval;
+static int pending_add_cycles = 0, pending_rebase = 0, pending_irq = 0;
+
+uint8  dreads[4];
+uint32 dwrites_c[2], dwrites_a[2];
+int dread_count_c, dread_count_a, dwrite_count_c, dwrite_count_a;
+int mapirq_cyc_c, mapirq_cyc_a;
 
 static void leave(void)
 {
        printf("\nA: %02x, X: %02x, Y: %02x, S: %02x\n", X.A, X.X, X.Y, X.S);
-       printf("PC = %04lx, OP=%02X\n", PC_prev, (PC_prev < 0x2000) ? RAM[PC_prev&0x7ff] : ARead[PC_prev](PC_prev));
+       printf("PC = %04lx, OP=%02lX\n", PC_prev, OP_prev);
+       printf("rest = %08lx\n", nes_registers[4]);
        exit(1);
 }
 
 static void compare_state(void)
 {
        uint8 nes_flags;
-       int fail = 0;
+       int i, fail = 0;
 
        if ((nes_registers[0] >> 24) != X.A) {
                printf("A: %02lx vs %02x\n", nes_registers[0] >> 24, X.A);
@@ -66,21 +74,63 @@ static void compare_state(void)
                fail = 1;
        }
 
-       if ((int32)nes_registers[7] != X.count) {
-               printf("cycles: %li vs %li\n", (int32)nes_registers[7], X.count);
+       if (((int32)nes_registers[7] >> 16) != X.count) {
+               printf("cycles: %li vs %li\n", (int32)nes_registers[7] >> 16, X.count);
+               fail = 1;
+       }
+
+       if (dread_count_a != dread_count_c) {
+               printf("dread_count: %i vs %i\n", dread_count_a, dread_count_c);
+               fail = 1;
+       }
+
+       if (dwrite_count_a != dwrite_count_c) {
+               printf("dwrite_count: %i vs %i\n", dwrite_count_a, dwrite_count_c);
+               fail = 1;
+       }
+
+       for (i = dwrite_count_a - 1; !fail && i >= 0; i--)
+               if (dwrites_a[i] != dwrites_c[i]) {
+                       printf("dwrites[%i]: %06lx vs %06lx\n", dwrite_count_a, dwrites_a[i], dwrites_c[i]);
+                       fail = 1;
+               }
+
+       if (mapirq_cyc_a != mapirq_cyc_c) {
+               printf("mapirq_cyc: %i vs %i\n", mapirq_cyc_a, mapirq_cyc_c);
+               fail = 1;
+       }
+
+       if (timestamp_a != timestamp) {
+               printf("timestamp: %lu vs %lu\n", timestamp_a, timestamp);
                fail = 1;
        }
 
        if (fail) leave();
 }
 
+#if 1
+static void compare_ram(void)
+{
+       int i, fail = 0;
+       for (i = 0; i < 0x800/4; i++)
+       {
+               if (((int *)nes_internal_ram)[i] != ((int32 *)RAM)[i]) {
+                       int u;
+                       fail = 1;
+                       for (u = i*4; u < i*4+4; u++)
+                               if (nes_internal_ram[u] != RAM[u])
+                                       printf("RAM[%03x]: %02x vs %02x\n", u, nes_internal_ram[u], RAM[u]);
+               }
+       }
+
+       if (fail) leave();
+}
+#endif
 
 void TriggerIRQ_d(void)
 {
        printf("-- irq\n");
-       TriggerIRQ_c();
-       TriggerIRQ_a();
-       compare_state();
+       pending_irq |= 0x100;
 }
 
 void TriggerNMI_d(void)
@@ -97,26 +147,52 @@ void TriggerNMINSF_d(void)
 
 void X6502_Run_d(int32 c)
 {
-       int32 cycles = c << 4; /* *16 */ \
-       if (PAL) cycles -= c;  /* *15 */ \
+       int32 cycles = c << 4; /* *16 */
+       if (PAL) cycles -= c;  /* *15 */
 
        //printf("-- %06i: run(%i)\n", (int)g_cnt, (int)c);
-       g_cnt += c;
+       g_cnt += cycles;
 
-       while (cycles > 0)
+       if (c > 200)
+               compare_ram();
+
+       while (g_cnt > 0)
        {
-               PC_prev = X.PC;
-               nes_registers[7]=1;
+               if (pending_irq) {
+                       if (pending_irq & 0x100) {
+                               TriggerIRQ_c();
+                               TriggerIRQ_a();
+                       }
+                       if (pending_irq & 0xff) {
+                               TriggerIRQ_c();
+                               TriggerIRQ_a();
+                               X6502_IRQBegin_c(pending_irq & 0xff);
+                               X6502_IRQBegin_a(pending_irq & 0xff);
+                       }
+                       pending_irq = 0;
+               }
+
+               nes_registers[7]=1<<16;
                X.count=1;
 
-               cpu_repeat = 0;
+               dread_count_c = dread_count_a = dwrite_count_c = dwrite_count_a = 0;
+               mapirq_cyc_a = mapirq_cyc_c = 0;
+               timestamp_a = timestamp;
+
                X6502_Run_c();
 
-               cpu_repeat = 1;
                X6502_Run_a();
 
                compare_state();
-               cycles -= 1 - X.count;
+               g_cnt -= 1 - X.count;
+               if (pending_add_cycles) {
+                       g_cnt -= pending_add_cycles*48;
+                       pending_add_cycles = 0;
+               }
+               if (pending_rebase) {
+                       X6502_Rebase_a();
+                       pending_rebase = 0;
+               }
        }
 
        //printf("-- run_end\n");
@@ -134,6 +210,9 @@ void X6502_Reset_d(void)
 void X6502_Power_d(void)
 {
        printf("-- power\n");
+       if (nes_internal_ram == RAM) printf("nes_internal_ram == RAM!!\n");
+       dread_count_c = dread_count_a = dwrite_count_c = dwrite_count_a = 0;
+       mapirq_cyc_c = mapirq_cyc_a = 0;
 
        X6502_Power_c();
        X6502_Power_a();
@@ -144,8 +223,9 @@ void X6502_AddCycles_d(int x)
 {
        printf("-- AddCycles(%i|%i)\n", x, x*48);
 
-       printf("can't use this in debug\n");
-       exit(1);
+       pending_add_cycles = x; // *48;
+//     printf("can't use this in debug\n");
+//     exit(1);
        //X6502_AddCycles_c(x);
        //X6502_AddCycles_a(x);
        //compare_state();
@@ -155,8 +235,9 @@ void X6502_IRQBegin_d(int w)
 {
        printf("-- IRQBegin(%02x)\n", w);
 
-       X6502_IRQBegin_c(w);
-       X6502_IRQBegin_a(w);
+       // X6502_IRQBegin_c(w);
+       // X6502_IRQBegin_a(w);
+       pending_irq |= w;
 }
 
 void X6502_IRQEnd_d(int w)
@@ -168,4 +249,9 @@ void X6502_IRQEnd_d(int w)
 }
 
 
+void X6502_Rebase_d(void)
+{
+       pending_rebase = 1;
+}
+