cmdline parser fix, missing init stuff
[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 extern uint32 timestamp_a;
13 extern uint32 framecount;
14 extern X6502 X_;
15 static uint32 framecount_d;
16 uint32 PC_prev = 0xcccccc, OP_prev = 0xcccccc;
17 int32  g_cnt = 0;
18
19 static int pending_add_cycles = 0, pending_rebase = 0, pending_irq = 0;
20
21 uint8  dreads[4];
22 uint32 dwrites_c[2], dwrites_a[2];
23 int dread_count_c, dread_count_a, dwrite_count_c, dwrite_count_a;
24 int mapirq_cyc_c, mapirq_cyc_a;
25
26 extern void DumpEmptyCartMapping(void);
27
28 static void leave(void)
29 {
30         printf("\nA: %02x, X: %02x, Y: %02x, S: %02x\n", X.A, X.X, X.Y, X.S);
31         printf("PC = %04x, OP=%02X\n", PC_prev, OP_prev);
32         printf("rest = %08x\n", nes_registers[4]);
33         DumpEmptyCartMapping();
34         exit(1);
35 }
36
37 static void compare_state(void)
38 {
39         uint8 nes_flags;
40         int i, fail = 0;
41
42         if ((nes_registers[0] >> 24) != X.A) {
43                 printf("A: %02x vs %02x\n", nes_registers[0] >> 24, X.A);
44                 fail = 1;
45         }
46
47         if ((nes_registers[1] & 0xff) != X.X) {
48                 printf("X: %02x vs %02x\n", nes_registers[1] & 0xff, X.X);
49                 fail = 1;
50         }
51
52         if ((nes_registers[2] & 0xff) != X.Y) {
53                 printf("Y: %02x vs %02x\n", nes_registers[2] & 0xff, X.Y);
54                 fail = 1;
55         }
56
57         if (nes_registers[3] - pc_base != X.PC) {
58                 printf("PC: %04x vs %04x\n", nes_registers[3] - pc_base, X.PC);
59                 fail = 1;
60         }
61
62         if ((nes_registers[4] >> 24) != X.S) {
63                 printf("S: %02x vs %02x\n", nes_registers[4] >> 24, X.S);
64                 fail = 1;
65         }
66
67         if (((nes_registers[4]>>8)&0xff) != X.IRQlow) {
68                 printf("IRQlow: %02x vs %02x\n", ((nes_registers[4]>>8)&0xff), X.IRQlow);
69                 fail = 1;
70         }
71
72         // NVUB DIZC
73         nes_flags = nes_registers[4] & 0x5d;
74         if (  nes_registers[5]&0x80000000)  nes_flags |= 0x80; // N
75         if (!(nes_registers[5]&0x000000ff)) nes_flags |= 0x02; // Z
76         // nes_flags |= 0x20; // U, not set in C core (set only when pushing)
77
78         if (nes_flags != (X.P&~0x20)) {
79                 printf("flags: %02x vs %02x\n", nes_flags, (X.P&~0x20));
80                 fail = 1;
81         }
82
83         if (((int32)nes_registers[7] >> 16) != X.count) {
84                 printf("cycles: %i vs %i\n", (int32)nes_registers[7] >> 16, X.count);
85                 fail = 1;
86         }
87
88         if (dread_count_a != dread_count_c) {
89                 printf("dread_count: %i vs %i\n", dread_count_a, dread_count_c);
90                 fail = 1;
91         }
92
93         if (dwrite_count_a != dwrite_count_c) {
94                 printf("dwrite_count: %i vs %i\n", dwrite_count_a, dwrite_count_c);
95                 fail = 1;
96         }
97
98         for (i = dwrite_count_a - 1; !fail && i >= 0; i--)
99                 if (dwrites_a[i] != dwrites_c[i]) {
100                         printf("dwrites[%i]: %06x vs %06x\n", dwrite_count_a, dwrites_a[i], dwrites_c[i]);
101                         fail = 1;
102                 }
103
104         if (mapirq_cyc_a != mapirq_cyc_c) {
105                 printf("mapirq_cyc: %i vs %i\n", mapirq_cyc_a, mapirq_cyc_c);
106                 fail = 1;
107         }
108
109         if (timestamp_a != timestamp) {
110                 printf("timestamp: %u vs %u\n", timestamp_a, timestamp);
111                 fail = 1;
112         }
113
114 /*
115         if (X_.DB != X.DB) {
116                 printf("DB: %02x vs %02x\n", X_.DB, X.DB);
117                 fail = 1;
118         }
119 */
120         if (fail) leave();
121 }
122
123 #if 1
124 static void compare_ram(void)
125 {
126         int i, fail = 0;
127         for (i = 0; i < 0x800/4; i++)
128         {
129                 if (((int *)nes_internal_ram)[i] != ((int32 *)RAM)[i]) {
130                         int u;
131                         fail = 1;
132                         for (u = i*4; u < i*4+4; u++)
133                                 if (nes_internal_ram[u] != RAM[u])
134                                         printf("RAM[%03x]: %02x vs %02x\n", u, nes_internal_ram[u], RAM[u]);
135                 }
136         }
137
138         if (fail) leave();
139 }
140 #endif
141
142 void TriggerIRQ_d(void)
143 {
144         printf("-- irq\n");
145         pending_irq |= 0x100;
146 }
147
148 void TriggerNMI_d(void)
149 {
150         printf("-- nmi\n");
151         TriggerNMI_c();
152         TriggerNMI_a();
153         compare_state();
154 }
155
156 void X6502_Run_d(int32 c)
157 {
158         int32 cycles = c << 4; /* *16 */
159         if (PAL) cycles -= c;  /* *15 */
160
161         //printf("-- %06i: run(%i)\n", (int)g_cnt, (int)c);
162         g_cnt += cycles;
163
164         if (framecount != framecount_d) {
165                 compare_ram();
166                 framecount_d = framecount;
167         }
168
169         timestamp_a = timestamp;
170
171         while (g_cnt > 0)
172         {
173                 if (pending_irq) {
174                         if (pending_irq & 0x100) {
175                                 TriggerIRQ_c();
176                                 TriggerIRQ_a();
177                         }
178                         if (pending_irq & 0xff) {
179                                 X6502_IRQBegin_c(pending_irq & 0xff);
180                                 X6502_IRQBegin_a(pending_irq & 0xff);
181                         }
182                         pending_irq = 0;
183                 }
184
185                 //printf("%04x: %02x\n", nes_registers[3] - pc_base, *(unsigned char *)nes_registers[3]);
186
187                 nes_registers[7]=1<<16;
188                 X.count=1;
189
190                 dread_count_c = dread_count_a = dwrite_count_c = dwrite_count_a = 0;
191                 mapirq_cyc_a = mapirq_cyc_c = 0;
192                 //timestamp_a = timestamp;
193
194                 X6502_Run_c();
195
196                 X6502_Run_a();
197
198                 compare_state();
199                 g_cnt -= 1 - X.count;
200                 if (pending_add_cycles) {
201                         g_cnt -= pending_add_cycles*48;
202                         //X6502_AddCycles_c(pending_add_cycles);
203                         //X6502_AddCycles_a(pending_add_cycles);
204                         timestamp   += pending_add_cycles;
205                         timestamp_a += pending_add_cycles;
206                         pending_add_cycles = 0;
207                 }
208                 if (pending_rebase) {
209                         X6502_Rebase_a();
210                         pending_rebase = 0;
211                 }
212         }
213
214         //printf("-- run_end\n");
215 }
216
217 void X6502_Reset_d(void)
218 {
219         printf("-- reset\n");
220
221         X6502_Reset_c();
222         X6502_Reset_a();
223         compare_state();
224 }
225
226 void X6502_Power_d(void)
227 {
228         printf("-- power\n");
229         if (nes_internal_ram == RAM) printf("nes_internal_ram == RAM!!\n");
230         dread_count_c = dread_count_a = dwrite_count_c = dwrite_count_a = 0;
231         mapirq_cyc_c = mapirq_cyc_a = 0;
232
233         X6502_Power_c();
234         X6502_Power_a();
235         compare_state();
236
237 #if 0
238         {
239                 unsigned char *p = (void *) nes_registers[3];
240                 int i, u, nop = 0xea;
241
242                 for (i = 0; i < 256; i++)
243                 {
244                         if (i == 0 || i == 0x20 || i == 0x40 || i == 0x60 || i == 0x4c || i == 0x6c) continue; /* BRK, JSR, RET, etc. */
245                         if ((i & 0x1f) == 0x10) continue; /* Bxx */
246                         switch (i)
247                         {
248                                 case 0x02: /* JAM */
249                                 case 0x12:
250                                 case 0x22:
251                                 case 0x32:
252                                 case 0x42:
253                                 case 0x52:
254                                 case 0x62:
255                                 case 0x72:
256                                 case 0x92:
257                                 case 0xB2:
258                                 case 0xD2:
259                                 case 0xF2: continue;
260                         }
261
262                         *p++ = i;
263                         for (u = 0; u < 3; u++)
264                                 *p++ = nop;
265                 }
266         }
267 #endif
268 }
269
270 void X6502_AddCycles_d(int x)
271 {
272         printf("-- AddCycles(%i|%i)\n", x, x*48);
273
274         pending_add_cycles = x; // *48;
275 //      printf("can't use this in debug\n");
276 //      exit(1);
277         //X6502_AddCycles_c(x);
278         //X6502_AddCycles_a(x);
279         //compare_state();
280 }
281
282 void X6502_IRQBegin_d(int w)
283 {
284         printf("-- IRQBegin(%02x)\n", w);
285
286         // X6502_IRQBegin_c(w);
287         // X6502_IRQBegin_a(w);
288         pending_irq |= w;
289 }
290
291 void X6502_IRQEnd_d(int w)
292 {
293         printf("-- IRQEnd(%02x)\n", w);
294
295         X6502_IRQEnd_c(w);
296         X6502_IRQEnd_a(w);
297 }
298
299
300 void X6502_Rebase_d(void)
301 {
302         pending_rebase = 1;
303 }
304
305