af32b6c2 |
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; |
c0bf6f9f |
11 | extern uint8 nes_internal_ram[0x800]; |
8fa5eb33 |
12 | extern uint32 timestamp_a; |
0b65fdb3 |
13 | extern uint32 framecount; |
e7f52878 |
14 | extern X6502 X_; |
0b65fdb3 |
15 | static uint32 framecount_d; |
c0bf6f9f |
16 | uint32 PC_prev = 0xcccccc, OP_prev = 0xcccccc; |
17 | int32 g_cnt = 0; |
af32b6c2 |
18 | |
655f8df0 |
19 | static int pending_add_cycles = 0, pending_rebase = 0, pending_irq = 0; |
af32b6c2 |
20 | |
81bd66a1 |
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; |
370cff9a |
24 | int mapirq_cyc_c, mapirq_cyc_a; |
81bd66a1 |
25 | |
e7f52878 |
26 | extern void DumpEmptyCartMapping(void); |
27 | |
af32b6c2 |
28 | static void leave(void) |
29 | { |
92e249b1 |
30 | printf("\nA: %02x, X: %02x, Y: %02x, S: %02x\n", X.A, X.X, X.Y, X.S); |
0b65fdb3 |
31 | printf("PC = %04x, OP=%02X\n", PC_prev, OP_prev); |
32 | printf("rest = %08x\n", nes_registers[4]); |
e7f52878 |
33 | DumpEmptyCartMapping(); |
af32b6c2 |
34 | exit(1); |
35 | } |
36 | |
37 | static void compare_state(void) |
38 | { |
39 | uint8 nes_flags; |
81bd66a1 |
40 | int i, fail = 0; |
af32b6c2 |
41 | |
42 | if ((nes_registers[0] >> 24) != X.A) { |
0b65fdb3 |
43 | printf("A: %02x vs %02x\n", nes_registers[0] >> 24, X.A); |
92e249b1 |
44 | fail = 1; |
af32b6c2 |
45 | } |
46 | |
47 | if ((nes_registers[1] & 0xff) != X.X) { |
0b65fdb3 |
48 | printf("X: %02x vs %02x\n", nes_registers[1] & 0xff, X.X); |
92e249b1 |
49 | fail = 1; |
af32b6c2 |
50 | } |
51 | |
52 | if ((nes_registers[2] & 0xff) != X.Y) { |
0b65fdb3 |
53 | printf("Y: %02x vs %02x\n", nes_registers[2] & 0xff, X.Y); |
92e249b1 |
54 | fail = 1; |
af32b6c2 |
55 | } |
56 | |
57 | if (nes_registers[3] - pc_base != X.PC) { |
0b65fdb3 |
58 | printf("PC: %04x vs %04x\n", nes_registers[3] - pc_base, X.PC); |
92e249b1 |
59 | fail = 1; |
af32b6c2 |
60 | } |
61 | |
62 | if ((nes_registers[4] >> 24) != X.S) { |
0b65fdb3 |
63 | printf("S: %02x vs %02x\n", nes_registers[4] >> 24, X.S); |
92e249b1 |
64 | fail = 1; |
af32b6c2 |
65 | } |
66 | |
67 | if (((nes_registers[4]>>8)&0xff) != X.IRQlow) { |
0b65fdb3 |
68 | printf("IRQlow: %02x vs %02x\n", ((nes_registers[4]>>8)&0xff), X.IRQlow); |
92e249b1 |
69 | fail = 1; |
af32b6c2 |
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 | |
92e249b1 |
78 | if (nes_flags != (X.P&~0x20)) { |
79 | printf("flags: %02x vs %02x\n", nes_flags, (X.P&~0x20)); |
80 | fail = 1; |
af32b6c2 |
81 | } |
82 | |
8fa5eb33 |
83 | if (((int32)nes_registers[7] >> 16) != X.count) { |
0b65fdb3 |
84 | printf("cycles: %i vs %i\n", (int32)nes_registers[7] >> 16, X.count); |
92e249b1 |
85 | fail = 1; |
af32b6c2 |
86 | } |
92e249b1 |
87 | |
81bd66a1 |
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]) { |
0b65fdb3 |
100 | printf("dwrites[%i]: %06x vs %06x\n", dwrite_count_a, dwrites_a[i], dwrites_c[i]); |
81bd66a1 |
101 | fail = 1; |
102 | } |
103 | |
370cff9a |
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 | |
8fa5eb33 |
109 | if (timestamp_a != timestamp) { |
0b65fdb3 |
110 | printf("timestamp: %u vs %u\n", timestamp_a, timestamp); |
8fa5eb33 |
111 | fail = 1; |
112 | } |
113 | |
e7f52878 |
114 | /* |
115 | if (X_.DB != X.DB) { |
116 | printf("DB: %02x vs %02x\n", X_.DB, X.DB); |
117 | fail = 1; |
118 | } |
119 | */ |
92e249b1 |
120 | if (fail) leave(); |
af32b6c2 |
121 | } |
122 | |
c0bf6f9f |
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 |
af32b6c2 |
141 | |
142 | void TriggerIRQ_d(void) |
143 | { |
144 | printf("-- irq\n"); |
655f8df0 |
145 | pending_irq |= 0x100; |
af32b6c2 |
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 | { |
c0bf6f9f |
162 | int32 cycles = c << 4; /* *16 */ |
163 | if (PAL) cycles -= c; /* *15 */ |
af32b6c2 |
164 | |
92e249b1 |
165 | //printf("-- %06i: run(%i)\n", (int)g_cnt, (int)c); |
c0bf6f9f |
166 | g_cnt += cycles; |
af32b6c2 |
167 | |
0b65fdb3 |
168 | if (framecount != framecount_d) { |
c0bf6f9f |
169 | compare_ram(); |
0b65fdb3 |
170 | framecount_d = framecount; |
171 | } |
172 | |
173 | timestamp_a = timestamp; |
c0bf6f9f |
174 | |
175 | while (g_cnt > 0) |
af32b6c2 |
176 | { |
655f8df0 |
177 | if (pending_irq) { |
178 | if (pending_irq & 0x100) { |
179 | TriggerIRQ_c(); |
180 | TriggerIRQ_a(); |
181 | } |
182 | if (pending_irq & 0xff) { |
655f8df0 |
183 | X6502_IRQBegin_c(pending_irq & 0xff); |
184 | X6502_IRQBegin_a(pending_irq & 0xff); |
185 | } |
186 | pending_irq = 0; |
187 | } |
188 | |
8fa5eb33 |
189 | nes_registers[7]=1<<16; |
af32b6c2 |
190 | X.count=1; |
92e249b1 |
191 | |
81bd66a1 |
192 | dread_count_c = dread_count_a = dwrite_count_c = dwrite_count_a = 0; |
370cff9a |
193 | mapirq_cyc_a = mapirq_cyc_c = 0; |
0b65fdb3 |
194 | //timestamp_a = timestamp; |
8fa5eb33 |
195 | |
af32b6c2 |
196 | X6502_Run_c(); |
92e249b1 |
197 | |
af32b6c2 |
198 | X6502_Run_a(); |
92e249b1 |
199 | |
af32b6c2 |
200 | compare_state(); |
c0bf6f9f |
201 | g_cnt -= 1 - X.count; |
202 | if (pending_add_cycles) { |
c0bf6f9f |
203 | g_cnt -= pending_add_cycles*48; |
0b65fdb3 |
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; |
c0bf6f9f |
208 | pending_add_cycles = 0; |
209 | } |
210 | if (pending_rebase) { |
211 | X6502_Rebase_a(); |
212 | pending_rebase = 0; |
213 | } |
af32b6c2 |
214 | } |
92e249b1 |
215 | |
216 | //printf("-- run_end\n"); |
af32b6c2 |
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"); |
c0bf6f9f |
231 | if (nes_internal_ram == RAM) printf("nes_internal_ram == RAM!!\n"); |
81bd66a1 |
232 | dread_count_c = dread_count_a = dwrite_count_c = dwrite_count_a = 0; |
370cff9a |
233 | mapirq_cyc_c = mapirq_cyc_a = 0; |
af32b6c2 |
234 | |
235 | X6502_Power_c(); |
236 | X6502_Power_a(); |
237 | compare_state(); |
238 | } |
239 | |
240 | void X6502_AddCycles_d(int x) |
241 | { |
92e249b1 |
242 | printf("-- AddCycles(%i|%i)\n", x, x*48); |
af32b6c2 |
243 | |
c0bf6f9f |
244 | pending_add_cycles = x; // *48; |
245 | // printf("can't use this in debug\n"); |
246 | // exit(1); |
92e249b1 |
247 | //X6502_AddCycles_c(x); |
248 | //X6502_AddCycles_a(x); |
af32b6c2 |
249 | //compare_state(); |
250 | } |
251 | |
252 | void X6502_IRQBegin_d(int w) |
253 | { |
254 | printf("-- IRQBegin(%02x)\n", w); |
255 | |
655f8df0 |
256 | // X6502_IRQBegin_c(w); |
257 | // X6502_IRQBegin_a(w); |
258 | pending_irq |= w; |
af32b6c2 |
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 | |
c0bf6f9f |
270 | void X6502_Rebase_d(void) |
271 | { |
272 | pending_rebase = 1; |
273 | } |
274 | |
af32b6c2 |
275 | |