forked from iPodLinux-Community/iDarcNES
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_68k.c
746 lines (569 loc) · 13.3 KB
/
test_68k.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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
/*
* test_68k.c
*
* test the "junk68k" core.
*/
/* $Id: test_68k.c,v 1.11 2000/04/23 01:49:09 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 "emu68k.h"
#include "tool.h"
#define MAX_MEMLOCS 32
struct emu68k_context context_true;
struct emu68k_context *context = &context_true;
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;
u16 data;
};
struct memloc memlocs[MAX_MEMLOCS];
int memlocs_used;
#define SET_MEMORY(addr, data16) do { memlocs[memlocs_used].data = data16; memlocs[memlocs_used].address = addr; memlocs_used++; } while (0)
u8 test_read8(cal_cpu cpu, u32 address)
{
int i;
u32 addr;
addr = address & 0xffffff;
for (i = 0; i < memlocs_used; i++) {
if (memlocs[i].address == (addr & ~1)) {
if (addr & 1) {
return memlocs[i].data & 0xff;
} else {
return memlocs[i].data >> 8;
}
}
}
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;
for (i = 0; i < memlocs_used; i++) {
if (memlocs[i].address == (addr & ~1)) {
if (addr & 1) {
memlocs[i].data &= 0xff00;
memlocs[i].data |= data;
} else {
memlocs[i].data &= 0xff;
memlocs[i].data |= data << 8;
}
return;
}
}
printf("write8 access 0x%02x to unknown location 0x%06lx.\n", data, addr);
did_test_fail = 1;
}
u16 test_read16(cal_cpu cpu, u32 address)
{
int i;
u32 addr;
addr = address & 0xffffff;
if (addr & 1) {
printf("BUS error.\n");
did_test_fail = 1;
return 0;
}
for (i = 0; i < memlocs_used; i++) {
if (memlocs[i].address == (addr)) {
return memlocs[i].data;
}
}
printf("read16 access to unknown location 0x%06lx.\n", addr);
did_test_fail = 1;
return 0;
}
void test_write16(cal_cpu cpu, u32 address, u16 data)
{
int i;
u32 addr;
addr = address & 0xffffff;
if (addr & 1) {
printf("BUS error.\n");
did_test_fail = 1;
return;
}
for (i = 0; i < memlocs_used; i++) {
if (memlocs[i].address == (addr)) {
memlocs[i].data = data;
return;
}
}
printf("write16 access 0x%04hx to unknown location 0x%06lx.\n", data, addr);
did_test_fail = 1;
}
memread8_t read8table[1] = {test_read8};
memwrite8_t write8table[1] = {test_write8};
memread16_t read16table[1] = {test_read16};
memwrite16_t write16table[1] = {test_write16};
void clear_cpu(void)
{
int i;
for (i = 0; i < 8; i++) {
context->regs_a[i] = 0;
context->regs_d[i] = 0;
}
context->pc = 0;
/* context->flags = 0; */
emu68k_set_flags(context, 0);
context->other_sp = 0;
context->cycles_left = 0;
context->read8table = read8table;
context->write8table = write8table;
context->read16table = read16table;
context->write16table = write16table;
context->memshift = 0;
context->memmask = 0;
context->cpu = NULL;
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)
{
unsigned long *regset;
int reg;
char *bufptr;
if (buf[0] == 'a') {
regset = context->regs_a;
} else if (buf[0] == 'd') {
regset = context->regs_d;
} else {
printf("unknown reg.\n");
return 1;
}
if ((buf[1] < '0') || (buf[1] > '7')) {
printf("unknown reg.\n");
return 1;
}
reg = buf[1] - '0';
bufptr = buf + 2;
bufptr = strip_blanks(bufptr);
if (*bufptr != '=') {
printf("expected '='.\n");
return 1;
}
bufptr++;
bufptr = strip_blanks(bufptr);
regset[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)
{
/* context->flags = strtoul(buf, NULL, 0); */
emu68k_set_flags(context, strtoul(buf, NULL, 0));
return 0;
}
int do_set_other_sp(char *buf)
{
context->other_sp = strtoul(buf, NULL, 0);
return 0;
}
int do_set_cycles(char *buf)
{
if (!isdigit(*buf)) {
printf("expected number.\n");
return 1;
}
context->cycles_left = strtol(buf, NULL, 0);
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},
{"other_sp", do_set_other_sp},
{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;
}
emu68k_step(context);
return 0;
}
void check_reg(u32 reg, u32 data)
{
if (reg != data) {
printf("check reg: expected 0x%08lx, found 0x%08lx.\n", data, reg);
did_test_fail = 1;
}
}
int do_check_reg(char *buf)
{
unsigned long *regset;
unsigned long data;
int reg;
char *bufptr;
if (buf[0] == 'a') {
regset = context->regs_a;
} else if (buf[0] == 'd') {
regset = context->regs_d;
} else {
printf("unknown reg.\n");
return 1;
}
if ((buf[1] < '0') || (buf[1] > '7')) {
printf("unknown reg.\n");
return 1;
}
reg = buf[1] - '0';
bufptr = buf + 2;
bufptr = strip_blanks(bufptr);
if (*bufptr != '=') {
printf("expected '='.\n");
return 1;
}
bufptr++;
bufptr = strip_blanks(bufptr);
data = strtoul(bufptr, &bufptr, 0);
check_reg(regset[reg], data);
return 0;
}
void check_memory(u32 addr, u16 data)
{
u16 real_data;
real_data = test_read16(NULL, addr);
if (data != real_data) {
printf("check mem: expected 0x%04hx, found 0x%04hx.\n", data, real_data);
did_test_fail = 1;
}
}
int do_check_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);
check_memory(addr, data);
return 0;
}
void check_flags(u16 flags)
{
u16 real_flags;
real_flags = emu68k_get_flags(context);
if (flags != real_flags) {
printf("check flags: expected 0x%04hx, found 0x%04hx.\n", flags, real_flags);
did_test_fail = 1;
}
}
int do_check_flags(char *buf)
{
unsigned short data;
if (!isdigit(*buf)) {
printf("expected number.\n");
return 1;
}
data = strtoul(buf, NULL, 0);
check_flags(data);
return 0;
}
int do_check_other_sp(char *buf)
{
unsigned long data;
if (!isdigit(*buf)) {
printf("expected number.\n");
return 1;
}
data = strtoul(buf, NULL, 0);
if (data != context->other_sp) {
printf("check other_sp: expected 0x%08lx, found 0x%08lx.\n", data, context->other_sp);
did_test_fail = 1;
}
return 0;
}
void check_cycles(int cycles)
{
if (cycles != context->cycles_left) {
printf("check cycles: expected 0x%04hx, found 0x%04hx.\n", cycles, context->cycles_left);
did_test_fail = 1;
}
}
int do_check_cycles(char *buf)
{
int data;
data = strtol(buf, NULL, 0);
check_cycles(data);
return 0;
}
void check_pc(u32 pc)
{
if (pc != (context->pc & 0xffffff)) {
printf("check pc: expected 0x%06lx, found 0x%06lx.\n", pc, context->pc & 0x00ffffff);
did_test_fail = 1;
}
}
int do_check_pc(char *buf)
{
unsigned long data;
data = strtoul(buf, NULL, 0);
check_pc(data);
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},
{"other_sp", do_check_other_sp},
{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;
}
}
}
#include "test_68k_1.h"
int main(int argc, char *argv[])
{
num_passed = 0;
num_failed = 0;
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_68k.c,v $
* Revision 1.11 2000/04/23 01:49:09 nyef
* added more support for compiled tests
*
* Revision 1.10 2000/04/22 22:53:21 nyef
* added support for compiled tests in test_68k_1.h
*
* Revision 1.9 2000/03/18 18:12:19 nyef
* changed to use new emu68k flags interface
*
* Revision 1.8 1999/12/18 17:07:18 nyef
* added support for setting/checking other_sp
*
* Revision 1.7 1999/12/17 01:44:47 nyef
* changed deb_printf() to print the debug string out and fail the test
*
* Revision 1.6 1999/12/17 01:27:28 nyef
* fixed bug in output from do_check_mem()
*
* Revision 1.5 1999/12/13 01:53:56 nyef
* fixed address truncation
* fixed diagnostic message in test_write16()
*
* Revision 1.4 1999/12/13 01:44:49 nyef
* fixed memory read functions to truncate addresses to 24 bits
*
* Revision 1.3 1999/12/13 01:26:29 nyef
* added some diagnostic output to the cycle set and check functions
*
* Revision 1.2 1999/12/12 20:32:02 nyef
* fixed bug with diagnostic message for cycle check failure
*
* Revision 1.1 1999/12/12 18:19:37 nyef
* Initial revision
*
*/