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 | |
92e249b1 |
15 | int cpu_repeat; |
16 | int cpu_lastval; |
c0bf6f9f |
17 | static int pending_add_cycles = 0, pending_rebase = 0; |
af32b6c2 |
18 | |
19 | static void leave(void) |
20 | { |
92e249b1 |
21 | printf("\nA: %02x, X: %02x, Y: %02x, S: %02x\n", X.A, X.X, X.Y, X.S); |
c0bf6f9f |
22 | printf("PC = %04lx, OP=%02lX\n", PC_prev, OP_prev); |
23 | printf("cpu_lastval = %02x\n", cpu_lastval); |
af32b6c2 |
24 | exit(1); |
25 | } |
26 | |
27 | static void compare_state(void) |
28 | { |
29 | uint8 nes_flags; |
92e249b1 |
30 | int fail = 0; |
af32b6c2 |
31 | |
32 | if ((nes_registers[0] >> 24) != X.A) { |
33 | printf("A: %02lx vs %02x\n", nes_registers[0] >> 24, X.A); |
92e249b1 |
34 | fail = 1; |
af32b6c2 |
35 | } |
36 | |
37 | if ((nes_registers[1] & 0xff) != X.X) { |
38 | printf("X: %02lx vs %02x\n", nes_registers[1] & 0xff, X.X); |
92e249b1 |
39 | fail = 1; |
af32b6c2 |
40 | } |
41 | |
42 | if ((nes_registers[2] & 0xff) != X.Y) { |
43 | printf("Y: %02lx vs %02x\n", nes_registers[2] & 0xff, X.Y); |
92e249b1 |
44 | fail = 1; |
af32b6c2 |
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); |
92e249b1 |
49 | fail = 1; |
af32b6c2 |
50 | } |
51 | |
52 | if ((nes_registers[4] >> 24) != X.S) { |
53 | printf("S: %02lx vs %02x\n", nes_registers[4] >> 24, X.S); |
92e249b1 |
54 | fail = 1; |
af32b6c2 |
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); |
92e249b1 |
59 | fail = 1; |
af32b6c2 |
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 | |
92e249b1 |
68 | if (nes_flags != (X.P&~0x20)) { |
69 | printf("flags: %02x vs %02x\n", nes_flags, (X.P&~0x20)); |
70 | fail = 1; |
af32b6c2 |
71 | } |
72 | |
73 | if ((int32)nes_registers[7] != X.count) { |
74 | printf("cycles: %li vs %li\n", (int32)nes_registers[7], X.count); |
92e249b1 |
75 | fail = 1; |
af32b6c2 |
76 | } |
92e249b1 |
77 | |
78 | if (fail) leave(); |
af32b6c2 |
79 | } |
80 | |
c0bf6f9f |
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 |
af32b6c2 |
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 | { |
c0bf6f9f |
122 | int32 cycles = c << 4; /* *16 */ |
123 | if (PAL) cycles -= c; /* *15 */ |
af32b6c2 |
124 | |
92e249b1 |
125 | //printf("-- %06i: run(%i)\n", (int)g_cnt, (int)c); |
c0bf6f9f |
126 | g_cnt += cycles; |
af32b6c2 |
127 | |
c0bf6f9f |
128 | if (c > 200) |
129 | compare_ram(); |
130 | |
131 | while (g_cnt > 0) |
af32b6c2 |
132 | { |
af32b6c2 |
133 | nes_registers[7]=1; |
134 | X.count=1; |
92e249b1 |
135 | |
c0bf6f9f |
136 | cpu_lastval = 0; |
92e249b1 |
137 | cpu_repeat = 0; |
af32b6c2 |
138 | X6502_Run_c(); |
92e249b1 |
139 | |
140 | cpu_repeat = 1; |
af32b6c2 |
141 | X6502_Run_a(); |
92e249b1 |
142 | |
af32b6c2 |
143 | compare_state(); |
c0bf6f9f |
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 | } |
af32b6c2 |
155 | } |
92e249b1 |
156 | |
157 | //printf("-- run_end\n"); |
af32b6c2 |
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"); |
c0bf6f9f |
172 | if (nes_internal_ram == RAM) printf("nes_internal_ram == RAM!!\n"); |
af32b6c2 |
173 | |
174 | X6502_Power_c(); |
175 | X6502_Power_a(); |
176 | compare_state(); |
177 | } |
178 | |
179 | void X6502_AddCycles_d(int x) |
180 | { |
92e249b1 |
181 | printf("-- AddCycles(%i|%i)\n", x, x*48); |
af32b6c2 |
182 | |
c0bf6f9f |
183 | pending_add_cycles = x; // *48; |
184 | // printf("can't use this in debug\n"); |
185 | // exit(1); |
92e249b1 |
186 | //X6502_AddCycles_c(x); |
187 | //X6502_AddCycles_a(x); |
af32b6c2 |
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 | |
c0bf6f9f |
208 | void X6502_Rebase_d(void) |
209 | { |
210 | pending_rebase = 1; |
211 | } |
212 | |
af32b6c2 |
213 | |