-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu.c
777 lines (672 loc) · 17.3 KB
/
cpu.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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
#include "cpu.h"
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
int32_t *cpuCreateMemory(FILE *program, size_t stackCapacity, int32_t **stackBottom)
{
assert(program != NULL);
assert(stackBottom != NULL);
int32_t *memoryPointer = calloc(1024, sizeof(int32_t));
int32_t *memoryPointerCopy; // Copy used in "free" functions
if (memoryPointer == NULL) {
return NULL; // Not enough memory on heap
}
size_t allocatedBlocks = 1;
uint32_t actualIndex = 0;
uint32_t maxIndex = 1023;
int32_t input = 0;
int32_t instruction[4] = { 0 };
int8_t counter = 3;
while ((input = fgetc(program)) != EOF) {
instruction[counter] = input;
counter--;
if (counter < 0) {
if (maxIndex < actualIndex) { // True = Not enough memory for instructions
allocatedBlocks++;
memoryPointerCopy = memoryPointer;
memoryPointer = realloc(memoryPointer, allocatedBlocks * 1024 * sizeof(int32_t));
if (memoryPointer == NULL) {
free(memoryPointerCopy);
memoryPointerCopy = NULL;
return NULL; // Not enough memory on heap
}
maxIndex += 1024;
}
for (int8_t i = 0; i < 4; i++) {
*(memoryPointer + actualIndex) = (*(memoryPointer + actualIndex) << 8) | (instruction[i] & 0xff);
}
actualIndex++;
counter = 3;
}
}
if (counter != 3 || (actualIndex == 0 && stackCapacity == 0)) {
free(memoryPointer);
memoryPointer = NULL;
memoryPointerCopy = NULL;
return NULL; // Invalid amount of instructions or stack capacity
}
size_t memoryLeft = maxIndex - actualIndex + 1;
if (memoryLeft < stackCapacity) { // True = Not enough memory for stack
while (memoryLeft < stackCapacity) {
memoryLeft += 1024;
allocatedBlocks++;
}
memoryPointerCopy = memoryPointer;
memoryPointer = realloc(memoryPointer, allocatedBlocks * 1024 * sizeof(int32_t));
if (memoryPointer == NULL) {
free(memoryPointerCopy);
memoryPointerCopy = NULL;
return NULL; // Not enough memory on heap
}
maxIndex = allocatedBlocks * 1024 - 1;
}
*stackBottom = memoryPointer + maxIndex;
memset(memoryPointer + actualIndex, 0, (maxIndex - actualIndex + 1) * sizeof(int32_t));
return memoryPointer;
}
static void resetRegisters(struct cpu *cpu)
{
cpu->A = 0;
cpu->B = 0;
cpu->C = 0;
cpu->D = 0;
cpu->status = 0;
cpu->stackSize = 0;
cpu->instructionPointer = 0;
#ifdef BONUS_JMP
cpu->result = 0;
#endif
}
void cpuCreate(struct cpu *cpu, int32_t *memory, int32_t *stackBottom, size_t stackCapacity)
{
assert(cpu != NULL);
assert(memory != NULL);
assert(stackBottom != NULL);
resetRegisters(cpu);
cpu->memory = memory;
cpu->stackBottom = stackBottom;
cpu->stackLimit = stackBottom - stackCapacity;
}
void cpuDestroy(struct cpu *cpu)
{
assert(cpu != NULL);
free(cpu->memory);
resetRegisters(cpu);
cpu->memory = NULL;
cpu->stackBottom = NULL;
cpu->stackLimit = NULL;
}
void cpuReset(struct cpu *cpu)
{
assert(cpu != NULL);
memset(cpu->stackLimit + 1, 0, (cpu->stackBottom - cpu->stackLimit) * sizeof(int32_t));
resetRegisters(cpu);
}
int cpuStatus(struct cpu *cpu)
{
assert(cpu != NULL);
return cpu->status;
}
int32_t cpuPeek(struct cpu *cpu, char reg)
{
assert(cpu != NULL);
#ifdef BONUS_JMP
if (reg == 'R') {
return cpu->result;
}
#endif
switch (reg) {
case 'A':
return cpu->A;
case 'B':
return cpu->B;
case 'C':
return cpu->C;
case 'D':
return cpu->D;
case 'S':
return cpu->stackSize;
case 'I':
return cpu->instructionPointer;
default:
return 0; // Invalid register
}
}
int cpuRun(struct cpu *cpu, size_t steps)
{
assert(cpu != NULL);
if (cpuStatus(cpu) != cpuOK) {
return 0;
}
uint32_t counter = 0;
while (steps > counter) {
cpuStep(cpu);
counter++;
if (cpu->status == cpuHalted) {
break;
}
if (cpu->status != cpuOK) {
return counter * (-1); // Error code
}
}
return counter; // OK
}
static int32_t next32Bits(struct cpu *cpu, int32_t addition)
{
if (cpu->memory + cpu->instructionPointer + addition > cpu->stackLimit) {
cpu->status = cpuInvalidAddress;
return 0;
}
return *(cpu->memory + cpu->instructionPointer + addition);
}
static int32_t getRegister(struct cpu *cpu, int32_t index)
{
#ifdef BONUS_JMP
if (index == 4) {
return cpu->result;
}
#endif
switch (index) {
case 0:
return cpu->A;
case 1:
return cpu->B;
case 2:
return cpu->C;
case 3:
return cpu->D;
}
return 0;
}
static int operationInstruction(struct cpu *cpu, char operand)
{
int32_t registerIndex = next32Bits(cpu, 1);
if (cpuStatus(cpu) == cpuInvalidAddress) {
return 0;
}
if (registerIndex < 0 || registerIndex > 3) {
cpu->status = cpuIllegalOperand;
return 0;
}
int32_t registerInValue = getRegister(cpu, registerIndex);
if (operand == '/' && registerInValue == 0) {
cpu->status = cpuDivByZero;
return 0;
}
int32_t value = cpuPeek(cpu, 'A');
switch (operand) {
case '+':
value += registerInValue;
break;
case '-':
value -= registerInValue;
break;
case '*':
value *= registerInValue;
break;
case '/':
value /= registerInValue;
break;
}
cpu->A = value;
#ifdef BONUS_JMP
cpu->result = value;
#endif
cpu->instructionPointer += 2;
return 1;
}
static int decIncInstruction(struct cpu *cpu, int value)
{
int32_t registerIndex = next32Bits(cpu, 1);
if (cpuStatus(cpu) == cpuInvalidAddress) {
return 0;
}
if (registerIndex < 0 || registerIndex > 3) {
cpu->status = cpuIllegalOperand;
return 0;
}
switch (registerIndex) {
case 0:
cpu->A += value;
break;
case 1:
cpu->B += value;
break;
case 2:
cpu->C += value;
break;
case 3:
cpu->D += value;
break;
}
#ifdef BONUS_JMP
cpu->result = getRegister(cpu, registerIndex);
#endif
cpu->instructionPointer += 2;
return 1;
}
static int loopInstruction(struct cpu *cpu)
{
if (cpuPeek(cpu, 'C') != 0) {
int32_t jumpIndex = next32Bits(cpu, 1);
if (cpuStatus(cpu) == cpuInvalidAddress) {
return 0;
}
cpu->instructionPointer = jumpIndex;
return 1;
}
cpu->instructionPointer += 2;
return 1;
}
static void setRegister(struct cpu *cpu, int32_t index, int32_t value)
{
switch (index) {
case 0:
cpu->A = value;
break;
case 1:
cpu->B = value;
break;
case 2:
cpu->C = value;
break;
case 3:
cpu->D = value;
break;
}
}
static int movInstruction(struct cpu *cpu)
{
int32_t registerIndex = next32Bits(cpu, 1);
int32_t numValue = next32Bits(cpu, 2);
if (cpuStatus(cpu) == cpuInvalidAddress) {
return 0;
}
if (registerIndex < 0 || registerIndex > 3) {
cpu->status = cpuIllegalOperand;
return 0;
}
setRegister(cpu, registerIndex, numValue);
cpu->instructionPointer += 3;
return 1;
}
static int loadStoreInstruction(struct cpu *cpu, char type)
{
int32_t registerIndex = next32Bits(cpu, 1);
int32_t numValue = next32Bits(cpu, 2);
if (cpuStatus(cpu) == cpuInvalidAddress) {
return 0;
}
#ifdef BONUS_JMP
if (registerIndex == 4 && type == 'l') {
cpu->status = cpuIllegalOperand;
return 0;
}
int32_t specialIndex = registerIndex;
if (specialIndex == 4) {
registerIndex--;
}
#endif
if (registerIndex < 0 || registerIndex > 3) {
cpu->status = cpuIllegalOperand;
return 0;
}
#ifdef BONUS_JMP
if (specialIndex == 4) {
registerIndex++;
}
#endif
int32_t shiftValue = numValue + cpuPeek(cpu, 'D');
if (cpu->stackSize - shiftValue - 1 < 0 || shiftValue < 0) {
cpu->status = cpuInvalidStackOperation;
return 0;
}
shiftValue = cpu->stackSize - shiftValue - 1;
if (type == 'l') { // load
int32_t value = *(cpu->stackBottom - shiftValue);
setRegister(cpu, registerIndex, value);
}
if (type == 's') { // store
int32_t value = getRegister(cpu, registerIndex);
*(cpu->stackBottom - shiftValue) = value;
}
cpu->instructionPointer += 3;
return 1;
}
static int inInstruction(struct cpu *cpu)
{
int32_t registerIndex = next32Bits(cpu, 1);
if (cpuStatus(cpu) == cpuInvalidAddress) {
return 0;
}
if (registerIndex < 0 || registerIndex > 3) {
cpu->status = cpuIllegalOperand;
return 0;
}
int64_t value = 0;
int scanStatus = scanf("%ld", &value);
if (scanStatus == EOF) {
cpu->C = 0;
setRegister(cpu, registerIndex, -1);
} else if (scanStatus == 0 || value > INT32_MAX || value < INT32_MIN) { // Invalid input
cpu->status = cpuIOError;
return 0;
} else {
setRegister(cpu, registerIndex, value);
}
cpu->instructionPointer += 2;
return 1;
}
static int getInstruction(struct cpu *cpu)
{
int32_t registerIndex = next32Bits(cpu, 1);
if (cpuStatus(cpu) == cpuInvalidAddress) {
return 0;
}
if (registerIndex < 0 || registerIndex > 3) {
cpu->status = cpuIllegalOperand;
return 0;
}
unsigned char value = 0;
if (scanf("%c", &value) == EOF) {
cpu->C = 0;
setRegister(cpu, registerIndex, -1);
} else {
setRegister(cpu, registerIndex, value);
}
cpu->instructionPointer += 2;
return 1;
}
static int outPutInstruction(struct cpu *cpu, char type)
{
int32_t registerIndex = next32Bits(cpu, 1);
if (cpuStatus(cpu) == cpuInvalidAddress) {
return 0;
}
#ifdef BONUS_JMP
int32_t specialIndex = registerIndex;
if (specialIndex == 4) {
registerIndex--;
}
#endif
if (registerIndex < 0 || registerIndex > 3) {
cpu->status = cpuIllegalOperand;
return 0;
}
#ifdef BONUS_JMP
if (specialIndex == 4) {
registerIndex++;
}
#endif
int32_t registerValue = getRegister(cpu, registerIndex);
if (type == 'p') {
if (registerValue >= 256 || registerValue < 0) {
cpu->status = cpuIllegalOperand;
return 0;
}
printf("%c", registerValue);
}
if (type == 'o') {
printf("%d", registerValue);
}
cpu->instructionPointer += 2;
return 1;
}
static int swapInstruction(struct cpu *cpu)
{
int32_t registerIndex1 = next32Bits(cpu, 1);
int32_t registerIndex2 = next32Bits(cpu, 2);
if (cpuStatus(cpu) == cpuInvalidAddress) {
return 0;
}
if (registerIndex1 < 0 || registerIndex1 > 3 || registerIndex2 < 0 || registerIndex2 > 3) {
cpu->status = cpuIllegalOperand;
return 0;
}
int32_t swap = getRegister(cpu, registerIndex1);
setRegister(cpu, registerIndex1, getRegister(cpu, registerIndex2));
setRegister(cpu, registerIndex2, swap);
cpu->instructionPointer += 3;
return 1;
}
static int pushInstruction(struct cpu *cpu)
{
int32_t registerIndex = next32Bits(cpu, 1);
if (cpuStatus(cpu) == cpuInvalidAddress) {
return 0;
}
#ifdef BONUS_JMP
int32_t specialIndex = registerIndex;
if (specialIndex == 4) {
registerIndex--;
}
#endif
if (registerIndex < 0 || registerIndex > 3) {
cpu->status = cpuIllegalOperand;
return 0;
}
#ifdef BONUS_JMP
if (specialIndex == 4) {
registerIndex++;
}
#endif
int32_t registerValue = getRegister(cpu, registerIndex);
if (cpu->stackBottom - cpu->stackSize <= cpu->stackLimit) {
cpu->status = cpuInvalidStackOperation;
return 0;
}
*(cpu->stackBottom - cpu->stackSize) = registerValue;
cpu->stackSize++;
cpu->instructionPointer += 2;
return 1;
}
static int popInstruction(struct cpu *cpu)
{
if (cpu->stackSize <= 0) {
cpu->status = cpuInvalidStackOperation;
return 0;
}
int32_t registerIndex = next32Bits(cpu, 1);
if (cpuStatus(cpu) == cpuInvalidAddress) {
return 0;
}
if (registerIndex < 0 || registerIndex > 3) {
cpu->status = cpuIllegalOperand;
return 0;
}
int32_t value = *(cpu->stackBottom - cpu->stackSize + 1);
setRegister(cpu, registerIndex, value);
cpu->stackSize--;
cpu->instructionPointer += 2;
return 1;
}
#ifdef BONUS_JMP
static int jumpInstruction(struct cpu *cpu, char type)
{
int32_t jumpIndex = next32Bits(cpu, 1);
if (cpuStatus(cpu) == cpuInvalidAddress) {
return 0;
}
switch (type) {
case 'i':
cpu->instructionPointer = jumpIndex;
return 1;
case 'e':
if (cpuPeek(cpu, 'R') == 0) {
cpu->instructionPointer = jumpIndex;
return 1;
}
break;
case 'n':
if (cpuPeek(cpu, 'R') != 0) {
cpu->instructionPointer = jumpIndex;
return 1;
}
break;
case 'b':
if (cpuPeek(cpu, 'R') > 0) {
cpu->instructionPointer = jumpIndex;
return 1;
}
break;
}
cpu->instructionPointer += 2;
return 1;
}
static int cmpInstruction(struct cpu *cpu)
{
int32_t registerIndexFirst = next32Bits(cpu, 1);
int32_t registerIndexSecond = next32Bits(cpu, 2);
if (cpuStatus(cpu) == cpuInvalidAddress) {
return 0;
}
if (registerIndexFirst < 0 || registerIndexFirst > 4 || registerIndexSecond < 0 || registerIndexSecond > 4) {
cpu->status = cpuIllegalOperand;
return 0;
}
cpu->result = getRegister(cpu, registerIndexFirst) - getRegister(cpu, registerIndexSecond);
cpu->instructionPointer += 3;
return 1;
}
#endif
#ifdef BONUS_CALL
static int callInstruction(struct cpu *cpu)
{
int32_t jumpIndex = next32Bits(cpu, 1);
if (cpuStatus(cpu) == cpuInvalidAddress) {
return 0;
}
if (cpu->stackBottom - cpu->stackSize <= cpu->stackLimit) {
cpu->status = cpuInvalidStackOperation;
return 0;
}
*(cpu->stackBottom - cpu->stackSize) = cpu->instructionPointer + 2;
cpu->instructionPointer = jumpIndex;
cpu->stackSize++;
return 1;
}
static int retInstruction(struct cpu *cpu)
{
if (cpu->stackSize <= 0) {
cpu->status = cpuInvalidStackOperation;
return 0;
}
cpu->instructionPointer = *(cpu->stackBottom - cpu->stackSize + 1);
cpu->stackSize--;
return 1;
}
#endif
enum instructionKeyWords
{
nope,
halt,
add,
sub,
mul,
divI,
inc,
dec,
loop,
mov,
load,
store,
in,
get,
out,
put,
swap,
push,
pop,
cmp,
jmp,
jz,
jnz,
jgt,
call,
ret
};
int cpuStep(struct cpu *cpu)
{
assert(cpu != NULL);
if (cpuStatus(cpu) != cpuOK) {
return 0;
}
if (cpu->memory + cpu->instructionPointer < cpu->memory ||
cpu->memory + cpu->instructionPointer > cpu->stackLimit) {
cpu->status = cpuInvalidAddress;
return 0;
}
int32_t instruction = next32Bits(cpu, 0);
if (cpuStatus(cpu) == cpuInvalidAddress) {
return 0;
}
#ifdef BONUS_JMP
switch (instruction) {
case cmp:
return cmpInstruction(cpu);
case jmp:
return jumpInstruction(cpu, 'i'); // Ignore R
case jz:
return jumpInstruction(cpu, 'e'); // Equal zero
case jnz:
return jumpInstruction(cpu, 'n'); // Not zero
case jgt:
return jumpInstruction(cpu, 'b'); // Bigger then zero
}
#endif
#ifdef BONUS_CALL
if (instruction == call) {
return callInstruction(cpu);
} else if (instruction == ret) {
return retInstruction(cpu);
}
#endif
switch (instruction) {
case nope:
cpu->instructionPointer++;
return 1;
case halt:
cpu->status = cpuHalted;
cpu->instructionPointer++;
return 0;
case add:
return operationInstruction(cpu, '+');
case sub:
return operationInstruction(cpu, '-');
case mul:
return operationInstruction(cpu, '*');
case divI:
return operationInstruction(cpu, '/');
case inc:
return decIncInstruction(cpu, 1);
case dec:
return decIncInstruction(cpu, -1);
case loop:
return loopInstruction(cpu);
case mov:
return movInstruction(cpu);
case load:
return loadStoreInstruction(cpu, 'l');
case store:
return loadStoreInstruction(cpu, 's');
case in:
return inInstruction(cpu);
case get:
return getInstruction(cpu);
case out:
return outPutInstruction(cpu, 'o');
case put:
return outPutInstruction(cpu, 'p');
case swap:
return swapInstruction(cpu);
case push:
return pushInstruction(cpu);
case pop:
return popInstruction(cpu);
default:
cpu->status = cpuIllegalInstruction;
return 0;
}
}