-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
432 lines (417 loc) · 12.7 KB
/
main.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
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <SDL.h>
#include "instruction.c"
#include "graphics.c"
const SDL_Scancode KEYS[16] = {
SDL_SCANCODE_1, SDL_SCANCODE_2, SDL_SCANCODE_3, SDL_SCANCODE_4,
SDL_SCANCODE_Q, SDL_SCANCODE_W, SDL_SCANCODE_E, SDL_SCANCODE_R,
SDL_SCANCODE_A, SDL_SCANCODE_S, SDL_SCANCODE_D, SDL_SCANCODE_F,
SDL_SCANCODE_Z, SDL_SCANCODE_X, SDL_SCANCODE_C, SDL_SCANCODE_V
};
typedef struct {
uint16_t stack[256];
uint8_t ptr;
} callstack;
callstack new_callstack() {
callstack stack;
stack.ptr = 0;
return stack;
}
int callstack_push(callstack* stack, uintptr_t addr) {
if (stack->ptr == 255) return -1;
stack->stack[stack->ptr] = addr;
return stack->ptr++;
}
int callstack_pop(callstack* stack) {
if (stack->ptr == 0) return -1;
return stack->stack[--stack->ptr];
}
uint8_t rand_byte() {
return rand() % 256;
}
typedef enum {
SUCCESS,
ERR_STACK_OVERFLOW,
ERR_STACK_UNDERFLOW,
ERR_INVALID,
} tick_result;
#define DIGIT_BASE_ADDR 0
#define DIGIT_LEN 5
static uint8_t DIGIT_SPRITES[80] = {0xF0, 0x90, 0x90, 0x90, 0xF0, \
0x20, 0x60, 0x20, 0x20, 0x70, \
0xF0, 0x10, 0xF0, 0x80, 0xF0, \
0xF0, 0x10, 0xF0, 0x10, 0xF0, \
0x90, 0x90, 0xF0, 0x10, 0x10, \
0xF0, 0x80, 0xF0, 0x10, 0xF0, \
0xF0, 0x80, 0xF0, 0x90, 0xF0, \
0xF0, 0x10, 0x20, 0x40, 0x40, \
0xF0, 0x90, 0xF0, 0x90, 0xF0, \
0xF0, 0x90, 0xF0, 0x10, 0xF0, \
0xF0, 0x90, 0xF0, 0x90, 0x90, \
0xE0, 0x90, 0xE0, 0x90, 0xE0, \
0xF0, 0x80, 0x80, 0x80, 0xF0, \
0xE0, 0x90, 0x90, 0x90, 0xE0, \
0xF0, 0x80, 0xF0, 0x80, 0xF0, \
0xF0, 0x80, 0xF0, 0x80, 0x80};
typedef struct vm {
uint8_t ram[4096];
uint8_t reg[16];
uint16_t i;
uint16_t pc;
uint8_t tpu;
uint8_t delay;
uint8_t sound;
bool waiting_for_keypress;
SDL_Scancode* key_released;
callstack stack;
fps_clock clock;
sdl_handle* gfx;
} chip8_vm;
void vm_load_program(chip8_vm* vm, sdl_handle* gfx, uint8_t ticks_per_update, const uint8_t program[], int program_len) {
for (int i = 0; i < 16; i++){
vm->reg[i] = 0;
}
vm->i = 0;
vm->delay = 0;
vm->sound = 0;
vm->waiting_for_keypress = false;
vm->key_released = NULL;
vm->tpu = ticks_per_update;
vm->stack = new_callstack();
vm->clock = new_fps_clock(60);
vm->gfx = gfx;
// Chip-8 programs are loaded at address 0x200
vm->pc = 0x200;
for (int i = 0; i < program_len; i++) {
vm->ram[i+0x200] = program[i];
}
// Load the digit sprites into memory
for (int i = 0; i < DIGIT_LEN * 16; i++) {
vm->ram[i + DIGIT_BASE_ADDR] = DIGIT_SPRITES[i];
}
}
tick_result vm_tick(chip8_vm* vm) {
// Load the next two bytes that make up the instruction
uint16_t raw_inst = (((uint16_t) vm->ram[vm->pc]) << 8) + (uint16_t) vm->ram[vm->pc+1];
instruction inst = decode_instruction(raw_inst);
vm->pc += 2;
switch (inst.tag) {
case CLEAR:
clear_screen(vm->gfx);
break;
case LOAD:
vm->reg[inst.reg1] = inst.data;
break;
case MOV:
vm->reg[inst.reg1] = vm->reg[inst.reg2];
break;
case ADD_NUM:
vm->reg[inst.reg1] += inst.data;
break;
case ADD_REG: {
uint16_t x = (uint16_t) vm->reg[inst.reg1];
uint16_t y = (uint16_t) vm->reg[inst.reg2];
uint16_t res = x + y;
if (res > 255) {
res = res % 256;
vm->reg[0xF] = 0x01;
} else {
vm->reg[0xF] = 0x00;
}
vm->reg[inst.reg1] = (uint8_t) res;
break;
}
case SUB_REG: {
uint8_t x = vm->reg[inst.reg1];
uint8_t y = vm->reg[inst.reg2];
uint8_t res = x - y;
if (x > y) {
vm->reg[0xF] = 0x01;
} else {
vm->reg[0xF] = 0x00;
}
vm->reg[inst.reg1] = res;
break;
}
case SUB_FROM: {
uint8_t x = vm->reg[inst.reg1];
uint8_t y = vm->reg[inst.reg2];
uint8_t res = y - x;
if (y > x) {
vm->reg[0xF] = 0x01;
} else {
vm->reg[0xF] = 0x00;
}
vm->reg[inst.reg1] = res;
break;
}
case AND:
vm->reg[inst.reg1] &= vm->reg[inst.reg2];
break;
case OR:
vm->reg[inst.reg1] |= vm->reg[inst.reg2];
break;
case XOR:
vm->reg[inst.reg1] ^= vm->reg[inst.reg2];
break;
case RSHIFT: {
uint8_t y = vm->reg[inst.reg2];
vm->reg[inst.reg1] = y >> 1;
vm->reg[0xF] = y & 0b1;
break;
}
case LSHIFT: {
uint8_t y = vm->reg[inst.reg2];
vm->reg[inst.reg1] = y << 1;
vm->reg[0xF] = (y & 0b10000000) >> 7;
break;
}
case RAND:
vm->reg[inst.reg1] = rand_byte() & inst.data;
break;
case JMP:
vm->pc = inst.data;
break;
case JMP_REL:
vm->pc = inst.data + vm->reg[0];
break;
case CALL:
if (callstack_push(&vm->stack, vm->pc) < 0) return ERR_STACK_OVERFLOW;
vm->pc = inst.data;
break;
case RET:
int16_t ret_addr = callstack_pop(&vm->stack);
if (ret_addr < 0) return ERR_STACK_UNDERFLOW;
vm->pc = ret_addr;
break;
case SKP_EQ:
if (vm->reg[inst.reg1] == inst.data) vm->pc += 2;
break;
case SKP_NEQ:
if (vm->reg[inst.reg1] != inst.data) vm->pc += 2;
break;
case SKP_EQ_REG:
if (vm->reg[inst.reg1] == vm->reg[inst.reg2]) vm->pc += 2;
break;
case SKP_NEQ_REG:
if (vm->reg[inst.reg1] != vm->reg[inst.reg2]) vm->pc += 2;
break;
case SET_DELAY:
vm->delay = vm->reg[inst.reg1];
break;
case STORE_DELAY:
vm->reg[inst.reg1] = vm->delay;
break;
case SET_SOUND:
vm->sound = (vm->reg[inst.reg1] > 1) ? vm->reg[inst.reg1] : 0;
break;
case WAIT_FOR_KEY:
if (!vm->waiting_for_keypress) {
vm->waiting_for_keypress = true;
}
if (vm->key_released != NULL) {
uint8_t code;
switch (*vm->key_released) {
case SDL_SCANCODE_1:
code = 0x0;
break;
case SDL_SCANCODE_2:
code = 0x1;
break;
case SDL_SCANCODE_3:
code = 0x2;
break;
case SDL_SCANCODE_4:
code = 0x3;
break;
case SDL_SCANCODE_Q:
code = 0x4;
break;
case SDL_SCANCODE_W:
code = 0x5;
break;
case SDL_SCANCODE_E:
code = 0x6;
break;
case SDL_SCANCODE_R:
code = 0x7;
break;
case SDL_SCANCODE_A:
code = 0x8;
break;
case SDL_SCANCODE_S:
code = 0x9;
break;
case SDL_SCANCODE_D:
code = 0xa;
break;
case SDL_SCANCODE_F:
code = 0xb;
break;
case SDL_SCANCODE_Z:
code = 0xc;
break;
case SDL_SCANCODE_X:
code = 0xd;
break;
case SDL_SCANCODE_C:
code = 0xe;
break;
case SDL_SCANCODE_V:
code = 0xf;
break;
default:
puts("Invalid keycode received in wait_for_key!!!");
code = 0x0;
break;
}
vm->key_released = NULL;
vm->waiting_for_keypress = false;
} else {
// If a key has not been released change vm->pc to point at this same instruction
// so the VM loops until a key is released
vm->pc -= 2;
}
break;
case SKP_IF_KEY: {
const uint8_t *state = SDL_GetKeyboardState(NULL);
if (state[KEYS[vm->reg[inst.reg1]]]) {
vm->pc += 2;
}
break;
}
case SKP_IF_NOT_KEY: {
const uint8_t *state = SDL_GetKeyboardState(NULL);
if (!state[KEYS[vm->reg[inst.reg1]]]) {
vm->pc += 2;
}
break;
}
case LOAD_I:
vm->i = inst.data;
break;
case ADD_I:
vm->i += vm->reg[inst.reg1];
break;
case DRAW: {
uint8_t x = vm->reg[inst.reg1];
uint8_t y = vm->reg[inst.reg2];
bool changed = draw_sprite(vm->gfx, x, y, &vm->ram[vm->i], (uint8_t) inst.data);
vm->reg[0xF] = changed ? 0x1 : 0x0;
break;
}
case LOAD_DIGIT_SPRITE:
vm->i = DIGIT_BASE_ADDR + DIGIT_LEN * vm->reg[inst.reg1];
break;
case STORE_BCD: {
uint8_t x = vm->reg[inst.reg1];
vm->ram[vm->i] = x / 100;
vm->ram[vm->i + 1] = (x / 10) % 10;
vm->ram[vm->i + 2] = x % 10;
break;
}
case SAVE_REG:
for (int j = 0; j <= inst.reg1; j++) {
vm->ram[vm->i + j] = vm->reg[j];
}
vm->i += inst.reg1 + 1;
break;
case RESTORE_REG:
for (int j = 0; j <= inst.reg1; j++) {
vm->reg[j] = vm->ram[vm->i + j];
}
vm->i += inst.reg1 + 1;
break;
case INVALID:
default:
printf("!!! INVALID INSTRUCTION %#04x !!!", raw_inst);
return ERR_INVALID;
}
return SUCCESS;
}
tick_result vm_run_frame(chip8_vm* vm) {
tick_result res;
for (int i = 0; i < vm->tpu; i++) {
res = vm_tick(vm);
if (res != SUCCESS) return res;
}
if (vm-> sound > 0) vm->sound--;
if (vm->delay > 0) vm->delay--;
display_screen(vm->gfx);
fps_clock_tick(&vm->clock);
return SUCCESS;
}
uint8_t* read_binary_file(char *path, long* size_out) {
FILE* file = fopen(path, "rb");
if (file == NULL) {
printf("Error opening file \"%s\"", path);
return NULL;
}
// Get file size by seeking to the end of the file
fseek(file, 0, SEEK_END);
long size = ftell(file);
rewind(file);
uint8_t* buf = malloc(size);
fread(buf, sizeof(uint8_t), size, file);
if (ferror(file)) return NULL;
*size_out = size;
return buf;
}
void vm_run(chip8_vm* vm) {
bool quit = false;
SDL_Event e;
tick_result res;
puts("Starting main loop");
while (!quit) {
while (SDL_PollEvent(&e) != 0) {
if (e.type == SDL_QUIT) {
quit = true;
break;
}
if ((e.type == SDL_KEYUP) && vm->waiting_for_keypress) {
for (int i = 0; i < 16; i++) {
if (e.key.keysym.scancode == KEYS[i]) {
vm->key_released = &KEYS[i];
}
}
}
}
res = vm_run_frame(vm);
switch (res) {
case ERR_INVALID:
puts("Invalid instruction!!!");
break;
case ERR_STACK_OVERFLOW:
puts("Stack Overflow!!!");
break;
case ERR_STACK_UNDERFLOW:
puts("Stack Underflow!!!");
break;
default:
break;
}
}
}
int main(int argc, char *argv[]) {
if (argc < 2) {
puts("ERROR: ROM path not given");
return 1;
}
char* rom_path = argv[1];
long rom_size;
uint8_t* rom = read_binary_file(rom_path, &rom_size);
if (rom == NULL) {
printf("Error reading \"%s\"", rom_path);
return 1;
}
sdl_handle h = graphics_init();
clear_screen(&h);
display_screen(&h);
chip8_vm vm;
vm_load_program(&vm, &h, 700, rom, rom_size);
vm_run(&vm);
return 0;
}