-
Notifications
You must be signed in to change notification settings - Fork 3
/
cpu.go
319 lines (306 loc) · 7.58 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
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
package main
import (
"io"
"math/rand"
"os"
"time"
)
const (
height = byte(0x20)
width = byte(0x40)
)
type cpu struct {
pc uint16 // program counter
memory [4096]byte // 4k memory
stack [16]uint16 // 16 level stack
sp uint16 // stack pointer
V [16]byte // 16 registers
I uint16 // The address register
delayTimer byte // The delay timer counts down at 60hz
soundTimer byte //sound timer counts down at 60hz
display [height][width]byte // 2d array representing 64x32 grid
keys [16]byte // state of the keys
draw bool // to draw or not
inputflag bool // stop everything wait for input
inputRegister byte // Stre value of input
}
var fontset = [...]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
}
func NewCpu() cpu {
c := cpu{pc: 0x200}
c.LoadFontSet()
return c
}
func (c *cpu) LoadFontSet() {
for i := 0x00; i < 0x50; i++ {
c.memory[i] = fontset[i]
}
}
func (c *cpu) ClearDisplay() {
for x := 0x00; x < 0x20; x++ {
for y := 0x00; y < 0x40; y++ {
c.display[x][y] = 0x00
}
}
}
func (c *cpu) LoadProgram(rom string) int {
f, err := os.Open(rom)
if err != nil {
panic(err)
}
defer f.Close()
memory := make([]byte, 3584)
n, err := f.Read(memory)
if err != nil {
if err != io.EOF {
panic(err)
}
}
for index, b := range memory {
c.memory[index+0x200] = b
}
return n
}
func (c *cpu) Reset() {
c.pc = 0x200
c.delayTimer = 0
c.soundTimer = 0
c.I = 0
c.sp = 0
for i := 0; i < len(c.memory); i++ {
c.memory[i] = 0
}
for i := 0; i < len(c.stack); i++ {
c.stack[i] = 0
}
for i := 0; i < len(c.V); i++ {
c.V[i] = 0
}
for i := 0; i < len(c.keys); i++ {
c.keys[i] = 0
}
c.LoadFontSet()
c.ClearDisplay()
}
func (c *cpu) Run() {
c.RunCpuCycle()
if c.delayTimer > 0 {
c.delayTimer = c.delayTimer - 1
}
if c.soundTimer > 0 {
c.soundTimer = c.soundTimer - 1
}
}
func (c *cpu) RunCpuCycle() {
opcode := uint16(c.memory[c.pc])<<8 | uint16(c.memory[c.pc+1])
c.pc = c.pc + 2
switch opcode & 0xF000 {
case 0x0000:
switch opcode & 0x000F {
case 0x0000:
c.ClearDisplay()
case 0x000E:
c.pc = c.stack[c.sp-1]
c.sp = c.sp - 1
}
case 0x1000:
c.pc = opcode & 0x0FFF
case 0x2000:
c.stack[c.sp] = c.pc
c.sp = c.sp + 1
c.pc = opcode & 0x0FFF
case 0x3000:
compareTo := byte(opcode & 0x00FF)
register := (opcode & 0x0F00) >> 8
if c.V[register] == compareTo {
c.pc = c.pc + 2
}
case 0x4000:
compareTo := byte(opcode & 0x00FF)
register := (opcode & 0x0F00) >> 8
if c.V[register] != compareTo {
c.pc = c.pc + 2
}
case 0x5000:
registerX := (opcode & 0x0F00) >> 8
registerY := (opcode & 0x00F0) >> 4
if c.V[registerX] == c.V[registerY] {
c.pc = c.pc + 2
}
case 0x6000:
register := byte((opcode & 0x0F00) >> 8)
c.V[register] = byte(opcode & 0x00FF)
case 0x7000:
register := byte((opcode & 0x0F00) >> 8)
value := byte(opcode & 0x00FF)
c.V[register] = c.V[register] + value
case 0x8000:
switch opcode & 0x000F {
case 0x0000:
registerX := (opcode & 0x0F00) >> 8
registerY := (opcode & 0x00F0) >> 4
c.V[registerX] = c.V[registerY]
case 0x0001:
registerX := (opcode & 0x0F00) >> 8
registerY := (opcode & 0x00F0) >> 4
c.V[registerX] = c.V[registerX] | c.V[registerY]
case 0x0002:
registerX := (opcode & 0x0F00) >> 8
registerY := (opcode & 0x00F0) >> 4
c.V[registerX] = c.V[registerX] & c.V[registerY]
case 0x0003:
registerX := (opcode & 0x0F00) >> 8
registerY := (opcode & 0x00F0) >> 4
c.V[registerX] = c.V[registerX] ^ c.V[registerY]
case 0x0004:
registerX := byte((opcode & 0x0F00) >> 8)
registerY := byte((opcode & 0x00F0) >> 4)
c.V[registerX] = c.V[registerX] + c.V[registerY]
if uint16(c.V[registerX])+uint16(c.V[registerY]) > 0xFF {
c.V[0xF] = 1
} else {
c.V[0xF] = 0
}
case 0x0005:
registerX := (opcode & 0x0F00) >> 8
registerY := (opcode & 0x00F0) >> 4
if c.V[registerX] > c.V[registerY] {
c.V[0xF] = 1
} else {
c.V[0xF] = 0
}
c.V[registerX] = c.V[registerX] - c.V[registerY]
case 0x0006:
registerX := (opcode & 0x0F00) >> 8
if c.V[registerX]&0x1 == 1 {
c.V[0xF] = 1
} else {
c.V[0xF] = 0
}
c.V[registerX] = c.V[registerX] >> 1
case 0x0007:
registerX := (opcode & 0x0F00) >> 8
registerY := (opcode & 0x00F0) >> 4
if c.V[registerY] > c.V[registerX] {
c.V[0xF] = 1
} else {
c.V[0xF] = 0
}
c.V[registerX] = c.V[registerY] - c.V[registerX]
case 0x000E:
registerX := (opcode & 0x0F00) >> 8
if c.V[registerX]&0x80 == 0x80 {
c.V[0xF] = 1
} else {
c.V[0xF] = 0
}
c.V[registerX] = c.V[registerX] << 1
}
case 0x9000:
registerX := (opcode & 0x0F00) >> 8
registerY := (opcode & 0x00F0) >> 4
if c.V[registerX] != c.V[registerY] {
c.pc = c.pc + 2
}
case 0xA000:
c.I = (opcode & 0x0FFF)
case 0xB000:
c.pc = (opcode & 0x0FFF) + uint16(c.V[0x0])
case 0xC000:
registerX := (opcode & 0x0F00) >> 8
value := byte(opcode & 0x00FF)
rand.Seed(time.Now().Unix())
c.V[registerX] = byte(rand.Intn(256)) & value
case 0xD000:
registerX := (opcode & 0x0F00) >> 8
registerY := (opcode & 0x00F0) >> 4
nibble := byte(opcode & 0x000F)
x := c.V[registerX]
y := c.V[registerY]
c.V[0xF] = 0x00
for i := y; i < y+nibble; i++ {
for j := x; j < x+8; j++ {
bit := (c.memory[c.I+uint16(i-y)] >> (7 - j + x)) & 0x01
xIndex, yIndex := j, i
if j >= width {
xIndex = j - width
}
if i >= height {
yIndex = i - height
}
if bit == 0x01 && c.display[yIndex][xIndex] == 0x01 {
c.V[0xF] = 0x01
}
c.display[yIndex][xIndex] = c.display[yIndex][xIndex] ^ bit
}
}
c.draw = true
case 0xE000:
switch opcode & 0x00FF {
case 0x009E:
register := (opcode & 0x0F00) >> 8
if c.keys[c.V[register]] == 0x01 {
c.pc = c.pc + 2
}
case 0x00A1:
register := (opcode & 0x0F00) >> 8
if c.keys[c.V[register]] == 0x00 {
c.pc = c.pc + 2
}
}
case 0xF000:
switch opcode & 0x00FF {
case 0x007:
register := (opcode & 0x0F00) >> 8
c.V[register] = c.delayTimer
case 0x0015:
register := (opcode & 0x0F00) >> 8
c.delayTimer = c.V[register]
case 0x0018:
register := (opcode & 0x0F00) >> 8
c.soundTimer = c.V[register]
case 0x000A:
register := (opcode & 0x0F00) >> 8
c.inputflag = true
c.inputRegister = byte(register)
case 0x001E:
register := (opcode & 0x0F00) >> 8
c.I = c.I + uint16(c.V[register])
case 0x0029:
register := (opcode & 0x0F00) >> 8
c.I = uint16(c.V[register] * 0x5)
case 0x0033:
register := (opcode & 0x0F00) >> 8
number := c.V[register]
c.memory[c.I] = (number / 100) % 10
c.memory[c.I+1] = (number / 10) % 10
c.memory[c.I+2] = number % 10
case 0x0055:
register := (opcode & 0x0F00) >> 8
for i := uint16(0x00); i <= register; i++ {
c.memory[c.I+i] = c.V[i]
}
case 0x0065:
register := (opcode & 0x0F00) >> 8
for i := uint16(0x00); i <= register; i++ {
c.V[i] = c.memory[c.I+i]
}
}
}
}