2016-04-27 15:55:27 -04:00
|
|
|
/* asgoldsmith-jikomissar */
|
2016-04-27 13:40:58 -04:00
|
|
|
/**
|
|
|
|
@file csim.c
|
|
|
|
@author Adam Goldsmith
|
|
|
|
@author Jacob Komissar
|
2016-04-28 12:01:30 -04:00
|
|
|
@date 2016-04-25, 2016-04-26, 2016-04-27, 2016-04-28
|
2016-04-27 15:55:27 -04:00
|
|
|
|
2016-04-28 12:01:30 -04:00
|
|
|
Cachelab: csim.c
|
|
|
|
Simulates a cache with arbitrary sets, associativity, and block size.
|
|
|
|
Reads from a valgrind trace, and outputs the number of hits, misses, and evictions.
|
|
|
|
With the verbose option (-v), outputs details for each operation.
|
2016-04-27 15:55:27 -04:00
|
|
|
|
2016-04-28 12:01:30 -04:00
|
|
|
Exit statuses:
|
2016-04-29 17:46:48 -04:00
|
|
|
0 - success
|
|
|
|
1 - usage error
|
2016-04-28 12:01:30 -04:00
|
|
|
-1 - file error
|
2016-04-29 17:46:48 -04:00
|
|
|
2 - memory error
|
2016-04-28 12:01:30 -04:00
|
|
|
*/
|
2016-04-27 13:40:58 -04:00
|
|
|
|
2016-04-28 12:01:30 -04:00
|
|
|
/**** Headers ****/
|
2016-04-25 17:07:24 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2016-04-27 10:50:33 -04:00
|
|
|
#include <getopt.h>
|
2016-04-24 20:07:46 -04:00
|
|
|
#include "cachelab.h"
|
|
|
|
|
2016-04-28 12:01:30 -04:00
|
|
|
|
|
|
|
/******** Typedefs and structs ********/
|
2016-04-27 13:40:58 -04:00
|
|
|
typedef unsigned char uint8_t;
|
2016-04-27 18:18:30 -04:00
|
|
|
|
2016-04-28 12:01:30 -04:00
|
|
|
typedef struct {
|
|
|
|
int hits;
|
|
|
|
int misses;
|
|
|
|
int evictions;
|
|
|
|
} results;
|
|
|
|
|
2016-04-27 21:24:53 -04:00
|
|
|
/* Linked Line and related prototypes */
|
|
|
|
typedef struct linked_line_s linked_line;
|
|
|
|
struct linked_line_s {
|
2016-04-27 13:40:58 -04:00
|
|
|
uint8_t validity;
|
2016-04-27 21:24:53 -04:00
|
|
|
long tag;
|
|
|
|
linked_line* newer;
|
|
|
|
linked_line* older;
|
2016-04-27 13:40:58 -04:00
|
|
|
};
|
2016-04-27 21:24:53 -04:00
|
|
|
|
2016-04-27 13:40:58 -04:00
|
|
|
|
2016-04-28 12:01:30 -04:00
|
|
|
/******** Macros ********/
|
2016-04-27 10:50:33 -04:00
|
|
|
#define mprintf(...) if (VERBOSE) printf(__VA_ARGS__)
|
|
|
|
|
2016-04-28 12:01:30 -04:00
|
|
|
|
|
|
|
/******** Global variables ********/
|
|
|
|
static uint8_t VERBOSE = 0; /**< If nonzero, mprintf will not print. Set in main() if -v flag is given. */
|
|
|
|
|
|
|
|
/******** General Prototypes ********/
|
|
|
|
|
|
|
|
/* Option-parsing functions */
|
2016-04-27 13:40:58 -04:00
|
|
|
void print_usage(void);
|
|
|
|
void bad_usage_error(void);
|
2016-04-27 18:18:30 -04:00
|
|
|
long parse_int_arg(char *arg, char opt);
|
|
|
|
long bits_to_size(int bits);
|
2016-04-28 12:01:30 -04:00
|
|
|
|
|
|
|
/* Sub-main functions */
|
|
|
|
void increment_result_counters(char result, results* score);
|
|
|
|
|
|
|
|
/* Cache creation/deletion functions */
|
2016-04-28 01:46:49 -04:00
|
|
|
linked_line** make_cache(long num_sets, long num_lines);
|
2016-04-27 21:24:53 -04:00
|
|
|
linked_line* make_set(linked_line* newer, long num_lines);
|
|
|
|
void free_cache(linked_line** cache, long num_sets);
|
|
|
|
void free_set(linked_line* line);
|
2016-04-28 12:01:30 -04:00
|
|
|
/* Cache-viewing functions */
|
2016-04-27 22:16:16 -04:00
|
|
|
void print_cache(linked_line** cache, long num_sets);
|
|
|
|
void print_set(linked_line* line);
|
2016-04-28 12:01:30 -04:00
|
|
|
|
|
|
|
/* Cache access functions */
|
2016-04-29 10:48:55 -04:00
|
|
|
char cache_access(linked_line** cache, long address, int set_bits, int block_offset_bits, long num_lines);
|
2016-04-28 01:42:47 -04:00
|
|
|
linked_line* find_line(linked_line* set_head, long tag);
|
2016-04-29 17:57:02 -04:00
|
|
|
linked_line* update_set(linked_line* old_head, linked_line* new_head);
|
2016-04-28 12:01:30 -04:00
|
|
|
//linked_line* move_to_head(linked_line* new_head, linked_line* old_head);
|
2016-04-27 10:50:33 -04:00
|
|
|
|
2016-04-26 10:30:54 -04:00
|
|
|
|
2016-04-28 12:01:30 -04:00
|
|
|
/** MAIN **/
|
2016-04-25 17:07:24 -04:00
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2016-04-28 01:40:54 -04:00
|
|
|
results score;
|
2016-04-27 18:18:30 -04:00
|
|
|
char buffer[20];
|
2016-04-29 10:48:55 -04:00
|
|
|
int set_index_bits = 0, block_offset_bits = 0; // Not-useful input variables.
|
2016-04-28 01:46:49 -04:00
|
|
|
long num_sets = 0, num_lines = 0, block_size = 0; //Useful variables
|
2016-04-27 11:30:58 -04:00
|
|
|
char *filename = NULL;
|
2016-04-27 12:07:51 -04:00
|
|
|
int opt = 0;
|
2016-04-28 01:40:54 -04:00
|
|
|
|
|
|
|
score.hits = 0;
|
|
|
|
score.misses = 0;
|
|
|
|
score.evictions = 0;
|
|
|
|
|
2016-04-27 10:50:33 -04:00
|
|
|
while ((opt=getopt(argc, argv, "hvs:E:b:t:")) != -1) {
|
|
|
|
switch (opt) {
|
|
|
|
/* TODO: maybe add checking for optarg swallowing actual arguments. */
|
2016-04-25 17:07:24 -04:00
|
|
|
case 'v':
|
2016-04-26 10:30:54 -04:00
|
|
|
VERBOSE = 1;
|
2016-04-25 17:07:24 -04:00
|
|
|
break;
|
2016-04-27 10:50:33 -04:00
|
|
|
case 's': // 2^s = number of sets
|
2016-04-29 10:48:55 -04:00
|
|
|
set_index_bits = (int)parse_int_arg(optarg, 's');
|
2016-04-28 01:46:49 -04:00
|
|
|
num_sets = bits_to_size(set_index_bits);
|
2016-04-25 17:07:24 -04:00
|
|
|
break;
|
2016-04-27 10:50:33 -04:00
|
|
|
case 'E': // associativity - lines per set
|
2016-04-27 15:55:27 -04:00
|
|
|
num_lines = parse_int_arg(optarg, 'E');
|
2016-04-25 17:07:24 -04:00
|
|
|
break;
|
2016-04-27 10:50:33 -04:00
|
|
|
case 'b': // 2^b = block size
|
2016-04-29 10:48:55 -04:00
|
|
|
block_offset_bits = (int)parse_int_arg(optarg, 'b');
|
|
|
|
block_size = bits_to_size(block_offset_bits);
|
2016-04-25 17:07:24 -04:00
|
|
|
break;
|
|
|
|
case 't':
|
2016-04-27 10:50:33 -04:00
|
|
|
filename = optarg;
|
2016-04-25 17:07:24 -04:00
|
|
|
break;
|
2016-04-27 10:50:33 -04:00
|
|
|
case 'h':
|
|
|
|
print_usage();
|
|
|
|
return 0;
|
|
|
|
default:
|
2016-04-27 13:41:51 -04:00
|
|
|
bad_usage_error();
|
2016-04-25 17:07:24 -04:00
|
|
|
}
|
|
|
|
}
|
2016-04-27 10:50:33 -04:00
|
|
|
/* If any required arguments were not provided. (argflags != 0b1111) */
|
2016-04-28 01:46:49 -04:00
|
|
|
if (!num_sets || !num_lines || !block_size || !filename) {
|
2016-04-27 13:41:51 -04:00
|
|
|
bad_usage_error();
|
2016-04-27 10:50:33 -04:00
|
|
|
}
|
|
|
|
/* End of argument parsing. */
|
2016-04-25 17:07:24 -04:00
|
|
|
|
2016-04-28 01:46:49 -04:00
|
|
|
linked_line** cache = make_cache(num_sets, num_lines);
|
2016-04-27 16:13:27 -04:00
|
|
|
|
2016-04-27 13:41:51 -04:00
|
|
|
/* FILE READING */
|
2016-04-27 12:12:11 -04:00
|
|
|
FILE* f = fopen(filename, "r");
|
2016-04-29 17:46:48 -04:00
|
|
|
if(!f) {
|
2016-04-27 12:12:11 -04:00
|
|
|
printf("Invalid file name: %s", filename);
|
|
|
|
exit(-1);
|
|
|
|
}
|
2016-04-27 15:55:27 -04:00
|
|
|
|
2016-04-27 18:18:30 -04:00
|
|
|
//char buffer[20];
|
2016-04-27 15:55:27 -04:00
|
|
|
while (fgets(buffer, 20, f)) {
|
2016-04-29 17:46:48 -04:00
|
|
|
if (buffer[0] == 'I') {
|
2016-04-27 12:12:11 -04:00
|
|
|
continue;
|
2016-04-29 17:46:48 -04:00
|
|
|
}
|
2016-04-27 12:12:11 -04:00
|
|
|
|
|
|
|
char* end;
|
|
|
|
char op = buffer[1];
|
2016-04-27 18:18:30 -04:00
|
|
|
long address = strtol(buffer+3, &end, 16);
|
2016-04-28 01:54:49 -04:00
|
|
|
long size = strtol(end+1, NULL, 16);
|
2016-04-28 12:01:30 -04:00
|
|
|
|
2016-04-27 15:55:27 -04:00
|
|
|
if (*end != ',' || !(op == 'S' || op == 'L' || op == 'M')) {
|
2016-04-27 12:12:11 -04:00
|
|
|
printf("Invalid input file, last line:\n%s\n", buffer);
|
|
|
|
return -1;
|
|
|
|
}
|
2016-04-28 01:42:24 -04:00
|
|
|
|
2016-04-28 01:46:49 -04:00
|
|
|
/* if(VERBOSE) */
|
|
|
|
/* print_cache(cache, num_sets); */
|
2016-04-28 01:54:49 -04:00
|
|
|
|
2016-04-28 20:53:59 -04:00
|
|
|
mprintf(" %c %lx,%lx", op, address, size);
|
|
|
|
|
2016-04-29 10:48:55 -04:00
|
|
|
char r = cache_access(cache, address, set_index_bits, block_offset_bits, num_lines);
|
2016-04-28 01:43:56 -04:00
|
|
|
increment_result_counters(r, &score);
|
|
|
|
if (op == 'M') { //Access again if modifying
|
2016-04-29 10:48:55 -04:00
|
|
|
r = cache_access(cache, address, set_index_bits, block_offset_bits, num_lines);
|
2016-04-28 01:43:56 -04:00
|
|
|
increment_result_counters(r, &score);
|
|
|
|
}
|
2016-04-28 20:53:59 -04:00
|
|
|
|
|
|
|
mprintf("\n");
|
2016-04-27 12:12:11 -04:00
|
|
|
}
|
|
|
|
|
2016-04-28 01:40:54 -04:00
|
|
|
printSummary(score.hits, score.misses, score.evictions);
|
2016-04-27 16:15:02 -04:00
|
|
|
|
|
|
|
fclose(f);
|
2016-04-27 21:24:53 -04:00
|
|
|
free_cache(cache, set_index_bits);
|
2016-04-27 16:13:27 -04:00
|
|
|
|
2016-04-26 10:43:07 -04:00
|
|
|
return 0;
|
2016-04-24 20:07:46 -04:00
|
|
|
}
|
2016-04-27 12:03:51 -04:00
|
|
|
|
2016-04-28 12:01:30 -04:00
|
|
|
|
|
|
|
|
|
|
|
/************************** OPTION-PARSING FUNCTIONS **************************/
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-04-29 10:44:40 -04:00
|
|
|
Parses a string into a an positive integer; error if not possible.
|
2016-04-28 12:01:30 -04:00
|
|
|
@param arg The string to parse.
|
|
|
|
@param opt The option being parsed. If 'E', the output can not be 0.
|
2016-04-29 10:44:40 -04:00
|
|
|
Otherwise, used only for helpful error messages.
|
|
|
|
@return The parsed long integer if the argument was valid .
|
2016-04-28 12:01:30 -04:00
|
|
|
@note Exits if argument is invalid.
|
|
|
|
*/
|
|
|
|
long parse_int_arg(char* arg, char opt)
|
|
|
|
{
|
|
|
|
char* end;
|
|
|
|
long i = strtol(arg, &end, 0);
|
|
|
|
|
2016-04-29 17:17:52 -04:00
|
|
|
if (*end != '\0') { // associativity can not be zero
|
2016-04-28 12:01:30 -04:00
|
|
|
printf("Invalid argument \"%s\" for option -%c\n", arg, opt);
|
|
|
|
bad_usage_error();
|
|
|
|
}
|
|
|
|
else if (i < 0) {
|
2016-04-29 17:17:52 -04:00
|
|
|
printf("Argument %s for option -%c too small (must be greater than 0)", arg, opt);
|
2016-04-28 12:01:30 -04:00
|
|
|
exit(1);
|
|
|
|
}
|
2016-04-29 17:17:52 -04:00
|
|
|
else if (opt == 'E') {
|
|
|
|
long long_max = (~0UL)>>1;
|
|
|
|
if (i==0) {
|
|
|
|
printf("Argument for option -E can not be 0\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
else if (i == long_max) {
|
|
|
|
printf("Argument %s for option -E too large (must be less than %ld)\n", arg, long_max);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2016-04-28 12:01:30 -04:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Calculates a size using a given number of bits.
|
|
|
|
@param bits The power of 2 to use.
|
2016-04-29 10:44:40 -04:00
|
|
|
@return 2^bits if it does not overflow.
|
|
|
|
@note Exits if number of bits is larger than a signed int.
|
2016-04-28 12:01:30 -04:00
|
|
|
*/
|
|
|
|
long bits_to_size(int bits)
|
|
|
|
{
|
2016-04-29 10:44:40 -04:00
|
|
|
int max_shift = 8*sizeof(int)-1;
|
2016-04-28 12:01:30 -04:00
|
|
|
if (bits > max_shift-1) {
|
2016-04-29 10:48:55 -04:00
|
|
|
printf("Argument %d too large (-s and -b must be less than %d)", bits, max_shift);
|
2016-04-28 12:01:30 -04:00
|
|
|
exit(1);
|
|
|
|
}
|
2016-04-29 10:44:40 -04:00
|
|
|
return (long)1 << bits;
|
2016-04-28 12:01:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***************************** SUB-MAIN FUNCTIONS *****************************/
|
|
|
|
|
2016-04-29 10:44:40 -04:00
|
|
|
/**
|
|
|
|
Increments the cgiven ache operation counters based on the given result code.
|
|
|
|
@param result The cache operation result code to parse (H, M, or E).
|
|
|
|
@param score The score struct to increment.
|
|
|
|
@TODO Fix this into a total of 3 conditionals.
|
|
|
|
*/
|
2016-04-28 01:40:54 -04:00
|
|
|
void increment_result_counters(char result, results* score)
|
|
|
|
{
|
2016-04-29 17:46:48 -04:00
|
|
|
if (result == 'H') {
|
|
|
|
score->hits++;
|
|
|
|
mprintf(" hit");
|
|
|
|
}
|
|
|
|
else if (result == 'M') {
|
|
|
|
score->misses++;
|
|
|
|
mprintf(" miss");
|
|
|
|
}
|
|
|
|
else if (result == 'E') {
|
|
|
|
score->misses++;
|
|
|
|
score->evictions++;
|
|
|
|
mprintf(" miss eviction");
|
|
|
|
}
|
2016-04-28 01:40:54 -04:00
|
|
|
}
|
2016-04-27 21:24:53 -04:00
|
|
|
|
|
|
|
|
2016-04-28 12:01:30 -04:00
|
|
|
|
|
|
|
/********************* CACHE CREATION/DELETION FUNCTIONS *********************/
|
|
|
|
|
2016-04-29 10:44:40 -04:00
|
|
|
/**
|
|
|
|
@param num_sets The number of sets in the cache.
|
|
|
|
@param num_lines The number of lines in each set.
|
|
|
|
@return A pointer to the allocated cache's set list.
|
|
|
|
@TODO Handle cases where not enough space can be allocated.
|
|
|
|
*/
|
2016-04-28 01:46:49 -04:00
|
|
|
linked_line** make_cache(long num_sets, long num_lines)
|
2016-04-27 16:13:27 -04:00
|
|
|
{
|
2016-04-28 01:46:49 -04:00
|
|
|
linked_line** cache = (linked_line**)calloc(num_sets, sizeof(linked_line*));
|
2016-04-29 17:46:48 -04:00
|
|
|
if (! cache) {
|
|
|
|
printf("Error: calloc failed\n");
|
|
|
|
exit(2);
|
|
|
|
}
|
2016-04-27 21:24:53 -04:00
|
|
|
long ii;
|
2016-04-28 01:46:49 -04:00
|
|
|
for (ii = 0; ii < num_sets; ii++) {
|
2016-04-27 21:24:53 -04:00
|
|
|
cache[ii] = make_set(NULL, num_lines);
|
2016-04-27 18:18:30 -04:00
|
|
|
}
|
2016-04-27 16:13:27 -04:00
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
2016-04-27 21:24:53 -04:00
|
|
|
/**
|
|
|
|
Creates a set by creating and linking a given number of lines.
|
|
|
|
@param newer A pointer to the previously-created element of the set.
|
|
|
|
@param num_lines The number of lines remaining to be added.
|
|
|
|
@return A pointer to the created line.
|
2016-04-29 17:46:48 -04:00
|
|
|
@bug Large values for num_lines result in a segfault in the call to calloc.
|
|
|
|
@TODO Free already-allocated space in event of allocation failure.
|
2016-04-27 21:24:53 -04:00
|
|
|
*/
|
|
|
|
linked_line* make_set(linked_line* newer, long num_lines) {
|
|
|
|
linked_line* current = (linked_line*)calloc(1, sizeof(linked_line));
|
2016-04-29 17:46:48 -04:00
|
|
|
if (! current) {
|
|
|
|
printf("Error: calloc failed\n");
|
|
|
|
exit(2);
|
|
|
|
}
|
2016-04-27 21:24:53 -04:00
|
|
|
current->newer = newer;
|
2016-04-27 22:16:01 -04:00
|
|
|
num_lines--;
|
2016-04-27 21:24:53 -04:00
|
|
|
if (num_lines) { // If not on the last line
|
2016-04-27 22:16:01 -04:00
|
|
|
current->older = make_set(current, num_lines);
|
2016-04-27 21:24:53 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
current->older = NULL;
|
|
|
|
}
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
2016-04-29 10:44:40 -04:00
|
|
|
/**
|
|
|
|
@param cache A pointer to the cache to free.
|
|
|
|
@param num_sets The number of sets in the cache.
|
|
|
|
*/
|
2016-04-27 21:24:53 -04:00
|
|
|
void free_cache(linked_line** cache, long num_sets)
|
2016-04-27 16:13:27 -04:00
|
|
|
{
|
|
|
|
int ii;
|
2016-04-27 21:24:53 -04:00
|
|
|
for (ii = 0; ii < num_sets; ii++) {
|
|
|
|
free_set(cache[ii]);
|
2016-04-27 18:18:30 -04:00
|
|
|
}
|
2016-04-27 16:13:27 -04:00
|
|
|
free(cache);
|
|
|
|
}
|
2016-04-27 12:03:51 -04:00
|
|
|
|
2016-04-27 21:24:53 -04:00
|
|
|
/**
|
|
|
|
Frees a set by recursing through it, then freeing each element from oldest to newest.
|
|
|
|
@param line A pointer to the next line in the set.
|
|
|
|
@param num_lines The number
|
|
|
|
*/
|
|
|
|
void free_set(linked_line* line) {
|
|
|
|
if (line != NULL) {
|
|
|
|
free_set(line->older);
|
|
|
|
free(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-28 12:01:30 -04:00
|
|
|
/************************** CACHE VIEWING FUNCTIONS **************************/
|
|
|
|
|
2016-04-29 10:44:40 -04:00
|
|
|
/**
|
|
|
|
@cache The cache to print.
|
|
|
|
@cache num_sets The number of sets in the cache.
|
|
|
|
*/
|
2016-04-27 22:16:16 -04:00
|
|
|
void print_cache(linked_line** cache, long num_sets)
|
|
|
|
{
|
|
|
|
int ii;
|
|
|
|
for (ii = 0; ii < num_sets; ii++) {
|
|
|
|
print_set(cache[ii]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 10:44:40 -04:00
|
|
|
/**
|
|
|
|
Prints a cache line and all lines modified less recently than it in its set.<br />
|
|
|
|
The format is: <br />
|
|
|
|
address validity.tag address of newer:address of older
|
|
|
|
@param line The set/line to print, or NULL. If null, does nothing.
|
|
|
|
*/
|
2016-04-27 22:16:16 -04:00
|
|
|
void print_set(linked_line* line)
|
|
|
|
{
|
|
|
|
if (line != NULL) {
|
|
|
|
print_set(line->older);
|
2016-04-28 12:01:30 -04:00
|
|
|
printf("%p %x.%lx\t\t%p:%p\n", (void *) line, line->validity,
|
|
|
|
line->tag, (void *) line->newer, (void *) line->older);
|
2016-04-27 22:16:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-28 12:01:30 -04:00
|
|
|
|
|
|
|
|
|
|
|
/*************************** CACHE ACCESS FUNCTIONS ***************************/
|
|
|
|
|
2016-04-29 10:44:40 -04:00
|
|
|
/**
|
|
|
|
Simulates a cache access operation.
|
|
|
|
@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.
|
2016-04-29 10:48:55 -04:00
|
|
|
@param block_offset_bits The number of block offset bits in the address.
|
2016-04-29 10:44:40 -04:00
|
|
|
@param num_lines The number of lines per set.
|
|
|
|
@return 'H' if a hit, 'M' if a miss, 'E' if a miss and eviction
|
|
|
|
|
|
|
|
@TODO Pass set and tag into cache_access instead of an address and sizes.
|
|
|
|
@TODO Make the tag and set retrieval a function?
|
|
|
|
@TODO Make the re-linking of the set into its own function.
|
|
|
|
*/
|
2016-04-28 12:01:30 -04:00
|
|
|
char cache_access(linked_line** cache, long address,
|
2016-04-29 10:48:55 -04:00
|
|
|
int set_bits, int block_offset_bits, long num_lines)
|
2016-04-28 01:43:56 -04:00
|
|
|
{
|
|
|
|
char out;
|
2016-04-29 10:48:55 -04:00
|
|
|
long tag = address >> set_bits >> block_offset_bits;
|
|
|
|
long set = (address >> block_offset_bits) & ((1 << set_bits) - 1);
|
2016-04-28 01:43:56 -04:00
|
|
|
linked_line* set_head = cache[set];
|
|
|
|
linked_line* line = find_line(set_head, tag);
|
|
|
|
|
2016-04-29 17:46:48 -04:00
|
|
|
if (!line->validity) {
|
2016-04-29 17:57:02 -04:00
|
|
|
operation = 'M';
|
2016-04-29 17:46:48 -04:00
|
|
|
}
|
|
|
|
else if (line->tag == tag) {
|
2016-04-29 17:57:02 -04:00
|
|
|
operation = 'H';
|
2016-04-29 17:46:48 -04:00
|
|
|
}
|
|
|
|
else {
|
2016-04-29 17:57:02 -04:00
|
|
|
operation = 'E';
|
2016-04-28 01:43:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
line->tag = tag;
|
|
|
|
line->validity = 1;
|
|
|
|
|
2016-04-29 17:57:02 -04:00
|
|
|
cache[set] = update_set(set_head, line);
|
|
|
|
|
|
|
|
return operation;
|
2016-04-28 01:43:56 -04:00
|
|
|
}
|
|
|
|
|
2016-04-28 01:39:57 -04:00
|
|
|
/**
|
2016-04-29 10:44:40 -04:00
|
|
|
Finds a line in the given set that holds the given tag, or the least-recently
|
|
|
|
used line if the tag is not in the set, or the next invalid line if not all
|
|
|
|
of the lines have been used.
|
|
|
|
@param line Initially, the most-recently-used line in the set. In the
|
|
|
|
recursion, iterates through the lines in the set.
|
|
|
|
@returns The first line with a matching tag, the first invalid line,
|
|
|
|
or the first line without an older (i.e. the end of the set).
|
2016-04-28 01:39:57 -04:00
|
|
|
*/
|
|
|
|
linked_line* find_line(linked_line* line, long tag)
|
|
|
|
{
|
2016-04-29 17:46:48 -04:00
|
|
|
if (line->tag == tag || !line->validity || !line->older) {
|
2016-04-28 01:39:57 -04:00
|
|
|
return line;
|
2016-04-29 17:46:48 -04:00
|
|
|
}
|
|
|
|
else {
|
2016-04-28 01:39:57 -04:00
|
|
|
return find_line(line->older, tag);
|
2016-04-29 17:46:48 -04:00
|
|
|
}
|
2016-04-28 01:39:57 -04:00
|
|
|
}
|
|
|
|
|
2016-04-29 17:57:02 -04:00
|
|
|
/**
|
|
|
|
Updates the use order of a set by moving the line being accessed in front of
|
|
|
|
the previously-accessed line and linking the neighbors of the new head.
|
|
|
|
@param old_head The previous most-recently modified line of the set.
|
|
|
|
@param new_head The line of the set being accessed.
|
|
|
|
@return new_head
|
|
|
|
*/
|
|
|
|
linked_line* update_set(linked_line* old_head, linked_line* new_head) {
|
|
|
|
if(!(new_head == old_head)) {
|
|
|
|
new_head->newer->older = new_head->older;
|
|
|
|
if(new_head->older) {
|
|
|
|
new_head->older->newer = new_head->newer;
|
|
|
|
}
|
|
|
|
new_head->newer = NULL;
|
|
|
|
new_head->older = old_head;
|
|
|
|
old_head->newer = new_head;
|
|
|
|
}
|
|
|
|
return new_head;
|
|
|
|
}
|
|
|
|
|
2016-04-27 13:41:51 -04:00
|
|
|
|