-
Notifications
You must be signed in to change notification settings - Fork 0
/
chip8.go
284 lines (266 loc) · 6.21 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
package main
import (
"fmt"
"io"
"log"
"math/rand"
"os"
"strings"
"time"
)
const (
memSize = 0xFFF // fff == 4096
startOff = 0x200
stackOff = 0xFA0
)
type Chip struct {
mem [memSize]byte //0x000 to 0x1FF (511)
v [16]byte
i uint16
pc uint16
sp uint8
delay uint8
sound uint8
keyboard [16]byte
//display [64 * 32]byte // since display is 64*32
display *Display
stack [24]uint16 // for now let's not use the emulated memory
jmp bool // this flag is not part of the spec, used for iteration
}
type Display struct {
Buff [64 * 32]byte
}
func (d *Display) Clear() {
termops := []string{
"\x1b[2J", // clear the screen
"\x1b[H", // CUP - get the cursor UP (top left)
"\x1b[?25l", // display the cursor
}
for _, op := range termops {
fmt.Fprint(os.Stdout, op)
}
for i, _ := range d.Buff {
d.Buff[i] = 0
}
d.Draw()
}
func (d *Display) Draw() {
var vbuff strings.Builder
vbuff.Grow(3072)
for i := range d.Buff {
if i%64 == 0 && i != 0 {
vbuff.WriteString("\r\n")
}
if d.Buff[i] == 0 {
vbuff.WriteString(" ")
} else {
vbuff.WriteString("■")
}
}
fmt.Printf("\x1b[H\x1b[0J%s\r\n", vbuff.String())
}
func NewChip() *Chip {
var chip Chip
chip.pc = startOff
//chip.sp = stackOff
chip.loadFonts()
chip.display = &Display{}
return &chip
}
func (c *Chip) NextInstr() {
// if the `pc` was manually set
// don't increment by 2
if !c.jmp {
c.pc += 2
} else {
// reset value for next iteration
c.jmp = !c.jmp
}
}
func (c *Chip) SetJump(val uint16) {
c.jmp = true
c.pc = val
}
func (c *Chip) LoadROM(path string) error {
var err error
var rom *os.File
rom, err = os.Open(path)
if err != nil {
return err
}
b := make([]byte, 1)
for i := 0; i < memSize; i++ {
cur := i + startOff
_, err = rom.Read(b)
if err == io.EOF {
break
} else if err != nil {
return fmt.Errorf("error while loading rom: %w", err)
}
c.mem[cur] = b[0]
}
if err == nil {
return fmt.Errorf("ROM too large to fit in RAM")
}
return nil
}
func (c *Chip) loadFonts() {
fonts := [80]byte{
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
}
for i, x := range fonts {
c.mem[i] = x
}
}
func (c *Chip) Run() error {
var opcode uint16
rand.Seed(time.Now().UTC().UnixNano())
cnt := 0
for ; c.pc < memSize-1; c.NextInstr() {
opcode = uint16(c.mem[c.pc])<<8 | uint16(c.mem[c.pc+1])
log.Printf("\npc:%02x, mem:%02x, shift:%02x, opcode: %04x, cnt: %d\n",
c.pc, c.mem[c.pc], uint16(c.mem[c.pc])<<8, opcode, cnt)
cnt++
switch opcode & 0xF000 {
case 0x0000:
switch opcode & 0x00FF {
case 0x00E0:
c.display.Clear()
case 0x00EE:
c.SetJump(c.stack[c.sp])
c.sp--
default:
return fmt.Errorf("unknown operation: %04x", opcode)
}
case 0x1000: // jmp addr
c.SetJump(opcode & 0x0FFF)
case 0x2000: // call addr
c.sp++
c.stack[c.sp] = c.pc
c.SetJump(opcode & 0x0FFF)
case 0x3000: // SE Vx, byte
x := (opcode & 0x0F00) >> 8
kk := byte(opcode & 0x00FF)
if c.v[x] == kk { // @TODO: check if x == 'F' ?
c.pc += 2
}
case 0x4000: // SNE Vx, byte
x := (opcode & 0x0F00) >> 8
kk := byte(opcode & 0x00FF)
if c.v[x] != kk { // @TODO: check if x == 'F' ?
c.pc += 2
}
case 0x5000: // SE Vx, Vy
x := (opcode & 0x0F00) >> 8
y := (opcode & 0x00F0) >> 4
if c.v[x] == c.v[y] { // @TODO: check if x == 'F' ?
c.pc += 2
}
case 0x6000: // LD Vx, byte
x := (opcode & 0x0F00) >> 8
kk := byte(opcode & 0x00FF)
c.v[x] = kk
case 0x7000: // Add Vx, byte
x := (opcode & 0x0F00) >> 8
kk := byte(opcode & 0x00FF)
c.v[x] += kk
case 0x8000:
x := (opcode & 0x0F00) >> 8
y := (opcode & 0x00F0) >> 4
switch opcode & 0x000F {
case 0x0000: // LD Vx, Vy
c.v[x] = c.v[y]
case 0x0001:
c.v[x] |= c.v[y]
case 0x0002:
c.v[x] &= c.v[y]
case 0x0003:
c.v[x] ^= c.v[y]
case 0x0004:
// check if v[x] has enough space for v[y]
// set the carry flag if there's no space
if c.v[y] > (0xFF - c.v[x]) {
c.v[0xF] = 1
} else {
c.v[0xF] = 0
}
c.v[x] += c.v[y]
case 0x0005:
if c.v[y] > c.v[x] {
c.v[0xF] = 1
} else {
c.v[0xF] = 0
}
c.v[x] = c.v[x] - c.v[y]
case 0x0006:
c.v[0xF] = c.v[x] & 1
c.v[x] /= 2
case 0x0007:
c.v[0xF] = c.v[x] & 1
c.v[x] = c.v[y] - c.v[x]
case 0x000E:
c.v[0xF] = c.v[x] >> 8
c.v[x] *= 2
default:
return fmt.Errorf("unknown operation: %04x", opcode)
}
case 0x9000: // SNE Vx, Vy
x := (opcode & 0x0F00) >> 8
y := (opcode & 0x00F0) >> 4
if c.v[x] != c.v[y] { // @TODO: check if x == 'F' ?
c.pc += 2
}
case 0xA000: // LD I, addr
c.i = opcode & 0x0FFF
case 0xB000: // JP V0, addr
c.SetJump((opcode & 0x0FFF) + uint16(c.v[0]))
case 0xC000: // RND Vx, byte
x := (opcode & 0x0F00) >> 8
c.v[x] = uint8(rand.Intn(256)) & uint8((opcode&0x00FF)>>8)
case 0xD000: // DRW Vx, Vy, nibble
x := uint16(c.v[(opcode&0x0F00)>>8])
y := uint16(c.v[(opcode&0x00F0)>>4])
n := opcode & 0x000F
for yLine := uint16(0); yLine < n; yLine++ {
row := c.mem[c.i+yLine]
// now read each bit from row (byte) to get x coors
for xLine := uint16(0); xLine < 8; xLine++ {
pix := row & (0x80 >> xLine) // 0x80 == 0b10000000
if pix == 0 {
continue
}
pos := x + xLine + (y+yLine)*64 // dis[y+yLine][(x+xLine)]
if c.display.Buff[pos] == 1 {
c.v[0xF] = 1 // collision!
}
c.display.Buff[pos] ^= 1
}
}
c.display.Draw()
case 0xE000:
fmt.Println("Keypad not implemented")
case 0xF000:
fmt.Println("Not implemented")
default:
return fmt.Errorf("unknown operation: %04x", opcode)
}
// not sure if this needs to be here
time.Sleep(500 * time.Microsecond)
}
return nil
}