-
Notifications
You must be signed in to change notification settings - Fork 0
/
simcache.cpp
801 lines (679 loc) · 28.2 KB
/
simcache.cpp
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
/*
CS-UY 2214
Jeff Epstein
Starter code for E20 cache assembler
simcache.cpp
*/
#include <cstddef>
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <limits>
#include <iomanip>
#include <regex>
#include "LRUCache.h"
using namespace std;
// Some helpful constant values that we'll be using.
size_t const static NUM_REGS = 8;
size_t const static MEM_SIZE = 1<<13;
/*
Prints out a correctly-formatted log entry.
@param cache_name The name of the cache where the event
occurred. "L1" or "L2"
@param status The kind of cache event. "SW", "HIT", or
"MISS"
@param pc The program counter of the memory
access instruction
@param addr The memory address being accessed.
@param line The cache line or set number where the data
is stored.
*/
void print_log_entry(const string &cache_name, const string &status, int pc, int addr, int line) {
cout << left << setw(8) << cache_name + " " + status << right <<
" pc:" << setw(5) << pc <<
"\taddr:" << setw(5) << addr <<
"\tline:" << setw(4) << line << endl;
}
/**
* alu(instruction, pc, registers)
*
* This function reads the last 4 bits of the instruction and performs the required operation,
* one of add, subtract, bitwise and, bitwise or, set less than, or jumping to a register. It decodes
* the middle 9 bits into their corresponding registers, and it also updates the pc. When updating
* the pc, it checks for overflow and wraps around if overflow is found.
*
* @param instruction - The numerical value of the current instruction
* @param pc - The current pc
* @param registers - Our register array
* @return Returns the next pc
*/
uint16_t alu(size_t instruction, uint16_t pc, uint16_t registers []){
//get bits 10 through 12 for regSrcA
size_t regSrcA = ((instruction & (0b111 << 10)) >> 10);
//get bits 7 through 9 for regSrcB
size_t regSrcB = ((instruction & (0b111 << 7)) >> 7);
//get bits 4 through 6 for regDst
size_t regDst = ((instruction & (0b111 << 4)) >> 4);
//keep the 4 least significant bits for the operation chooser
size_t operation = (instruction & 0xf);
switch (operation) {
case 0:
//add
registers[regDst] = registers[regSrcA] + registers[regSrcB];
pc++;
break;
case 1:
//sub
registers[regDst] = registers[regSrcA] - registers[regSrcB];
pc++;
break;
case 2:
//and - bitwise
registers[regDst] = registers[regSrcA] & registers[regSrcB];
pc++;
break;
case 3:
//or - bitwise
registers[regDst] = registers[regSrcA] | registers[regSrcB];
pc++;
break;
case 4:
//slt
registers[regDst] = (registers[regSrcA] < registers[regSrcB]) ? 1 : 0;
pc++;
break;
case 8:
//jr
pc = registers[regSrcA];
break;
default:
break;
}
/*ensure our pc does not overflow by only keeping the 13 least significant bits
if (pc >= MEM_SIZE) {
pc &= 0x1fff;
}*/
return pc;
}
/**
* jump(instruction)
*
* This function executes the jump instruction by putting the 13 least significant bits
* of the instruction into the pc.
*
* @param instruction - The numerical value of the instruction
* @return Returns the new pc, which is the last 13 bits of the instruction
*/
uint16_t jump(size_t instruction) {
//jump to the address given by the last 13 bits
uint16_t pc = (instruction & 0x1fff);
return pc;
}
/**
* slti(instruction, registers)
*
* This function executes the slti instruction. It decodes bits 7 through 12 into our source
* and destination registers, and sets the destination register to 1 if the source register's
* value is less than the sign-extended immediate data, and 0 otherwise. The comparison is
* unsigned - this means both numbers are treated as unsigned.
*
* @param instruction - The numerical value of the current instruction
* @param registers - Our register array
*/
void slti(size_t instruction, uint16_t registers[]) {
//get bits 10 through 12 for operand1 register
size_t operand1 = ((instruction & (0b111 << 10)) >> 10);
//the second operand is the unsigned representation of the last 7 bits of the instruction
uint16_t immData = (instruction & 0x7f);
//sign extend from 7 to 16 bits
immData |= (immData & (1 << 6)) ? (0x1ff << 7) : 0;
//get bits 7 through 9 for regDst
size_t regDst = ((instruction & (0b111 << 7)) >> 7);
registers[regDst] = (registers[operand1] < immData) ? 1 : 0;
}
/**
* jal(instruction, registers, pc)
*
* This function executes the jal instruction. It stores the next pc value into $7 after
* checking for overflow, and then calls jump to execute the jump part of the instruction.
*
* @param instruction - The numerical value of the current instruction
* @param registers - Our register array
* @param pc - The current pc value
* @return Returns the new pc value, which is the 13 bit immediate field of the instruction
*/
uint16_t jal(size_t instruction, uint16_t registers[], uint16_t pc) {
pc++;
/*ensure our pc does not overflow by only keeping the 13 least significant bits
if (pc >= MEM_SIZE) {
pc &= 0x1fff;
}*/
//placing the next instruction address into $7
registers[7] = pc;
//execute the jump
pc = jump(instruction);
return pc;
}
/**
* lw(instruction, registers, memory, L1)
*
* This function executes the lw instruction. It decodes bits 7 through 12 into the
* destination register and the address register, and sign extends the 7 bit immediate
* field to get the offset. Then it takes the value in memory found at the sum of the
* signed value of offset plus the value in the address register, and stores it into
* the destination register. Additionally, it accesses the cache and prints out whether
* it was a cache hit or miss.
*
* @param instruction - The numerical value of the current instruction
* @param registers - Our register array
* @param memory - Our memory array
* @param L1 - Reference to our cache
*/
void lw(uint16_t pc, size_t instruction, uint16_t registers[], const size_t memory [], vector<LRUCache> &L1) {
//the offset is the signed number represented by the least significant 7 bits of the instruction
//first take the least significant 7 bits
int16_t offset = (instruction & 0x7f);
//then sign extend from 7 bits to 16 bits
offset |= (offset & (1 << 6)) ? (0x1ff << 7) : 0;
//get bits 10 through 12 for regAddr
size_t regAddr = ((instruction & (0b111 << 10)) >> 10);
//get bits 7 through 9 for regDst
size_t regDst = ((instruction & (0b111 << 7)) >> 7);
//desired memory location is the least significant 13 bits of (value in $regAddr + offset)
int memLocation = ((offset + registers[regAddr]) & 0x1fff);
//get the cache line number this memory location will be stored on and check if it's already in the cache
int L1lineNumber = L1[0].lineNumber(memLocation);
bool isInCache = L1[L1lineNumber].accessCache(memLocation);
if(isInCache) {
print_log_entry("L1", "HIT", pc, memLocation, L1lineNumber);
}else {
print_log_entry("L1", "MISS", pc, memLocation, L1lineNumber);
}
//take the value found in the desired memory location and load it into $regDst
registers[regDst] = memory[memLocation];
}
/**
* lw(instruction, registers, memory, L1, L2)
*
* This function executes the lw instruction. It decodes bits 7 through 12 into the
* destination register and the address register, and sign extends the 7 bit immediate
* field to get the offset. Then it takes the value in memory found at the sum of the
* signed value of offset plus the value in the address register, and stores it into
* the destination register. Additionally, it accesses the first cache and prints out whether
* it was a cache hit or miss. If it was a miss, it accesses the second cache and does the
* same thing.
*
* @param instruction - The numerical value of the current instruction
* @param registers - Our register array
* @param memory - Our memory array
* @param L1 - Reference to our first cache
* @param L2 - Reference to our second cache
*/
void lw(uint16_t pc, size_t instruction, uint16_t registers[], const size_t memory [], vector<LRUCache> &L1, vector<LRUCache> &L2) {
//the offset is the signed number represented by the least significant 7 bits of the instruction
//first take the least significant 7 bits
int16_t offset = (instruction & 0x7f);
//then sign extend from 7 bits to 16 bits
offset |= (offset & (1 << 6)) ? (0x1ff << 7) : 0;
//get bits 10 through 12 for regAddr
size_t regAddr = ((instruction & (0b111 << 10)) >> 10);
//get bits 7 through 9 for regDst
size_t regDst = ((instruction & (0b111 << 7)) >> 7);
//desired memory location is the least significant 13 bits of (value in $regAddr + offset)
size_t memLocation = ((offset + registers[regAddr]) & 0x1fff);
//get the cache line number this memory location will be stored on and check if it's already in the cache
int L1lineNumber = L1[0].lineNumber(memLocation);
bool isInCache = L1[L1lineNumber].accessCache(memLocation);
if(isInCache) {
print_log_entry("L1", "HIT", pc, memLocation, L1lineNumber);
}else{ //if we had a miss on L1, check L2 and print
print_log_entry("L1", "MISS", pc, memLocation, L1lineNumber);
int L2lineNumber = L2[0].lineNumber(memLocation);
isInCache = L2[L2lineNumber].accessCache(memLocation);
if(isInCache) {
print_log_entry("L2", "HIT", pc, memLocation, L2lineNumber);
} else{
print_log_entry("L2", "MISS", pc, memLocation, L2lineNumber);
}
}
//take the value found in the desired memory location and load it into $regDst
registers[regDst] = memory[memLocation];
}
/**
* sw(instruction, registers, memory, L1)
*
* This function executes the sw instruction. It decodes bits 7 through 12 into the
* source register and the address register, and sign extends the 7 bit immediate
* field to get the offset. Then it takes the value found in the source register and
* stores it into memory at the address found by taking the sum of the signed value
* of offset plus the value in the address register. Additionally, it adds the new
* memory value to our cache and prints out the address, cache line, and pc of the
* cache access.
*
* @param instruction - The numerical value of the current instruction
* @param registers - Our register array
* @param memory - Our memory array
* @param L1 - Reference to our cache
*/
void sw(uint16_t pc, size_t instruction, const uint16_t registers[], size_t memory[], vector<LRUCache> &L1) {
//the offset is the signed number represented by the least significant 7 bits of the instruction
//first take the least significant 7 bits
int8_t offset = (instruction & 0x7f);
//then sign extend from 7 bits to 16 bits
offset |= (offset & (1 << 6)) ? (0x1ff << 7) : 0;
//get bits 10 through 12 for regAddr
size_t regAddr = ((instruction & (0b111 << 10)) >> 10);
//get bits 7 through 9 for regSrc
size_t regSrc = ((instruction & (0b111 << 7)) >> 7);
//desired memory location is the least significant 13 bits of (value in $regAddr + offset)
size_t memLocation = ((offset + registers[regAddr]) & 0x1fff);
//get the cache line that this memory address will be stored on, write to cache and print
int L1lineNumber = L1[0].lineNumber(memLocation);
L1[L1lineNumber].accessCache(memLocation);
print_log_entry("L1", "SW", pc, memLocation, L1lineNumber);
//take the value found in $regSrc and store it in the desired memory location
memory[memLocation] = registers[regSrc];
}
/**
* sw(instruction, registers, memory, L1, L2)
*
* This function executes the sw instruction. It decodes bits 7 through 12 into the
* source register and the address register, and sign extends the 7 bit immediate
* field to get the offset. Then it takes the value found in the source register and
* stores it into memory at the address found by taking the sum of the signed value
* of offset plus the value in the address register. Additionally, it adds the new
* memory value to both caches and prints out the address, cache line(s), and pc of
* the cache accesses.
*
* @param instruction - The numerical value of the current instruction
* @param registers - Our register array
* @param memory - Our memory array
* @param L1 - Reference to our first cache
* @param L2 - Reference to our second cache
*/
void sw(uint16_t pc, size_t instruction, const uint16_t registers[], size_t memory[], vector<LRUCache> &L1, vector<LRUCache> &L2) {
//the offset is the signed number represented by the least significant 7 bits of the instruction
//first take the least significant 7 bits
int8_t offset = (instruction & 0x7f);
//then sign extend from 7 bits to 16 bits
offset |= (offset & (1 << 6)) ? (0x1ff << 7) : 0;
//get bits 10 through 12 for regAddr
size_t regAddr = ((instruction & (0b111 << 10)) >> 10);
//get bits 7 through 9 for regSrc
size_t regSrc = ((instruction & (0b111 << 7)) >> 7);
//desired memory location is the least significant 13 bits of (value in $regAddr + offset)
size_t memLocation = ((offset + registers[regAddr]) & 0x1fff);
//get the L1 cache line that this memory address will be stored on, write to cache and print
int L1lineNumber = L1[0].lineNumber(memLocation);
L1[L1lineNumber].accessCache(memLocation);
print_log_entry("L1", "SW", pc, memLocation, L1lineNumber);
//get the L2 cache line that this memory address will be stored on, write to cache and print
int L2lineNumber = L2[0].lineNumber(memLocation);
L2[L2lineNumber].accessCache(memLocation);
print_log_entry("L2", "SW", pc, memLocation, L2lineNumber);
//take the value found in $regSrc and store it in the desired memory location
memory[memLocation] = registers[regSrc];
}
/**
* jeq(instruction, registers, pc)
*
* This function executes the jeq instruction. It decodes bits 7 through 12 into the 2
* operand registers, and performs an equality check on the values in those registers.
* If they're equal it returns the sum of pc + 1 + rel_imm. Rel_imm is considered a
* signed 7 bit number and is the sign-extended 7 bit immediate field. If the register
* values are not equal it returns the incremented pc.
*
* @param instruction - The numerical value of the current instruction
* @param registers - Our register array
* @param pc - The current pc value
* @return Returns the new pc value
*/
uint16_t jeq(size_t instruction, const uint16_t registers[], uint16_t pc) {
//get bits 10 through 12 for regSrcA
size_t regSrcA = ((instruction & (0b111 << 10)) >> 10);
//get bits 7 through 9 for regSrcB
size_t regSrcB = ((instruction & (0b111 << 7)) >> 7);
bool isEqual = (registers[regSrcA] == registers[regSrcB]);
pc++;
if(isEqual) {
//the rel_imm is the signed number represented by the least significant 7 bits of the instruction
//first take the least significant 7 bits
int16_t rel_imm = (instruction & 0x7f);
//then sign extend from 7 bits to 16 bits
rel_imm |= (rel_imm & (1 << 6)) ? (0x1ff << 7) : 0;
pc += rel_imm;
}
/*ensure our pc does not overflow by only keeping the 13 least significant bits
if (pc >= MEM_SIZE) {
pc &= 0x1fff;
}*/
return pc;
}
/**
* addi(instruction, registers)
*
* This function executes the addi instruction. It decodes bits 7 through 12 of the instruction
* into our source and destination registers, and sign extends the 7 bit immediate field data.
* It adds the signed immData to the value in the source register and places the sum into the
* destination register.
*
* @param instruction - The numerical value of the current instruction
* @param registers - Our register array
*/
void addi(size_t instruction, uint16_t registers[]) {
//the immData is the signed number represented by the least significant 7 bits of the instruction
//first take the least significant 7 bits
int16_t immData = (instruction & 0x7f);
//then sign extend from 7 bits to 16 bits
immData |= (immData & (1 << 6)) ? (0x1ff << 7) : 0;
//get bits 10 through 12 for regSrc
size_t regSrc = ((instruction & (0b111 << 10)) >> 10);
//get bits 7 through 9 for regDst
size_t regDst = ((instruction & (0b111 << 7)) >> 7);
registers[regDst] = immData + registers[regSrc];
}
/**
* simulation(memory, registers, L1)
*
* This function acts as a high level controller for our simulator. It takes in the memory array, which
* contains our instructions, our register array, and one cache. It checks the 3 most significant bits of the
* instruction and calls the corresponding function to execute the instruction, and updates the pc. It prevents
* the pc from exceeding its max value of 8191 by only looking at its first 13 bits, and also enforces $0's
* immutability by changing its value to 0 at the end of every instruction execution. It will run infinitely,
* going through each element of the memory array unless/until it encounters a halt instruction, and then
* returns the final value of the pc, which is where the halt is.
*
* @param memory - Our memory array, contains all of our instructions
* @param registers - Our register array
* @param L1 - Reference to our cache
* @return Returns the final pc
*/
uint16_t simulation(size_t memory [], uint16_t registers [], vector<LRUCache> &L1){
uint16_t pc = 0;
uint16_t lastPc;
bool isHalt = false;
size_t opCode;
size_t instruction;
while (!isHalt) {
lastPc = pc;
//Only use the least significant 13 bits of the pc for the instruction
instruction = memory[(pc & 0x1fff)];
//Only takes bits 13 through 15
opCode = ((instruction & 0xffff) >> 13);
switch (opCode) {
case 0: //add, sub, and, or, slt, jr
pc = alu(instruction, pc, registers);
break;
case 1: //slti
slti(instruction, registers);
pc++;
break;
case 2: //j - check if the instruction was a halt and set isHalt appropriately
pc = jump(instruction);
if(pc == lastPc)
isHalt = true;
break;
case 3: //jal
pc = jal(instruction, registers, pc);
break;
case 4: //lw
lw(pc, instruction, registers, memory, L1);
pc++;
break;
case 5: //sw
sw(pc, instruction, registers, memory, L1);
pc++;
break;
case 6: //jeq
pc = jeq(instruction, registers, pc);
break;
case 7: //addi
addi(instruction, registers);
pc++;
break;
default:
break;
}
//register $0 is always 0
registers[0] = 0;
}
return pc;
}
/**
* simulation(memory, registers, L1, L2)
*
* This function acts as a high level controller for our simulator. It takes in the memory array, which
* contains our instructions, our register array, and two caches. It checks the 3 most significant bits of the
* instruction and calls the corresponding function to execute the instruction, and updates the pc. It prevents
* the pc from exceeding its max value of 8191 by only looking at its first 13 bits, and also enforces $0's
* immutability by changing its value to 0 at the end of every instruction execution. It will run infinitely,
* going through each element of the memory array unless/until it encounters a halt instruction, and then
* returns the final value of the pc, which is where the halt is.
*
* @param memory - Our memory array, contains all of our instructions
* @param registers - Our register array
* @param L1 - Reference to our first cache
* @param L2 - Reference to our second cache
* @return Returns the final pc
*/
uint16_t simulation(size_t memory [], uint16_t registers [], vector<LRUCache> &L1, vector<LRUCache> &L2){
uint16_t pc = 0;
uint16_t lastPc;
bool isHalt = false;
size_t opCode;
size_t instruction;
while (!isHalt) {
lastPc = pc;
//Only use the least significant 13 bits of the pc for the instruction
instruction = memory[(pc & 0x1fff)];
//Only takes bits 13 through 15
opCode = ((instruction & 0xffff) >> 13);
switch (opCode) {
case 0: //add, sub, and, or, slt, jr
pc = alu(instruction, pc, registers);
break;
case 1: //slti
slti(instruction, registers);
pc++;
break;
case 2: //j - check if the instruction was a halt and set isHalt appropriately
pc = jump(instruction);
if(pc == lastPc)
isHalt = true;
break;
case 3: //jal
pc = jal(instruction, registers, pc);
break;
case 4: //lw
lw(pc, instruction, registers, memory, L1, L2);
pc++;
break;
case 5: //sw
sw(pc, instruction, registers, memory, L1, L2);
pc++;
break;
case 6: //jeq
pc = jeq(instruction, registers, pc);
break;
case 7: //addi
addi(instruction, registers);
pc++;
break;
default:
break;
}
//register $0 is always 0
registers[0] = 0;
}
return pc;
}
/*
Loads an E20 machine code file into the list
provided by mem. We assume that mem is
large enough to hold the values in the machine
code file.
@param f Open file to read from
@param mem Array representing memory into which to read program
*/
void load_machine_code(ifstream &f, size_t mem[]) {
regex machine_code_re("^ram\\[(\\d+)\\] = 16'b(\\d+);.*$");
size_t expectedaddr = 0;
string line;
while (getline(f, line)) {
smatch sm;
if (!regex_match(line, sm, machine_code_re)) {
cerr << "Can't parse line: " << line << endl;
exit(1);
}
size_t addr = stoi(sm[1], nullptr, 10);
size_t instr = stoi(sm[2], nullptr, 2);
if (addr != expectedaddr) {
cerr << "Memory addresses encountered out of sequence: " << addr << endl;
exit(1);
}
if (addr >= MEM_SIZE) {
cerr << "Program too big for memory" << endl;
exit(1);
}
expectedaddr ++;
mem[addr] = instr;
}
}
/*
Prints out the correctly-formatted configuration of a cache.
@param cache_name The name of the cache. "L1" or "L2"
@param size The total size of the cache, measured in memory cells.
Excludes metadata
@param assoc The associativity of the cache. One of [1,2,4,8,16]
@param blocksize The blocksize of the cache. One of [1,2,4,8,16,32,64])
*/
void print_cache_config(const string &cache_name, int size, int assoc, int blocksize, int num_lines) {
cout << "Cache " << cache_name << " has size " << size <<
", associativity " << assoc << ", blocksize " << blocksize <<
", lines " << num_lines << endl;
}
/**
Main function
Takes command-line args as documented below
*/
int main(int argc, char *argv[]) {
/*
Parse the command-line arguments
*/
char *filename = nullptr;
bool do_help = false;
bool arg_error = false;
string cache_config;
for (int i=1; i<argc; i++) {
string arg(argv[i]);
if (arg.rfind("-",0)==0) {
if (arg== "-h" || arg == "--help")
do_help = true;
else if (arg=="--cache") {
i++;
if (i>=argc)
arg_error = true;
else
cache_config = argv[i];
}
else
arg_error = true;
} else {
if (filename == nullptr)
filename = argv[i];
else
arg_error = true;
}
}
/* Display error message if appropriate */
if (arg_error || do_help || filename == nullptr) {
cerr << "usage " << argv[0] << " [-h] [--cache CACHE] filename" << endl << endl;
cerr << "Simulate E20 cache" << endl << endl;
cerr << "positional arguments:" << endl;
cerr << " filename The file containing machine code, typically with .bin suffix" << endl<<endl;
cerr << "optional arguments:"<<endl;
cerr << " -h, --help show this help message and exit"<<endl;
cerr << " --cache CACHE Cache configuration: size,associativity,blocksize (for one"<<endl;
cerr << " cache) or"<<endl;
cerr << " size,associativity,blocksize,size,associativity,blocksize"<<endl;
cerr << " (for two caches)"<<endl;
return 1;
}
ifstream f(filename);
if (!f.is_open()) {
cerr << "Can't open file "<<filename<<endl;
return 1;
}
//Load f and parse using load_machine_code
size_t memory [MEM_SIZE];
load_machine_code(f, memory);
//Create our array of registers
uint16_t registers [NUM_REGS] = {0};
//uint16_t pc;
/* parse cache config */
if (cache_config.size() > 0) {
vector<int> parts;
size_t pos;
size_t lastpos = 0;
while ((pos = cache_config.find(",", lastpos)) != string::npos) {
parts.push_back(stoi(cache_config.substr(lastpos,pos)));
lastpos = pos + 1;
}
parts.push_back(stoi(cache_config.substr(lastpos)));
if (parts.size() == 3) {
int L1size = parts[0];
int L1assoc = parts[1];
int L1blocksize = parts[2];
//Create our one cache and initialize its properties
//The entire vector is the cache. It is a vector of fully associative caches,
//and each line of the vector represents a line of the cache
vector<LRUCache> L1;
LRUCache temp(L1size, L1assoc, L1blocksize);
int L1numLines = temp.numLines();
print_cache_config("L1", L1size, L1assoc, L1blocksize, L1numLines);
//The vector should only be L1numLines in size
L1.reserve(L1numLines);
for(int i = 0; i < L1numLines; i++){
L1.push_back(temp);
}
//execute E20 program and simulate one cache here
simulation(memory, registers, L1);
} else if (parts.size() == 6) {
int L1size = parts[0];
int L1assoc = parts[1];
int L1blocksize = parts[2];
int L2size = parts[3];
int L2assoc = parts[4];
int L2blocksize = parts[5];
//Create our two caches and initialize their properties
//The entire vector is the cache. It is a vector of fully associative caches,
//and each line of the vector represents a line of the cache
vector<LRUCache> L1;
LRUCache temp1(L1size, L1assoc, L1blocksize);
int L1numLines = temp1.numLines();
//The vector should only be L1numLines in size
L1.reserve(L1numLines);
for(int i = 0; i < L1numLines; i++){
L1.push_back(temp1);
}
vector<LRUCache> L2;
LRUCache temp2(L2size, L2assoc, L2blocksize);
int L2numLines = temp2.numLines();
//The vector should only be L2\numLines in size
L2.reserve(L2numLines);
for(int i = 0; i < L2numLines; i++){
L2.push_back(temp2);
}
print_cache_config("L1", L1size, L1assoc, L1blocksize, L1numLines);
print_cache_config("L2", L2size, L2assoc, L2blocksize, L2numLines);
//execute E20 program and simulate two caches here
simulation(memory, registers, L1, L2);
} else {
cerr << "Invalid cache config" << endl;
return 1;
}
}
return 0;
}
//ra0Eequ6ucie6Jei0koh6phishohm9