-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab4.c
executable file
·885 lines (836 loc) · 22.6 KB
/
lab4.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
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
/* Name: Myron Zhao and Michael Ebert
* Section: 3
* Description: This program is a MIPS simulator that simulates a 5 stage
* pipelined datapath. It offers 8 commands that can be viewed by
* typing in 'h'. It will run in either an interactive mode or a
* script mode.
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define WHITESPACE_AND_PUNCTUATION "$,(): \t\n"
typedef struct labelList{
struct labelList *next;
char *data;
int lineNum;
} labelList;
typedef struct{
char* instruction;
int branchTaken;
int branchLocation;
int branchCorrect;
}interstageRegister;
int reg[27];
int dataMem[8192];
int arr[5000][4];
int pc = 0;
int sim_pc = 0;
int num_instr = 0;
int cycles = 0;
interstageRegister if_id = {.instruction = "empty", .branchTaken = 0, .branchLocation = 0, .branchCorrect = 0};
interstageRegister id_exe = {.instruction = "empty", .branchTaken = 0, .branchLocation = 0, .branchCorrect = 0};
interstageRegister exe_mem = {.instruction = "empty", .branchTaken = 0, .branchLocation = 0, .branchCorrect = 0};
interstageRegister mem_wb = {.instruction = "empty", .branchTaken = 0, .branchLocation = 0, .branchCorrect = 0};
//Predictor stuff
int GHR = 0;
int GHRSize = 0;
int *selectorTable;
int correctPredictions = 0;
int totalPredictions = 0;
void stripComments(char* line){
char* commentPos;
commentPos = strchr(line, '#');
if(commentPos == NULL){
commentPos = line + strlen(line);
}
*commentPos = '\0';
return;
}
inline int clampInt(int target, int min, int max){
if(target < min){
return min;
}
if(target > max){
return max;
}
return target;
}
void initializeSelectorTable(void){
selectorTable = calloc(sizeof(int), (1<<GHRSize));
}
void destroySelectorTable(void){
free(selectorTable);
}
int branchPredict(void){
totalPredictions++;
return selectorTable[GHR] >= 2;
}
void updateGHR(int pathTaken){
GHR <<= 1;
GHR &= (1 << GHRSize) - 1;
GHR |= (1 & pathTaken);
}
void updatePredict(int pathTaken){
selectorTable[GHR] += ((pathTaken*2) - 1);
selectorTable[GHR] = clampInt(selectorTable[GHR], 0, 3);
updateGHR(pathTaken);
}
void stripCommentsAndLabels(char* line){
char *colonAddr, *strippedLine;
int newLineLength;
int colonPos;
//first strip comments
stripComments(line);
//now strip label
colonAddr = strchr(line, ':');
if(colonAddr == NULL){
colonAddr = line - 1;
}
colonPos = colonAddr - line;
//don't include the colon in the line
newLineLength = strlen(line) - (colonPos+1);
strippedLine = malloc(sizeof(char) * newLineLength+1);
if(newLineLength>0){
strncpy(strippedLine, colonAddr+1, newLineLength);
}
strippedLine[newLineLength] = '\0';
strcpy(line, strippedLine);
free(strippedLine);
return;
}
char* cleanWord(char *word){
char *temp = word;
int len = strlen(word);
while(len--){
if (*temp == '\n' || *temp == '#'){
*temp = '\0';
}
temp++;
}
return word;
}
int strToReg(char* reg){
//strcpy(reg, strtok(NULL, ",() "));
//cleanWord(reg);
int num;
if(!strcmp(reg, "$0") || !strcmp(reg, "$zero")){
num = 0;
}
else if(!strcmp(reg, "$1") || !strcmp(reg, "1")){
num = 27;
}
else if(!strcmp(reg, "$v0") || !strcmp(reg, "v0")){
num = 1;
}
else if(!strcmp(reg, "$v1") || !strcmp(reg, "v1")){
num = 2;
}
else if(!strcmp(reg, "$a0") || !strcmp(reg, "a0")){
num = 3;
}
else if(!strcmp(reg, "$a1") || !strcmp(reg, "a1")){
num = 4;
}
else if(!strcmp(reg, "$a2") || !strcmp(reg, "a2")){
num = 5;
}
else if(!strcmp(reg, "$a3") || !strcmp(reg, "a3")){
num = 6;
}
else if(!strcmp(reg, "$t0") || !strcmp(reg, "t0")){
num = 7;
}
else if(!strcmp(reg, "$t1") || !strcmp(reg, "t1")){
num = 8;
}
else if(!strcmp(reg, "$t2") || !strcmp(reg, "t2")){
num = 9;
}
else if(!strcmp(reg, "$t3") || !strcmp(reg, "t3")){
num = 10;
}
else if(!strcmp(reg, "$t4") || !strcmp(reg, "t4")){
num = 11;
}
else if(!strcmp(reg, "$t5") || !strcmp(reg, "t5")){
num = 12;
}
else if(!strcmp(reg, "$t6") || !strcmp(reg, "t6")){
num = 13;
}
else if(!strcmp(reg, "$t7") || !strcmp(reg, "t7")){
num = 14;
}
else if(!strcmp(reg, "$s0") || !strcmp(reg, "s0")){
num = 15;
}
else if(!strcmp(reg, "$s1") || !strcmp(reg, "s1")){
num = 16;
}
else if(!strcmp(reg, "$s2") || !strcmp(reg, "s2")){
num = 17;
}
else if(!strcmp(reg, "$s3") || !strcmp(reg, "s3")){
num = 18;
}
else if(!strcmp(reg, "$s4") || !strcmp(reg, "s4")){
num = 19;
}
else if(!strcmp(reg, "$s5") || !strcmp(reg, "s5")){
num = 20;
}
else if(!strcmp(reg, "$s6") || !strcmp(reg, "s6")){
num = 21;
}
else if(!strcmp(reg, "$s7") || !strcmp(reg, "s7")){
num = 22;
}
else if(!strcmp(reg, "$t8") || !strcmp(reg, "t8")){
num = 23;
}
else if(!strcmp(reg, "$t9") || !strcmp(reg, "t9")){
num = 24;
}
else if(!strcmp(reg, "$sp") || !strcmp(reg, "sp")){
num = 25;
}
else if(!strcmp(reg, "$ra") || !strcmp(reg, "ra")){
num = 26;
}
else{
//might be direct numbered register
num = atoi(reg);
if(num < 0 || num > 27){
num = -1;
printf("Invalid register ");
}
}
return num;
}
//returns -1 if not a label
int strToLabel(char* arg, labelList* labelHead){
while(labelHead != NULL){
//if this is correct label
if(!strcmp(labelHead->data, arg)){
return labelHead->lineNum;
}
//otherwise
labelHead = labelHead->next;
}
//if no correct labels
return -1;
}
//if arg is a label, returns the instruction address the label points to.
int strToImm(char* arg, labelList* labelHead){
int instrAddr = strToLabel(arg, labelHead);
if(instrAddr != -1){
return instrAddr;
}
else{
return atoi(arg);
}
}
char* numToInstr(int num){
switch(num){
case 0 :
return "add";
break;
case 1 :
return "and";
break;
case 2 :
return "or";
break;
case 3 :
return "sub";
break;
case 4 :
return "slt";
break;
case 5 :
return "sll";
break;
case 6 :
return "addi";
break;
case 7 :
return "lw";
break;
case 8 :
return "sw";
break;
case 9 :
return "j";
break;
case 10 :
return "jal";
break;
case 11 :
return "jr";
break;
case 12 :
return "beq";
break;
case 13 :
return "bne";
break;
default:
return "ERR";
break;
}
}
int instrToNum(char* instr){
if(!strcmp(instr, "add")){
return 0;
}
if(!strcmp(instr, "and")){
return 1;
}
if(!strcmp(instr, "or")){
return 2;
}
if(!strcmp(instr, "sub")){
return 3;
}
if(!strcmp(instr, "slt")){
return 4;
}
if(!strcmp(instr, "sll")){
return 5;
}
if(!strcmp(instr, "addi")){
return 6;
}
if(!strcmp(instr, "lw")){
return 7;
}
if(!strcmp(instr, "sw")){
return 8;
}
if(!strcmp(instr, "j")){
return 9;
}
if(!strcmp(instr, "jal")){
return 10;
}
if(!strcmp(instr, "jr")){
return 11;
}
if(!strcmp(instr, "beq")){
return 12;
}
if(!strcmp(instr, "bne")){
return 13;
}
//invalid instruction!
return 999;
}
void execute(int instr, int r1, int r2, int r3){
switch(instr){
case 0 : // add
reg[r1] = reg[r2] + reg[r3];
break;
case 1 : // and
reg[r1] = reg[r2] & reg[r3];
break;
case 2 : // or
reg[r1] = reg[r2] | reg[r3];
break;
case 3 : // sub
reg[r1] = reg[r2] - reg[r3];
break;
case 4 : // slt
reg[r1] = reg[r2] < reg[r3];
break;
case 5 : // sll
reg[r1] = reg[r2] << r3;
break;
case 6 : // addi
reg[r1] = reg[r2] + r3;
break;
case 7 : // lw
reg[r1] = dataMem[reg[r3] + r2];
break;
case 8 : // sw
dataMem[reg[r3] + r2] = reg[r1];
break;
case 9 : // j
pc = r1;
break;
case 10 : // jal
reg[26] = pc + 1;
pc = r1;
break;
case 11 : // jr
pc = reg[r1];
break;
case 12 : // beq
if(reg[r1] == reg[r2])
pc = pc + r3 + 1;
break;
case 13 : // bne
if(reg[r1] != reg[r2])
pc = pc + r3 + 1;
break;
default:
break;
}
num_instr++;
}
void fakeexecute(int instr, int r1, int r2, int r3){
switch(instr){
case 9 : // j
pc = r1;
break;
case 10 : // jal
//reg[26] = pc + 1;
pc = r1;
break;
case 11 : // jr
pc = reg[r1];
break;
case 12 : // beq
if(reg[r1] == reg[r2])
pc = pc + r3 + 1;
break;
case 13 : // bne
if(reg[r1] != reg[r2])
pc = pc + r3 + 1;
break;
default:
break;
}
}
int detectStall (int pcount, char *idexe) {
int lwReg;
int stall = 0;
if (pcount && !strcmp(idexe, "lw")) {
lwReg = arr[pcount - 1][1];
if (arr[pcount][0] < 5) { // if depends on both r2 and r3
if (arr[pcount][2] == lwReg || arr[pcount][3] == lwReg)
stall = 1;
}
else if (arr[pcount][0] == 5 || arr[pcount][0] == 6) { // depends on only on r2
if (arr[pcount][2] == lwReg)
stall = 1;
}
else if (arr[pcount][0] == 7) { // depends only on r3
if (arr[pcount][3] == lwReg)
stall = 1;
}
else if (arr[pcount][0] == 8) { // depends on r1 and r3
if (arr[pcount][1] == lwReg || arr[pcount][3] == lwReg)
stall = 1;
}
else if (arr[pcount][0] == 11) { // depends only on r1
if (arr[pcount][1] == lwReg)
stall = 1;
}
else if (arr[pcount][0] > 11) { // depends on r1 and r2
if (arr[pcount][1] == lwReg || arr[pcount][2] == lwReg)
stall = 1;
}
}
return stall;
}
//Note: argNum starts from 1, not 0 - arg 0 is the instruction!
int correctArg(int instrNum, int instr, int argNum, char* arg, labelList* labelHead){
switch(instr){
//All registers
case 0 : // add
case 1 : //and
case 2 : // or
case 3 : // sub
case 4 : // slt
case 5 : // sll
case 11 : // jr
return strToReg(arg);
break;
//arg1, arg2 are registers, arg3 is immediate
case 6 : // addi
if(argNum == 1 || argNum == 2){
return strToReg(arg);
}
else{
return strToImm(arg, labelHead);
}
break;
//arg1, arg3 are registers, arg2 is immediate
case 7 : // lw
case 8 : // sw
if(argNum == 1 || argNum == 3){
return strToReg(arg);
}
else{
return strToImm(arg, labelHead);
}
break;
case 9 : // j
case 10 : // jal
return strToImm(arg, labelHead);
break;
//beq and bne require a relative address, not absolute
case 12 : // beq
case 13 : // bne
if(argNum == 1 || argNum == 2){
return strToReg(arg);
}
else{
int labelAddr = strToLabel(arg, labelHead);
//if arg is a label
if(labelAddr != -1){
return labelAddr - ( instrNum + 1 );
}
else{//is not a label, is a relative
return atoi(arg);
}
}
break;
default:
printf("Error: instruction %i not valid.",instr);
return 999;
break;
}
}
void writebackStage(void){
mem_wb.instruction = "empty";
return;
}
void memoryStage(void){
//has the beq squash
if(!strcmp(mem_wb.instruction, "empty")){
mem_wb.instruction = exe_mem.instruction;
mem_wb.branchTaken = exe_mem.branchTaken;
mem_wb.branchLocation = exe_mem.branchLocation;
mem_wb.branchCorrect = exe_mem.branchCorrect;
if(exe_mem.branchCorrect == 0
&& (!strcmp(exe_mem.instruction, "beq")
|| !strcmp(exe_mem.instruction, "bne"))){
exe_mem.instruction = "squash";
id_exe.instruction = "squash";
if_id.instruction = "squash";
pc = exe_mem.branchLocation;
}
else{
exe_mem.instruction = "empty";
}
}
}
void executeStage(void){
//this stage has the LW stall
//if exe_mem register is free
if(!strcmp(exe_mem.instruction, "empty")){
exe_mem.instruction = id_exe.instruction;
exe_mem.branchTaken = id_exe.branchTaken;
exe_mem.branchLocation = id_exe.branchLocation;
exe_mem.branchCorrect = id_exe.branchCorrect;
//need to figure out how detectStall works, feed in right parameters
//I do know that detectStall checks for "lw", so no need for that here
if(detectStall(pc - 1,id_exe.instruction)){
id_exe.instruction = "stall";
}
else{
id_exe.instruction = "empty";
}
}
}
void decodeStage(void){
//if id_exe register is free
if(!strcmp(id_exe.instruction, "empty")){
id_exe.instruction = if_id.instruction;
id_exe.branchTaken = if_id.branchTaken;
id_exe.branchLocation = if_id.branchLocation;
id_exe.branchCorrect = if_id.branchCorrect;
//j or jal or jr (add later) AND jumped
if(1 == 1
&& (!strcmp(if_id.instruction, "j")
|| !strcmp(if_id.instruction, "jr")
|| !strcmp(if_id.instruction, "jal"))){
if_id.instruction = "squash";
pc = if_id.branchLocation;
}
else{
if_id.instruction = "empty";
}
}
}
void fetchStage(void){
if(!strcmp(if_id.instruction, "empty")){
int unbranchedPC = pc;
//fetch new instruction at PC
if_id.instruction = numToInstr(arr[pc][0]);
if((exe_mem.branchCorrect == 0
&& (!strcmp(exe_mem.instruction, "beq")
|| !strcmp(exe_mem.instruction, "bne")))
||(id_exe.branchCorrect == 0
&& ((!strcmp(id_exe.instruction, "beq")
|| !strcmp(id_exe.instruction, "bne"))))){
fakeexecute(arr[pc][0], arr[pc][1], arr[pc][2], arr[pc][3]);
}
else{
execute(arr[pc][0], arr[pc][1], arr[pc][2], arr[pc][3]);
if(!strcmp(if_id.instruction, "beq")
||!strcmp(if_id.instruction, "bne")){
int wasBranchTaken = (unbranchedPC != pc);
//change branchPredict() based on what model you
//are using(ex: predict never branch, always branch,
// some other model...)
int prediction = branchPredict();
int predictionCorrect = (prediction == wasBranchTaken);
if_id.branchTaken = wasBranchTaken;
if_id.branchCorrect = predictionCorrect;
//if branch was not taken, next instruction is at
//(currentPC + 1).
if(!wasBranchTaken){
pc++;
}
if_id.branchLocation = pc;
//update prediction stuff
correctPredictions += predictionCorrect;
updatePredict(wasBranchTaken);
//HACK to get this to work:
//(ideally, we wouldn't use arr[unbrachedpc][3])
//if it predicts it does branch,
//set the PC to the branched instruction(even if its wrong).
//(it'll be fixed in the mem stage)
if(prediction == 1){
pc = unbranchedPC + arr[unbranchedPC][3];
}
else { //(prediction == 0)
pc = unbranchedPC;
}
}
else{
if_id.branchTaken = 1;
if_id.branchLocation = pc;
if_id.branchCorrect = 1;
pc = unbranchedPC;
}
}
pc++;
}
}
void outputCoords(void){
FILE* coordFile = fopen("coordinates.csv", "w");
if(coordFile == NULL){
printf("Writing coordinates.csv failed!");
}
//loop through each memory location, writing stuff each time.
//668 = hardcoded # of values to print.
for(int i = 0; i < 668; i++){
fprintf(coordFile, "%i", dataMem[i]);
if(i%2 == 0){
fputs(',', coordFile);
}
else{
fputs('\n', coordFile);
}
}
fclose(coordFile);
}
int main(int argc, char* argv[]){
FILE * asmFile;
FILE * script;
char *line = malloc(100);
char *label = malloc(100);
char *temp = label;
char *word = temp;
labelList* labelHead = NULL;
int lineNum = -1;
char instr = 0;
int i = 0;
int maxLineNum = 0;
int mStart = 0;
int mEnd = 0;
for(i=0;i<8192;i++){
dataMem[i] = 0;
}
for(i=0;i<27;i++){
reg[i] = 0;
}
asmFile = fopen(argv[1], "r");
if(asmFile == NULL){
perror ("Error opening file\n");
return 1;
}
//parse labels
//each label consists of 2 parts:
// A label name
// the instruction number the label is at
int instructionNum = 0;
while(fgets(line, 100, asmFile)){
char* colonAddr;
stripComments(line);
//is there a label on this line?
if(colonAddr = strchr(line, ':')){
//if yes, add the label to the list.
labelList* newLabel;
int labelSize = 0;
char* labelChar = colonAddr - 1;
//get size and beginning of label string
while(isalnum(*labelChar) && labelChar >= line){
labelSize++;
labelChar--;
}
//initialize new label
newLabel = malloc(sizeof(*newLabel));
newLabel->data = malloc(sizeof(char) * labelSize + 1);
strncpy(newLabel->data, labelChar + 1, labelSize);
newLabel->data[labelSize] = '\0';
newLabel->lineNum = instructionNum;
//NOTE: labels pushed in order, so
// last label encountered = first label in list.
newLabel->next = labelHead;
labelHead = newLabel;
}
//is there an instruction on this line?
//(actually just checks to see that there is SOMETHING that isn't a
//label, comment, whitespace, or punctuation)
stripCommentsAndLabels(line);
if(strspn(line, WHITESPACE_AND_PUNCTUATION) != strlen(line)){
//something there
instructionNum++;
}
}
rewind(asmFile);
maxLineNum = instructionNum;
lineNum = -1;
//Transfer instructions into memory
while(fgets(line, 100, asmFile)){
stripCommentsAndLabels(line);
word = strtok(line, WHITESPACE_AND_PUNCTUATION);
//if there is an instruciton on this line
if(word != NULL){
int i = 0;
lineNum++;
arr[lineNum][i] = instrToNum(word);
i++;
while(word = strtok(NULL, WHITESPACE_AND_PUNCTUATION)){
arr[lineNum][i] = correctArg(lineNum, arr[lineNum][0], i, word, labelHead);
i++;
}
}
//else word == null, do nothing
}
fclose(asmFile);
if(argc > 2){
script = fopen(argv[2], "r");
}
if(argc > 3){
//does this segfault?
GHRSize = atoi(argv[3]);
}
else{
GHRSize = 2;
}
initializeSelectorTable();
while(line){
char* commandArg;
int numLines;
printf("mips> ");
if(argc > 2){
fgets(line, 100, script);
printf("%s", line);
}
else{
fgets(line, 100, stdin);
}
instr = *strtok(line, " ");
switch(instr){
case 'h' :
printf("\nh = show help\n");
printf("d = dump register state\n");
printf("p = show pipeline registers\n");
printf("s = step through a single clock cycle step (i.e. simulate 1 cycle and stop)\n");
printf("s num = step through num clock cycles\n");
printf("r = run until the program ends and display timing summary\n");
printf("m num1 num2 = display data memory from location num1 to num2\n");
printf("c = clear all registers, memory, and the program counter to 0\n");
printf("q = exit the program\n");
break;
case 'd' :
printf("\npc = %d\n", pc);
printf("$0 = %d $v0 = %d $v1 = %d $a0 = %d\n", reg[0], reg[1], reg[2], reg[3]);
printf("$a1 = %d $a2 = %d $a3 = %d $t0 = %d\n", reg[4], reg[5], reg[6], reg[7]);
printf("$t1 = %d $t2 = %d $t3 = %d $t4 = %d\n", reg[8], reg[9], reg[10], reg[11]);
printf("$t5 = %d $t6 = %d $t7 = %d $s0 = %d\n", reg[12], reg[13], reg[14], reg[15]);
printf("$s1 = %d $s2 = %d $s3 = %d $s4 = %d\n", reg[16], reg[17], reg[18], reg[19]);
printf("$s5 = %d $s6 = %d $s7 = %d $t8 = %d\n", reg[20], reg[21], reg[22], reg[23]);
printf("$t9 = %d $sp = %d $ra = %d \n", reg[24], reg[25], reg[26]);
break;
case 's' :
//get number of instructions to execute
commandArg = strtok(NULL, " ");
if(commandArg == NULL){
numLines = 1;
}
else{
numLines = atoi(commandArg);
}
//execute the instructions
while(numLines--){
writebackStage();
memoryStage();
executeStage();
decodeStage();
fetchStage();
cycles++;
}
printf("\npc if/id id/exe exe/mem mem/wb\n");
printf("%d %s %s %s %s\n\n", pc, if_id.instruction, id_exe.instruction, exe_mem.instruction, mem_wb.instruction);
break;
case 'p' :
printf("\npc if/id id/exe exe/mem mem/wb\n");
printf("%d %s %s %s %s\n\n", pc, if_id.instruction, id_exe.instruction, exe_mem.instruction, mem_wb.instruction);
break;
case 'r' :
while (pc != maxLineNum) {
writebackStage();
memoryStage();
executeStage();
decodeStage();
fetchStage();
cycles++;
}
printf("\nProgram complete\n");
printf("CPI = %0.3lf\tCycles = %d\tInstructions = %d\n\n", (double)(cycles + 4)/num_instr, cycles + 4, num_instr);
break;
case 'm' :
mStart = atoi(strtok(NULL, " "));
mEnd = atoi(strtok(NULL, " "));
for(i = mStart; i<=mEnd; i++){
printf("[%d] = %d\n", i, dataMem[i]);
}
break;
case 'c' :
pc = 0;
for(i=0;i<8192;i++){
dataMem[i] = 0;
}
for(i=0;i<27;i++){
reg[i] = 0;
}
printf("\tSimulator reset\n");
break;
case 'b':
//output branch predictor accuracy
printf("accuracy %.2lf% (%i correct predictions, %i predictions)\n",
((double)correctPredictions/totalPredictions) * 100,
correctPredictions,
totalPredictions);
break;
case 'q' :
line = NULL;
break;
//output a comma separated listing of the x,y coords to coordinates.csv
case 'o':
outputCoords();
break;
default:
break;
}
}
//fclose(script);
return 0;
}