This repository has been archived by the owner on May 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chip8.c
421 lines (351 loc) · 9.42 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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <Windows.h>
#include "chip8.h"
uint8_t fontset[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
};
void init(struct Chip8* chip8)
{
chip8->pc = 0x200;
chip8->opcode = 0;
chip8->I = 0;
chip8->sp = 0;
chip8->instruction_num = 0;
for (int i = 0; i < 2048; i++)
chip8->graphics[i] = 0;
for (int i = 0; i < 16; i++)
chip8->stack[i] = 0;
for (int i = 0; i < 16; i++)
chip8->key[i] = chip8->V[i] = 0;
for (int i = 0; i < 4096; i++)
chip8->memory[i] = 0;
for (int i = 0; i < 80; i++)
chip8->memory[i] = fontset[i];
chip8->delay_timer = 0;
chip8->sound_timer = 0;
chip8->draw = 1;
srand(time(NULL));
}
void emulate_cycle(struct Chip8* chip8)
{
chip8->opcode = chip8->memory[chip8->pc] << 8 | chip8->memory[chip8->pc + 1];
switch (chip8->opcode & 0xF000)
{
case 0x0000:
switch (chip8->opcode & 0x000F)
{
case 0x0000:
for (int i = 0; i < 2048; i++)
chip8->graphics[i] = 0x0;
chip8->draw = 1;
chip8->pc += 2;
break;
case 0x000E:
chip8->sp--;
chip8->pc = chip8->stack[chip8->sp];
chip8->pc += 2;
break;
}
break;
case 0x1000:
chip8->pc = chip8->opcode & 0x0FFF;
break;
case 0x2000:
chip8->stack[chip8->sp] = chip8->pc;
chip8->sp++;
chip8->pc = chip8->opcode & 0x0FFF;
break;
case 0x3000:
if (chip8->V[(chip8->opcode & 0x0F00) >> 8] == (chip8->opcode & 0x00FF))
chip8->pc += 4;
else
chip8->pc += 2;
break;
case 0x4000:
if (chip8->V[(chip8->opcode & 0x0F00) >> 8] != (chip8->opcode & 0x00FF))
chip8->pc += 4;
else
chip8->pc += 2;
break;
case 0x5000:
if (chip8->V[(chip8->opcode & 0x0F00) >> 8] == chip8->V[(chip8->opcode & 0x00F0) >> 4])
chip8->pc += 4;
else
chip8->pc += 2;
break;
case 0x6000:
chip8->V[(chip8->opcode & 0x0F00) >> 8] = chip8->opcode & 0x00FF;
chip8->pc += 2;
break;
case 0x7000:
chip8->V[(chip8->opcode & 0x0F00) >> 8] += chip8->opcode & 0x00FF;
chip8->pc += 2;
break;
case 0x8000:
switch (chip8->opcode & 0x000F)
{
case 0x0000:
chip8->V[(chip8->opcode & 0x0F00) >> 8] = chip8->V[(chip8->opcode & 0x00F0) >> 4];
chip8->pc += 2;
break;
case 0x0001:
chip8->V[(chip8->opcode & 0x0F00) >> 8] |= chip8->V[(chip8->opcode & 0x00F0) >> 4];
chip8->pc += 2;
break;
case 0x0002:
chip8->V[(chip8->opcode & 0x0F00) >> 8] &= chip8->V[(chip8->opcode & 0x00F0) >> 4];
chip8->pc += 2;
break;
case 0x0003:
chip8->V[(chip8->opcode & 0x0F00) >> 8] ^= chip8->V[(chip8->opcode & 0x00F0) >> 4];
chip8->pc += 2;
break;
case 0x0004:
if (chip8->V[(chip8->opcode & 0x00F0) >> 4] > (0xFF - chip8->V[(chip8->opcode & 0x0F00) >> 8]))
chip8->V[0xF] = 1;
else
chip8->V[0xF] = 0;
chip8->V[(chip8->opcode & 0x0F00) >> 8] += chip8->V[(chip8->opcode & 0x00F0) >> 4];
chip8->pc += 2;
break;
case 0x0005:
if (chip8->V[(chip8->opcode & 0x00F0) >> 4] > chip8->V[(chip8->opcode & 0x0F00) >> 8])
chip8->V[0xF] = 0;
else
chip8->V[0xF] = 1;
chip8->V[(chip8->opcode & 0x0F00) >> 8] -= chip8->V[(chip8->opcode & 0x00F0) >> 4];
chip8->pc += 2;
break;
case 0x0006:
chip8->V[0xF] = chip8->V[(chip8->opcode & 0x0F00) >> 8] & 0x1;
chip8->V[(chip8->opcode & 0x0F00) >> 8] >>= 1;
chip8->pc += 2;
break;
case 0x0007:
if (chip8->V[(chip8->opcode & 0x0F00) >> 8] > chip8->V[(chip8->opcode & 0x00F0) >> 4]) // chip8->VY-chip8->VX
chip8->V[0xF] = 0;
else
chip8->V[0xF] = 1;
chip8->V[(chip8->opcode & 0x0F00) >> 8] = chip8->V[(chip8->opcode & 0x00F0) >> 4] - chip8->V[(chip8->opcode & 0x0F00) >> 8];
chip8->pc += 2;
break;
case 0x000E:
chip8->V[0xF] = chip8->V[(chip8->opcode & 0x0F00) >> 8] >> 7;
chip8->V[(chip8->opcode & 0x0F00) >> 8] <<= 1;
chip8->pc += 2;
break;
}
break;
case 0x9000:
if (chip8->V[(chip8->opcode & 0x0F00) >> 8] != chip8->V[(chip8->opcode & 0x00F0) >> 4])
chip8->pc += 4;
else
chip8->pc += 2;
break;
case 0xA000:
chip8->I = chip8->opcode & 0x0FFF;
chip8->pc += 2;
break;
case 0xB000:
chip8->pc = (chip8->opcode & 0x0FFF) + chip8->V[0];
break;
case 0xC000:
chip8->V[(chip8->opcode & 0x0F00) >> 8] = (rand() % 0xFF) & (chip8->opcode & 0x00FF);
chip8->pc += 2;
break;
case 0xD000:
{
uint16_t x = chip8->V[(chip8->opcode & 0x0F00) >> 8];
uint16_t y = chip8->V[(chip8->opcode & 0x00F0) >> 4];
uint16_t height = chip8->opcode & 0x000F;
uint16_t pixel;
chip8->V[0xF] = 0;
for (int yline = 0; yline < height; yline++)
{
pixel = chip8->memory[chip8->I + yline];
for (int xline = 0; xline < 8; xline++)
{
if ((pixel & (0x80 >> xline)) != 0)
{
if (chip8->graphics[(x + xline + ((y + yline) * 64))] == 1)
{
chip8->V[0xF] = 1;
}
chip8->graphics[x + xline + ((y + yline) * 64)] ^= 1;
}
}
}
chip8->draw = 1;
chip8->pc += 2;
}
break;
case 0xE000:
switch (chip8->opcode & 0x00FF)
{
case 0x009E:
if (chip8->key[chip8->V[(chip8->opcode & 0x0F00) >> 8]] != 0)
chip8->pc += 4;
else
chip8->pc += 2;
break;
case 0x00A1:
if (chip8->key[chip8->V[(chip8->opcode & 0x0F00) >> 8]] == 0)
chip8->pc += 4;
else
chip8->pc += 2;
break;
}
break;
case 0xF000:
switch (chip8->opcode & 0x00FF)
{
case 0x0007:
chip8->V[(chip8->opcode & 0x0F00) >> 8] = chip8->delay_timer;
chip8->pc += 2;
break;
case 0x000A:
{
uint8_t keyPress = 0;
for (int i = 0; i < 16; i++)
{
if(chip8->key[i] != 0)
{
chip8->V[(chip8->opcode & 0x0F00) >> 8] = i;
keyPress = 1;
}
}
if (!keyPress)
return;
chip8->pc += 2;
}
break;
case 0x0015:
chip8->delay_timer = chip8->V[(chip8->opcode & 0x0F00) >> 8];
chip8->pc += 2;
break;
case 0x0018:
chip8->sound_timer = chip8->V[(chip8->opcode & 0x0F00) >> 8];
chip8->pc += 2;
break;
case 0x001E:
if (chip8->I + chip8->V[(chip8->opcode & 0x0F00) >> 8] > 0xFFF) // chip8->VF is set to 1 when range overflow (chip8->I+chip8->VX>0xFFF), and 0 when there isn't.
chip8->V[0xF] = 1;
else
chip8->V[0xF] = 0;
chip8->I += chip8->V[(chip8->opcode & 0x0F00) >> 8];
chip8->pc += 2;
break;
case 0x0029:
chip8->I = chip8->V[(chip8->opcode & 0x0F00) >> 8] * 0x5;
chip8->pc += 2;
break;
case 0x0033:
chip8->memory[chip8->I] = chip8->V[(chip8->opcode & 0x0F00) >> 8] / 100;
chip8->memory[chip8->I + 1] = (chip8->V[(chip8->opcode & 0x0F00) >> 8] / 10) % 10;
chip8->memory[chip8->I + 2] = (chip8->V[(chip8->opcode & 0x0F00) >> 8] % 100) % 10;
chip8->pc += 2;
break;
case 0x0055:
for (int i = 0; i <= ((chip8->opcode & 0x0F00) >> 8); i++)
chip8->memory[chip8->I + i] = chip8->V[i];
chip8->I += ((chip8->opcode & 0x0F00) >> 8) + 1;
chip8->pc += 2;
break;
case 0x0065:
for (int i = 0; i <= ((chip8->opcode & 0x0F00) >> 8); i++)
chip8->V[i] = chip8->memory[chip8->I + i];
chip8->I += ((chip8->opcode & 0x0F00) >> 8) + 1;
chip8->pc += 2;
break;
}
break;
}
chip8->instruction_num++;
if (chip8->instruction_num == 9)
{
if (chip8->delay_timer > 0)
chip8->delay_timer--;
if (chip8->sound_timer > 0)
{
if (chip8->sound_timer == 1)
Beep(750, 160);
chip8->sound_timer--;
}
chip8->instruction_num = 0;
}
}
void debug_render(struct Chip8* chip8)
{
for (int y = 0; y < 32; y++)
{
for (int x = 0; x < 64; x++)
{
if (chip8->graphics[(y * 64) + x] == 0)
printf("O");
else
printf(" ");
}
printf("\n");
}
printf("\n");
}
void load_rom(struct Chip8* chip8, const char * filename)
{
init(chip8);
printf("Loading: %s\n", filename);
FILE* file = fopen(filename, "rb");
if (file == NULL)
{
fputs("File error", stderr);
getchar();
exit(-1);
}
fseek(file, 0, SEEK_END);
long fsize = ftell(file);
rewind(file);
printf("ROM size: %dB\n", (int)fsize);
uint8_t* buffer = (uint8_t*)malloc(fsize);
if (buffer == NULL)
{
fputs("Memory error", stderr);
getchar();
exit(-1);
}
size_t result = fread(buffer, 1, fsize, file);
if (result != fsize)
{
fputs("Reading error", stderr);
getchar();
exit(-1);
}
if ((4096 - 512) > fsize)
{
for(int i = 0; i < fsize; i++)
chip8->memory[i + 512] = buffer[i];
}
else
{
printf("Error: ROM too big for memory\n");
getchar();
exit(-1);
}
fclose(file);
free(buffer);
}