-
Notifications
You must be signed in to change notification settings - Fork 0
/
instruction.c
312 lines (263 loc) · 6.6 KB
/
instruction.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
#include <stdlib.h>
#include "instruction.h"
#include "chip8.h"
// 00E0
void clearScreen(Chip8* chip8) {
for (int x = 0; x < 64; ++x) {
for (int y = 0; y < 32; ++y) {
chip8->display[x][y] = 0;
}
}
}
// 00EE
void returnSubroutine(Chip8* chip8) {
--chip8->sp;
chip8->pc = chip8->stack[(chip8->sp)];
chip8->stack[chip8->sp] = 0;
}
// 1NNN
void jump(Chip8* chip8) {
chip8->pc = chip8->opcode & 0x0FFF;
}
// 2NNN
void callSubroutine(Chip8* chip8) {
chip8->stack[chip8->sp] = chip8->pc;
chip8->pc = chip8->opcode & 0x0FFF;
++chip8->sp;
}
// 3XNN
void skipVxByte(Chip8* chip8) {
uint8_t x = (chip8->opcode & 0x0F00) >> 8;
uint8_t byte = (chip8->opcode & 0x00FF);
if (chip8->v[x] == byte) {
chip8->pc += 2;
}
}
// 4XNN
void skipNotVxByte(Chip8* chip8) {
uint8_t x = (chip8->opcode & 0x0F00) >> 8;
uint8_t byte = (chip8->opcode & 0x00FF);
if (chip8->v[x] != byte) {
chip8->pc += 2;
}
}
// 5XY0
void skipVxVy(Chip8* chip8) {
uint8_t x = (chip8->opcode & 0x0F00) >> 8;
uint8_t y = (chip8->opcode & 0x00F0) >> 4;
if (chip8->v[x] == chip8->v[y]) {
chip8->pc += 2;
}
}
// 6XNN
void set(Chip8* chip8) {
uint8_t reg = (chip8->opcode & 0x0F00) >> 8;
chip8->v[reg] = chip8->opcode & 0x00FF;
}
// 7XNN
void add(Chip8* chip8) {
uint8_t reg = (chip8->opcode & 0x0F00) >> 8;
chip8->v[reg] += chip8->opcode & 0x00FF;
}
// 8XY0
void setXY(Chip8* chip8){
uint8_t x = (chip8->opcode & 0x0F00) >> 8;
uint8_t y = (chip8->opcode & 0x00F0) >> 4;
chip8->v[x] = chip8->v[y];
}
// 8XY1
void OR(Chip8* chip8){
uint8_t x = (chip8->opcode & 0x0F00) >> 8;
uint8_t y = (chip8->opcode & 0x00F0) >> 4;
chip8->v[x] |= chip8->v[y];
chip8->v[0xF] = 0;
}
// 8XY2
void AND(Chip8* chip8) {
uint8_t x = (chip8->opcode & 0x0F00) >> 8;
uint8_t y = (chip8->opcode & 0x00F0) >> 4;
chip8->v[x] &= chip8->v[y];
chip8->v[0xF] = 0;
}
// 8XY3
void XOR(Chip8* chip8) {
uint8_t x = (chip8->opcode & 0x0F00) >> 8;
uint8_t y = (chip8->opcode & 0x00F0) >> 4;
chip8->v[x] ^= chip8->v[y];
chip8->v[0xF] = 0;
}
// 8XY4
void addXY(Chip8* chip8) {
uint8_t x = (chip8->opcode & 0x0F00) >> 8;
uint8_t y = (chip8->opcode & 0x00F0) >> 4;
uint16_t sum = chip8->v[x] + chip8->v[y];
chip8->v[x] = sum & 0xFF;
if (sum > 255) {
chip8->v[0xF] = 1;
}
else {
chip8->v[0xF] = 0;
}
}
// 8XY5
void subXY(Chip8* chip8) {
uint8_t x = (chip8->opcode & 0x0F00) >> 8;
uint8_t y = (chip8->opcode & 0x00F0) >> 4;
uint8_t VX = chip8->v[x];
uint8_t VY = chip8->v[y];
chip8->v[x] -= chip8->v[y];
if (VX >= VY) {
chip8->v[0xF] = 1;
}
else {
chip8->v[0xF] = 0;
}
}
// 8XY6
void shiftRight(Chip8* chip8) {
uint8_t x = (chip8->opcode & 0x0F00) >> 8;
uint8_t y = (chip8->opcode & 0x00F0) >> 4;
chip8->v[x] = chip8->v[y];
uint8_t VX = chip8->v[x];
chip8->v[x] >>= 1;
chip8->v[0xF] = VX & 0x01;
}
// 8XY7
void subYX(Chip8* chip8) {
uint8_t x = (chip8->opcode & 0x0F00) >> 8;
uint8_t y = (chip8->opcode & 0x00F0) >> 4;
uint8_t VX = chip8->v[x];
uint8_t VY = chip8->v[y];
chip8->v[x] = chip8->v[y] - chip8->v[x];
if (chip8->v[y] > chip8->v[x]) {
chip8->v[0xF] = 1;
}
else {
chip8->v[0xF] = 0;
}
}
// 8XYE
void shiftLeft(Chip8* chip8) {
uint8_t x = (chip8->opcode & 0x0F00) >> 8;
uint8_t y = (chip8->opcode & 0x00F0) >> 4;
chip8->v[x] = chip8->v[y];
uint8_t VX = chip8->v[x];
chip8->v[x] <<= 1;
chip8->v[0xF] = (VX & 0x80) >> 7;
}
// 9XY0
void skipNotVxVy(Chip8* chip8) {
uint8_t x = (chip8->opcode & 0x0F00) >> 8;
uint8_t y = (chip8->opcode & 0x00F0) >> 4;
if (chip8->v[x] != chip8->v[y]) {
chip8->pc += 2;
}
}
// ANNN
void setIndex(Chip8* chip8) {
chip8->i = chip8->opcode & 0x0FFF;
}
// BNNN
void jumpOffset(Chip8* chip8) {
chip8->pc = (chip8->opcode & 0x0FFF) + chip8->v[0];
}
// CXNN
void randomNumber(Chip8* chip8) {
uint8_t x = (chip8->opcode & 0x0F00) >> 8;
chip8->v[x] = (chip8->opcode & 0x00FF) & rand();
}
// DXYN
void draw(Chip8* chip8) {
chip8->v[0xF] = 0;
uint8_t x = (chip8->opcode & 0x0F00) >> 8;
uint8_t y = (chip8->opcode & 0x00F0) >> 4;
uint8_t height = chip8->opcode & 0x000F;
uint8_t pixel = 0;
for (uint16_t yLine = 0; yLine < height; yLine++) {
pixel = chip8->ram[chip8->i + yLine];
for (uint16_t xLine = 0; xLine < 8; xLine++) {
if ((pixel & (0x80 >> xLine)) != 0) {
if (chip8->display[(chip8->v[x] + xLine) % 64][(chip8->v[y] + yLine) % 32] == 1) {
chip8->v[0xF] = 1;
}
chip8->display[(chip8->v[x] + xLine) % 64][(chip8->v[y] + yLine) % 32] ^= 1;
}
}
}
}
// EX9E
void skipIfKey(Chip8* chip8) {
uint8_t x = (chip8->opcode & 0x0F00) >> 8;
if (chip8->keypad[chip8->v[x]] == 1) {
chip8->pc += 2;
}
}
// EXA1
void skipIfNotKey(Chip8* chip8) {
uint8_t x = (chip8->opcode & 0x0F00) >> 8;
if (chip8->keypad[chip8->v[x]] == 0) {
chip8->pc += 2;
}
}
// FX07
void vxTimer(Chip8* chip8) {
uint8_t x = (chip8->opcode & 0x0F00) >> 8;
chip8->v[x] = chip8->delayTimer;
}
// FX15
void setDelay(Chip8* chip8) {
uint8_t x = (chip8->opcode * 0x0F00) >> 8;
chip8->delayTimer = chip8->v[x];
}
// FX18
void setSound(Chip8* chip8) {
uint8_t x = (chip8->opcode & 0x0F00) >> 8;
chip8->soundTimer = chip8->v[x];
}
// FX0A
void getKey(Chip8* chip8) {
uint8_t x = (chip8->opcode & 0x0F00) >> 8;
if (chip8->keyPressed == 0) {
chip8->pc -= 2;
}
else {
chip8->v[x] = chip8->keyPressed;
}
}
// FX1E
void addToIndex(Chip8* chip8) {
uint8_t x = (chip8->opcode & 0x0F00) >> 8;
chip8->i += chip8->v[x];
}
// FX29
void fontCharacter(Chip8* chip8) {
uint8_t x = (chip8->opcode & 0x0F00) >> 8;
uint8_t digit = chip8->v[x];
chip8->i = 0x50 + (5 * digit);
}
// FX33
void decimalConversion(Chip8* chip8) {
uint8_t x = (chip8->opcode & 0x0F00) >> 8;
uint8_t num = chip8->v[x];
chip8->ram[chip8->i+2] = num % 10;
num /= 10;
chip8->ram[chip8->i+1] = num % 10;
num /= 10;
chip8->ram[chip8->i] = num % 10;
}
// FX55
void storeReg(Chip8* chip8) {
uint8_t x = (chip8->opcode & 0x0F00) >> 8;
int n;
for (n = 0; n <= x; ++n) {
chip8->ram[chip8->i + n] = chip8->v[n];
}
chip8->i = n;
}
// FX65
void loadReg(Chip8* chip8) {
uint8_t x = (chip8->opcode & 0x0F00) >> 8;
for (int n = 0; n <= x; ++n) {
chip8->v[n] = chip8->ram[chip8->i + n];
}
}