Renamed block_bits to block_offset_bits. Fixed minor type errors.

Fixed format specifiers in bits_to_size(), fixed casts for set_index_bits and block_offset_bits during argument parsing.
This commit is contained in:
Jacob 2016-04-29 10:48:55 -04:00
parent 80c595ada2
commit f018d83848

29
csim.c
View File

@ -14,10 +14,6 @@
0 - success
1 - usage error
-1 - file error
@TODO Rename block_bits to block_offset_bits.
@TODO Fix format
@TODO Fix casts of parse_int_arg in main to int rather than uint8_t.
*/
/**** Headers ****/
@ -74,7 +70,7 @@ void print_cache(linked_line** cache, long num_sets);
void print_set(linked_line* line);
/* Cache access functions */
char cache_access(linked_line** cache, long address, int set_bits, int block_bits, long num_lines);
char cache_access(linked_line** cache, long address, int set_bits, int block_offset_bits, long num_lines);
linked_line* find_line(linked_line* set_head, long tag);
//linked_line* move_to_head(linked_line* new_head, linked_line* old_head);
@ -84,7 +80,7 @@ int main(int argc, char* argv[])
{
results score;
char buffer[20];
int set_index_bits = 0, block_bits = 0; // Not-useful input variables.
int set_index_bits = 0, block_offset_bits = 0; // Not-useful input variables.
long num_sets = 0, num_lines = 0, block_size = 0; //Useful variables
char *filename = NULL;
int opt = 0;
@ -100,15 +96,15 @@ int main(int argc, char* argv[])
VERBOSE = 1;
break;
case 's': // 2^s = number of sets
set_index_bits = (uint8_t)parse_int_arg(optarg, 's');
set_index_bits = (int)parse_int_arg(optarg, 's');
num_sets = 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);
block_offset_bits = (int)parse_int_arg(optarg, 'b');
block_size = bits_to_size(block_offset_bits);
break;
case 't':
filename = optarg;
@ -156,10 +152,10 @@ int main(int argc, char* argv[])
mprintf(" %c %lx,%lx", op, address, size);
char r = cache_access(cache, address, set_index_bits, block_bits, num_lines);
char r = cache_access(cache, address, set_index_bits, block_offset_bits, num_lines);
increment_result_counters(r, &score);
if (op == 'M') { //Access again if modifying
r = cache_access(cache, address, set_index_bits, block_bits, num_lines);
r = cache_access(cache, address, set_index_bits, block_offset_bits, num_lines);
increment_result_counters(r, &score);
}
@ -228,13 +224,12 @@ long parse_int_arg(char* arg, char opt)
@param bits The power of 2 to use.
@return 2^bits if it does not overflow.
@note Exits if number of bits is larger than a signed int.
@TODO Fix format string to ints.
*/
long bits_to_size(int bits)
{
int max_shift = 8*sizeof(int)-1;
if (bits > max_shift-1) {
printf("Argument %hhd too large (-s and -b must be less than %hhd)", bits, max_shift);
printf("Argument %d too large (-s and -b must be less than %d)", bits, max_shift);
exit(1);
}
return (long)1 << bits;
@ -361,7 +356,7 @@ void print_set(linked_line* line)
@param cache The cache to access.
@param address The address to access in the cache.
@param set_bits The number of set bits in the address.
@param block_bits The number of block offset bits in the address.
@param block_offset_bits The number of block offset bits in the address.
@param num_lines The number of lines per set.
@return 'H' if a hit, 'M' if a miss, 'E' if a miss and eviction
@ -370,11 +365,11 @@ void print_set(linked_line* line)
@TODO Make the re-linking of the set into its own function.
*/
char cache_access(linked_line** cache, long address,
int set_bits, int block_bits, long num_lines)
int set_bits, int block_offset_bits, long num_lines)
{
char out;
long tag = address >> set_bits >> block_bits;
long set = (address >> block_bits) & ((1 << set_bits) - 1);
long tag = address >> set_bits >> block_offset_bits;
long set = (address >> block_offset_bits) & ((1 << set_bits) - 1);
linked_line* set_head = cache[set];
linked_line* line = find_line(set_head, tag);