mirror of
https://github.com/Comp211-SP24/lab-05-Rushilwiz.git
synced 2025-04-09 14:50:19 -04:00
task1 done
This commit is contained in:
parent
25e9b0edf8
commit
69c47204ae
31
cache.c
31
cache.c
|
@ -50,15 +50,6 @@ int cread(unsigned int cmf, unsigned int* hex_addr, bool* hit, bool* replace) {
|
||||||
// hex_addr
|
// hex_addr
|
||||||
unsigned int line, tag, block_offset;
|
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
|
// (Optional) Indicates a line that is open and thus usable
|
||||||
int open_line;
|
int open_line;
|
||||||
// (Optional) For DM, indicates the cache line that should be replaced
|
// (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;
|
*replace = false;
|
||||||
|
|
||||||
if ((cmf == DM) && ((*hex_addr) < exp2(addr_bits))) {
|
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) {
|
if (cache[line]->tag == tag) {
|
||||||
// hit
|
// hit
|
||||||
*hit = true;
|
*hit = true;
|
||||||
|
@ -86,8 +81,11 @@ int cread(unsigned int cmf, unsigned int* hex_addr, bool* hit, bool* replace) {
|
||||||
*replace = true;
|
*replace = true;
|
||||||
cache[line]->tag = tag;
|
cache[line]->tag = tag;
|
||||||
cache[line]->hit_count = 1;
|
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];
|
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;
|
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() {
|
void cprint() {
|
||||||
unsigned int line;
|
unsigned int line;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user