diff --git a/main.py b/main.py index c0e997458a0a622721e453d3217b8994bc4f292e..b89cb9ddf53504a2053dfe1304ae742f52e10dee 100644 --- a/main.py +++ b/main.py @@ -66,7 +66,7 @@ def main(set_identifier_bit_count, line_count, block_identifier_bit_count, file_ # print('S: {0}'.format(S)) # print('E: {0}'.format(E)) # print('Tag Bits: {0}'.format(tag_identifier_bit_count)) - print('Cache: {0}'.format(cache)) + print_cache(cache) print('\n\n') run_simulation(cache, file_name, M, B, S) @@ -88,8 +88,7 @@ def run_simulation(cache, file_name, M, B, S): for line in file: # print('Line: {0}'.format(line)) cache, hits, misses, evictions = handle_line(cache, line, M, B, S, hits, misses, evictions) - # print('Cache: {0}'.format(cache)) - # print('') + # print_cache(cache) # print('M: {0} As Bits: {1:b}'.format(M, M - 1)) # print('B: {0} As Bits: {1:b}'.format(B, B - 1)) @@ -98,7 +97,7 @@ def run_simulation(cache, file_name, M, B, S): print('hits:{0} misses:{1} evictions:{2}'.format(hits, misses, evictions)) -def handle_line(cache, line, M, B, S, hits, misses, evictions): +def handle_line(cache, line, M, B, S, hits, misses, evictions, print_results=True): """ :param cache: @@ -157,42 +156,87 @@ def handle_line(cache, line, M, B, S, hits, misses, evictions): # Check if match. if cache[set][index]['valid'] == True and cache[set][index]['tag'] == tag: # Match found. - print('{0} hit'.format(line)) + if print_results: + print('{0} hit'.format(line)) + hits += 1 return (cache, hits, misses, evictions) else: # Not a match. Examine why. if cache[set][index]['valid'] == False: # Cache location was not set. Cold miss. We can just set and return. - print('{0} miss'.format(line)) + if print_results: + print('{0} miss'.format(line)) + misses += 1 cache[set][index]['valid'] = True cache[set][index]['tag'] = tag # Set block values that are "loaded" into memory. - offset = int(block_offset * (address / block_offset)) + lower_offset = int(address / B) * B + upper_offset = (int(address / B) + 1) * B + value_offset = address + value_size cache[set][index]['block'] = [] for i in range(B): - cache[set][index]['block'].append(offset + i) + cache[set][index]['block'].append(lower_offset + i) + + # Check if size sets address outside of current cache block. + if value_offset >= upper_offset: + # Size sets address outside of current cache block. Update additional blocks as needed. + # Start by getting our new size. + new_size = value_offset - upper_offset + if new_size < 0: + new_size = 0 + + # Pass our new line data. + new_line = ' L {0},{1}'.format(upper_offset, new_size) + cache, null, null, null = handle_line(cache, new_line, M, B, S, 0, 0, 0, False) return (cache, hits, misses, evictions) # If we made it this far, cache locations were set, but tags did not match. Conflict miss. - print('{0} miss eviction'.format(line)) + if print_results: + print('{0} miss eviction'.format(line)) + misses += 1 evictions += 1 cache[set][0]['tag'] = tag # Set block values that are "loaded" into memory. - offset = int(block_offset * (address / block_offset)) + lower_offset = int(address / B) * B + upper_offset = (int(address / B) + 1) * B + value_offset = address + value_size cache[set][0]['block'] = [] for i in range(B): - cache[set][0]['block'].append(offset + i) + cache[set][0]['block'].append(lower_offset + i) + + # Check if size sets address outside of current cache block. + if value_offset >= upper_offset: + # Size sets address outside of current cache block. Update additional blocks as needed. + # Start by getting our new size. + new_size = value_offset - upper_offset + if new_size < 0: + new_size = 0 + + # Pass our new line data. + new_line = ' L {0},{1}'.format(upper_offset, new_size) + cache, null, null, null = handle_line(cache, new_line, M, B, S, 0, 0, 0, False) return (cache, hits, misses, evictions) +def print_cache(cache): + """ + Prints simulated cache in an easier to read format. + :param cache: Simulated cache to print. + """ + print('\nCache:') + for set_index in range(len(cache)): + print(' Set {0}:'.format(set_index, cache[set_index])) + for line_index in range(len(cache[set_index])): + print(' Line {0}: {1}'.format(line_index, cache[set_index][line_index])) + print('') if __name__ == '__main__':