forked from superzazu/6502
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm6502_tests.c
367 lines (300 loc) · 9.79 KB
/
m6502_tests.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "m6502.h"
static m6502 cpu;
// memory callbacks
#define MEMORY_SIZE 0x10000
static uint8_t* memory;
static uint8_t rb(void* userdata, uint16_t addr) {
return memory[addr];
}
static void wb(void* userdata, uint16_t addr, uint8_t val) {
memory[addr] = val;
}
static int load_file_into_memory(const char* filename, uint16_t addr) {
FILE* f = fopen(filename, "rb");
if (f == NULL) {
fprintf(stderr, "error: can't open file '%s'.\n", filename);
return 1;
}
// file size check:
fseek(f, 0, SEEK_END);
unsigned long file_size = ftell(f);
rewind(f);
if (file_size + addr > MEMORY_SIZE) {
fprintf(stderr, "error: file %s can't fit in memory.\n", filename);
fclose(f);
return 1;
}
// copying the bytes in the memory:
size_t result = fread(&memory[addr], sizeof(uint8_t), file_size, f);
if (result != file_size) {
fprintf(stderr, "error: while reading file '%s'\n", filename);
fclose(f);
return 1;
}
fclose(f);
return 0;
}
static int test_allsuitea(unsigned long expected_cyc) {
printf("AllSuiteA: ");
memset(memory, 0, MEMORY_SIZE);
load_file_into_memory("programs/AllSuiteA.bin", 0x4000);
m6502_init(&cpu);
cpu.read_byte = &rb;
cpu.write_byte = &wb;
m6502_gen_res(&cpu);
int nb_instructions_executed = 0;
while (true) {
m6502_step(&cpu);
nb_instructions_executed += 1;
if (cpu.pc == 0x45C0) {
if (rb(&cpu, 0x0210) == 0xFF) {
printf("PASS");
}
else {
printf("FAIL");
}
break;
}
}
long long diff = expected_cyc - cpu.cyc;
printf(" (%d instructions executed on %lu cycles, "
" expected=%lu, diff=%lld)\n",
nb_instructions_executed, cpu.cyc,
expected_cyc, diff);
return cpu.cyc != expected_cyc;
}
static int test_6502_functional_test(unsigned long expected_cyc) {
printf("6502_functional_test: ");
memset(memory, 0, MEMORY_SIZE);
if (load_file_into_memory("programs/6502_65C02_functional_tests/bin_files/6502_functional_test.bin", 0) != 0) {
return 1;
}
m6502_init(&cpu);
cpu.read_byte = &rb;
cpu.write_byte = &wb;
cpu.pc = 0x400;
int nb_instructions_executed = 0;
uint16_t previous_pc = 0;
while (true) {
m6502_step(&cpu);
nb_instructions_executed += 1;
// if the program is trapped somewhere, print and exit
if (previous_pc == cpu.pc) {
if (cpu.pc == 0x3469) {
printf("PASS");
break;
}
printf("FAIL (trapped at 0x%04X)", cpu.pc);
break;
}
previous_pc = cpu.pc;
}
long long diff = expected_cyc - cpu.cyc;
printf(" (%d instructions executed on %lu cycles, "
" expected=%lu, diff=%lld)\n",
nb_instructions_executed, cpu.cyc,
expected_cyc, diff);
return cpu.cyc != expected_cyc;
}
static int test_6502_decimal_test(unsigned long expected_cyc) {
printf("6502_decimal_test: ");
memset(memory, 0, MEMORY_SIZE);
if (load_file_into_memory("programs/6502_decimal_test.bin", 0x200) != 0) {
return 1;
}
m6502_init(&cpu);
cpu.read_byte = &rb;
cpu.write_byte = &wb;
cpu.pc = 0x200;
int nb_instructions_executed = 0;
while (true) {
m6502_step(&cpu);
nb_instructions_executed += 1;
if (cpu.pc == 0x024b) {
printf("%s", cpu.a == 0 ? "PASS" : "FAIL");
break;
}
}
long long diff = expected_cyc - cpu.cyc;
printf(" (%d instructions executed on %lu cycles, "
" expected=%lu, diff=%lld)\n",
nb_instructions_executed, cpu.cyc,
expected_cyc, diff);
return cpu.cyc != expected_cyc;
}
static int test_timingtest(unsigned long expected_cyc) {
printf("timingtest: ");
memset(memory, 0, MEMORY_SIZE);
if (load_file_into_memory("programs/timingtest/timingtest-1.bin", 0x1000) != 0) {
return 1;
}
m6502_init(&cpu);
cpu.read_byte = &rb;
cpu.write_byte = &wb;
cpu.pc = 0x1000;
int nb_instructions_executed = 0;
while (true) {
m6502_step(&cpu);
nb_instructions_executed += 1;
if (cpu.pc == 0x1269) {
printf("%s", cpu.cyc == 1141 ? "PASS" : "FAIL");
break;
}
}
long long diff = expected_cyc - cpu.cyc;
printf(" (%d instructions executed on %lu cycles, "
" expected=%lu, diff=%lld)\n",
nb_instructions_executed, cpu.cyc,
expected_cyc, diff);
return cpu.cyc != expected_cyc;
}
static int test_65C02_extended_opcodes_test(unsigned long expected_cyc) {
printf("65C02_extended_opcodes_test: ");
memset(memory, 0, MEMORY_SIZE);
if (load_file_into_memory("programs/6502_65C02_functional_tests/bin_files/65C02_extended_opcodes_test.bin", 0) != 0) {
return 1;
}
m6502_init(&cpu);
cpu.read_byte = &rb;
cpu.write_byte = &wb;
cpu.pc = 0x400;
cpu.m65c02_mode = 1;
int nb_instructions_executed = 0;
uint16_t previous_pc = 0;
while (true) {
m6502_step(&cpu);
nb_instructions_executed += 1;
// if the program is trapped somewhere, print and exit
if (previous_pc == cpu.pc) {
if (cpu.pc == 0x24F1) {
printf("PASS");
break;
}
printf("FAIL (trapped at 0x%04X)", cpu.pc);
break;
}
previous_pc = cpu.pc;
}
long long diff = expected_cyc - cpu.cyc;
printf(" (%d instructions executed on %lu cycles, "
" expected=%lu, diff=%lld)\n",
nb_instructions_executed, cpu.cyc,
expected_cyc, diff);
return cpu.cyc != expected_cyc;
}
static int test_6502_interrupt_test(unsigned long expected_cyc) {
printf("6502_interrupt_test: ");
memset(memory, 0, MEMORY_SIZE);
load_file_into_memory("programs/6502_interrupt_test.bin", 0xA);
m6502_init(&cpu);
cpu.read_byte = &rb;
cpu.write_byte = &wb;
cpu.pc = 0x400;
int nb_instructions_executed = 0;
uint16_t previous_pc = 0;
wb(&cpu, 0xBFFC, 0); // initialise 0xBFFC
while (true) {
// m6502_debug_output(&cpu);
m6502_step(&cpu);
cpu.irq_status = rb(&cpu, 0xBFFC); // read interrupt status
m6502_interrupt_handler(&cpu);
wb(&cpu, 0xBFFC, cpu.irq_status); // write new value to 0xBFFC
nb_instructions_executed += 1;
// if the program is trapped somewhere, print and exit
if (previous_pc == cpu.pc) {
if (cpu.pc == 0x06f5) {
printf("PASS");
break;
}
printf("FAIL (trapped at 0x%04X)", cpu.pc);
break;
}
previous_pc = cpu.pc;
}
long long diff = expected_cyc - cpu.cyc;
printf(" (%d instructions executed on %lu cycles, "
" expected=%lu, diff=%lld)\n",
nb_instructions_executed, cpu.cyc,
expected_cyc, diff);
return cpu.cyc != expected_cyc;
}
// The following test has been assembled with this configuration:
// cputype = 1 ; 0 = 6502, 1 = 65C02, 2 = 65C816
// vld_bcd = 0 ; 0 = allow invalid bcd, 1 = valid bcd only
// chk_a = 1 ; check accumulator
// chk_n = 1 ; check sign (negative) flag
// chk_v = 0 ; check overflow flag
// chk_z = 1 ; check zero flag
// chk_c = 1 ; check carry flag
static int test_65C02_decimal_test(unsigned long expected_cyc) {
printf("65C02_decimal_test: ");
memset(memory, 0, MEMORY_SIZE);
if (load_file_into_memory("programs/65C02_decimal_test.bin", 0x200) != 0) {
return 1;
}
m6502_init(&cpu);
cpu.read_byte = &rb;
cpu.write_byte = &wb;
cpu.pc = 0x200;
cpu.m65c02_mode = 1;
int nb_instructions_executed = 0;
while (true) {
m6502_step(&cpu);
nb_instructions_executed += 1;
if (cpu.pc == 0x024b) {
printf("%s", cpu.a == 0 ? "PASS" : "FAIL");
break;
}
}
long long diff = expected_cyc - cpu.cyc;
printf(" (%d instructions executed on %lu cycles, "
" expected=%lu, diff=%lld)\n",
nb_instructions_executed, cpu.cyc,
expected_cyc, diff);
return cpu.cyc != expected_cyc;
}
// static int test_65c02_timingtest(unsigned long expected_cyc) {
// printf("timingtest: ");
// memset(memory, 0, MEMORY_SIZE);
// load_file_into_memory("programs/65c02timing/65c02timing.bin", 0x6000);
// m6502_init(&cpu);
// cpu.read_byte = &rb;
// cpu.write_byte = &wb;
// cpu.pc = 0x6000;
// cpu.m65c02_mode = 1;
// int nb_instructions_executed = 0;
// uint16_t previous_pc = 0;
// while (true) {
// // m6502_debug_output(&cpu);
// m6502_step(&cpu);
// nb_instructions_executed += 1;
// // if the program is trapped somewhere, print and exit
// if (previous_pc == cpu.pc) {
// printf("%s", cpu.y == 1 ? "PASS" : "FAIL");
// break;
// }
// previous_pc = cpu.pc;
// }
// long long diff = expected_cyc - cpu.cyc;
// printf(" (%d instructions executed on %lu cycles,"
// " expected=%lu, diff=%lld)\n",
// nb_instructions_executed, cpu.cyc,
// expected_cyc, diff);
// }
int main(void) {
memory = malloc(MEMORY_SIZE);
int r = 0;
// r += test_allsuitea(1946LU);
// r += test_6502_functional_test(96241367LU); // same cycle count on fake6502
// r += test_6502_decimal_test(46089505LU);
// r += test_timingtest(1141LU);
// r += test_65C02_extended_opcodes_test(66886142LU);
r += test_6502_interrupt_test(0LU);
// r += test_65C02_decimal_test(0LU);
// r += test_65c02_timingtest(0LU);
free(memory);
return r != 0;
}