diff options
Diffstat (limited to 'components/ram.py')
| -rw-r--r-- | components/ram.py | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/components/ram.py b/components/ram.py new file mode 100644 index 0000000..c6d05bb --- /dev/null +++ b/components/ram.py @@ -0,0 +1,66 @@ +from components.ijvm import ijvm + +class Ram: + + def __init__(self,components,size): + self.data=dict() + self.lastAddr=size-1 + self.c=components + + def loadRamFile(self,filepath): + data=dict() + addr=0 + f=open(filepath,"r") + for line in f.readlines(): + line=line.rstrip() # remove \n + if line in ijvm: + data[addr]=int(ijvm[line]) + else: + data[addr]=int(line,0) + addr+=1 + f.close() + self.data=data + + def write(self): + addr=self.c["MAR"] + if addr>self.lastAddr: + raise ValueError("You get out of the ram by trying to set a value at address {}, max address is {}".format(addr,self.lastAddr)) + self.data[addr]=self.c["MDR"] + + def read(self): + addr=self.c["MAR"] + value=None + try: + value=self.data[addr] + except: + if addr>self.lastAddr: + raise ValueError("You get out of the ram by trying to get value at address {}, max address is {}".format(addr,self.lastAddr)) + if(value==None): + return(0) + return(value) + + def fetch(self): + addr=self.c["PC"] + value=None + try: + value=self.data[addr] + except: + if addr>self.lastAddr: + raise ValueError("You get out of the ram by trying to get value at address {}, max address is {}".format(addr,self.lastAddr)) + if(value==None): + return(0) + return(value) + + def dump(self): + print("------- RAM --------") + for key,value in self.data.items(): + #print("{}:{}".format(key,bin(value)[2:])) + print("{}:{}".format(key,value)) + print("--------------------") + + def dump(self,start,end): + for i in range(start,end+1): + try: + print("{}:{}".format(i,self.data[i])) + except: + print("{}:0".format(i)) |
