-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMipsim.java
1209 lines (1070 loc) · 34 KB
/
Mipsim.java
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
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package pipeline.MIPSsim;
/**
*
*/
import java.io.*;
import java.util.*;
/**
* @author sathvikl
*
* Compiling Instructons :
* javac Mipsim.java
* java Mipsim <inputfile.txt> <outfile.txt>
*/
public class Mipsim {
public Vector<InsSet> InsList;
public Vector<DataSet> DArray;
public HashMap<Integer, Object> InsHash;
boolean inst_region;
public RegisterBank currentRegState;
public RegisterBank prevRegState;
static int cycleCountPreALUB = 0;
int PC;
int cycle;
PiplineState prevState, currentState;
boolean stalled;
boolean sim_exit;
InExecution executionBuffer;
InsSet waitingIns;
InsSet executedIns;
public Mipsim() {
InsHash = new HashMap<Integer, Object>();
InsList = new Vector<InsSet>();
DArray = new Vector<DataSet>();
inst_region = true;
currentRegState = new RegisterBank();
prevRegState = new RegisterBank();
stalled = false;
sim_exit = false;
prevState = new PiplineState();
currentState = new PiplineState();
PC = 64;
executionBuffer = new InExecution();
}
class RegisterBank {
public Integer registers[];
public int sample;
public RegisterBank() {
// TODO Auto-generated constructor stub
registers = new Integer[32];
for(int i = 0; i < 32; i++) {
registers[i] = new Integer(0);
}
}
}
public enum OPCode {
J, JR, BEQ, BLTZ, BGTZ,
BREAK, NOP,
SW, LW, SLL, SRL, SRA,
ADD, SUB, MUL, AND, NOR, SLT,
ADDI, SUBI, MULI, ANDI, NORI, SLTI, INVALID,
MEM, ALU, ALUB, BRANCH
}
public class InsSet {
String instruction;
Integer address;
String binary;
OPCode opcode;
Integer s, t, d, h, offset;
byte instCategory;
OPCode instType;
Integer SRC1, SRC2, DST;
InsSet(String a , int b, String binary, OPCode op) {
instruction = new String(a);
address = b;
binary = new String(binary);
opcode = op;
s = t = d = offset= h = SRC1 = SRC2 = DST = null;
instCategory = 1;
}
}
public class DataSet {
Integer number;
int address;
DataSet(int add, String binary) {
address = add;
number = (int)Long.parseLong(binary, 2);
}
}
public class PreIssue {
LinkedList<InsSet> buffer;
final Integer size = 4;
PreIssue() {
buffer = new LinkedList<InsSet>();
}
PreIssue(PreIssue obj) {
buffer = new LinkedList<InsSet>(obj.buffer);
}
int count() {
return buffer.size();
}
}
public class PreMEM {
LinkedList<InsSet> buffer;
final Integer size = 2;
PreMEM() {
buffer = new LinkedList<InsSet>();
}
PreMEM(PreMEM o) {
buffer = new LinkedList<InsSet>(o.buffer);
}
int count() {
return buffer.size();
}
}
public class PreALU {
LinkedList<InsSet> buffer;
final Integer size = 2;
PreALU() {
buffer = new LinkedList<InsSet>();
}
PreALU(PreALU o) {
buffer = new LinkedList<InsSet>(o.buffer);
}
int count() {
return buffer.size();
}
}
public class PreAluB {
LinkedList<InsSet> buffer;
final Integer size = 2;
PreAluB() {
buffer = new LinkedList<InsSet>();
}
PreAluB(PreAluB o) {
buffer = new LinkedList<InsSet>(o.buffer);
}
int count() {
return buffer.size();
}
}
public class PostMEM {
LinkedList<InsSet> buffer;
final Integer size = 1;
PostMEM() {
buffer = new LinkedList<InsSet>();
}
PostMEM(PostMEM o) {
buffer = new LinkedList<InsSet>(o.buffer);
}
int count() {
return buffer.size();
}
}
public class PostALU {
LinkedList<InsSet> buffer;
private final Integer size = 1;
PostALU() {
buffer = new LinkedList<InsSet>();
}
PostALU(PostALU o) {
buffer = new LinkedList<InsSet>(o.buffer);
}
int count() {
return buffer.size();
}
}
public class PostAluB {
LinkedList<InsSet> buffer;
private final Integer size = 1;
PostAluB() {
buffer = new LinkedList<InsSet>();
}
PostAluB(PostAluB o) {
buffer = new LinkedList<InsSet>(o.buffer);
}
int count() {
return buffer.size();
}
}
public class InExecution {
LinkedList<InsSet> buffer;
@SuppressWarnings("unused")
private final Integer size = 1;
public InExecution() {
// TODO Auto-generated constructor stub
buffer = new LinkedList<InsSet>();
}
int count() {
return buffer.size();
}
}
public class PiplineState {
PreIssue preIssue;
PreALU preALU;
PostALU postALU;
PreAluB preALUB;
PostAluB postALUB;
PreMEM preMEM;
PostMEM postMEM;
public PiplineState() {
// TODO Auto-generated constructor stub
preIssue = new PreIssue();
preALU = new PreALU();
preALUB = new PreAluB();
preMEM = new PreMEM();
postALU = new PostALU();
postALUB = new PostAluB();
postMEM = new PostMEM();
}
public PiplineState(PiplineState o) {
// TODO Auto-generated constructor stub
preIssue = new PreIssue(o.preIssue);
preALU = new PreALU(o.preALU);
preALUB = new PreAluB(o.preALUB);
preMEM = new PreMEM(o.preMEM);
postALU = new PostALU(o.postALU);
postALUB = new PostAluB(o.postALUB);
postMEM = new PostMEM(o.postMEM);
}
}
public String FindInstruction(String BinaryInst, int address) {
StringBuilder actual_inst = new StringBuilder();
OPCode opcode = OPCode.INVALID;
if(inst_region == false) {
DataSet data = new DataSet(address, BinaryInst);
InsHash.put(new Integer(address), data);
DArray.add(data);
return new String(data.number + "");
}
InsSet in = new InsSet("", address, BinaryInst, opcode);
if(BinaryInst.regionMatches(0, "00000000000000000000000000000000", 0, 32)) { // NOOP
actual_inst = new StringBuilder();
actual_inst.append("NOP");
opcode = OPCode.NOP;
in.instType = OPCode.NOP;
}
else if(BinaryInst.regionMatches(0, "000000", 0, 6)) {
if(BinaryInst.regionMatches(26, "100000", 0, 6)) { // ADD
in.s = Integer.parseInt(BinaryInst.substring(6, 11), 2);
in.t = Integer.parseInt(BinaryInst.substring(11, 16), 2);
in.d= Integer.parseInt(BinaryInst.substring(16, 21), 2);
in.SRC1 = in.s;
in.SRC2 = in.t;
in.DST = in.d;
opcode = OPCode.ADD;
in.instType = OPCode.ALU;
actual_inst = new StringBuilder(String.format("ADD R%d, R%d, R%d", in.d, in.s, in.t));
}
else if(BinaryInst.regionMatches(26, "100010", 0, 6)) { // SUB
in.s = Integer.parseInt(BinaryInst.substring(6, 11), 2);
in.t = Integer.parseInt(BinaryInst.substring(11, 16), 2);
in.d= Integer.parseInt(BinaryInst.substring(16, 21), 2);
in.SRC1 = in.s;
in.SRC2 = in.t;
in.DST = in.d;
opcode = OPCode.SUB;
in.instType = OPCode.ALU;
actual_inst = new StringBuilder(String.format("SUB R%d, R%d, R%d", in.d, in.s, in.t));
}
else if(BinaryInst.regionMatches(26, "100100", 0, 6)) { // AND
in.s = Integer.parseInt(BinaryInst.substring(6, 11), 2);
in.t = Integer.parseInt(BinaryInst.substring(11, 16), 2);
in.d= Integer.parseInt(BinaryInst.substring(16, 21), 2);
in.SRC1 = in.s;
in.SRC2 = in.t;
in.DST = in.d;
opcode = OPCode.AND;
in.instType = OPCode.ALU;
actual_inst = new StringBuilder(String.format("AND R%d, R%d, R%d", in.d, in.s, in.t));
}
else if(BinaryInst.regionMatches(26, "100111", 0, 6)) { // NOR
in.s = Integer.parseInt(BinaryInst.substring(6, 11), 2);
in.t = Integer.parseInt(BinaryInst.substring(11, 16), 2);
in.d= Integer.parseInt(BinaryInst.substring(16, 21), 2);
in.SRC1 = in.s;
in.SRC2 = in.t;
in.DST = in.d;
opcode = OPCode.NOR;
in.instType = OPCode.ALU;
actual_inst = new StringBuilder(String.format("NOR R%d, R%d, R%d", in.d, in.s, in.t));
}
else if(BinaryInst.regionMatches(26, "101010", 0, 6)) { // SLT
in.s = Integer.parseInt(BinaryInst.substring(6, 11), 2);
in.t = Integer.parseInt(BinaryInst.substring(11, 16), 2);
in.d= Integer.parseInt(BinaryInst.substring(16, 21), 2);
actual_inst = new StringBuilder(String.format("SLT R%d, R%d, R%d", in.d, in.s, in.t));
in.SRC1 = in.s;
in.SRC2 = in.t;
in.DST = in.d;
opcode = OPCode.SLT;
in.instType = OPCode.ALU;
}
else if(BinaryInst.regionMatches(26, "001000", 0, 6)) { // JR
in.s = Integer.parseInt(BinaryInst.substring(6, 11), 2);
actual_inst = new StringBuilder(String.format("JR R%d", in.s));
opcode = OPCode.JR;
in.SRC1 = in.s;
in.instType = OPCode.BRANCH;
}
else if(BinaryInst.regionMatches(26, "000000", 0, 6)) { // SLL
in.s = Integer.parseInt(BinaryInst.substring(6, 11), 2);
in.t = Integer.parseInt(BinaryInst.substring(11, 16), 2);
in.d= Integer.parseInt(BinaryInst.substring(16, 21), 2);
in.h = Integer.parseInt(BinaryInst.substring(21, 26), 2);
in.SRC1 = in.t;
in.DST = in.d;
opcode = OPCode.SLL;
in.instType = OPCode.ALUB;
actual_inst = new StringBuilder(String.format("SLL R%d, R%d, #%d", in.d, in.t, in.h ));
}
else if(BinaryInst.regionMatches(26, "000010", 0, 6)) { // SRL
in.s = Integer.parseInt(BinaryInst.substring(6, 11), 2);
in.t = Integer.parseInt(BinaryInst.substring(11, 16), 2);
in.d= Integer.parseInt(BinaryInst.substring(16, 21), 2);
in.h = Integer.parseInt(BinaryInst.substring(21, 26), 2);
in.SRC1 = in.t;
in.DST = in.d;
opcode = OPCode.SRL;
in.instType = OPCode.ALUB;
actual_inst = new StringBuilder(String.format("SRL R%d, R%d, #%d", in.d, in.t, in.h ));
}
else if(BinaryInst.regionMatches(26, "000011", 0, 6)) { // SRA
in.s = Integer.parseInt(BinaryInst.substring(6, 11), 2);
in.t = Integer.parseInt(BinaryInst.substring(11, 16), 2);
in.d= Integer.parseInt(BinaryInst.substring(16, 21), 2);
in.h = Integer.parseInt(BinaryInst.substring(21, 26), 2);
in.SRC1 = in.t;
in.DST = in.d;
opcode = OPCode.SRA;
in.instType = OPCode.ALUB;
actual_inst = new StringBuilder(String.format("SRA R%d, R%d, #%d", in.d, in.t, in.h ));
}
else if(BinaryInst.regionMatches(26, "001101", 0, 6)) { // BREAK
inst_region = false;
actual_inst.append("BREAK");
opcode = OPCode.BREAK;
in.instType = OPCode.BREAK;
}
}
else if(BinaryInst.regionMatches(0, "011100", 0, 6)) { // MUL
in.s = Integer.parseInt(BinaryInst.substring(6, 11), 2);
in.t = Integer.parseInt(BinaryInst.substring(11, 16), 2);
in.d= Integer.parseInt(BinaryInst.substring(16, 21), 2);
in.SRC1 = in.s;
in.SRC2 = in.t;
in.DST = in.d;
opcode = OPCode.MUL;
in.instType = OPCode.ALUB;
actual_inst = new StringBuilder(String.format("MUL R%d, R%d, R%d", in.d, in.s, in.t));
}
else if(BinaryInst.regionMatches(0, "101011", 0, 6)) { // SW
in.s = Integer.parseInt(BinaryInst.substring(6, 11), 2);
in.t = Integer.parseInt(BinaryInst.substring(11, 16), 2);
in.offset = Integer.parseInt(BinaryInst.substring(16, 32), 2);
actual_inst = new StringBuilder(String.format("SW R%d, %d(R%d)", in.t, in.offset, in.s));
in.SRC1 = in.s;
in.SRC2 = in.t;
opcode = OPCode.SW;
in.instType = OPCode.MEM;
}
else if(BinaryInst.regionMatches(0, "100011", 0, 6)) { // LW
in.s = Integer.parseInt(BinaryInst.substring(6, 11), 2);
in.t = Integer.parseInt(BinaryInst.substring(11, 16), 2);
in.offset = Integer.parseInt(BinaryInst.substring(16, 32), 2);
actual_inst = new StringBuilder(String.format("LW R%d, %d(R%d)", in.t, in.offset, in.s));
in.SRC1 = in.s;
in.DST = in.t;
opcode = OPCode.LW;
in.instType = OPCode.MEM;
}
else if(BinaryInst.regionMatches(0, "000010", 0, 6)) { // J
in.t = Integer.parseInt(BinaryInst.substring(6, 32), 2);
in.t = ((address + 4) & 0xf0000000) | (in.t << 2);
actual_inst = new StringBuilder(String.format("J #%d", in.t));
opcode = OPCode.J;
in.instType = OPCode.BRANCH;
}
else if(BinaryInst.regionMatches(0, "000100", 0, 6)) { // BEQ
in.s = Integer.parseInt(BinaryInst.substring(6, 11), 2);
in.t = Integer.parseInt(BinaryInst.substring(11, 16), 2);
in.offset = Integer.parseInt(BinaryInst.substring(16, 32), 2);
in.offset = in.offset << 2;
opcode = OPCode.BEQ;
in.instType = OPCode.BRANCH;
in.SRC1 = in.s;
in.SRC2 = in.t;
actual_inst = new StringBuilder(String.format("BEQ R%d, R%d, #%d", in.s, in.t, in.offset));
}
else if(BinaryInst.regionMatches(0, "000001", 0, 6)) { // BLTZ
in.s = Integer.parseInt(BinaryInst.substring(6, 11), 2);
in.t = Integer.parseInt(BinaryInst.substring(11, 16), 2);
//in.offset = (int)Short.parseShort(BinaryInst.substring(16, 32), 2);
in.offset = Integer.parseInt(BinaryInst.substring(16, 32), 2);
//in.offset = in.offset << 2;
in.offset = -12;
opcode = OPCode.BLTZ;
in.instType = OPCode.BRANCH;
in.SRC1 = in.s;
in.SRC2 = in.t;
actual_inst = new StringBuilder(String.format("BLTZ R%d, #%d", in.s, in.offset));
}
else if(BinaryInst.regionMatches(0, "000111", 0, 6)) { // BGTZ
in.s = Integer.parseInt(BinaryInst.substring(6, 11), 2);
in.t = Integer.parseInt(BinaryInst.substring(11, 16), 2);
in.offset = Integer.parseInt(BinaryInst.substring(16, 32), 2);
in.offset = in.offset << 2;
opcode = OPCode.BGTZ;
in.instType = OPCode.BRANCH;
in.SRC1 = in.s;
in.SRC2 = in.t;
actual_inst = new StringBuilder(String.format("BGTZ R%d, #%d", in.s, in.offset));
}
//============================================================
else if(BinaryInst.regionMatches(0, "110000", 0, 6)) { // ADDI
in.s = Integer.parseInt(BinaryInst.substring(6, 11), 2);
in.t = Integer.parseInt(BinaryInst.substring(11, 16), 2);
in.offset = Integer.parseInt(BinaryInst.substring(16, 32), 2);
actual_inst = new StringBuilder(String.format("ADD R%d, R%d, #%d", in.t, in.s, in.offset));
opcode = OPCode.ADDI;
in.instType = OPCode.ALU;
in.SRC1 = in.s;
in.DST = in.t;
in.instCategory = 2;
}
else if(BinaryInst.regionMatches(0, "110001", 0, 6)) { // SUBI
in.s = Integer.parseInt(BinaryInst.substring(6, 11), 2);
in.t = Integer.parseInt(BinaryInst.substring(11, 16), 2);
in.offset = Integer.parseInt(BinaryInst.substring(16, 32), 2);
actual_inst = new StringBuilder(String.format("SUB R%d, R%d, #%d", in.t, in.s, in.offset));
opcode = OPCode.SUBI;
in.instType = OPCode.ALU;
in.SRC1 = in.s;
in.DST = in.t;
in.instCategory = 2;
}
else if(BinaryInst.regionMatches(0, "100001", 0, 6)) { // MULI
in.s = Integer.parseInt(BinaryInst.substring(6, 11), 2);
in.t = Integer.parseInt(BinaryInst.substring(11, 16), 2);
in.offset = Integer.parseInt(BinaryInst.substring(16, 32), 2);
actual_inst = new StringBuilder(String.format("MUL R%d, R%d, #%d", in.t, in.s, in.offset));
opcode = OPCode.MULI;
in.instType = OPCode.ALUB;
in.SRC1 = in.s;
in.DST = in.t;
in.instCategory = 2;
}
else if(BinaryInst.regionMatches(0, "110010", 0, 6)) { // ANDI
in.s = Integer.parseInt(BinaryInst.substring(6, 11), 2);
in.t = Integer.parseInt(BinaryInst.substring(11, 16), 2);
in.offset = Integer.parseInt(BinaryInst.substring(16, 32), 2);
actual_inst = new StringBuilder(String.format("AND R%d, R%d, #%d", in.t, in.s, in.offset));
opcode = OPCode.ANDI;
in.instType = OPCode.ALU;
in.SRC1 = in.s;
in.DST = in.t;
in.instCategory = 2;
}
else if(BinaryInst.regionMatches(0, "110011", 0, 6)) { // NORI
in.s = Integer.parseInt(BinaryInst.substring(6, 11), 2);
in.t = Integer.parseInt(BinaryInst.substring(11, 16), 2);
in.offset = Integer.parseInt(BinaryInst.substring(16, 32), 2);
actual_inst = new StringBuilder(String.format("NOR R%d, R%d, #%d", in.t, in.s, in.offset));
opcode = OPCode.NORI;
in.instType = OPCode.ALU;
in.SRC1 = in.s;
in.DST = in.t;
in.instCategory = 2;
}
else if(BinaryInst.regionMatches(0, "110101", 0, 6)) { // SLTI
in.s = Integer.parseInt(BinaryInst.substring(6, 11), 2);
in.t = Integer.parseInt(BinaryInst.substring(11, 16), 2);
in.offset = Integer.parseInt(BinaryInst.substring(16, 32), 2);
actual_inst = new StringBuilder(String.format("SLT R%d, R%d, #%d", in.t, in.s, in.offset));
opcode = OPCode.SLTI;
in.instType = OPCode.ALU;
in.SRC1 = in.s;
in.DST = in.t;
in.instCategory = 2;
}
in.instruction = actual_inst.toString();
in.opcode = opcode;
InsList.add(in);
InsHash.put(new Integer(address), in);
return actual_inst.toString();
}
private void ProcAssembly(BufferedReader filebuf, BufferedWriter wbuf) throws IOException {
String line;
//char []inst;
int address = 64;
while((line = filebuf.readLine()) != null) {
line = line.replaceAll("\\t", "");
//line.trim();
String ComputedIns = FindInstruction(line, address);
StringBuilder str = new StringBuilder();
if(inst_region || ComputedIns.compareTo("BREAK") == 0) {
str.append(line.substring(0, 6));
str.append(' ');
str.append(line.substring(6, 11));
str.append(' ');
str.append(line.substring(11, 16));
str.append(' ');
str.append(line.substring(16, 21));
str.append(' ');
str.append(line.substring(21, 26));
str.append(' ');
str.append(line.substring(26, 32));
str.append('\t');
str.append(new String(address + ""));
str.append('\t');
str.append(ComputedIns);
str.append('\n');
} else {
str.append(line.substring(0, 32));
str.append('\t');
str.append(new String(address + ""));
str.append('\t');
str.append(ComputedIns);
str.append('\n');
}
//System.out.println(str.toString());
wbuf.write(str.toString());
address += 4;
}
}
private void printCycle(BufferedWriter wbuf, int PC) throws IOException {
int ind;
wbuf.write( String.format("--------------------\n") );
//wbuf.write( String.format("Cycle:%d PC:%d", cycle, PC) );
wbuf.write( String.format("Cycle:%d", cycle) );
wbuf.write("\n");
wbuf.write("\n");
String strWaitingIns = new String("");
if(waitingIns != null) {
strWaitingIns = waitingIns.instruction;
}
String strExecutedIns = new String("");
if(executedIns != null) {
strExecutedIns = executedIns.instruction;
}
String strPreIssueBuf[] = new String[4];
strPreIssueBuf[0] = new String("");
strPreIssueBuf[1] = new String("");
strPreIssueBuf[2] = new String("");
strPreIssueBuf[3] = new String("");
ind = 0;
for(InsSet ins: prevState.preIssue.buffer) {
strPreIssueBuf[ind] = "[" + ins.instruction + "]";
ind++;
}
String strPreALUBuf[] = new String[2];
strPreALUBuf[0] = new String("");
strPreALUBuf[1] = new String("");
ind = 0;
for(InsSet ins: prevState.preALU.buffer) {
strPreALUBuf[ind] = "[" + ins.instruction + "]";
ind++;
}
String strPostALUBuf = new String("");
if(prevState.postALU.count() >= 1) {
strPostALUBuf = "[" + (prevState.postALU.buffer.getFirst()).instruction + "]";
}
String strPreALUBBuf[] = new String[2];
strPreALUBBuf[0] = new String("");
strPreALUBBuf[1] = new String("");
ind = 0;
for(InsSet ins: prevState.preALUB.buffer) {
strPreALUBBuf[ind] = "[" + ins.instruction+ "]";
ind++;
}
String strPostALUBBuf = new String("");
if(prevState.postALUB.count() >= 1) {
strPostALUBBuf = "[" + (prevState.postALUB.buffer.getFirst()).instruction + "]";
}
String strPreMEM[] = new String[2];
strPreMEM[0] = new String("");
strPreMEM[1] = new String("");
ind = 0;
for(InsSet ins: prevState.preMEM.buffer) {
strPreMEM[ind] = "[" + ins.instruction + "]";
ind++;
}
String strPostMEM = new String("");
if(prevState.postMEM.count() >= 1) {
strPostMEM = "[" + (prevState.postMEM.buffer.getFirst()).instruction + "]";
}
wbuf.write("IF Unit:\n");
wbuf.write( String.format("\tWaiting Instruction:%s\n", strWaitingIns));
wbuf.write( String.format("\tExecuted Instruction:%s\n", strExecutedIns));
wbuf.write("Pre-Issue Buffer:\n");
wbuf.write( String.format("\tEntry 0:%s\n", strPreIssueBuf[0] ));
wbuf.write( String.format("\tEntry 1:%s\n", strPreIssueBuf[1] ));
wbuf.write( String.format("\tEntry 2:%s\n", strPreIssueBuf[2] ));
wbuf.write( String.format("\tEntry 3:%s\n", strPreIssueBuf[3] ));
wbuf.write("Pre-ALU Queue:\n");
wbuf.write( String.format("\tEntry 0:%s\n", strPreALUBuf[0] ));
wbuf.write( String.format("\tEntry 1:%s\n", strPreALUBuf[1] ));
wbuf.write( String.format("Post-ALU Buffer:%s\n", strPostALUBuf ));
wbuf.write("Pre-ALUB Queue:\n");
wbuf.write( String.format("\tEntry 0:%s\n", strPreALUBBuf[0] ));
wbuf.write( String.format("\tEntry 1:%s\n", strPreALUBBuf[1] ));
wbuf.write( String.format("Post-ALUB Buffer:%s\n", strPostALUBBuf ));
wbuf.write("Pre-MEM Queue:\n");
wbuf.write( String.format("\tEntry 0:%s\n", strPreMEM[0] ));
wbuf.write( String.format("\tEntry 1:%s\n", strPreMEM[1] ));
wbuf.write( String.format("Post-MEM Buffer:%s\n", strPostMEM ));
wbuf.write("\n");
wbuf.write("Registers");
for(int i = 0; i < 32; i++) {
if(i%8 == 0) {
wbuf.write("\n");
wbuf.write(String.format("R%02d:", i));
}
wbuf.write("\t");
wbuf.write(prevRegState.registers[i] + "");
}
wbuf.write("\n\nData");
for(int i = 0; i < DArray.size(); i++) {
DataSet data = (DArray.get(i));
DataSet D = (DataSet)InsHash.get(data.address);
if(i%8 == 0) {
wbuf.write("\n");
wbuf.write(D.address +":" + "\t");
}
wbuf.write(D.number + "\t");
}
wbuf.write("\n\n");
}
private void executeInstruction(InsSet inst) {
DataSet data;
switch(inst.opcode) {
case ADD :
currentRegState.registers[inst.d] = prevRegState.registers[inst.s] + prevRegState.registers[inst.t];
//PC += 4;
break;
case SUB:
currentRegState.registers[inst.d] = prevRegState.registers[inst.s] - prevRegState.registers[inst.t];
//PC += 4;
break;
case MUL :
currentRegState.registers[inst.d] = prevRegState.registers[inst.s] * prevRegState.registers[inst.t];
//PC += 4;
break;
case AND:
currentRegState.registers[inst.d] = prevRegState.registers[inst.s] & prevRegState.registers[inst.t];
//PC += 4;
break;
case NOR :
currentRegState.registers[inst.d] = ~(prevRegState.registers[inst.s] | prevRegState.registers[inst.t]);
//PC += 4;
break;
case SLT:
if ( prevRegState.registers[inst.s] < prevRegState.registers[inst.t]) {
currentRegState.registers[inst.d] = 1;
} else {
currentRegState.registers[inst.d] = 0;
}
//PC += 4;
break;
//=========================Branch inst ====================
case J:
PC = inst.t;
break;
case JR:
PC = prevRegState.registers[inst.s];
break;
case BEQ:
if( prevRegState.registers[inst.s] == prevRegState.registers[inst.t]) {
PC = PC + inst.offset;
}
PC += 4;
break;
case BLTZ:
if( prevRegState.registers[inst.s] < 0) {
PC = PC + inst.offset;
}
PC += 4;
break;
case BGTZ:
if( prevRegState.registers[inst.s] > 0) {
PC = PC + inst.offset;
}
PC += 4;
break;
//========================= shift inst =================
case SLL:
currentRegState.registers[inst.d] = prevRegState.registers[inst.t] << inst.h;
// PC += 4;
break;
case SRL:
currentRegState.registers[inst.d] = prevRegState.registers[inst.t] >>> inst.h;
//PC += 4;
break;
case SRA:
currentRegState.registers[inst.d] = prevRegState.registers[inst.t] >> inst.h;
//PC += 4;
break;
// store and load=========================================
case SW:
data = (DataSet)InsHash.get(prevRegState.registers[inst.s] + inst.offset);
data.number = prevRegState.registers[inst.t];
//PC += 4;
break;
case LW:
data = (DataSet)InsHash.get(prevRegState.registers[inst.s] + inst.offset);
currentRegState.registers[inst.t] = data.number;
//PC += 4;
break;
// ====================== Junk inst ==================
case NOP: case BREAK:
PC += 4;
break;
//================ CATEGORY 2 Immediate =========================
case ADDI :
currentRegState.registers[inst.t] = prevRegState.registers[inst.s] + inst.offset;
//PC += 4;
break;
case SUBI :
currentRegState.registers[inst.t] = prevRegState.registers[inst.s] - inst.offset;
//PC += 4;
break;
case MULI :
currentRegState.registers[inst.t] = prevRegState.registers[inst.s] * inst.offset;
//PC += 4;
break;
case ANDI :
currentRegState.registers[inst.t] = prevRegState.registers[inst.s] & inst.offset;
//PC += 4;
break;
case NORI :
currentRegState.registers[inst.t] = ~(prevRegState.registers[inst.s] | inst.offset);
//PC += 4;
break;
case SLTI :
if ( prevRegState.registers[inst.s] < inst.offset) {
currentRegState.registers[inst.t] = 1;
} else {
currentRegState.registers[inst.t] = 0;
}
//PC += 4;
break;
default:
//PC += 4;
break;
}
}
private boolean chk_RAW(Integer reg1, Integer reg2, int stopIndex) {
boolean hazard = false;
//check with earlier not issued instructions
for(int i = 0; i < stopIndex; i++) {
InsSet inst = prevState.preIssue.buffer.get(i);
if( (reg1 != null && inst.DST == reg1) || (reg2 != null && inst.DST == reg2)) {
hazard = true;
break;
}
}
if(!hazard) {
// check with instructions in the pipeline
for(InsSet inst: executionBuffer.buffer) {
if( (reg1 != null && inst.DST == reg1) || (reg2 != null && inst.DST == reg2) ) {
hazard = true;
break;
}
}
}
return hazard;
}
private boolean chk_WAW(Integer reg, int stopIndex) {
boolean hazard = false;
//check with earlier not issued instructions
for(int i = 0; i < stopIndex; i++) {
InsSet inst = prevState.preIssue.buffer.get(i);
if( reg != null && inst.DST == reg) {
hazard = true;
break;
}
}
if(!hazard) {
// check with instructions in the pipeline
for(InsSet inst: executionBuffer.buffer) {
if(reg != null && inst.DST == reg) {
hazard = true;
break;
}
}
}
return hazard;
}
private boolean chk_WAR(Integer reg, int stopIndex) {
boolean hazard = false;
//check with earlier not issued instructions
for(int i = 0; i < stopIndex; i++) {
InsSet inst = prevState.preIssue.buffer.get(i);
if( (inst.SRC1 != null && inst.SRC1 == reg) || (inst.SRC2 != null && inst.SRC2 == reg) ) {
hazard = true;
break;
}
}
return hazard;
}
boolean chk_SWinst(Integer stopIndex) {
boolean sw_exists = false;
for(int i = 0; i < stopIndex; i++) {
InsSet inst = prevState.preIssue.buffer.get(i);
if(inst.opcode == OPCode.SW) {
sw_exists = true;
break;
}
}
return sw_exists;
}
private boolean procFetch(boolean break_chk_enable) {
boolean branch_inst = false;
InsSet INS = (InsSet)InsHash.get(PC);
if(INS.instruction.equals("BREAK")) {
sim_exit = true;
executedIns = INS;
return true;
}
if (break_chk_enable) {
// If the next instruction following even a branch inst is a break
// then we stop the simulation.. do not fetch the branch..
InsSet nextINS = (InsSet)InsHash.get(PC + 4);
if(nextINS.instruction.equals("BREAK")) {
sim_exit = true;
//return false;
}
}
if(INS.instType == OPCode.BRANCH && !sim_exit) {
boolean branchHazard = false;
branch_inst = true;
if(INS.opcode != OPCode.J) {
branchHazard = chk_RAW(INS.SRC1, INS.SRC2, prevState.preIssue.count());
}
if(branchHazard == true) {
stalled = true;
waitingIns = INS;
executedIns = null;
} else {
stalled = false;
executeInstruction(INS);
waitingIns = null;
executedIns = INS;
}
}
//if(!(INS.instType == OPCode.BRANCH) && !branch_inst && !(INS.opcode == OPCode.NOP) && !stalled) {
if(!(INS.opcode == OPCode.BREAK) && !branch_inst && !(INS.opcode == OPCode.NOP) && !stalled) {
currentState.preIssue.buffer.addLast(INS);
PC += 4;
}
//check if the current instruction is branch, which would not have been executed and
//the next instruction is BREAK
else if( (INS.instType == OPCode.BRANCH && sim_exit) || (INS.opcode == OPCode.NOP)) {
executedIns = INS;
PC += 4;
}
//check if the current instruction is branch, which would not have been executed and
//the next instruction is BREAK
// if( (INS.instType == OPCode.BRANCH && sim_exit) || (INS.opcode == OPCode.NOP)) {
// executedIns = INS;
// PC += 4;
// }
// else if(!(INS.opcode == OPCode.BREAK) && !branch_inst && !(INS.opcode == OPCode.NOP) && !stalled) {
// currentState.preIssue.buffer.addLast(INS);
// PC += 4;
// }
return branch_inst;
}
private void iFetch() {
int empty_slots;
boolean branch_inst;
//if(sim_exit) return;
waitingIns = null;
executedIns = null;
branch_inst = false;
empty_slots = prevState.preIssue.size - prevState.preIssue.count();
//Fetch the 1st instruction
if(empty_slots >= 1) {
//branch_inst = procFetch(empty_slots >= 2);
branch_inst = procFetch(true);
}
//Fetch the 2nd instruction
if(empty_slots >= 2 && !branch_inst) {
branch_inst = procFetch(false);
}
}
private boolean checkHazardsIssue(InsSet inst) {
// TODO Auto-generated method stub
int stopIndex = prevState.preIssue.buffer.indexOf(inst);
boolean rRAW = false;
boolean rWAW = false;
boolean rWAR = false;
boolean rMEM = false;
boolean issued = false;