-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchip8.c
491 lines (448 loc) Β· 18.9 KB
/
chip8.c
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/* Thank you to http://devernay.free.fr/hacks/chip8/C8TECH10.HTM for being an amazing technical reference. */
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <SDL2/SDL.h>
#define RAMSIZE 4096
#define PROGRAM_START 512
#ifdef DEBUG /* -DDEBUG in cc or debug=1 in the make command. */
#pragma message("Debug mode enabled.")
#endif
#define SPRITES 16
#define SPRITE_SIZE 5
#define SPRITE_OFFSET 0x50
uint8_t sprite_data[SPRITES][SPRITE_SIZE] = /* Sprites are things that can be drawn onto the screen, unlike modern bitmap systems which allow pixel by pixel drawing. */
{
{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 */
};
#define KEY_COUNT 16
uint8_t key_data[KEY_COUNT] = /* Keycodes. */
{
SDLK_x,
SDLK_1,
SDLK_2,
SDLK_3,
SDLK_q,
SDLK_w,
SDLK_e,
SDLK_a,
SDLK_s,
SDLK_d,
SDLK_z,
SDLK_c,
SDLK_4,
SDLK_r,
SDLK_f,
SDLK_v
};
#define W 64
#define H 32
#define WINDOW_SCALE 16
int main(int argc, char *argv[])
{
if (argc < 2)
{
fprintf(stderr, "Please provide a Chip8 ROM.\n");
return -1;
}
uint8_t RAM[RAMSIZE]; /* 0x000 - 0x1FF for Interpreter, 0x200 - 0xFFF for program. */
uint8_t REGISTERS[16]; /* V0 - VF (VF is for flags and should not be used by programs.) */
uint16_t I = 0; /* Use lowest 12 bits for storing memory addresses. */
uint16_t PC = PROGRAM_START; /* Stores the currently executing address. */
uint8_t SP = 0; /* Points to the topmost level of the stack. */
uint16_t STACK[16]; /* Stores return point for subroutines. */
uint8_t DT = 0; /* When greater than 0, decrement by 1 at a rate of 60hz. */
uint8_t ST = 0; /* When greater than 0, decrement by 1 at a rate of 60hz and play a sound. */
uint8_t SCREEN[W][H]; /* 64x32 monochrome display, only least significant bit is used because I'm lazy. */
uint8_t KEYS[KEY_COUNT]; /* Every key 0-F. */
memset(RAM, 0, sizeof RAM);
memset(REGISTERS, 0, sizeof REGISTERS);
memset(STACK, 0, sizeof STACK);
memset(SCREEN, 0, sizeof SCREEN);
memset(KEYS, 0, sizeof KEYS);
double ticks = 0; /* Add 60ths of a second taken to calculate the instruction. When >= 1, decrement ticks, ST, and DT. */
#define CALCULATE_TIMERS \
{ \
\
uint32_t currTime = SDL_GetTicks(); \
ticks += (currTime - startTime) / 16.6; \
\
if (ticks >= 1) /* TODO: Make the sound timer actually play sound. */ \
{ \
if (DT > 0) \
--DT; \
if (ST > 0) \
--ST; \
--ticks; \
} \
}
const uint8_t *state = SDL_GetKeyboardState(NULL);
for (int i = 0; i < SPRITES; ++i) /* Load some sprites into memory. */
{
for (int j = 0; j < SPRITE_SIZE; ++j)
{
RAM[i * SPRITE_SIZE + j + SPRITE_OFFSET] = sprite_data[i][j];
}
}
FILE *file = fopen(argv[1], "rb");
fread(RAM + PROGRAM_START /* Pointer arithmetic! */, 1, RAMSIZE - PROGRAM_START, file);
fclose(file);
#ifdef DEBUG
for (int i = 0; i < 0xFFF; i += 1)
{
printf("%02X ", RAM[i]);
if (!((i+1) % 16))
printf("\n");
}
printf("\n\n"); /* Just to offset the later debug things. */
#endif
/* SDL Stuff. */
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
printf("SDL failed to init.\n%s\n", SDL_GetError());
return -1;
}
SDL_Window *window = SDL_CreateWindow("Chip-8 Interpreter", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, W*WINDOW_SCALE, H*WINDOW_SCALE, SDL_WINDOW_SHOWN);
if (!window)
{
printf("Window failed to be created.\n%s\n", SDL_GetError());
return -1;
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (!renderer)
{
printf("Renderer failed to be created.\n%s\n", SDL_GetError());
return -1;
}
uint8_t quit = 0;
srand(time(NULL)); /* Generate random seed. */
while (!quit)
{
uint32_t startTime = SDL_GetTicks();
SDL_Event e;
SDL_PollEvent(&e);
if(e.type == SDL_QUIT)
quit = 1;
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0xFF);
SDL_RenderClear(renderer);
SDL_PumpEvents();
for (int i = 0; i < KEY_COUNT; ++i)
{
KEYS[i] = 0;
if (state[SDL_GetScancodeFromKey(key_data[i])])
KEYS[i] = 1;
}
/* Back to Chip-8 stuff. */
uint16_t instruction = (RAM[PC] << 8) + RAM[PC+1]; /* The ram is an array of single bytes, while each instruction is 2 bytes long, so we need to combine them into one 16 bit value. */
#ifdef DEBUG
printf("%04X\n", instruction);
#endif
uint8_t broken_instruction[4] = /* All 4 nibbles of the instruction, from left to right. */
{
instruction >> 12,
instruction >> 8 & 15,
instruction >> 4 & 15,
instruction & 15
};
#define UNKNOWN_INSTRUCTION
#ifdef DEBUG
#undef UNKNOWN_INSTRUCTION
#define UNKNOWN_INSTRUCTION printf("Unknown instruction %04X\n", instruction)
#endif
uint8_t jumped = 0;
switch (broken_instruction[0])
{
case 0:
if (instruction == 0x00E0) /* Clear the screen. */
memset(SCREEN, 0, sizeof SCREEN);
else if (instruction == 0x00EE) /* Return from subroutine. */
{
#ifdef DEBUG
printf("Stack pointer: %i\n", SP-1);
#endif
PC = STACK[SP--];
}
else
UNKNOWN_INSTRUCTION;
break;
case 1: /* Jump. */
PC = broken_instruction[3] + (broken_instruction[2] << 4) + (broken_instruction[1] << 8);
#ifdef DEBUG
printf("Jumping to location %03X\n", PC);
#endif
jumped = 1;
break;
case 2: /* Start a subroutine. */
#ifdef DEBUG
printf("Stack pointer: %i\n", SP+1);
#endif
STACK[++SP] = PC;
PC = broken_instruction[3] + (broken_instruction[2] << 4) + (broken_instruction[1] << 8);
#ifdef DEBUG
printf("Starting subroutine at %03X\n", PC);
#endif
jumped = 1;
break;
case 3: /* Skip the next instruction if a register equals a value. */
if (REGISTERS[broken_instruction[1]] == (broken_instruction[2] << 4) + broken_instruction[3])
PC += 2;
break;
case 4: /* Skip the next instruction if a register does not equal a value. */
if (REGISTERS[broken_instruction[1]] != (broken_instruction[2] << 4) + broken_instruction[3])
PC += 2;
break;
case 5: /* Skip the next instruction if a register equals another register. */
if (broken_instruction[3]) /* Needs to end in a zero. */
UNKNOWN_INSTRUCTION;
else if (REGISTERS[broken_instruction[1]] == REGISTERS[broken_instruction[2]])
PC += 2;
break;
case 6: /* Put a value into a register. */
REGISTERS[broken_instruction[1]] = (broken_instruction[2] << 4) + broken_instruction[3];
break;
case 7: /* Adds a value to a register. */
REGISTERS[broken_instruction[1]] += (broken_instruction[2] << 4) + broken_instruction[3];
break;
case 8: /* Operations between two registers. */
switch (broken_instruction[3])
{
case 0: /* Stores the value of a register into another register. */
REGISTERS[broken_instruction[1]] = REGISTERS[broken_instruction[2]];
break;
case 1: /* Bitwise or. */
REGISTERS[broken_instruction[1]] |= REGISTERS[broken_instruction[2]];
REGISTERS[15] = 0;
break;
case 2: /* Bitwise and. */
REGISTERS[broken_instruction[1]] &= REGISTERS[broken_instruction[2]];
REGISTERS[15] = 0;
break;
case 3: /* Bitwise xor. */
REGISTERS[broken_instruction[1]] ^= REGISTERS[broken_instruction[2]];
REGISTERS[15] = 0;
break;
case 4: /* Add. */
REGISTERS[broken_instruction[1]] += REGISTERS[broken_instruction[2]];
REGISTERS[15] = 0;
if (REGISTERS[broken_instruction[1]] < REGISTERS[broken_instruction[2]]) /* Overflow. */
REGISTERS[15] = 1; /* Carry flag. */
break;
case 5: /* Subtract. */
REGISTERS[15] = 0;
if (REGISTERS[broken_instruction[1]] > REGISTERS[broken_instruction[2]])
REGISTERS[15] = 1; /* Carry flag. */
REGISTERS[broken_instruction[1]] -= REGISTERS[broken_instruction[2]];
break;
case 6: /* Right shift. */
REGISTERS[15] = 0;
if (REGISTERS[broken_instruction[2]] & 1) /* Least significant bit. */
REGISTERS[15] = 1; /* Carry flag. */
REGISTERS[broken_instruction[1]] = REGISTERS[broken_instruction[2]] >> 1;
break;
case 7: /* Subtract but different. */
REGISTERS[15] = 0;
if (REGISTERS[broken_instruction[2]] > REGISTERS[broken_instruction[1]])
REGISTERS[15] = 1; /* Carry flag. */
REGISTERS[broken_instruction[1]] = REGISTERS[broken_instruction[2]] - REGISTERS[broken_instruction[1]];
break;
case 14: /* Left shift. */
REGISTERS[15] = 0;
if (REGISTERS[broken_instruction[2]] & 0b10000000) /* Most significant bit. */
REGISTERS[15] = 1; /* Carry flag. */
REGISTERS[broken_instruction[1]] = REGISTERS[broken_instruction[2]] << 1;
break;
default:
UNKNOWN_INSTRUCTION;
break;
}
break;
case 9: /* Skip next instruction if a register does not equal another register. */
if (broken_instruction[3]) /* Needs to end in a zero. */
UNKNOWN_INSTRUCTION;
if (REGISTERS[broken_instruction[1]] != REGISTERS[broken_instruction[2]])
PC += 2;
break;
case 10: /* Set register I to a 12-bit memory address. */
I = broken_instruction[3] + (broken_instruction[2] << 4) + (broken_instruction[1] << 8);
break;
case 11: /* Jump to a value added to a register. */
PC = broken_instruction[3] + (broken_instruction[2] << 4) + (broken_instruction[1] << 8) + REGISTERS[0];
jumped = 1;
break;
case 12: /* Sets a register to a random value and performs a bitwise and operation on it. */
REGISTERS[broken_instruction[1]] = (rand() & 0xFF) & (broken_instruction[3] + (broken_instruction[2] << 4));
#ifdef DEBUG
printf("Random number is %i\n", REGISTERS[broken_instruction[1]]);
#endif
break;
case 13: /* Draw the sprite stored in the memory address in register I. */
REGISTERS[15] = 0;
for (int i = 0; i < broken_instruction[3]; ++i)
{
uint8_t row = RAM[I + i];
for (int j = 0; j < 8; ++j)
{
int x = (REGISTERS[broken_instruction[1]] % 64 + j);
int y = (REGISTERS[broken_instruction[2]] % 32 + i);
if (x < 64 && y < 32)
{
uint8_t pixel = (row >> (7 - j)) & 1;
if (SCREEN[x][y]) REGISTERS[15] = 1;
SCREEN[x][y] ^= pixel;
}
}
}
break;
case 14: /* Skip next instruction if a specific key is/isn't pressed. */
switch (broken_instruction[3])
{
case 14: /* Is pressed. */
if (KEYS[REGISTERS[broken_instruction[1]]])
PC += 2;
break;
case 1: /* Isn't pressed. */
if (!KEYS[REGISTERS[broken_instruction[1]]])
PC += 2;
break;
default:
UNKNOWN_INSTRUCTION;
break;
}
break;
case 15: /* More register operations. */
switch (broken_instruction[2])
{
case 0:
switch (broken_instruction[3])
{
case 7: /* Set a register to the value of the delay timer. */
REGISTERS[broken_instruction[1]] = DT;
break;
case 10: /* Pause execution until a key is pressed, and then store the key value in a register. */
while (1)
{
CALCULATE_TIMERS;
uint8_t done = 0;
SDL_PumpEvents();
for (int i = 0; i < KEY_COUNT; ++i)
{
if (state[SDL_GetScancodeFromKey(key_data[i])])
{
REGISTERS[broken_instruction[1]] = i;
done = 1;
break;
}
}
if (done)
break;
}
break;
default:
UNKNOWN_INSTRUCTION;
break;
}
break;
case 1:
switch (broken_instruction[3])
{
case 5: /* Set the value of the delay timer to a register. */
DT = REGISTERS[broken_instruction[1]];
break;
case 8: /* Set the value of the sound timer to a register. */
ST = REGISTERS[broken_instruction[1]];
break;
case 14: /* Add the value of a register to I. */
I += REGISTERS[broken_instruction[1]];
break;
default:
UNKNOWN_INSTRUCTION;
break;
}
break;
case 2:
if (broken_instruction[3] == 9) /* I is set to the location of one of the default sprites. */
I = SPRITE_OFFSET + REGISTERS[broken_instruction[1]] * SPRITE_SIZE;
else
UNKNOWN_INSTRUCTION;
break;
case 3:
if (broken_instruction[3] == 3) /* Converts a number into its bcd equivalent. */
{
RAM[I] = REGISTERS[broken_instruction[1]]/100;
RAM[I+1] = REGISTERS[broken_instruction[1]]/10%10;
RAM[I+2] = REGISTERS[broken_instruction[1]]%10;
}
else
UNKNOWN_INSTRUCTION;
break;
case 5:
if (broken_instruction[3] == 5) /* Copy n amount of registers starting at register 0 into memory at the location stored in I. */
{
for (int i = 0; i <= broken_instruction[1] ; ++i)
{
RAM[I++] = REGISTERS[i];
}
}
else
UNKNOWN_INSTRUCTION;
break;
case 6:
if (broken_instruction[3] == 5) /* Load n amount of registers starting at register 0 from memory at the location stored in I.*/
{
for (int i = 0; i <= broken_instruction[1]; ++i)
{
REGISTERS[i] = RAM[I++];
}
}
else
UNKNOWN_INSTRUCTION;
break;
default:
UNKNOWN_INSTRUCTION;
break;
}
break;
default:
UNKNOWN_INSTRUCTION;
break;
}
if (!jumped)
PC += 2;
/* Draw the screen. */
SDL_Rect pixel = {0, 0, WINDOW_SCALE, WINDOW_SCALE};
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
for (int x = 0; x < W; ++x)
{
for (int y = 0; y < H; ++y)
{
pixel.x = x * WINDOW_SCALE;
pixel.y = y * WINDOW_SCALE;
if (SCREEN[x][y])
{
SDL_RenderFillRect(renderer, &pixel);
}
}
}
SDL_RenderPresent(renderer);
CALCULATE_TIMERS;
}
return 0;
}