-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchip_8_opcode_functions.cc
380 lines (326 loc) · 10.6 KB
/
chip_8_opcode_functions.cc
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
#include <iostream>
#include "chip_8_opcode_functions.hpp"
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time *
/* All The opcodes execution is performed according
* to the chip-8 standarizaton mentioned here in this link
* http://devernay.free.fr/hacks/chip8/C8TECH10.HTM#3.0
*/
const opcode_function_executer opcode_table [] =
{
opcode_0_fn_sets,
opcode_1_fn_sets,
opcode_2_fn_sets,
opcode_3_fn_sets,
opcode_4_fn_sets,
opcode_5_fn_sets,
opcode_6_fn_sets,
opcode_7_fn_sets,
opcode_8_fn_sets,
opcode_9_fn_sets,
opcode_A_fn_sets,
opcode_B_fn_sets,
opcode_C_fn_sets,
opcode_D_fn_sets,
opcode_E_fn_sets,
opcode_F_fn_sets
};
//0x0nnn - 0x00E0 - 0x00EE
void opcode_0_fn_sets(Chip_8 & chip8){
switch(chip8.opcode & 0x0fff)
{
case 0x00E0: // Clear The display
chip8.clearDisplay();
chip8.drawOnDisplay = true;
chip8.PC_increment(); // By default it's 2 bytes As Chip 8 does.
break;
case 0x00EE: // Return from a subroutine.
chip8.SP -= 1;
chip8.PC = chip8.Stack[chip8.SP];
chip8.PC_increment();
break;
default: // 0nnn => SYS addr (Jump to a machine code routine at nnn.)
chip8.PC = chip8.opcode & 0x0fff;
break;
}
}
// 0x1nnn - JP addr
void opcode_1_fn_sets(Chip_8 & chip8){
chip8.PC = chip8.opcode & 0x0fff;
}
// 0x2nnn - CALL addr
void opcode_2_fn_sets(Chip_8 & chip8){
chip8.Stack[chip8.SP] = chip8.PC;
chip8.SP += 1;
chip8.PC = chip8.opcode & 0x0fff;
}
// 3xkk - SE Vx, byte
// Skip next instruction if Vx = kk.
void opcode_3_fn_sets(Chip_8 & chip8){
auto x = (chip8.opcode & 0x0f00) >> 8;
auto byte = (chip8.opcode & 0x00ff);
if(chip8.V[x] == byte)
chip8.PC_increment(4);
else
chip8.PC_increment(2);
}
// 4xkk - SNE Vx, byte
// Skip next instruction if Vx != kk.
void opcode_4_fn_sets(Chip_8 & chip8){
auto x = (chip8.opcode & 0x0f00) >> 8;
auto byte = (chip8.opcode & 0x00ff);
if(chip8.V[x] != byte)
chip8.PC_increment(4);
else
chip8.PC_increment(2);
}
// 5xy0 - SE Vx, Vy
// Skip next instruction if Vx = Vy.
void opcode_5_fn_sets(Chip_8 & chip8){
auto x = (chip8.opcode & 0x0f00) >> 8;
auto y = (chip8.opcode & 0x00f0) >> 4;
if(chip8.V[x] == chip8.V[y])
chip8.PC_increment(4);
else
chip8.PC_increment(2);
}
// 6xkk - LD Vx, byte
// Set Vx = kk.
void opcode_6_fn_sets(Chip_8 & chip8){
chip8.V[(chip8.opcode & 0x0f00) >> 8] = (chip8.opcode & 0x00ff);
chip8.PC_increment();
}
// 7xkk - ADD Vx, byte
// Set Vx = Vx + kk.
void opcode_7_fn_sets(Chip_8 & chip8){
chip8.V[(chip8.opcode & 0x0f00) >> 8] += (chip8.opcode & 0x00ff);
chip8.PC_increment();
}
// 8xy0 - 8xy1 - 8xy2 - 8xy3 - 8xy4 - 8xy5 - 8xy6 - 8xy7 - 8xyE
void opcode_8_fn_sets(Chip_8 & chip8){
auto code_select = chip8.opcode & 0x000f;
auto x = (chip8.opcode & 0x0f00) >> 8;
auto y = (chip8.opcode & 0x00f0) >> 4;
switch(code_select)
{
// Set Vx = Vy
case 0x00:
chip8.V[x] = chip8.V[y];
chip8.PC_increment();
break;
// Set Vx = Vx | Vy
case 0x01:
chip8.V[x] |= chip8.V[y];
chip8.PC_increment();
break;
// Set Vx = Vx & Vy
case 0x02:
chip8.V[x] &= chip8.V[y];
chip8.PC_increment();
break;
// Set Vx = Vx xor Vy
case 0x03:
chip8.V[x] ^= chip8.V[y];
chip8.PC_increment();
break;
// Set Vx = Vx + Vy , VF = carry
case 0x04:
{ // Create an enclosing scope so not violating the switch case rule
// while creating variables.
unsigned short result = (unsigned short)chip8.V[x] + (unsigned short)chip8.V[y];
chip8.V[0xF] = (result > 0xFF) ? 0x01 : 0x00;
chip8.V[x] = result & 0x000F;
chip8.PC_increment();
}
break;
// Set Vx = Vx - Vy, set VF = NOT borrow.
case 0x05:
chip8.V[0xF] = (chip8.V[x] > chip8.V[y]) ? 0x01 : 0x00;
chip8.V[x] = chip8.V[x] - chip8.V[y];
chip8.PC_increment();
break;
// Set Vx = Vx SHR(Shift Right by one bit) 1
case 0x06:
chip8.V[0xF] = (chip8.V[x] & 0x01);
chip8.V[x] >>= 0x01;
chip8.PC_increment();
break;
// Set Vx = Vy - Vx, set VF = NOT borrow
case 0x07:
chip8.V[0xF] = (chip8.V[y] > chip8.V[x]) ? 0x01 : 0x00;
chip8.V[x] = chip8.V[y] - chip8.V[x];
chip8.PC_increment();
break;
// Set Vx = Vx SHL(Shift Left) 1.
case 0x0E:
chip8.V[0xF] = chip8.V[x] >> 0x07;
chip8.V[x] <<= 0x01;
chip8.PC_increment();
break;
default:
printf("Unknown opcode [0x8000]: 0x%X\n", chip8.opcode);
exit(-1);
break;
}
}
// 9xy0 - SNE Vx, Vy
// Skip next instruction if Vx != Vy.
void opcode_9_fn_sets(Chip_8 & chip8){
auto code_select = chip8.opcode & 0x000f;
auto x = (chip8.opcode & 0x0f00) >> 8;
auto y = (chip8.opcode & 0x00f0) >> 4;
switch(code_select)
{
case 0x0000:
if(chip8.V[x] != chip8.V[y])
chip8.PC_increment(4);
else
chip8.PC_increment(2);
break;
default:
printf("Unknown opcode [0x9000]: 0x%X\n", chip8.opcode);
exit(-1);
break;
}
}
// Annn, Set I = nnn.
void opcode_A_fn_sets(Chip_8 & chip8){
chip8.I = chip8.opcode & 0x0FFF;
chip8.PC_increment();
}
// Bnnn, Jump to location nnn + V0.
void opcode_B_fn_sets(Chip_8 & chip8){
chip8.PC = (chip8.opcode & 0x0FFF) + chip8.V[0];
}
// Cxkk - RND Vx, byte
// Set Vx = random byte AND kk.
void opcode_C_fn_sets(Chip_8 & chip8){
auto x = (chip8.opcode & 0x0F00) >> 8;
auto max_byte = (chip8.opcode) & 0x00FF;
chip8.V[x] = (rand() % 0xFF) & max_byte;
chip8.PC_increment();
}
// Dxyn - DRW Vx, Vy, nibble
// Display n-byte sprite starting at memory location I at (Vx, Vy), set VF = collision.
void opcode_D_fn_sets(Chip_8 & chip8){
auto n = chip8.opcode & 0x000F;
auto x = chip8.V[(chip8.opcode & 0x0f00) >> 8];
auto y = chip8.V[(chip8.opcode & 0x00f0) >> 4];
unsigned char row_sprite = 0;
chip8.V[0xF] = 0;
for(size_t y_offest = 0; y_offest < n; y_offest++)
{
//printf("I = %d\n",chip8.I);
row_sprite = chip8.Memory[chip8.I + y_offest];
for(size_t x_offest = 0; x_offest < 8; x_offest++)
{
//printf("D (%d,%d) = %d\n",x + x_offest,(y + y_offest),row_sprite & (0x80 >> x_offest));
chip8.drawPixel(x + x_offest,
y + y_offest,
row_sprite & (0x80 >> x_offest) );
}
}
chip8.drawOnDisplay = true;
chip8.PC_increment();
}
// Ex9E - ExA1
void opcode_E_fn_sets(Chip_8 & chip8){
auto x = (chip8.opcode & 0x0f00) >> 8;
auto code_select = chip8.opcode & 0x00ff;
switch(code_select)
{
// Skip next instruction if key with the value of Vx is pressed.
case 0x009E:
if(chip8.key_status[chip8.V[x]] == CHIP_8_KEY_PRESSED)
chip8.PC_increment(4);
else
chip8.PC_increment();
break;
// Skip next instruction if key with the value of Vx is not press
case 0x00A1:
if(chip8.key_status[chip8.V[x]] == CHIP_8_KEY_NOT_PRESSED)
chip8.PC_increment(4);
else
chip8.PC_increment();
break;
default:
printf("Unknown opcode [0xE000]: 0x%X\n", chip8.opcode);
exit(-1);
break;
}
}
// Fx07 - Fx0A - Fx15 - Fx18 - Fx1E - Fx29 - Fx23 - Fx55 - Fx65
void opcode_F_fn_sets(Chip_8 & chip8){
auto x = (chip8.opcode & 0x0f00) >> 8;
auto code_select = chip8.opcode & 0x00FF;
switch(code_select)
{
// Set Vx = delay timer value.
case 0x07:
chip8.V[x] = chip8.DT;
chip8.PC_increment();
break;
// Wait for a key press, store the value of the key in Vx.
case 0x0A:
{
bool keyIsPressed = false;
for(size_t i = 0; i < 16; i++)
if(chip8.key_status[i] == CHIP_8_KEY_PRESSED)
{
chip8.V[x] = i;
keyIsPressed = true;
}
if(!keyIsPressed)
return; // Return To Skip This Cycle And repeat Fx0A opcode to wait for a key press.
chip8.PC_increment();
}
break;
// Set delay timer = Vx
case 0x15:
chip8.DT = chip8.V[x];
chip8.StartDelayTimer();
chip8.PC_increment();
break;
// Set sound timer = Vx.
case 0x18:
chip8.ST = chip8.V[x];
chip8.StartSoundTimer();
chip8.PC_increment();
break;
// Set I = I + Vx.
case 0x1E:
// Should I care about V[0xF] ? (Not mentioned in the specifications).
chip8.I += chip8.V[x];
chip8.PC_increment();
break;
// Set I = location of sprite for digit Vx.
case 0x29:
// The start of each hex-digit is 5 bytes along.
chip8.I = chip8.V[x]*0x05;
chip8.PC_increment();
break;
// Store BCD representation of Vx in memory locations I, I+1, and I+2
case 0x33:
chip8.Memory[chip8.I] = chip8.V[x] / 100; // The hundard digit .
chip8.Memory[chip8.I + 1] = (chip8.V[x] / 10) % 10; // The ten digit .
chip8.Memory[chip8.I + 2] = (chip8.V[x] % 100) % 10; // The one digit .
chip8.PC_increment();
break;
// Store registers V0 through Vx in memory starting at location I
case 0x55:
for(size_t i = 0; i <= x; i++)
chip8.Memory[chip8.I + i] = chip8.V[i];
chip8.PC_increment();
break;
// Read registers V0 through Vx from memory starting at location I.
case 0x65:
for(size_t i = 0; i <= x; i++)
chip8.V[i] = chip8.Memory[chip8.I + i];
chip8.PC_increment();
break;
default:
printf("Unknown opcode [0xF000]: 0x%X\n", chip8.opcode);
exit(-1);
break;
}
}