-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_6502.c
663 lines (501 loc) · 11.4 KB
/
test_6502.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
/*
* test_6502.c
*
* test the "cpu6502" core.
*/
/* $Id: test_6502.c,v 1.6 2000/09/23 02:29:29 nyef Exp $ */
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdarg.h>
#include "types.h"
#include "ui.h"
#include "cal.h"
#include "tool.h"
#define TEST_CPU6502
/* #define TEST_MARAT6502 */
#ifdef TEST_CPU6502
#include "cpu6502int.h"
#endif
#ifdef TEST_MARAT6502
#include "m6502.h"
#define reg_a A
#define reg_x X
#define reg_y Y
#define reg_s S
#define pc PC.W
#define flags P
#define NO_CYCLE_CHECKS
#define SET_FLAGS(context, data) context->P = (data)
#define GET_FLAGS(context) context->P
#define cpu6502_context M6502
#endif
#define MAX_MEMLOCS 32
cal_cpu test_cpu;
struct cpu6502_context *context;
u8 page_0[0x200];
u32 system_flags;
int did_test_fail;
int num_passed;
int num_failed;
int in_test;
char cur_test[256];
int cur_line;
typedef void (*testfunc)(void);
struct memloc {
u32 address;
u8 data;
};
struct memloc memlocs[MAX_MEMLOCS];
int memlocs_used;
#define SET_MEMORY(addr, data8) do { memlocs[memlocs_used].data = data8; memlocs[memlocs_used].address = addr; memlocs_used++; if (addr < 0x200) page_0[addr] = data8; } while (0)
u8 test_read8(cal_cpu cpu, u32 address)
{
int i;
u32 addr;
addr = address & 0xffffff;
if (addr < 0x0200) {
return page_0[addr];
}
for (i = 0; i < memlocs_used; i++) {
if (memlocs[i].address == addr) {
return memlocs[i].data;
}
}
printf("read8 access to unknown location 0x%06lx.\n", addr);
did_test_fail = 1;
return 0;
}
void test_write8(cal_cpu cpu, u32 address, u8 data)
{
int i;
u32 addr;
addr = address & 0xffffff;
if (addr < 0x0200) {
page_0[addr] = data;
}
for (i = 0; i < memlocs_used; i++) {
if (memlocs[i].address == addr) {
memlocs[i].data = data;
return;
}
}
printf("write8 access 0x%02x to unknown location 0x%06lx.\n", data, addr);
did_test_fail = 1;
}
memread8_t read8table[1] = {test_read8};
memwrite8_t write8table[1] = {test_write8};
void clear_cpu(void)
{
bzero(page_0, sizeof(page_0));
context->reg_a = 0x00;
context->reg_x = 0x00;
context->reg_y = 0x00;
context->reg_s = 0xff;
context->pc = 0;
context->flags = 0;
#ifndef NO_CYCLE_CHECKS
context->cycles_left = 0;
#endif
memlocs_used = 0;
}
void deb_printf(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
did_test_fail = 1;
}
char *strip_blanks(char *buf)
{
char *bufptr;
bufptr = buf;
while (isblank(*bufptr)) {
bufptr++;
}
return bufptr;
}
typedef int (*line_handler)(char *);
struct parse_table {
char *match;
line_handler handler;
};
int do_start(char *buf)
{
if (in_test) {
printf("start found while already in test.\n");
return 1;
}
if (!*buf) {
printf("test has no name.\n");
return 1;
}
strcpy(cur_test, buf);
did_test_fail = 0;
clear_cpu();
in_test = 1;
return 0;
}
int do_set_reg(char *buf)
{
u8 *reg;
char *bufptr;
if (buf[0] == 'a') {
reg = &context->reg_a;
} else if (buf[0] == 'x') {
reg = &context->reg_x;
} else if (buf[0] == 'y') {
reg = &context->reg_y;
} else if (buf[0] == 's') {
reg = &context->reg_s;
} else {
printf("unknown reg.\n");
return 1;
}
bufptr = buf + 1;
bufptr = strip_blanks(bufptr);
if (*bufptr != '=') {
printf("expected '='.\n");
return 1;
}
bufptr++;
bufptr = strip_blanks(bufptr);
*reg = strtoul(bufptr, &bufptr, 0);
return 0;
}
int do_set_mem(char *buf)
{
char *bufptr;
unsigned long addr;
unsigned short data;
addr = strtoul(buf, &bufptr, 0);
bufptr = strip_blanks(bufptr);
if (*bufptr != '=') {
printf("expected '='.\n");
return 1;
}
bufptr++;
bufptr = strip_blanks(bufptr);
data = strtoul(bufptr, &bufptr, 0);
SET_MEMORY(addr, data);
return 0;
}
int do_set_flags(char *buf)
{
u8 flags;
flags = strtoul(buf, NULL, 0);
SET_FLAGS(context, flags);
return 0;
}
int do_set_cycles(char *buf)
{
if (!isdigit(*buf)) {
printf("expected number.\n");
return 1;
}
#ifndef NO_CYCLE_CHECKS
context->cycles_left = strtol(buf, NULL, 0);
#endif
return 0;
}
int do_set_pc(char *buf)
{
context->pc = strtoul(buf, NULL, 0);
return 0;
}
struct parse_table set_line_types[] = {
{"reg", do_set_reg},
{"register", do_set_reg},
{"mem", do_set_mem},
{"memory", do_set_mem},
{"pc", do_set_pc},
{"cycles", do_set_cycles},
{"flags", do_set_flags},
{NULL, NULL}, /* must be last entry */
};
int do_set(char *buf)
{
struct parse_table *cur_match;
char *bufptr;
int match_len;
if (!in_test) {
printf("set found while not in test.\n");
return 1;
}
bufptr = buf;
for (cur_match = set_line_types; cur_match->match; cur_match++) {
match_len = strlen(cur_match->match);
if (!strncmp(bufptr, cur_match->match, match_len)) {
if (bufptr[match_len] && (!isblank(bufptr[match_len]))) {
continue;
}
bufptr += match_len;
bufptr = strip_blanks(bufptr);
return cur_match->handler(bufptr);
}
}
return 1;
}
int do_run(char *buf)
{
if (!in_test) {
printf("run found while not in test.\n");
return 1;
}
cpu6502_step(context);
return 0;
}
void check_reg(u8 reg, u8 data)
{
if (reg != data) {
printf("check reg: expected 0x%02x, found 0x%02x.\n", data, reg);
did_test_fail = 1;
}
}
int do_check_reg(char *buf)
{
u8 *reg;
u8 data;
char *bufptr;
if (buf[0] == 'a') {
reg = &context->reg_a;
} else if (buf[0] == 'x') {
reg = &context->reg_x;
} else if (buf[0] == 'y') {
reg = &context->reg_y;
} else if (buf[0] == 's') {
reg = &context->reg_s;
} else {
printf("unknown reg.\n");
return 1;
}
bufptr = buf + 1;
bufptr = strip_blanks(bufptr);
if (*bufptr != '=') {
printf("expected '='.\n");
return 1;
}
bufptr++;
bufptr = strip_blanks(bufptr);
data = strtoul(bufptr, &bufptr, 0);
check_reg(*reg, data);
return 0;
}
void check_mem(u16 addr, u8 data)
{
u8 data2;
data2 = test_read8(NULL, addr);
if (data != data2) {
printf("check mem: expected 0x%02x, found 0x%02x.\n", data, data2);
did_test_fail = 1;
}
}
int do_check_mem(char *buf)
{
char *bufptr;
unsigned long addr;
u8 data;
addr = strtoul(buf, &bufptr, 0);
bufptr = strip_blanks(bufptr);
if (*bufptr != '=') {
printf("expected '='.\n");
return 1;
}
bufptr++;
bufptr = strip_blanks(bufptr);
data = strtoul(bufptr, &bufptr, 0);
check_mem(addr, data);
return 0;
}
void check_flags(u8 data)
{
u8 flags;
flags = GET_FLAGS(context);
if (data != flags) {
printf("check flags: expected 0x%02x, found 0x%02x.\n", data, flags);
did_test_fail = 1;
}
}
int do_check_flags(char *buf)
{
u8 data;
if (!isdigit(*buf)) {
printf("expected number.\n");
return 1;
}
data = strtoul(buf, NULL, 0);
check_flags(data);
return 0;
}
int do_check_cycles(char *buf)
{
int data;
data = strtol(buf, NULL, 0);
#ifndef NO_CYCLE_CHECKS
if (data != context->cycles_left) {
printf("check cycles: expected 0x%04x, found 0x%04x.\n", data, context->cycles_left);
did_test_fail = 1;
}
#endif
return 0;
}
int do_check_pc(char *buf)
{
u16 data;
data = strtoul(buf, NULL, 0);
if (data != context->pc) {
printf("check pc: expected 0x%04hx, found 0x%04hx.\n", data, context->pc);
did_test_fail = 1;
}
return 0;
}
struct parse_table check_line_types[] = {
{"reg", do_check_reg},
{"register", do_check_reg},
{"mem", do_check_mem},
{"memory", do_check_mem},
{"pc", do_check_pc},
{"cycles", do_check_cycles},
{"flags", do_check_flags},
{NULL, NULL}, /* must be last entry */
};
int do_check(char *buf)
{
struct parse_table *cur_match;
char *bufptr;
int match_len;
if (!in_test) {
printf("check found while not in test.\n");
return 1;
}
bufptr = buf;
for (cur_match = check_line_types; cur_match->match; cur_match++) {
match_len = strlen(cur_match->match);
if (!strncmp(bufptr, cur_match->match, match_len)) {
if (bufptr[match_len] && (!isblank(bufptr[match_len]))) {
continue;
}
bufptr += match_len;
bufptr = strip_blanks(bufptr);
return cur_match->handler(bufptr);
}
}
return 1;
}
int do_done(char *buf)
{
if (!in_test) {
printf("done found while not in test.\n");
return 1;
}
if (did_test_fail) {
printf("test \"%s\" failed.\n", cur_test);
num_failed++;
} else {
num_passed++;
}
in_test = 0;
return 0;
}
struct parse_table line_types[] = {
{"start", do_start},
{"set", do_set},
{"run", do_run},
{"check", do_check},
{"done", do_done}, /* Do Done is a song by Kudo Shizuka */
{NULL, NULL}, /* must be last entry */
};
int parse_test_file_line(char *buf)
{
struct parse_table *cur_match;
char *bufptr;
int match_len;
bufptr = buf;
bufptr = strip_blanks(bufptr);
if (!*bufptr) {
/* blank line, no action */
return 0;
}
if (*bufptr == '#') {
/* comment, no action */
return 0;
}
for (cur_match = line_types; cur_match->match; cur_match++) {
match_len = strlen(cur_match->match);
if (!strncmp(bufptr, cur_match->match, match_len)) {
if (bufptr[match_len] && (!isblank(bufptr[match_len]))) {
continue;
}
bufptr += match_len;
bufptr = strip_blanks(bufptr);
return cur_match->handler(bufptr);
}
}
return 1;
}
void parse_test_file(FILE *testfile)
{
char buf[256];
in_test = 0;
while (!feof(testfile)) {
cur_line++;
if (!fgets(buf, 256, testfile)) {
return;
}
if (buf[strlen(buf) - 1] == '\n') {
buf[strlen(buf) - 1] = '\0';
}
if (parse_test_file_line(buf)) {
printf("Syntax error, line %d.\n", cur_line);
did_test_fail = 1;
}
}
}
void create_cpu(void)
{
test_cpu = calloc(sizeof(struct cal_cpu), 1);
cal_cpu6502_init(&test_cpu);
context = test_cpu->data.d_cpu6502;
test_cpu->setmmu8(test_cpu, 0, 0, read8table, write8table);
test_cpu->setzpage(test_cpu, page_0);
}
#include "test_6502_1.h"
int main(int argc, char *argv[])
{
num_passed = 0;
num_failed = 0;
create_cpu();
printf("Running tests...\n");
run_internal_tests();
parse_test_file(stdin);
if (in_test) {
printf("Still in test at EOF.\n");
printf("Syntax error, line %d.\n", cur_line + 1);
num_failed++;
}
printf("Summary: %d passed, %d failed, %d total.\n", num_passed, num_failed, num_passed + num_failed);
return 0;
}
/*
* $Log: test_6502.c,v $
* Revision 1.6 2000/09/23 02:29:29 nyef
* dug check_{reg,mem,flags} out from do_check_{reg,mem,flags}
*
* Revision 1.5 2000/09/19 02:00:31 nyef
* added an internal test system similar to test_68k.c
*
* Revision 1.4 2000/09/07 02:55:40 nyef
* fixed bug added in last revision
*
* Revision 1.3 2000/08/25 02:48:55 nyef
* started adding support for testing the Marat6502 core
*
* Revision 1.2 2000/07/15 22:19:01 nyef
* converted to use lazy flag evaluation
*
* Revision 1.1 2000/05/01 00:35:51 nyef
* Initial revision
*
*/