improved debug
[fceu.git] / ncpu_debug.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include "types.h"
5 #include "x6502.h"
6 #include "fce.h"
7
8 // asm core state
9 extern uint32 nes_registers[0x10];
10 extern uint32 pc_base;
11 extern uint8  nes_internal_ram[0x800];
12 uint32 PC_prev = 0xcccccc, OP_prev = 0xcccccc;
13 int32  g_cnt = 0;
14
15 static int pending_add_cycles = 0, pending_rebase = 0;
16
17 uint8  dreads[4];
18 uint32 dwrites_c[2], dwrites_a[2];
19 int dread_count_c, dread_count_a, dwrite_count_c, dwrite_count_a;
20
21 static void leave(void)
22 {
23         printf("\nA: %02x, X: %02x, Y: %02x, S: %02x\n", X.A, X.X, X.Y, X.S);
24         printf("PC = %04lx, OP=%02lX\n", PC_prev, OP_prev);
25         exit(1);
26 }
27
28 static void compare_state(void)
29 {
30         uint8 nes_flags;
31         int i, fail = 0;
32
33         if ((nes_registers[0] >> 24) != X.A) {
34                 printf("A: %02lx vs %02x\n", nes_registers[0] >> 24, X.A);
35                 fail = 1;
36         }
37
38         if ((nes_registers[1] & 0xff) != X.X) {
39                 printf("X: %02lx vs %02x\n", nes_registers[1] & 0xff, X.X);
40                 fail = 1;
41         }
42
43         if ((nes_registers[2] & 0xff) != X.Y) {
44                 printf("Y: %02lx vs %02x\n", nes_registers[2] & 0xff, X.Y);
45                 fail = 1;
46         }
47
48         if (nes_registers[3] - pc_base != X.PC) {
49                 printf("PC: %04lx vs %04x\n", nes_registers[3] - pc_base, X.PC);
50                 fail = 1;
51         }
52
53         if ((nes_registers[4] >> 24) != X.S) {
54                 printf("S: %02lx vs %02x\n", nes_registers[4] >> 24, X.S);
55                 fail = 1;
56         }
57
58         if (((nes_registers[4]>>8)&0xff) != X.IRQlow) {
59                 printf("IRQlow: %02lx vs %02x\n", ((nes_registers[4]>>8)&0xff), X.IRQlow);
60                 fail = 1;
61         }
62
63         // NVUB DIZC
64         nes_flags = nes_registers[4] & 0x5d;
65         if (  nes_registers[5]&0x80000000)  nes_flags |= 0x80; // N
66         if (!(nes_registers[5]&0x000000ff)) nes_flags |= 0x02; // Z
67         // nes_flags |= 0x20; // U, not set in C core (set only when pushing)
68
69         if (nes_flags != (X.P&~0x20)) {
70                 printf("flags: %02x vs %02x\n", nes_flags, (X.P&~0x20));
71                 fail = 1;
72         }
73
74         if ((int32)nes_registers[7] != X.count) {
75                 printf("cycles: %li vs %li\n", (int32)nes_registers[7], X.count);
76                 fail = 1;
77         }
78
79         if (dread_count_a != dread_count_c) {
80                 printf("dread_count: %i vs %i\n", dread_count_a, dread_count_c);
81                 fail = 1;
82         }
83
84         if (dwrite_count_a != dwrite_count_c) {
85                 printf("dwrite_count: %i vs %i\n", dwrite_count_a, dwrite_count_c);
86                 fail = 1;
87         }
88
89         for (i = dwrite_count_a - 1; !fail && i >= 0; i--)
90                 if (dwrites_a[i] != dwrites_c[i]) {
91                         printf("dwrites[%i]: %06lx vs %06lx\n", dwrite_count_a, dwrites_a[i], dwrites_c[i]);
92                         fail = 1;
93                 }
94
95         if (fail) leave();
96 }
97
98 #if 1
99 static void compare_ram(void)
100 {
101         int i, fail = 0;
102         for (i = 0; i < 0x800/4; i++)
103         {
104                 if (((int *)nes_internal_ram)[i] != ((int32 *)RAM)[i]) {
105                         int u;
106                         fail = 1;
107                         for (u = i*4; u < i*4+4; u++)
108                                 if (nes_internal_ram[u] != RAM[u])
109                                         printf("RAM[%03x]: %02x vs %02x\n", u, nes_internal_ram[u], RAM[u]);
110                 }
111         }
112
113         if (fail) leave();
114 }
115 #endif
116
117 void TriggerIRQ_d(void)
118 {
119         printf("-- irq\n");
120         TriggerIRQ_c();
121         TriggerIRQ_a();
122         compare_state();
123 }
124
125 void TriggerNMI_d(void)
126 {
127         printf("-- nmi\n");
128         TriggerNMI_c();
129         TriggerNMI_a();
130         compare_state();
131 }
132
133 void TriggerNMINSF_d(void)
134 {
135 }
136
137 void X6502_Run_d(int32 c)
138 {
139         int32 cycles = c << 4; /* *16 */
140         if (PAL) cycles -= c;  /* *15 */
141
142         //printf("-- %06i: run(%i)\n", (int)g_cnt, (int)c);
143         g_cnt += cycles;
144
145         if (c > 200)
146                 compare_ram();
147
148         while (g_cnt > 0)
149         {
150                 nes_registers[7]=1;
151                 X.count=1;
152
153                 dread_count_c = dread_count_a = dwrite_count_c = dwrite_count_a = 0;
154                 X6502_Run_c();
155
156                 X6502_Run_a();
157
158                 compare_state();
159                 g_cnt -= 1 - X.count;
160                 if (pending_add_cycles) {
161                         //X6502_AddCycles_c(pending_add_cycles);
162                         //X6502_AddCycles_a(pending_add_cycles);
163                         g_cnt -= pending_add_cycles*48;
164                         pending_add_cycles = 0;
165                 }
166                 if (pending_rebase) {
167                         X6502_Rebase_a();
168                         pending_rebase = 0;
169                 }
170         }
171
172         //printf("-- run_end\n");
173 }
174
175 void X6502_Reset_d(void)
176 {
177         printf("-- reset\n");
178
179         X6502_Reset_c();
180         X6502_Reset_a();
181         compare_state();
182 }
183
184 void X6502_Power_d(void)
185 {
186         printf("-- power\n");
187         if (nes_internal_ram == RAM) printf("nes_internal_ram == RAM!!\n");
188         dread_count_c = dread_count_a = dwrite_count_c = dwrite_count_a = 0;
189
190         X6502_Power_c();
191         X6502_Power_a();
192         compare_state();
193 }
194
195 void X6502_AddCycles_d(int x)
196 {
197         printf("-- AddCycles(%i|%i)\n", x, x*48);
198
199         pending_add_cycles = x; // *48;
200 //      printf("can't use this in debug\n");
201 //      exit(1);
202         //X6502_AddCycles_c(x);
203         //X6502_AddCycles_a(x);
204         //compare_state();
205 }
206
207 void X6502_IRQBegin_d(int w)
208 {
209         printf("-- IRQBegin(%02x)\n", w);
210
211         X6502_IRQBegin_c(w);
212         X6502_IRQBegin_a(w);
213 }
214
215 void X6502_IRQEnd_d(int w)
216 {
217         printf("-- IRQEnd(%02x)\n", w);
218
219         X6502_IRQEnd_c(w);
220         X6502_IRQEnd_a(w);
221 }
222
223
224 void X6502_Rebase_d(void)
225 {
226         pending_rebase = 1;
227 }
228
229