-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu.c
64 lines (50 loc) · 1.1 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "shell.h"
#include "ram.h"
#include "constants.h"
struct CPU {
int IP;
char IR[1000];
int quanta;
} cpu;
int _cpuAvail = 1;
int run(int quanta);
int isCPUAvailable();
int getCPU_IP();
void setCPU_IP(int ip);
void initCPU();
int run(int quanta){
if(quanta > CPU_QUANTA) quanta = CPU_QUANTA;
while(quanta > 0){
_cpuAvail = -1;
int isNextInstructionValid = -1;
char* nextInstruction = getLineFromRAM(&isNextInstructionValid, cpu.IP);
if(isNextInstructionValid == -1){
initCPU();
_cpuAvail = 1;
return -1;
}
strcpy(cpu.IR, nextInstruction);
cpu.IR[strlen(cpu.IR) - 1] = '\0';
strtok(cpu.IR, "\r");
parse(cpu.IR);
cpu.IP++;
quanta--;
}
_cpuAvail = 1;
return 0;
}
int isCPUAvailable(){
return _cpuAvail;
}
int getCPU_IP(){
return cpu.IP;
}
void setCPU_IP(int ip) {
cpu.IP = ip;
}
void initCPU(){
cpu.quanta = CPU_QUANTA;
}