-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchip8_test.go
827 lines (752 loc) · 19.8 KB
/
chip8_test.go
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
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
package chip8
import (
"testing"
)
func TestDecodeOps(t *testing.T) {
for _, testCase := range []struct {
op EncodedOp
expected Op // nil means decoding should panic
}{
{0x0000, nil},
{0x00E0, CLS{}},
{0x01E0, nil},
{0x00EE, RET{}},
{0x01EE, nil},
{0x1123, JP{0x123}},
{0x2123, CALL{0x123}},
{0x3123, SEVx{x: 0x1, kk: 0x23}},
{0x4123, SNEVx{x: 0x1, kk: 0x23}},
{0x5120, SEVxVy{x: 0x1, y: 0x2}},
{0x5121, nil},
{0x6123, LDVx{x: 0x1, kk: 0x23}},
{0x7123, ADDVx{x: 0x1, kk: 0x23}},
{0x8120, LDVxVy{x: 0x1, y: 0x2}},
{0x8121, ORVxVy{x: 0x1, y: 0x2}},
{0x8122, ANDVxVy{x: 0x1, y: 0x2}},
{0x8123, XORVxVy{x: 0x1, y: 0x2}},
{0x8124, ADDVxVy{x: 0x1, y: 0x2}},
{0x8125, SUBVxVy{x: 0x1, y: 0x2}},
{0x8126, SHRVx{x: 0x1}},
{0x8136, SHRVx{x: 0x1}},
{0x8127, SUBNVxVy{x: 0x1, y: 0x2}},
{0x8128, nil},
{0x812E, SHLVx{x: 0x1}},
{0x813E, SHLVx{x: 0x1}},
{0x812F, nil},
{0x9120, SNEVxVy{x: 0x1, y: 0x2}},
{0x9121, nil},
{0xA123, LDI{nnn: 0x123}},
{0xB123, JPV0{nnn: 0x123}},
{0xC123, RNDVx{x: 0x1, kk: 0x23}},
{0xD123, DRWVxVy{x: 0x1, y: 0x2, n: 0x3}},
{0xE19D, nil},
{0xE18E, nil},
{0xE19E, SKPVx{x: 0x1}},
{0xE1A0, nil},
{0xE1A1, SKNPVx{x: 0x1}},
{0xE1B1, nil},
{0xF106, nil},
{0xF107, LDVxDT{x: 0x1}},
{0xF108, nil},
{0xF10A, LDVxK{x: 0x1}},
{0xF114, nil},
{0xF115, LDDTVx{x: 0x1}},
{0xF116, nil},
{0xF118, LDSTVx{x: 0x1}},
{0xF119, nil},
{0xF11E, ADDIVx{x: 0x1}},
{0xF11F, nil},
{0xF128, nil},
{0xF129, LDFVx{x: 0x1}},
{0xF12A, nil},
{0xF132, nil},
{0xF133, LDBVx{x: 0x1}},
{0xF134, nil},
{0xF154, nil},
{0xF155, LDIVx{x: 0x1}},
{0xF156, nil},
{0xF164, nil},
{0xF165, LDVxI{x: 0x1}},
{0xF166, nil},
{0xFFFF, nil},
} {
if testCase.expected == nil {
// assert that decode() panics
func() {
defer func() { recover() }()
actual := testCase.op.decode()
t.Errorf(
"(%#x).decode(): Should panic, Actual %#v",
testCase.op, actual)
}()
} else {
func() {
defer func() {
if r := recover(); r != nil {
t.Errorf(
"(%#x).decode(): Paniced, Expected %s",
testCase.op, testCase.expected)
}
}()
var actual Op = testCase.op.decode()
if testCase.expected != actual {
t.Errorf(
"(%#x).decode(): Expected %#v, Actual %#v",
testCase.op, testCase.expected, actual)
}
}()
}
}
}
func TestBCD(t *testing.T) {
for _, testCase := range []struct {
n, hundreds, tens, ones uint8
}{
{0, 0, 0, 0},
{1, 0, 0, 1},
{10, 0, 1, 0},
{11, 0, 1, 1},
{100, 1, 0, 0},
{102, 1, 0, 2},
{123, 1, 2, 3},
{200, 2, 0, 0},
{202, 2, 0, 2},
{242, 2, 4, 2},
} {
hundreds, tens, ones := bcd(testCase.n)
if hundreds != testCase.hundreds || tens != testCase.tens || ones != testCase.ones {
t.Errorf(
"bcd(%d) Expected: %d %d %d Actual: %d %d %d",
testCase.n, testCase.hundreds, testCase.tens, testCase.ones,
hundreds, tens, ones)
}
}
}
type Constant struct {
c uint8
}
func (c Constant) Next() uint8 {
return c.c
}
func TestOps(t *testing.T) {
for _, testCase := range []struct {
before VM
op Op
after VM
msg string
}{
/*
00E0 - CLS
Clear the display.
*/
{
before: VM{VideoMemory: [32]uint64{0: 0x1, 31: 0x1}},
op: CLS{},
after: VM{},
},
/*
00EE - RET
Return from a subroutine.
The interpreter sets the program counter to the address at the top of the
stack, then subtracts 1 from the stack pointer.
*/
{
before: VM{PC: 0x300, SP: 1, Stack: [16]uint16{0: 0x200}},
op: RET{},
after: VM{PC: 0x200, SP: 0, Stack: [16]uint16{0: 0x200}},
},
/*
1nnn - JP addr
Jump to location nnn.
The interpreter sets the program counter to nnn.
*/
{
before: VM{PC: 0x300},
op: JP{nnn: 0x400},
after: VM{PC: 0x400},
},
/*
2nnn - CALL addr
Call subroutine at nnn.
The interpreter increments the stack pointer, then puts the current PC on
the top of the stack. The PC is then set to nnn.
*/
{
before: VM{PC: 0x300},
op: CALL{nnn: 0x400},
after: VM{PC: 0x400, SP: 1, Stack: [16]uint16{0: 0x300}},
},
/*
3xkk - SE Vx, byte
Skip next instruction if Vx = kk.
The interpreter compares register Vx to kk, and if they are equal,
increments the program counter by 2.
*/
{
msg: "should skip",
before: VM{PC: 0x200, V: [16]uint8{0xA: 0x12}},
op: SEVx{x: 0xA, kk: 0x12},
after: VM{PC: 0x202, V: [16]uint8{0xA: 0x12}},
},
{
msg: "should not skip",
before: VM{PC: 0x200, V: [16]uint8{0xA: 0x12}},
op: SEVx{x: 0xB, kk: 0x12},
after: VM{PC: 0x200, V: [16]uint8{0xA: 0x12}},
},
/*
4xkk - SNE Vx, byte
Skip next instruction if Vx != kk.
The interpreter compares register Vx to kk, and if they are not equal,
increments the program counter by 2.
*/
{
msg: "should skip",
before: VM{PC: 0x200, V: [16]uint8{0xA: 0x13}},
op: SNEVx{x: 0xA, kk: 0x12},
after: VM{PC: 0x202, V: [16]uint8{0xA: 0x13}},
},
{
msg: "should not skip",
before: VM{PC: 0x200, V: [16]uint8{0xB: 0x12}},
op: SNEVx{x: 0xB, kk: 0x12},
after: VM{PC: 0x200, V: [16]uint8{0xB: 0x12}},
},
/*
5xy0 - SE Vx, Vy
Skip next instruction if Vx = Vy.
The interpreter compares register Vx to register Vy, and if they are
equal, increments the program counter by 2.
*/
{
msg: "should skip",
before: VM{PC: 0x200, V: [16]uint8{0xA: 0x1, 0xB: 0x1}},
op: SEVxVy{x: 0xA, y: 0xB},
after: VM{PC: 0x202, V: [16]uint8{0xA: 0x1, 0xB: 0x1}},
},
{
msg: "should not skip",
before: VM{PC: 0x200, V: [16]uint8{0xC: 0x1, 0xD: 0x2}},
op: SEVxVy{x: 0xC, y: 0xD},
after: VM{PC: 0x200, V: [16]uint8{0xC: 0x1, 0xD: 0x2}},
},
/*
6xkk - LD Vx, byte
Set Vx = kk.
The interpreter puts the value kk into register Vx.
*/
{
before: VM{V: [16]uint8{0xA: 0x1}},
op: LDVx{x: 0xA, kk: 0xB},
after: VM{V: [16]uint8{0xA: 0xB}},
},
/*
7xkk - ADD Vx, byte
Set Vx = Vx + kk.
Adds the value kk to the value of register Vx, then stores the result in
Vx.
*/
{
before: VM{V: [16]uint8{0xA: 0x1}},
op: ADDVx{x: 0xA, kk: 0x1},
after: VM{V: [16]uint8{0xA: 0x2}},
},
/*
8xy0 - LD Vx, Vy
Set Vx = Vy.
Stores the value of register Vy in register Vx.
*/
{
before: VM{V: [16]uint8{0xB: 0x1}},
op: LDVxVy{x: 0xA, y: 0xB},
after: VM{V: [16]uint8{0xA: 0x1, 0xB: 0x1}},
},
/*
8xy1 - OR Vx, Vy
Set Vx = Vx OR Vy.
Performs a bitwise OR on the values of Vx and Vy, then stores the result
in Vx. A bitwise OR compares the corrseponding bits from two values, and
if either bit is 1, then the same bit in the result is also 1.
Otherwise, it is 0.
*/
{
before: VM{V: [16]uint8{0xA: 0x0, 0xB: 0x1}},
op: ORVxVy{x: 0xA, y: 0xB},
after: VM{V: [16]uint8{0xA: 0x1, 0xB: 0x1}},
},
/*
8xy2 - AND Vx, Vy
Set Vx = Vx AND Vy.
Performs a bitwise AND on the values of Vx and Vy, then stores the result
in Vx. A bitwise AND compares the corrseponding bits from two values, and
if both bits are 1, then the same bit in the result is also 1. Otherwise,
it is 0.
*/
{
before: VM{V: [16]uint8{0xA: 0x1, 0xB: 0x0}},
op: ANDVxVy{x: 0xA, y: 0xB},
after: VM{V: [16]uint8{0xA: 0x0, 0xB: 0x0}},
},
/*
8xy3 - XOR Vx, Vy
Set Vx = Vx XOR Vy.
Performs a bitwise exclusive OR on the values of Vx and Vy, then stores
the result in Vx. An exclusive OR compares the corrseponding bits from
two values, and if the bits are not both the same, then the corresponding
bit in the result is set to 1. Otherwise, it is 0.
*/
{
before: VM{V: [16]uint8{0xA: 0x1, 0xB: 0x1}},
op: XORVxVy{x: 0xA, y: 0xB},
after: VM{V: [16]uint8{0xA: 0x0, 0xB: 0x1}},
},
/*
8xy4 - ADD Vx, Vy
Set Vx = Vx + Vy, set VF = carry.
The values of Vx and Vy are added together. If the result is greater than
8 bits (i.e., > 255,) VF is set to 1, otherwise 0. Only the lowest 8 bits
of the result are kept, and stored in Vx.
*/
{
msg: "no carry",
before: VM{V: [16]uint8{0xA: 0x1, 0xB: 0x1, 0xF: 0}},
op: ADDVxVy{x: 0xA, y: 0xB},
after: VM{V: [16]uint8{0xA: 0x2, 0xB: 0x1, 0xF: 0}},
},
{
msg: "carry",
before: VM{V: [16]uint8{0xA: 0xFF, 0xB: 0x2, 0xF: 0}},
op: ADDVxVy{x: 0xA, y: 0xB},
after: VM{V: [16]uint8{0xA: 0x1, 0xB: 0x2, 0xF: 1}},
},
/*
8xy5 - SUB Vx, Vy
Set Vx = Vx - Vy, set VF = NOT borrow.
If Vx > Vy, then VF is set to 1, otherwise 0. Then Vy is subtracted from
Vx, and the results stored in Vx.
*/
{
msg: "Vx > Vy",
before: VM{V: [16]uint8{0xA: 0x2, 0xB: 0x1, 0xF: 0}},
op: SUBVxVy{x: 0xA, y: 0xB},
after: VM{V: [16]uint8{0xA: 0x1, 0xB: 0x1, 0xF: 1}},
},
{
msg: "Vx == Vy",
before: VM{V: [16]uint8{0xA: 0x2, 0xB: 0x2, 0xF: 1}},
op: SUBVxVy{x: 0xA, y: 0xB},
after: VM{V: [16]uint8{0xA: 0x0, 0xB: 0x2, 0xF: 0}},
},
{
msg: "Vx < Vy",
before: VM{V: [16]uint8{0xA: 0x1, 0xB: 0x2, 0xF: 1}},
op: SUBVxVy{x: 0xA, y: 0xB},
after: VM{V: [16]uint8{0xA: 0xFF, 0xB: 0x2, 0xF: 0}},
},
/*
8xy6 - SHR Vx {, Vy}
Set Vx = Vx SHR 1.
If the least-significant bit of Vx is 1, then VF is set to 1, otherwise
0. Then Vx is divided by 2.
*/
{
msg: "no overflow",
before: VM{V: [16]uint8{0xA: 0x4, 0xF: 1}},
op: SHRVx{x: 0xA},
after: VM{V: [16]uint8{0xA: 0x2, 0xF: 0}},
},
{
msg: "overflow",
before: VM{V: [16]uint8{0xA: 0x1, 0xF: 0}},
op: SHRVx{x: 0xA},
after: VM{V: [16]uint8{0xA: 0x0, 0xF: 1}},
},
/*
8xy7 - SUBN Vx, Vy
Set Vx = Vy - Vx, set VF = NOT borrow.
If Vy > Vx, then VF is set to 1, otherwise 0. Then Vx is subtracted from
Vy, and the results stored in Vx.
*/
{
msg: "Vy > Vx",
before: VM{V: [16]uint8{0xA: 0x1, 0xB: 0x3, 0xF: 0}},
op: SUBNVxVy{x: 0xA, y: 0xB},
after: VM{V: [16]uint8{0xA: 0x2, 0xB: 0x3, 0xF: 1}},
},
{
msg: "Vy == Vx",
before: VM{V: [16]uint8{0xA: 0x2, 0xB: 0x2, 0xF: 1}},
op: SUBNVxVy{x: 0xA, y: 0xB},
after: VM{V: [16]uint8{0xA: 0x0, 0xB: 0x2, 0xF: 0}},
},
{
msg: "Vy < Vx",
before: VM{V: [16]uint8{0xA: 0x2, 0xB: 0x1, 0xF: 1}},
op: SUBNVxVy{x: 0xA, y: 0xB},
after: VM{V: [16]uint8{0xA: 0xFF, 0xB: 0x1, 0xF: 0}},
},
/*
8xyE - SHL Vx {, Vy}
Set Vx = Vx SHL 1.
If the most-significant bit of Vx is 1, then VF is set to 1, otherwise to
0. Then Vx is multiplied by 2.
*/
{
msg: "no overflow",
before: VM{V: [16]uint8{0xA: 0x4, 0xF: 1}},
op: SHLVx{x: 0xA},
after: VM{V: [16]uint8{0xA: 0x8, 0xF: 0}},
},
{
msg: "overflow",
before: VM{V: [16]uint8{0xA: 0x80, 0xF: 0}},
op: SHLVx{x: 0xA},
after: VM{V: [16]uint8{0xA: 0x0, 0xF: 1}},
},
/*
9xy0 - SNE Vx, Vy
Skip next instruction if Vx != Vy.
The values of Vx and Vy are compared, and if they are not equal, the
program counter is increased by 2.
*/
{
msg: "Vx == Vy",
before: VM{PC: 0x200, V: [16]uint8{0xA: 0x1, 0xB: 0x1}},
op: SNEVxVy{x: 0xA, y: 0xB},
after: VM{PC: 0x200, V: [16]uint8{0xA: 0x1, 0xB: 0x1}},
},
{
msg: "Vx != Vy",
before: VM{PC: 0x200, V: [16]uint8{0xA: 0x1, 0xB: 0x0}},
op: SNEVxVy{x: 0xA, y: 0xB},
after: VM{PC: 0x202, V: [16]uint8{0xA: 0x1, 0xB: 0x0}},
},
/*
Annn - LD I, addr
Set I = nnn.
The value of register I is set to nnn.
*/
{
before: VM{I: 0x200},
op: LDI{nnn: 0x400},
after: VM{I: 0x400},
},
/*
Bnnn - JP V0, addr
Jump to location nnn + V0.
The program counter is set to nnn plus the value of V0.
*/
{
before: VM{PC: 0x200, V: [16]uint8{0x0: 0xA}},
op: JPV0{nnn: 0x400},
after: VM{PC: 0x40A, V: [16]uint8{0x0: 0xA}},
},
/*
Cxkk - RND Vx, byte
Set Vx = random byte AND kk.
The interpreter generates a random number from 0 to 255, which is then
ANDed with the value kk. The results are stored in Vx. See instruction
8xy2 for more information on AND.
*/
{
before: VM{V: [16]uint8{0xA: 0x10}, random: Constant{0x77}},
op: RNDVx{x: 0xA, kk: 0xF0},
after: VM{V: [16]uint8{0xA: 0x70}, random: Constant{0x77}},
},
/*
Dxyn - DRW Vx, Vy, nibble
Display n-byte sprite starting at memory location I at (Vx, Vy), set VF =
collision.
The interpreter reads n bytes from memory, starting at the address stored
in I. These bytes are then displayed as sprites on screen at coordinates
(Vx, Vy). Sprites are XORed onto the existing screen. If this causes any
pixels to be erased, VF is set to 1, otherwise it is set to 0. If the
sprite is positioned so part of it is outside the coordinates of the
display, it wraps around to the opposite side of the screen. See
instruction 8xy3 for more information on XOR, and section 2.4, Display,
for more information on the Chip-8 screen and sprites.
*/
{
msg: "no collision, no clipping",
before: VM{
I: 0x5,
V: [16]uint8{0xA: 0x4, 0xB: 0x1},
Memory: [4096]uint8{
0x5: 0xFF,
0x6: 0x0F,
0x7: 0xFF,
},
VideoMemory: [32]uint64{
0x1: 0x0000000000000000,
0x2: 0x0000000000000000,
0x3: 0x0000000000000000,
},
},
op: DRWVxVy{x: 0xA, y: 0xB, n: 3},
after: VM{
I: 0x5,
V: [16]uint8{0xA: 0x4, 0xB: 0x1, 0xF: 0},
Memory: [4096]uint8{
0x5: 0xFF,
0x6: 0x0F,
0x7: 0xFF,
},
VideoMemory: [32]uint64{
0x1: 0x0FF0000000000000,
0x2: 0x00F0000000000000,
0x3: 0x0FF0000000000000,
},
},
},
{
msg: "collision, no clipping",
before: VM{
I: 0x5,
V: [16]uint8{0xA: 0x4, 0xB: 0x1},
Memory: [4096]uint8{
0x5: 0xFF,
0x6: 0x0F,
0x7: 0xFF,
},
VideoMemory: [32]uint64{
0x1: 0x0000000000000000,
0x2: 0x00F0000000000000,
0x3: 0x0000000000000000,
},
},
op: DRWVxVy{x: 0xA, y: 0xB, n: 3},
after: VM{
I: 0x5,
V: [16]uint8{0xA: 0x4, 0xB: 0x1, 0xF: 1},
Memory: [4096]uint8{
0x5: 0xFF,
0x6: 0x0F,
0x7: 0xFF,
},
VideoMemory: [32]uint64{
0x1: 0x0FF0000000000000,
0x2: 0x0000000000000000,
0x3: 0x0FF0000000000000,
},
},
},
{
msg: "no collision, clipping",
before: VM{
I: 0x5,
V: [16]uint8{0xA: 60, 0xB: 0x1},
Memory: [4096]uint8{
0x5: 0xFF,
0x6: 0x0F,
0x7: 0xFF,
},
VideoMemory: [32]uint64{
0x1: 0x0000000000000000,
0x2: 0x0000000000000000,
0x3: 0x0000000000000000,
},
},
op: DRWVxVy{x: 0xA, y: 0xB, n: 3},
after: VM{
I: 0x5,
V: [16]uint8{0xA: 60, 0xB: 0x1, 0xF: 0},
Memory: [4096]uint8{
0x5: 0xFF,
0x6: 0x0F,
0x7: 0xFF,
},
VideoMemory: [32]uint64{
0x1: 0x000000000000000F,
0x2: 0x0000000000000000,
0x3: 0x000000000000000F,
},
},
},
{
msg: "collision, clipping",
before: VM{
I: 0x5,
V: [16]uint8{0xA: 60, 0xB: 0x1},
Memory: [4096]uint8{
0x5: 0xFF,
0x6: 0x0F,
0x7: 0xFF,
},
VideoMemory: [32]uint64{
0x1: 0x000000000000000F,
0x2: 0x0000000000000000,
0x3: 0x0000000000000000,
},
},
op: DRWVxVy{x: 0xA, y: 0xB, n: 3},
after: VM{
I: 0x5,
V: [16]uint8{0xA: 60, 0xB: 0x1, 0xF: 1},
Memory: [4096]uint8{
0x5: 0xFF,
0x6: 0x0F,
0x7: 0xFF,
},
VideoMemory: [32]uint64{
0x1: 0x0000000000000000,
0x2: 0x0000000000000000,
0x3: 0x000000000000000F,
},
},
},
/*
Ex9E - SKP Vx
Skip next instruction if key with the value of Vx is pressed.
Checks the keyboard, and if the key corresponding to the value of Vx is
currently in the down position, PC is increased by 2.
*/
{
msg: "key is pressed",
before: VM{PC: 0x200, V: [16]uint8{0xA: 0xB}, Keys: [16]bool{0xB: true}},
op: SKPVx{x: 0xA},
after: VM{PC: 0x202, V: [16]uint8{0xA: 0xB}, Keys: [16]bool{0xB: true}},
},
{
msg: "key is not pressed",
before: VM{PC: 0x200, V: [16]uint8{0xA: 0xB}, Keys: [16]bool{0xB: false}},
op: SKPVx{x: 0xA},
after: VM{PC: 0x200, V: [16]uint8{0xA: 0xB}, Keys: [16]bool{0xB: false}},
},
/*
ExA1 - SKNP Vx
Skip next instruction if key with the value of Vx is not pressed.
Checks the keyboard, and if the key corresponding to the value of Vx is
currently in the up position, PC is increased by 2.
*/
{
msg: "key is pressed",
before: VM{PC: 0x200, V: [16]uint8{0xA: 0xB}, Keys: [16]bool{0xB: true}},
op: SKNPVx{x: 0xA},
after: VM{PC: 0x200, V: [16]uint8{0xA: 0xB}, Keys: [16]bool{0xB: true}},
},
{
msg: "key is not pressed",
before: VM{PC: 0x200, V: [16]uint8{0xA: 0xB}, Keys: [16]bool{0xB: false}},
op: SKNPVx{x: 0xA},
after: VM{PC: 0x202, V: [16]uint8{0xA: 0xB}, Keys: [16]bool{0xB: false}},
},
/*
Fx07 - LD Vx, DT
Set Vx = delay timer value.
The value of DT is placed into Vx.
*/
{
before: VM{V: [16]uint8{0xA: 0xB}, DT: 0xC},
op: LDVxDT{x: 0xA},
after: VM{V: [16]uint8{0xA: 0xC}, DT: 0xC},
},
/*
Fx0A - LD Vx, K
Wait for a key press, store the value of the key in Vx.
All execution stops until a key is pressed, then the value of that key is
stored in Vx.
*/
{
before: VM{},
op: LDVxK{x: 0xA},
after: VM{IsWaitingForKeyPress: true, K: 0xA},
},
/*
Fx15 - LD DT, Vx
Set delay timer = Vx.
DT is set equal to the value of Vx.
*/
{
before: VM{V: [16]uint8{0xA: 0xB}},
op: LDDTVx{x: 0xA},
after: VM{V: [16]uint8{0xA: 0xB}, DT: 0xB},
},
/*
Fx18 - LD ST, Vx
Set sound timer = Vx.
ST is set equal to the value of Vx.
*/
{
before: VM{V: [16]uint8{0xA: 0xB}},
op: LDSTVx{x: 0xA},
after: VM{V: [16]uint8{0xA: 0xB}, ST: 0xB},
},
/*
Fx1E - ADD I, Vx
Set I = I + Vx.
The values of I and Vx are added, and the results are stored in I.
*/
{
before: VM{I: 0x2, V: [16]uint8{0xA: 0xB}},
op: ADDIVx{x: 0xA},
after: VM{I: 0xD, V: [16]uint8{0xA: 0xB}},
},
/*
Fx29 - LD F, Vx
Set I = location of sprite for digit Vx.
The value of I is set to the location for the hexadecimal sprite
corresponding to the value of Vx. See section 2.4, Display, for more
information on the Chip-8 hexadecimal font.
*/
{
before: VM{V: [16]uint8{0xA: 0xB}},
op: LDFVx{x: 0xA},
after: VM{I: 55, V: [16]uint8{0xA: 0xB}},
},
/*
Fx33 - LD B, Vx
Store BCD representation of Vx in memory locations I, I+1, and I+2.
The interpreter takes the decimal value of Vx, and places the hundreds
digit in memory at location in I, the tens digit at location I+1, and
the ones digit at location I+2.
*/
{
before: VM{I: 0x300, V: [16]uint8{0xA: 123}},
op: LDBVx{x: 0xA},
after: VM{
I: 0x300,
V: [16]uint8{0xA: 123},
Memory: [4096]uint8{0x300: 1, 0x301: 2, 0x302: 3},
},
},
/*
Fx55 - LD [I], Vx
Store registers V0 through Vx in memory starting at location I.
The interpreter copies the values of registers V0 through Vx into
memory, starting at the address in I.
*/
{
before: VM{I: 0x300, V: [16]uint8{0x0: 0x0, 0x1: 0x1, 0x2: 0x2, 0x3: 0x3}},
op: LDIVx{x: 0x3},
after: VM{
I: 0x300,
V: [16]uint8{0x0: 0x0, 0x1: 0x1, 0x2: 0x2, 0x3: 0x3},
Memory: [4096]uint8{0x300: 0x0, 0x301: 0x1, 0x302: 0x2, 0x303: 0x3},
},
},
/*
Fx65 - LD Vx, [I]
Read registers V0 through Vx from memory starting at location I.
The interpreter reads values from memory starting at location I into
registers V0 through Vx.
*/
{
before: VM{
I: 0x300,
Memory: [4096]uint8{0x300: 0x0, 0x301: 0x1, 0x302: 0x2, 0x303: 0x3},
},
op: LDVxI{x: 0x3},
after: VM{
I: 0x300,
Memory: [4096]uint8{0x300: 0x0, 0x301: 0x1, 0x302: 0x2, 0x303: 0x3},
V: [16]uint8{0x0: 0x0, 0x1: 0x1, 0x2: 0x2, 0x3: 0x3},
},
},
} {
actualAfter := testCase.before
testCase.op.execute(&actualAfter)
if testCase.after != actualAfter {
t.Errorf("Unexpected VM state after executing %#v %s", testCase.op, testCase.msg)
}
}
}