-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstructions.go
332 lines (273 loc) · 8.25 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
package chip8
import (
"math/rand"
)
//I00E0 clears the myMonitor
func (c8 *Chip8) I00E0() { //CLS
c8.frameBuffer = [64 * 32]byte{}
c8.MustDraw = true
}
//I00EE returns from a subroutine
func (c8 *Chip8) I00EE() { //RET
c8.sp--
c8.pc = c8.stack[c8.sp]
}
//I1NNN Jumps to location nnn
func (c8 *Chip8) I1NNN() { //JP (ADDR)
addr := c8.cOpcode.NNN()
c8.pc = addr
}
// I2NNN CALL (ADDR)
func (c8 *Chip8) I2NNN() {
addr := c8.cOpcode.NNN()
c8.stack[c8.sp] = c8.pc
c8.sp++
c8.pc = addr
}
//I3XKK
//Skip next instruction if Vx = kk
func (c8 *Chip8) I3XKK() { //SE (VX, BYTE)
vx := c8.registers[c8.cOpcode.X()]
_byte := c8.cOpcode.KK()
if vx == _byte {
c8.pc += 2
}
}
//I4XKK
//Skip next instruction if Vx != kk
func (c8 *Chip8) I4XKK() { //SNE (VX, BYTE)
vx := c8.registers[c8.cOpcode.X()]
_byte := c8.cOpcode.KK()
if vx != _byte {
c8.pc += 2
}
}
//I5XY0
//Skip next instruction if vx = vy
func (c8 *Chip8) I5XY0() { //SE (VX, VY)
vx := c8.registers[c8.cOpcode.X()]
vy := c8.registers[c8.cOpcode.Y()]
if vx == vy {
c8.pc += 2
}
}
//I6XKK Set vx = kk
func (c8 *Chip8) I6XKK() { //LV (VX, BYTE)
_byte := c8.cOpcode.KK()
c8.registers[c8.cOpcode.X()] = _byte
}
//I7XKK ADD (VX, BYTE)
func (c8 *Chip8) I7XKK() {
_byte := c8.cOpcode.KK()
c8.registers[c8.cOpcode.X()] += _byte
}
//I8XY0 LD (VX, VY) Set Vx = Vy
func (c8 *Chip8) I8XY0() {
c8.registers[c8.cOpcode.X()] = c8.registers[c8.cOpcode.Y()]
}
//I8XY1 OR(VX, VY)
func (c8 *Chip8) I8XY1() {
c8.registers[c8.cOpcode.X()] |= c8.registers[c8.cOpcode.Y()]
}
//I8XY2 AND (VX, VY)
func (c8 *Chip8) I8XY2() {
c8.registers[c8.cOpcode.X()] &= c8.registers[c8.cOpcode.Y()]
}
//I8XY3 XOR (VX, VY)
func (c8 *Chip8) I8XY3() {
x := c8.cOpcode.X()
y := c8.cOpcode.Y()
c8.registers[x] ^= c8.registers[y]
}
//I8XY4
//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 (c8 *Chip8) I8XY4() { //ADD (VS, VY)
sum := c8.registers[c8.cOpcode.X()] + c8.registers[c8.cOpcode.Y()]
c8.registers[c8.cOpcode.X()] = sum & 0x00FF
if sum > 255 {
c8.registers[0xF] = 1
} else {
c8.registers[0xF] = 0
}
}
//f Vx > Vy, then VF is set to 1, otherwise 0. Then Vy is subtracted from Vx, and the results stored in Vx.
func (c8 *Chip8) I8XY5() { //SUB (VX, VY)
x := c8.cOpcode.X()
y := c8.cOpcode.Y()
if c8.registers[x] > c8.registers[y] {
c8.registers[0xF] = 1
} else {
c8.registers[0xF] = 0
}
c8.registers[c8.cOpcode.X()] -= c8.registers[c8.cOpcode.Y()]
}
//I8XY6
//If the least-significant bit of Vx is 1, then VF is set to 1, otherwise 0. Then Vx is divided by 2.
func (c8 *Chip8) I8XY6() { //SHR (VX, {, VY})
c8.registers[0xF] = c8.registers[c8.cOpcode.X()] & 0x1 // 0x1: 00000001
c8.registers[c8.cOpcode.X()] >>= 1
}
//I8XY7
//If Vy > Vx, then VF is set to 1, otherwise 0. Then Vx is subtracted from Vy, and the results stored in Vx.
func (c8 *Chip8) I8XY7() { //SUBN (VX, VY)
if c8.registers[c8.cOpcode.Y()] > c8.registers[c8.cOpcode.X()] {
c8.registers[0xF] = 1
} else {
c8.registers[0xF] = 0
}
c8.registers[c8.cOpcode.X()] = c8.registers[c8.cOpcode.Y()] - c8.registers[c8.cOpcode.X()]
}
//I8XYE
//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 (c8 *Chip8) I8XYE() { //SHL (Vx {, Vy})
c8.registers[0xF] = c8.registers[c8.cOpcode.X()] & 0x80 //0x80: 10000000
c8.registers[c8.cOpcode.X()] <<= 1
}
//I9XY0
//Skip next instruction if Vx != Vy.
func (c8 *Chip8) I9XY0() { //SNE (Vx, Vy)
if c8.registers[c8.cOpcode.X()] != c8.registers[c8.cOpcode.Y()] {
c8.pc += 2
}
}
//IANNN Set I = NNN
func (c8 *Chip8) IANNN() { // LD I, addr
c8.i = c8.cOpcode.NNN()
}
//IBNNN Jump to location nnn + V0.
func (c8 *Chip8) IBNNN() { // JP V0, addr
c8.pc = uint16(c8.registers[0]) + c8.cOpcode.NNN()
}
//ICXKK Set Vx = random byte AND kk.
func (c8 *Chip8) ICXKK() { // RND Vx, byte
c8.registers[c8.cOpcode.X()] = uint8(rand.Intn(256)) & c8.cOpcode.KK()
}
//IDXYN displays n-byte sprite starting at memory location I at (Vx, Vy) and set VF = collision.
func (c8 *Chip8) IDXYN() { // DRW (Vx, Vy, hSprite)
vx := c8.registers[c8.cOpcode.X()]
vy := c8.registers[c8.cOpcode.Y()]
//If a sprite is attempting to draw outside the bounds of the screen,
//it wraps around to the other side, that's why we do x0 = vx % width, y0 = vy % height.
x0 := int(vx % WidthScreen)
y0 := int(vy % HeightScreen)
//Each sprite has a width of 8 pixels (represented by a byte), and a height N
hSprite := int(c8.cOpcode.N())
i := int(c8.i)
var _byte byte
var bit byte
c8.registers[0xF] = 0
for y := 0; y < hSprite; y++ {
_byte = c8.memory[i+y]
//Every bit of each i-byte (0<i<N) of the sprite represents a pixel on the screen which can be ON or OFF.
//if the sprite pixel is ON, then we check if that pixel is already ON in the FrameBuffer. In that case we set Vf = 1 to indicate a collision
//then we use a XOR operation, so if the sprite pixel is ON and the display pixel is ON, we set the display pixel to OFF
//and if the sprite pixel is ON and the display pixel is OFF, we set the display pixel to ON
for x := 0; x < 8; x++ {
bit = _byte & (0x80 >> x)
if bit != 0 {
if c8.frameBuffer.CheckOverlap(x0+x, y0+y) {
continue
} else {
cellFrameBuffer := c8.frameBuffer.Get(x0+x, y0+y)
if *cellFrameBuffer == 1 {
c8.registers[0xF] = 1
}
*cellFrameBuffer ^= 1
}
}
}
}
c8.MustDraw = true
}
//IEX9E Skip next instruction if key with the value of Vx is pressed.
func (c8 *Chip8) IEX9E() { //SKP(VX)
key := c8.registers[c8.cOpcode.X()]
select {
case keyPressed := <-c8.keyPressed:
if keyPressed == key {
c8.pc += 2
}
return
default:
return
}
}
//IEXA1 Skip next instruction if key with the value of Vx is not pressed.
func (c8 *Chip8) IEXA1() { //SKP(VX)
key := c8.registers[c8.cOpcode.X()]
select {
case keyPressed := <-c8.keyPressed:
if keyPressed == key {
return
}
c8.pc += 2
default:
c8.pc += 2
}
}
//IFX07 Set Vx = delay timer value.
func (c8 *Chip8) IFX07() { //LD (Vx, DT)
c8.registers[c8.cOpcode.X()] = c8.delayTimer
}
//IFX0A Wait for a key press, store the value of the key in Vx.
func (c8 *Chip8) IFX0A() { //LD (Vx, K)
for {
select {
case key := <-c8.keyPressed:
if key <= NumberOfKeys || key == AsciiEscape {
c8.registers[c8.cOpcode.X()] = key
return
}
}
}
}
//IFX15 Set delay timer = Vx
func (c8 *Chip8) IFX15() { //LD (DT, Vx)
c8.delayTimer = c8.registers[c8.cOpcode.X()]
}
//IFX18 Set sound timer = Vx
func (c8 *Chip8) IFX18() { //LD (ST, Vx)
c8.soundTimer = c8.registers[c8.cOpcode.X()]
}
//IFX1E Set I = I + Vx
func (c8 *Chip8) IFX1E() { //ADD (I, Vx)
c8.i += uint16(c8.registers[c8.cOpcode.X()])
}
//IFX29 Set I = location of sprite for digit Vx.
func (c8 *Chip8) IFX29() { //LD (F, Vx)
vx := c8.registers[c8.cOpcode.X()] // c between 0 and F
c8.i = FontsetStartAddress + uint16(FontSize*vx)
}
//IFX33 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 (c8 *Chip8) IFX33() { //LD (B, Vx)
vx := c8.registers[c8.cOpcode.X()]
c8.memory[c8.i+2] = vx % 10
c8.memory[c8.i+1] = (vx / 10) % 10
c8.memory[c8.i] = (vx / 100) % 10
}
//IFX55 Stores registers V0 through Vx in memory starting at location I.
func (c8 *Chip8) IFX55() { //LD (I,Vx)
for k := 0; k <= int(c8.cOpcode.X()); k++ {
c8.memory[c8.i+uint16(k)] = c8.registers[k]
}
}
//IFX65 Reads registers V0 through Vx from memory starting at location I.
func (c8 *Chip8) IFX65() { //LD (Vx, I)
for k := 0; k <= int(c8.cOpcode.X()); k++ {
c8.registers[k] = c8.memory[c8.i+uint16(k)]
}
}
//I9XY1 save vx in the first 8 bits of i and vy in the last 8.
//This instruction is part of our extended instruction set, required for the c8-compiler
func (c8 *Chip8) I9XY1() {
c8.i = uint16(c8.registers[c8.cOpcode.X()])<<8 | uint16(c8.registers[c8.cOpcode.Y()])
}
//I9XY2 save the first 8 bits of i in vx, and the last 8 bits in vy
//This instruction is part of our extended instruction set, required for the c8-compiler
func (c8 *Chip8) I9XY2() {
c8.registers[c8.cOpcode.X()] = byte(c8.i >> 8)
c8.registers[c8.cOpcode.Y()] = byte(c8.i)
}