Jarod Cavalcante
2024-11-12T02:24:52Z

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]}")