-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstructions.go
777 lines (702 loc) · 16.8 KB
/
instructions.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
package main
import (
"fmt"
"log"
"math/rand"
)
type instruction struct {
id string
asm string
x uint8
y uint8
b uint8
n uint8
addr uint16
}
func parseOpcode(op uint16) instruction {
// 00E0 - CLS
if op == 0x00E0 {
return instruction{
id: "CLS",
asm: "CLS",
}
}
// 00EE - RET
if op == 0x00EE {
return instruction{
id: "RET",
asm: "RET",
}
}
// 0nnn - SYS addr
if op&0xF000 == 0x0000 {
addr := op & 0x0FFF
return instruction{
id: "SYS addr",
asm: fmt.Sprintf("SYS %04X", addr),
addr: addr,
}
}
// 1nnn - JP addr
if op&0xF000 == 0x1000 {
addr := op & 0x0FFF
return instruction{
id: "JP addr",
asm: fmt.Sprintf("JP %04X", addr),
addr: addr,
}
}
// 2nnn - CALL addr
if op&0xF000 == 0x2000 {
addr := op & 0x0FFF
return instruction{
id: "CALL addr",
asm: fmt.Sprintf("CALL %04X", addr),
addr: addr,
}
}
// 3xkk - SE Vx, byte
if op&0xF000 == 0x3000 {
x := uint8((op & 0x0F00) >> 8)
b := uint8(op & 0x00FF)
return instruction{
id: "SE Vx, byte",
asm: fmt.Sprintf("SE V%01X, %02X", x, b),
x: x,
b: b,
}
}
// 4xkk - SNE Vx, byte
if op&0xF000 == 0x4000 {
x := uint8((op >> 8) & 0xF)
b := uint8(op & 0xFF)
return instruction{
id: "SNE Vx, byte",
asm: fmt.Sprintf("SNE V%01X, %02X", x, b),
x: x,
b: b,
}
}
// 5xy0 - SE Vx, Vy
if op&0xF000 == 0x5000 {
x := uint8((op >> 8) & 0xF)
y := uint8((op >> 4) & 0xF)
return instruction{
id: "SE Vx, Vy",
asm: fmt.Sprintf("SE V%01X, V%01X", x, y),
}
}
// 6xkk - LD Vx, byte
if op&0xF000 == 0x6000 {
x := uint8((op >> 8) & 0xF)
b := uint8(op & 0xFF)
return instruction{
id: "LD Vx, byte",
asm: fmt.Sprintf("LD V%01X, %02X", x, b),
x: x,
b: b,
}
}
// 7xkk - ADD Vx, byte
if op&0xF000 == 0x7000 {
x := uint8((op >> 8) & 0xF)
b := uint8(op & 0xFF)
return instruction{
id: "ADD Vx, byte",
asm: fmt.Sprintf("ADD V%01X, %02X", x, b),
x: x,
b: b,
}
}
// 8xy0 - LD Vx, Vy
if op&0xF00F == 0x8000 {
x := uint8((op >> 8) & 0xF)
y := uint8((op >> 4) & 0xF)
return instruction{
id: "LD Vx, Vy",
asm: fmt.Sprintf("LD V%01X, V%01X", x, y),
x: x,
y: y,
}
}
// 8xy1 - OR Vx, Vy
if op&0xF00F == 0x8001 {
x := uint8((op >> 8) & 0xF)
y := uint8((op >> 4) & 0xF)
return instruction{
id: "OR Vx, Vy",
asm: fmt.Sprintf("OR V%01X, V%01X", x, y),
x: x,
y: y,
}
}
// 8xy2 - AND Vx, Vy
if op&0xF00F == 0x8002 {
x := uint8((op >> 8) & 0xF)
y := uint8((op >> 4) & 0xF)
return instruction{
id: "AND Vx, Vy",
asm: fmt.Sprintf("AND V%01X, V%01X", x, y),
x: x,
y: y,
}
}
// 8xy3 - XOR Vx, Vy
if op&0xF00F == 0x8003 {
x := uint8((op >> 8) & 0xF)
y := uint8((op >> 4) & 0xF)
return instruction{
id: "XOR Vx, Vy",
asm: fmt.Sprintf("XOR V%01X, V%01X", x, y),
x: x,
y: y,
}
}
// 8xy4 - ADD Vx, Vy
if op&0xF00F == 0x8004 {
x := uint8((op >> 8) & 0xF)
y := uint8((op >> 4) & 0xF)
return instruction{
id: "ADD Vx, Vy",
asm: fmt.Sprintf("ADD V%01X, V%01X", x, y),
x: x,
y: y,
}
}
// 8xy5 - SUB Vx, Vy
if op&0xF00F == 0x8005 {
x := uint8((op >> 8) & 0xF)
y := uint8((op >> 4) & 0xF)
return instruction{
id: "SUB Vx, Vy",
asm: fmt.Sprintf("SUB V%01X, V%01X", x, y),
x: x,
y: y,
}
}
// 8xy6 - SHR Vx
if op&0xF00F == 0x8006 {
x := uint8((op >> 8) & 0xF)
y := uint8((op >> 4) & 0xF)
return instruction{
id: "SHR Vx {, Vy}",
asm: fmt.Sprintf("SUB V%01X, {, V%01X}", x, y),
x: x,
y: y,
}
}
// 8xy7 - SUBN Vx, Vy
if op&0xF00F == 0x8007 {
x := uint8((op >> 8) & 0xF)
y := uint8((op >> 4) & 0xF)
return instruction{
id: "SUBN Vx, Vy",
asm: fmt.Sprintf("SUBN V%01X, V%01X", x, y),
x: x,
y: y,
}
}
// 8xyE - SHL Vx{, Vy}
if op&0xF00F == 0x800E {
x := uint8((op >> 8) & 0xF)
y := uint8((op >> 4) & 0xF)
return instruction{
id: "SHL Vx {, Vy}",
asm: fmt.Sprintf("SHL V%01X {, V%01X}", x, y),
x: x,
y: y,
}
}
// 9xy0 - SNE Vx, Vy
if op&0xF000 == 0x9000 {
x := uint8((op >> 8) & 0xF)
y := uint8((op >> 4) & 0xF)
return instruction{
id: "SNE Vx, Vy",
asm: fmt.Sprintf("SNE V%01X, V%01X", x, y),
x: x,
y: y,
}
}
// Annn - LD I, addr
if op&0xF000 == 0xA000 {
addr := op & 0xFFF
return instruction{
id: "LD I, addr",
asm: fmt.Sprintf("LD I, %04X", addr),
addr: addr,
}
}
// Bnnn - JP V0, addr
if op&0xF000 == 0xA000 {
addr := op & 0xFFF
return instruction{
id: "JP V0, addr",
asm: fmt.Sprintf("JP V0, %04X", addr),
addr: addr,
}
}
// Cxkk - RND Vx, byte
if op&0xF000 == 0xC000 {
x := uint8((op >> 8) & 0xF)
b := uint8(op & 0xFF)
return instruction{
id: "RND Vx, byte",
asm: fmt.Sprintf("RND V%01X, %02X", x, b),
x: x,
b: b,
}
}
// Dxyn - DRW, Vx, Vy, nibble
if op&0xF000 == 0xD000 {
x := (op & 0x0F00) >> 8
y := (op & 0x00F0) >> 4
n := op & 0x000F
return instruction{
id: "DRW Vx, Vy, nibble",
asm: fmt.Sprintf("DRW V%01X, V%01X, %d", x, y, n),
x: uint8(x),
y: uint8(y),
n: uint8(n),
}
}
// Ex9E - SKP Vx
if op&0xF00F == 0xE00E {
x := uint8((op >> 8) & 0xF)
return instruction{
id: "SKP Vx",
asm: fmt.Sprintf("SKP V%01X", x),
x: x,
}
}
// ExA1 - SKNP Vx
if op&0xF00F == 0xE001 {
x := uint8((op >> 8) & 0xF)
return instruction{
id: "SKNP Vx",
asm: fmt.Sprintf("SKNP V%01X", x),
x: x,
}
}
// Fx07 - LD Vx, DT
if op&0xF0FF == 0xF007 {
x := uint8((op >> 8) & 0xF)
return instruction{
id: "LD Vx, DT",
asm: fmt.Sprintf("LD V%01X, DT", x),
x: x,
}
}
// Fx0A - LD Vx, K
if op&0xF0FF == 0xF00A {
x := uint8((op >> 8) & 0xF)
return instruction{
id: "LD Vx, K",
asm: fmt.Sprintf("LD V%01X, K", x),
x: x,
}
}
// Fx15 - LD DT, Vx
if op&0xF0FF == 0xF015 {
x := uint8((op >> 8) & 0xF)
return instruction{
id: "LD DT, Vx",
asm: fmt.Sprintf("LD DT, V%01X", x),
x: x,
}
}
// Fx18 - LD ST, Vx
if op&0xF0FF == 0xF018 {
x := uint8((op >> 8) & 0xF)
return instruction{
id: "LD ST, Vx",
asm: fmt.Sprintf("LD ST, V%01X", x),
x: x,
}
}
// Fx1E - ADD I, Vx
if op&0xF0FF == 0xF01E {
x := uint8((op >> 8) & 0xF)
return instruction{
id: "ADD I, Vx",
asm: fmt.Sprintf("ADD I, V%01X", x),
x: x,
}
}
// Fx29 - LD F, Vx
if op&0xF0FF == 0xF029 {
x := uint8((op >> 8) & 0xF)
return instruction{
id: "LD F, Vx",
asm: fmt.Sprintf("LD F, V%01X", x),
x: x,
}
}
// Fx33 - LD B, Vx
if op&0xF0FF == 0xF033 {
x := uint8((op >> 8) & 0xF)
return instruction{
id: "LD B, Vx",
asm: fmt.Sprintf("LD B, V%01X", x),
x: x,
}
}
// Fx55 - LD [I], Vx
if op&0xF0FF == 0xF055 {
x := uint8((op >> 8) & 0xF)
return instruction{
id: "LD [I], Vx",
asm: fmt.Sprintf("LD [I], V%01X", x),
x: x,
}
}
// Fx65 - LD Vx, [I]
if op&0xF0FF == 0xF065 {
x := uint8((op >> 8) & 0xF)
return instruction{
id: "LD Vx, [I]",
asm: fmt.Sprintf("LD V%01X, [I]", x),
x: x,
}
}
return instruction{}
}
func (c *chip8) print(format string, args ...any) {
return
log.Printf("\033[1;33m"+format+"\033[0m", args...)
log.Printf("| PC: %04X | I: %04X | RET: %03X | DT: %02X | ST: %02X | [I]: %02X", c.pc, c.i, c.stack[c.sp], c.dt, c.st, c.ram[c.i])
s := "|"
for i := 0; i < 8; i++ {
s += fmt.Sprintf(" V%01X: %02X |", i, c.v[i])
}
s += "\n|"
for i := 8; i <= 0xF; i++ {
s += fmt.Sprintf(" V%01X: %02X |", i, c.v[i])
}
log.Print(s)
}
// 0nnn - SYS addr
// Jump to a machine code routine at nnn.
//
// This instruction is only used on the old computers on which Chip-8
// was originally implemented. It is ignored by modern interpreters.
func (c *chip8) sysAddr(addr uint16) {
c.pc = addr
}
// 00E0 - CLS
// Clear the display.
func (c *chip8) cls() {
for x := range c.screen {
for y := range c.screen[x] {
c.screen[x][y] = false
}
}
}
// 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.
func (c *chip8) ret() {
c.pc = c.stack[c.sp]
c.sp--
}
// 1nnn - JP addr
// Jump to location nnn.
//
// The interpreter sets the program counter to nnn.
func (c *chip8) jpAddr(addr uint16) {
c.pc = addr
}
// 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.
func (c *chip8) callAddr(addr uint16) {
c.sp++
c.stack[c.sp] = c.pc
c.pc = addr
}
// 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.
func (c *chip8) seVxB(x, b uint8) {
if c.v[x] == b {
c.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.
func (c *chip8) sneVxB(x, b uint8) {
if c.v[x] != b {
c.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.
func (c *chip8) seVxVy(x, y uint8) {
if c.v[x] == c.v[y] {
c.pc += 2
}
}
// 6xkk - LD Vx, byte
// Set Vx = kk.
//
// The interpreter puts the value kk into register Vx.
func (c *chip8) ldVxB(x, b uint8) {
c.v[x] = b
}
//
//
// 7xkk - ADD Vx, byte
// Set Vx = Vx + kk.
//
// Adds the value kk to the value of register Vx, then stores the result in Vx.
func (c *chip8) addVxB(x, b uint8) {
c.v[x] += b
}
// 8xy0 - LD Vx, Vy
// Set Vx = Vy.
//
// Stores the value of register Vy in register Vx.
func (c *chip8) ldVxVy(x, y uint8) {
c.v[x] = c.v[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.
func (c *chip8) orVxVy(x, y uint8) {
c.v[x] |= c.v[y]
}
// 8xy2 - AND Vx, Vy
// Set Vx = Vx AND Vy.
//
// Performs a bitwise AND on the values of Vx andVxVy Vy, then stores the result in Vx. A bitwise AND compares the corrseponding bits from two values, andVxVy if both bits are 1, then the same bit in the result is also 1. Otherwise, it is 0.
func (c *chip8) andVxVy(x, y uint8) {
c.v[x] &= c.v[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.
func (c *chip8) xorVxVy(x, y uint8) {
c.v[x] ^= c.v[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.
func (c *chip8) addVxVy(x, y uint8) {
vx := c.v[x] + y
c.v[0xF] = 0
if vx < c.v[x] {
c.v[0xF] = 1
}
c.v[x] = vx
}
// 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.
func (c *chip8) subVxVy(x, y uint8) {
c.v[0xF] = 0
if c.v[x] > c.v[y] {
c.v[0xF] = 1
}
c.v[x] -= c.v[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.
func (c *chip8) shrVx(x uint8) {
c.v[0xF] = 0
if c.v[x]&1 == 1 {
c.v[0xF] = 1
}
c.v[x] = c.v[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.
func (c *chip8) subnVxVy(x, y uint8) {
c.v[0xF] = 0
if c.v[y] > c.v[x] {
c.v[0xF] = 1
}
c.v[x] = c.v[y] - c.v[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.
func (c *chip8) shlVx(x uint8) {
c.v[0xF] = 0
if c.v[x]&0x80 == 1 {
c.v[0xF] = 1
}
c.v[x] = c.v[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.
func (c *chip8) sneVxVy(x, y uint8) {
if c.v[x] != c.v[y] {
c.pc += 2
}
}
// Annn - LD I, addr
// Set I = nnn.
//
// The value of register I is set to nnn.
func (c *chip8) ldIAddr(addr uint16) {
c.i = addr
}
// Bnnn - JP V0, addr
// Jump to location nnn + V0.
//
// The program counter is set to nnn plus the value of V0.
func (c *chip8) jpV0Addr(addr uint16) {
c.pc = addr + uint16(c.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.
func (c *chip8) rndVxB(x, b uint8) {
c.v[x] = uint8(rand.Uint32()%256) & b
}
// 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.
func (c *chip8) drwVxVyN(x, y, n uint8) {
c.v[0xF] = 0
for i := uint8(0); i < n; i++ {
lin := c.v[y] + i
b := c.ram[c.i+uint16(i)]
for j := uint8(0); j < 8; j++ {
col := c.v[x] + j
set := b>>(7-j)&1 == 1
if !set {
continue
}
col %= 64
lin %= 32
if c.screen[lin][col] {
c.v[0xF] = 1
set = false
}
c.screen[lin][col] = set
}
}
}
// 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.
func (c *chip8) skpVx(x uint8) {
if c.isKeyDown(c.v[x]) {
c.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.
func (c *chip8) sknpVx(x uint8) {
if !c.isKeyDown(c.v[x]) {
c.pc += 2
}
}
// Fx07 - LD Vx, DT
// Set Vx = delay timer value.
//
// The value of DT is placed into Vx.
func (c *chip8) ldVxDT(x uint8) {
c.v[x] = c.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.
func (c *chip8) ldVxK(x uint8) {
c.waitKey()
var k uint8
c.v[x] = k
}
// Fx15 - LD DT, Vx
// Set delay timer = Vx.
//
// DT is set equal to the value of Vx.
func (c *chip8) ldDTVx(x uint8) {
c.dt = c.v[x]
}
// Fx18 - LD ST, Vx
// Set sound timer = Vx.
//
// ST is set equal to the value of Vx.
func (c *chip8) ldSTVx(x uint8) {
c.st = c.v[x]
}
// Fx1E - ADD I, Vx
// Set I = I + Vx.
//
// The values of I and Vx are added, and the results are stored in I.
func (c *chip8) addIVx(x uint8) {
c.i += uint16(c.v[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.
func (c *chip8) ldFVx(x uint8) {
c.i = uint16(c.v[x] * 5)
}
// 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.
func (c *chip8) ldBVx(x uint8) {
c.ram[c.i] = c.v[x] / 100
c.ram[c.i+1] = c.v[x] % 100 / 10
c.ram[c.i+2] = c.v[x] % 10
// panic("todo")
}
// 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.
func (c *chip8) ldIVx(x uint8) {
for i := uint8(0); i <= x; i++ {
c.ram[c.i+uint16(i)] = c.v[x]
}
}
// 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.
func (c *chip8) ldVxI(x uint8) {
for i := uint8(0); i <= x; i++ {
c.v[i] = c.ram[c.i+uint16(i)]
}
}