-
Notifications
You must be signed in to change notification settings - Fork 1
/
chip8.c
279 lines (256 loc) · 7.04 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
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <math.h>
#include <time.h>
#ifdef _WIN32
#include <winsock.h>
#else
#include <arpa/inet.h>
#include <unistd.h>
#endif
#include "util.h"
#include "chip8.h"
chip8_vm vm;
#define FONT_OFFSET 0x100
static u8 font[] = {
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, 0x80, 0x80, 0x80, 0xE0, // D
0xF0, 0x80, 0xF0, 0x80, 0xF0, // E
0xF0, 0x80, 0xF0, 0x80, 0x80 // F
};
void init_vm(void) {
vm.PC = 0x200; // start of chip8 programs
// write fonts into memory
for (int i = 0; i < sizeof(font); i++) {
vm.ram[FONT_OFFSET + i] = font[i];
}
}
void load_rom(char *path) {
memset(&vm, 0, sizeof(chip8_vm));
int fd = open(path, O_RDONLY);
FILE* f = fdopen(fd, "r");
fseek(f, 0, SEEK_END);
off_t fsize = ftell(f);
rewind(f);
vm.ram = (u8*)malloc(0xFFF);
memset(vm.ram, 0, 0xFFF);
#ifdef _WIN32
size_t res = read(fd, &vm.ram[0x200], fsize);
#else
size_t res = fread(&vm.ram[0x200], 1, fsize, f);
#endif
if (res != fsize) {
printf("err: failed to read rom fully\n");
}
fclose(f); close(fd);
}
void vm_step(void) {
// read 2 bytes and convert to big-endian
u16 in = htons(vm.ram[vm.PC + 0x1]<<8 | vm.ram[vm.PC]);
// in>>12&0xF 0xFFFF
// ^
// in>>8&0xF 0xFFFF
// ^
// in>>4&0xF 0xFFFF
// ^
// in&0xF 0xFFFF
// ^
// in&0xFF 0xFFFF
// ^^
// in&0xFFF 0xFFFF
// ^^^
switch ((in>>12&0xF)) {
case 0x0:
switch (in&0xFF) {
case 0xE0: // CLS
memset(&vm.screen, 0, sizeof(vm.screen));
break;
case 0xEE: // RET
vm.PC = vm.stack[vm.SP--];
break;
}
break;
case 0x1: // JP addr
vm.PC = (in&0xFFF) - 0x2;
break;
case 0x2: // CALL addr
vm.stack[++vm.SP] = vm.PC;
vm.PC = (in&0xFFF) - 0x2;
break;
case 0x3: // SE Vx, byte
if (vm.Vx[in>>8&0xF] == (in&0xFF)) {
vm.PC += 0x2;
}
break;
case 0x4: // SNE Vx, byte
if (vm.Vx[in>>8&0xF] != (in&0xFF)) {
vm.PC += 0x2;
}
break;
case 0x5: // SE Vx, Vy
if (vm.Vx[in>>8&0xF] == vm.Vx[in>>4&0xF]) {
vm.PC += 0x2;
}
break;
case 0x6: // LD Vx, byte
vm.Vx[in>>8&0xF] = in&0xFF;
break;
case 0x7: // ADD Vx, byte
vm.Vx[in>>8&0xF] += in&0xFF;
break;
case 0x8:
switch(in&0xF) {
case 0x0: // LD Vx, Vy
vm.Vx[in>>8&0xF] = vm.Vx[in>>4&0xF];
break;
case 0x1: // OR Vx, Vy
vm.Vx[in>>8&0xF] |= vm.Vx[in>>4&0xF];
break;
case 0x2: // AND Vx, Vy
vm.Vx[in>>8&0xF] &= vm.Vx[in>>4&0xF];
break;
case 0x3: // XOR Vx, Vy
vm.Vx[in>>8&0xF] ^= vm.Vx[in>>4&0xF];
break;
case 0x4: // ADD Vx, Vy
if ((vm.Vx[in>>8&0xF] + vm.Vx[in>>4&0xF]) > 0xFF) {
vm.Vx[0xF] = 1;
} else {
vm.Vx[0xF] = 0;
}
vm.Vx[in>>8&0xF] = (vm.Vx[in>>8&0xF] + vm.Vx[in>>4&0xF])&0xFF;
break;
case 0x5: // SUB Vx, Vy
if (vm.Vx[in>>8&0xF] > vm.Vx[in>>4&0xF]) {
vm.Vx[0xF] = 1;
} else {
vm.Vx[0xF] = 0;
}
vm.Vx[in>>8&0xF] -= vm.Vx[in>>4&0xF];
break;
case 0x6: // SHR Vx {, Vy}
vm.Vx[0xF] = (vm.Vx[in>>8&0xF] & 1);
// TODO: toggle for Vx /= 2
vm.Vx[in>>8&0xF] = vm.Vx[in>>8&0xF]>>1;
break;
case 0x7: // SUBN Vx, Vy
if (vm.Vx[in>>8&0xF] < vm.Vx[in>>4&0xF]) {
vm.Vx[0xF] = 1;
} else {
vm.Vx[0xF] = 0;
}
vm.Vx[in>>8&0xF] = vm.Vx[in>>4&0xF] - vm.Vx[in>>8&0xF];
break;
case 0xE: // SHL Vx {, Vy}
vm.Vx[0xF] = ((vm.Vx[in>>8&0xF]>>7) & 1);
// TODO: toggle for Vx *= 2;
vm.Vx[in>>8&0xF] = vm.Vx[in>>8&0xF]<<1;
break;
}
break;
case 0x9: // SNE Vx, Vy
if (vm.Vx[(in>>8&0xF)] != vm.Vx[(in>>4&0xF)]) {
vm.PC += 0x2;
}
break;
case 0xA: // LD I, addr
vm.I = in&0xFFF;
break;
case 0xB: // JP V0, addr
vm.PC = ((in&0xFFF) + vm.Vx[0x0]) - 0x2;
break;
case 0xC: // RND Vx, byte
vm.Vx[in>>8&0xF] = (rand() % 255) & in&0xFF;
break;
case 0xD: // DRW Vx, Vy, nibble
vm.Vx[0xF] = 0;
for (u32 i = 0; i < (in&0xF); i++) {
for (u32 s = 0; s < 8; s++) {
// 0x80 = b10000000
u8 pix = (vm.ram[vm.I + i] & (0x80 >> s)) != 0;
if (pix) {
u8 y = (vm.Vx[in>>4&0xF] + i)&0x1F;
u8 x = (vm.Vx[in>>8&0xF] + s)&0x3F;
u32 byte = y * 64 + x;
// put the single '1' bit in the position
// that is relative to the pixel
u8 bit = 1 << (byte & 0x07);
// byte>>3 ~ byte / 8
// used to get the index of the byte
// containing the bit to change
if (vm.screen[byte>>3] & bit) {
vm.Vx[0xF] = 1;
}
vm.screen[byte>>3] ^= bit;
}
}
}
break;
case 0xE: switch (in&0xFF) {
case 0x9E: // SKP Vx
if (vm.keyboard[vm.Vx[in>>8&0xF]]) {
vm.PC += 0x2;
}
break;
case 0xA1: // SKNP Vx
if (!vm.keyboard[vm.Vx[in>>8&0xF]]) {
vm.PC += 0x2;
}
break;
}
break;
case 0xF:
switch (in&0xFF) {
case 0x07: // LD Vx, DT
vm.Vx[in>>8&0xF] = vm.DT;
break;
case 0x0A: // LD Vx, K
//vm.Vx[in>>8&0xF] = 0xA;//ui_get_key(true);
break;
case 0x15: // LD DT, Vx
vm.DT = vm.Vx[in>>8&0xF];
break;
case 0x18: // LD ST, Vx
vm.ST = vm.Vx[in>>8&0xF];
break;
case 0x1E: // ADD I, Vx
vm.I += vm.Vx[in>>8&0xF];
break;
case 0x29: // LD F, Vx
vm.I = FONT_OFFSET + (vm.Vx[in>>8&0xF] * 5);
break;
case 0x33: // LD B, Vx
vm.ram[vm.I] = (vm.Vx[in>>8&0xF] / 100);
vm.ram[vm.I+0x1] = ((vm.Vx[in>>8&0xF] % 100)/10);
vm.ram[vm.I+0x2] = (vm.Vx[in>>8&0xF] % 10);
break;
case 0x55: // LD [I], Vx
for (u8 i = 0; i <= (in>>8&0xF); i++) {
vm.ram[(vm.I) + i] = vm.Vx[i];
}
break;
case 0x65: // LD Vx, [I]
for (u8 i = 0; i <= (in>>8&0xF); i++) {
vm.Vx[i] = vm.ram[(vm.I) + i];
}
break;
}
break;
}
vm.PC += 0x2;
}