-
Notifications
You must be signed in to change notification settings - Fork 2
/
pmachine.c
398 lines (359 loc) · 8.47 KB
/
pmachine.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
/* COP4302 Systems Software PM/0 Virtual Machine
Coded by Nestor J. Maysonet
University of Central Florida
PID: 2536902
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Constants
#define MAX_STACK_HEIGHT 2000
#define MAX_CODE_LENGTH 500
#define MAX_LEXI_LEVELS 3
FILE* ifp, *ofp; //Global input and output file pointers
typedef struct {
int op; //opcode
int l; // L
int m; // M
int line;//line of code
}instruction;
//Code array
instruction code[MAX_CODE_LENGTH];
//Stack array
int stack[MAX_STACK_HEIGHT];
//Current line of code
int line = 0;
//Lexical level array
int lexi[3];
//Lexi array pointer
int lexilvl = 0;
//CPU globals
int bp = 1; //base of current AR
int sp = 0; //top of stack
int pc = 0; //program counter
instruction* ir; //instruction register
int halt = 0; //halt condition
char opString [5]; //string for opcode
//FUNCTION PROTOTYPES//
void fetch(void);
void execute(void);
int codeStore(void);
void oprSwitch(int m);
void printCode(int count);
void stringSwitch(int op);
void printTrace(void);
void printTraceToConsole(void);
int base(int level, int b);
//Main
int pmachinemain(int argc, char** argv)
{
ifp = fopen("mcode.txt","r");
ofp = fopen("stacktrace.txt","w");
stack[0] = 0;
stack[1] = 0;
stack[2] = 0;
lexi[0] = 0;
lexi[1] = 0;
lexi[2] = 0;
lexi[3] = 0;
int count = codeStore();
printCode(count);
fprintf(ofp," pc bp sp stack\n");
fprintf(ofp,"Initial values %2d %2d %2d\n",pc,bp,sp);
fprintf(ofp," ");
int consoleFlag = 0;
//For command directive "-v"
if(argc == 2)
{
if(!strcmp(argv[1],"print"))
{
consoleFlag = 1;
}
}
while( halt != 1)
{
fetch();
if(pc == count)
halt = 1;
execute();
printTrace();
if(consoleFlag == 1)
{
printTraceToConsole();
printf(" \n ");
}
fprintf(ofp," \n ");
}
fclose(ifp);
fclose(ofp);
}
//Fetch Cycle
//An instruction is fetched from the code store
//Then, the program counter is incremented by one
void fetch(void)
{
ir = &code[pc];
pc++;
}
//ir.op indicates the operation to be executed
//If ir.op is 02, then ir.m identifies the arithmetic
//or logical instruction to be executed.
void execute(void)
{
switch(ir->op)
{
case 1: //LIT 0 M
sp = sp + 1;
stack[sp] = ir->m;
strcpy(opString,"lit");
break;
case 2: //OPR 0 M
oprSwitch(ir->m);
strcpy(opString,"opr");
break;
case 3: //LOD L M
sp++;
stack[sp] = stack[ base(ir->l, bp) + ir->m];
strcpy(opString,"lod");
break;
case 4: //STO L M
stack[base(ir->l,bp) + ir->m] = stack[sp];
sp--;
strcpy(opString,"sto");
break;
case 5: //CAL L M
stack[sp + 1] = 0; //return value (FV)
stack[sp + 2] = base(ir->l,bp); //static link (SL)
stack[sp + 3] = bp; //dynamic link (DL)
stack[sp + 4] = pc; //return address (RA)
bp = sp + 1;
pc = ir->m;
//For printing out correct stack
lexi[lexilvl] = sp;
lexilvl++;
strcpy(opString,"cal");
break;
case 6: //INC 0 M
sp = sp + ir->m;
strcpy(opString,"inc");
break;
case 7: //JMP 0 M
pc = ir->m;
strcpy(opString,"jmp");
break;
case 8: //JPC
if( stack[sp] == 0)
{
pc = ir->m;
}
sp--;
strcpy(opString,"jpc");
break;
case 9: //SIO - pop & print, read & push, halt
if( ir->m == 0)
{
printf("\n%d\n",stack[sp]);
sp--;
}else if(ir-> m == 1)
{
sp++;
printf("\nEnter value to push to stack: ");
scanf("%d",&stack[sp]);
printf("\n");
}else if(ir->m == 2)
{
halt = 1;
}
strcpy(opString,"sio");
break;
}
}
//Reads ints from input file and stores them into
//the code store which is an array of instruction structs.
int codeStore(void)
{
int halt = 0;
int count = 0;
while(halt != 1)
{
fscanf(ifp,"%d",&code[count].op);
fscanf(ifp,"%d",&code[count].l);
fscanf(ifp,"%d",&code[count].m);
code[count].line = count;
if(code[count].op == 9 && code[count].m == 2)
{
halt = 1;
}
count++;
}
return count;
}
//Switch statement for the OPR instruction
void oprSwitch(int m)
{
switch(m)
{
case 0: //RET
lexilvl--;
sp = bp - 1;
pc = stack[sp + 4];
bp = stack[sp + 3];
break;
case 1: //NEG
stack[sp] = -stack[sp];
break;
case 2: //ADD
sp--;
stack[sp] = stack[sp] + stack[sp + 1];
break;
case 3: //SUB
sp--;
stack[sp] = stack[sp] - stack[sp + 1];
break;
case 4: //MUL
sp--;
stack[sp] = stack[sp]*stack[sp + 1];
break;
case 5: //DIV
sp--;
stack[sp] = stack[sp] / stack[sp + 1];
break;
case 6: //ODD
stack[sp] = stack[sp]%2;
break;
case 7: //MOD
sp--;
stack[sp] = stack[sp] % stack[sp + 1];
break;
case 8: //EQL
sp--;
stack[sp] = stack[sp] == stack[sp + 1];
break;
case 9: //NEQ
sp--;
stack[sp] = stack[sp] != stack[sp + 1];
break;
case 10: //LSS
sp--;
stack[sp] = stack[sp] < stack[sp + 1];
break;
case 11: //LEQ
sp--;
stack[sp] = stack[sp] <= stack[sp + 1];
break;
case 12: //GTR
sp--;
stack[sp] = stack[sp] > stack[sp + 1];
break;
case 13: //GEQ
sp--;
stack[sp] = stack[sp] >= stack[sp + 1];
}
}
//Iterates through code array printing out the code
//that was scanned from the input file
void printCode(int count)
{
instruction temp;
int i;
fprintf(ofp,"Line OP L M\n");
for(i = 0; i < count; i++)
{
temp = code[i];
stringSwitch(temp.op);
fprintf(ofp," %2d %2s %2d %2d\n",i,opString,temp.l,temp.m);
}
}
//Function to handle assigning appropriate string
//for printCode and printTrace. Takes the opcode
//as its only parameter.
void stringSwitch(int op)
{
switch(op)
{
case 1:
strcpy(opString,"lit");
break;
case 2:
strcpy(opString,"opr");
break;
case 3:
strcpy(opString,"lod");
break;
case 4:
strcpy(opString,"sto");
break;
case 5:
strcpy(opString,"cal");
break;
case 6:
strcpy(opString,"inc");
break;
case 7:
strcpy(opString,"jmp");
break;
case 8:
strcpy(opString,"jpc");
break;
case 9:
strcpy(opString,"sio");
break;
}
}
//Function to act as place holder for the print
//statements used to print the stack trace to the
//output file. Uses stringSwitch to get correct op
//string to display. Will print vertical bar to
//separate each Activation Record.
void printTrace(void)
{
int temp;
int i;
int current = 0;
stringSwitch(ir->op);
if(pc > 1)
line = pc -1;
fprintf(ofp,"%2d %3s %2d %2d %2d %2d %2d ",
ir->line,opString,ir->l,ir->m,pc,bp,sp);
temp = sp;
for(i = 1; i <= sp; i++)
{
if(lexi[current] > 0 && i > lexi[current])
{
fprintf(ofp,"|");
current++;
}
fprintf(ofp,"%d ",stack[i]);
}
}
void printTraceToConsole(void)
{
int temp;
int i;
int current = 0;
stringSwitch(ir->op);
if(pc > 1)
line = pc -1;
printf("%2d %3s %2d %2d %2d %2d %2d ",
ir->line,opString,ir->l,ir->m,pc,bp,sp);
temp = sp;
for(i = 1; i <= sp; i++)
{
if(lexi[current] > 0 && i > lexi[current])
{
printf("|");
current++;
}
printf("%d ",stack[i]);
}
}
//Compute base of activation record L levels down
int base(int level,int b )
{
while(level > 0)
{
b = stack[b+2];
level--;
}
return b;
}