-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopcodes.c
More file actions
371 lines (342 loc) · 10.4 KB
/
opcodes.c
File metadata and controls
371 lines (342 loc) · 10.4 KB
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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "cpu.h"
#include "opcodes.h"
void sys(cpu_t* cpu,uint16_t current_opcode)
{
if((current_opcode & 0xF0) == 0xC0)
{
cpu->errno = ENIMP;
return;
}
switch(current_opcode & 0xFF)
{
case 0xE0:
memset(cpu->screen,0,(64 * 32));
cpu->pc += 2;
break;
case 0xEE:
if(cpu->stack_pointer == 0)
{
cpu->errno = ESTUF;
break;
}
cpu->pc = cpu->stack[cpu->stack_pointer];
cpu->stack_pointer--;
cpu->pc += 2; // expected to move past the CALL opcode
break;
default:
cpu->errno = EBDOP;
break;
}
}
void jmp(cpu_t* cpu,uint16_t current_opcode)
{
if((current_opcode & 0xF000) == 0x1000)
cpu->pc = (current_opcode & 0x0FFF);
else if((current_opcode & 0xF000) == 0xB000)
cpu->pc = (current_opcode & 0x0FFF) + cpu->registers[0x0];
}
void call(cpu_t* cpu,uint16_t current_opcode)
{
cpu->stack_pointer++;
cpu->stack[cpu->stack_pointer] = cpu->pc;
cpu->pc = (current_opcode & 0x0FFF);
}
void se(cpu_t* cpu,uint16_t current_opcode)
{
switch(current_opcode & 0xF000)
{
case 0x3000:
if(cpu->registers[(current_opcode & 0x0F00) >> 8] == (current_opcode & 0x00FF))
cpu->pc += 4;
else
cpu->pc += 2;
break;
case 0x5000:
if(cpu->registers[(current_opcode & 0x0F00) >> 8] == cpu->registers[(current_opcode & 0x00F0) >> 4])
cpu->pc += 4;
else
cpu->pc += 2;
break;
default:
cpu->errno = EBDOP;
break;
}
}
void sne(cpu_t* cpu,uint16_t current_opcode)
{
switch((current_opcode & 0xF000))
{
case 0x4000:
if(cpu->registers[(current_opcode & 0x0F00) >> 8] != (current_opcode & 0x00FF))
cpu->pc += 4;
else
cpu->pc += 2;
break;
case 0x9000:
if(cpu->registers[(current_opcode & 0x0F00) >> 8] != cpu->registers[(current_opcode & 0x00F0) >> 4])
cpu->pc += 4;
else
cpu->pc += 2;
break;
default:
cpu->errno = EBDOP;
break;
}
}
void ld(cpu_t* cpu,uint16_t current_opcode)
{
int i = 0;
if((current_opcode & 0xF000) == 0xF000)
{
switch(current_opcode & 0xFF)
{
case 0x07:
cpu->registers[(current_opcode & 0x0F00) >> 8] = cpu->delay;
break;
case 0x0A:
// wait until keypress is registered before continuing
cpu->wait = b_TRUE;
break;
case 0x15:
cpu->delay = cpu->registers[(current_opcode & 0x0F00) >> 8];
break;
case 0x18:
cpu->sound = cpu->registers[(current_opcode & 0x0F00) >> 8];
break;
case 0x29:
if(get_sprite_loc(cpu->registers[(current_opcode & 0x0F00) >> 8] & 0xF) < 0x200)
cpu->address = get_sprite_loc(cpu->registers[(current_opcode & 0x0F00) >> 8] & 0xF);
else
cpu->errno = ESEGV;
break;
case 0x33:
cpu->memory[cpu->address] = cpu->registers[((current_opcode & 0x0F00) >> 8)] / 100;
cpu->memory[cpu->address+1] = (cpu->registers[((current_opcode & 0x0F00) >> 8)] / 10) % 10;
cpu->memory[cpu->address+2] = cpu->registers[((current_opcode & 0x0F00) >> 8)] % 10;
break;
case 0x55:
for(i=0;i<((current_opcode & 0x0F00)>>8);i++)
{
cpu->memory[cpu->address+i] = cpu->registers[i];
}
cpu->address += ((current_opcode & 0x0F00)>>8)+1;
break;
case 0x65:
for(i=0;i<((current_opcode & 0x0F00)>>8);i++)
{
cpu->registers[i] = cpu->memory[cpu->address+i];
}
cpu->address += ((current_opcode & 0x0F00)>>8)+1;
break;
default:
cpu->errno = EBDOP;
break;
}
}
else if((current_opcode & 0xF00F) == 0x8000)
{
cpu->registers[(current_opcode & 0x0F00) >> 8] = cpu->registers[(current_opcode & 0x00F0) >> 4];
}
else if((current_opcode & 0xF000) == 0x6000)
{
cpu->registers[(current_opcode & 0x0F00) >> 8] = (current_opcode & 0x00FF);
}
else if((current_opcode & 0xF000) == 0xA000)
{
cpu->address = (current_opcode & 0x0FFF);
}
else
{
cpu->errno = EBDOP;
}
// make an exception for 0xF00A because it's the paste-eating child of this family of opcodes
if(current_opcode != 0xF00A)
cpu->pc += 2;
}
void add(cpu_t* cpu,uint16_t current_opcode)
{
uint8_t cache;
if((current_opcode & 0xF000) == 0x7000)
{
cpu->registers[(current_opcode & 0x0F00) >> 8] += (current_opcode & 0x00FF);
}
else if((current_opcode & 0xF00F) == 0x8004)
{
/*
var register_x = (opcode & 0x0F00) >> 8;
var register_y = (opcode & 0x00F0) >> 4;
this.registers[register_x] = this.registers[register_x] + this.registers[register_y];
if (this.registers[register_x] > 255)
{
this.registers[register_x] = this.registers[register_x] % 255;
// carry
this.registers[0xF] = 0x1;
}
else
{
// carry
this.registers[0xF] = 0x0;
}
*/
uint16_t new_value = cpu->registers[(current_opcode & 0x0F00) >> 8] + cpu->registers[(current_opcode & 0x00F0) >> 4];
if(new_value > 255)
{
cpu->registers[(current_opcode & 0x0F00) >> 8] = new_value % 255;
cpu->registers[0xF] = 0x01;
}
else
{
cpu->registers[(current_opcode & 0x0F00) >> 8] = new_value;
cpu->registers[0xF] = 0x00;
}
}
else if((current_opcode & 0xF0FF) == 0xF01E)
{
if((cpu->address + cpu->registers[(current_opcode & 0x0F00) >> 8]) > 0xFFF)
{
cpu->registers[0xF] = 1;
cpu->address = 0xFFF;
}
else
{
cpu->registers[0xF] = 0;
cpu->address += cpu->registers[(current_opcode & 0x0F00) >> 8];
}
}
else
{
cpu->errno = EBDOP;
}
cpu->pc += 2;
}
void or(cpu_t* cpu,uint16_t current_opcode)
{
cpu->registers[(current_opcode & 0x0F00) >> 8] |= cpu->registers[(current_opcode & 0x00F0) >> 4];
cpu->pc += 2;
}
void and(cpu_t* cpu,uint16_t current_opcode)
{
cpu->registers[(current_opcode & 0x0F00) >> 8] &= cpu->registers[(current_opcode & 0x00F0) >> 4];
cpu->pc += 2;
}
void xor(cpu_t* cpu,uint16_t current_opcode)
{
cpu->registers[(current_opcode & 0x0F00) >> 8] ^= cpu->registers[(current_opcode & 0x00F0) >> 4];
cpu->pc += 2;
}
void sub(cpu_t* cpu,uint16_t current_opcode)
{
if(cpu->registers[(current_opcode & 0x0F00) >> 8] > cpu->registers[(current_opcode & 0x00F0) >> 4])
cpu->registers[0xF] = 1;
else
cpu->registers[0xF] = 0;
cpu->registers[(current_opcode & 0x0F00) >> 8] -= cpu->registers[(current_opcode & 0x00F0) >> 4];
cpu->pc += 2;
}
void shr(cpu_t* cpu,uint16_t current_opcode)
{
cpu->registers[0xF] = cpu->registers[(current_opcode & 0x0F00) >> 8] & 0x1;
cpu->registers[(current_opcode & 0x0F00) >> 8] >> 1;
cpu->pc += 2;
}
void subn(cpu_t* cpu,uint16_t current_opcode)
{
if(cpu->registers[(current_opcode & 0x0F00) >> 8] > cpu->registers[(current_opcode & 0x00F0) >> 4])
cpu->registers[0xF] = 1;
else
cpu->registers[0xF] = 0;
cpu->registers[(current_opcode & 0x0F00) >> 8] = cpu->registers[(current_opcode & 0x00F0) >> 4] - cpu->registers[(current_opcode & 0x0F00) >> 8];
cpu->pc += 2;
}
void shl(cpu_t* cpu,uint16_t current_opcode)
{
cpu->registers[0xF] = (cpu->registers[(current_opcode & 0x0F00) >> 8] & 0x80) >> 7;
cpu->registers[(current_opcode & 0x0F00) >> 8] = cpu->registers[(current_opcode & 0x0F00) >> 8] << 1;
cpu->pc += 2;
}
void rnd(cpu_t* cpu,uint16_t current_opcode)
{
cpu->registers[(current_opcode & 0x0F00) >> 8] = (get_random_byte() & (current_opcode & 0x00FF));
cpu->pc += 2;
}
void drw(cpu_t* cpu,uint16_t current_opcode)
{
uint8_t num_bytes = (uint8_t)(current_opcode & 0x000F);
uint8_t x_coord = cpu->registers[(current_opcode & 0x0F00) >> 8];
uint8_t y_coord = cpu->registers[(current_opcode & 0x00F0) >> 4];
uint8_t sprite_byte;
uint8_t pixels[8] = {0};
unsigned int i = 0;
// no collision yet
cpu->registers[0xF] = 0x0;
for(i=0;i<num_bytes;i++)
{
sprite_byte = cpu->memory[cpu->address + i];
// set up the individual pixels
pixels[0] = (sprite_byte & 0x80) >> 7;
pixels[1] = (sprite_byte & 0x40) >> 6;
pixels[2] = (sprite_byte & 0x20) >> 5;
pixels[3] = (sprite_byte & 0x10) >> 4;
pixels[4] = (sprite_byte & 0x08) >> 3;
pixels[5] = (sprite_byte & 0x04) >> 2;
pixels[6] = (sprite_byte & 0x02) >> 1;
pixels[7] = (sprite_byte & 0x01);
// XOR them into video memory, wrapping round if it's too big
cpu->screen[(x_coord+0) % 64][(y_coord+i) % 32] ^= pixels[0];
cpu->screen[(x_coord+1) % 64][(y_coord+i) % 32] ^= pixels[1];
cpu->screen[(x_coord+2) % 64][(y_coord+i) % 32] ^= pixels[2];
cpu->screen[(x_coord+3) % 64][(y_coord+i) % 32] ^= pixels[3];
cpu->screen[(x_coord+4) % 64][(y_coord+i) % 32] ^= pixels[4];
cpu->screen[(x_coord+5) % 64][(y_coord+i) % 32] ^= pixels[5];
cpu->screen[(x_coord+6) % 64][(y_coord+i) % 32] ^= pixels[6];
cpu->screen[(x_coord+7) % 64][(y_coord+i) % 32] ^= pixels[7];
// first check if the register is unset - otherwise we waste a lot of time
// making pointless comparisons (it can't really be set twice!)
if( cpu->registers[0xF] == 0 && (
// if the old pixel was set
cpu->screen[(x_coord+0) % 64][(y_coord+i) % 32] & 0x1 ||
cpu->screen[(x_coord+1) % 64][(y_coord+i) % 32] & 0x1 ||
cpu->screen[(x_coord+2) % 64][(y_coord+i) % 32] & 0x1 ||
cpu->screen[(x_coord+3) % 64][(y_coord+i) % 32] & 0x1 ||
cpu->screen[(x_coord+4) % 64][(y_coord+i) % 32] & 0x1 ||
cpu->screen[(x_coord+5) % 64][(y_coord+i) % 32] & 0x1 ||
cpu->screen[(x_coord+6) % 64][(y_coord+i) % 32] & 0x1 ||
cpu->screen[(x_coord+7) % 64][(y_coord+i) % 32] & 0x1))
{
cpu->registers[0xF] = 1;
}
}
cpu->draw = 1;
cpu->pc += 2;
}
// this macro just gives us a bit of a helping hand getting to the right bit
#define key_bit(x) (1<<x)
void skp(cpu_t* cpu,uint16_t current_opcode)
{
uint16_t which_key = cpu->registers[(current_opcode & 0x0F00) >> 8];
if((cpu->keypad & key_bit(which_key)))
cpu->pc += 4;
else
cpu->pc += 2;
}
void sknp(cpu_t* cpu,uint16_t current_opcode)
{
uint16_t which_key = cpu->registers[(current_opcode & 0x0F00) >> 8];
if(!(cpu->keypad & key_bit(which_key)))
cpu->pc += 4;
else
cpu->pc += 2;
}
// helper functions
uint16_t get_sprite_loc(uint8_t sprite)
{
return (sprite & 0xF) * 6; // sprites are at 10-byte offsets, handily
}
uint8_t get_random_byte(void)
{
return (uint8_t)(rand() % 256);
}