MicSim/MicSim/components/caretaker.py

45 lines
1.6 KiB
Python
Raw Normal View History

2018-08-31 18:42:12 +02:00
#!/usr/bin/python
2018-09-02 20:02:27 +02:00
from components.ram import Ram
2018-08-31 18:42:12 +02:00
class Caretaker:
2018-09-02 20:02:27 +02:00
def __init__(self,ramSize):
2018-08-31 18:42:12 +02:00
self.objects=dict() # Create empty objects pool
# Add registers to pool
for reg in ["MAR","MDR", "PC", "MBR", "SP","LV","CPP","TOS","OPC","H"]:
self.objects[reg]=0
2018-09-02 20:02:27 +02:00
self.objects["RAM"]=Ram(self,ramSize)
2018-08-31 18:42:12 +02:00
2018-09-01 10:19:06 +02:00
def __getitem__(self,key):
if key=="MBRU": # If we ask for unsigned
2018-09-01 16:49:12 +02:00
return(abs(self.objects["MBR"]))
elif key== "MBR":
2018-09-02 23:10:31 +02:00
if self.objects[key] < 0:
return(self.objects[key])
elif self.objects[key]>>7==1: # If it a negative number (2 complement)
return(-(self.objects[key]&0x7F))
2018-09-01 16:49:12 +02:00
else:
return(self.objects[key])
2018-08-31 18:42:12 +02:00
return(self.objects[key])
2018-09-01 17:22:49 +02:00
def __setitem__(self,key,value):
if key!="RAM":
2018-09-02 23:10:31 +02:00
if (-(2**7-1))>value and (key=="MBR" or key=="MBRU"):
raise RuntimeError("Value {} cannot fit in MBR register (-2^7 minimum value)".format(value))
elif value>(2**32-1) and (key!="MBR" and key!="MBRU"):
raise RuntimeError("Value {} cannot fit in MBR register (2^32 minimum value)".format(value))
# if key!="RAM":
# if (value > (2**32) or value <(2**32)/2) and key!="MBR" and key!="MBRU": # Check value fit in word
# print("Warning word overflow: value {} on register {}".format(value,key))
# value=value%(2**32) # Force to fit in word
# elif (value > (2**8) or value <126) and key=="MBR" or key=="MBRU": # Check value fit in byte
# print("Warning byte overflow: value {} on register {}".format(value,key))
# value=abs(value)%256 # Force to fit in byte
2018-08-31 18:42:12 +02:00
self.objects[key]=value
def items(self):
return(self.objects.items())