From 25e9b0edf8d4d32989ec535b790bd97bd063314d Mon Sep 17 00:00:00 2001 From: Rushil Umaretiya Date: Wed, 10 Apr 2024 19:24:27 -0400 Subject: [PATCH] task1 for shits and giggles --- cache.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/cache.c b/cache.c index aa16e9a..b89b2e8 100644 --- a/cache.c +++ b/cache.c @@ -42,8 +42,6 @@ int initialize_cache(unsigned int number_of_lines) { } 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; @@ -52,6 +50,15 @@ int cread(unsigned int cmf, unsigned int* hex_addr, bool* hit, bool* replace) { // hex_addr unsigned int line, tag, block_offset; + if (cmf == DM) { + line = bit_select(*hex_addr, addr_bits - NUM_LINE_BITS, addr_bits - 1); + } else { + line = 0; + } + + tag = bit_select(*hex_addr, 0, addr_bits - NUM_LINE_BITS - 1); + block_offset = bit_select(*hex_addr, 0, NUM_BLOCK_OFFSET_BITS - 1); + // (Optional) Indicates a line that is open and thus usable int open_line; // (Optional) For DM, indicates the cache line that should be replaced @@ -69,7 +76,21 @@ int cread(unsigned int cmf, unsigned int* hex_addr, bool* hit, bool* replace) { *replace = false; if ((cmf == DM) && ((*hex_addr) < exp2(addr_bits))) { - // TODO: Part 1 + if (cache[line]->tag == tag) { + // hit + *hit = true; + cache[line]->hit_count++; + ret_val = cache[line]->block[block_offset]; + } else { + // miss + *replace = true; + cache[line]->tag = tag; + cache[line]->hit_count = 1; + cache[line]->block[block_offset] = + phy_memory[block_location[*hex_addr]]; + ret_val = cache[line]->block[block_offset]; + } + } else if ((cmf == FA) && ((*hex_addr) < exp2(addr_bits))) { // TODO: Part 2 }