-
Notifications
You must be signed in to change notification settings - Fork 0
/
chip8.cpp
400 lines (363 loc) · 8.93 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
#include "chip8.h"
std::vector<uint8_t> fontset
{
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
};
Chip8::Chip8()
: gfx{ 2048 }
, keys{ 16 }
, opcode{ 0 }
, pc{ 0x200 }
, sp{ 0 }
, I{ 0 }
, delay_timer{ 0 }
, sound_timer{ 0 }
, stack(32)
, V(16)
, memory(4096)
{
// load fontset into memory
for (int i{ 0 }; i < 80; ++i) {
memory[i] = fontset[i];
};
};
void Chip8::fetchOpcode() {
opcode = memory[pc] << 8 | memory[pc + 1];
pc += 2;
}
void Chip8::decodeAndExecute() {
uint8_t left_nib = (opcode >> 12) & 0xf;
switch (left_nib) {
case 0x0: // 0--- Instructions
{
uint8_t second_byte = opcode & 0xff;
switch (second_byte)
{
case 0xe0: // 00E0 - clear screen
{
std::fill(gfx, gfx + 2048, 0);
//for (int i{ 0 }; i < 2048; ++i) {
// gfx[i] = 0;
//};
} break;
case 0xee: // 00EE - return from subroutine
{
sp -= 1;
pc = stack[sp];
} break;
default: // 0NNN - skip this instruction
break;
};
} break;
case 0x1: // 1NNN - JMP to address NNN
{
uint16_t addr = opcode & 0xfff;
pc = addr;
} break;
case 0x2: // 2NNN - Execute subroutine at address NNN
{
uint16_t addr = opcode & 0xfff;
stack[sp] = pc;
sp += 1;
pc = addr;
} break;
case 0x3: // 3XNN - Skip next instruction if VX == NN
{
uint8_t x = (opcode >> 8) & 0xf;
uint8_t byte = opcode & 0xff;
if (V[x] == byte)
pc += 2;
} break;
case 0x4: // 4XNN - Skip next instruction if VX != NN
{
uint8_t x = (opcode >> 8) & 0xf;
uint8_t byte = opcode & 0xff;
if (V[x] != byte)
pc += 2;
} break;
case 0x5: // 5XY0 - Skip next instruction if VX == VY
{
uint8_t x = (opcode >> 8) & 0xf;
uint8_t y = (opcode >> 4) & 0xf;
if (V[x] == V[y])
pc += 2;
} break;
case 0x6: // 6XNN - Store NN in register VX
{
uint8_t x = (opcode >> 8) & 0xf;
uint8_t byte = opcode & 0xff;
V[x] = byte;
} break;
case 0x7: // 7XNN - Add NN to register VX
{
uint8_t x = (opcode >> 8) & 0xf;
uint8_t byte = opcode & 0xff;
V[x] += byte;
} break;
case 0x8: // 8--- Instructions
{
uint8_t right_nib = opcode & 0xf;
uint8_t x = (opcode >> 8) & 0xf;
uint8_t y = (opcode >> 4) & 0xf;
uint16_t result{};
uint8_t temp{}; // for carry flag V[F]
switch (right_nib) {
case 0x0: { V[x] = V[y]; } break; // 8XY0 - VX <- VY
case 0x1: { V[x] |= V[y]; } break; // 8XY1 - VX = VX | VY
case 0x2: { V[x] &= V[y]; } break; // 8XY2 - VX = VX & VY
case 0x3: { V[x] ^= V[y]; } break; // 8XY3 - VX = VX ^ VY
case 0x4: // 8XY4 - Add & set carry (VF)
{
result = V[x] + V[y];
if (result > 0x00ff)
temp = 1;
else
temp = 0;
V[x] = result & 0xff;
V[0xF] = temp;
} break;
case 0x5: // 8XY5 - Sub & set carry (VF)
{
result = V[x] - V[y];
if (V[x] >= V[y])
temp = 1;
else
temp = 0;
V[x] = result & 0xff;
V[0xF] = temp;
} break;
case 0x6: // 8XY6 - Shift right
{
V[x] = V[y];
temp = (V[x] & 0x1) == 1;
V[x] >>= 1;
V[0xF] = temp;
} break;
case 0x7: // 8XY7 - Sub & set carry (VF)
{
result = V[y] - V[x];
if (V[y] >= V[x])
temp = 1;
else
temp = 0;
V[x] = result & 0xff;
V[0xF] = temp;
} break;
case 0xE: // 8XYE - Shift left
{
V[x] = V[y];
temp = (V[x] >> 7) == 1;
V[x] <<= 1;
V[0xF] = temp;
} break;
}; // end of 0x8 switch
} break;
case 0x9: // 9XY0 - Skip next instruction if VX != VY
{
uint8_t x = (opcode >> 8) & 0xf;
uint8_t y = (opcode >> 4) & 0xf;
if (V[x] != V[y])
pc += 2;
} break;
case 0xa: // ANNN - Set index register to NNN
{
uint16_t addr = opcode & 0x0fff;
I = addr;
} break;
case 0xb: // BNNN - Jump to address NNN + V0
{
uint16_t addr = opcode & 0xfff;
pc = V[0] + addr;
} break;
case 0xc: // CXNN - VX <- random number AND NN
{
uint8_t x = (opcode >> 8) & 0xf;
uint8_t rand = (std::rand() % 256) & (opcode & 0xff);
//uint8_t rand = ((uint8_t) (std::rand() % 256)) & ((uint8_t) (opcode & 0xff));
V[x] = rand;
} break;
case 0xd: // DXYN - Display/draw
{
uint8_t x = V[(opcode >> 8) & 0xf] % 64;
uint8_t y = V[(opcode >> 4) & 0xf] % 32;
uint8_t n = opcode & 0xf;
uint8_t pixel{};
int index{};
V[0xF] = 0;
for (int i{ 0 }; i < n; ++i) {
pixel = memory[I + i];
for (int j{ 0 }; j < 8; ++j) {
index = (64 * (y + i)) + x + j;
if (j > 0 && (index % 64 == 0)) { break; }; // stop drawing if on right edge
if ((pixel & (0x80 >> j)) == (0x80 >> j)) { // check if sprite bit is on
if (gfx[index] == 0xFFFFFFFF) {
V[0xF] = 1;
}
gfx[index] ^= 0xFFFFFFFF;
};
};
if ((y + i) > 32) { break; }; // stop if on bottom of screen
};
} break;
case 0xe: // E--- Instructions
{
uint8_t right_byte = opcode & 0xff;
uint8_t x = (opcode >> 8) & 0xf;
switch (right_byte) {
case 0x9e: // EX9E - skip next opcode if key in VX is being pressed
{
if (keys[V[x]])
pc += 2;
}break;
case 0xa1: // EX9E - skip next opcode if key in VX is NOT being pressed
{
if (!(keys[V[x]]))
pc += 2;
}break;
};
} break;
case 0xf: // F--- Instructions
{
uint8_t right_byte = opcode & 0xff;
uint8_t x = (opcode >> 8) & 0xf;
switch (right_byte) {
case 0x07:{ V[x] = delay_timer; } break; // FX07 - store delay timer into VX
case 0x0a: // FX0A - wait for keypress and store into VX
{
if (keys[0])
V[x] = 0;
else if (keys[1])
V[x] = 1;
else if (keys[2])
V[x] = 2;
else if (keys[3])
V[x] = 3;
else if (keys[4])
V[x] = 4;
else if (keys[5])
V[x] = 5;
else if (keys[6])
V[x] = 6;
else if (keys[7])
V[x] = 7;
else if (keys[8])
V[x] = 8;
else if (keys[9])
V[x] = 9;
else if (keys[10])
V[x] = 10;
else if (keys[11])
V[x] = 11;
else if (keys[12])
V[x] = 12;
else if (keys[13])
V[x] = 13;
else if (keys[14])
V[x] = 14;
else if (keys[15])
V[x] = 15;
else
pc -= 2;
} break;
case 0x15: { delay_timer = V[x]; } break;
case 0x18: { sound_timer = V[x]; } break;
case 0x1e: { I += V[x]; } break;
case 0x29: // FX29 - Font character
{
I = V[x] * 5;
} break;
case 0x33: // FX33 - Binary-coded decimal conversion
{
uint16_t value = V[x];
memory[I + 2] = value % 10;
value /= 10;
memory[I + 1] = value % 10;
value /= 10;
memory[I] = value % 10;
} break;
case 0x55: // FX55 - Store registers V[0] to V[X] into memory
{
for (uint8_t i{ 0 }; i <= x; ++i) {
memory[I + i] = V[i];
};
} break;
case 0x65: // FX65 - Load registers V[0] to V[X] from memory
{
for (uint8_t i{ 0 }; i <= x; ++i) {
V[i] = memory[I + i];
};
} break;
}; // end of 0xf switch
} break;
}; // end of main switch
}
void Chip8::updateTimers() {
if (delay_timer > 0)
--delay_timer;
// emit a sound if non-zero
if (sound_timer > 0)
--sound_timer;
}
void Chip8::emulateCycle() {
fetchOpcode(); // Fetch Opcode (Increment PC here)
decodeAndExecute(); // Decode + Execute Opcode
updateTimers(); // Update Timers
};
bool Chip8::loadApplication(const char* filename) {
// open file as binary stream and seek to the end
std::ifstream file(filename, std::ios::binary | std::ios::ate);
if (file.is_open()) {
// make a buffer with filesize
std::streampos size = file.tellg();
char* buffer = new char[size];
// move file pointer to start
file.seekg(0, std::ios::beg);
file.read(buffer, size);
file.close();
// load ROM into memory starting at 0x200
for (int i{ 0 }; i < size; ++i) {
memory[0x200 + i] = buffer[i];
}
delete[] buffer;
}
else {
return false;
}
return true;
};
void Chip8::testMemory() {
// Print bytes of loaded ROM
std::cout << std::hex;
for (long i = 512; i < memory.size()/6; i +=2) {
std::cout << std::uppercase << std::setfill('0') << std::setw(4) << i;
uint16_t opcode = memory[i] << 8 | memory[i + 1];
std::cout << " 0x" << std::nouppercase << std::setfill('0') << std::setw(4) << opcode << "\n";
}
}
void Chip8::testDisplay() {
// Print display array to terminal
for (int i{ 0 }; i < 2048; ++i) {
if (i % 64 == 0)
std::cout << "\n";
if (gfx[i] == 1)
std::cout << "8";
else if (gfx[i] == 0)
std::cout << "-";
};
std::cout << "\n\n";
}