fb6c9e3a27
Renamed lines to num_lines. Removed trailing whitespaces. Fixed some indentation and comments. Fixed usage string.
176 lines
4.3 KiB
C
176 lines
4.3 KiB
C
/* asgoldsmith-jikomissar */
|
|
/**
|
|
@file csim.c
|
|
@author Adam Goldsmith
|
|
@author Jacob Komissar
|
|
@date 2016-04-25, 2016-04-26, 2016-04-27
|
|
|
|
@TODO ADD CORRECT NAME COMMENT AT TOP OF FILE
|
|
|
|
Exit statuses:
|
|
0 - success
|
|
1 - usage error
|
|
-1 - file error
|
|
|
|
*/
|
|
/* Headers */
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <getopt.h>
|
|
#include "cachelab.h"
|
|
|
|
/* Typedefs and structs */
|
|
typedef unsigned char uint8_t;
|
|
/* UNSURE. As defined in conference. */
|
|
struct line_s {
|
|
uint8_t validity;
|
|
int tag;
|
|
};
|
|
typedef struct line_s line;
|
|
|
|
/* Macros */
|
|
#define mprintf(...) if (VERBOSE) printf(__VA_ARGS__)
|
|
|
|
/* Prototypes */
|
|
void print_usage(void);
|
|
void bad_usage_error(void);
|
|
size_t parse_int_arg(char *arg, char opt);
|
|
size_t bits_to_size(uint8_t bits);
|
|
|
|
/* Global variables */
|
|
uint8_t VERBOSE = 0; /**< If nonzero, mprintf will not print. Set in main() if -v flag is given. */
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
{
|
|
int hits, misses, evictions;
|
|
|
|
uint8_t set_index_bits = 0, block_bits = 0; // Not-useful input variables.
|
|
size_t set_indices = 0, num_lines = 0, block_size = 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. */
|
|
case 'v':
|
|
VERBOSE = 1;
|
|
break;
|
|
case 's': // 2^s = number of sets
|
|
set_index_bits = (uint8_t)parse_int_arg(optarg, 's');
|
|
set_indices = bits_to_size(set_index_bits);
|
|
break;
|
|
case 'E': // associativity - lines per set
|
|
num_lines = parse_int_arg(optarg, 'E');
|
|
break;
|
|
case 'b': // 2^b = block size
|
|
block_bits = (uint8_t)parse_int_arg(optarg, 'b');
|
|
block_size = bits_to_size(block_bits);
|
|
break;
|
|
case 't':
|
|
filename = optarg;
|
|
break;
|
|
case 'h':
|
|
print_usage();
|
|
return 0;
|
|
default:
|
|
bad_usage_error();
|
|
}
|
|
}
|
|
/* If any required arguments were not provided. (argflags != 0b1111) */
|
|
if (!set_indices || !num_lines || !block_size || !filename) {
|
|
bad_usage_error();
|
|
}
|
|
/* End of argument parsing. */
|
|
//printf("Arguments: s=%hhu, E=%zu, b=%hhu, t=%s, v=%hhu, 2^s=%zd, 2^b=%zu\n", set_index_bits, num_lines, block_bits, filename, VERBOSE, set_indices, block_size);
|
|
|
|
|
|
/* FILE READING */
|
|
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];
|
|
void* 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);
|
|
}
|
|
|
|
printSummary(0, 0, 0);
|
|
return 0;
|
|
}
|
|
|
|
|
|
void print_usage(void)
|
|
{
|
|
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.");
|
|
}
|
|
|
|
/**
|
|
Prints the summarized usage and exits with status 1.
|
|
*/
|
|
void bad_usage_error(void)
|
|
{
|
|
printf("Usage: ./csim [-hv] -s <number> -E <number> -b <number> -t <file>\n");
|
|
exit(1);
|
|
}
|
|
|
|
/**
|
|
Parses a string into a size_t.
|
|
@param arg The string to parse.
|
|
@param opt The option being parsed. If 'E', the output can not be 0.
|
|
@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 (*end != '\0' || (!i && opt=='E')) { // associativity can not be zero
|
|
printf("Invalid argument \"%s\" for option -%c\n", arg, opt);
|
|
bad_usage_error();
|
|
}
|
|
else if (i < 0) {
|
|
printf("Argument %ld for option %c too small (must be greater than 0)", i, opt);
|
|
exit(1);
|
|
}
|
|
return (size_t)i;
|
|
}
|
|
|
|
/**
|
|
Calculates a size using a given number of bits.
|
|
@param bits The power of 2 to use.
|
|
@note Exits if number of bits is larger than a size_t.
|
|
*/
|
|
size_t bits_to_size(uint8_t bits)
|
|
{
|
|
size_t max_shift = 8*sizeof(size_t);
|
|
if (bits > max_shift-1) {
|
|
printf("Argument %hhu too large (-s and -b must be less than %zu)", bits, max_shift);
|
|
exit(1);
|
|
}
|
|
return (size_t)1 << bits;
|
|
}
|
|
|
|
|