See previous commit.

I did that slightly wrong, not quite sure how. But this one is the most-changed, and all the notes are in the last commit.
This commit is contained in:
Jacob 2016-04-27 13:41:51 -04:00
parent 3b97949150
commit 50ee8769fa

58
csim.c
View File

@ -54,13 +54,15 @@ int main(int argc, char* argv[])
VERBOSE = 1; VERBOSE = 1;
break; break;
case 's': // 2^s = number of sets case 's': // 2^s = number of sets
set_index_bits = parse_int_arg(optarg, 's'); set_index_bits = (uint8_t)parse_int_arg(optarg, 's');
set_indices = bits_to_size(set_index_bits);
break; break;
case 'E': // associativity - lines per set case 'E': // associativity - lines per set
lines = parse_int_arg(optarg, 'E'); lines = parse_int_arg(optarg, 'E');
break; break;
case 'b': // 2^b = block size case 'b': // 2^b = block size
block_bits = parse_int_arg(optarg, 'b'); block_bits = (uint8_t)parse_int_arg(optarg, 'b');
block_size = bits_to_size(block_bits);
break; break;
case 't': case 't':
filename = optarg; filename = optarg;
@ -69,18 +71,18 @@ int main(int argc, char* argv[])
print_usage(); print_usage();
return 0; return 0;
default: default:
printf("Usage: ./csim [-hv] -s <number> -E <number> -b <number> -t <file>\n"); bad_usage_error();
return 1;
} }
} }
/* If any required arguments were not provided. (argflags != 0b1111) */ /* If any required arguments were not provided. (argflags != 0b1111) */
if (!set_index_bits || !lines || !block_bits || !filename) { if (!set_indices || !lines || !block_size || !filename) {
printf("Usage: ./csim [-hv] -s <number> -E <number> -b <number> -t <file>\n"); bad_usage_error();
return 1;
} }
/* End of argument parsing. */ /* End of argument parsing. */
printf("Arguments: %zd, %zd, %zd, %hd, %s\n", set_index_bits, lines, block_bits, VERBOSE, filename); printf("Arguments: s=%hhu, E=%zu, b=%hhu, t=%s, v=%hhu, 2^s=%zd, 2^b=%zu\n", set_index_bits, lines, block_bits, filename, VERBOSE, set_indices, block_size);
/* FILE READING */
char buffer[20]; char buffer[20];
FILE* f = fopen(filename, "r"); FILE* f = fopen(filename, "r");
if(!f) if(!f)
@ -105,12 +107,13 @@ int main(int argc, char* argv[])
printf("%c %ld\n", op, address); printf("%c %ld\n", op, address);
} }
printSummary(0, 0, 0); printSummary(0, 0, 0);
return 0; return 0;
} }
void print_usage() void print_usage(void)
{ {
printf("Usage: ./csim [-hv] -s <number> -E <number> -b <number> -t <file>\n" printf("Usage: ./csim [-hv] -s <number> -E <number> -b <number> -t <file>\n"
"Options:\n" "Options:\n"
@ -121,30 +124,51 @@ void print_usage()
" -b <number> The number of block bits. 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."); " -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 Parses a string into a size_t.
@param arg The string to parse. @param arg The string to parse.
@param opt The option being parsed. @param opt The option being parsed. If 'E', the output can not be 0.
@return The parsed integer if the argument was an integer. @return The parsed integer if the argument was an integer.
@note Exits if argument is invalid @note Exits if argument is invalid.
*/ */
size_t parse_int_arg(char* arg, char opt) size_t parse_int_arg(char* arg, char opt)
{ {
char* end; char* end;
long i = strtol(arg, &end, 0); long i = strtol(arg, &end, 0);
if (!i || *end != '\0') { if (*end != '\0' || (!i && opt=='E')) { // associativity can not be zero
printf("Invalid argument \"%s\" for option -%c\n", arg, opt); printf("Invalid argument \"%s\" for option -%c\n", arg, opt);
printf("Usage: ./csim [-hv] -s <number> -E <number> -b <number> -t <file>\n"); bad_usage_error();
exit(1);
} }
else if (i < 0) { else if (i < 0) {
printf("Argument %ld for option %c too small (must be greater than 0)", i, opt); printf("Argument %ld for option %c too small (must be greater than 0)", i, opt);
exit(1); exit(1);
} }
else {
return (size_t)i; 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;
}