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