-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache1.py
More file actions
307 lines (255 loc) · 9.08 KB
/
cache1.py
File metadata and controls
307 lines (255 loc) · 9.08 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
'''Brief:
Author-0deadLock0
Contact- guptaabhimanyu23@gmail.com
Functionality- Simulation of single level cache
'''
import math
memoryAddresslength=16
mainMemorySize=2**memoryAddresslength
blocksCount=None
blockSize=None
mainMemory=None
def is_in_power_of_2(num):
return num!=0 and (num&(num-1))==0
def is_valid_address(address,length):
if len(address)!=length:
return False
for a in address:
if a!='0' and a!='1':
return False
return True
def print_main():
print()
for i in range(blocksCount):
print(bin(i)[2:],mainMemory[i])
print()
def print_cache(type,cacheMemory):
print()
if type=='S':
setNumber=0
for sets in cacheMemory:
print(bin(setNumber)[2:],"*"*6*len(sets[0][1]))
setNumber+=1
for cacheLines in sets:
print(cacheLines)
else:
for cacheLines in cacheMemory:
print(cacheLines)
print()
def read_from_cache(type,cacheMemory,address):
if type=='D':
lineSize=int(math.log2(len(cacheMemory)))
blockOffsetSize=int(math.log2(len(cacheMemory[0][1])))
tagSize=memoryAddresslength-lineSize-blockOffsetSize
tag=address[0:tagSize]
line=int(address[tagSize:tagSize+lineSize],2)
blockOffset=int(address[memoryAddresslength-blockOffsetSize:],2)
if cacheMemory[line][0]==tag:
print("Cache Hit")
else:
print("Cache Miss")
print("Bringing block from main memory.....")
oldTag=None
oldBlocks=None
replace=False
if cacheMemory[line][0]!=None:
oldTag=cacheMemory[line][0]
replace=True
oldBlocks=list(cacheMemory[line][1])
cacheMemory[line][0]=tag
cacheMemory[line][1]=list(mainMemory[line+int(tag,2)*len(cacheMemory)])
if replace:
mainMemory[line+int(oldTag,2)*len(cacheMemory)]=list(oldBlocks)
print("Value-",cacheMemory[line][1][blockOffset])
elif type=='F':
blockOffsetSize=int(math.log2(len(cacheMemory[0][1])))
blockNumber=memoryAddresslength-blockOffsetSize
block=address[:blockNumber]
blockOffset=int(address[memoryAddresslength-blockOffsetSize:],2)
line=-1
for i in range(len(cacheMemory)):
if cacheMemory[i][0]==block:
line=i
cacheMemory.insert(0,list(cacheMemory[line]))
del cacheMemory[line+1]
line=0
print("Cache Hit")
break
if line==-1:
print("Cache Miss")
print("Bringing block from main memory.....")
cacheMemory.insert(0,[block,list(mainMemory[int(block,2)])])
if cacheMemory[-1][0]!=None:
mainMemory[int(cacheMemory[-1][0],2)]=list(cacheMemory[-1][1])
del cacheMemory[-1]
line=0
print("Value-",cacheMemory[line][1][blockOffset])
else:
setSize=int(math.log2(len(cacheMemory)))
blockOffsetSize=int(math.log2(len(cacheMemory[0][0][1])))
tagSize=memoryAddresslength-setSize-blockOffsetSize
tag=address[0:tagSize]
sets=int(address[tagSize:tagSize+setSize],2)
blockOffset=int(address[memoryAddresslength-blockOffsetSize:],2)
line=-1
for i in range(len(cacheMemory[sets])):
if cacheMemory[sets][i][0]==tag:
line=i
cacheMemory[sets].insert(0,list(cacheMemory[sets][line]))
del cacheMemory[sets][line+1]
line=0
print("Cache Hit")
break
if line==-1:
print("Cache Miss")
print("Bringing block from main memory.....")
cacheMemory[sets].insert(0,[tag,list(mainMemory[sets+int(tag,2)*len(cacheMemory)])])
if cacheMemory[sets][-1][0]!=None:
mainMemory[sets+int(cacheMemory[sets][-1][0],2)*len(cacheMemory)]=list(cacheMemory[sets][-1][1])
del cacheMemory[sets][-1]
line=0
print("Value-",cacheMemory[sets][line][1][blockOffset])
def write_to_cache(type,cacheMemory,address,value):
if type=='D':
lineSize=int(math.log2(len(cacheMemory)))
blockOffsetSize=int(math.log2(len(cacheMemory[0][1])))
tagSize=memoryAddresslength-lineSize-blockOffsetSize
tag=address[0:tagSize]
line=int(address[tagSize:tagSize+lineSize],2)
blockOffset=int(address[memoryAddresslength-blockOffsetSize:],2)
if cacheMemory[line][0]==tag:
print("Cache Hit")
cacheMemory[line][1][blockOffset]=value
else:
print("Cache Miss")
print("Bringing block from main memory.....")
oldTag=None
oldBlocks=None
replace=False
if cacheMemory[line][0]!=None:
oldTag=cacheMemory[line][0]
replace=True
oldBlocks=list(cacheMemory[line][1])
cacheMemory[line][0]=tag
cacheMemory[line][1]=list(mainMemory[line+int(tag,2)*len(cacheMemory)])
cacheMemory[line][1][blockOffset]=value
if replace:
mainMemory[line+int(oldTag,2)*len(cacheMemory)]=list(oldBlocks)
print("Value updated")
elif type=='F':
blockOffsetSize=int(math.log2(len(cacheMemory[0][1])))
blockNumber=memoryAddresslength-blockOffsetSize
block=address[:blockNumber]
blockOffset=int(address[memoryAddresslength-blockOffsetSize:],2)
line=-1
for i in range(len(cacheMemory)):
if cacheMemory[i][0]==block:
line=i
cacheMemory.insert(0,list(cacheMemory[line]))
del cacheMemory[line+1]
line=0
print("Cache Hit")
break
if line==-1:
print("Cache Miss")
print("Bringing block from main memory.....")
cacheMemory.insert(0,[block,list(mainMemory[int(block,2)])])
if cacheMemory[-1][0]!=None:
mainMemory[int(cacheMemory[-1][0],2)]=list(cacheMemory[-1][1])
del cacheMemory[-1]
line=0
cacheMemory[line][1][blockOffset]=value
print("Value Updated")
else:
setSize=int(math.log2(len(cacheMemory)))
blockOffsetSize=int(math.log2(len(cacheMemory[0][0][1])))
tagSize=memoryAddresslength-setSize-blockOffsetSize
tag=address[0:tagSize]
sets=int(address[tagSize:tagSize+setSize],2)
blockOffset=int(address[memoryAddresslength-blockOffsetSize:],2)
line=-1
for i in range(len(cacheMemory[sets])):
if cacheMemory[sets][i][0]==tag:
line=i
cacheMemory[sets].insert(0,list(cacheMemory[sets][line]))
del cacheMemory[sets][line+1]
line=0
print("Cache Hit")
break
if line==-1:
print("Cache Miss")
print("Bringing block from main memory.....")
cacheMemory[sets].insert(0,[tag,list(mainMemory[sets+int(tag,2)*len(cacheMemory)])])
if cacheMemory[sets][-1][0]!=None:
mainMemory[sets+int(cacheMemory[sets][-1][0],2)*len(cacheMemory)]=list(cacheMemory[sets][-1][1])
del cacheMemory[sets][-1]
line=0
cacheMemory[sets][line][1][blockOffset]=value
print("Value updated")
if __name__=="__main__":
memoryAddresslength=int(input("Enter length of memory address "))
if memoryAddresslength<1:
raise ValueError("Address length should be positive")
mainMemorySize=2**memoryAddresslength
cacheSize=int(input("Enter cache size "))
if not(is_in_power_of_2(cacheSize)):
raise ValueError("Cache Size should be in power of 2")
if cacheSize>mainMemorySize:
raise ValueError("Cache Size should be less than Main Memory Size")
lineSize=int(input("Enter blockSize "))
if not(is_in_power_of_2(lineSize)):
raise ValueError("Block Size should be in power of 2");
cacheLines=cacheSize//lineSize
sets=None
cacheMemory=None
mappingType=input("Choose Mapping Type\nDirect Mapping(D)\tFully Associative Mapping(F)\tSet Associative Mapping(S)\n")
if mappingType=='D' or mappingType=='d':
mappingType='D'
cacheMemory=[[None,[-1 for _ in range(lineSize)]] for _ in range(cacheLines)]
elif mappingType=='F' or mappingType=='F':
mappingType='F'
cacheMemory=[[None,[-1 for _ in range(lineSize)]] for _ in range(cacheLines)]
elif mappingType=='S' or mappingType=='s':
mappingType='S'
sets=int(input("Enter number of sets "))
if not(is_in_power_of_2(sets)):
raise ValueError("Number of Sets should be in power of 2");
if sets>cacheLines:
raise ValueError("Number of sets should be less than or equal to number of cache lines")
cacheMemory=[[[None,[-1 for _ in range(lineSize)]] for _ in range(cacheLines//sets)] for _ in range(sets)]
else:
raise TypeError("Invalid Mapping Type '"+mappingType+"'")
blockSize=lineSize
blocksCount=mainMemorySize//blockSize
mainMemory=[[-1 for _ in range(blockSize)] for _ in range(blocksCount)]
print()
# print("Main-")
# print_main()
print("Cache-")
print_cache(mappingType,cacheMemory)
print()
print("Enter each querry in seperate lines")
print("To view a memory location, just enter the address")
print("To write to a memory location, enter address with value to be written, on the same line")
print("Enter "+str(memoryAddresslength)+" characters long address")
print("Enter 'E' any time to exit the program")
print()
while True:
querry=input()
if querry=="E":
print("Exit initiated")
break
querries=querry.strip().split(" ")
if len(querries)<1 or len(querries)>2:
raise TypeError("Invalid Querry")
if not(is_valid_address(querries[0],memoryAddresslength)):
raise TypeError("Invalid address format '"+querries[0]+"'")
if len(querries)==1:
read_from_cache(mappingType,cacheMemory,querries[0]);
else:
write_to_cache(mappingType,cacheMemory,querries[0],querries[1]);
print()
# print("Main Memory")
# print_main()
print("Cache Memory")
print_cache(mappingType,cacheMemory)