#include #include #include #include #include int bbits = 4; // 16 bytes per block int sbits = 4; // 16 sets int E = 1; // direct-mapped cache int verbose = 0; // default of NOT verbose FILE *infile = NULL; char *filename = "NOFILE"; void usage(char * prog) { fprintf(stderr, "Usage: %s -v -s -b -E -t \n", prog); exit(1); } void processArgs(int argc, char** argv) { char c; infile = stdin; while((c = getopt(argc, argv, "b:s:E:t:vhq")) != -1) { switch(c) { case 'v': verbose = 1 - verbose; break; case 'h': usage(argv[0]); break; case 'q': verbose = 0; break; case 't': filename = strdup(optarg); infile = fopen(filename, "r"); if (!infile) { fprintf(stderr, "Cannot open file %s\n", optarg); usage(argv[0]); } break; case 'E': E = strtol(optarg, NULL, 0); if (E <=0 ) { if (E == 0 && errno == EINVAL) { fprintf(stderr, "Unable to convert -E argument: %s\n", optarg); } else { fprintf(stderr, "Invalid argument to -E: %d\n", E); } usage(argv[0]); } break; case 's': sbits = strtol(optarg, NULL, 0); if (sbits <=0 ) { if (sbits == 0 && errno == EINVAL) { fprintf(stderr, "Unable to convert -s argument: %s\n", optarg); } else { fprintf(stderr, "Invalid argument to -s: %d\n", sbits); } usage(argv[0]); } break; case 'b': bbits = strtol(optarg, NULL, 0); if (bbits <=0 ) { if (bbits == 0 && errno == EINVAL) { fprintf(stderr, "Unable to convert -b argument: %s\n", optarg); } else { fprintf(stderr, "Invalid argument to -b: %d\n", bbits); } usage(argv[0]); } break; default: usage(argv[0]); } } } void printCachesimResults(int hits, int misses, int evictions) { printf(" Hits: %d\n", hits); printf(" Misses: %d\n", misses); printf("Evictions: %d\n", evictions); } int main(int argc, char **argv) { int hits = 0; int misses = 0; int evictions = 0; processArgs(argc, argv); if (verbose) { printf("E = %2d, s = %2d, b = %2d, file: %s\n", E, sbits, bbits, filename); } /* Fill in the body of the simulator here */ printCachesimResults(hits, misses, evictions); return 0; }