-
Notifications
You must be signed in to change notification settings - Fork 0
/
chip.c
331 lines (303 loc) · 5.79 KB
/
chip.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
#include <stdlib.h>
#include <time.h>
#include "chip.h"
unsigned char memory[4096];
unsigned short opcode;//current opcode
unsigned short stack[16];//return stack
unsigned char sp;//stack pointer
unsigned short ir;//index reg
unsigned short pc;//program counter
unsigned char V[16];//16 8bit registers
unsigned char timerS;//sound timer
unsigned char timerD;//delay timer
unsigned char gfx_buf[256];//bit array for monochrome gfx (64*32)/8 bytes
void (*op_table[16])();
void (*math_table[15])();
unsigned char chip8_fontset[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
};
#define CARRY(condition) (V[15] = (condition) ? 1 : 0)//maybe remove one CARRY
void xorBit(int bit)
{
if(bit<2048){
if(V[15] == 0){CARRY(gfx_buf[bit/8] & (1 << (bit%8)));}
gfx_buf[bit/8] ^= (1 << (bit%8));
}
}
void setBit(int bit)
{
gfx_buf[bit/8] |= (1 << (bit%8));
}
void clearBit(int bit)
{
gfx_buf[bit/8] &= ~(1 << (bit%8));
}
int readBit(int bit)
{
return gfx_buf[bit/8] & (1 << (bit%8));
}
void init()
{
srand(time(0));
pc = 0x200;//512
ir = 0;
sp = 0;
for (int i = 0; i < 4096; i++)
{
memory[i] = 0;
}
for (int i = 0; i < 256; i++)
{
gfx_buf[i] = 0;
}
for (int i = 0; i < 80; i++)
{
memory[i + 0x50] = chip8_fontset[i];
}
}
int fetch()
{
if(pc > 4096){return 0;}
opcode = (memory[pc] << 8) | (memory[pc+1]);
pc += 2;
return 1;
}
void execute()
{
op_table[(opcode & 0xF000) >> 12]();
}
////////////////////////////////////
////////////////////////////////////
#define NNN (opcode & 0x0FFF)
#define N (opcode & 0x0F)
#define KK (opcode & 0xFF)
#define X ((opcode & 0xF00) >> 8)
#define Y ((opcode & 0x0F0) >> 4)
#define CARRY(condition) (V[15] = (condition) ? 1 : 0)
//macro defines
void NULLop(){}//does nothing
void mathBranch()//branches to math table
{
math_table[opcode & 0x000F]();
}
//misc functions
void ZERO()
{
if ((opcode & 0xFF) == 0xE0)//00E0
{
for (int i = 0; i < (64*32)/8; i++)
{
gfx_buf[i] = 0;
}
}
else if ((opcode & 0xFF) == 0xEE)//00EE
{
if (sp)
{
pc = stack[--sp];//decrement return stack
}
}
}
void JP()//1nnn
{
pc = NNN;
}
void CALL()//2nnn
{
stack[sp++] = pc;
pc = NNN;
}
void SE()//3xkk
{
if (V[X] == KK)
{
pc += 2;
}
}
void SNE()//4xkk
{
if (V[X] != KK)
{
pc += 2;
}
}
void SER()//5xy0
{
if (V[X] == V[Y])
{
pc += 2;
}
}
void LD()//6xkk
{
V[X] = KK;
}
void ADD()//7xkk
{
V[X] += KK;
}
//opcode functions
void LD2()//8xy0
{
V[X] = V[Y];
}
void OR()//8xy1
{
V[X] |= V[Y];
}
void AND()//8xy2
{
V[X] &= V[Y];
}
void XOR()//8xy3
{
V[X] ^= V[Y];
}
void ADD2()//8xy4
{
CARRY(V[X] + V[Y] > 255);
V[X] += V[Y];
}
void SUB()//8xy5
{
CARRY(V[X] > V[Y]);
V[X] -= V[Y];
}
void SHR()//8xy6
{
CARRY(V[X] & 0x1);//mybe shift if not working properly
V[X] >>= 1;
}
void SUBN()//8xy7
{
CARRY(V[Y] > V[X]);
V[X] = V[Y] - V[X];
}
void SHL()//8xyE
{
CARRY(V[X] & 0x8);//same as shr
V[X] <<= 1;
}
//math functions
void SNE2()//9xy0
{
if (V[X] != V[Y])
{
pc += 2;
}
}
void LD3()//Annn
{
ir = NNN;
}
void JP2()//Bnnn
{
pc = V[0] + NNN;
}
void RND()//Cxkk
{
V[X] = rand()%255 & KK;
}
void DRW()//Dxyn
{
V[15] = 0;//for carry flag
for (int i = 0; i < N; i++)
{
for (int j = 0; j < 8; j++)
{
if ((memory[ir+i] & 0x80 >> j) == 0x80 >> j)
{
xorBit(((V[Y]+i)*64)+V[X]+j);
}
}
}
//draws a sprite on x,y with height N
//located on ir
//sets carry flag (draw) flag
}
void SKP()
{
if((opcode & 0x0F) == 0x0E)//Ex9E
{
if(key[V[X]] == 1)
{
pc += 2;
}
}
else//ExA1
{
if(key[V[X]] != 1)
{
pc += 2;
}
}
}
void fBranch()
{
switch (opcode & 0xF0FF)
{
case 0xF007:
V[X] = timerD;
break;
case 0xF00A:
do
{
V[X] = getEvents(1);
}
while(V[X] > 15);
break;
case 0xF015:
timerD = V[X];
break;
case 0xF018:
timerS = V[X];
break;
case 0xF01E:
ir += V[X];
break;
case 0xF029:
ir = V[X]*5 + 0x50;//sprite adress of sprite num vx - hopefully works
break;
case 0xF033://prob not the smartest nor fastest implementation
memory[ir] = V[X] %1000 / 100;//hundreds
memory[ir+1] = V[X] %100 / 10;//tens
memory[ir+2] = V[X] %10;//ones
break;
case 0xF055:
for (int i = 0; i <= X; i++)
{
memory[ir+i] = V[i];
}
break;
case 0xF065:
for (int i = 0; i <= X; i++)
{
V[i] = memory[ir+i];
}
break;
default:
break;
}
}
////////////////////////////////////
////////////////////////////////////
void (*op_table[16])() = {ZERO, JP, CALL, SE, SNE, SER, LD, ADD,
mathBranch, SNE2, LD3, JP2, RND, DRW, SKP, fBranch};
void (*math_table[15])() = {LD2, OR, AND, XOR, ADD2, SUB, SHR, SUBN, NULLop, NULLop, NULLop, NULLop, NULLop, NULLop, SHL};
//function tables