-
Notifications
You must be signed in to change notification settings - Fork 0
/
chip8.go
1033 lines (827 loc) · 18.1 KB
/
chip8.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
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package chip8
import (
"fmt"
"math/rand"
)
const (
ScreenWidth = 64
ScreenHeight = 32
)
type Random interface {
Next() uint8
}
type defaultRandom struct{}
func (r defaultRandom) Next() uint8 {
return uint8(rand.Uint32())
}
type VM struct {
// SP is the stack pointer
SP uint8
// I is a pointer register
I uint16
// PC is the program counter
PC uint16
// V are the 16 general purpose registers
V [16]uint8
// DT is the delay timer register
DT uint8
// DT is the sound timer register
ST uint8
// Memory contains the default sprites, the ROM and the RAM
Memory [4096]uint8
// Stack holds up to 16 memory locations
Stack [16]uint16
// VideoMemory represents the 64x32 pixel screen as 64-bit scan lines
VideoMemory [ScreenHeight]uint64
// Keys are a list of flags (0x0 - 0xF) signfying if a key is held down or not
Keys [16]bool
// IsWaitingForKeyPress is true when the VM is waiting for a key to be pressed
IsWaitingForKeyPress bool
// K is the register (0x0 - 0xF) waiting for a key to be pressed
K uint8
// random provides a random byte value
random Random
}
const (
romStartAddress = 0x200
digitStartAddress = 0x000
digitSpriteSize = 5
)
var digitSprites = []uint8{
0xF0, 0x90, 0x90, 0x90, 0xF0, // 0
0x20, 0x60, 0x20, 0x20, 0x70, // 1
0xF0, 0x10, 0xF0, 0x80, 0xF0, // 2
0xF0, 0x10, 0xF0, 0x10, 0xF0, // 3
0x90, 0x90, 0xF0, 0x10, 0x10, // 4
0xF0, 0x80, 0xF0, 0x10, 0xF0, // 5
0xF0, 0x80, 0xF0, 0x90, 0xF0, // 6
0xF0, 0x10, 0x20, 0x40, 0x40, // 7
0xF0, 0x90, 0xF0, 0x90, 0xF0, // 8
0xF0, 0x90, 0xF0, 0x10, 0xF0, // 9
0xF0, 0x90, 0xF0, 0x90, 0x90, // A
0xE0, 0x90, 0xE0, 0x90, 0xE0, // B
0xF0, 0x80, 0x80, 0x80, 0xF0, // C
0xE0, 0x90, 0x90, 0x90, 0xE0, // D
0xF0, 0x80, 0xF0, 0x80, 0xF0, // E
0xF0, 0x80, 0xF0, 0x80, 0x80, // F
}
func New(rom []uint8) *VM {
vm := VM{random: defaultRandom{}, PC: romStartAddress}
copy(vm.Memory[0:len(digitSprites)], digitSprites)
bytesCopied := copy(vm.Memory[romStartAddress:romStartAddress+len(rom)], rom)
if bytesCopied < len(rom) {
panic(fmt.Sprintf("Not enough memory to fit ROM of size %d bytes", len(rom)))
}
return &vm
}
func (vm *VM) SetKeys(keys [16]bool) {
vm.Keys = keys
}
func (vm *VM) SetKeyDown(key uint8) {
if key > 0xff {
panic(fmt.Sprintf("Unsupported key: %#x", key))
}
vm.Keys[key] = true
if vm.IsWaitingForKeyPress {
vm.V[vm.K] = key
vm.IsWaitingForKeyPress = false
}
}
func (vm *VM) SetKeyUp(key uint8) {
if key > 0xff {
panic(fmt.Sprintf("Unsupported key: %#x", key))
}
vm.Keys[key] = false
}
func (vm *VM) TickTimers() {
if vm.DT > 0 {
vm.DT--
}
if vm.ST > 0 {
vm.ST--
}
}
func (vm *VM) Step() {
if vm.IsWaitingForKeyPress {
return
}
vm.fetch().decode().execute(vm)
}
func (vm *VM) fetch() EncodedOp {
op := EncodedOp(uint16(vm.Memory[vm.PC])<<8 | uint16(vm.Memory[vm.PC+1]))
vm.PC += 2
return op
}
type EncodedOp uint16
// .nnn
func (op EncodedOp) nnn() uint16 {
return uint16(op & 0x0FFF)
}
// ..kk
func (op EncodedOp) kk() uint8 {
return uint8(op & 0x00FF)
}
// ...n
func (op EncodedOp) n() uint8 {
return uint8(op & 0x000F)
}
// .x..
func (op EncodedOp) x() uint8 {
return uint8((op & 0x0F00) >> 8)
}
// ..y.
func (op EncodedOp) y() uint8 {
return uint8((op & 0x00F0) >> 4)
}
func (op EncodedOp) decode() Op {
switch op >> 12 {
case 0x0:
switch op {
case 0x00E0:
return op.decodeCLS()
case 0x00EE:
return op.decodeRET()
}
case 0x1:
return op.decodeJP()
case 0x2:
return op.decodeCALL()
case 0x3:
return op.decodeSEVx()
case 0x4:
return op.decodeSNEVx()
case 0x5:
switch op & 0x000F {
case 0x0:
return op.decodeSEVxVy()
}
case 0x6:
return op.decodeLDVx()
case 0x7:
return op.decodeADDVx()
case 0x8:
switch op & 0x000F {
case 0x0:
return op.decodeLDVxVy()
case 0x1:
return op.decodeORVxVy()
case 0x2:
return op.decodeANDVxVy()
case 0x3:
return op.decodeXORVxVy()
case 0x4:
return op.decodeADDVxVy()
case 0x5:
return op.decodeSUBVxVy()
case 0x6:
return op.decodeSHRVx()
case 0x7:
return op.decodeSUBNVxVy()
case 0xE:
return op.decodeSHLVx()
}
case 0x9:
switch op & 0x000F {
case 0x0:
return op.decodeSNEVxVy()
}
case 0xA:
return op.decodeLDI()
case 0xB:
return op.decodeJPV0()
case 0xC:
return op.decodeRNDVx()
case 0xD:
return op.decodeDRWVxVy()
case 0xE:
switch op & 0x00FF {
case 0x9E:
return op.decodeSKPVx()
case 0xA1:
return op.decodeSKNPVx()
}
case 0xF:
switch op & 0x00FF {
case 0x07:
return op.decodeLDVxDT()
case 0x0A:
return op.decodeLDVxK()
case 0x15:
return op.decodeLDDTVx()
case 0x18:
return op.decodeLDSTVx()
case 0x1E:
return op.decodeADDIVx()
case 0x29:
return op.decodeLDFVx()
case 0x33:
return op.decodeLDBVx()
case 0x55:
return op.decodeLDIVx()
case 0x65:
return op.decodeLDVxI()
}
}
panic(fmt.Sprintf("Unsupported op: %#X", op))
}
type Op interface {
execute(*VM)
}
/*
00E0 - CLS
Clear the display.
*/
type CLS struct{}
func (op EncodedOp) decodeCLS() CLS {
return CLS{}
}
func (op CLS) execute(vm *VM) {
for i := range vm.VideoMemory {
vm.VideoMemory[i] = 0
}
}
/*
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.
*/
type RET struct{}
func (op EncodedOp) decodeRET() RET {
return RET{}
}
func (op RET) execute(vm *VM) {
vm.SP--
vm.PC = vm.Stack[vm.SP]
}
/*
1nnn - JP addr
Jump to location nnn.
The interpreter sets the program counter to nnn.
*/
type JP struct {
nnn uint16
}
func (op EncodedOp) decodeJP() JP {
return JP{op.nnn()}
}
func (op JP) execute(vm *VM) {
vm.PC = op.nnn
}
/*
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.
*/
type CALL struct {
nnn uint16
}
func (op EncodedOp) decodeCALL() CALL {
return CALL{op.nnn()}
}
func (op CALL) execute(vm *VM) {
if vm.SP >= uint8(len(vm.Stack)) {
panic("Stack overflow")
}
vm.Stack[vm.SP] = vm.PC
vm.SP += 1
vm.PC = op.nnn
}
/*
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.
*/
type SEVx struct {
x, kk uint8
}
func (op EncodedOp) decodeSEVx() SEVx {
return SEVx{op.x(), op.kk()}
}
func (op SEVx) execute(vm *VM) {
if vm.V[op.x] == op.kk {
vm.PC += 2
}
}
/*
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.
*/
type SNEVx struct {
x, kk uint8
}
func (op EncodedOp) decodeSNEVx() SNEVx {
return SNEVx{op.x(), op.kk()}
}
func (op SNEVx) execute(vm *VM) {
if vm.V[op.x] != op.kk {
vm.PC += 2
}
}
/*
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.
*/
type SEVxVy struct {
x, y uint8
}
func (op EncodedOp) decodeSEVxVy() SEVxVy {
return SEVxVy{op.x(), op.y()}
}
func (op SEVxVy) execute(vm *VM) {
if vm.V[op.x] == vm.V[op.y] {
vm.PC += 2
}
}
/*
6xkk - LD Vx, byte
Set Vx = kk.
The interpreter puts the value kk into register Vx.
*/
type LDVx struct {
x, kk uint8
}
func (op EncodedOp) decodeLDVx() LDVx {
return LDVx{op.x(), op.kk()}
}
func (op LDVx) execute(vm *VM) {
vm.V[op.x] = op.kk
}
/*
7xkk - ADD Vx, byte
Set Vx = Vx + kk.
Adds the value kk to the value of register Vx, then stores the result in Vx.
*/
type ADDVx struct {
x, kk uint8
}
func (op EncodedOp) decodeADDVx() ADDVx {
return ADDVx{op.x(), op.kk()}
}
func (op ADDVx) execute(vm *VM) {
vm.V[op.x] += op.kk
}
/*
8xy0 - LD Vx, Vy
Set Vx = Vy.
Stores the value of register Vy in register Vx.
*/
type LDVxVy struct {
x, y uint8
}
func (op EncodedOp) decodeLDVxVy() LDVxVy {
return LDVxVy{op.x(), op.y()}
}
func (op LDVxVy) execute(vm *VM) {
vm.V[op.x] = vm.V[op.y]
}
/*
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.
*/
type ORVxVy struct {
x, y uint8
}
func (op EncodedOp) decodeORVxVy() ORVxVy {
return ORVxVy{op.x(), op.y()}
}
func (op ORVxVy) execute(vm *VM) {
vm.V[op.x] = vm.V[op.x] | vm.V[op.y]
}
/*
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.
*/
type ANDVxVy struct {
x, y uint8
}
func (op EncodedOp) decodeANDVxVy() ANDVxVy {
return ANDVxVy{op.x(), op.y()}
}
func (op ANDVxVy) execute(vm *VM) {
vm.V[op.x] = vm.V[op.x] & vm.V[op.y]
}
/*
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.
*/
type XORVxVy struct {
x, y uint8
}
func (op EncodedOp) decodeXORVxVy() XORVxVy {
return XORVxVy{op.x(), op.y()}
}
func (op XORVxVy) execute(vm *VM) {
vm.V[op.x] = vm.V[op.x] ^ vm.V[op.y]
}
/*
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.
*/
type ADDVxVy struct {
x, y uint8
}
func (op EncodedOp) decodeADDVxVy() ADDVxVy {
return ADDVxVy{op.x(), op.y()}
}
func (op ADDVxVy) execute(vm *VM) {
sum := uint16(vm.V[op.x]) + uint16(vm.V[op.y])
if sum > 255 {
vm.V[0xF] = 1
} else {
vm.V[0xF] = 0
}
vm.V[op.x] = uint8(sum)
}
/*
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.
*/
type SUBVxVy struct {
x, y uint8
}
func (op EncodedOp) decodeSUBVxVy() SUBVxVy {
return SUBVxVy{op.x(), op.y()}
}
func (op SUBVxVy) execute(vm *VM) {
if vm.V[op.x] > vm.V[op.y] {
vm.V[0xF] = 1
} else {
vm.V[0xF] = 0
}
vm.V[op.x] = vm.V[op.x] - vm.V[op.y]
}
/*
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.
*/
type SHRVx struct {
x uint8
}
func (op EncodedOp) decodeSHRVx() SHRVx {
return SHRVx{op.x()}
}
func (op SHRVx) execute(vm *VM) {
if vm.V[op.x]&0x01 == 1 {
vm.V[0xF] = 1
} else {
vm.V[0xF] = 0
}
vm.V[op.x] >>= 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.
*/
type SUBNVxVy struct {
x, y uint8
}
func (op EncodedOp) decodeSUBNVxVy() SUBNVxVy {
return SUBNVxVy{op.x(), op.y()}
}
func (op SUBNVxVy) execute(vm *VM) {
if vm.V[op.y] > vm.V[op.x] {
vm.V[0xF] = 1
} else {
vm.V[0xF] = 0
}
vm.V[op.x] = vm.V[op.y] - vm.V[op.x]
}
/*
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.
*/
type SHLVx struct {
x uint8
}
func (op EncodedOp) decodeSHLVx() SHLVx {
return SHLVx{op.x()}
}
func (op SHLVx) execute(vm *VM) {
if vm.V[op.x]&0x80 > 0 {
vm.V[0xF] = 1
} else {
vm.V[0xF] = 0
}
vm.V[op.x] <<= 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.
*/
type SNEVxVy struct {
x, y uint8
}
func (op EncodedOp) decodeSNEVxVy() SNEVxVy {
return SNEVxVy{op.x(), op.y()}
}
func (op SNEVxVy) execute(vm *VM) {
if vm.V[op.x] != vm.V[op.y] {
vm.PC += 2
}
}
/*
Annn - LD I, addr
Set I = nnn.
The value of register I is set to nnn.
*/
type LDI struct {
nnn uint16
}
func (op EncodedOp) decodeLDI() LDI {
return LDI{op.nnn()}
}
func (op LDI) execute(vm *VM) {
vm.I = op.nnn
}
/*
Bnnn - JP V0, addr
Jump to location nnn + V0.
The program counter is set to nnn plus the value of V0.
*/
type JPV0 struct {
nnn uint16
}
func (op EncodedOp) decodeJPV0() JPV0 {
return JPV0{op.nnn()}
}
func (op JPV0) execute(vm *VM) {
vm.PC = op.nnn + uint16(vm.V[0])
}
/*
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.
*/
type RNDVx struct {
x, kk uint8
}
func (op EncodedOp) decodeRNDVx() RNDVx {
return RNDVx{op.x(), op.kk()}
}
func (op RNDVx) execute(vm *VM) {
vm.V[op.x] = vm.random.Next() & op.kk
}
/*
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.
*/
type DRWVxVy struct {
x, y, n uint8
}
func (op EncodedOp) decodeDRWVxVy() DRWVxVy {
return DRWVxVy{op.x(), op.y(), op.n()}
}
func expandSpriteRowToScanLine(spriteRow, x uint8) uint64 {
if x <= 56 {
// shift sprite left
return uint64(spriteRow) << (56 - x)
} else {
// shift sprite right
return uint64(spriteRow) >> (x - 56)
}
}
func (op DRWVxVy) execute(vm *VM) {
collision := false
x0 := vm.V[op.x]
y0 := vm.V[op.y]
sprite := vm.Memory[vm.I : vm.I+uint16(op.n)]
for yi, spriteRow := range sprite {
y := y0 + uint8(yi)
if y >= uint8(len(vm.VideoMemory)) {
break
}
oldScanLine := vm.VideoMemory[y]
newScanLine := oldScanLine ^ expandSpriteRowToScanLine(spriteRow, x0)
vm.VideoMemory[y] = newScanLine
if (oldScanLine & ^newScanLine) > 0 {
collision = true
}
}
if collision {
vm.V[0xF] = 1
} else {
vm.V[0xF] = 0
}
}
/*
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.
*/
type SKPVx struct {
x uint8
}
func (op EncodedOp) decodeSKPVx() SKPVx {
return SKPVx{op.x()}
}
func (op SKPVx) execute(vm *VM) {
if vm.Keys[vm.V[op.x]] {
vm.PC += 2
}
}
/*
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.
*/
type SKNPVx struct {
x uint8
}
func (op EncodedOp) decodeSKNPVx() SKNPVx {
return SKNPVx{op.x()}
}
func (op SKNPVx) execute(vm *VM) {
if !vm.Keys[vm.V[op.x]] {
vm.PC += 2
}
}
/*
Fx07 - LD Vx, DT
Set Vx = delay timer value.
The value of DT is placed into Vx.
*/
type LDVxDT struct {
x uint8
}
func (op EncodedOp) decodeLDVxDT() LDVxDT {
return LDVxDT{op.x()}
}
func (op LDVxDT) execute(vm *VM) {
vm.V[op.x] = vm.DT
}
/*
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.
*/
type LDVxK struct {
x uint8
}
func (op EncodedOp) decodeLDVxK() LDVxK {
return LDVxK{op.x()}
}
func (op LDVxK) execute(vm *VM) {
vm.IsWaitingForKeyPress = true
vm.K = op.x
}
/*
Fx15 - LD DT, Vx
Set delay timer = Vx.
DT is set equal to the value of Vx.
*/
type LDDTVx struct {
x uint8
}
func (op EncodedOp) decodeLDDTVx() LDDTVx {
return LDDTVx{op.x()}
}
func (op LDDTVx) execute(vm *VM) {
vm.DT = vm.V[op.x]
}
/*
Fx18 - LD ST, Vx
Set sound timer = Vx.
ST is set equal to the value of Vx.
*/
type LDSTVx struct {
x uint8
}
func (op EncodedOp) decodeLDSTVx() LDSTVx {
return LDSTVx{op.x()}
}
func (op LDSTVx) execute(vm *VM) {
vm.ST = vm.V[op.x]
}
/*
Fx1E - ADD I, Vx
Set I = I + Vx.
The values of I and Vx are added, and the results are stored in I.
*/
type ADDIVx struct {
x uint8
}
func (op EncodedOp) decodeADDIVx() ADDIVx {
return ADDIVx{op.x()}
}
func (op ADDIVx) execute(vm *VM) {
vm.I += uint16(vm.V[op.x])
}
/*
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.
*/
type LDFVx struct {
x uint8
}
func (op EncodedOp) decodeLDFVx() LDFVx {
return LDFVx{op.x()}
}
func (op LDFVx) execute(vm *VM) {
if op.x > 0xF {
panic(fmt.Sprintf("LDFVx unsupported digit %#x", op.x))
}
vm.I = digitStartAddress + digitSpriteSize*uint16(vm.V[op.x])
}
/*
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.
*/
type LDBVx struct {
x uint8
}
func (op EncodedOp) decodeLDBVx() LDBVx {
return LDBVx{op.x()}
}
func bcd(n uint8) (hundreds, tens, ones uint8) {
hundreds = n / 100
tens = (n / 10) % 10
ones = n % 10
return
}
func (op LDBVx) execute(vm *VM) {
vm.Memory[vm.I], vm.Memory[vm.I+1], vm.Memory[vm.I+2] = bcd(vm.V[op.x])
}
/*
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.
*/
type LDIVx struct {
x uint8