diff options
| author | ov-4 <hausemasterstream@gmail.com> | 2026-05-24 14:34:28 +0000 |
|---|---|---|
| committer | ov-4 <hausemasterstream@gmail.com> | 2026-05-24 14:34:28 +0000 |
| commit | 21c76a8ece5a7652d760c6f3d74c0dab2c55e0bd (patch) | |
| tree | 5b0dc60481497a81ff5f23f87612188fdea72b59 /cachelab-handout | |
| parent | af1ddae06b2eef9e94b65272200a5847d8a0eeaf (diff) | |
finished part A, with modern C++
Diffstat (limited to 'cachelab-handout')
| -rw-r--r-- | cachelab-handout/Makefile | 26 | ||||
| -rw-r--r-- | cachelab-handout/csim.c | 52 | ||||
| -rw-r--r-- | cachelab-handout/csim.cc | 144 |
3 files changed, 159 insertions, 63 deletions
diff --git a/cachelab-handout/Makefile b/cachelab-handout/Makefile index 1712d8c..6fc127d 100644 --- a/cachelab-handout/Makefile +++ b/cachelab-handout/Makefile @@ -5,21 +5,25 @@ CC = gcc CFLAGS = -g -Wall -Werror -std=c99 -m64 -all: csim test-trans tracegen - # Generate a handin tar file each time you compile - -tar -cvf ${USER}-handin.tar csim.c trans.c +CXX = g++ +CXXFLAGS = -g -Wall -Werror -std=c++17 -m64 -csim: csim.c cachelab.c cachelab.h - $(CC) $(CFLAGS) -o csim csim.c cachelab.c -lm +# all: csim test-trans tracegen +# # Generate a handin tar file each time you compile +# -tar -cvf ${USER}-handin.tar csim.c trans.c -test-trans: test-trans.c trans.o cachelab.c cachelab.h - $(CC) $(CFLAGS) -o test-trans test-trans.c cachelab.c trans.o +csim: csim.cc cachelab.c cachelab.h + $(CC) $(CFLAGS) -c cachelab.c -o cachelab.o + $(CXX) $(CXXFLAGS) -o csim csim.cc cachelab.o -lm -tracegen: tracegen.c trans.o cachelab.c - $(CC) $(CFLAGS) -O0 -o tracegen tracegen.c trans.o cachelab.c +# test-trans: test-trans.c trans.o cachelab.c cachelab.h +# $(CC) $(CFLAGS) -o test-trans test-trans.c cachelab.c trans.o -trans.o: trans.c - $(CC) $(CFLAGS) -O0 -c trans.c +# tracegen: tracegen.c trans.o cachelab.c +# $(CC) $(CFLAGS) -O0 -o tracegen tracegen.c trans.o cachelab.c + +# trans.o: trans.c +# $(CC) $(CFLAGS) -O0 -c trans.c # # Clean the src dirctory diff --git a/cachelab-handout/csim.c b/cachelab-handout/csim.c deleted file mode 100644 index 007d04b..0000000 --- a/cachelab-handout/csim.c +++ /dev/null @@ -1,52 +0,0 @@ -#include "cachelab.h" - -#include <stdio.h> - -#include <getopt.h> -#include <stdlib.h> -#include <unistd.h> - - -int time,s, E, b, setIndexLen, blockLen; -char traceFile[200]; - -void findSlot() -{ - ; -} - -void solve() -{ - ; -} - -int main(int argc, char *argv[]) -{ - int opt; - while ((opt = getopt(argc, argv, "s:E:b:t:")) != -1) - { - switch (opt) - { - case 's': - s = atol(optarg); - break; - case 'E': - E = atol(optarg); - break; - case 'b': - b = atol(optarg); - break; - case 't': - snprintf(traceFile, sizeof(traceFile), "%s", optarg); - break; - case '?': - printf("Unknown\n"); - break; - default: - break; - } - } - - printSummary(0, 0, 0); - return 0; -} diff --git a/cachelab-handout/csim.cc b/cachelab-handout/csim.cc new file mode 100644 index 0000000..ff0be4b --- /dev/null +++ b/cachelab-handout/csim.cc @@ -0,0 +1,144 @@ +#include <cstdio> +#include <iostream> +#include <fstream> +#include <string> +#include <vector> +#include <algorithm> +#include <cmath> + + +extern "C" { + void printSummary(int hits, int misses, int evictions); + + #include <getopt.h> + #include <stdlib.h> + #include <unistd.h> +} + +using namespace std; + +long long s, E, b, + hitCnt, missCnt, evaCnt; +size_t timeCnt; +string traceFile; + +class ov4Cache +{ +public: + bool valid = false; + size_t tag = 0; + size_t time = 0; +}; + +vector<vector<ov4Cache>> cache; + +void findFreeSlot(const size_t Tag, const size_t Set) // evaCnt +{ + // if we can find free slot! + auto findFree = find_if(cache[Set].begin(), cache[Set].end(), + [&](const ov4Cache &c) + { + return !c.valid; + }); + if (findFree != cache[Set].end()) + { + findFree->valid = true; + findFree->time = timeCnt; + findFree->tag = Tag; + return; + } + + // no slot is free, and we have to find the oldest + evaCnt++; + auto findOld = min_element(cache[Set].begin(), cache[Set].end(), + [](const ov4Cache &x, const ov4Cache &y) + { + return x.time < y.time; + }); + findOld->valid = true; + findOld->time = timeCnt; + findOld->tag = Tag; +} + +void solve(const size_t addr) // hitCnt, missCnt +{ + timeCnt++; + size_t Tag, Set; + Tag = addr >> (s+b); + Set = ((addr << (64-s-b)) >> (64-s-b))>>b; + + auto res = find_if(cache[Set].begin(), cache[Set].end(), + [&](const ov4Cache &c) + { + return c.valid && c.tag == Tag; + }); + if (res != cache[Set].end()) + { + hitCnt++; + res->time = timeCnt; + return; + } + + missCnt++; + findFreeSlot(Tag, Set); +} + + + + +int main(int argc, char *argv[]) +{ + // get arguments + int opt = '?'; + while ((opt = getopt(argc, argv, "s:E:b:t:")) != -1) + { + switch (opt) + { + case 's': + s = atol(optarg); + break; + case 'E': + E = atol(optarg); + break; + case 'b': + b = atol(optarg); + break; + case 't': + traceFile = optarg; + break; + case '?': + cerr << "Unknow arg" << endl; + break; + default: + break; + } + } + + // update size of cache + cache.resize((1<<s), vector<ov4Cache>(E)); + + ifstream file(traceFile); + if (!file.is_open()) { + cerr << "LOL" << endl; + return -1; + } + + string line; + while (getline(file, line)) + { + if (line.size()==0 || line[0] == 'I') continue; // ignore I + char op; + size_t addr; + int useless; + sscanf(line.c_str(), " %c %zx,%d", &op, &addr, &useless); + + if (op == 'S' || op == 'L') solve(addr); + else {solve(addr); solve(addr); } + } + + + printSummary(hitCnt, missCnt, evaCnt); + + file.close(); + return 0; +} |
