diff --git a/MicSim/components/caretaker.py b/MicSim/components/caretaker.py index 82ac63d..cb92cbb 100644 --- a/MicSim/components/caretaker.py +++ b/MicSim/components/caretaker.py @@ -15,20 +15,28 @@ class Caretaker: if key=="MBRU": # If we ask for unsigned return(abs(self.objects["MBR"])) elif key== "MBR": - if abs(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 + 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)) 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 + 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 self.objects[key]=value def items(self): diff --git a/MicSim/test/test_caretaker.py b/MicSim/test/test_caretaker.py index 5cc7483..e4b8dfb 100644 --- a/MicSim/test/test_caretaker.py +++ b/MicSim/test/test_caretaker.py @@ -16,13 +16,16 @@ class CaretakerTest(unittest.TestCase): """ Test if getitem operation follow Mic-1 rules """ - toWrite=randint(0,126) # Only 7 bit for signed MBR (2^7=127) - self.c["MBR"]=-toWrite - self.assertEqual(self.c["MBRU"],toWrite,"Tested with {}".format(-toWrite)) - self.assertEqual(self.c["MBR"],-(-((toWrite-1)^0xFF)),"Tested with {}".format(-toWrite)) - self.c["MBR"]=toWrite - self.assertEqual(self.c["MBRU"],toWrite,"Tested with {}".format(toWrite)) - self.assertEqual(self.c["MBR"],toWrite,"Tested with {}".format(toWrite)) + for toWrite in range(0,127):# Only 7 bit for signed MBR (2^7=127) + self.c["MBR"]=-toWrite + self.assertEqual(self.c["MBRU"],toWrite,"Tested with {}".format(-toWrite)) + self.assertEqual(self.c["MBR"],-toWrite,"Tested with {}".format(-toWrite)) + + for toWrite in range(0,255):# Only 2^8 value for unsigned + self.c["MBR"]=toWrite + self.assertEqual(self.c["MBRU"],toWrite,"Tested with {}".format(toWrite)) + if toWrite>127: # We enter in the zone of negative number at 127 + self.assertEqual(self.c["MBR"],-(toWrite&0x7F),"Tested with {}".format(toWrite)) with self.assertRaises(KeyError): # Check it returns a KeyError self.c["kjhkjhkoih"+str(randint(0,7698))]