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