-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathemu.c
262 lines (223 loc) · 7.39 KB
/
emu.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
#ifndef _STATIC_EMU_C
#define _STATIC_EMU_C
#include <stdio.h>
#include <assert.h>
#include "defines.h"
static void op_cls(emu_t *emu) {
for (int row = 0; row < DISPLAY_ROWS; row++)
for (int col = 0; col < DISPLAY_COLS; col++)
emu->display[row][col] = 0;
}
static void op_ret(emu_t *emu) {
emu->SP--;
emu->pc = emu->stack[emu->SP];
}
static void op_jp(emu_t *emu, uint16_t addr) {
emu->pc = addr;
}
static void op_call(emu_t *emu, uint16_t addr) {
emu->stack[emu->SP] = emu->pc;
emu->SP++;
emu->pc = addr;
}
static void op_se(emu_t *emu, uint8_t reg, uint8_t val) {
if (emu->V[reg] == val)
emu->pc += 2;
}
static void op_sne(emu_t *emu, uint8_t reg, uint8_t val) {
if (emu->V[reg] != val)
emu->pc += 2;
}
static void op_se_regs(emu_t *emu, uint8_t reg1, uint8_t reg2) {
if (emu->V[reg1] == emu->V[reg2])
emu->pc += 2;
}
static void op_ld(emu_t *emu, uint8_t reg, uint16_t val) {
emu->V[reg] = val;
}
static void op_add(emu_t *emu, uint8_t reg, uint16_t val) {
emu->V[reg] += val;
}
static void op_ld_regs(emu_t *emu, uint8_t reg1, uint8_t reg2) {
emu->V[reg1] = emu->V[reg2];
}
static void op_or(emu_t *emu, uint8_t reg1, uint8_t reg2) {
emu->V[reg1] |= emu->V[reg2];
}
static void op_and(emu_t *emu, uint8_t reg1, uint8_t reg2) {
emu->V[reg1] &= emu->V[reg2];
}
static void op_xor(emu_t *emu, uint8_t reg1, uint8_t reg2) {
emu->V[reg1] ^= emu->V[reg2];
}
static void op_add_regs(emu_t *emu, uint8_t reg1, uint8_t reg2) {
uint16_t res = emu->V[reg1] + emu->V[reg2];
emu->V[0xF] = res > 255;
emu->V[reg1] = res;
}
static void op_sub(emu_t *emu, uint8_t reg1, uint8_t reg2) {
emu->V[0xF] = emu->V[reg1] > emu->V[reg2];
emu->V[reg1] -= emu->V[reg2];
}
static void op_shr(emu_t *emu, uint8_t reg1, uint8_t reg2) {
emu->V[0xF] = emu->V[reg1] & 0x0001;
emu->V[reg1] >>= 1;
}
static void op_subn(emu_t *emu, uint8_t reg1, uint8_t reg2) {
emu->V[0xF] = emu->V[reg2] > emu->V[reg1];
emu->V[reg1] = emu->V[reg2] - emu->V[reg1];
}
static void op_shl(emu_t *emu, uint8_t reg1, uint8_t reg2) {
emu->V[0xF] = (emu->V[reg1] >> 7) & 0x1;
emu->V[reg1] <<= 1;
}
static void op_sne_regs(emu_t *emu, uint8_t reg1, uint8_t reg2) {
if (emu->V[reg1] != emu->V[reg2])
emu->pc += 2;
}
static void op_ld_I(emu_t *emu, uint16_t addr) {
emu->I = addr;
}
static void op_jp_V0(emu_t *emu, uint16_t addr) {
emu->I = addr + emu->V[0x0];
}
static void op_rnd(emu_t *emu, uint8_t reg, uint16_t val) {
emu->V[reg] = rand() & val;
}
static void op_display(emu_t *emu, uint8_t x, uint8_t y, uint8_t n) {
x = emu->V[x];
y = emu->V[y];
emu->V[0xF] = 0;
/* Each line of a sprite is 8 pixels => it's 1 byte */
for (int line = 0; line < n; line++) {
for (int px = 7; px >= 0; px--) {
uint8_t rx = (x + 7 - px) % DISPLAY_COLS;
uint8_t ry = (y + line) % DISPLAY_ROWS;
if (BIT_SET(emu->memory[emu->I+line], px)) {
if (emu->display[ry][rx] == 1)
emu->V[0xF] = 1;
emu->display[ry][rx] ^= 1;
}
}
}
}
static void op_skp(emu_t *emu, uint8_t reg) {
if (emu->keypad[emu->V[reg]])
emu->pc += 2;
}
static void op_sknp(emu_t *emu, uint8_t reg) {
if (!emu->keypad[emu->V[reg]])
emu->pc += 2;
}
// WARNING not sure how the delay timer should behave
static void op_ld_Vx_DT(emu_t *emu, uint8_t reg) {
emu->V[reg] = emu->DT;
}
// TODO is this fine?
static void op_ld_Vx_K(emu_t *emu, uint8_t reg) {
bool pressed = false;
while (!pressed) {
for (int i = 0x0; i <= 0xF; i++) {
if (emu->keypad[i]) {
emu->V[reg] = i;
pressed = true;
break;
}
}
}
}
static void op_ld_DT_Vx(emu_t *emu, uint8_t reg) {
emu->DT = emu->V[reg];
}
// WARNING not sure how the Sound Timer should behave
static void op_ld_ST_Vx(emu_t *emu, uint8_t reg) {
emu->ST = emu->V[reg];
}
static void op_add_I(emu_t *emu, uint8_t reg) {
emu->I += emu->V[reg];
}
static void op_ld_F(emu_t *emu, uint8_t reg) {
emu->I = 0x0000 + emu->V[reg]*5;
}
static uint8_t hex2bcd(uint8_t x) {
uint8_t y;
y = (x / 10) << 4;
y = y | (x % 10);
return y;
}
static void op_ld_BCD(emu_t *emu, uint8_t reg) {
uint8_t bcd = hex2bcd(emu->V[reg]);
emu->memory[(emu->I)+0] = (bcd >> 2) & 0x1;
emu->memory[(emu->I)+1] = (bcd >> 1) & 0x1;
emu->memory[(emu->I)+2] = (bcd >> 0) & 0x1;
}
static void op_ld_array_Vx(emu_t *emu, uint8_t reg) {
for (uint16_t i = 0; i <= reg; i++)
emu->memory[emu->I + i] = emu->V[i];
}
static void op_ld_Vx_array(emu_t *emu, uint8_t reg) {
for (uint16_t i = 0; i <= reg; i++)
emu->V[i] = emu->memory[emu->I + i];
}
static void handle_instruction(emu_t *emu, uint16_t ins) {
uint8_t reg1 = NIBBLE_2(ins);
uint8_t reg2 = NIBBLE_1(ins);
uint8_t val = BYTE_0(ins);
uint16_t addr = ins & 0x0FFF;
switch (NIBBLE_3(ins)) {
case 0x0:
if (val == 0xE0) op_cls(emu);
else if (val == 0xEE) op_ret(emu);
else goto unknown;
break;
case 0x1: op_jp (emu, addr); break;
case 0x2: op_call (emu, addr); break;
case 0x3: op_se (emu, reg1, val); break;
case 0x4: op_sne (emu, reg1, val); break;
case 0x5: op_se_regs (emu, reg1, reg2); break;
case 0x6: op_ld (emu, reg1, val); break;
case 0x7: op_add (emu, reg1, val); break;
case 0x8:
switch (NIBBLE_0(ins)) {
case 0x0: op_ld_regs (emu, reg1, reg2); break;
case 0x1: op_or (emu, reg1, reg2); break;
case 0x2: op_and (emu, reg1, reg2); break;
case 0x3: op_xor (emu, reg1, reg2); break;
case 0x4: op_add_regs (emu, reg1, reg2); break;
case 0x5: op_sub (emu, reg1, reg2); break;
case 0x6: op_shr (emu, reg1, reg2); break;
case 0x7: op_subn (emu, reg1, reg2); break;
case 0xE: op_shl (emu, reg1, reg2); break;
default: goto unknown;
}
break;
case 0x9: op_sne (emu, reg1, reg2); break;
case 0xA: op_ld_I (emu, addr); break;
case 0xB: op_jp_V0 (emu, addr); break;
case 0xC: op_rnd (emu, reg1, val); break;
case 0xD: op_display (emu, reg1, reg2, NIBBLE_0(ins)); break;
case 0xE:
if (val == 0x9E) op_skp (emu, reg1);
else if (val == 0xA1) op_sknp(emu, reg1);
else goto unknown;
break;
case 0xF:
switch (val) {
case 0x07: op_ld_Vx_DT (emu, reg1); break;
case 0x0A: op_ld_Vx_K (emu, reg1); break;
case 0x15: op_ld_DT_Vx (emu, reg1); break;
case 0x18: op_ld_ST_Vx (emu, reg1); break;
case 0x1E: op_add_I (emu, reg1); break;
case 0x29: op_ld_F (emu, reg1); break;
case 0x33: op_ld_BCD (emu, reg1); break;
case 0x55: op_ld_array_Vx (emu, reg1); break;
case 0x65: op_ld_Vx_array (emu, reg1); break;
}
break;
default:
unknown:
printf("INSTRUCTION %04X NOT FOUND\n", ins);
break;
}
}
#endif