merged ppu code, added input+zapper, FDS/VS insert in 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 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 TriggerNMINSF_d(void)
157 {
158 }
159
160 void X6502_Run_d(int32 c)
161 {
162         int32 cycles = c << 4; /* *16 */
163         if (PAL) cycles -= c;  /* *15 */
164
165         //printf("-- %06i: run(%i)\n", (int)g_cnt, (int)c);
166         g_cnt += cycles;
167
168         if (framecount != framecount_d) {
169                 compare_ram();
170                 framecount_d = framecount;
171         }
172
173         timestamp_a = timestamp;
174
175         while (g_cnt > 0)
176         {
177                 if (pending_irq) {
178                         if (pending_irq & 0x100) {
179                                 TriggerIRQ_c();
180                                 TriggerIRQ_a();
181                         }
182                         if (pending_irq & 0xff) {
183                                 X6502_IRQBegin_c(pending_irq & 0xff);
184                                 X6502_IRQBegin_a(pending_irq & 0xff);
185                         }
186                         pending_irq = 0;
187                 }
188
189                 nes_registers[7]=1<<16;
190                 X.count=1;
191
192                 dread_count_c = dread_count_a = dwrite_count_c = dwrite_count_a = 0;
193                 mapirq_cyc_a = mapirq_cyc_c = 0;
194                 //timestamp_a = timestamp;
195
196                 X6502_Run_c();
197
198                 X6502_Run_a();
199
200                 compare_state();
201                 g_cnt -= 1 - X.count;
202                 if (pending_add_cycles) {
203                         g_cnt -= pending_add_cycles*48;
204                         //X6502_AddCycles_c(pending_add_cycles);
205                         //X6502_AddCycles_a(pending_add_cycles);
206                         timestamp   += pending_add_cycles;
207                         timestamp_a += pending_add_cycles;
208                         pending_add_cycles = 0;
209                 }
210                 if (pending_rebase) {
211                         X6502_Rebase_a();
212                         pending_rebase = 0;
213                 }
214         }
215
216         //printf("-- run_end\n");
217 }
218
219 void X6502_Reset_d(void)
220 {
221         printf("-- reset\n");
222
223         X6502_Reset_c();
224         X6502_Reset_a();
225         compare_state();
226 }
227
228 void X6502_Power_d(void)
229 {
230         printf("-- power\n");
231         if (nes_internal_ram == RAM) printf("nes_internal_ram == RAM!!\n");
232         dread_count_c = dread_count_a = dwrite_count_c = dwrite_count_a = 0;
233         mapirq_cyc_c = mapirq_cyc_a = 0;
234
235         X6502_Power_c();
236         X6502_Power_a();
237         compare_state();
238 }
239
240 void X6502_AddCycles_d(int x)
241 {
242         printf("-- AddCycles(%i|%i)\n", x, x*48);
243
244         pending_add_cycles = x; // *48;
245 //      printf("can't use this in debug\n");
246 //      exit(1);
247         //X6502_AddCycles_c(x);
248         //X6502_AddCycles_a(x);
249         //compare_state();
250 }
251
252 void X6502_IRQBegin_d(int w)
253 {
254         printf("-- IRQBegin(%02x)\n", w);
255
256         // X6502_IRQBegin_c(w);
257         // X6502_IRQBegin_a(w);
258         pending_irq |= w;
259 }
260
261 void X6502_IRQEnd_d(int w)
262 {
263         printf("-- IRQEnd(%02x)\n", w);
264
265         X6502_IRQEnd_c(w);
266         X6502_IRQEnd_a(w);
267 }
268
269
270 void X6502_Rebase_d(void)
271 {
272         pending_rebase = 1;
273 }
274
275