#!/usr/bin/python class Caretaker: def __init__(self): 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 self.objects["RAM"]=None def __getitem__(self,key): if key=="MBRU": # If we ask for unsigned return(abs(self.objects["MBR"])) elif key== "MBR": if (self.objects[key]>>7)==1: # If it a negative number (2 complement) return(-((self.objects[key]-1)^0xFF)) # transforme bin negative number to python negative number else: return(self.objects[key]) return(self.objects[key]) def __setitem__(self,key,value): if key!="RAM": if value > (2**32) 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) and key=="MBR" and key=="MBRU": # Check value fit in byte print("Warning byte overflow: value {} on register {}".format(value,key)) value=value%256 # Force to fit in byte self.objects[key]=value def items(self): return(self.objects.items())