aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--MicSim/components/ram.py34
1 files changed, 15 insertions, 19 deletions
diff --git a/MicSim/components/ram.py b/MicSim/components/ram.py
index fa2198d..59de862 100644
--- a/MicSim/components/ram.py
+++ b/MicSim/components/ram.py
@@ -34,32 +34,32 @@ class Ram:
"""
Write data to memory based Mic-1 architecture
"""
- addr=self.c["MAR"]
+ addr=self.c["MAR"] # Fetch address
if addr>self.lastAddr or addr<0:
raise ValueError("You get out of the ram by trying to set a value at address {}, max address is {}".format(addr,self.lastAddr))
- if self.c["MDR"] >=2**32:
+ elif self.c["MDR"] >=2**32:
raise ValueError("You try to write a the value {} at address {} but this value does not fit in a int".format(self.c["MDR"],addr))
#### Split bytes and write ####
self.data[addr+3]=self.c["MDR"] & 0xFF
self.data[addr+2]=self.c["MDR"]>>8 & 0xFF
self.data[addr+1]=self.c["MDR"]>>16 & 0xFF
self.data[addr]=self.c["MDR"]>>24 & 0xFF
+ ###############################
def read(self):
"""
Read data from memory based Mic-1 architecture
"""
- addr=self.c["MAR"]
- value=None
- try:
- #### Combine bytes ####
- value=self.data[addr]<<24|(self.data[addr+1]<<16)|(self.data[addr+2]<<8)|(self.data[addr+3])
- except:
- if addr>self.lastAddr or addr<0:
- 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):
+ addr=self.c["MAR"] # Fetch address
+ if addr>self.lastAddr or addr<0:
+ raise ValueError("You get out of the ram by trying to get value at address {}, max address is {}".format(addr,self.lastAddr))
+ elif not(addr in self.data):
return(0)
+
+ #### Combine bytes ####
+ value=self.data[addr]<<24|(self.data[addr+1]<<16)|(self.data[addr+2]<<8)|(self.data[addr+3])
+ #######################
return(value)
def fetch(self):
@@ -67,15 +67,11 @@ class Ram:
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 or addr<0:
- 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):
+ if addr>self.lastAddr or addr<0:
+ raise ValueError("You get out of the ram by trying to get value at address {}, max address is {}".format(addr,self.lastAddr))
+ elif not(addr in self.data):
return(0)
- return(value)
+ return(self.data[addr])
def getData(self):
"""