-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchip8.c
More file actions
328 lines (278 loc) · 6.53 KB
/
Copy pathchip8.c
File metadata and controls
328 lines (278 loc) · 6.53 KB
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
// (c) 2018 Zachary Wells
// This code is licensed under MIT license (see LICENSE for details)
#include "chip8.h"
#include <memory.h>
#include <stdlib.h>
#include <stdio.h>
#define PROGRAM_START 0x200
static uint8_t font[80] = {
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
};
void chip8_init(chip8_t *emulator) {
memset(emulator->memory, 0, sizeof(emulator->memory));
emulator->PC = PROGRAM_START;
memcpy(emulator->font, font, 80);
}
void chip8_load_rom(chip8_t *emulator, const uint16_t *rom, size_t size) {
chip8_init(emulator);
memcpy(emulator->memory + PROGRAM_START, rom, size);
}
void chip8_cycle(chip8_t *emulator) {
uint16_t opcode = emulator->memory[emulator->PC] << 8 | emulator->memory[emulator->PC + 1];
#define DELAY (emulator->delay)
#define SOUND (emulator->sound)
#define PC (emulator->PC)
#define SP (emulator->SP)
#define I (emulator->I)
#define X ((opcode & 0x0F00) >> 8)
#define Y ((opcode & 0x00F0) >> 4)
#define VX (emulator->V[X])
#define VY (emulator->V[Y])
#define V0 (emulator->V[0x0])
#define VF (emulator->V[0xF])
#define N (opcode & 0x000F)
#define NN (opcode & 0x00FF)
#define NNN (opcode & 0x0FFF)
switch (opcode & 0xF000) {
case 0x0000: {
switch (opcode) {
case 0x00E0: {
memset(emulator->vram, 0, CHIP8_VRAM_SIZE);
goto increment;
} break;
case 0x00EE: {
PC = emulator->stack[--SP];
goto increment;
} break;
}
//call RCA 1802
} break;
case 0x1000: {
PC = NNN;
return;
} break;
case 0x2000: {
emulator->stack[SP++] = PC;
PC = NNN;
return;
} break;
case 0x3000: {
if (VX == NN) {
PC += 2;
}
goto increment;
} break;
case 0x4000: {
if (VX != NN) {
PC += 2;
}
goto increment;
} break;
case 0x5000: {
if (VX == VY) {
PC += 2;
}
goto increment;
} break;
case 0x6000: {
VX = NN;
goto increment;
} break;
case 0x7000: {
VX += NN;
goto increment;
} break;
case 0x8000: {
switch (opcode & 0xF) {
case 0x0: {
VX = VY;
goto increment;
} break;
case 0x1: {
VX = VX | VY;
goto increment;
} break;
case 0x2: {
VX = VX & VY;
goto increment;
} break;
case 0x3: {
VX = VX ^ VY;
goto increment;
} break;
case 0x4: {
uint16_t temp = (uint16_t)VX + (uint16_t)VY;
VF = temp > 0xFF;
VX = temp;
goto increment;
} break;
case 0x5: {
VF = VX >= VY;
VX -= VY;
goto increment;
} break;
case 0x6: {
VF = VX & 1;
VX >>= 1;
goto increment;
} break;
case 0x7: {
uint16_t temp = (uint16_t)VY - (uint16_t)VX;
VF = !(temp > 0xFF);
VX = temp;
goto increment;
} break;
case 0xE: {
VF = VX >> 7;
VX <<= 1;
goto increment;
} break;
}
} break;
case 0x9000: {
if (VX != VY) {
PC += 2;
}
goto increment;
} break;
case 0xA000: {
I = NNN;
goto increment;
} break;
case 0xB000: {
PC = NNN + V0;
return;
} break;
case 0xC000: {
VX = (rand() % 0xFF) & NN;
} break;
case 0xD000: {
VF = 0;
for (uint16_t y = VY; y < (N + (uint16_t)VY); y++) {
uint8_t *row = emulator->vram + y * 8;
uint8_t texel = emulator->memory[I + (y - VY)];
row += VX / 8;
uint8_t byte0 = ((uint16_t)texel >> (VX % 8) & 0x00FF);
uint8_t byte1 = ((uint16_t)texel << (8 - VX % 8) & 0x00FF);
VF = (row[0] & byte0) || VF;
row[0] ^= byte0;
VF = (row[1] & byte1) || VF;
row[1] ^= byte1;
}
goto increment;
} break;
case 0xE000: {
switch (opcode & 0xFF) {
case 0x9E: {
if ((emulator->keypad.value >> VX) & 1) {
PC += 2;
}
goto increment;
} break;
case 0xA1: {
if (!((emulator->keypad.value >> VX) & 1)) {
PC += 2;
}
goto increment;
} break;
} break;
} break;
case 0xF000: {
switch (opcode & 0xFF) {
case 0x07: {
VX = DELAY;
goto increment;
} break;
case 0x0A: {
uint16_t keypad = emulator->keypad.value;
uint16_t keypad_prev = emulator->keypad_previous.value;
if (keypad != keypad_prev) {
VX = 0;
while(1) {
if ((keypad & 1) != (keypad_prev & 1)) {
break;
}
keypad >>= 1;
keypad_prev >>= 1;
VX++;
}
goto increment;
}
return;
} break;
case 0x15: {
DELAY = VX;
goto increment;
} break;
case 0x18: {
SOUND = VX;
goto increment;
} break;
case 0x1E: {
I += VX;
goto increment;
} break;
case 0x29: {
I = (VX & 0xF) * 5;
goto increment;
} break;
case 0x33: {
emulator->memory[I + 0] = VX / 100 % 10;
emulator->memory[I + 1] = VX / 10 % 10;
emulator->memory[I + 2] = VX / 1 % 10;
goto increment;
} break;
case 0x55: {
memcpy(emulator->memory + I, emulator->V, X + 1);
goto increment;
} break;
case 0x65: {
memcpy(emulator->V, emulator->memory + I, X + 1);
goto increment;
} break;
} break;
} break;
}
increment:
PC += 2;
#undef DELAY
#undef SOUND
#undef PC
#undef SP
#undef I
}
void chip8_update(chip8_t *emulator, double deltaTime) {
emulator->_timer += deltaTime;
emulator->_cycle += deltaTime;
double timerHz = 1.0 / 60.0;
double clockHz = 1.0 / 500.0;
while (emulator->_timer >= timerHz) {
if (emulator->delay) {
emulator->delay--;
}
if (emulator->sound) {
emulator->sound--;
}
emulator->_timer -= timerHz;
}
while (emulator->_cycle >= clockHz) {
chip8_cycle(emulator);
emulator->keypad_previous.value = emulator->keypad.value;
emulator->_cycle -= clockHz;
}
}