#!/usr/bin/python3
“””
CS-UY 2214
Jeff Epstein
Starter code for E20 cache simulator
simcache.py
“””
import argparse
def print_cache_config(cache_name, size, assoc, blocksize, num_lines):
“””
Prints out the correctly-formatted configuration of a cache.
cache_name — The name of the cache. “L1” or “L2”
size — The total size of the cache, measured in memory cells.
Excludes metadata
assoc — The associativity of the cache. One of [1,2,4,8,16]
blocksize — The blocksize of the cache. One of [1,2,4,8,16,32,64])
sig: str, int, int, int, int -> NoneType
“””
summary = “Cache %s has size %s, associativity %s, ” \
“blocksize %s, lines %s” % (cache_name,
size, assoc, blocksize, num_lines)
print(summary)
def print_log_entry(cache_name, status, pc, addr, line):
“””
Prints out a correctly-formatted log entry.
cache_name — The name of the cache where the event
occurred. “L1” or “L2”
status — The kind of cache event. “SW”, “HIT”, or
“MISS”
pc — The program counter of the memory
access instruction
addr — The memory address being accessed.
line — The cache line or set number where the data
is stored.
sig: str, str, int, int, int -> NoneType
“””
log_entry = “{event:8s} pc:{pc:5d}\taddr:{addr:5d}\t” \
“line:{line:4d}”.format(line=line, pc=pc, addr=addr,
event = cache_name + ” ” + status)
print(log_entry)
def main():
parser = argparse.ArgumentParser(description=’Simulate E20 cache’)
parser.add_argument(‘filename’, help=
‘The file containing machine code, typically with .bin suffix’)
parser.add_argument(‘–cache’, help=
‘Cache configuration: size,associativity,blocksize (for one cache) ‘
‘or size,associativity,blocksize,size,associativity,blocksize (for two caches)’)
cmdline = parser.parse_args()
if cmdline.cache is not None:
parts = cmdline.cache.split(“,”)
if len(parts) == 3:
[L1size, L1assoc, L1blocksize] = [int(x) for x in parts]
# TODO: execute E20 program and simulate one cache here
elif len(parts) == 6:
[L1size, L1assoc, L1blocksize, L2size, L2assoc, L2blocksize] = \
[int(x) for x in parts]
# TODO: execute E20 program and simulate two caches here
else:
raise Exception(“Invalid cache config”)
if __name__ == “__main__”:
main()
#ra0Eequ6ucie6Jei0koh6phishohm9