# -*- coding: utf-8 -*- """ Spyder Editor This is a temporary script file. """ import numpy as np okupatu=0 ald=1 tag=2 Ordz=3 blokea=4 class cache1: a= np.zeros((8,5)) hitzt=0 blokt=0 mult=0 huts=0 ziklo=0 ord= 0 helb=0 erag=0 hutse=0 def __init__(self,param1,param2,param3, param4): a= np.zeros((8,5)) hitzt=param1 blokt=param2 mult=param3 ord= param4 def helb_kal(self,helb): emaitza= [0,0,0] emaitza[0]=int(helb//blokt) emaitza[1]=int(emaitza[0] % (8//mult)) emaitza[2]=int(emaitza[0]//(8//mult)) return emaitza def kalkulatu(self,emaitz,eragi): if self.huts(emaitz): print("Hutsegitea") self.hutse=self.hutse+1 print("Atzipen-denbora: cachean bilatzea, 2 -- bloke bat eramatea (M>C,C>M), 20+8") print("T_atz: 30 ziklo") ob.hutsegit(emaitz,ob.nonda(emaitz)) else: print("Atzipen-denbora: cachean bilatzea, 2") print("T_atz: 2 ziklo") if eragi==1: for i in range(mult): if self.a[mult*(emaitz[1])+i][okupatu]==1: if (self.a[mult*(emaitz[1])+i][tag]==emaitz[0]): self.a[mult*(emaitz[1])+i][ald]=1 ob.aktualizatu(emaitz) ob.imprimatu() def huts(self,emaitz): for i in range(mult): if self.a[mult*(emaitz[1])+i][okupatu]==1: if (self.a[(mult*(emaitz[1]))+i][tag]==emaitz[2]): return False return True def aktualizatu(self,datu): nond=0 for i in range(mult): if (self.a[mult*(datu[1])+i][tag]==datu[2]): nond=mult*(datu[1])+i for j in range(mult): if ord==0 and (j+mult*datu[1]!=nond) and not ob.huts(datu): self.a[j+mult*datu[1]][Ordz]=self.a[j+mult*datu[1]][Ordz]+1 if ord==1 and (j+mult*datu[1])!=nond and not ob.huts(datu): if self.a[j+mult*datu[1]][tag]==datu[2]: self.a[j+mult*datu[1]][Ordz]=0 else: self.a[j+mult*datu[1]][Ordz]=self.a[j+mult*datu[1]][Ordz]+1 def hutsegit(self,emaitz,non): self.a[non][okupatu]=1 self.a[non][ald]=0 self.a[non][tag]=emaitz[2] self.a[non][Ordz]=0 self.a[non][blokea]=emaitz[0] def nonda(self,emaitz): libre=False for i in range(mult): if self.a[mult*emaitz[1]+i][okupatu]==0: libre=True break alt=0 if not libre: for i in range(mult): if self.a[mult*emaitz[1]+i][Ordz]>=alt: i=alt print(i) return mult*emaitz[1]+i def imprimatu(self): print("okup ald tag ord || blokea") print("--------------------------------") i=0 kont=0 while (i<8): print(self.a[i]) kont=kont+1 i=i+1 if kont % mult==0: print("--------------------------------") print("--------------------------------") hitzt= int(input("Hitzen tamaina (byteak): 4 - 8 > ")) blokt= int(input("Blokeen tamaina (byteak): 32 - 64 > ")) mult= int(input("Multzoen tam. (blokeak): 1-2-4-8 > ")) ord = int(input("Ordezk.-polit.: 0(FIFO) - 1(LRU) > ")) ob= cache1(hitzt,blokt,mult,ord) helb=0 kont=0 while helb!=-1: helb= int(input("Helbidea (byte) (-1 uzteko) > ")) if helb==-1: break b=ob.helb_kal(helb) erag= int(input("Load (0) / Store (1) > ")) kont+=1 ob.kalkulatu(b,erag) kh=kont-ob.hutse kkh=(kont-ob.hutse)//kont khh=(kont-ob.hutse)*2+ob.hutse*30 print("Mem-atz: ",kont," -- Asmatzeak: ",kh," -- Asmatze-tasa, h = ",kkh," Denbora osoa = ",khh," ziklo")