-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchip8.c
428 lines (378 loc) · 10.3 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
422
423
424
425
426
427
428
#include "chip8.h"
#include "SDL_events.h"
#include "constants.h"
#include <stdlib.h>
/**
* http://devernay.free.fr/hacks/chip8/C8TECH10.HTM
*
*
memory offsets
0x000-0x1FF - Chip 8 interpreter (contains font set in emu)
0x050-0x0A0 - Used for the built in 4x5 pixel font set (0-F)
0x200-0xFFF - Program ROM and work RAM
* 3.1 - Standard Chip-8 Instructions
00E0 - CLS
00EE - RET
0nnn - SYS addr
1nnn - JP addr
2nnn - CALL addr
3xkk - SE Vx, byte
4xkk - SNE Vx, byte
5xy0 - SE Vx, Vy
6xkk - LD Vx, byte
7xkk - ADD Vx, byte
8xy0 - LD Vx, Vy
8xy1 - OR Vx, Vy
8xy2 - AND Vx, Vy
8xy3 - XOR Vx, Vy
8xy4 - ADD Vx, Vy
8xy5 - SUB Vx, Vy
8xy6 - SHR Vx {, Vy}
8xy7 - SUBN Vx, Vy
8xyE - SHL Vx {, Vy}
9xy0 - SNE Vx, Vy
Annn - LD I, addr
Bnnn - JP V0, addr
Cxkk - RND Vx, byte
Dxyn - DRW Vx, Vy, nibble
Ex9E - SKP Vx
ExA1 - SKNP Vx
Fx07 - LD Vx, DT
Fx0A - LD Vx, K
Fx15 - LD DT, Vx
Fx18 - LD ST, Vx
Fx1E - ADD I, Vx
Fx29 - LD F, Vx
Fx33 - LD B, Vx
Fx55 - LD [I], Vx
Fx65 - LD Vx, [I]
*/
// 0nnn - SYS addr
void chip8_sys(Chip8* chip8, unsigned short nnn)
{
chip8->pc = nnn;
}
// 00E0 - CLS
void chip8_cls(Chip8* chip8)
{
for(int i = 0; i < SCREEN_SIZE; i++) // 64x32 Screen
{
chip8->gfx[i] = 0; // Set to black
}
}
// 00EE - RET
void chip8_ret(Chip8* chip8)
{
chip8->pc = chip8->stack[chip8->sp];
chip8->sp--;
}
// 1nnn - JP addr
// Jump to a machine code routine at nnn.
void chip8_jp(Chip8* chip8, unsigned short nnn)
{
chip8->pc = nnn;
}
// 2nnn - CALL addr
// Clear the display
void chip8_call(Chip8* chip8, unsigned short nnn)
{
chip8->sp++;
chip8->stack[chip8->sp] = chip8->pc;
chip8->pc = nnn;
}
// 3xkk - SE Vx, byte
void chip8_se(Chip8* chip8, unsigned char x, unsigned char kk)
{
if(chip8->V[x] == kk)
{
chip8->pc += 2;
}
}
// 4xkk - SNE Vx, byte
void chip8_sne(Chip8* chip8, unsigned char x, unsigned char kk)
{
if(chip8->V[x] != kk)
{
chip8->pc += 2;
}
}
// 5xy0 - SE Vx, Vy
void chip8_se_vx_vy(Chip8* chip8, unsigned char x, unsigned char y)
{
if(chip8->V[x] == chip8->V[y])
{
chip8->pc += 2;
}
}
// 6xkk - LD Vx, byte
void chip8_ld_vx_byte(Chip8* chip8, unsigned char x, unsigned char kk)
{
chip8->V[x] = kk;
}
// 7xkk - ADD Vx, byte
void chip8_add_vx_byte(Chip8* chip8, unsigned char x, unsigned char kk)
{
chip8->V[x] = chip8->V[x] + kk;
}
// 8xy0 - LD Vx, Vy
void chip8_ld_vx_vy(Chip8* chip8, unsigned char x, unsigned char y)
{
chip8->V[x] = chip8->V[y];
}
// 8xy1 - OR Vx, Vy
void chip8_or_vx_vy(Chip8* chip8, unsigned char x, unsigned char y)
{
chip8->V[x] = chip8->V[x] | chip8->V[y];
}
// 8xy2 - AND Vx, Vy
void chip8_and_vx_vy(Chip8* chip8, unsigned char x, unsigned char y)
{
chip8->V[x] = chip8->V[x] & chip8->V[y];
}
// 8xy3 - XOR Vx, Vy
void chip8_xor_vx_vy(Chip8* chip8, unsigned char x, unsigned char y)
{
chip8->V[x] = chip8->V[x] ^ chip8->V[y];
}
// 8xy4 - ADD Vx, Vy
void chip8_add_vx_vy(Chip8* chip8, unsigned char x, unsigned char y)
{
unsigned short temp = chip8->V[x] + chip8->V[y];
if(temp > 255)
{
chip8->V[0xF] = 1;
}
else
{
chip8->V[0xF] = 0;
}
chip8->V[x] = (unsigned char) temp;
}
// 8xy5 - SUB Vx, Vy
// Set Vx = Vx - Vy, set VF = NOT borrow.
// If Vx > Vy, then VF is set to 1, otherwise 0. Then Vy is subtracted from Vx, and the results stored in Vx.
void chip8_sub_vx_vy(Chip8* chip8, unsigned char x, unsigned char y)
{
if(chip8->V[x] > chip8->V[y])
{
chip8->V[0xF] = 1;
}
else
{
chip8->V[0xF] = 0;
}
chip8->V[x] = chip8->V[x] - chip8->V[y];
}
// 8xy6 - SHR Vx {, Vy}
// Set Vx = Vx SHR 1.
// If the least-significant bit of Vx is 1, then VF is set to 1, otherwise 0. Then Vx is divided by 2.
void chip8_shr_vx_vy(Chip8* chip8, unsigned char x, unsigned char y)
{
// vx = vy?
chip8->V[0xF] = chip8->V[x] & 0x1;
chip8->V[x] = chip8->V[x] >> 1;
}
// 8xy7 - SUBN Vx, Vy
// Set Vx = Vy - Vx, set VF = NOT borrow.
// If Vy > Vx, then VF is set to 1, otherwise 0. Then Vx is subtracted from Vy, and the results stored in Vx.
void chip8_subn_vx_vy(Chip8* chip8, unsigned char x, unsigned char y)
{
if(chip8->V[y] > chip8->V[x])
{
chip8->V[0xF] = 1;
}
else
{
chip8->V[0xF] = 0;
}
chip8->V[x] = chip8->V[y] - chip8->V[x];
}
// 8xyE - SHL Vx {, Vy}
// Set Vx = Vx SHL 1.
// If the most-significant bit of Vx is 1, then VF is set to 1, otherwise to 0. Then Vx is multiplied by 2.
void chip8_shl_vx_vy(Chip8* chip8, unsigned char x, unsigned char y)
{
chip8->V[0xF] = chip8->V[x] & 0x1;
chip8->V[x] = chip8->V[x] * 2;
}
// 9xy0 - SNE Vx, Vy
void chip8_sne_vx_vy(Chip8* chip8, unsigned char x, unsigned char y)
{
if(chip8->V[x] != chip8->V[y])
{
chip8->pc += 2;
}
}
// Annn - LD I, addr
void chip8_ld_i_addr(Chip8* chip8, unsigned short nnn)
{
chip8->I = nnn;
}
// Bnnn - JP V0, addr
// Jump to location nnn + V0.
// The program counter is set to nnn plus the value of V0.
void chip8_jp_v0_addr(Chip8* chip8, unsigned short nnn)
{
chip8->pc = nnn + chip8->V[0];
}
// Cxkk - RND Vx, byte
// Set Vx = random byte AND kk.
// The interpreter generates a random number from 0 to 255, which is then ANDed with the value kk. The results are stored in Vx. See instruction 8xy2 for more information on AND.
void chip8_rnd_vx_byte(Chip8* chip8, unsigned char x, unsigned short kk)
{
int rn = rand() % 256;
chip8->V[x] = rn & (unsigned short) kk;
}
// Dxyn - DRW Vx, Vy, nibble
// Display n-byte sprite starting at memory location I at (Vx, Vy), set VF = collision.
//
// The interpreter reads n bytes from memory, starting at the address stored in
// I. These bytes are then displayed as sprites on screen at coordinates (Vx, Vy).
//
// Sprites are XORed onto the existing screen.
//
// If this causes any pixels to be erased, VF is set to 1, otherwise it is set to 0.
// If the sprite is positioned so part of it is outside the coordinates of the display, it wraps around to
// the opposite side of the screen.
//
// See instruction 8xy3 for more information on XOR, and section 2.4, Display, for more information on the Chip-8 screen and sprites.
void chip8_drw_vx_vy_nibble(Chip8* chip8, unsigned char x, unsigned char y, unsigned char n)
{
unsigned short px = chip8->V[x] % 64;
unsigned short py = chip8->V[y] % 32;
unsigned short height = n;
unsigned char pixel;
chip8->V[0xF] = 0;
for(int yline = 0; yline < height; yline++)
{
if(py + yline >= 32)
break;
pixel = chip8->memory[chip8->I + yline];
for(int xline = 0; xline < 8; xline++)
{
if(px + xline >= 64)
break;
if((pixel & (0x80 >> xline)) != 0)
{
unsigned int index = (px + xline + ((py + yline) * 64));
if(chip8->gfx[index] == 1)
{
chip8->V[0xF] = 1;
}
chip8->gfx[index] ^= 1;
}
}
}
chip8->drawFlag = 1;
}
// Ex9E - SKP Vx
// Skip next instruction if key with the value of Vx is pressed.
// Checks the keyboard, and if the key corresponding to the value of Vx is currently in the down position, PC is increased by 2.
void chip8_skp_vx(Chip8* chip8, unsigned char x)
{
if(chip8->key[chip8->V[x]] == 1)
{
chip8->pc += 2;
}
}
// ExA1 - SKNP Vx
// Skip next instruction if key with the value of Vx is not pressed.
// Checks the keyboard, and if the key corresponding to the value of Vx is currently in the up position, PC is increased by 2.
void chip8_sknp_vx(Chip8* chip8, unsigned char x)
{
if(chip8->key[chip8->V[x]] == 0)
{
chip8->pc += 2;
}
}
// Fx07 - LD Vx, DT
void chip8_ld_vx_dt(Chip8* chip8, unsigned char x)
{
chip8->V[x] = chip8->delayTimer;
}
// Fx0A - LD Vx, K
void chip8_ld_vx_k(Chip8* chip8, unsigned char x)
{
char validInputPressed = 0;
while (validInputPressed == 0)
{
SDL_Event event;
while (SDL_PollEvent(&event))
{
if (event.type == SDL_KEYDOWN)
{
switch (event.key.keysym.sym)
{
case SDLK_z:
case SDLK_x:
case SDLK_c:
case SDLK_v:
case SDLK_a:
case SDLK_s:
case SDLK_d:
case SDLK_f:
case SDLK_q:
case SDLK_w:
case SDLK_e:
case SDLK_r:
case SDLK_1:
case SDLK_2:
case SDLK_3:
case SDLK_4:
validInputPressed = 1;
chip8->V[x] = event.key.keysym.sym;
break;
}
}
}
}
}
// Fx15 - LD DT, Vx
// Load Delay Timer from register X
void chip8_ld_dt_vx(Chip8* chip8, unsigned char x)
{
chip8->delayTimer = chip8->V[x];
}
// Fx18 - LD ST, Vx
// Load sound timer from register x
void chip8_ld_st_vx(Chip8* chip8, unsigned char x)
{
chip8->soundTimer = chip8->V[x];
}
// Fx1E - ADD I, Vx
void chip8_add_i_vx(Chip8* chip8, unsigned char x)
{
chip8->I = chip8->I + chip8->V[x];
}
// Fx29 - LD F, Vx
void chip8_ld_f_vx(Chip8* chip8, unsigned char x)
{
chip8->I = CHIP8_FONTSET[chip8->V[x]];
}
// Fx33 - LD B, Vx
void chip8_ld_b_vx(Chip8* chip8, unsigned char x)
{
unsigned char h = chip8->V[x] / 100;
unsigned char t = (chip8->V[x] / 10) % 10;
unsigned char o = chip8->V[x] % 10;
chip8->memory[chip8->I] = h;
chip8->memory[chip8->I + 1] = t;
chip8->memory[chip8->I + 2] = o;
}
// Fx55 - LD [I], Vx
void chip8_ld_i_vx(Chip8* chip8, unsigned char x)
{
for(int i = 0; i <= x; i++)
{
chip8->memory[(chip8->I + i)] = chip8->V[i]; // todo: confirm
}
}
// Fx65 - LD Vx, [I]
void chip8_ld_vx_i(Chip8* chip8, unsigned char x)
{
for(int i = 0; i <= x; i++)
{
chip8->V[i] = chip8->memory[(chip8->I + i)]; // todo: confirm
}
}
// TODO: Super Chip-48 Instructions