Skip to content

Instantly share code, notes, and snippets.

@jarodsim
Created November 12, 2024 02:24
Show Gist options
  • Save jarodsim/dba8e151e0d6e8216cc83424540b4021 to your computer and use it in GitHub Desktop.
Save jarodsim/dba8e151e0d6e8216cc83424540b4021 to your computer and use it in GitHub Desktop.
cache_simulation_block_size_2.py
import math
def simulate_cache(addresses, cache_size, block_size):
block_size_bits = block_size * 32
index_bits = int(math.log2(cache_size))
cache = {}
results = []
for address in addresses:
binary_address = bin(address)[2:].zfill(32)
tag = binary_address[:-index_bits]
index = binary_address[-index_bits:]
if index in cache and cache[index] == tag:
hit = "Acerto"
else:
hit = "Falha"
cache[index] = tag
results.append((address, tag, index, hit))
return results
addresses = [3, 180, 43, 2, 191, 88, 190, 14, 181, 44, 186, 253]
cache_size = 8
block_size = 2
results = simulate_cache(addresses, cache_size, block_size)
for result in results:
print(f"{result[0]}, {result[1]}, {result[2]}, {result[3]}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment