/
chip8.go
1100 lines (906 loc) · 23.5 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
/* Copyright (c) 2017 Jeffrey Massung
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*/
package chip8
import (
"errors"
"fmt"
"io/ioutil"
"math/rand"
"time"
"unicode"
)
// CHIP_8 virtual machine emulator.
type CHIP_8 struct {
// ROM memory for CHIP-8. This holds the reserved 512 bytes as
// well as the program memory. It is a pristine state upon being
// loaded that Memory can be reset back to.
ROM [0x1000]byte
// Memory addressable by CHIP-8. The first 512 bytes are reserved
// for the font sprites, any RCA 1802 code, and the stack.
Memory [0x1000]byte
// Video memory for CHIP-8 (64x32 bits). Each bit represents a
// single pixel. It is stored MSB first. For example, pixel <0,0>
// is bit 0x80 of byte 0. 4x the video memory is used for the
// CHIP-48, which is 128x64 resolution. There are 4 extra lines
// to prevent overflows when scrolling.
Video [0x440]byte
// The stack was in a reserved section of memory on the 1802.
// Originally it was only 12-cells deep, but later implementations
// went as high as 16-cells.
Stack [16]uint
// SP is the stack pointer.
SP uint
// PC is the program counter. All programs begin at 0x200.
PC uint
// Base is the starting address of the program in hardware. This is
// 0x200 for all ROMs except those running on the ETI-660, which start
// at 0x600.
Base uint
// The size of the ROM.
Size int
// I is the address register.
I uint
// V are the 16 virtual registers.
V [16]byte
// R are the 8, HP-RPL user flags.
R [8]byte
// DT is the delay timer register. It is set to a time (in ns) in the
// future and compared against the current time.
DT int64
// ST is the sound timer register. It is set to a time (in ns) in the
// future and compared against the current time.
ST int64
// Clock is the time (in ns) when emulation begins.
Clock int64
// Cycles is how many clock cycles have been processed. It is assumed
// one clock cycle per instruction.
Cycles int64
// Speed is how many cycles (instructions) should execute per second.
// By default this is 700. The RCA CDP1802 ran at 1.76 MHz, with each
// instruction taking 16-24 clock cycles, which is a bit over 70,000
// instructions per second.
Speed int64
// W is the wait key (V-register) pointer. When waiting for a key
// to be pressed, it will be set to &V[0..F].
W *byte
// Keys hold the current state for the 16-key pad keys.
Keys [16]bool
// Number of bytes per scan line. This is 8 in low mode and 16 when high.
Pitch int
// A mapping of address breakpoints.
Breakpoints map[int]Breakpoint
}
// Breakpoint is an implementation of error.
type Breakpoint struct {
// Address is the memory address where the PC should break.
Address int
// Reason is used to identify what id happening in code.
Reason string
// Conditional is true if the breakpoint only trips when VF != 0.
Conditional bool
// Once is true if the breakpoint should be removed once hit.
Once bool
}
// Error implements the error interface for a Breakpoint.
func (b Breakpoint) Error() string {
if b.Conditional {
return fmt.Sprintf("hit assert @ %04X: %s", b.Address, b.Reason)
} else {
return fmt.Sprintf("hit breakpoint @ %04X: %s", b.Address, b.Reason)
}
}
// SysCall is an implementation of error.
type SysCall struct {
// Address is the memory location where the CDP1802 instructions
// are located.
Address uint
}
// Error implements the error interface for a SysCall.
func (call SysCall) Error() string {
return fmt.Sprintf("unimplmented syscall to #%04X", call.Address)
}
// Load a ROM from a byte array and return a new CHIP-8 virtual machine.
func LoadROM(program []byte, eti bool) (*CHIP_8, error) {
base := 0x200
// ETI-660 roms begin at 0x600
if eti {
base = 0x600
}
// make sure the program fits within 4k
if len(program) > 0x1000-base {
return nil, errors.New("Program too large to fit in memory!")
}
// initialize any data that doesn't Reset()
vm := &CHIP_8{
Size: len(program),
Breakpoints: make(map[int]Breakpoint),
Base: uint(base),
Speed: 700,
}
// copy the RCA 1802 512 byte ROM into the CHIP-8 followed by the program
copy(vm.ROM[:base], EmulatorROM[:])
copy(vm.ROM[base:], program[:])
// reset the VM memory
vm.Reset()
return vm, nil
}
// Load a compiled assembly and return a new CHIP-8 virtual machine.
func LoadAssembly(asm *Assembly, eti bool) (*CHIP_8, error) {
if vm, err := LoadROM(asm.ROM, eti); err != nil {
return nil, err
} else {
// set all the breakpoints found in the assembly
for _, b := range asm.Breakpoints {
vm.SetBreakpoint(b)
}
return vm, nil
}
}
// Load a ROM file and return a new CHIP-8 virtual machine.
func LoadFile(file string, eti bool) (*CHIP_8, error) {
if program, err := ioutil.ReadFile(file); err != nil {
return nil, err
} else {
for _, c := range string(program) {
if unicode.IsSpace(c) || unicode.IsGraphic(c) {
continue
}
// file is a binary rom, load that
return LoadROM(program, eti)
}
// a text file that needs assembled
if asm, err := Assemble(program, eti); err != nil {
return nil, err
} else {
return LoadAssembly(asm, eti)
}
}
}
// Write the ROM file to disk.
func (vm *CHIP_8) SaveROM(file string, includeInterpreter bool) error {
bytes := vm.ROM[vm.Base : vm.Base+uint(vm.Size)]
// if including the interpreter, prepend it
if includeInterpreter {
bytes = append(Interpreter, bytes...)
}
return ioutil.WriteFile(file, bytes, 666)
}
// Reset the CHIP-8 virtual machine memory.
func (vm *CHIP_8) Reset() {
copy(vm.Memory[:], vm.ROM[:])
// reset video memory
vm.Video = [0x440]byte{}
// reset keys
vm.Keys = [16]bool{}
// reset program counter and stack pointer
vm.PC = vm.Base
vm.SP = 0
// reset address register
vm.I = 0
// reset virtual registers and user flags
vm.V = [16]byte{}
vm.R = [8]byte{}
// reset timer registers
vm.DT = 0
vm.ST = 0
// reset the clock and cycles executed
vm.Clock = time.Now().UnixNano()
vm.Cycles = 0
// not waiting for a key
vm.W = nil
// not in high-res mode
vm.Pitch = 8
}
// HighRes returns true if the CHIP-8 is in high resolution mode.
func (vm *CHIP_8) HighRes() bool {
return vm.Pitch > 8
}
// IncSpeed increases CHIP-8 virtual machine performance.
func (vm *CHIP_8) IncSpeed() int {
if vm.Speed < 15000 {
vm.Speed += 200
// reset the clock
vm.Clock = time.Now().UnixNano()
vm.Cycles = 0
}
return int(vm.Speed * 100 / 700)
}
// DecSpeed lowers CHIP-8 virtual machine performance.
func (vm *CHIP_8) DecSpeed() int {
if vm.Speed > 100 {
vm.Speed -= 200
// reset the clock
vm.Clock = time.Now().UnixNano()
vm.Cycles = 0
}
return int(vm.Speed * 100 / 700)
}
// SetBreakpoint at a ROM address to the CHIP-8 virtual machine.
func (vm *CHIP_8) SetBreakpoint(b Breakpoint) {
if b.Address >= 0x200 && b.Address < len(vm.ROM) {
vm.Breakpoints[b.Address] = b
}
}
// StepOverBreakpoint creates a one-time breakpoint on the next instruction
// if the current instruction is a CALL.
func (vm *CHIP_8) StepOverBreakpoint() bool {
if vm.Memory[vm.PC]&0xF0 != 0x20 {
return false
}
// set create a one-time breakpoint at the next instruction
address := int(vm.PC) + 2
// only if one isn't already there
if _, ok := vm.Breakpoints[address]; !ok {
vm.SetBreakpoint(Breakpoint{
Address: address,
Once: true,
})
}
return true
}
// RemoveBreakpoint clears a breakpoint at a given ROM address.
func (vm *CHIP_8) RemoveBreakpoint(address int) {
delete(vm.Breakpoints, address)
}
// ToggleBreakpoint at the current PC. Any reason is lost.
func (vm *CHIP_8) ToggleBreakpoint() {
address := int(vm.PC)
if _, ok := vm.Breakpoints[address]; !ok {
vm.SetBreakpoint(Breakpoint{
Address: address,
Reason: "User break",
})
} else {
vm.RemoveBreakpoint(address)
}
}
// ClearBreakpoints removes all breakpoints.
func (vm *CHIP_8) ClearBreakpoints() {
vm.Breakpoints = make(map[int]Breakpoint)
}
// PressKey emulates a CHIP-8 key being pressed.
func (vm *CHIP_8) PressKey(key uint) {
if key < 16 {
vm.Keys[key] = true
// if waiting for a key, set it now
if vm.W != nil {
*vm.W = byte(key)
// clear wait flag
vm.W = nil
}
}
}
// ReleaseKey emulates a CHIP-8 key being released.
func (vm *CHIP_8) ReleaseKey(key uint) {
if key < 16 {
vm.Keys[key] = false
}
}
// Converts a CHIP-8 delay timer register to a byte.
func (vm *CHIP_8) GetDelayTimer() byte {
now := time.Now().UnixNano()
if now < vm.DT {
return uint8((vm.DT - now) * 60 / 1000000000)
}
return 0
}
// Converts the CHIP-8 sound timer register to a byte.
func (vm *CHIP_8) GetSoundTimer() byte {
now := time.Now().UnixNano()
if now < vm.ST {
return uint8((vm.ST - now) * 60 / 1000000000)
}
return 0
}
// GetResolution returns the width and height of the CHIP-8.
func (vm *CHIP_8) GetResolution() (int, int) {
return vm.Pitch << 3, vm.Pitch << 2
}
// Process CHIP-8 emulation. This will execute until the clock is caught up.
func (vm *CHIP_8) Process(paused bool) error {
now := time.Now().UnixNano()
// calculate how many cycles should have been executed
count := (now - vm.Clock) * vm.Speed / 1000000000
// if paused, count cycles without stepping
if paused {
vm.Cycles = count
} else {
for vm.Cycles < count {
if err := vm.Step(); err != nil {
return err
}
// if waiting for a key, catch up
if vm.W != nil {
vm.Cycles = count
}
}
}
return nil
}
// Step the CHIP-8 virtual machine a single instruction.
func (vm *CHIP_8) Step() error {
if vm.W != nil {
return nil
}
// fetch the next instruction
inst := vm.fetch()
// 12-bit address operand
a := inst & 0xFFF
// byte and nibble operands
b := byte(inst & 0xFF)
n := byte(inst & 0xF)
// x and y register operands
x := inst >> 8 & 0xF
y := inst >> 4 & 0xF
// instruction decoding
if inst == 0x00E0 {
vm.cls()
} else if inst == 0x00EE {
vm.ret()
} else if inst == 0x00FB {
vm.scrollRight()
} else if inst == 0x00FC {
vm.scrollLeft()
} else if inst == 0x00FD {
vm.exit()
} else if inst == 0x00FE {
vm.low()
} else if inst == 0x00FF {
vm.high()
} else if inst&0xFFF0 == 0x00B0 {
vm.scrollUp(n)
} else if inst&0xFFF0 == 0x00C0 {
vm.scrollDown(n)
} else if inst&0xF000 == 0x0000 {
vm.sys(a)
} else if inst&0xF000 == 0x1000 {
vm.jump(a)
} else if inst&0xF000 == 0x2000 {
vm.call(a)
} else if inst&0xF000 == 0x3000 {
vm.skipIf(x, b)
} else if inst&0xF000 == 0x4000 {
vm.skipIfNot(x, b)
} else if inst&0xF00F == 0x5000 {
vm.skipIfXY(x, y)
} else if inst&0xF00F == 0x5001 {
vm.skipIfGreater(x, y)
} else if inst&0xF00F == 0x5002 {
vm.skipIfLess(x, y)
} else if inst&0xF000 == 0x6000 {
vm.loadX(x, b)
} else if inst&0xF000 == 0x7000 {
vm.addX(x, b)
} else if inst&0xF00F == 0x8000 {
vm.loadXY(x, y)
} else if inst&0xF00F == 0x8001 {
vm.or(x, y)
} else if inst&0xF00F == 0x8002 {
vm.and(x, y)
} else if inst&0xF00F == 0x8003 {
vm.xor(x, y)
} else if inst&0xF00F == 0x8004 {
vm.addXY(x, y)
} else if inst&0xF00F == 0x8005 {
vm.subXY(x, y)
} else if inst&0xF00F == 0x8006 {
vm.shr(x)
} else if inst&0xF00F == 0x8007 {
vm.subYX(x, y)
} else if inst&0xF00F == 0x800E {
vm.shl(x)
} else if inst&0xF00F == 0x9000 {
vm.skipIfNotXY(x, y)
} else if inst&0xF00F == 0x9001 {
vm.mulXY(x, y)
} else if inst&0xF00F == 0x9002 {
vm.divXY(x, y)
} else if inst&0xF0FF == 0xF033 {
vm.bcd(x)
} else if inst&0xF00F == 0x9003 {
vm.bcd16(x, y)
} else if inst&0xF000 == 0xA000 {
vm.loadI(a)
} else if inst&0xF000 == 0xB000 {
vm.jumpV0(a)
} else if inst&0xF000 == 0xC000 {
vm.loadRandom(x, b)
} else if inst&0xF00F == 0xD000 {
vm.drawSpriteEx(x, y)
} else if inst&0xF000 == 0xD000 {
vm.drawSprite(x, y, n)
} else if inst&0xF0FF == 0xE09E {
vm.skipIfPressed(x)
} else if inst&0xF0FF == 0xE0A1 {
vm.skipIfNotPressed(x)
} else if inst&0xF0FF == 0xF007 {
vm.loadXDT(x)
} else if inst&0xF0FF == 0xF00A {
vm.loadXK(x)
} else if inst&0xF0FF == 0xF015 {
vm.loadDTX(x)
} else if inst&0xF0FF == 0xF018 {
vm.loadSTX(x)
} else if inst&0xF0FF == 0xF01E {
vm.addIX(x)
} else if inst&0xF0FF == 0xF029 {
vm.loadF(x)
} else if inst&0xF0FF == 0xF030 {
vm.loadHF(x)
} else if inst&0xF0FF == 0xF055 {
vm.saveRegs(x)
} else if inst&0xF0FF == 0xF065 {
vm.loadRegs(x)
} else if inst&0xF0FF == 0xF075 {
vm.storeR(x)
} else if inst&0xF0FF == 0xF085 {
vm.readR(x)
} else if inst&0xF0FF == 0xF094 {
vm.loadASCII(x)
} else {
return fmt.Errorf("Invalid opcode: %04X", inst)
}
// increment the cycle count
vm.Cycles += 1
// if at a breakpoint, return it
if b, ok := vm.Breakpoints[int(vm.PC)]; ok {
if !b.Conditional || vm.V[0xF] != 0 {
if b.Once {
delete(vm.Breakpoints, int(vm.PC))
}
return b
}
}
return nil
}
// StepOut executes instructions until a RET instruction is executed.
func (vm *CHIP_8) StepOut() error {
sp := vm.SP
// if not in a subroutine, don't do anything
for sp > 0 && vm.SP >= sp {
if err := vm.Step(); err != nil {
return err
}
}
return nil
}
// Fetch the next 16-bit instruction to execute.
func (vm *CHIP_8) fetch() uint {
i := vm.PC
// advance the program counter
vm.PC += 2
// return the 16-bit instruction
return uint(vm.Memory[i])<<8 | uint(vm.Memory[i+1])
}
// Clear the video display memory.
func (vm *CHIP_8) cls() {
for i := range vm.Video {
vm.Video[i] = 0
}
}
// System call an RCA 1802 program at an address.
func (vm *CHIP_8) sys(address uint) {
panic("SYS calls are unimplemented")
}
// Call a subroutine at address.
func (vm *CHIP_8) call(address uint) {
if int(vm.SP) >= len(vm.Stack) {
panic("Stack overflow!")
}
// post increment
vm.Stack[vm.SP] = vm.PC
vm.SP += 1
// jump to address
vm.PC = address
}
// Return from subroutine.
func (vm *CHIP_8) ret() {
if vm.SP == 0 {
panic("Stack underflow!")
}
// pre-decrement
vm.SP -= 1
vm.PC = vm.Stack[vm.SP]
}
// Exit the interpreter.
func (vm *CHIP_8) exit() {
vm.PC -= 2
}
// Set low res mode.
func (vm *CHIP_8) low() {
vm.Pitch = 8
}
// Set high res mode.
func (vm *CHIP_8) high() {
vm.Pitch = 16
}
// Scroll n pixels up.
func (vm *CHIP_8) scrollUp(n byte) {
if vm.Pitch == 8 {
n >>= 1
}
// shift all the pixels up
copy(vm.Video[:], vm.Video[int(n)*vm.Pitch:])
// wipe the bottom-most pixels
for i := 0x400 - int(n)*vm.Pitch; i < 0x400; i++ {
vm.Video[i] = 0
}
}
// Scroll n pixels down.
func (vm *CHIP_8) scrollDown(n byte) {
if vm.Pitch == 8 {
n >>= 1
}
// shift all the pixels down
copy(vm.Video[int(n)*vm.Pitch:], vm.Video[:])
// wipe the top-most pixels
for i := 0; i < int(n)*vm.Pitch; i++ {
vm.Video[i] = 0
}
}
// Scroll pixels right.
func (vm *CHIP_8) scrollRight() {
shift := uint(vm.Pitch >> 2)
for i := 0x3FF; i >= 0; i-- {
vm.Video[i] >>= shift
// get the lower bits from the previous byte
if i&(vm.Pitch-1) > 0 {
vm.Video[i] |= vm.Video[i-1] << (8 - shift)
}
}
}
// Scroll pixels left.
func (vm *CHIP_8) scrollLeft() {
shift := uint(vm.Pitch >> 2)
for i := 0; i < 0x400; i++ {
vm.Video[i] <<= shift
// get the upper bits from the next byte
if i&(vm.Pitch-1) < (vm.Pitch - 1) {
vm.Video[i] |= vm.Video[i+1] >> (8 - shift)
}
}
}
// Jump to address.
func (vm *CHIP_8) jump(address uint) {
vm.PC = address
}
// Jump to address + v0.
func (vm *CHIP_8) jumpV0(address uint) {
vm.PC = address + uint(vm.V[0])
}
// Skip next instruction if vx == n.
func (vm *CHIP_8) skipIf(x uint, b byte) {
if vm.V[x] == b {
vm.PC += 2
}
}
// Skip next instruction if vx != n.
func (vm *CHIP_8) skipIfNot(x uint, b byte) {
if vm.V[x] != b {
vm.PC += 2
}
}
// Skip next instruction if vx == vy.
func (vm *CHIP_8) skipIfXY(x, y uint) {
if vm.V[x] == vm.V[y] {
vm.PC += 2
}
}
// Skip next instruction if vx != vy.
func (vm *CHIP_8) skipIfNotXY(x, y uint) {
if vm.V[x] != vm.V[y] {
vm.PC += 2
}
}
// Skip next instruction if vx > vy.
func (vm *CHIP_8) skipIfGreater(x, y uint) {
if vm.V[x] > vm.V[y] {
vm.PC += 2
}
}
// Skip next instruction if vx < vy.
func (vm *CHIP_8) skipIfLess(x, y uint) {
if vm.V[x] < vm.V[y] {
vm.PC += 2
}
}
// Skip next instruction if key(vx) is pressed.
func (vm *CHIP_8) skipIfPressed(x uint) {
if vm.Keys[vm.V[x]] {
vm.PC += 2
}
}
// Skip next instruction if key(vx) is not pressed.
func (vm *CHIP_8) skipIfNotPressed(x uint) {
if !vm.Keys[vm.V[x]] {
vm.PC += 2
}
}
// Load n into vx.
func (vm *CHIP_8) loadX(x uint, b byte) {
vm.V[x] = b
}
// Load y into vx.
func (vm *CHIP_8) loadXY(x, y uint) {
vm.V[x] = vm.V[y]
}
// Load delay timer into vx.
func (vm *CHIP_8) loadXDT(x uint) {
vm.V[x] = vm.GetDelayTimer()
}
// Load vx into delay timer.
func (vm *CHIP_8) loadDTX(x uint) {
vm.DT = time.Now().UnixNano() + int64(vm.V[x])*1000000000/60
}
// Load vx into sound timer.
func (vm *CHIP_8) loadSTX(x uint) {
vm.ST = time.Now().UnixNano() + int64(vm.V[x])*1000000000/60
}
// Load vx with next key hit (blocking).
func (vm *CHIP_8) loadXK(x uint) {
vm.W = &vm.V[x]
}
// Load address register.
func (vm *CHIP_8) loadI(address uint) {
vm.I = address
}
// Load address with 8-bit, BCD of vx.
func (vm *CHIP_8) bcd(x uint) {
n := uint(vm.V[x])
b := uint(0)
// perform 8 shifts
for i := uint(0); i < 8; i++ {
if (b>>0)&0xF >= 5 {
b += 3
}
if (b>>4)&0xF >= 5 {
b += 3 << 4
}
if (b>>8)&0xF >= 5 {
b += 3 << 8
}
// apply shift, pull next bit
b = (b << 1) | (n >> (7 - i) & 1)
}
// write to memory
vm.Memory[vm.I+0] = byte(b>>8) & 0xF
vm.Memory[vm.I+1] = byte(b>>4) & 0xF
vm.Memory[vm.I+2] = byte(b>>0) & 0xF
}
// Load address with 16-bit, BCD of vx, vy.
func (vm *CHIP_8) bcd16(x, y uint) {
n := uint(vm.V[x])<<8 | uint(vm.V[y])
b := uint(0)
// perform 16 shifts
for i := uint(0); i < 16; i++ {
if (b>>0)&0xF >= 5 {
b += 3
}
if (b>>4)&0xF >= 5 {
b += 3 << 4
}
if (b>>8)&0xF >= 5 {
b += 3 << 8
}
if (b>>12)&0xF >= 5 {
b += 3 << 12
}
if (b>>16)&0xF >= 5 {
b += 3 << 16
}
// apply shift, pull next bit
b = (b << 1) | (n >> (15 - i) & 1)
}
// write to memory
vm.Memory[vm.I+0] = byte(b>>16) & 0xF
vm.Memory[vm.I+1] = byte(b>>12) & 0xF
vm.Memory[vm.I+2] = byte(b>>8) & 0xF
vm.Memory[vm.I+3] = byte(b>>4) & 0xF
vm.Memory[vm.I+4] = byte(b>>0) & 0xF
}
// Load font sprite for vx into I.
func (vm *CHIP_8) loadF(x uint) {
vm.I = uint(vm.V[x]) * 5
}
// Load high font sprite for vx into I.
func (vm *CHIP_8) loadHF(x uint) {
vm.I = 0x50 + uint(vm.V[x])*10
}
// Load ASCII font sprite for vx into I and length into v0.
func (vm *CHIP_8) loadASCII(x uint) {
c := 0x100 + int(vm.V[x])*3
// AB CD EF are the bytes in memory, but are unpacked as
// EF CD AB where E is the length and F-B are the rows
ab, cd, ef := vm.Memory[c], vm.Memory[c+1], vm.Memory[c+2]
// write the byte patters of each nibble to character memory
vm.Memory[0x1C0] = vm.Memory[0xF0+(ef&0xF)]
vm.Memory[0x1C1] = vm.Memory[0xF0+(cd>>4)]
vm.Memory[0x1C2] = vm.Memory[0xF0+(cd&0xF)]
vm.Memory[0x1C3] = vm.Memory[0xF0+(ab>>4)]
vm.Memory[0x1C4] = vm.Memory[0xF0+(ab&0xF)]
// set the length to v0
vm.V[0] = ef >> 4
// point I to where the ascii character was unpacked
vm.I = 0x1C0
}
// Bitwise or vx with vy into vx.
func (vm *CHIP_8) or(x, y uint) {
vm.V[x] |= vm.V[y]
}
// Bitwise and vx with vy into vx.
func (vm *CHIP_8) and(x, y uint) {
vm.V[x] &= vm.V[y]
}
// Bitwise xor vx with vy into vx.
func (vm *CHIP_8) xor(x, y uint) {
vm.V[x] ^= vm.V[y]
}
// Bitwise shift vx 1 bit, set carry to MSB of vx before shift.
func (vm *CHIP_8) shl(x uint) {
vm.V[0xF] = vm.V[x] >> 7
vm.V[x] <<= 1
}
// Bitwise shift vx 1 bit, set carry to LSB of vx before shift.
func (vm *CHIP_8) shr(x uint) {
vm.V[0xF] = vm.V[x] & 1
vm.V[x] >>= 1
}
// Add n to vx.
func (vm *CHIP_8) addX(x uint, b byte) {
vm.V[x] += b
}
// Add vy to vx and set carry.
func (vm *CHIP_8) addXY(x, y uint) {
vm.V[x] += vm.V[y]
if vm.V[x] < vm.V[y] {
vm.V[0xF] = 1
} else {
vm.V[0xF] = 0
}
}
// Add v to i.
func (vm *CHIP_8) addIX(x uint) {
vm.I += uint(vm.V[x])
if vm.I >= 0x1000 {
vm.V[0xF] = 1
} else {
vm.V[0xF] = 0
}
}
// Subtract vy from vx, set carry if no borrow.
func (vm *CHIP_8) subXY(x, y uint) {
if vm.V[x] >= vm.V[y] {
vm.V[0xF] = 1
} else {
vm.V[0xF] = 0
}
vm.V[x] -= vm.V[y]
}
// Subtract vx from vy and store in vx, set carry if no borrow.
func (vm *CHIP_8) subYX(x, y uint) {
if vm.V[y] >= vm.V[x] {
vm.V[0xF] = 1
} else {
vm.V[0xF] = 0
}
vm.V[x] = vm.V[y] - vm.V[x]
}
// Multiply vx and vy; vf contains the most significant byte.
func (vm *CHIP_8) mulXY(x, y uint) {
r := uint(vm.V[x]) * uint(vm.V[y])
// most significant byte to vf
vm.V[0xF], vm.V[x] = byte(r>>8&0xFF), byte(r&0xFF)
}
// Divide vx by vy; vf is set to the remainder.
func (vm *CHIP_8) divXY(x, y uint) {
vm.V[x], vm.V[0xF] = vm.V[x]/vm.V[y], vm.V[x]%vm.V[y]
}
// Load a random number & n into vx.
func (vm *CHIP_8) loadRandom(x uint, b byte) {
vm.V[x] = byte(rand.Intn(256) & int(b))
}
// Draw a sprite in memory to video at x,y with a height of n.
func (vm *CHIP_8) draw(a uint, x, y int8, n byte) byte {
c := byte(0)
// byte offset and bit index
b := uint(x >> 3)