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