// PID: 9DigitPidNoSpacesOrDashes // I pledge the COMP 211 honor code. #include #include #include #include "bit_utils.h" #include "cache.h" #include "memory.h" /** * Global variables. The extern ones are initialized in memory.c and are * accessible via the extern keyword */ extern unsigned int* phy_memory; extern unsigned int* block_location; extern unsigned int addr_bits; cache_line** cache; int initialize_cache(unsigned int number_of_lines) { unsigned int line; // Allocate memory for the cache (array of cache lines). cache = malloc(sizeof(cache_line*) * number_of_lines); // For each cache line, allocate memory and initialize the cache line. if (cache != NULL) { for (line = 0; line < number_of_lines; line++) { cache[line] = (cache_line*)malloc(sizeof(cache_line)); cache[line]->tag = EMPTY; cache[line]->hit_count = 0; cache[line]->block = malloc(sizeof(unsigned int) * (int)exp2(NUM_BLOCK_OFFSET_BITS)); } } else { return FAIL; } return OK; } int cread(unsigned int cmf, unsigned int* hex_addr, bool* hit, bool* replace) { // TODO: // Either the value at the requested hexadecimal address or FAIL int ret_val = FAIL; // Initialize these variables by // extracting the line (for DM, not FA), tag, and block offset bits from // hex_addr unsigned int line, tag, block_offset; // (Optional) Indicates a line that is open and thus usable int open_line; // (Optional) For DM, indicates the cache line that should be replaced int replace_line; // For FA, keeps track of LFU cache line int min_hit_cnt = (int)1E10; // For FA, keeps track of index of LFU cache line int min_line; // Variables that are passed by reference // Must indicate to the driver code whether there was a cache hit and // whether we had to overwrite and replace something *hit = false; *replace = false; if ((cmf == DM) && ((*hex_addr) < exp2(addr_bits))) { // TODO: Part 1 } else if ((cmf == FA) && ((*hex_addr) < exp2(addr_bits))) { // TODO: Part 2 } // Print state of cache after mapping algorithm is applied cprint(); return ret_val; } void cprint() { unsigned int line; printf("\n---------------------------------------------\n"); printf("line\ttag\tnum of hits\n"); printf("---------------------------------------------\n"); for (line = 0; line < NUM_LINES; line++) { if (cache[line]->tag == EMPTY) { printf("%d\t%d\t%d\n", line, cache[line]->tag, cache[line]->hit_count); } else { printf("%d\t%02X\t%d\n", line, cache[line]->tag, cache[line]->hit_count); } } }