-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComputer.c
293 lines (274 loc) · 8.04 KB
/
Computer.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
#include "Computer.h"
void SetupComputer() {
for (int i = 0; i < REGISTER_SIZE; i++)
registers[i] = 0;
index_register = 0;
program_counter_register = PROGRAM_ROM;
for (int i = 0; i < STACK_SIZE; i++)
stack[i] = 0;
stack_pointer = 0;
delay_timer = 0;
sound_timer = 0;
opcode = 0;
}
void UpdateTimers() {
//FIXME::PLAY SOUND WHEN TIMER IS ABOVE WHEN
if (delay_timer > 0)
delay_timer--;
if (sound_timer > 0) {
PlayBeep();
sound_timer--;
}
else {
StopBeep();
}
}
void FetchInstruction() {
opcode = memory[program_counter_register] << 8 | memory[program_counter_register + 1];
}
void PrintRegisters() {
for (int i = 0; i < REGISTER_SIZE; i++) {
printf("REGISTER %d", i); printf(" EQUALS %X\n", registers[i]);
}
}
void ExecuteInsruction(char* draw_flag) {
//variables for switch statement
unsigned short position_x;
unsigned short position_y;
unsigned short height;
unsigned short pixel;
unsigned char set;
unsigned char final_x;
unsigned char final_y;
unsigned char reg;
unsigned char value;
switch (opcode & 0xF000) {
case 0x0000:
switch (0x00FF & opcode) {
case 0x00E0: //CLS
SetupScreen();
program_counter_register += 2;
break;
case 0x00EE: //RET
stack_pointer--;
program_counter_register = stack[stack_pointer];
program_counter_register += 2;
break;
default:
printf("Opcode doesn't exist! %X\n", opcode);
exit(-1);
}
break;
case 0x1000: //JP addr
program_counter_register = 0x0FFF & opcode;
break;
case 0x2000: //CALL addr
stack[stack_pointer] = program_counter_register;
stack_pointer++;
program_counter_register = 0x0FFF & opcode;
break;
case 0x3000: //SE Vx, byte
if (registers[(0x0F00 & opcode) >> 8] == (0x00FF & opcode))
program_counter_register += 2;
program_counter_register += 2;
break;
case 0x4000: //SE Vx, byte
if (registers[(0x0F00 & opcode) >> 8] != (0x00FF & opcode))
program_counter_register += 2;
program_counter_register += 2;
break;
case 0x5000: //SE Vx, Vy
if (registers[(0x0F00 & opcode) >> 8] == registers[(0x00F0 & opcode) >> 4])
program_counter_register += 2;
program_counter_register += 2;
break;
case 0x6000: //LD, Vx, byte
registers[(0x0F00 & opcode) >> 8] = 0x00FF & opcode;
program_counter_register += 2;
break;
case 0x7000: //ADD Vx, byte
registers[(0x0F00 & opcode) >> 8] += 0x00FF & opcode;
program_counter_register += 2;
break;
case 0x8000:
switch (0x000F & opcode) {
case 0x0000: //LD Vx, Vy
registers[(0x0F00 & opcode) >> 8] = registers[(0x00F0 & opcode) >> 4];
program_counter_register += 2;
break;
case 0x0001: //OR Vx, Vy
registers[(0x0F00 & opcode) >> 8] |= registers[(0x00F0 & opcode) >> 4];
program_counter_register += 2;
break;
case 0x0002: //AND Vx, Vy
registers[(0x0F00 & opcode) >> 8] &= registers[(0x00F0 & opcode) >> 4];
program_counter_register += 2;
break;
case 0x0003: //XOR Vx, Vy
registers[(0x0F00 & opcode) >> 8] ^= registers[(0x00F0 & opcode) >> 4];
program_counter_register += 2;
break;
case 0x0004: //ADD Vx, Vy
if (registers[(0x0F00 & opcode) >> 8] + registers[(0x00F0 & opcode) >> 4] > 255)
registers[0xF] = 1;
else
registers[0xF] = 0;
registers[(0x0F00 & opcode) >> 8] += registers[(0x00F0 & opcode) >> 4];
program_counter_register += 2;
break;
case 0x0005: //SUB Vx, Vy
if (registers[(0x0F00 & opcode) >> 8] > registers[(0x00F0 & opcode) >> 4])
registers[0xF] = 1;
else
registers[0xF] = 0;
registers[(0x0F00 & opcode) >> 8] -= registers[(0x00F0 & opcode) >> 4];
program_counter_register += 2;
break;
case 0x0006: //SHR Vx {, Vy}
reg = (0x0F00 & opcode) >> 8;
registers[0xF] = registers[reg] & 0x1;
registers[reg] >>= 1;
program_counter_register += 2;
break;
case 0x0007: //SUBN Vx, Vy
if (registers[(0x00F0 & opcode) >> 4] > registers[(0x0F00 & opcode) >> 8])
registers[0xF] = 1;
else
registers[0xF] = 0;
registers[(0x0F00 & opcode) >> 8] = registers[(0x00F0 & opcode) >> 4] - registers[(0x0F00 & opcode) >> 8];
program_counter_register += 2;
break;
case 0x000E: //SHL Vx {, Vy}
reg = (0x0F00 & opcode) >> 8;
registers[0xF] = (registers[reg] & 0x80) >> 7;
registers[reg] <<= 1;
program_counter_register += 2;
break;
default:
printf("Opcode doesn't exist! %X\n", opcode);
exit(-1);
}
break;
case 0x9000: //SNE Vx, Vy
if (registers[(0x0F00 & opcode) >> 8] != registers[(0x00F0 & opcode) >> 4])
program_counter_register += 2;
program_counter_register += 2;
break;
case 0xA000: //LD I, addr
index_register = 0x0FFF & opcode;
program_counter_register += 2;
break;
case 0xB000: //JP v0, addr
program_counter_register = registers[0] + (0x0FFF & opcode);
break;
case 0xC000:
registers[(0x0F00 & opcode) >> 8] = GenerateRandByte() & (0x00FF & opcode);
program_counter_register += 2;
break;
case 0xD000: //DRW Vx, Vy nibble
position_x = registers[(0x0F00 & opcode) >> 8];
position_y = registers[(0x00F0 & opcode) >> 4];
height = 0x000F & opcode;
registers[0xF] = 0;
for (int yline = 0; yline < height; yline++){
pixel = memory[index_register + yline];
for (int xline = 0; xline < 8; xline++){
if ((pixel & (0x80 >> xline)) != 0){
final_x = (position_x + xline) % WINDOW_WIDTH_RES;
final_y = (position_y + yline) % WINDOW_HEIGHT_RES;
if (screen[(final_y * WINDOW_WIDTH_RES) + final_x])
registers[0xF] = 1;
screen[(final_y * WINDOW_WIDTH_RES) + final_x] ^= 1;
}
}
}
*draw_flag = 1;
program_counter_register += 2;
break;
case 0xE000:
switch(0x00FF & opcode){
case 0x009E: //SKP Vx
if (keys[registers[(0x0F00 & opcode) >> 8]] == 1)
program_counter_register += 2;
program_counter_register += 2;
break;
case 0x00A1: //SKNP Vx
if (keys[registers[(0x0F00 & opcode) >> 8]] != 1) {
program_counter_register += 2;
}
program_counter_register += 2;
break;
default:
printf("Opcode doesn't exist! %X\n", opcode);
exit(-1);
}
break;
case 0xF000:
switch (0x00FF & opcode) {
case 0x0007: //LD Vx, DT
registers[(0x0F00 & opcode) >> 8] = delay_timer;
program_counter_register += 2;
break;
case 0x000A: //LD Vx, K
reg = (0x0F00 & opcode) >> 8;
set = 0;
for (int i = 0; i < KEY_SIZE; i++) {
if (keys[i] == 1) {
set = 1;
registers[reg] = i;
}
}
if (set != 0)
program_counter_register += 2;
break;
case 0x0015: //LD DT, Vx
delay_timer = registers[(0x0F00 & opcode) >> 8];
program_counter_register += 2;
break;
case 0x0018: //LD ST, Vx
sound_timer = registers[(0x0F00 & opcode) >> 8];
program_counter_register += 2;
break;
case 0x001E: //ADD I, Vx
index_register += registers[(0x0F00 & opcode) >> 8];
program_counter_register += 2;
break;
case 0x0029: //LD F, Vx
index_register = FONT_DATA_LOCATION + (registers[(0x0F00 & opcode) >> 8] * FONT_CHARACTER_SIZE);
program_counter_register += 2;
break;
case 0x0033: //LD B, Vx
reg = (0x0F00 & opcode) >> 8;
value = registers[reg];
memory[index_register + 2] = value % 10;
value /= 10;
memory[index_register + 1] = value % 10;
value /= 10;
memory[index_register] = value % 10;
program_counter_register += 2;
break;
case 0x0055: //LD [I], Vx
reg = (0x0F00 & opcode) >> 8;
for (int i = 0; i <= reg; i++)
memory[index_register + i] = registers[i];
program_counter_register += 2;
break;
case 0x0065: //LD Vx, [I]
reg = (0x0F00 & opcode) >> 8;
for (int i = 0; i <= reg; i++)
registers[i] = memory[index_register + i];
program_counter_register += 2;
break;
default:
printf("Opcode doesn't exist! %X\n", opcode);
exit(-1);
}
break;
default:
printf("Opcode doesn't exist! %X\n", opcode);
exit(-1);
}
}
int GenerateRandByte() {
return rand() % 255;
}