-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpu.c
More file actions
365 lines (338 loc) · 8.14 KB
/
cpu.c
File metadata and controls
365 lines (338 loc) · 8.14 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
/*
* =====================================================================================
*
* Filename: emulateCycle.c
*
* Description: cpu emulation
*
* Version: 1.0
* Created: 07/15/2017 12:55:54 AM
* Revision: none
* Compiler: clang
*
* Author: Viktor Horsmanheimo
*
* =====================================================================================
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "chip8.h"
#define columnWidth 64
static int firstNibble = 0xF000;
static int secondNibble = 0x000F;
unsigned short positionX = 0;
unsigned short positionY = 0;
static inline void clearScreen();
static inline void returnFromSubroutine();
static inline void setAddress();
static inline void goToSubroutine();
static inline void eqVx();
static inline void neqVx();
static inline void eqVxWithShift();
static inline void setVx();
static inline void addVx();
static void opcode_8000();
static int shift4(int opcode);
static int shift8(int opcode);
void emulateCycle(){
c8->opcode = (c8->memory[c8->pc] << 8)|c8->memory[c8->pc + 1];
printf("executing opcode: %X at: %X, sp: %d\n", c8->opcode, c8->pc, c8->sp);
switch(c8->opcode & firstNibble){
case 0x0000:
switch(c8->opcode & secondNibble){
case 0x0000:
clearScreen();
break;
case 0x000E:
returnFromSubroutine();
break;
default:
printf("unkown opcode: %X\n", c8->opcode);
break;
}
break;
case 0x1000:
setAddress();
break;
case 0x2000:
goToSubroutine();
break;
case 0x3000:
eqVx();
break;
case 0x4000:
neqVx();
break;
case 0x5000:
eqVxWithShift();
break;
case 0x6000:
setVx();
break;
case 0x7000:
addVx();
break;
case 0x8000:
opcode_8000();
break;
case 0x9000:
if(c8->V[shift8(c8->opcode)] != c8->V[shift4(c8->opcode)])
c8->pc += 4;
else c8->pc += 2;
break;
case 0xA000:
c8->I = c8->opcode & 0x0FFF;
c8->pc += 2;
break;
case 0xB000:
c8->pc = (c8->opcode & 0x0FFF) + c8->V[0];
break;
case 0xC000:
c8->V[shift8(c8->opcode)] = (rand() % 256) & (c8->opcode & 0x00FF);
c8->pc += 2;
break;
case 0xD000:
c8->V[0xF] = 0;
int xCoordinate = c8->V[shift8(c8->opcode)];
int yCoordinate = c8->V[shift4(c8->opcode)];
uint8_t height = c8->opcode & 0x000F;
uint8_t pixel = 0, width = 8;
int coordinate = 0;
uint8_t columnHeight = 32;
for(int y = 0; y < height; y++){
pixel = c8->memory[c8->I + y];
for(int x = 0; x < width; x++){
if((pixel & (0x80 >> x))!= 0) {
/* TODO: fix this garbage */
int realX = (xCoordinate + x) > columnWidth-1 ?
(xCoordinate + x) - columnWidth-1 :
(xCoordinate+x) < 0 ?
columnWidth-1 - (xCoordinate+x) :
(xCoordinate+x);
int realY = (yCoordinate + y) > columnHeight-1 ?
(yCoordinate + y) - columnHeight-1 :
(yCoordinate+y) < 0 ?
columnHeight-1 - (yCoordinate+y) :
(yCoordinate+y);
coordinate = ((realY)*columnWidth)+ (realX);
if(c8->gfx[coordinate] == 255)
c8->V[0xF] = 1;
c8->gfx[coordinate] ^= 255;
}
}
}
c8->flag = 1;
c8->pc += 2;
break;
case 0xE000:
switch(c8->opcode & 0x00F0){
case 0x0090:
//if key pressed skip instruction
;
short key1 = c8->V[shift8(c8->opcode)];
if(key1 < 16 && c8->key[key1] == 1)
c8->pc += 4;
else c8->pc += 2;
break;
case 0x00A0:
//if key not pressed skip next instruction
;
short key2 = c8->V[shift8(c8->opcode)];
if(c8->key[key2] == 0)
c8->pc += 4;
else c8->pc += 2;
break;
default:
printf("unkown opcode: %X\n", c8->opcode);
break;
}
break;
case 0xF000:
switch(c8->opcode & 0x00FF){
case 0x0007:
c8->V[shift8(c8->opcode)] = c8->delay_timer;
c8->pc+=2;
break;
case 0x000A:
;
char keypress = 0;
for(int i = 0; i <16; i++){
if(c8->key[i]){
keypress = 1;
c8->V[shift8(c8->opcode)] = i;
break;
}
}
if(!keypress)
return;
c8->pc+=2;
break;
case 0x0015:
c8->delay_timer = c8->V[shift8(c8->opcode)];
c8->pc += 2;
break;
case 0x0018:
printf("sound_timer: %d", shift8(c8->opcode));
c8->sound_timer = c8->V[shift8(c8->opcode)];
c8->pc += 2;
break;
case 0x001E:
c8->I += c8->V[shift8(c8->opcode)];
c8->pc += 2;
break;
case 0x0029:
c8->I = c8->V[shift8(c8->opcode)] * 5;
c8->pc += 2;
break;
case 0x0033:
positionX = shift8(c8->opcode);
c8->memory[c8->I] = c8->V[positionX] / 100;
c8->memory[c8->I + 1] = (c8->V[positionX] / 10) % 10;
c8->memory[c8->I + 2] = c8->V[positionX] % 10;
c8->pc += 2;
break;
case 0x0055:
positionX = shift8(c8->opcode);
for(int i = 0; i < positionX; i++){
c8->memory[c8->I + i] = c8->V[i];
}
c8->pc += 2;
break;
case 0x0065:
positionX = shift8(c8->opcode);
for(int i = 0; i <= positionX; i++){
c8->V[i] = c8->memory[c8->I + i];
}
c8->pc += 2;
break;
}
break;
default:
printf("unknown opcode: %X\n", c8->opcode);
break;
}
if(c8->delay_timer)
c8->delay_timer--;
if(c8->sound_timer)
c8->sound_timer--;
}
static inline void clearScreen(){
memset(c8->gfx, 0, sizeof(c8->gfx));
c8->pc += 2;
}
static inline void returnFromSubroutine(){
if(c8->sp)
c8->sp -= 1;
c8->pc = c8->stack[c8->sp];
c8->pc += 2;
}
static inline void setAddress(){
c8->pc = c8->opcode & 0x0FFF;
}
static inline void goToSubroutine(){
c8->stack[c8->sp] = c8->pc;
c8->sp += 1;
c8->pc = c8->opcode & 0x0FFF;
}
static inline void eqVx(){ /* something wrong here maybe */
if(c8->V[shift8(c8->opcode)] == (c8->opcode & 0x00FF))
c8->pc+=4;
else c8->pc += 2;
}
static inline void neqVx(){
if(c8->V[shift8(c8->opcode)] != (c8->opcode & 0x00FF))
c8->pc += 4;
else c8->pc += 2;
}
static inline void eqVxWithShift(){ /* 0x5XY0 */
if(c8->V[shift8(c8->opcode)] == c8->V[shift4(c8->opcode)])
c8->pc += 4;
else c8->pc += 2;
}
static inline void setVx(){
c8->V[shift8(c8->opcode)] = c8->opcode & 0x00FF;
c8->pc += 2;
}
static inline void addVx(){
c8->V[shift8(c8->opcode)] += c8->opcode & 0x00FF;
c8->pc += 2;
}
static void opcode_8000(){
switch(c8->opcode & 0x000F){
case 0x0000:
c8->V[shift8(c8->opcode)] =
c8->V[shift4(c8->opcode)];
c8->pc += 2;
break;
case 0x0001:
c8->V[shift8(c8->opcode)] = c8->V[shift8(c8->opcode)] | c8->V[shift4(c8->opcode)];
c8->pc += 2;
break;
case 0x0002:
c8->V[shift8(c8->opcode)] = c8->V[shift8(c8->opcode)] & c8->V[shift4(c8->opcode)];
//adds 6 instead of 2 for some reason???
c8->pc += 2;
break;
case 0x0003:
c8->V[shift8(c8->opcode)] ^=
c8->V[shift4(c8->opcode)];
c8->pc += 2;
break;
case 0x0004:
positionX = shift8(c8->opcode);
if(c8->V[positionX] + c8->V[shift4(c8->opcode)] > 255)
c8->V[0xF] = 1;
else c8->V[0xF] = 0;
c8->V[positionX] += (c8->V[shift4(c8->opcode)] & 0x00FF);
c8->pc += 2;
break;
case 0x0005:
positionX = shift8(c8->opcode);
positionY = shift4(c8->opcode);
if(c8->V[positionX] > c8->V[positionY])
c8->V[0xF] = 1;
else c8->V[0xF] = 0;
c8->V[positionX] -= c8->V[positionY];
c8->pc += 2;
break;
case 0x0006:
positionX = shift8(c8->opcode);
c8->V[0xF] = c8->V[positionX] & 0xFE;
c8->V[positionX] >>= 1;
c8->pc += 2;
break;
case 0x0007:
positionX = shift8(c8->opcode);
positionY = shift4(c8->opcode);
if(c8->V[positionX] < c8->V[positionY])
c8->V[0xF] = 1;
else c8->V[0xF] = 0;
c8->V[positionX] =
c8->V[positionY] - c8->V[positionX];
c8->pc += 2;
break;
case 0x000E:
positionX = shift8(c8->opcode);
c8->V[0xF] =
c8->V[positionX] >> 7;
c8->V[positionX] <<= 1;
c8->pc+=2;
break;
default:
printf("unkown opcode: %X\n", c8->opcode);
break;
}
}
static int shift4(int opcode) {
return (opcode & 0x00F0) >> 4;
}
static int shift8(int opcode) {
return (opcode & 0x0F00) >> 8;
}
/*
static void extract_bits(unsigned char *dst, char src){
for(int i = 0; i < 8; i++)
dst[i] ^= src & (1 << i) ? 0xFF : 0;
}*/