-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchip8.cpp
425 lines (340 loc) · 7.89 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#include "chip8.h"
#include <bitset>
void Chip8::initialize(std::string const& game) {
//Program counter starts at byte 512, or address 0x200
pc = 512;
//clear these
opcode = 0;
idx = 0;
//set the stack pointer to the beginning of memory
sp = (unsigned short*) memory;
soundTimer = 0;
delayTimer = 0;
/*load fontset, with 16 different
characters, and 5 bytes per character*/
for(int i = 0; i < 80; i++) {
//leave first 96 bytes for the stack!
memory[i+96] = fontset[i];
//there MAY be a bug with fonts
if (i%5 == 0) {
/*inserts the characters 1 to
F and maps them to their respective
memory addresses*/
char ch = (i/5 <= 10 ? '0' : 'A') + i/5;
fontsetMap.insert({'0' + i/5, i+96});
}
}
loadGame(game);
};
void Chip8::loadGame(std::string const& name) {
int constexpr maxLen = 3584;
std::ifstream is(name, std::ios::binary | std::ios::in);
is.seekg(0, is.end);
int fileSize = is.tellg();
is.seekg(0, is.beg);
if (!is || fileSize > maxLen) {
std::cout << "Error: file size is " << fileSize << '\n';
/*Just throw a system error just to mess
with whoever is using this*/
throw std::system_error();
}
/*first 512 bytes are typically reserved
for the interpreter*/
is.read(reinterpret_cast<char*> (memory+512), fileSize);
}
void Chip8::executeCycle() {
if (pc > 4095)
throw std::exception();
drawFlag = false;
//merge two bytes of the pc
opcode = (memory[pc] << 8) | (memory[pc + 1]);
//opcode nibbles, MUCH more readable, and efficent
unsigned char nibble[4];
//fancy code to get each nibble (4 bits) from the opcode
for (int i = 3, j = 0xF000; i >= 0; i--, j >>= 4) {
nibble[3-i] = (opcode & j) >> i*4;
}
/*Warning: slightly cringey code.
These are notations that make it more
convenient to read from and write to
registers that are frequently accessed.
These registers represent the "X"
(second nibble) and "Y" (third nibble)
in the code.*/
unsigned char& regX = reg[nibble[1]];
unsigned char& regY = reg[nibble[2]];
//the "F" register
unsigned char& regF = reg[0xF];
unsigned short nnn = opcode & 0x0FFF;
unsigned short nn = opcode & 0x00FF;
unsigned short n = opcode & 0x000F;
/*
if ur a girl and ur reading this hi this is me
and id really like a gf plz u can contact me on
discord my discord is Rift#8844 and u can dm me
for my snap if u want
im not desparate anything im just a nice
guy who would want a gf its ok if u dont want to
date me but itd be nice thats all thank you
*/
switch (nibble[0]) {
case 0x0:
//break;
/*(apparently) depracated, and obscure opcode...
maybe don't implement?
if (opcode != 0x00E0 && opcode != 0x00EE)
opcode = memory[nnn];*/
switch (opcode) {
case 0x00E0:
//clearDisplay();
for (int x = 0; x < screenWidth; x++) {
for (int y = 0; y < screenHeight; y++) {
gfx[x][y] = 0;
}
}
break;
case 0x00EE:
pc = *sp;
sp -= 2;
break;
}
break;
//1NNN: jump to NNN
case 0x1:
//subtract two since it will be added at the end
pc = nnn - 2;
break;
//2NNN: function call at nnn
case 0x2:
sp += 2;
*sp = pc;
pc = nnn - 2;
break;
/*OUT OF BOUNDS POSSIBLE on 3XNN and 4XNN,
make sure to change later!*/
//3XNN: if V[X] == NN, skip instruction
case 0x3:
if (regX == nn) {
pc += 2;
}
break;
//4XNN: if V[X] != NN, skip instruction
case 0x4:
if (regX != nn) {
pc += 2;
}
break;
//5XY0: if V[X] == V[Y], skip instruction
case 0x5:
if (regX == regY) {
pc += 2;
}
break;
//6XNN: set V[X] to NN
case 0x6:
regX = nn;
break;
/*7XNN: increment V[X] by NN,
NOT SURE if carry flag is changed or not!*/
case 0x7:
regX += nn;
break;
case 0x8:
switch (n) {
//8XY0: set V[X] to V[Y]
case 0x0:
regX = regY;
break;
case 0x1:
regX |= regY;
break;
case 0x2:
regX &= regY;
break;
case 0x3:
regX ^= regY;
break;
case 0x4:
regF = (regX - regY) > 0xFF;
regX += regY;
break;
case 0x5:
regF = (regX - regY) >= 0;
regX -= regY;
break;
case 0x6:
regF = regX >> 7;
regX >>= 1;
break;
case 0x7:
regF = (regY - regX) < 0;
regX = regY - regX;
break;
case 0xE:
regF = (regX << 7) >> 7;
regX <<= 1;
break;
}
break;
case 0x9:
if (regX != regY) {
pc += 2;
}
break;
case 0xA:
idx = nnn;
break;
case 0xB:
pc = reg[0] + nnn - 2;
break;
case 0xC:
regX = rand() & nn;
break;
/*Display opcode. This is the most
complex opcode, come back to it later.*/
case 0xD: {
/*consider making these local to
the class itself*/
int constexpr width = 8;
//not n+1 for some reason...
int height = n;
/*wrap around starting positions
but DONT wrap around the actual drawings*/
int startX = regX % screenWidth;
int startY = regY % screenHeight;
unsigned char spriteRow;
//false by default
regF = false;
//clipping.
for (int y = 0; y < height && y + startY < screenHeight; y++) {
spriteRow = memory[idx + y];
for (int x = 0; x < width && x + startX < screenWidth; x++) {
unsigned char& pixel = gfx[startX + x][startY + y];
/*if the current pixel is XOR'd to zero,
set the V[F] flag to true*/
if (spriteRow&0x80 && pixel)
regF = true;
/*xor the current pixel with the current
bit of the sprite row byte*/
pixel ^= (spriteRow >> 7);
spriteRow <<= 1;
if (x < 0 || x > 64 || y < 0 || y > 32)
throw std::exception();
}
}
break;
}
case 0xE:
switch (nn) {
case 0x9E:
if (keyDown(regX))
pc += 2;
break;
case 0xA1:
if (!keyDown(regX))
pc += 2;
break;
}
break;
case 0xF:
switch (nn) {
case 0x07:
regX = delayTimer;
break;
case 0XA:
//consider using an interrupt for performance benefits
for (unsigned char i = 0;; i++) {
if (keyDown(i)) {
regX = i;
pc += 2;
break;
}
}
pc -= 2;
break;
case 0x15:
delayTimer = regX;
break;
case 0x1E:
idx += regX;
break;
case 0x29:
idx = fontsetMap[regX];
break;
case 0x33: {
unsigned int val = regX;
for (int i = 2; i >= 0; i--) {
memory[idx+i] = val%10;
val /= 10;
}
break;
}
break;
case 0x55:
for (int i = 0; i <= nibble[1]; i++) {
memory[i+idx] = reg[i];
}
break;
case 0x65:
for (int i = 0; i <= nibble[1]; i++) {
reg[i] = memory[idx+i];
}
break;
}
break;
default: {
std::cout << "Encountered unknown opcode! Terminating!\n";
throw std::exception();
}
}
pc += 2;
//count down timers at 60hz
if (cycleNumber/clockSpeed*60 >= 1) {
if (delayTimer > 0)
delayTimer--;
if (soundTimer > 0)
soundTimer--;
//sleep for 1/60th of a second
usleep(16667);
cycleNumber = 1;
drawFlag = true;
} else {
cycleNumber++;
}
}
std::ostream& Chip8::memdump(std::ostream& os) {
os << "Memory dump:\n";
for (int i = 0; i < 4096; i++) {
if (memory[i] != 0) {
os << "[" << i <<
"]: " << std::hex << (int) memory[i] <<
std::dec << '\n';
}
}
return os;
}
/*old, deprecated debug draw function for drawing
graphics to terminal before I had a proper GUI*/
std::ostream& Chip8::gfxDraw(std::ostream& os) {
std::stringstream buffer;
/*for (int i = 0; i < 64; i++)
os << std::setw(2) << i;*/
buffer << "\033[H\033[J";
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 64; j++) {
buffer << ((int) gfxBuffer()[j*32 + i] ? "\u2588\u2588" : " ");
}
buffer << '\n';
}
return os << buffer.str();
}
//there was an attempt at least...
/*std::ostream& Chip8::debugInfo(std::ostream& os, DebugFlags flags) {
os << "Debug Info:\n";
if (flags & 0x1) {
os << ""
}
}*/
/*Chip8::Chip8() {
initialize();
}*/