aboutsummaryrefslogtreecommitdiff
path: root/components/ram.py
diff options
context:
space:
mode:
Diffstat (limited to 'components/ram.py')
-rw-r--r--components/ram.py94
1 files changed, 0 insertions, 94 deletions
diff --git a/components/ram.py b/components/ram.py
deleted file mode 100644
index dd99ada..0000000
--- a/components/ram.py
+++ /dev/null
@@ -1,94 +0,0 @@
-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):
- """
- Load a Ram file into self.data
- """
- 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:
- try:
- value=int(line,0)
- except:
- raise ValueError("Invalide RAM entry: Address {} value {}".format(addr,line))
- if value>255:
- raise ValueError("Ram contain values that does not fit in a byte: value {} at address {}".format(value,addr))
- data[addr]=value
- addr+=1
- f.close()
- self.data=data
-
- def write(self):
- """
- Write data to memory based Mic-1 architecture
- """
- addr=self.c["MAR"]*4 # Don't forget MAR address 32bits block of memory
- 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))
- #### Little endian ####
- self.data[addr]=self.c["MDR"] & 0xFF
- self.data[addr+1]=self.c["MDR"] & 0xFF00
- self.data[addr+2]=self.c["MDR"] & 0xFF0000
- self.data[addr+3]=self.c["MDR"] & 0xFF000000
-
-
- def read(self):
- """
- Read data from memory based Mic-1 architecture
- """
- addr=self.c["MAR"]*4 # Don't forget MAR address 32bits block of memory
- value=None
- try:
- #### Little endian ####
- value=(self.data[addr+3]<<24)|(self.data[addr+2]<<16)|(self.data[addr+1]<<8)|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):
- """
- Fetch next byte from memory based Mic-1 architecture
- """
- 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):
- """
- Simple dump helper
- """
- for key,value in self.data.items():
- #print("{}:{}".format(key,bin(value)[2:]))
- print("{}:{}".format(key,value))
-
- def dumpRange(self,start,end,step):
- """
- Another dump helper
- """
- for i in range(start,end+1,step):
- try:
- print("{}:{}".format(i,self.data[i]))
- except:
- print("{}:0".format(i))