forked from KJacob/xsm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulator.c
207 lines (188 loc) · 5.45 KB
/
simulator.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
#include "simulator.h"
int parseArgument(char * arg) {
if (strcmp(arg, "--debug") == 0 || strcmp(arg, "-d") == 0) {
enableDebugMode();
return 0;
} else {
char * flag_name = strtok(arg, "=");
char * flag_value = strtok(NULL, "=");
int flag_intValue;
if (strcmp(flag_name, "--timer") == 0 || strcmp(flag_name, "-t") == 0) {
if (flag_value != NULL) flag_intValue = getInteger(flag_value);
if (flag_intValue >= 1 && flag_intValue <= 1024) {
enableTimer();
setTimeSlice(flag_intValue);
return 0;
} else if (flag_intValue != 0) {
printf("Invalid arguement %d to timer flag. Timer value should be between 0 and 1024\n", flag_intValue);
return -1;
} else {
disableTimer();
return 0;
}
} else {
printf("Invalid arguement %s", arg);
return -1;
}
}
}
int main(int argc, char **argv) {
initializeDebug();
int i = 1;
while (i < argc) {
if (parseArgument(argv[i++]) == -1) exit(0);
}
initializeRegisters();
run(isTimerEnabled());
}
/*
* This function does the following:
* 1. Loads OS Startup Code.
* 2. Copies the instruction to be parsed as per the address specified by the IP register.
* 3. Checks whether interrupt is disabled. If not th clock ticks.
* 4. Begins the lexical analysis by getting the first token and passing it as arguement to executeOneInstruction.
* 5. If step flag is enabled enters debug mode
* 6. Finally checks if time slice allocated is over or not. If yes and mode is user mode ,and if interrupt is enabled then
* INT 0 code is run.
*/
void run(int is_timer_enabled) {
if (is_timer_enabled) resetTimer();
loadStartupCode();
int instr;
while (1) {
YY_FLUSH_BUFFER;
if (getInstruction(instruction) == -1) continue; //gets the next instruction in variable instruction
instr = yylex();
if (mode == USER_MODE && is_timer_enabled) timerTick();
executeOneInstruction(instr);
if ((watch_count > 0 && checkWatch() == 1) || step_flag == 1) debugInterface();
if (isTimerCountZero() && is_timer_enabled && mode == USER_MODE) {
resetTimer();
invokeHardwareInterrupt(0);
if (step_flag == 1) printf("TIMER Interrupt\n");
}
}
}
/*
* This code is used to execute each instruction.
*/
void executeOneInstruction(int instr) {
int X, Y, flagX1, flagX2, flagY1, flagY2, operation;
char string[WORD_SIZE];
bzero(string, 16);
switch (instr) {
case START:
storeInteger(reg[IP_REG], getInteger(reg[IP_REG]) + WORDS_PER_INSTR); //increment IP
break;
case MOV: //1st phase:get the value 2nd phase:store the value
X = yylex();
flagX1 = yylval.flag;
flagX2 = yylval.flag2;
Y = yylex();
flagY1 = yylval.flag;
flagY2 = yylval.flag2;
strcpy(string, yylval.data);
if (!performMOV(X, flagX1, flagX2, Y, flagY1, flagY2, string)) return;
storeInteger(reg[IP_REG], getInteger(reg[IP_REG]) + WORDS_PER_INSTR);
break;
case ARITH:
operation = yylval.flag;
X = yylex();
flagX1 = yylval.flag;
if (operation != INR && operation != DCR) {
Y = yylex();
flagY1 = yylval.flag;
}
if (!performArithmetic(X, flagX1, Y, flagY1, operation)) return;
storeInteger(reg[IP_REG], getInteger(reg[IP_REG]) + WORDS_PER_INSTR);
break;
case LOGIC:
operation = yylval.flag;
X = yylex();
flagX1 = yylval.flag;
Y = yylex();
flagY1 = yylval.flag;
if (!performLogic(X, flagX1, Y, flagY1, operation)) return;
storeInteger(reg[IP_REG], getInteger(reg[IP_REG]) + WORDS_PER_INSTR);
break;
case BRANCH:
operation = yylval.flag;
X = yylex();
flagX1 = yylval.flag;
if (operation != JMP) {
Y = yylex();
flagY1 = yylval.flag;
}
if (!performBranching(X, flagX1, Y, flagY1, operation)) return;
break;
case PUSH:
X = yylex();
flagX1 = yylval.flag;
if (!performPush(X, flagX1)) return;
storeInteger(reg[IP_REG], getInteger(reg[IP_REG]) + WORDS_PER_INSTR);
break;
case POP:
X = yylex();
flagX1 = yylval.flag;
if (!performPop(X, flagX1)) return;
storeInteger(reg[IP_REG], getInteger(reg[IP_REG]) + WORDS_PER_INSTR);
break;
case CALL:
X = yylex();
flagX1 = yylval.flag;
if (!performCall(X, flagX1)) return;
break;
case RET:
if (!performRet()) return;
break;
case INT:
X = yylex();
flagX1 = yylval.flag;
if (!performINT(X, flagX1)) return;
break;
case IRET:
if (!performIRET()) return;
break;
case IN:
X = yylex();
flagX1 = yylval.flag;
if (!performIN(X, flagX1)) return;
storeInteger(reg[IP_REG], getInteger(reg[IP_REG]) + WORDS_PER_INSTR);
break;
case OUT:
X = yylex();
flagX1 = yylval.flag;
if (!performOUT(X, flagX1)) return;
storeInteger(reg[IP_REG], getInteger(reg[IP_REG]) + WORDS_PER_INSTR);
break;
case LOAD:
case STORE:
X = yylex();
flagX1 = yylval.flag;
Y = yylex();
flagY1 = yylval.flag;
if (!performLoadStore(X, flagX1, Y, flagY1, instr)) return;
storeInteger(reg[IP_REG], getInteger(reg[IP_REG]) + WORDS_PER_INSTR);
break;
case HALT:
if (mode == USER_MODE) {
raiseException(newException(EX_ILLINSTR, "Call to Privileged Instruction HALT in USER mode", 0));
return;
}
printf("Machine is halting\n");
exit(0);
break;
case END:
break;
case BRKP:
if (isDebugModeOn()) {
step_flag = 1;
printf("\nXSM Debug Environment\nType \"help\" for getting a list of commands\n");
}
storeInteger(reg[IP_REG], getInteger(reg[IP_REG]) + WORDS_PER_INSTR);
break;
default:
raiseException(newException(EX_ILLINSTR, "Illegal Instruction", 0));
return;
}
}