From 69c47204ae5d25cfbcc5eb2c7cee15a837cb0bbf Mon Sep 17 00:00:00 2001 From: Rushil Umaretiya Date: Wed, 10 Apr 2024 20:30:50 -0400 Subject: [PATCH] task1 done --- cache.c | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/cache.c b/cache.c index b89b2e8..0340454 100644 --- a/cache.c +++ b/cache.c @@ -50,15 +50,6 @@ 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 @@ -76,6 +67,10 @@ int cread(unsigned int cmf, unsigned int* hex_addr, bool* hit, bool* replace) { *replace = false; if ((cmf == DM) && ((*hex_addr) < exp2(addr_bits))) { + tag = bit_select(*hex_addr, addr_bits - 1, addr_bits - NUM_TAG_BITS_DM); + line = bit_select(*hex_addr, addr_bits - NUM_TAG_BITS_DM - 1, NUM_BLOCK_OFFSET_BITS); + block_offset = bit_select(*hex_addr, NUM_BLOCK_OFFSET_BITS - 1, 0); + if (cache[line]->tag == tag) { // hit *hit = true; @@ -86,8 +81,11 @@ int cread(unsigned int cmf, unsigned int* hex_addr, bool* hit, bool* replace) { *replace = true; cache[line]->tag = tag; cache[line]->hit_count = 1; - cache[line]->block[block_offset] = - phy_memory[block_location[*hex_addr]]; + + unsigned int mem_index = block_location[*hex_addr >> NUM_BLOCK_OFFSET_BITS]; + + memcpy(cache[line]->block, phy_memory + mem_index, sizeof(unsigned int) * (int)exp2(NUM_BLOCK_OFFSET_BITS)); + ret_val = cache[line]->block[block_offset]; } @@ -101,6 +99,17 @@ int cread(unsigned int cmf, unsigned int* hex_addr, bool* hit, bool* replace) { return ret_val; } +void print_binary(unsigned char num) { + // Start from the most significant bit (MSB) and move to the least significant bit (LSB) + for (int i = 7; i >= 0; i--) { + // Use bitwise AND to isolate the specific bit and shift right to get 1 or 0 + int bit = (num >> i) & 1; + printf("%d", bit); + } + printf("\n"); // Newline after printing the binary number +} + + void cprint() { unsigned int line;