-
Notifications
You must be signed in to change notification settings - Fork 0
/
chip8.c
378 lines (320 loc) · 10.1 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
#include "chip8.h"
static const u8 chip8_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
};
int chip8_init(chip8* const m) {
m->memory = calloc(0x1000, sizeof(u8));
m->V = calloc(16, sizeof(u8));
m->I = 0;
m->pc = 0x200;
m->sp = 0;
m->stack = calloc(16, sizeof(u8));
m->screen = calloc(0x800, sizeof(u8));
m->sound_timer = 0;
m->delay_timer = 0;
m->key_pressed = calloc(16, sizeof(u8));
m->draw_flag = false;
memcpy(&m->memory[0x50], chip8_fontset, sizeof(chip8_fontset));
return 0;
}
void chip8_quit(chip8* const m) {
free(m->memory);
free(m->V);
free(m->stack);
free(m->screen);
free(m->key_pressed);
}
int chip8_load_file_in_memory(chip8* const m, const char* filename) {
FILE* f = fopen(filename, "rb");
if (f == NULL) {
printf("error: can't open ROM file.\n");
return 1;
}
fseek(f, 0, SEEK_END);
unsigned long file_size = ftell(f);
rewind(f);
if (file_size > 0x1000 - 0x200) {
printf("error: ROM file too big to fit in memory\n");
return 1;
}
if (fread(&m->memory[0x200], 1, file_size, f) != file_size) {
printf("error: while reading ROM file\n");
return 1;
}
fclose(f);
return 0;
}
void chip8_step(chip8* const m) {
const u16 opcode = (m->memory[m->pc] << 8) | m->memory[m->pc + 1];
m->pc += 2;
const u8 x = (opcode & 0x0F00) >> 8;
const u8 y = (opcode & 0x00F0) >> 4;
const u8 n = opcode & 0x000F;
const u8 nn = opcode & 0x00FF;
const u16 nnn = opcode & 0x0FFF;
switch (opcode & 0xF000)
{
case 0x0000:
switch (n) {
case 0x0000:
// 00E0 Clears the screen.
memset(m->screen, 0, 0x800);
m->draw_flag = true;
break;
case 0x000E:
// 00EE Returns from a subroutine.
m->sp -= 1;
m->pc = m->stack[m->sp];
break;
}
break;
case 0x1000:
// 1NNN Jumps to address NNN.
m->pc = nnn;
break;
case 0x2000:
// 2NNN Calls subroutine at NNN.
m->stack[m->sp] = m->pc;
m->sp += 1;
m->pc = nnn;
break;
case 0x3000:
// 3XNN Skips the next instruction if VX equals NN.
if (m->V[x] == nn) {
m->pc += 2;
}
break;
case 0x4000:
// 4XNN Skips the next instruction if VX doesn't equal NN.
if (m->V[x] != nn) {
m->pc += 2;
}
break;
case 0x5000:
// 5XY0 Skips the next instruction if VX equals VY.
if (m->V[x] == m->V[y]) {
m->pc += 2;
}
break;
case 0x6000:
// 6XNN Sets VX to NN.
m->V[x] = nn;
break;
case 0x7000:
// 7XNN Adds NN to VX.
m->V[x] += nn;
break;
case 0x8000: {
switch (n) {
case 0x0000:
// 8XY0 Sets VX to the value of VY.
m->V[x] = m->V[y];
break;
case 0x0001:
// 8XY1 Sets VX to VX or VY.
m->V[x] |= m->V[y];
break;
case 0x0002:
// 8XY2 Sets VX to VX and VY.
m->V[x] &= m->V[y];
break;
case 0x0003:
// 8XY3 Sets VX to VX xor VY.
m->V[x] ^= m->V[y];
break;
case 0x0004:
// 8XY4 Adds VY to VX. VF is set to 1 when there's
// a carry, and to 0 when there isn't.
m->V[0xF] = m->V[x] + m->V[y] > 0xFF;
m->V[x] += m->V[y];
break;
case 0x0005:
// 8XY5 VY is subtracted from VX. VF is set to 0 when
// there's a borrow, and 1 when there isn't.
m->V[0xF] = m->V[x] >= m->V[y];
m->V[x] -= m->V[y];
break;
case 0x0006:
// 8XY6 Shifts VX right by one. VF is set to the value
// of the least significant bit of VX before the shift.
// On the original interpreter, the value of VY is
// shifted, and the result is stored into VX. On current
// implementations, Y is ignored
m->V[0xF] = m->V[x] & 1;
m->V[x] >>= 1;
break;
case 0x0007:
// 8XY7 Sets VX to VY minus VX. VF is set to 0 when
// there's a borrow, and 1 when there isn't.
m->V[0xF] = m->V[x] <= m->V[y];
m->V[x] = m->V[y] - m->V[x];
break;
case 0x000E:
// 8XYE Shifts VX left by one. VF is set to the value
// of the most significant bit of VX before the shift.
// On the original interpreter, the value of VY is
// shifted, and the result is stored into VX. On current
// implementations, Y is ignored
m->V[0xF] = m->V[x] >> 7;
m->V[x] <<= 1;
break;
}
} break;
case 0x9000:
// 9XY0 Skips the next instruction if VX doesn't equal VY.
if (m->V[x] != m->V[y]) {
m->pc += 2;
}
break;
case 0xA000:
// ANNN Sets I to the address NNN.
m->I = nnn;
break;
case 0xB000:
// BNNN Jumps to the address NNN plus V0.
m->pc = nnn + m->V[0x0];
break;
case 0xC000:
// CXNN Sets VX to a random number, masked by NN.
m->V[x] = rand() & nn;
break;
case 0xD000: {
// DXYN Sprites stored in memory at location in index register,
// maximum 8bits wide. Wraps around the screen. If when drawn,
// clears a pixel, register VF is set to 1 otherwise it is zero.
// All drawing is XOR drawing (i.e. it toggles the screen pixels)
m->V[0xF] = 0;
for (int yline = 0; yline < n; yline++) {
u8 sprite = m->memory[m->I + yline];
for (int xline = 0; xline < 8; xline++) {
int px = (m->V[x] + xline) & 63;
int py = (m->V[y] + yline) & 31;
int pos = 64 * py + px;
int pixel = (sprite >> (7 - xline)) & 0x1;
m->V[0xF] |= (m->screen[pos] & pixel);
m->screen[pos] ^= pixel;
}
}
m->draw_flag = true;
} break;
case 0xE000:
switch (n) {
case 0x000E:
// EX9E Skips the next instruction if the key stored in VX
// is pressed.
if (m->key_pressed[m->V[x]]) {
m->pc += 2;
}
break;
case 0x001:
// EXA1 Skips the next instruction if the key stored in VX
// isn't pressed.
if (!m->key_pressed[m->V[x]]) {
m->pc += 2;
}
break;
}
break;
case 0xF000:
switch (nn) {
case 0x0007:
// FX07 Sets VX to the value of the delay timer.
m->V[x] = m->delay_timer;
break;
case 0x000A: {
// FX0A A key press is awaited, and then stored in VX.
bool key_pressed = false;
for (int i = 0; i < 16; i++) {
if (m->key_pressed[i] == true) {
m->V[x] = i;
key_pressed = true;
break;
}
}
// continue executing this opcode until a key is pressed:
if (!key_pressed) {
m->pc -= 2;
return;
}
} break;
case 0x0015:
// FX15 Sets the delay timer to VX.
m->delay_timer = m->V[x];
break;
case 0x0018:
// FX18 Sets the sound timer to VX.
m->sound_timer = m->V[x];
break;
case 0x001E:
// FX1E Adds VX to I.
// (VF is set to 1 when range overflow (I+VX>0xFFF),
// and 0 when there isn't. This is an undocumented feature
// of the CHIP-8 and used by Spacefight 2091! game.)
m->V[0xF] = m->I + m->V[x] > 0xFFF;
m->I += m->V[x];
break;
case 0x0029:
// FX29 Sets I to the location of the sprite for the
// character in VX. Characters 0-F (in hexadecimal)
// are represented by a 4x5 font.
m->I = 0x50 + (m->V[x] & 0xF) * 5;
break;
case 0x0033:
// FX33 Stores the Binary-coded decimal representation
// of VX, with the most significant of three digits
// at the address in I, the middle digit at I plus 1,
// and the least significant digit at I plus 2.
// (In other words, take the decimal representation of VX,
// place the hundreds digit in memory at location in I,
// the tens digit at location I+1, and the ones digit
// at location I+2.)
m->memory[m->I + 2] = m->V[x] % 10;
m->memory[m->I + 1] = (m->V[x] / 10) % 10;
m->memory[m->I] = (m->V[x] / 100);
break;
case 0x0055:
// FX55 Stores V0 to VX in memory starting at address I
for (int i = 0; i <= x; ++i) {
m->memory[m->I + i] = m->V[i];
}
// I += x + 1;
break;
case 0x0065:
// FX65 Fills V0 to VX with values from memory
// starting at address I.
for (int i = 0; i <= x; ++i) {
m->V[i] = m->memory[m->I + i];
}
// I += x + 1;
break;
}
break;
default:
printf("Unknown opcode: %d\n", opcode);
break;
}
if (m->delay_timer > 0) {
m->delay_timer--;
}
if (m->sound_timer > 0) {
if (m->sound_timer == 1) {
printf("beep"); // '\a'
}
m->sound_timer--;
}
}