-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCache Simulator.py
103 lines (89 loc) · 3.38 KB
/
Cache Simulator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# -*- coding: utf-8 -*-
import math as math
while 0 == 0:
result1 = []
result2 = []
print("Cache model")
print("1. 32KB")
print("2. 16KB + 16KB")
model = input("Choose a model: ")
if int(model) == 1:
cacheSize = 32768
elif int(model) == 2:
cacheSize = 16384
blockSize = input("Please enter the block size: ")
arraySize = int(cacheSize)/int(blockSize)
indexSize = int(math.log(arraySize,2))
if int(model) == 1:
cache = [0]*int(arraySize)
missCount = 0
hitCount = 0
elif int(model) ==2:
cache1 = [0]*int(arraySize)
cache2 = [0]*int(arraySize)
missCount1 = 0
hitCount1 = 0
missCount2 = 0
hitCount2 = 0
print("Reading file")
with open("trace.din") as infile:
for line in infile:
result1.append(line.split(' ')[0])
result2.append(line.split(' ')[1])
infile.closed
if int(model) == 1:
i=0
while i<len(result2):
hexValue = result2[i]
i = i+1
binStr = str(bin(int(hexValue, 16))[2:].zfill(32))
tag = int(binStr[:20],2)
index = int(binStr[20:20+indexSize],2)
modIndex = index%int(arraySize)
if cache[modIndex] == 0:
cache.insert(modIndex, tag)
missCount = missCount + 1
elif cache[modIndex] == tag:
hitCount = hitCount + 1
elif tag != cache[modIndex]:
missCount = missCount + 1
cache.insert(modIndex, tag)
print("Miss Count -- "+ str(missCount))
print("Hit Count -- "+ str(hitCount)+"\n\n")
elif int(model) == 2:
i=0
while i<len(result2):
opCode = result1[i]
opCode = int(opCode)
hexValue = result2[i]
i = i+1
if opCode == 0 or opCode == 1:
binStr = str(bin(int(hexValue, 16))[2:].zfill(32))
tag = int(binStr[:20],2)
index = int(binStr[20:20+indexSize],2)
modIndex = index%int(arraySize)
if cache1[modIndex] == 0:
cache1.insert(modIndex, tag)
missCount1 = missCount1 + 1
elif cache1[modIndex] == tag:
hitCount1 = hitCount1 + 1
elif tag != cache1[modIndex]:
missCount1 = missCount1 + 1
cache1.insert(modIndex, tag)
elif opCode == 2:
binStr = str(bin(int(hexValue, 16))[2:].zfill(32))
tag = int(binStr[:20],2)
index = int(binStr[20:20+indexSize],2)
modIndex = index%int(arraySize)
if cache2[modIndex] == 0:
cache2.insert(modIndex, tag)
missCount2 = missCount2 + 1
elif cache2[modIndex] == tag:
hitCount2 = hitCount2 + 1
elif tag != cache2[modIndex]:
missCount2 = missCount2 + 1
cache2.insert(modIndex, tag)
print("Data Cache Miss Count -- "+ str(missCount1))
print("Data Cache Hit Count -- "+ str(hitCount1))
print("Instructions Cache Miss Count -- "+ str(missCount2))
print("Instructions Cache Hit Count -- "+ str(hitCount2)+"\n\n")