This repository has been archived on 2020-09-21. You can view files and clone it, but cannot push or open issues or pull requests.
cs2011-cachelab/csim.c

132 lines
3.2 KiB
C
Raw Normal View History

2016-04-25 17:07:24 -04:00
#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
#include <stdint.h>
2016-04-24 20:07:46 -04:00
#include "cachelab.h"
#define mprintf(...) if (VERBOSE) printf(__VA_ARGS__)
void print_usage();
size_t parse_int_arg(char *arg, char opt);
/**
If VERBOSE is non-zero, mprintf will not print. Otherwise, it will.
Set in main().
*/
int8_t VERBOSE = 0;
/* USURE. As defined in conference. */
struct line_s {
int8_t validity;
int tag;
};
typedef struct line_s line;
2016-04-26 10:30:54 -04:00
2016-04-25 17:07:24 -04:00
int main(int argc, char* argv[])
{
size_t set_index_bits = 0, lines = 0, block_bits = 0; // Useful variables.
char *filename = NULL;
int opt = 0;
while ((opt=getopt(argc, argv, "hvs:E:b:t:")) != -1) {
switch (opt) {
/* TODO: maybe add checking for optarg swallowing actual arguments. */
2016-04-25 17:07:24 -04:00
case 'v':
2016-04-26 10:30:54 -04:00
VERBOSE = 1;
2016-04-25 17:07:24 -04:00
break;
case 's': // 2^s = number of sets
set_index_bits = parse_int_arg(optarg, 's');
2016-04-25 17:07:24 -04:00
break;
case 'E': // associativity - lines per set
lines = parse_int_arg(optarg, 'E');
2016-04-25 17:07:24 -04:00
break;
case 'b': // 2^b = block size
block_bits = parse_int_arg(optarg, 'b');
2016-04-25 17:07:24 -04:00
break;
case 't':
filename = optarg;
2016-04-25 17:07:24 -04:00
break;
case 'h':
print_usage();
return 0;
default:
printf("Usage: ./csim [-hv] -s <number> -E <number> -b <number> -t <file>\n");
return 1;
2016-04-25 17:07:24 -04:00
}
}
/* If any required arguments were not provided. (argflags != 0b1111) */
if (!set_index_bits || !lines || !block_bits || !filename) {
printf("Usage: ./csim [-hv] -s <number> -E <number> -b <number> -t <file>\n");
return 1;
}
/* End of argument parsing. */
printf("Arguments: %zd, %zd, %zd, %hd, %s\n", set_index_bits, lines, block_bits, VERBOSE, filename);
2016-04-25 17:07:24 -04:00
2016-04-27 12:12:11 -04:00
char buffer[20];
FILE* f = fopen(filename, "r");
if(!f)
{
printf("Invalid file name: %s", filename);
exit(-1);
}
while (fgets(buffer, 20, f))
{
if (buffer[0] == 'I')
continue;
char* end;
char op = buffer[1];
long address = strtol(buffer+3, &end, 16);
if (*end != ',' || !(op == 'S' || op == 'L' || op == 'M'))
{
printf("Invalid input file, last line:\n%s\n", buffer);
return -1;
}
printf("%c %ld\n", op, address);
}
2016-04-26 10:43:07 -04:00
printSummary(0, 0, 0);
return 0;
2016-04-24 20:07:46 -04:00
}
void print_usage()
{
printf("Usage: ./csim [-hv] -s <number> -E <number> -b <number> -t <file>\n"
"Options:\n"
" -h Print this help.\n"
" -v Display trace info.\n"
" -s <number> The number of set index bits. Must be a positive integer.\n"
" -E <number> Associativity (lines per set). Must be a positive integer.\n"
" -b <number> The number of block bits. Must be a positive integer.\n"
" -t <file> Name of the file to read a valgrind trace from.");
}
/**
Parses a string
@param arg The string to parse.
@param opt The option being parsed.
@return The parsed integer if the argument was an integer.
@note Exits if argument is invalid
*/
size_t parse_int_arg(char* arg, char opt)
{
char* end;
long i = strtol(arg, &end, 0);
if (!i || *end != '\0') {
printf("Invalid argument \"%s\" for option -%c\n", arg, opt);
printf("Usage: ./csim [-hv] -s <number> -E <number> -b <number> -t <file>\n");
exit(1);
}
else if (i < 0) {
printf("Argument %ld for option %c too small (must be greater than 0)", i, opt);
exit(1);
}
else {
return (size_t)i;
}
}