-
Notifications
You must be signed in to change notification settings - Fork 0
/
chip8.cpp
407 lines (355 loc) · 11 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
#include "chip8.hpp"
CHIP8::CHIP8()
{
std::fill(std::begin(this->ram), std::end(this->ram), 0); // memory
this->delayTimer = 0x00;
this->soundTimer = 0x00;
this->pc = &ram[START_ROM]; // program counter: current instruction
setupFont();
readRom();
}
void CHIP8::setupFont()
{
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
};
// Store fontset in memory
int ramIndex = START_FONT;
for (int i = 0; i < sizeof(fontset); i++)
{
(this->ram)[ramIndex] = fontset[i];
ramIndex++;
}
}
void CHIP8::readRom()
{
// Read ROM and store in RAM
std::string filePath = "test.ch8";
std::ifstream romFile(filePath, std::ios::binary);
if (romFile.is_open())
{
std::string data;
while (romFile)
data.push_back(romFile.get());
for (int i = 0; i < data.size(); i++)
(this->ram)[START_ROM + i] = data[i];
}
}
void CHIP8::setupGUI()
{
// Initialize SDL
SDL_Init(SDL_INIT_VIDEO);
// Create window and renderer
this->window = SDL_CreateWindow(
"CHIP-8",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
GRID_WIDTH * PIXEL_SIZE,
GRID_HEIGHT * PIXEL_SIZE,
0);
this->renderer = SDL_CreateRenderer(window, -1, 0);
// Main loop flag
this->running = true;
// Event handler
this->event;
// Keyboard status
this->keyDown = false;
// Keyboard mapping
this->keyMap = {
{SDL_SCANCODE_1, 1}, {SDL_SCANCODE_2, 2}, {SDL_SCANCODE_3, 3}, {SDL_SCANCODE_4, 12},
{SDL_SCANCODE_Q, 4}, {SDL_SCANCODE_W, 5}, {SDL_SCANCODE_E, 6}, {SDL_SCANCODE_R, 13},
{SDL_SCANCODE_A, 7}, {SDL_SCANCODE_S, 8}, {SDL_SCANCODE_D, 9}, {SDL_SCANCODE_F, 14},
{SDL_SCANCODE_Z, 10}, {SDL_SCANCODE_X, 0}, {SDL_SCANCODE_C, 11}, {SDL_SCANCODE_V, 15}
};
}
void CHIP8::cycle()
{
// Fetch instruction using 2 adjacent bytes
uint8_t left = *(this->pc);
uint8_t right = *((this->pc) + 1);
uint16_t full = (left << 8) + right;
std::cout << "0x" << std::setfill('0') << std::setw(4) << std::hex << full << std::endl;
uint8_t instructionType = (left & 0xF0) >> 4;
uint8_t X = left & 0x0F;
uint8_t Y = (right & 0xF0) >> 4;
uint8_t N = right & 0x0F;
uint8_t NN = right;
uint16_t NNN = (X << 8) + right;
pc += 2;
// Decode instruction
switch (instructionType)
{
case 0:
if (right == 0xE0)
{
SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderClear(renderer);
}
else if (right == 0xEE)
{
pc = stack.top();
stack.pop();
}
break;
case 1: // 1NNN
pc = &ram[NNN];
break;
case 2: // 2NNN
stack.push(pc);
pc = &ram[NNN];
break;
case 3: // 3XNN
pc += ((this->vars)[X] == NN) ? 2 : 0;
break;
case 4: // 4XNN
pc += ((this->vars)[X] != NN) ? 2 : 0;
break;
case 5: // 5XY0
pc += ((this->vars)[X] == (this->vars)[Y]) ? 2 : 0;
break;
case 6: // 6XNN
(this->vars)[X] = NN;
break;
case 7: // 7XNN
(this->vars)[X] += NN;
break;
case 8: // 8XYN
instruction8XYN(X, Y, N);
break;
case 9: // 9XY0
pc += ((this->vars)[X] == (this->vars)[Y]) ? 2 : 0;
break;
case 10: // ANNN
I = &ram[NNN];
break;
case 11: // BNNN, BXNN
pc = (oldBCommand) ? &ram[NNN + (this->vars)[0]] : &ram[NNN + (this->vars)[X]];
break;
case 12: // CXNN
instructionCXNN(X, NN);
break;
case 13: // DXYN
instructionDXYN(X, Y, N);
break;
case 14: // EX__
if (right == 0x9E)
{
pc += (keyHex == (this->vars)[X]) ? 2 : 0;
}
else if (right == 0xA1)
{
pc += (keyHex != (this->vars)[X]) ? 2 : 0;
}
break;
case 15: // F
instructionFX(right, X);
break;
default:
break;
}
}
void CHIP8::handleEvents()
{
// Poll for events
while (SDL_PollEvent(&(this->event))) {
// Check for quit event
if ((this->event).type == SDL_QUIT)
{
this->running = false;
}
else if ((this->event).type == SDL_KEYDOWN || (this->event).type == SDL_KEYUP)
{
if ((this->event).type == SDL_KEYDOWN)
{
this->keyDown = true;
this->scancode = (this->event).key.keysym.scancode;
this->keyHex = this->keyMap[this->scancode];
// std::cout << this->scancode << std::endl;
}
else
{
this->keyDown = false;
}
}
}
}
void CHIP8::instruction8XYN(uint8_t X, uint8_t Y, uint8_t N)
{
// 8XY_
switch (N)
{
case 0: // set
(this->vars)[X] = (this->vars)[Y];
break;
case 1: // or
(this->vars)[X] |= (this->vars)[Y];
break;
case 2: // and
(this->vars)[X] &= (this->vars)[Y];
break;
case 3: // xor
(this->vars)[X] ^= (this->vars)[Y];
break;
case 4: // add
{
uint8_t temp = (this->vars)[X] + (this->vars)[Y];
(this->vars)[NUM_VARS - 1] = (temp > 255) ? 1 : 0;
(this->vars)[X] += (this->vars)[Y];
break;
}
case 5: // subtract
(this->vars)[NUM_VARS - 1] = ((this->vars)[X] > (this->vars)[Y]) ? 1 : 0;
(this->vars)[X] -= (this->vars)[Y];
break;
case 7: // subtract
(this->vars)[NUM_VARS - 1] = ((this->vars)[Y] > (this->vars)[X]) ? 1 : 0;
(this->vars)[X] = (this->vars)[Y] - (this->vars)[X];
break;
case 6: // shift
{
(this->vars)[X] = (this->vars)[Y];
bool outBit = (this->vars)[X] & 1;
(this->vars)[X] >>= 1;
(this->vars)[NUM_VARS - 1] = (outBit) ? 1 : 0;
break;
}
case 14: // shift
{
(this->vars)[X] = (this->vars)[Y];
bool outBit = ((this->vars)[X] & (1 << 7)) >> 7;
(this->vars)[X] <<= 1;
(this->vars)[NUM_VARS - 1] = (outBit) ? 1 : 0;
break;
}
default:
break;
}
}
void CHIP8::instructionDXYN(uint8_t X, uint8_t Y, uint8_t N)
{
uint8_t xIni = (this->vars)[X] & (GRID_WIDTH - 1);
uint8_t xCoord = xIni;
uint8_t yCoord = (this->vars)[Y] & (GRID_HEIGHT - 1);
(this->vars)[NUM_VARS - 1] = 0;
for (int i = 0; i < N; i++) {
xCoord = xIni;
uint8_t spriteRow = *((this->I) + i);
for (int j = 0; j < 8; j++) {
// If you reach right edge of screen, stop drawing row
if (xCoord >= GRID_WIDTH)
break;
bool spriteBit = (spriteRow & (1 << (7 - j))) != 0;
// Read the pixel color at (xCoord, yCoord)
Uint32 pixel;
SDL_Rect rect = { xCoord, yCoord, 1, 1 };
SDL_RenderReadPixels(this->renderer, &rect, SDL_PIXELFORMAT_ARGB8888, &pixel, sizeof(pixel));
// Extract color components
Uint8 r, g, b, a;
SDL_GetRGBA(pixel, SDL_GetWindowSurface(this->window)->format, &r, &g, &b, &a);
// Check bit
bool windowBit = (r == 255 && g == 255 && b == 255);
// Update display accordingly
if (spriteBit && windowBit)
{
SDL_SetRenderDrawColor(this->renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
SDL_Rect pixelRect = {xCoord * PIXEL_SIZE, yCoord * PIXEL_SIZE, PIXEL_SIZE, PIXEL_SIZE};
SDL_RenderFillRect(this->renderer, &pixelRect);
(this->vars)[NUM_VARS - 1] = 1;
}
else if (spriteBit && !windowBit)
{
SDL_SetRenderDrawColor(this->renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
SDL_Rect pixelRect = {xCoord * PIXEL_SIZE, yCoord * PIXEL_SIZE, PIXEL_SIZE, PIXEL_SIZE};
SDL_RenderFillRect(this->renderer, &pixelRect);
}
xCoord++;
}
// Stop if you reach bottom edge of screen
if (yCoord >= GRID_HEIGHT)
break;
yCoord++;
}
}
void CHIP8::instructionCXNN(uint8_t X, uint8_t NN)
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<uint16_t> dist(0, std::numeric_limits<uint8_t>::max());
uint8_t randomValue = static_cast<uint8_t>(dist(gen));
(this->vars)[X] = randomValue & NN;
}
void CHIP8::instructionFX(uint8_t right, uint8_t X)
{
switch (right)
{
case 0x07:
(this->vars)[X] = static_cast<uint8_t>(this->delayTimer);
break;
case 0x15:
this->delayTimer = (this->vars)[X];
break;
case 0x18:
this->soundTimer = (this->vars)[X];
break;
case 0x1E:
(*(this->I)) += (this->vars)[X];
break;
case 0x0A:
this->pc -= (!this->keyDown) ? 2 : 0;
(this->vars)[X] = (this->keyDown) ? this->keyHex : (this->vars)[X];
break;
case 0x29: // TODO
break;
case 0x33:
{
uint8_t num = (this->vars)[X];
(*(this->I + 2)) = num % 10;
num /= 10;
(*(this->I + 1)) = num % 10;
num /= 10;
(*(this->I)) = num;
}
break;
default:
break;
}
}
void CHIP8::frameCleanup()
{
// Update screen
SDL_RenderPresent(this->renderer);
// Delay to cap frame rate
SDL_Delay(FRAME_DELAY);
if (this->delayTimer > 0) { delayTimer--; }
if (this->soundTimer > 0) { soundTimer--; }
if (soundTimer > 0)
{
// Beep
}
}
void CHIP8::fullCleanup()
{
// Cleanup and exit
SDL_DestroyRenderer(this->renderer);
SDL_DestroyWindow(this->window);
SDL_Quit();
}
bool CHIP8::isRunning()
{
return this->running;
}