-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulator.cpp
More file actions
1433 lines (1168 loc) · 46.3 KB
/
simulator.cpp
File metadata and controls
1433 lines (1168 loc) · 46.3 KB
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
//
// simulator.cpp
// simulator
//
// By Jonathan Go, Mark Hamlin, Matt Schartman
//
#include <iostream>
#include <assert.h>
#include <string>
using std::string;
#include <vector>
using std::vector;
#include <list>
using std::list;
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef enum {
RFormat,
IFormat,
JFormat,
} MIPSFormat;
#define UNUSED_CTRL_BIT 0
typedef struct {
uint32_t op;
uint32_t func;
MIPSFormat format;
struct {
uint32_t rd;
uint32_t rs;
uint32_t rt;
uint32_t imm;
uint32_t targ_address;
} fields;
struct {
uint32_t alu;
uint32_t mw;
uint32_t mtr;
uint32_t mr;
uint32_t asrc;
uint32_t bt;
uint32_t rdst;
uint32_t rw;
} control_bits;
} DecoderOp;
static void usage()
{
fprintf(stderr, "Usage: driver -file filename blocksize(bytes power of 2) cachesize1(bytes) cachesize2(bytes) L1accesstime(cycles) L2accesstime(cycles) L1associativity L2associativity misspenalty(cycles)\n");
}
#define OP_ADD_SLT_SUB 0
#define OP_ADDI 14
#define OP_XORI 20
#define OP_LW 3
#define OP_SW 7
#define OP_BNE 44
#define OP_BGE 34
#define OP_JAL 36
#define FUNC_ADD 10
#define FUNC_SLT 12
#define FUNC_SUB 17
#define SIX_BITS ((1|(1<<1)|(1<<2)|(1<<3)|(1<<4)|(1<<5)))
#define FIVE_BITS ((1|(1<<1)|(1<<2)|(1<<3)|(1<<4)))
#define FOUR_BITS ((1|(1<<1)|(1<<2)|(1<<3)))
#define THREE_BITS ((1|(1<<1)|(1<<2)))
#define TWO_BITS ((1|(1<<1)))
#define ONE_BIT 1
static string bits_to_string(uint32_t field, unsigned int nbits)
{
string result;
for (unsigned int i = 0; i < nbits; ++i) {
if (field&0x1) {
result.insert(result.begin(), 1, '1');
}
else {
result.insert(result.begin(), 1, '0');
}
field >>= 1;
}
return result;
}
static void decode_control_bits(uint32_t op, uint32_t func, DecoderOp *decoded)
{
#define SET_CTRL_BITS(aluv, mwv, mtrv, mrv, asrcv, btv, rdstv, rwv) \
decoded->control_bits.alu = aluv;\
decoded->control_bits.mw = mwv;\
decoded->control_bits.mtr = mtrv;\
decoded->control_bits.mr = mrv;\
decoded->control_bits.asrc = asrcv;\
decoded->control_bits.bt = btv;\
decoded->control_bits.rdst = rdstv;\
decoded->control_bits.rw = rwv;
switch (op) {
// add
// slt
// sub
case 0:
{
switch (func) {
// add
case 10:
SET_CTRL_BITS(2, 0, 0, 0, 0, 0, 1, 1);
break;
// slt
case 12:
SET_CTRL_BITS(6, 0, 0, 0, 0, 0, 1, 1);
break;
// sub
case 17:
SET_CTRL_BITS(3, 0, 0, 0, 0, 0, 1, 1);
break;
default:
assert(false && "Unknown func!");
break;
}
}
break;
// addi
case 14:
SET_CTRL_BITS(2, 0, 0, 0, 1, 0, 0, 1);
break;
// xori
case 20:
SET_CTRL_BITS(5, 0, 0, 0, 1, 0, 0, 1);
break;
// lw
case 3:
SET_CTRL_BITS(2, 0, 1, 1, 1, 0, 0, 1);
break;
// sw
case 7:
SET_CTRL_BITS(2, 1, 1, 1, 1, 0, UNUSED_CTRL_BIT, 0);
break;
// bne
case 44:
SET_CTRL_BITS(5, 0, UNUSED_CTRL_BIT, 0, 0, 3, UNUSED_CTRL_BIT, 0);
break;
// bge
case 34:
SET_CTRL_BITS(3, 0, UNUSED_CTRL_BIT, 0, 0, 2, UNUSED_CTRL_BIT, 0);
break;
// jal
case 36:
SET_CTRL_BITS(UNUSED_CTRL_BIT, 0, UNUSED_CTRL_BIT, 0, UNUSED_CTRL_BIT, 1, UNUSED_CTRL_BIT, 0);
break;
default:
assert(false && "Unknown op!");
break;
}
}
static DecoderOp parse_r_format(uint32_t op, uint32_t b, uint32_t c, uint32_t d, uint32_t e, uint32_t f)
{
DecoderOp decoded;
bzero(&decoded, sizeof(decoded));
decoded.op = op;
decoded.func = f;
decoded.format = RFormat;
decoded.fields.rd = d;
decoded.fields.rs = b;
decoded.fields.rt = c;
//decoded.fields.imm = UNUSED_FIELD;
//decoded.fields.targ_address = UNUSED_FIELD;
decode_control_bits(op, f, &decoded);
return decoded;
}
static DecoderOp parse_i_format(uint32_t op, uint32_t b, uint32_t c, uint32_t d, uint32_t e, uint32_t f)
{
DecoderOp decoded;
bzero(&decoded, sizeof(decoded));
decoded.op = op;
decoded.func = 0;
decoded.format = IFormat;
//decoded.fields.rd = UNUSED_FIELD;
decoded.fields.rs = b;
decoded.fields.rt = c;
decoded.fields.imm = (f|(e<<6)|(d<<11));
//decoded.fields.targ_address = UNUSED_FIELD;
decode_control_bits(op, 0, &decoded);
return decoded;
}
static DecoderOp parse_j_format(uint32_t op, uint32_t b, uint32_t c, uint32_t d, uint32_t e, uint32_t f)
{
DecoderOp decoded;
bzero(&decoded, sizeof(decoded));
decoded.op = op;
decoded.func = 0;
decoded.format = JFormat;
//decoded.fields.rd = UNUSED_FIELD;
//decoded.fields.rs = UNUSED_FIELD;
//decoded.fields.rt = UNUSED_FIELD;
//decoded.fields.imm = UNUSED_FIELD;
decoded.fields.targ_address = (f|(e<<(6))|(d<<(6+5))|(c<<(6+5+5))|(b<<(6+5+5+5)));
decode_control_bits(op, 0, &decoded);
return decoded;
}
static string op_name(uint32_t op, uint32_t func)
{
switch (op) {
// add
// slt
// sub
case 0:
{
switch (func) {
// add
case 10:
return "add";
break;
// slt
case 12:
return "slt";
break;
// sub
case 17:
return "sub";
break;
default:
assert(false && "Unknown func!");
break;
}
}
break;
// addi
case 14:
return "addi";
break;
// xori
case 20:
return "xori";
break;
// lw
case 3:
return "lw";
break;
// sw
case 7:
return "sw";
break;
// bne
case 44:
return "bne";
break;
// bge
case 34:
return "bge";
break;
// jal
case 36:
return "jal";
break;
default:
assert(false && "Unknown op!");
break;
}
return "---";
}
static string string_from_field(uint32_t field)
{
char buf[64];
snprintf(buf, 64, "%u", field);
return string(buf);
}
static void print_decoder_op(DecoderOp op)
{
printf("%s ", op_name(op.op, op.func).c_str());
switch (op.format) {
case RFormat:
printf("$%d, $%d, $%d\n", op.fields.rd, op.fields.rs, op.fields.rt);
break;
case IFormat:
printf("$%d, $%d, %d\n", op.fields.rt, op.fields.rs, *(int16_t *)(&op.fields.imm));
break;
case JFormat:
printf("%d\n", op.fields.targ_address);
break;
}
printf("Fields: {rd: %s, rs: %s, rt: %s, imm: %s, targ: %s}\n",
string_from_field(op.fields.rd).c_str(),
string_from_field(op.fields.rs).c_str(),
string_from_field(op.fields.rt).c_str(),
string_from_field(op.fields.imm).c_str(),
string_from_field(op.fields.targ_address).c_str());
printf("Control Bits: {alu: %s, mw: %s, mtr: %s, mr: %s, asrc: %s, bt: %s, rdst: %s, rw: %s}\n",
string_from_field(op.control_bits.alu).c_str(),
string_from_field(op.control_bits.mw).c_str(),
string_from_field(op.control_bits.mtr).c_str(),
string_from_field(op.control_bits.mr).c_str(),
string_from_field(op.control_bits.asrc).c_str(),
string_from_field(op.control_bits.bt).c_str(),
string_from_field(op.control_bits.rdst).c_str(),
string_from_field(op.control_bits.rw).c_str());
}
static string print_string_decoder_op(DecoderOp op)
{
char buffer[512];
bzero(buffer, sizeof(buffer));
char *ptr = buffer;
ptr += snprintf(ptr, sizeof(buffer)-(ptr-buffer), "%s ", op_name(op.op, op.func).c_str());
switch (op.format) {
case RFormat:
ptr += snprintf(ptr, sizeof(buffer)-(ptr-buffer), "$%d, $%d, $%d", op.fields.rd, op.fields.rs, op.fields.rt);
break;
case IFormat:
ptr += snprintf(ptr, sizeof(buffer)-(ptr-buffer), "$%d, $%d, %d", op.fields.rt, op.fields.rs, *(int16_t *)(&op.fields.imm));
break;
case JFormat:
ptr += snprintf(ptr, sizeof(buffer)-(ptr-buffer), "%d", op.fields.targ_address);
break;
}
return string(buffer);
}
static DecoderOp decode(uint32_t instruction)
{
uint32_t op = (instruction>>(32-6))&SIX_BITS;
uint32_t b = (instruction>>(32-6-5))&FIVE_BITS;
uint32_t c = (instruction>>(32-6-5-5))&FIVE_BITS;
uint32_t d = (instruction>>(32-6-5-5-5))&FIVE_BITS;
uint32_t e = (instruction>>(32-6-5-5-5-5))&FIVE_BITS;
uint32_t f = (instruction>>(32-6-5-5-5-5-6))&SIX_BITS;
switch (op) {
// add
// slt
// sub
case OP_ADD_SLT_SUB:
{
return parse_r_format(op, b, c, d, e, f);
}
break;
// addi
case OP_ADDI:
return parse_i_format(op, b, c, d, e, f);
break;
// xori
case OP_XORI:
return parse_i_format(op, b, c, d, e, f);
break;
// lw
case OP_LW:
return parse_i_format(op, b, c, d, e, f);
break;
// sw
case OP_SW:
return parse_i_format(op, b, c, d, e, f);
break;
// bne
case OP_BNE:
return parse_i_format(op, b, c, d, e, f);
break;
// bge
case OP_BGE:
return parse_i_format(op, b, c, d, e, f);
break;
// jal
case OP_JAL:
return parse_j_format(op, b, c, d, e, f);
break;
default:
assert(false && "Unknown op!");
break;
}
}
class DesignFetchUnit {
private:
int _program_counter; // Byte offset in instructions
vector<uint32_t> _instructions;
public:
// Initialize program
DesignFetchUnit(const char *filename) {
_program_counter = 0;
FILE *file = fopen(filename, "r");
if (!file) {
fprintf(stderr, "File not found\n");
exit(EXIT_FAILURE);
}
string line;
while (!feof(file)) {
char c;
ssize_t nread = fread(&c, 1, 1, file);
// EOF
if (nread <= 0) {
break;
}
// Finished line
if (c == '\n') {
if (line.size() > 0) {
_instructions.push_back(atoi(line.c_str()));
}
line.clear();
}
else {
line.append(&c, 1);
}
}
fclose(file);
// Sentinel instruction; 0 is flag for exit
_instructions.push_back(0);
}
~DesignFetchUnit() {};
// Increment program counter by 4
uint32_t next_instruction() {
uint32_t next = _instructions[_program_counter>>2];
if (next) {
_program_counter += 4;
}
return next;
}
bool is_end_of_program() { return _instructions[_program_counter>>2] == 0; };
#define BRANCH_NONE 0
#define BRANCH_BGE 2
#define BRANCH_BNE 3
#define BRANCH_JMP 1
// Jump/branch support
bool set_pc(int16_t *registers, uint32_t btype, int16_t zero_bit, int16_t offset, uint32_t jump_address) {
switch (btype) {
case BRANCH_NONE:
break;
case BRANCH_BGE:
if ((zero_bit&(1<<15)) == 0) {
_program_counter += (offset<<2);
return true;
}
break;
case BRANCH_BNE:
if (zero_bit != 0) {
_program_counter += (offset<<2);
return true;
}
break;
case BRANCH_JMP:
registers[31] = _program_counter-4;
_program_counter = (jump_address<<2);
return true;
break;
}
return false;
}
};
#define ALU_OP_AND 0
#define ALU_OP_OR 1
#define ALU_OP_ADD 2
#define ALU_OP_SUB 3
#define ALU_OP_NOT 4
#define ALU_OP_XOR 5
#define ALU_OP_SLT 6
static int32_t alu(uint32_t alu_op, int32_t a, int32_t b)
{
switch (alu_op) {
case ALU_OP_AND:
return (a && b);
break;
case ALU_OP_OR:
return (a || b);
break;
case ALU_OP_ADD:
return (a + b);
break;
case ALU_OP_SUB:
return (a - b);
break;
case ALU_OP_NOT:
return !a;
break;
case ALU_OP_XOR:
return (a ^ b);
break;
case ALU_OP_SLT:
return (a < b)?1:0;
break;
}
// Unused ALU op
return 0;
}
// We went a little overboard with formatting register output...
static void pad_minus_digits(int desired_chars, int val) {
printf("%d", val);
if (val < 0) {
--desired_chars;
}
int nchar = 1;
while (val/10 != 0) {
val /= 10;
++nchar;
}
int remaining = desired_chars-nchar;
for (int i = 0; i < remaining; ++i) {
printf(" ");
}
}
typedef enum {
InstructionFetchState,
InstructionDecodeState,
InstructionExecuteState,
InstructionMemoryState,
InstructionWritebackState,
} InstructionState;
typedef enum {
MemoryL1CacheStage,
MemoryL2CacheStage,
MemoryDRAMCacheStage,
} MemoryStage;
typedef struct {
InstructionState state;
// Debug output
string description;
// Fetch state
uint32_t instruction;
// Decode state
DecoderOp decoded;
// Execute state
int32_t alu_result;
// Data forwarding support
int first_read_reg; // -1 if not needed
int second_read_reg; // -1 if not needed
bool forwarded_first_reg;
int16_t forwarded_first_value;
bool forwarded_second_reg;
int16_t forwarded_second_value;
unsigned int latest_forwarded_cycle;
// Memory state
MemoryStage memory_state;
// L1 stage
unsigned int l1_start_cycle;
// L2 stage
unsigned int l2_start_cycle;
// Dram?
unsigned int dram_start_cycle;
// Writeback state
int reg_to_write; // -1 if none
} PipelineEntry;
static list<PipelineEntry>::iterator oldest_with_state(list<PipelineEntry> &pipeline, InstructionState state, list<PipelineEntry>::iterator skip)
{
list<PipelineEntry>::iterator itr;
itr = pipeline.end();
for (int i = ((int)pipeline.size())-1; i >= 0; --i) {
--itr;
if (itr->state == state && itr != skip) {
return itr;
}
}
return pipeline.end();
}
typedef struct {
uint32_t address; // Bytes
uint32_t word; // Contents
unsigned int last_access_cycle;
} CacheWordEntry;
static unsigned int bits_for_binary_num(unsigned int num)
{
unsigned int bits = 0;
while (num != 0) {
num >>= 1;
bits += 1;
}
return bits;
}
typedef struct {
uint32_t full_address;
unsigned int tag;
unsigned int index;
unsigned int block_offset;
unsigned int byte_offset;
} DecodedAddress;
typedef struct {
unsigned int index_bit_size;
unsigned int block_offset_bit_size;
unsigned int byte_offset_bit_size;
} CacheDecoderParameters;
static unsigned int mask_of_size(unsigned int bits)
{
unsigned int mask = 0;
for (unsigned int i = 0; i < bits; ++i) {
mask |= (1<<i);
}
return mask;
}
// Note naming conventions:
// The blocksize is the size of the data,
// but we're calling the whole thing including
// valid bit, tag and data as a block
typedef struct {
unsigned int access_cycle;
bool valid;
unsigned int index;
unsigned int tag;
vector<uint32_t> data;
} CacheBlock;
typedef struct {
vector<CacheBlock> blocks;
} CacheLine;
static void init_cache_line(CacheLine *line, unsigned int words_per_block, unsigned int associativity)
{
line->blocks.resize(associativity);
for (unsigned int i = 0; i < associativity; ++i) {
line->blocks[i].access_cycle = 0;
line->blocks[i].valid = false;
line->blocks[i].index = i;
// Unnecessary, but zero everything initially
line->blocks[i].tag = 0;
line->blocks[i].data.resize(words_per_block);
}
}
static DecodedAddress decode_cache_address(CacheDecoderParameters params, uint32_t address)
{
DecodedAddress decoded;
decoded.full_address = address;
unsigned int byte_offset_mask = mask_of_size(params.byte_offset_bit_size);
unsigned int block_offset_mask = mask_of_size(params.block_offset_bit_size);
unsigned int index_mask = mask_of_size(params.index_bit_size);
unsigned int walked_bits = 0;
// Pull out byte_offset
decoded.byte_offset = (((address&(byte_offset_mask<<walked_bits)))>>walked_bits);
walked_bits += params.byte_offset_bit_size;
assert(decoded.byte_offset == 0); // Only handle word-aligned lw/sw
// Pull out block_offset
decoded.block_offset = (((address&(block_offset_mask<<walked_bits)))>>walked_bits);
walked_bits += params.block_offset_bit_size;
// Pull out index
decoded.index = (((address&(index_mask<<walked_bits)))>>walked_bits);
walked_bits += params.index_bit_size;
// Pull out tag
decoded.tag = (address>>walked_bits);
return decoded;
}
static uint32_t encode_block_cache_address(CacheDecoderParameters params, CacheBlock block)
{
uint32_t address = 0;
address |= (block.index<<(params.byte_offset_bit_size+params.block_offset_bit_size));
address |= (block.tag<<(params.byte_offset_bit_size+params.block_offset_bit_size+params.index_bit_size));
return address;
}
static CacheBlock *read_cache(unsigned int current_cycle, vector<CacheLine> *cache, DecodedAddress address)
{
CacheLine& line = (*cache)[address.index];
// Check all associativity levels for desired data
for (unsigned int i = 0; i < line.blocks.size(); ++i) {
if (line.blocks[i].valid &&
line.blocks[i].tag == address.tag) {
line.blocks[i].access_cycle = current_cycle;
return &(line.blocks[i]);
}
}
return NULL;
}
static CacheBlock write_cache(unsigned int current_cycle, vector<CacheLine> *cache, DecodedAddress address, uint32_t *line_data, unsigned int line_size)
{
CacheLine& line = (*cache)[address.index];
// Determine target block
CacheBlock *target_block = NULL;
for (unsigned int i = 0; i < line.blocks.size(); ++i) {
if (!target_block ||
line.blocks[i].valid == false ||
target_block->access_cycle < line.blocks[i].access_cycle) {
target_block = &line.blocks[i];
if (target_block->valid == false) {
break;
}
}
}
CacheBlock evicted = (*target_block);
// Write to cache
target_block->access_cycle = current_cycle;
target_block->valid = true;
target_block->tag = address.tag;
memcpy(&(target_block->data[0]), line_data, sizeof(uint32_t)*line_size);
return evicted;
}
static void write_dram(uint8_t *dram, uint32_t address, uint32_t *line_data, unsigned int line_size)
{
memcpy(dram+address, line_data, sizeof(uint32_t)*line_size);
}
int main(int argc, const char * argv[])
{
if (argc != 11 || strcmp(argv[1], "-file") != 0) {
usage();
return EXIT_FAILURE;
}
const char *filename = argv[2];
unsigned int blocksize = atoi(argv[3]);
unsigned int cachesize_1 = atoi(argv[4]);
unsigned int cachesize_2 = atoi(argv[5]);
unsigned int accesstime_L1 = atoi(argv[6]);
unsigned int accesstime_L2 = atoi(argv[7]);
unsigned int associativity_L1 = atoi(argv[8]);
unsigned int associativity_L2 = atoi(argv[9]);
unsigned int misspenalty = atoi(argv[10]);
unsigned int num_words_per_block = (blocksize>>2);
unsigned int num_lines_in_l1_cache = (cachesize_1/(blocksize*associativity_L1));
unsigned int num_lines_in_l2_cache = (cachesize_2/(blocksize*associativity_L2));
CacheDecoderParameters l1_params;
CacheDecoderParameters l2_params;
l1_params.index_bit_size = bits_for_binary_num(num_lines_in_l1_cache-1);
l1_params.block_offset_bit_size = bits_for_binary_num(num_words_per_block-1);
l1_params.byte_offset_bit_size = bits_for_binary_num(3); // 2 :) num bits to address a word
l2_params.index_bit_size = bits_for_binary_num(num_lines_in_l2_cache-1);
l2_params.block_offset_bit_size = bits_for_binary_num(num_words_per_block-1);
l2_params.byte_offset_bit_size = bits_for_binary_num(3); // 2 :) num bits to address a word
DesignFetchUnit fetcher(filename);
// General registers
int16_t registers[32];
// Memory
uint8_t memory[1024];
vector<CacheLine> l1_cache;
vector<CacheLine> l2_cache;
// Init both caches
l1_cache.resize(num_lines_in_l1_cache);
for (unsigned int i = 0; i < num_lines_in_l1_cache; ++i) {
init_cache_line(&(l1_cache[i]),
num_words_per_block,
associativity_L1);
}
l2_cache.resize(num_lines_in_l2_cache);
for (unsigned int i = 0; i < num_lines_in_l2_cache; ++i) {
init_cache_line(&(l2_cache[i]),
num_words_per_block,
associativity_L2);
}
// Initially zero all registers
for (int i = 0; i < 32; ++i) {
registers[i] = 0;
}
unsigned int cycle_num = 0;
unsigned int instructions_completed = 0;
unsigned int l1_accesses = 0;
unsigned int l2_accesses = 0;
unsigned int dram_accesses = 0;
unsigned int l1_hits = 0;
unsigned int l2_hits = 0;
list<PipelineEntry> pipeline;
// Run program
while (!fetcher.is_end_of_program() || pipeline.size() > 0) {
list<PipelineEntry>::iterator entry;
string writeback_instruction;
string memory_instruction;
string execute_instruction;
string decode_instruction;
string fetch_instruction;
//
// Writeback State
//
entry = oldest_with_state(pipeline, InstructionWritebackState, pipeline.end());
if (entry != pipeline.end()) {
++instructions_completed;
writeback_instruction = entry->description;
// Store ALU result from register
if (entry->decoded.control_bits.rw == 1) {
unsigned int reg_num = (entry->decoded.control_bits.rdst?entry->decoded.fields.rd:entry->decoded.fields.rt);
registers[reg_num] = entry->alu_result;
}
pipeline.erase(entry);
}
list<PipelineEntry>::iterator memory_instruction_itr = pipeline.end();
list<PipelineEntry>::iterator execute_instruction_itr = pipeline.end();
list<PipelineEntry>::iterator decode_instruction_itr = pipeline.end();
list<PipelineEntry>::iterator fetch_instruction_itr = pipeline.end();
//
// Decode state
//
entry = oldest_with_state(pipeline, InstructionDecodeState, fetch_instruction_itr);
//bool decode_had_forwarding = false;
if (entry != pipeline.end()) {
decode_instruction = entry->description;
entry->decoded = decode(entry->instruction);
// Declare any hazards
entry->first_read_reg = entry->decoded.fields.rs;
entry->second_read_reg = entry->decoded.control_bits.asrc?-1:entry->decoded.fields.rt;
if (entry->second_read_reg == -1 && entry->decoded.control_bits.mw) {
entry->second_read_reg = entry->decoded.fields.rt;
}
if (entry->decoded.control_bits.rw && !entry->decoded.control_bits.mw) {
entry->reg_to_write = (entry->decoded.control_bits.rdst?entry->decoded.fields.rd:entry->decoded.fields.rt);
}
else {
entry->reg_to_write = -1;
}
//decode_had_forwarding = (entry->forwarded_first_reg || entry->forwarded_second_reg);
decode_instruction_itr = entry;
}
//
// Execute state
//
entry = oldest_with_state(pipeline, InstructionExecuteState, decode_instruction_itr);
// Do some trickery so we don't pick up the decode we just bumped
if (entry != pipeline.end()) {
execute_instruction = entry->description;
int16_t effective_imm = *(int16_t *)(&(entry->decoded.fields.imm));
int first_reg = entry->decoded.fields.rs;
int second_reg = entry->decoded.control_bits.asrc?-1:entry->decoded.fields.rt;
int32_t a = registers[first_reg];
int32_t b = (entry->decoded.control_bits.asrc?effective_imm:registers[second_reg]);
if (entry->forwarded_first_reg) {
a = entry->forwarded_first_value;
}
if (!entry->decoded.control_bits.asrc && entry->forwarded_second_reg) {
b = entry->forwarded_second_value;
}
entry->alu_result = alu(entry->decoded.control_bits.alu,
a,
b);
// See if any other instructions needed our output register's value
// DATA FORWARDING
if (entry->reg_to_write != -1 && !entry->decoded.control_bits.mr) {
list<PipelineEntry>::iterator itr = entry;
while (1) {
if (itr == pipeline.begin()) {
break;
}
--itr;
/*
if (itr->state < InstructionDecodeState) {
continue;
}*/
if (itr->first_read_reg == entry->reg_to_write) {
if (!itr->forwarded_first_reg) {
itr->forwarded_first_reg = true;
itr->forwarded_first_value = entry->alu_result;
itr->latest_forwarded_cycle = cycle_num;
}
}
if (itr->second_read_reg == entry->reg_to_write) {
if (!itr->forwarded_second_reg) {
itr->forwarded_second_reg = true;
itr->forwarded_second_value = entry->alu_result;
itr->latest_forwarded_cycle = cycle_num;
}