Browse Source

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.
masstesting
Jacob 6 years ago
parent
commit
50ee8769fa
  1. 58
      csim.c

58
csim.c

@ -54,13 +54,15 @@ int main(int argc, char* argv[]) @@ -54,13 +54,15 @@ int main(int argc, char* argv[])
VERBOSE = 1;
break;
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;
case 'E': // associativity - lines per set
lines = parse_int_arg(optarg, 'E');
break;
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;
case 't':
filename = optarg;
@ -69,18 +71,18 @@ int main(int argc, char* argv[]) @@ -69,18 +71,18 @@ int main(int argc, char* argv[])
print_usage();
return 0;
default:
printf("Usage: ./csim [-hv] -s <number> -E <number> -b <number> -t <file>\n");
return 1;
bad_usage_error();
}
}
/* 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;
if (!set_indices || !lines || !block_size || !filename) {
bad_usage_error();
}
/* 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];
FILE* f = fopen(filename, "r");
if(!f)
@ -88,7 +90,7 @@ int main(int argc, char* argv[]) @@ -88,7 +90,7 @@ int main(int argc, char* argv[])
printf("Invalid file name: %s", filename);
exit(-1);
}
while (fgets(buffer, 20, f))
{
if (buffer[0] == 'I')
@ -105,12 +107,13 @@ int main(int argc, char* argv[]) @@ -105,12 +107,13 @@ int main(int argc, char* argv[])
printf("%c %ld\n", op, address);
}
printSummary(0, 0, 0);
return 0;
}
void print_usage()
void print_usage(void)
{
printf("Usage: ./csim [-hv] -s <number> -E <number> -b <number> -t <file>\n"
"Options:\n"
@ -121,30 +124,51 @@ void print_usage() @@ -121,30 +124,51 @@ void print_usage()
" -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
Parses a string into a size_t.
@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.
@note Exits if argument is invalid
@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') {
if (*end != '\0' || (!i && opt=='E')) { // associativity can not be zero
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);
bad_usage_error();
}
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;
}
/**
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;
}