-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.c
762 lines (721 loc) · 18.4 KB
/
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
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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "chip8.h"
#include "stack.h"
#include "tests.h"
#include "list.h"
#define SUCCESS 0
#define FAILURE 1
int (*test_table[16])(chip8_t *chip8) =
{
test_0, test_1, test_2, test_3, test_4, test_5, test_6,
test_7, test_8, test_9, test_A, test_B, test_C, test_D,
test_E, test_F
};
int (*chip8_test_arithmetic[16])(chip8_t *chip8) =
{
test_arithmetic_0, test_arithmetic_1, test_arithmetic_2, test_arithmetic_3, test_arithmetic_4,
test_arithmetic_5, test_arithmetic_6, test_arithmetic_7, illegal_func_int, illegal_func_int,
illegal_func_int, illegal_func_int, illegal_func_int, illegal_func_int, test_arithmetic_E,
illegal_func_int
};
int test_arithmetic_0(chip8_t *chip8) {
/*Tests the following opcodes:
> 0x8XY0
Set VX = VY
*/
unsigned char x = (chip8->opcode & 0x0F00) >> 8;
unsigned char y = (chip8->opcode & 0x00F0) >> 4;
chip8->V[x] = 0;
chip8->V[y] = 10;
opcode_8(chip8);
if (chip8->V[x] == 10) {
return SUCCESS;
}
else {
return FAILURE;
}
}
int test_arithmetic_1(chip8_t *chip8) {
/*Tests the following opcodes:
> 0x8XY1
Set VX = VX (OR) VY
*/
unsigned char x = (chip8->opcode & 0x0F00) >> 8;
unsigned char y = (chip8->opcode & 0x00F0) >> 4;
chip8->V[x] = 0x5F;
chip8->V[y] = 0xAF;
arithmetic_1(chip8);
if (chip8->V[x] == 0x5F | 0xAF) {
return SUCCESS;
}
else {
return FAILURE;
}
}
int test_arithmetic_2(chip8_t *chip8) {
/*Tests the following opcodes:
> 0x8XY2
Set VX = VX (AND) VY
*/
unsigned char x = (chip8->opcode & 0x0F00) >> 8;
unsigned char y = (chip8->opcode & 0x00F0) >> 4;
chip8->V[x] = 0x5F;
chip8->V[y] = 0xAF;
arithmetic_2(chip8);
if (chip8->V[x] == 0x5F & 0xAF) {
return SUCCESS;
}
else {
return FAILURE;
}
}
int test_arithmetic_3(chip8_t *chip8) {
/*Tests the following opcodes:
> 0x8XY3
Sets VX to VX (XOR) VY
*/
unsigned char x = (chip8->opcode & 0x0F00) >> 8;
unsigned char y = (chip8->opcode & 0x00F0) >> 4;
chip8->V[x] = 0x5F;
chip8->V[y] = 0xAF;
arithmetic_3(chip8);
if (chip8->V[x] == 0x5F ^ 0xAF) {
return SUCCESS;
}
else {
return FAILURE;
}
}
int test_arithmetic_4(chip8_t *chip8) {
/*Tests the following opcodes:
> 0x8XY4
Sets VX to VX + VY, VF = 1 if overflow
*/
unsigned char x = (chip8->opcode & 0x0F00) >> 8;
unsigned char y = (chip8->opcode & 0x00F0) >> 4;
chip8->V[x] = 10;
chip8->V[y] = 100;
chip8->V[0xF] = 0;
arithmetic_4(chip8);
if (!((chip8->V[x] == 10 + 100) && (chip8->V[0xF] == 0))) {
return FAILURE;
}
chip8->V[x] = 254;
chip8->V[y] = 250;
chip8->V[0xF] = 0;
if ((chip8->V[x] == (254 + 250)) && (chip8->V[0xF] == 1)) {
return SUCCESS;
}
else {
return FAILURE;
}
}
int test_arithmetic_5(chip8_t *chip8) {
/*Tests the following opcodes:
> 0x8XY5
Set VX = VX - VY, VF = VX > VY
*/
unsigned char x = (chip8->opcode & 0x0F00) >> 8;
unsigned char y = (chip8->opcode & 0x00F0) >> 4;
chip8->V[x] = 10;
chip8->V[y] = 11;
chip8->V[0xF] = 1;
arithmetic_5(chip8);
if (!((chip8->V[x] == (unsigned char)(10 - 11)) && (chip8->V[0xF] == 0))) {
return FAILURE;
}
chip8->V[x] = 11;
chip8->V[y] = 10;
chip8->V[0xF] = 0;
arithmetic_5(chip8);
if ((chip8->V[x] == 11 - 10) && (chip8->V[0xF] == 1)) {
return SUCCESS;
}
else {
return FAILURE;
}
}
int test_arithmetic_6(chip8_t *chip8) {
/*Tests the following opcodes:
> 0x8XY6
Set VF = LSB(VX), then shift VX right once
*/
unsigned char x = (chip8->opcode & 0x0F00) >> 8;
unsigned char y = (chip8->opcode & 0x00F0) >> 4;
chip8->V[x] = 10;
chip8->V[0xF] = 1;
arithmetic_6(chip8);
if (!((chip8->V[x] == 10 >> 1) && (chip8->V[0xF] == 0))) {
return FAILURE;
}
chip8->V[x] = 11;
chip8->V[0xF] = 0;
arithmetic_6(chip8);
if ((chip8->V[x] == 11 >> 1) && (chip8->V[0xF] == 1)) {
return SUCCESS;
}
else {
return FAILURE;
}
}
int test_arithmetic_7(chip8_t *chip8) {
/*Tests the following opcodes:
> 0x8XY7
Set VX = VY - VX, VF = VY > VX
*/
unsigned char x = (chip8->opcode & 0x0F00) >> 8;
unsigned char y = (chip8->opcode & 0x00F0) >> 4;
chip8->V[x] = 10;
chip8->V[y] = 11;
chip8->V[0xF] = 1;
arithmetic_5(chip8);
if (!((chip8->V[x] == 11 - 10) && (chip8->V[0xF] == 1))) {
return FAILURE;
}
chip8->V[x] = 11;
chip8->V[y] = 10;
chip8->V[0xF] = 0;
arithmetic_5(chip8);
if ((chip8->V[x] == (unsigned char)(10 - 11)) && (chip8->V[0xF] == 0)) {
return SUCCESS;
}
else {
return FAILURE;
}
}
int test_arithmetic_E(chip8_t *chip8) {
/*Tests the following opcodes:
> 0x8XYE
Sets VF = MSB(VX), then shifts VX left by 1
*/
unsigned char x = (chip8->opcode & 0x0F00) >> 8;
unsigned char y = (chip8->opcode & 0x00F0) >> 4;
chip8->V[x] = 0x0F;
chip8->V[0xF] = 1;
arithmetic_E(chip8);
if (!((chip8->V[x] == 0x0F << 1) && (chip8->V[0xF] == 0))) {
return FAILURE;
}
chip8->V[x] = 0xF0;
chip8->V[0xF] = 0;
arithmetic_E(chip8);
if ((chip8->V[x] == (unsigned char)(0xF0 << 1)) && (chip8->V[0xF] == 1)) {
return SUCCESS;
}
else {
return FAILURE;
}
}
int illegal_func_int(chip8_t *chip8) {
/*Tests the following opcodes:
> 0x8XY[8, 9, A, B, C, D, F]
*/
return SUCCESS;
}
int test_0(chip8_t *chip8) {
/*Tests the following opcodes:
> 0x00E0
Clear the display
> 0x00EE
Return from subroutine
> 0x0NNN
Jump to machine code routine at NNN. Ignored by modern interpreters
*/
unsigned short stack_num = 100;
if (chip8->opcode == 0x00E0) {
//load some crap into the corners of the display
memset((void*)chip8->gfx, 1, sizeof(unsigned char) * GFX_WIDTH *
GFX_HEIGHT);
}
else if (chip8->opcode == 0x00EE) {
//the chip8 should
stack_push(&chip8->stack, stack_num);
}
else {
printf("Opcode 0x0NNN not supported.\n");
}
//run opcode
opcode_0(chip8);
//test
if (chip8->opcode == 0x00E0) {
//make sure the gfx have been set to 0
int i;
for (i = 0; i < GFX_WIDTH * GFX_HEIGHT; i++) {
if (chip8->gfx[i]) {
// fail
return FAILURE;
}
}
}
else if (chip8->opcode == 0x00EE) {
//make sure we did pop something off the stack
if (chip8->pc != stack_num + 2) {
// fail
return FAILURE;
}
}
// success!
return SUCCESS;
}
int test_1(chip8_t *chip8) {
/*Tests the following opcodes:
> 0x1NNN
Sets the program counter to NNN.
*/
unsigned short nnn = (chip8->opcode & 0x0FFF);
chip8->pc = nnn + 0x1000;
opcode_1(chip8);
if (chip8->pc == nnn) {
return SUCCESS;
}
else {
return FAILURE;
}
}
int test_2(chip8_t *chip8) {
/*Tests the following opcodes:
> 0x2NNN
Call subroutine at NNN.
*/
unsigned short nnn = (chip8->opcode & 0x0FFF);
chip8->pc = nnn + 0x1000;
opcode_2(chip8);
if ((chip8->pc == nnn) && (chip8->stack.data[0] == nnn + 0x1000)) {
return SUCCESS;
}
else {
return FAILURE;
}
}
int test_3(chip8_t *chip8) {
/*Tests the following opcodes:
> 0x3XKK
Skip the next instruction if VX = KK.
*/
unsigned char kk = (chip8->opcode & 0x00FF);
unsigned short x = (chip8->opcode & 0x0F00) >> 8;
unsigned short old_pc = chip8->pc;
chip8->V[x] = kk;
opcode_3(chip8);
if (chip8->pc != old_pc + 4) {
return FAILURE;
}
chip8->V[x] = kk + 1;
old_pc = chip8->pc;
opcode_3(chip8);
if (chip8->pc != old_pc + 2) {
return FAILURE;
}
return SUCCESS;
}
int test_4(chip8_t *chip8) {
/*Tests the following opcodes:
> 0x4XKK
Skips the next instruction if VX != KK
*/
unsigned char kk = (chip8->opcode & 0x00FF);
unsigned short x = (chip8->opcode & 0x0F00) >> 8;
unsigned short old_pc = chip8->pc;
chip8->V[x] = kk;
opcode_4(chip8);
if (chip8->pc != old_pc + 2) {
return FAILURE;
}
chip8->V[x] = kk + 1;
old_pc = chip8->pc;
opcode_4(chip8);
if (chip8->pc != old_pc + 4) {
return FAILURE;
}
return SUCCESS;
}
int test_5(chip8_t *chip8) {
/*Tests the following opcodes:
> 0x5XY0
Skips the next instruction if VX = VY
*/
unsigned short x = (chip8->opcode & 0x0F00) >> 8;
unsigned short y = (chip8->opcode & 0x00F0) >> 4;
unsigned short old_pc = chip8->pc;
chip8->V[x] = 1;
chip8->V[y] = 2;
opcode_5(chip8);
if (chip8->pc != old_pc + 2) {
return FAILURE;
}
old_pc = chip8->pc;
chip8->V[x] = 1;
chip8->V[y] = 1;
opcode_5(chip8);
if (chip8->pc != old_pc + 4) {
return FAILURE;
}
return SUCCESS;
}
int test_6(chip8_t *chip8) {
/*Tests the following opcodes:
> 0x6XKK
Sets VX to KK
*/
unsigned char kk = (chip8->opcode & 0x00FF);
unsigned char x = (chip8->opcode & 0x0F00) >> 8;
chip8->V[x] = kk + 1;
opcode_6(chip8);
if (chip8->V[x] == kk) {
return SUCCESS;
}
else {
return FAILURE;
}
}
int test_7(chip8_t *chip8) {
/*Tests the following opcodes:
> 0x7XKK
Set VX = VX + KK
*/
unsigned char kk = (chip8->opcode & 0x00FF);
unsigned char x = (chip8->opcode & 0x0F00) >> 8;
unsigned char old_vx = chip8->V[x];
opcode_7(chip8);
if (chip8->V[x] = old_vx + kk) {
return SUCCESS;
}
else {
return FAILURE;
}
}
int test_8(chip8_t *chip8) {
/*Tests the following opcodes:
> 0x8XY?
*/
return chip8_test_arithmetic[(chip8->opcode & 0x000F)](chip8);
}
int test_9(chip8_t *chip8) {
/*Tests the following opcodes:
> 0x9XY0
Skip next instruction if VX != VY
*/
unsigned short x = (chip8->opcode & 0x0F00) >> 8;
unsigned short y = (chip8->opcode & 0x00F0) >> 4;
chip8->V[x] = 1;
chip8->V[y] = 2;
chip8->pc = 10;
opcode_9(chip8);
if (chip8->pc != 14) {
return FAILURE;
}
chip8->V[x] = 1;
chip8->V[y] = 1;
chip8->pc = 10;
opcode_9(chip8);
if (chip8->pc != 12) {
return FAILURE;
}
return SUCCESS;
}
int test_A(chip8_t *chip8) {
/* Tests the following opcodes:
> 0xANNN
Sets I to NNN
*/
unsigned short nnn = (chip8->opcode & 0x0FFF);
chip8->I = nnn + 1;
opcode_A(chip8);
if (chip8->I != nnn) {
return FAILURE;
}
return SUCCESS;
}
int test_B(chip8_t *chip8) {
/*Tests the following opcodes:
> 0xBNNN
Jumps to location NNN + V0
*/
unsigned short nnn = (chip8->opcode & 0x0FFF);
chip8->V[0] = 1;
chip8->pc = 0;
opcode_B(chip8);
if (chip8->pc != nnn + 1) {
return FAILURE;
}
return SUCCESS;
}
int test_C(chip8_t *chip8) {
/*Tests the following opcodes:
> 0xCXKK
Set VX = random byte AND kk
*/
unsigned char x = (chip8->opcode & 0x0F00) >> 8;
unsigned char kk = (chip8->opcode & 0x00FF);
chip8->V[x] = 0;
printf("X: %02X\nKK: %02X\n", x, kk);
opcode_C(chip8);
printf("Trial 1:\n\tVX: %02X\n", chip8->V[x]);
opcode_C(chip8);
printf("Trial 2:\n\tVX: %02X\n", chip8->V[x]);
opcode_C(chip8);
printf("Trial 3:\n\tVX: %02X\n", chip8->V[x]);
return SUCCESS;
}
int test_D(chip8_t *chip8) {
/*Tests the following opcodes:
> 0xDXYN
Draws an N-byte sprite starting at memory location I at (VX, VY)
*/
unsigned char x = (chip8->opcode & 0x0F00) >> 8;
unsigned char y = (chip8->opcode & 0x00F0) >> 4;
unsigned char n = (chip8->opcode & 0x000F);
chip8->V[x] = 0;
chip8->V[y] = 0;
chip8->I = 0;
chip8->memory[0] = 1;
chip8->memory[1] = 1;
chip8->memory[2] = 0;
chip8->memory[3] = 0;
chip8->gfx[0] = 0;
chip8->gfx[1] = 1;
chip8->gfx[2] = 0;
chip8->gfx[3] = 1;
opcode_D(chip8);
if (chip8->gfx[0] != 1 ^ 0) {
return FAILURE;
}
if (chip8->gfx[1] != 1 ^ 1) {
return FAILURE;
}
if (chip8->gfx[2] != 0 ^ 0) {
return FAILURE;
}
if (chip8->gfx[3] != 0 ^ 1) {
return FAILURE;
}
if (chip8->V[0xF] = 0) {
return FAILURE;
}
return SUCCESS;
}
int test_E(chip8_t *chip8) {
/*Tests the following opcodes:
> 0xEX9E
Skip next instruction if key[VX] is pressed
> 0xEXA1
Skip next instruction if key[VX] is not pressed
*/
unsigned char x = (chip8->opcode & 0x0F00) >> 8;
if (chip8->opcode & 0x00FF == 0x9E) {
chip8->V[x] = 0;
chip8->key[0] = 1;
chip8->pc = 10;
opcode_E(chip8);
if (chip8->pc != 14) {
return FAILURE;
}
chip8->V[x] = 0;
chip8->key[0] = 0;
chip8->pc = 10;
opcode_E(chip8);
if (chip8->pc != 12) {
return FAILURE;
}
}
else if (chip8->opcode & 0x00FF == 0xA1) {
chip8->V[x] = 0;
chip8->key[0] = 0;
chip8->pc = 10;
opcode_E(chip8);
if (chip8->pc != 14) {
return FAILURE;
}
chip8->V[x] = 0;
chip8->key[0] = 1;
chip8->pc = 10;
opcode_E(chip8);
if (chip8->pc != 12) {
return FAILURE;
}
}
else {
printf("Something weird happened in test_E.\n");
return FAILURE;
}
return SUCCESS;
}
int test_F(chip8_t *chip8) {
/*Tests the following opcodes:
> 0xFX07
Set VX = delay timer value
> 0xFX0A
Wait for a key press, store the value of the key in VX
> 0xFX15
Set delay timer = VX
> 0xFX18
Set sound timer = VX
> 0xFX1E
Set I += VX
> 0xFX29
Set I = location of sprite for digit VX
> 0xFX33
Store BCD repr of VX in memory locations I, I+1, I+2
> 0xFX55
Store registers V0 through VX in memory starting at location I
*/
unsigned char x = chip8->opcode & 0x0F00 >> 8;
if (chip8->opcode & 0x00FF == 0x07) {
chip8->delay_timer = 100;
chip8->V[x] = 0;
opcode_F(chip8);
if (chip8->V[x] != 100) {
return FAILURE;
}
}
else if (chip8->opcode & 0x00FF == 0x0A) {
chip8->V[x] = 0;
chip8->pc = 0;
opcode_F(chip8);
if (chip8->pc != 0 || chip8->V[x] != 0) {
return FAILURE;
}
chip8->V[x] = 0;
chip8->pc = 0;
chip8->key[0x4] = 1;
opcode_F(chip8);
if (chip8->pc == 0 || chip8->V[x] != 0x4) {
return FAILURE;
}
}
else if (chip8->opcode & 0x00FF == 0x15) {
chip8->delay_timer = 100;
chip8->V[x] = 10;
opcode_F(chip8);
if (chip8->delay_timer != 10) {
return FAILURE;
}
}
else if (chip8->opcode & 0x00FF == 0x18) {
chip8->sound_timer = 100;
chip8->V[x] = 10;
opcode_F(chip8);
if (chip8->sound_timer != 10) {
return FAILURE;
}
}
else if (chip8->opcode & 0x00FF == 0x1E) {
chip8->I = 10;
chip8->V[x] = 10;
opcode_F(chip8);
if (chip8->I != 20) {
return FAILURE;
}
}
else if (chip8->opcode & 0x00FF == 0x29) {
int i;
for (i = 0; i <= 0xF; i++) {
opcode_F(chip8);
if (chip8->I != i*FONT_HEIGHT) {
return FAILURE;
}
}
}
else if (chip8->opcode & 0x00FF == 0x33) {
chip8->I = 0;
int hundreds = chip8->V[x] / 100;
int tens = (chip8->V[x] % 100) / 10;
int ones = chip8->V[x] % 100 % 10;
opcode_F(chip8);
if (chip8->memory[0] != hundreds ||
chip8->memory[1] != tens ||
chip8->memory[2] != ones) {
return FAILURE;
}
}
else if (chip8->opcode & 0x00FF == 0x55) {
int i;
chip8->I = 10;
for (i = 0; i < x; i++) {
chip8->V[x] = i;
}
opcode_F(chip8);
for (i = 0; i < x; i++) {
if (chip8->memory[10 + i] != i) {
return FAILURE;
}
}
}
else if (chip8->opcode & 0x00FF == 0x65) {
int i;
chip8->I = 10;
for (i = 0; i < x; i++) {
chip8->memory[10 + i] = i;
}
opcode_F(chip8);
for (i = 0; i < x; i++) {
if (chip8->V[x] != i) {
return FAILURE;
}
}
}
else {
printf("Something weird happened in test_F.\n");
return FAILURE;
}
return SUCCESS;
}
int run_test(unsigned short test_opcode) {
chip8_t chip8;
init_chip8(&chip8);
chip8.opcode = test_opcode;
if (test_table[(chip8.opcode >> 12)](&chip8)) {
return 1;
}
else {
return 0;
}
}
int main(int argc, char** argv) {
FILE *fp;
int size;
int i;
unsigned short opcode;
list_t succeeded;
list_t failed;
init_list(&succeeded, sizeof(unsigned short));
init_list(&failed, sizeof(unsigned short));
fp = fopen(argv[1], "r");
if (fp == NULL) {
fprintf(stderr, "File error: could not open %s.\n", argv[1]);
return FILE_OPEN_ERROR;
}
fseek(fp, 0L, SEEK_END);
size = ftell(fp);
rewind(fp);
if (size > MEMORY_SZ - PROG_MEM_START) {
fputs("File error: ROM file too large.", stderr);
return FILE_SIZE_ERROR;
}
unsigned char *buffer = (unsigned char*)malloc(sizeof(unsigned char) * size);
if (buffer == NULL) {
fputs("Memory error: could not allocate buffer for ROM.", stderr);
return BUFFER_SIZE_ERROR;
}
// read ROM into buffer
fread(buffer, sizeof(unsigned char), size, (FILE*)fp);
for (i = 0; i < size/2; i++) {
opcode = (buffer[2*i] << 8) | buffer[2*i + 1];
if (run_test(opcode)) {
list_append(&failed, &opcode);
}
else {
list_append(&succeeded, &opcode);
}
}
printf("Failed:\n");
for (i = 0; i < list_size(&failed); i++) {
printf("- %04X\n", *(unsigned short *)list_get(&failed, i));
}
destroy_list(&succeeded);
destroy_list(&failed);
free(buffer);
return 0;
}