-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu.c
396 lines (332 loc) · 8.85 KB
/
cpu.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "cpu.h"
uint8 fonts[5 * 16] = {
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};
void reset(cpu_t *cpu) {
cpu->program_counter = START_ADDRESS;
cpu->i_register = 0;
cpu->stack_pointer = 0;
cpu->delay_timer = 0;
cpu->sound_timer = 0;
cpu->draw_flag = FALSE;
memset(cpu->registers, 0, sizeof(cpu->registers));
memset(cpu->stack, 0, sizeof(cpu->stack));
memset(cpu->memory, 0, sizeof(cpu->memory));
memset(cpu->keypad, 0, sizeof(cpu->keypad));
memset(cpu->video, 0, sizeof(cpu->video));
memcpy(cpu->memory, fonts, sizeof(fonts));
}
int load_rom(cpu_t *cpu, const char *path) {
long rom_file_size = 0;
unsigned long bytes_read = 0;
FILE *rom_file;
rom_file = fopen(path, "rb");
if (rom_file == NULL) {
return -1;
}
fseek(rom_file, 0L, SEEK_END);
rom_file_size = ftell(rom_file);
rewind(rom_file);
bytes_read = fread(cpu->memory + START_ADDRESS, 1, rom_file_size, rom_file);
if (bytes_read != rom_file_size) {
fclose(rom_file);
return -1;
}
fclose(rom_file);
return 0;
}
void push_stack(cpu_t *cpu, uint16 value) {
cpu->stack[cpu->stack_pointer] = value;
cpu->stack_pointer += 1;
}
uint16 pop_stack(cpu_t *cpu) {
cpu->stack_pointer -= 1;
return cpu->stack[cpu->stack_pointer];
}
nibble_t _decode(uint16 opcode) {
nibble_t data;
data.opcode = opcode;
data.t = opcode >> 12;
data.x = opcode >> 8 & 0xF;
data.y = opcode >> 4 & 0xF;
data.n = opcode & 0xF;
data.nn = opcode & 0xFF;
data.nnn = opcode & 0xFFF;
return data;
}
nibble_t _fetch(cpu_t *cpu) {
uint16 opcode = 0;
uint16 high = cpu->memory[cpu->program_counter];
uint16 low = cpu->memory[cpu->program_counter + 1];
opcode = (high << 8) | low;
cpu->program_counter += 2;
return _decode(opcode);
}
void tick(cpu_t *cpu) {
nibble_t op = _fetch(cpu);
/*cpu->opcode = op;*/
execute(cpu, op);
}
void tick_timers(cpu_t *cpu) {
if (cpu->delay_timer > 0) {
cpu->delay_timer -= 1;
}
if (cpu->sound_timer > 0) {
cpu->sound_timer -= 1;
}
}
void push_key(cpu_t *cpu, uint8 key) { cpu->keypad[key] = TRUE; }
void release_key(cpu_t *cpu, uint8 key) { cpu->keypad[key] = FALSE; }
void execute(cpu_t *cpu, nibble_t op) {
/*printf("0x%X, PC:%d\n", op.opcode, cpu->program_counter);*/
switch (op.t) {
case 0x0:
if (op.nn == 0xE0) {
/* CLS */
memset(cpu->video, 0, sizeof(cpu->video));
cpu->draw_flag = TRUE;
} else if (op.nn == 0xEE) {
/* RET */
cpu->program_counter = pop_stack(cpu);
}
break;
case 0x1:
/* JP addr */
cpu->program_counter = op.nnn;
break;
case 0x2:
/* CALL addr*/
push_stack(cpu, cpu->program_counter);
cpu->program_counter = op.nnn;
break;
case 0x3:
/* SE Vx, byte*/
if (cpu->registers[op.x] == op.nn)
cpu->program_counter += 2;
break;
case 0x4:
/* SNE Vx, byte*/
if (cpu->registers[op.x] != op.nn)
cpu->program_counter += 2;
break;
case 0x5:
/* SE Vx, Vy*/
if (cpu->registers[op.x] == cpu->registers[op.y])
cpu->program_counter += 2;
break;
case 0x6:
/* LD Vx, byte*/
cpu->registers[op.x] = op.nn;
break;
case 0x7:
/* ADD Vx, byte */
_add(cpu, op);
break;
case 0x8:
switch (op.n) {
case 0x0:
/* LD Vx, Vy */
cpu->registers[op.x] = cpu->registers[op.y];
case 0x1:
/* OR Vx, Vy */
cpu->registers[op.x] = cpu->registers[op.x] | cpu->registers[op.y];
break;
case 0x2:
/* AND Vx, Vy */
cpu->registers[op.x] = cpu->registers[op.x] & cpu->registers[op.y];
break;
case 0x3:
/* OR Vx, Vy */
cpu->registers[op.x] = cpu->registers[op.x] ^ cpu->registers[op.y];
break;
case 0x4:
/* ADD Vx, Vy (Perform ADD with Carry)*/
_add_with_carry(cpu, op);
break;
case 0x5:
/* SUB Vx, Vy (Perform SUB with Carry)*/
_sub_with_carry(cpu, op);
break;
case 0x6:
/* SHR Vx {, Vy} */
_shr(cpu, op);
break;
case 0x7:
/* SUBN Vx, Vy (Perform SUB with Carry)*/
_subn_with_carry(cpu, op);
break;
case 0xE:
/* SHL Vx {, Vy} */
_shl(cpu, op);
break;
}
break;
case 0x9:
/* SNE Vx, Vy*/
if (cpu->registers[op.x] != cpu->registers[op.y])
cpu->program_counter += 2;
break;
case 0xA:
/* LD I, addr */
cpu->i_register = op.nnn;
break;
case 0xB:
/* JP V0, addr */
cpu->program_counter = cpu->registers[0] + op.nnn;
break;
case 0xC:
/* RND Vx, byte */
_rnd(cpu, op);
break;
case 0xD:
/* DRW Vx, Vy, nibble */
_drw(cpu, op);
cpu->draw_flag = TRUE;
break;
case 0xE:
switch (op.nn) {
case 0x9E:
if (cpu->keypad[cpu->registers[op.x]] == TRUE)
cpu->program_counter += 2;
break;
case 0xA1:
if (cpu->keypad[cpu->registers[op.x]] == FALSE)
cpu->program_counter += 2;
break;
}
break;
case 0xF:
switch (op.nn) {
case 0x07:
cpu->registers[op.x] = cpu->delay_timer;
break;
case 0x0A:
chk_key_pressed(cpu, op);
break;
case 0x15:
cpu->delay_timer = cpu->registers[op.x];
break;
case 0x18:
cpu->sound_timer = cpu->registers[op.x];
break;
case 0x1E:
cpu->i_register += cpu->registers[op.x];
break;
case 0x29:
cpu->i_register = (uint16)(cpu->registers[op.x] * 5);
break;
case 0x33:
_bcd(cpu, op);
break;
case 0x55:
_str_v_to_mem(cpu, op);
break;
case 0x65:
_ld_mem_to_v(cpu, op);
break;
}
break;
default:
printf("Unsupported opcode 0x%X\n", op.opcode);
}
}
void _add(cpu_t *cpu, nibble_t op) {
uint16 res = cpu->registers[op.x] + op.nn;
cpu->registers[op.x] = res;
}
void _add_with_carry(cpu_t *cpu, nibble_t op) {
int res = cpu->registers[op.x] + cpu->registers[op.y];
cpu->registers[0xF] = (res > 255) ? 1 : 0;
cpu->registers[op.x] = res & 0xFF;
}
void _sub_with_carry(cpu_t *cpu, nibble_t op) {
cpu->registers[0xF] = (cpu->registers[op.x] >= cpu->registers[op.y]) ? 1 : 0;
cpu->registers[op.x] = (cpu->registers[op.x] - cpu->registers[op.y]) & 0xFF;
}
void _shr(cpu_t *cpu, nibble_t op) {
cpu->registers[0xF] = cpu->registers[op.x] & 1;
cpu->registers[op.x] >>= 1;
}
void _subn_with_carry(cpu_t *cpu, nibble_t op) {
if (cpu->registers[op.y] >= cpu->registers[op.x])
cpu->registers[0xF] = 1;
else
cpu->registers[0xF] = 0;
cpu->registers[op.x] = cpu->registers[op.y] - cpu->registers[op.x];
}
void _shl(cpu_t *cpu, nibble_t op) {
cpu->registers[0xF] = (cpu->registers[op.x] >> 7) & 1;
cpu->registers[op.x] <<= 1;
}
void _rnd(cpu_t *cpu, nibble_t op) {
uint8 rd_num = rand() % 256; /* Not the best way to obtain a random */
cpu->registers[op.x] = rd_num & op.nn;
}
void _drw(cpu_t *cpu, nibble_t op) {
bool collision = FALSE;
uint8 x_coord = cpu->registers[op.x];
uint8 y_coord = cpu->registers[op.y];
uint8 i, j = 0;
cpu->registers[0xF] = 0;
for (i = 0; i < op.n; i++) {
uint16 addr = cpu->i_register + i;
uint8 sprite_row;
sprite_row = cpu->memory[addr];
for (j = 0; j < 8; j++) {
if ((sprite_row & (0x80 >> j)) != 0) {
uint8 x = (x_coord + j) % SCREEN_WIDTH;
uint8 y = (y_coord + i) % SCREEN_HEIGHT;
uint16 video_index = x + SCREEN_WIDTH * y;
if (cpu->video[video_index] == TRUE)
collision = TRUE;
cpu->video[video_index] ^= TRUE;
}
}
}
cpu->registers[0xF] = collision ? 1 : 0;
}
void chk_key_pressed(cpu_t *cpu, nibble_t opcode) {
bool pressed = FALSE;
int i = 0;
for (; i < 16; i++) {
if (cpu->keypad[i] == TRUE) {
pressed = TRUE;
cpu->registers[opcode.x] = cpu->keypad[i];
break;
}
}
if (!pressed)
cpu->program_counter -= 2;
}
void _bcd(cpu_t *cpu, nibble_t op) {
int vx = cpu->registers[op.x]; /* Cast Vx to float */
uint8 hundreds = vx / 100;
uint8 tens = (vx / 10) % 10;
uint8 ones = vx % 10;
cpu->memory[cpu->i_register] = hundreds;
cpu->memory[cpu->i_register + 1] = tens;
cpu->memory[cpu->i_register + 2] = ones;
}
void _str_v_to_mem(cpu_t *cpu, nibble_t op) {
uint8 i = 0;
uint8 x = op.x;
for (; i <= x; i++) {
cpu->memory[cpu->i_register + i] = cpu->registers[i];
}
}
void _ld_mem_to_v(cpu_t *cpu, nibble_t op) {
uint8 i = 0;
uint8 x = op.x;
for (; i <= x; i++) {
cpu->registers[i] = cpu->memory[cpu->i_register + i];
}
}