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