-
Notifications
You must be signed in to change notification settings - Fork 0
/
chip8.cpp
346 lines (332 loc) · 10.6 KB
/
chip8.cpp
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
//
// Created by Jessica on 5/26/2020.
//
#include "chip8.h"
void chip8::initialize() {
s.pc = 512;
s.opcode = 0;
s.I = 0;
s.sp = 0;
//clear memory
for (int i = 0; i < 4096; i++) {
s.memory[i] = 0;
}
clearScreen();
//clear V registers and stack
for (int i = 0; i < 16; i++) {
s.V[i] = 0;
s.stack[i] = 0;
}
//clear input
for (int i = 0; i < 16; i++) {
s.key[i] = 0;
}
//load the font set
for (int i = 0; i < 80; i++) {
s.memory[i] = fontset[i];
}
//seed randomness
srand(time(NULL));
isRunning = true;
drawFlag = true;
}
void chip8::loadGame(const unsigned char *data, int size) {
for (int i = 0; i < size; i++) {
//chip8 reserves the first 512 bytes for the interpreter
s.memory[i + 0x200] = data[i];
}
}
void chip8::emulateCycle() {
//op code is 2 bytes, load both bytes from memory
s.opcode = s.memory[s.pc] << 8 | s.memory[s.pc + 1];
drawFlag = false;
switch ((s.opcode & 0xF000) >> 12) {
case 0x0: {
if (s.opcode == 0x00E0) {
drawFlag = true;
clearScreen();
s.pc += 2;
} else if (s.opcode == 0x00EE) {
//return from function call
s.sp--;
s.pc = s.stack[s.sp] + 2;
//s.stack[s.sp] = 0;
} else {
printf("Not implemented.\n");
}
break;
}
case 0x1: {
//jump to address
s.pc = s.opcode & 0x0FFF;
break;
}
case 0x2: {
//call function
s.stack[s.sp] = s.pc;
s.sp++;
s.pc = s.opcode & 0x0FFF;
break;
}
case 0x3: {
//skip next instruction if VX == val
unsigned char val = s.opcode & 0x00FF;
unsigned char v = (s.opcode & 0x0F00) >> 8;
if (s.V[v] == val) {
s.pc += 4; // skip the next instruction
} else {
s.pc += 2;
}
break;
}
case 0x4: {
//skip next instruction if VX != val
unsigned char val = s.opcode & 0x00FF;
unsigned char v = (s.opcode & 0x0F00) >> 8;
if (s.V[v] != val) s.pc += 4; // skip the next instruction
else s.pc += 2;
break;
}
case 0x5: {
//skip next instruction if VX == VY
unsigned char v1 = (s.opcode & 0x0F00) >> 8;
unsigned char v2 = (s.opcode & 0x00F0) >> 4;
if (s.V[v1] == s.V[v2]) s.pc += 4;
else s.pc += 2;
break;
}
case 0x6: {
//set VX to val
unsigned char v = (s.opcode & 0x0F00) >> 8;
unsigned char val = s.opcode & 0x00FF;
s.V[v] = val;
s.pc += 2;
break;
}
case 0x7: {
//add val to VX
unsigned char v = (s.opcode & 0x0F00) >> 8;
unsigned char val = s.opcode & 0x00FF;
s.V[v] += val;
s.pc += 2;
break;
}
case 0x8: {
unsigned char v1 = (s.opcode & 0x0F00) >> 8;
unsigned char v2 = (s.opcode & 0x00F0) >> 4;
switch (s.opcode & 0x000F) {
case 0x0:
//set VX to VY
s.V[v1] = s.V[v2];
break;
case 0x1:
//set VX to VX OR VY
s.V[v1] = s.V[v1] | s.V[v2];
break;
case 0x2:
//set VX to VX AND VY
s.V[v1] = s.V[v1] & s.V[v2];
break;
case 0x3:
//set VX to VX XOR VY
s.V[v1] = s.V[v1] ^ s.V[v2];
break;
case 0x4:
//add VY to VX
if (s.V[v2] > (0xFF - s.V[v1]))
s.V[0xF] = 1; //if v1+v2 would result in a number greater than 255, set carry bit
else s.V[0xF] = 0;
s.V[v1] += s.V[v2];
break;
case 0x5:
//subtract VY from VX
if (s.V[v2] > s.V[v1]) s.V[0xF] = 0; //if v1-v2 would result in a number less than 0, set carry bit
else s.V[0xF] = 1;
s.V[v1] -= s.V[v2];
break;
case 0x6:
//set VF to VX's LSB, right shift VX
s.V[0xF] = (s.V[v1] & 0x0001);
s.V[v1] = s.V[v1] >> 1;
break;
case 0x7:
//set VX to VY - VX
if (s.V[v1] > s.V[v2]) s.V[0xF] = 0; //if v2-v1 would result in a number less than 0, set carry bit
else s.V[0xF] = 1;
s.V[v1] = s.V[v2] - s.V[v1];
break;
case 0xE:
//set VF to VX's MSB, left shift VX
s.V[0xF] = s.V[v1] >> 7;
s.V[v1] = s.V[v1] << 1;
break;
default:
printf("Unknown instruction 0x%X", s.opcode);
}
s.pc += 2;
break;
}
case 0x9: {
//skip next instruction if VX != VY
unsigned char v1 = (s.opcode & 0x0F00) >> 8;
unsigned char v2 = (s.opcode & 0x00F0) >> 4;
if (s.V[v1] != s.V[v2]) s.pc += 4;
else s.pc += 2;
break;
}
case 0xA: {
//set I register to address NNN
s.I = (s.opcode & 0x0FFF);
s.pc += 2;
break;
}
case 0xB: {
//set PC to V0 + NNN
s.pc = s.V[0] + (s.opcode & 0x0FFF);
break;
}
case 0xC: {
//set VX to random number & NN
unsigned char v = (s.opcode & 0x0F00) >> 8;
unsigned char r = rand();
s.V[v] = r & (s.opcode & 0x00FF);
s.pc += 2;
break;
}
case 0xD: {
//draw sprite to screen with X, Y coordinate and height
unsigned short x = s.V[(s.opcode & 0x0F00) >> 8];
unsigned short y = s.V[(s.opcode & 0x00F0) >> 4];
unsigned short h = s.opcode & 0x000F;
drawSprite(x, y, h);
drawFlag = true;
s.pc += 2;
break;
}
case 0xE: {
//input
unsigned char v = (s.opcode & 0x0F00) >> 8;
//("checking for input\n");
if ((s.opcode & 0x00FF) == 0x9E) {
//if key at VX has been pressed, skip next instruction
if (s.key[s.V[v]] != 0) {
s.pc += 4;
} else {
s.pc += 2;
}
s.key[s.V[v]] = 0;
} else if ((s.opcode & 0x00FF) == 0xA1) {
//if key at VX hasn't been pressed, skip next instruction
if (s.key[s.V[v]] == 0) {
s.pc += 4;
} else {
s.pc += 2;
}
s.key[s.V[v]] = 0;
} else {
printf("Unknown instruction 0x%X", s.opcode);
}
break;
}
case 0xF: {
unsigned char v = (s.opcode & 0x0F00) >> 8;
switch (s.opcode & 0x00FF) {
case 0x07: {
//set VX to delay_timer
s.V[v] = s.delay_timer;
s.pc += 2;
break;
}
case 0x0A: {
//block execution until key pressed
int key = getBlockingKey();
if (key != -1) {
s.V[v] = key;
s.pc += 2;
}
break;
}
case 0x15: {
//set delay_timer to VX
s.delay_timer = s.V[v];
s.pc += 2;
break;
}
case 0x18: {
//set sound_timer to VX
s.sound_timer = s.V[v];
s.pc += 2;
break;
}
case 0x1E: {
//add VX to I
if (s.I + s.V[v] > 0xFFF) s.V[0xF] = 1; //set VF if S.I + VX > 0xFFF
else s.V[0xF] = 0;
s.I += s.V[v];
s.pc += 2;
break;
}
case 0x29:
//get sprite at addr VX
s.I = s.V[v] * 5;
s.pc += 2;
break;
case 0x33:
//stores binary-coded decimal representation of VX
//this was copied, should go back and understand what it does.
s.memory[s.I] = s.V[v] / 100;
s.memory[s.I + 1] = (s.V[v] / 10) % 10;
s.memory[s.I + 2] = (s.V[v] % 100) % 10;
s.pc += 2;
break;
case 0x55:
//dump V0->VX (inclusive) to memory
for (int i = 0; i <= v; i++) {
s.memory[s.I + i] = s.V[i];
}
s.pc += 2;
break;
case 0x65:
//load into V0->VX (inclusive) from memory
for (int i = 0; i <= v; i++) {
s.V[i] = s.memory[s.I + i];
}
s.pc += 2;
break;
}
break;
}
}
if (s.delay_timer > 0) s.delay_timer--;
if (s.sound_timer > 0) s.sound_timer--;
}
void chip8::clearScreen() {
for (int i = 0; i < 2048; i++) {
s.gfx[i] = 0;
}
printf("Clearing screen\n");
}
void chip8::drawSprite(unsigned short x, unsigned short y, unsigned short h) {
//draw each row of the sprite
s.V[0xF] = 0;
for (int i = 0; i < h; i++) {
unsigned char row = s.memory[s.I + i];
int start = ((y + i) * 64) + x;
for (int j = 0; j < 8; j++) {
unsigned short pix = row & (0x80 >> j);
unsigned short end = pix ^ s.gfx[start + j];
if (end == 0 && pix == 1) s.V[0xF] = 1;
s.gfx[start + j] = end;
}
}
}
int chip8::getBlockingKey() {
int key = -1;
for (int i = 0; i < 16; i++) {
if (s.key[i] != 0) {
key = i;
break;
}
}
return key;
}