add opcodes from before
[swap_emu.git] / cpu / cpu.c
blobf24e152248929a321382278113228f7b2b5a2f29
1 #include "cpu_definitions.h"
2 #include <stdio.h>
3 /* CPU Status flags */
4 uint64_t N = 0x00000000; //Negative
5 uint64_t Z = 0x00000000; //Zero
6 uint64_t C = 0x00000000; //Carry (or Unsigned Overflow)
7 uint64_t V = 0x00000000; //Overflow (Signed)
8 uint64_t opcode;
9 uint64_t PC = 0x00000000; //Program Counter
10 int isRunning;
11 uint64_t *mem; //mem
13 void mem_writeData(uint64_t *data) {
14 uint64_t despacito = 0x00000000;
15 uint64_t dametucosita = 0x00000000;
16 while(sizeof(despacito) < sizeof(data)) {
17 mem[despacito] = data[dametucosita];
18 despacito = despacito + 0x01;
19 dametucosita = dametucosita + 0x01;
23 void mem_write(uint64_t addr, uint64_t value) {
24 mem[addr] = value;
27 uint64_t mem_read(uint64_t addr) {
28 return mem[addr];
31 void start_cpu() { //Start emulating the CPU
32 isRunning = 1;
33 while(isRunning == 1) {
34 opcode = mem[PC]; //fetch opcode
35 interpret(opcode); //interpret opcode
39 void stop_cpu() { //Stop emulating the CPU
40 isRunning = 0;
43 void interpret(uint64_t opcode) { /* emulate opcode */
44 switch(opcode) { //ADC (Add with Carry)
45 /* Add/subtract (with carry). */
46 case 0x1a000000:
47 mem[pc+1] = mem[pc+2] + mem[pc+3] + C;
48 //dest = op_1 + op_2 + Carry
49 if (mem[pc+1] => 0xffffffff) { //overflow (unsigned signed)?
50 C = 0x00000001;
51 pc = pc + 1;
52 break;
54 if (mem[pc+1] < 0x00000000) { //negative?
55 N = 0x00000001;
56 pc = pc + 1;
57 break;
59 if (mem[pc+1] = 0x00000000) { //zero?
60 Z = 0x00000001;
61 pc = pc + 1;
62 break;
64 pc = pc + 1;
65 break;
66 case 0x3a000000: //ADCS (Add with Carry With Status)
67 mem[pc+1] = mem[pc+2] + mem[pc+3] + C;
68 if (mem[pc+1] => 0xffffffff) { //overflow (unsigned signed)?
69 C = 0x00000001;
70 pc = pc + 1;
71 break;
73 if (mem[pc+1] < 0x00000000) { //negative?
74 N = 0x00000001;
75 pc = pc + 1;
76 break;
78 if (mem[pc+1] = 0x00000000) { //zero?
79 Z = 0x00000001;
80 pc = pc + 1;
81 break;
83 //dest = op_1 + op_2 + Carry
84 pc = pc + 1;
85 break;
86 case 0x5a000000: //SBC (Subtract with Carry)
87 mem[pc+1] = mem[pc+2] - mem[pc+3] - ~C;
88 if (mem[pc+1] => 0xffffffff) { //overflow (unsigned signed)?
89 C = 0x00000001;
90 pc = pc + 1;
91 break;
93 if (mem[pc+1] < 0x00000000) { //negative?
94 N = 0x00000001;
95 pc = pc + 1;
96 break;
98 if (mem[pc+1] = 0x00000000) { //zero?
99 Z = 0x00000001;
100 pc = pc + 1;
101 break;
103 //dest = op_1 - op_2 - NOT(Carry)
104 pc = pc + 1;
105 break;
106 case 0x7a000000: //SBCS (Subtract with Carry With Status)
107 mem[pc+1] = mem[pc+2] - mem[pc+3] - ~C;
108 if (mem[pc+1] => 0xffffffff) { //overflow (unsigned signed)?
109 C = 0x00000001;
110 pc = pc + 1;
111 break;
113 if (mem[pc+1] < 0x00000000) { //negative?
114 N = 0x00000001;
115 pc = pc + 1;
116 break;
118 if (mem[pc+1] = 0x00000000) { //zero?
119 Z = 0x00000001;
120 pc = pc + 1;
121 break;
123 //dest = op_1 - op_2 - NOT(Carry)
124 pc = pc + 1;
125 break;
126 case 0x5a0003e0: //NGC (Negate with Carry) (This instruction is an alias of SBC.)
127 mem[pc+1] = mem[pc+2] - mem[pc+3] - ~C;
128 if (mem[pc+1] => 0xffffffff) { //overflow (unsigned signed)?
129 C = 0x00000001;
130 pc = pc + 1;
131 break;
133 if (mem[pc+1] < 0x00000000) { //negative?
134 N = 0x00000001;
135 pc = pc + 1;
136 break;
138 if (mem[pc+1] = 0x00000000) { //zero?
139 Z = 0x00000001;
140 pc = pc + 1;
141 break;
143 //dest = op_1 - op_2 - NOT(Carry)
144 pc = pc + 1;
145 break;
146 case 0x7a0003e0: //NGCS (Negate with Carry With Status) (This instruction is an alias of SBCS.)
147 mem[pc+1] = mem[pc+2] - mem[pc+3] - ~C;
148 if (mem[pc+1] => 0xffffffff) { //overflow (unsigned signed)?
149 C = 0x00000001;
150 pc = pc + 1;
151 break;
153 if (mem[pc+1] < 0x00000000) { //negative?
154 N = 0x00000001;
155 pc = pc + 1;
156 break;
158 if (mem[pc+1] = 0x00000000) { //zero?
159 Z = 0x00000001;
160 pc = pc + 1;
161 break;
163 //dest = op_1 - op_2 - NOT(Carry)
164 pc = pc + 1;
165 break;
166 /* Add/subtract (extended register). */
167 case 0x0b200000: //ADD (adds...)
168 mem[pc+1] = mem[pc+2] + mem[pc+3];
169 if (mem[pc+1] => 0xffffffff) { //overflow (unsigned signed)?
170 C = 0x00000001;
171 pc = pc + 1;
172 break;
174 if (mem[pc+1] < 0x00000000) { //negative?
175 N = 0x00000001;
176 pc = pc + 1;
177 break;
179 if (mem[pc+1] = 0x00000000) { //zero?
180 Z = 0x00000001;
181 pc = pc + 1;
182 break;
184 //dest = op_1 + op_2
185 pc = pc + 1;
186 break;
187 case 0x2b200000: //ADDS (adds With Status)
188 mem[pc+1] = mem[pc+2] + mem[pc+3];
189 if (mem[pc+1] => 0xffffffff) { //overflow (unsigned signed)?
190 C = 0x00000001;
191 pc = pc + 1;
192 break;
194 if (mem[pc+1] < 0x00000000) { //negative?
195 N = 0x00000001;
196 pc = pc + 1;
197 break;
199 if (mem[pc+1] = 0x00000000) { //zero?
200 Z = 0x00000001;
201 pc = pc + 1;
202 break;
204 //dest = op_1 + op_2
205 pc = pc + 1;
206 break;
207 case 0x2b20001f: //CMN (Compare Negative)
208 N = mem[pc+2] - ~mem[pc+3];
209 if (mem[pc+1] => 0xffffffff) { //overflow (unsigned signed)?C = 0x00000001;
210 pc = pc + 1;
211 break;
213 if (mem[pc+1] = 0x00000000) { //zero?
214 Z = 0x00000001;
215 pc = pc + 1;
216 break;
218 //<flags> = op_1 - (NOT op_2) ; result is not stored, only flags updated
219 pc = pc + 1;
220 break;
221 case 0x4b200000: //SUB (substracts)
222 mem[pc+1] = mem[pc+2] - mem[pc+3];
223 if (mem[pc+1] => 0xffffffff) { //overflow (unsigned signed)?
224 C = 0x00000001;
225 pc = pc + 1;
226 break;
228 if (mem[pc+1] < 0x00000000) { //negative?
229 N = 0x00000001;
230 pc = pc + 1;
231 break;
233 if (mem[pc+1] = 0x00000000) { //zero?
234 Z = 0x00000001;
235 pc = pc + 1;
236 break;
238 //dest = op_1 - op_2
239 pc = pc + 1;
240 break;
241 case 0x6b200000: //SUBS (substracts with status)
242 mem[pc+1] = mem[pc+2] - mem[pc+3];
243 if (mem[pc+1] => 0xffffffff) { //overflow (unsigned signed)?
244 C = 0x00000001;
245 pc = pc + 1;
246 break;
248 if (mem[pc+1] < 0x00000000) { //negative?
249 N = 0x00000001;
250 pc = pc + 1;
251 break;
253 if (mem[pc+1] = 0x00000000) { //zero?
254 Z = 0x00000001;
255 pc = pc + 1;
256 break;
258 //dest = op_1 - op_2
259 pc = pc + 1;
260 break;
261 case 0x6b20001f: //CMP (COMPARE)
262 N = mem[pc+2] - mem[pc+3];
263 if (mem[pc+1] => 0xffffffff) { //overflow (unsigned signed)?
264 C = 0x00000001;
265 pc = pc + 1;
266 break;
268 if (mem[pc+1] = 0x00000000) { //zero?
269 Z = 0x00000001;
270 pc = pc + 1;
271 break;
273 //<flags> = op_1 - op_2 ; result is not stored, only flags updated
274 pc = pc + 1;
275 break;
277 /* Add/subtract (immediate). */
278 case 0x11000000: //ADD (adds...)
279 mem[pc+1] = mem[pc+2] + mem[pc+3];
280 if (mem[pc+1] => 0xffffffff) { //overflow (unsigned signed)?
281 C = 0x00000001;
282 pc = pc + 1;
283 break;
285 if (mem[pc+1] < 0x00000000) { //negative?
286 N = 0x00000001;
287 pc = pc + 1;
288 break;
290 if (mem[pc+1] = 0x00000000) { //zero?
291 Z = 0x00000001;
292 pc = pc + 1;
293 break;
295 //dest = op_1 + op_2
296 pc = pc + 1;
297 break;
299 case 0x11000000: // mov ( moves )
300 mem[pc+1] = mem[pc+2];
301 if (mem[pc+1] => 0xffffffff) { //overflow (unsigned signed)?
302 C = 0x00000001;
303 pc = pc + 1;
304 break;
306 if (mem[pc+1] < 0x00000000) { //negative?
307 N = 0x00000001;
308 pc = pc + 1;
309 break;
311 if (mem[pc+1] = 0x00000000) { //zero?
312 Z = 0x00000001;
313 pc = pc + 1;
314 break;
316 //dest = op_1
317 pc = pc + 1;
318 break;
319 case 0x31000000: //ADD (adds...)
320 mem[pc+1] = mem[pc+2] + mem[pc+3];
321 if (mem[pc+1] => 0xffffffff) { //overflow (unsigned signed)?
322 C = 0x00000001;
323 pc = pc + 1;
324 break;
326 if (mem[pc+1] < 0x00000000) { //negative?
327 N = 0x00000001;
328 pc = pc + 1;
329 break;
331 if (mem[pc+1] = 0x00000000) { //zero?
332 Z = 0x00000001;
333 pc = pc + 1;
334 break;
336 //dest = op_1 + op_2
337 pc = pc + 1;
338 break;
339 case 0x3100001f: //CMN (Compare Negative)
340 N = mem[pc+2] - ~mem[pc+3];
341 if (mem[pc+1] => 0xffffffff) { //overflow (unsigned signed)?
342 C = 0x00000001;
343 pc = pc + 1;
344 break;
346 if (mem[pc+1] = 0x00000000) { //zero?
347 Z = 0x00000001;
348 pc = pc + 1;
349 break;
351 //<flags> = op_1 - (NOT op_2) ; result is not stored, only flags updated
352 pc = pc + 1;
353 break;
354 case 0x51000000: //SUB (substracts)
355 mem[pc+1] = mem[pc+2] - mem[pc+3];
356 if (mem[pc+1] => 0xffffffff) { //overflow (unsigned signed)?
357 C = 0x00000001;
358 pc = pc + 1;
359 break;
361 if (mem[pc+1] < 0x00000000) { //negative?
362 N = 0x00000001;
363 pc = pc + 1;
364 break;
366 if (mem[pc+1] = 0x00000000) { //zero?
367 Z = 0x00000001;
368 pc = pc + 1;
369 break;
371 //dest = op_1 - op_2
372 pc = pc + 1;
373 break;
374 case 0x71000000: //SUBS (substracts with flags)
375 mem[pc+1] = mem[pc+2] - mem[pc+3];
376 if (mem[pc+1] => 0xffffffff) { //overflow (unsigned signed)?
377 C = 0x00000001;
378 pc = pc + 1;
379 break;
381 if (mem[pc+1] < 0x00000000) { //negative?
382 N = 0x00000001;
383 pc = pc + 1;
384 break;
386 if (mem[pc+1] = 0x00000000) { //zero?
387 Z = 0x00000001;
388 pc = pc + 1;
389 break;
391 //dest = op_1 - op_2
392 pc = pc + 1;
393 break;
394 case 0x7100001f: //CMP (COMPARE)
395 N = mem[pc+2] - mem[pc+3];
396 if (mem[pc+1] => 0xffffffff) { //overflow (unsigned signed)?
397 C = 0x00000001;
398 pc = pc + 1;
399 break;
401 if (mem[pc+1] = 0x00000000) { //zero?
402 Z = 0x00000001;
403 pc = pc + 1;
404 break;
406 //<flags> = op_1 - op_2 ; result is not stored, only flags updated
407 pc = pc + 1;
408 break;
409 /* Add/subtract (shifted register). */
410 case 0x0b000000: //ADD (adds...)
411 mem[pc+1] = mem[pc+2] + mem[pc+3];
412 if (mem[pc+1] => 0xffffffff) { //overflow (unsigned signed)?
413 C = 0x00000001;
414 pc = pc + 1;
415 break;
417 if (mem[pc+1] < 0x00000000) { //negative?
418 N = 0x00000001;
419 pc = pc + 1;
420 break;
422 if (mem[pc+1] = 0x00000000) { //zero?
423 Z = 0x00000001;
424 pc = pc + 1;
425 break;
427 //dest = op_1 + op_2
428 pc = pc + 1;
429 break;
430 case 0x2b000000: //ADDS (adds... WITH STATUSES)
431 mem[pc+1] = mem[pc+2] + mem[pc+3];
432 if (mem[pc+1] => 0xffffffff) { //overflow (unsigned signed)?
433 C = 0x00000001;
434 pc = pc + 1;
435 break;
437 if (mem[pc+1] < 0x00000000) { //negative?
438 N = 0x00000001;
439 pc = pc + 1;
440 break;
442 if (mem[pc+1] = 0x00000000) { //zero?
443 Z = 0x00000001;
444 pc = pc + 1;
445 break;
447 //dest = op_1 + op_2
448 pc = pc + 1;
449 break;
450 case 0x2b00001f: //CMN (Compare Negative)
451 N = mem[pc+2] - ~mem[pc+3];
452 if (mem[pc+1] => 0xffffffff) { //overflow (unsigned signed)?
453 C = 0x00000001;
454 pc = pc + 1;
455 break;
457 if (mem[pc+1] = 0x00000000) { //zero?
458 Z = 0x00000001;
459 pc = pc + 1;
460 break;
462 //<flags> = op_1 - (NOT op_2) ; result is not stored, only flags updated
463 pc = pc + 1;
464 break;
465 case 0x4b000000: //SUB (substracts)
466 mem[pc+1] = mem[pc+2] - mem[pc+3];
467 if (mem[pc+1] => 0xffffffff) { //overflow (unsigned signed)?
468 C = 0x00000001;
469 pc = pc + 1;
470 break;
472 if (mem[pc+1] < 0x00000000) { //negative?
473 N = 0x00000001;
474 pc = pc + 1;
475 break;
477 if (mem[pc+1] = 0x00000000) { //zero?
478 Z = 0x00000001;
479 pc = pc + 1;
480 break;
482 //dest = op_1 - op_2
483 pc = pc + 1;
484 break;
485 case 0x4b0003e0: //NEG (negates)
486 mem[pc+1] = -mem[pc+2];
487 if (mem[pc+1] => 0xffffffff) { //overflow (unsigned signed)?
488 C = 0x00000001;
489 pc = pc + 1;
490 break;
492 if (mem[pc+1] < 0x00000000) { //negative?
493 N = 0x00000001;
494 pc = pc + 1;
495 break;
497 if (mem[pc+1] = 0x00000000) { //zero?
498 Z = 0x00000001;
499 pc = pc + 1;
500 break;
502 //dest = op_1 - op_2
503 pc = pc + 1;
504 break;
505 case 0x6b00001f: //SUBS (substracts with statuses)
506 mem[pc+1] = mem[pc+2] - mem[pc+3];
507 if (mem[pc+1] => 0xffffffff) { //overflow (unsigned signed)?
508 C = 0x00000001;
509 pc = pc + 1;
510 break;
512 if (mem[pc+1] < 0x00000000) { //negative?
513 N = 0x00000001;
514 pc = pc + 1;
515 break;
517 if (mem[pc+1] = 0x00000000) { //zero?
518 Z = 0x00000001;
519 pc = pc + 1;
520 break;
522 //dest = op_1 - op_2
523 pc = pc + 1;
524 break;
525 case 0x6b20001f: //CMPS (COMPARE WITH STATUSES)
526 N = mem[pc+2] - mem[pc+3];
527 if (mem[pc+1] => 0xffffffff) { //overflow (unsigned signed)?
528 C = 0x00000001;
529 pc = pc + 1;
530 break;
532 if (mem[pc+1] = 0x00000000) { //zero?
533 Z = 0x00000001;
534 pc = pc + 1;
535 break;
537 //<flags> = op_1 - op_2 ; result is not stored, only flags updated
538 pc = pc + 1;
539 break;
540 case 0x6b0003e0: //NEGS (negates with statuses)
541 mem[pc+1] = -mem[pc+2];
542 if (mem[pc+1] => 0xffffffff) { //overflow (unsigned signed)?
543 C = 0x00000001;
544 pc = pc + 1;
545 break;
547 if (mem[pc+1] < 0x00000000) { //negative?
548 N = 0x00000001;
549 pc = pc + 1;
550 break;
552 if (mem[pc+1] = 0x00000000) { //zero?
553 Z = 0x00000001;
554 pc = pc + 1;
555 break;
557 //dest = op_1 - op_2
558 pc = pc + 1;
559 break;
560 /* TODO: add AdvSIMD instructions */