Skip to content

Instantly share code, notes, and snippets.

@michaelee
Last active Jun 17, 2021
Embed
What would you like to do?
csim-starter
#include <getopt.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include "cachelab.h"
void load_trace(char* tracefile)
{
char buf[1000];
uint64_t addr=0;
unsigned int len=0;
FILE* fp = fopen(tracefile, "r");
if (!fp){
fprintf(stderr, "%s: %s\n", tracefile, strerror(errno));
exit(1);
}
while (fgets(buf, 1000, fp) != NULL) {
if (buf[1]=='S' || buf[1]=='L' || buf[1]=='M') {
sscanf(buf+3, "%lx,%u", &addr, &len);
printf("%c, %lx\n", buf[1], addr);
}
}
fclose(fp);
}
void print_usage(char* argv[])
{
printf("Usage: %s [-hv] -s <num> -E <num> -b <num> -t <file>\n", argv[0]);
printf("Options:\n");
printf(" -h Print this help message.\n");
printf(" -v Optional verbose flag.\n");
printf(" -s <num> Number of set index bits.\n");
printf(" -E <num> Number of lines per set.\n");
printf(" -b <num> Number of block offset bits.\n");
printf(" -t <file> Trace file.\n");
exit(0);
}
int main(int argc, char* argv[])
{
char c;
int s, S, E, b, B;
char *trace_file;
while((c=getopt(argc,argv,"s:E:b:t:")) != -1){
switch(c){
case 's':
s = atoi(optarg);
break;
case 'E':
E = atoi(optarg);
break;
case 'b':
b = atoi(optarg);
break;
case 't':
trace_file = optarg;
break;
default:
print_usage(argv);
exit(1);
}
}
if (s == 0 || E == 0 || b == 0 || trace_file == NULL) {
printf("%s: Missing required command line argument\n", argv[0]);
print_usage(argv);
exit(1);
}
S = (unsigned int) pow(2, s);
B = (unsigned int) pow(2, b);
printf("Using cache with S=%d, E=%d, B=%d\n", S, E, B);
load_trace(trace_file);
printSummary(0, 0, 0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment