Last active
January 31, 2025 13:46
-
-
Save adielBm/8d35e3710bb71719eb38df5ea201093f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def cache_block_range(word_addresses, m, n, tag_width, address_width, K): | |
block_size = 2 ** m | |
index_mask = (1 << n) - 1 | |
tag_mask = (1 << tag_width) - 1 | |
# Cache: Dictionary of lists for each set (index), each with up to K ways | |
cache = {i: [{'tag': None} for _ in range(K)] for i in range(1 << n)} | |
# LRU tracking: LRU[index] keeps track of LRU order for K ways in that index | |
LRU = [[j for j in range(K)] for _ in range(1 << n)] | |
mv_count, mt_count, hit_count = 0, 0, 0 | |
print(f"{'Address':<10}{'Miss/Hit':<20}{'Index':<10}{'Tag':<10}{'Way':<10}{'Data (word)':<25}") | |
print("=" * 80) | |
for x in word_addresses: | |
base_address = (x // block_size) * block_size | |
ending_address = base_address + block_size - 1 | |
index = (x // block_size) & index_mask | |
tag = (x // block_size) >> n & tag_mask | |
data_range = f"Mem[{base_address}, {ending_address}]" | |
hit = False | |
way = -1 | |
# Check if tag is in any way for this index (hit detection) | |
for i in range(K): | |
if cache[index][i]['tag'] == tag: | |
hit = True | |
way = i | |
break | |
if hit: | |
miss_hit = "Hit" | |
hit_count += 1 | |
else: | |
# Cache miss - Check for an empty way first (valid miss) | |
empty_way = next((i for i in range(K) if cache[index][i]['tag'] is None), None) | |
if empty_way is not None: # Space available | |
miss_hit = "Miss-Valid" | |
mv_count += 1 | |
way = empty_way | |
else: # No space, evict LRU block | |
miss_hit = "Miss-Tag" | |
mt_count += 1 | |
way = LRU[index].index(0) # Find way with LRU=0 | |
# Store new block in the selected way | |
cache[index][way]['tag'] = tag | |
# Update LRU order for this index | |
accessed_LRU = LRU[index][way] # Get current LRU position of the way | |
for i in range(K): | |
if LRU[index][i] > accessed_LRU: | |
LRU[index][i] -= 1 # Shift older entries down | |
LRU[index][way] = K - 1 # Set accessed way as most recently used | |
print(f"{x:<10}{miss_hit:<20}{index:<10}{tag:<10}{way:<10}{data_range:<25}") | |
print(f"Total Miss-Valid: {mv_count}") | |
print(f"Total Miss-Tag: {mt_count}") | |
print(f"Total Hits: {hit_count}") | |
print(f"Hit Rate: {hit_count / len(word_addresses) * 100:.2f}%") | |
# Example Q9b | |
# Example usage: | |
word_addresses = [12,33,67,15,73,56,89,13,68,44,46,60,63,45,54,8,79] | |
m = 3 | |
n = 0 | |
tag_width = 23 | |
address_width = 28 | |
K = 4 | |
cache_block_range(word_addresses, m, n, tag_width, address_width, K) | |
print("\n\n\n") | |
# Example Q9c | |
word_addresses = [12,33,67,15,73,56,89,13,68,44,46,60,63,45,54,8,79] | |
m = 1 | |
n = 3 | |
tag_width = 22 | |
address_width = 28 | |
K = 2 | |
cache_block_range(word_addresses, m, n, tag_width, address_width, K) |
Author
adielBm
commented
Jan 31, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment