-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu.go
84 lines (73 loc) · 1.54 KB
/
cpu.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
package main
import (
rl "github.com/gen2brain/raylib-go/raylib"
)
func (e *emulator) fetch() (b0, b1 uint8) {
b0 = e.memory[e.pc]
b1 = e.memory[e.pc+1]
e.pc += 2
return
}
func (e *emulator) decode() (inst, X, Y, N, NN uint8, NNN uint16) {
b0, b1 := e.fetch()
inst = (b0 & 0xF0) >> 4
X = b0 & 0x0F
Y = (b1 & 0xF0) >> 4
N = b1 & 0x0F
NN = b1
NNN = uint16(X)<<8 | uint16(NN)
return
}
func (e *emulator) execute() {
inst, X, Y, N, NN, NNN := e.decode()
switch inst {
case 0x0:
switch Y {
case 0xE:
switch N {
case 0x0:
e.screen = [64][32]int{}
case 0xE:
// Ommited
}
}
case 0x1:
e.pc = NNN
case 0x6:
e.registers[X] = NN
case 0x7:
e.registers[X] += NN
case 0xA:
e.I = NNN
case 0xD:
e.drawSprite(e.registers[X], e.registers[Y], N)
}
}
func (e *emulator) draw(screenImage *rl.Image) {
for x, row := range e.screen {
for y, value := range row {
if value > 0 {
rl.ImageDrawPixel(screenImage, int32(x), int32(y), rl.White)
}
}
}
}
func (e *emulator) drawSprite(VX, VY, N uint8) {
x := VX % uint8(Settings.width)
y := VY % uint8(Settings.height)
e.registers[0xF] = 0
for row := uint8(0); row < N; row++ {
spriteByte := e.memory[e.I+uint16(row)]
for col := uint8(0); col < 8; col++ {
if spriteByte&(0x80>>col) != 0 { // Check if the pixel in the sprite is set
pixelX := (x + col) % uint8(Settings.width)
pixelY := (y + row) % uint8(Settings.height)
if e.screen[pixelX][pixelY] == 1 {
e.registers[0xF] = 1
}
e.screen[pixelX][pixelY] ^= 1
}
}
}
e.draw(screenImage)
}