# cache.py - Cache simulation # Set up the cache, read in the list of dynamic memory addresses, # run the simulation, and then print out the final results. # Initialize global variables linesInCache = 8 wordsPerLine = 4 numHits = 0 numMisses = 0 fetchNumber = 0 EMPTY = -1 tagBits = 0 lineBits = 0 wordBits = 0 # *** You need to implement this function def calcBits(numLines, numWords): tagBits = 0 lineBits = 0 wordBits = 0 return (tagBits, lineBits, wordBits) # Main program - start by initializing the cache. tag = [] contents = [] for i in range(0, linesInCache): tag.append(EMPTY) contents.append([]) for j in range(0, wordsPerLine): contents[i].append(EMPTY) (tagBits, lineBits, wordBits) = calcBits(linesInCache, wordsPerLine) print("tag bits = " + str(tagBits)) print("line bits = " + str(lineBits)) print("word bits = " + str(wordBits)) # Next, read in the input consisting of memory addresses. # We want to store the address as integers, and their input # format is hexadecimal. list = [] fileName = input("What is the name of the input file? ") inFile = open(fileName, "r") for line in inFile: list.append(int(line, 16)) inFile.close() # Run the simulation. # Simulate the cache on a list of address references. # Keep track of the number of hits and misses, as well as the # current state of the cache (tag + contents). We should print # the cache contents every time it changes (i.e. on a miss). for address in list: fetchNumber += 1 print("fetch # " + str(fetchNumber)) # Now it's time to play cache! Find the tagNum, lineNum, wordNum, # see if it's in cache, and if not then put the whole line in. # Remember to keep track of the hits and misses. tagNum = 0 lineNum = 0 wordNum = 0 # *** You need to add code here. # Print out state of the cache when there is a miss. if True: print(" " * (17 + 10*wordsPerLine) + "tags") for i in range (0, linesInCache): print("line {0:2d}:".format(i), end = "") for j in range(0, wordsPerLine): if contents[i][j] == EMPTY: print(" ...", end = "") else: print("{0:10d}".format(contents[i][j]), end = "") if tag[i] == EMPTY: print(" ...") else: print("{0:12d}".format(tag[i])) # Print the final results print("Number of hits = " + str(numHits)) print("Number of misses = " + str(numMisses)) print("miss rate = " + str(100.0 * numMisses / (numMisses + numHits)) + "%")