def test_init(self): """ Validates good working of the __init__ method for MemoryArray """ ma = MemoryArray() self.assertEqual(MEMORY_ARRAY_NUMBER_OF_MEMORY_CELL, len(ma._memoryCellArray)) for mc in ma.readMemory(MEMORY_START_AT, MEMORY_ARRAY_NUMBER_OF_MEMORY_CELL): self.assertEqual(MEMORY_CELL_INITIAL_VALUE, mc)
def test_init(self): """ Validates good working of the __init__ method for MemoryIOController """ ma = MemoryArray() self.assertRaises(RuntimeError, MemoryIOController) self.assertRaises(RuntimeError, MemoryIOController, "invalid") self.assertEqual(MemoryIOController, type(MemoryIOController(ma)))
def test_init(self): """ Validates good working of the __init__ method for InstructionFetchUnit """ self.assertRaises( RuntimeError, InstructionFetchUnit) # IFU needs a memory array to work with ma = MemoryArray() ifu = InstructionFetchUnit(ma) self.assertEqual(ifu._memoryArray, ma)
def __init__(self, ma=None, mioc=None, name="System"): """ Preparing the whole execution environment for this Capua instance. Note that a single memory array can be shared between multiple Capua environment. """ # Two execution units CAN share a single memory array and MIOC if ma is None: self.ma = MemoryArray() else: self.ma = ma if mioc is None: self.mioc = MemoryIOController(self.ma, testOnly=False) else: self.mioc = mioc self.ifu = InstructionFetchUnit(self.ma) self.eu = ExecutionUnit(self.mioc, self.ifu, name)
class TestInstructionFetchUnit(unittest.TestCase): ma = MemoryArray() ifu = InstructionFetchUnit(ma) def test_init(self): """ Validates good working of the __init__ method for InstructionFetchUnit """ self.assertRaises( RuntimeError, InstructionFetchUnit) # IFU needs a memory array to work with ma = MemoryArray() ifu = InstructionFetchUnit(ma) self.assertEqual(ifu._memoryArray, ma) def test_fetchInstructionAtAddress(self): """ Validates good working of the fetchInstructionAtAddress method for InstructionFetchUnit """ self.ifu._memoryArray.writeMemory( MEMORY_START_AT, [0xFF]) # Insert nop instruction in memory instruction, nextInstructionAddress = self.ifu.fetchInstructionAtAddress( MEMORY_START_AT) self.assertIsNotNone(instruction.instructionCode) self.assertIsNone(instruction.sourceRegister) self.assertIsNone(instruction.destinationRegister) self.assertIsNone(instruction.sourceImmediate) self.assertIsNone(instruction.destinationImmediate) self.assertIsNone(instruction.width) self.assertIsNone(instruction.flags) self.assertEqual(nextInstructionAddress, MEMORY_START_AT + 1) self.ifu._memoryArray.writeMemory( MEMORY_START_AT, [0b00010000]) # Insert InsWidthRegReg instruction type in memory instruction, nextInstructionAddress = self.ifu.fetchInstructionAtAddress( MEMORY_START_AT) self.assertIsNotNone(instruction.instructionCode) self.assertIsNotNone(instruction.sourceRegister) self.assertIsNotNone(instruction.destinationRegister) self.assertIsNone(instruction.sourceImmediate) self.assertIsNone(instruction.destinationImmediate) self.assertIsNotNone(instruction.width) self.assertIsNone(instruction.flags) self.assertEqual(nextInstructionAddress, MEMORY_START_AT + 3) def test_fetchInstructionFormAtAddress(self): """ Validates good working of the fetchInstructionFormAtAddress method for InstructionFetchUnit """ self.ifu._memoryArray.writeMemory(MEMORY_START_AT, [0xFF]) form = self.ifu._fetchInstructionFormAtAddress(MEMORY_START_AT) self.assertEqual(formDescription["Ins"], form) self.ifu._memoryArray.writeMemory(MEMORY_START_AT, [0b01110000]) form = self.ifu._fetchInstructionFormAtAddress(MEMORY_START_AT) self.assertEqual(formDescription["InsReg"], form) self.ifu._memoryArray.writeMemory(MEMORY_START_AT, [0b10000000]) form = self.ifu._fetchInstructionFormAtAddress(MEMORY_START_AT) self.assertEqual(formDescription["InsImm"], form) self.ifu._memoryArray.writeMemory(MEMORY_START_AT, [0b01100000]) form = self.ifu._fetchInstructionFormAtAddress(MEMORY_START_AT) self.assertEqual(formDescription["InsImmReg"], form) self.ifu._memoryArray.writeMemory(MEMORY_START_AT, [0b00000000]) form = self.ifu._fetchInstructionFormAtAddress(MEMORY_START_AT) self.assertEqual(formDescription["InsWidthImmReg"], form) self.ifu._memoryArray.writeMemory(MEMORY_START_AT, [0b00010000]) form = self.ifu._fetchInstructionFormAtAddress(MEMORY_START_AT) self.assertEqual(formDescription["InsWidthRegReg"], form) self.ifu._memoryArray.writeMemory(MEMORY_START_AT, [0b00100000]) form = self.ifu._fetchInstructionFormAtAddress(MEMORY_START_AT) self.assertEqual(formDescription["InsWidthRegImm"], form) self.ifu._memoryArray.writeMemory(MEMORY_START_AT, [0b00110000]) form = self.ifu._fetchInstructionFormAtAddress(MEMORY_START_AT) self.assertEqual(formDescription["InsWidthImmImm"], form) self.ifu._memoryArray.writeMemory(MEMORY_START_AT, [0b01000000]) form = self.ifu._fetchInstructionFormAtAddress(MEMORY_START_AT) self.assertEqual(formDescription["InsFlagImm"], form) self.ifu._memoryArray.writeMemory(MEMORY_START_AT, [0b01010000]) form = self.ifu._fetchInstructionFormAtAddress(MEMORY_START_AT) self.assertEqual(formDescription["InsFlagReg"], form) self.ifu._memoryArray.writeMemory(MEMORY_START_AT, [0b10010000]) form = self.ifu._fetchInstructionFormAtAddress(MEMORY_START_AT) self.assertEqual(formDescription["InsRegReg"], form) def test_fetchInstructionAtAddressUsingForm(self): """ Validates good working of the fetchInstructionAtAddressUsingForm method for InstructionFetchUnit """ ins = self.ifu._fetchInstructionAtAddressUsingForm( MEMORY_START_AT, formDescription["Ins"]) self.assertIsNotNone(ins.instructionCode) self.assertIsNone(ins.sourceRegister) self.assertIsNone(ins.destinationRegister) self.assertIsNone(ins.sourceImmediate) self.assertIsNone(ins.destinationImmediate) self.assertIsNone(ins.width) self.assertIsNone(ins.flags) ins = self.ifu._fetchInstructionAtAddressUsingForm( MEMORY_START_AT, formDescription["InsReg"]) self.assertIsNotNone(ins.instructionCode) self.assertIsNotNone(ins.sourceRegister) self.assertIsNone(ins.destinationRegister) self.assertIsNone(ins.sourceImmediate) self.assertIsNone(ins.destinationImmediate) self.assertIsNone(ins.width) self.assertIsNone(ins.flags) ins = self.ifu._fetchInstructionAtAddressUsingForm( MEMORY_START_AT, formDescription["InsImm"]) self.assertIsNotNone(ins.instructionCode) self.assertIsNone(ins.sourceRegister) self.assertIsNone(ins.destinationRegister) self.assertIsNotNone(ins.sourceImmediate) self.assertIsNone(ins.destinationImmediate) self.assertIsNone(ins.width) self.assertIsNone(ins.flags) ins = self.ifu._fetchInstructionAtAddressUsingForm( MEMORY_START_AT, formDescription["InsImmReg"]) self.assertIsNotNone(ins.instructionCode) self.assertIsNone(ins.sourceRegister) self.assertIsNotNone(ins.destinationRegister) self.assertIsNotNone(ins.sourceImmediate) self.assertIsNone(ins.destinationImmediate) self.assertIsNone(ins.width) self.assertIsNone(ins.flags) ins = self.ifu._fetchInstructionAtAddressUsingForm( MEMORY_START_AT, formDescription["InsWidthImmReg"]) self.assertIsNotNone(ins.instructionCode) self.assertIsNone(ins.sourceRegister) self.assertIsNotNone(ins.destinationRegister) self.assertIsNotNone(ins.sourceImmediate) self.assertIsNone(ins.destinationImmediate) self.assertIsNotNone(ins.width) self.assertIsNone(ins.flags) ins = self.ifu._fetchInstructionAtAddressUsingForm( MEMORY_START_AT, formDescription["InsWidthRegReg"]) self.assertIsNotNone(ins.instructionCode) self.assertIsNotNone(ins.sourceRegister) self.assertIsNotNone(ins.destinationRegister) self.assertIsNone(ins.sourceImmediate) self.assertIsNone(ins.destinationImmediate) self.assertIsNotNone(ins.width) self.assertIsNone(ins.flags) ins = self.ifu._fetchInstructionAtAddressUsingForm( MEMORY_START_AT, formDescription["InsWidthRegImm"]) self.assertIsNotNone(ins.instructionCode) self.assertIsNotNone(ins.sourceRegister) self.assertIsNone(ins.destinationRegister) self.assertIsNone(ins.sourceImmediate) self.assertIsNotNone(ins.destinationImmediate) self.assertIsNotNone(ins.width) self.assertIsNone(ins.flags) ins = self.ifu._fetchInstructionAtAddressUsingForm( MEMORY_START_AT, formDescription["InsWidthImmImm"]) self.assertIsNotNone(ins.instructionCode) self.assertIsNone(ins.sourceRegister) self.assertIsNone(ins.destinationRegister) self.assertIsNotNone(ins.sourceImmediate) self.assertIsNotNone(ins.destinationImmediate) self.assertIsNotNone(ins.width) self.assertIsNone(ins.flags) ins = self.ifu._fetchInstructionAtAddressUsingForm( MEMORY_START_AT, formDescription["InsFlagImm"]) self.assertIsNotNone(ins.instructionCode) self.assertIsNone(ins.sourceRegister) self.assertIsNone(ins.destinationRegister) self.assertIsNotNone(ins.sourceImmediate) self.assertIsNone(ins.destinationImmediate) self.assertIsNone(ins.width) self.assertIsNotNone(ins.flags) ins = self.ifu._fetchInstructionAtAddressUsingForm( MEMORY_START_AT, formDescription["InsFlagReg"]) self.assertIsNotNone(ins.instructionCode) self.assertIsNotNone(ins.sourceRegister) self.assertIsNone(ins.destinationRegister) self.assertIsNone(ins.sourceImmediate) self.assertIsNone(ins.destinationImmediate) self.assertIsNone(ins.width) self.assertIsNotNone(ins.flags) ins = self.ifu._fetchInstructionAtAddressUsingForm( MEMORY_START_AT, formDescription["InsRegReg"]) self.assertIsNotNone(ins.instructionCode) self.assertIsNotNone(ins.sourceRegister) self.assertIsNotNone(ins.destinationRegister) self.assertIsNone(ins.sourceImmediate) self.assertIsNone(ins.destinationImmediate) self.assertIsNone(ins.width) self.assertIsNone(ins.flags)
class TestMemoryArray(unittest.TestCase): ma = MemoryArray() def test_init(self): """ Validates good working of the __init__ method for MemoryArray """ ma = MemoryArray() self.assertEqual(MEMORY_ARRAY_NUMBER_OF_MEMORY_CELL, len(ma._memoryCellArray)) for mc in ma.readMemory(MEMORY_START_AT, MEMORY_ARRAY_NUMBER_OF_MEMORY_CELL): self.assertEqual(MEMORY_CELL_INITIAL_VALUE, mc) def test_readMemory(self): """ Validates good working of the readMemory method for MemoryArray """ self.assertEqual(1, len(self.ma.readMemory(MEMORY_START_AT, 1))) self.assertEqual(5, len(self.ma.readMemory(MEMORY_START_AT, 5))) self.assertEqual( MEMORY_ARRAY_NUMBER_OF_MEMORY_CELL, len( self.ma.readMemory(MEMORY_START_AT, MEMORY_ARRAY_NUMBER_OF_MEMORY_CELL))) self.assertEqual(1, len(self.ma.readMemory(MEMORY_END_AT - 1, 1))) self.assertRaises(MemoryError, self.ma.readMemory, MEMORY_END_AT - 5, 6) self.assertRaises(MemoryError, self.ma.readMemory, MEMORY_START_AT - 1, 6) def test_writeMemory(self): """ Validates good working of the writeMemory method for MemoryArray """ self.ma.writeMemory(MEMORY_START_AT, [1]) self.assertEqual(1, self.ma.readMemory(MEMORY_START_AT, 1)[0]) self.ma.writeMemory(MEMORY_START_AT, [0, 0, 0, 1]) self.assertEqual(0, self.ma.readMemory(MEMORY_START_AT, 1)[0]) self.assertEqual(0, self.ma.readMemory(MEMORY_START_AT + 1, 1)[0]) self.assertEqual(0, self.ma.readMemory(MEMORY_START_AT + 2, 1)[0]) self.assertEqual(1, self.ma.readMemory(MEMORY_START_AT + 3, 1)[0]) self.assertRaises(MemoryError, self.ma.writeMemory, MEMORY_END_AT + 5, [1]) self.assertRaises(MemoryError, self.ma.writeMemory, MEMORY_START_AT - 1, [1]) def test_validateAddressForLengthAccess(self): """ Validates good working of the _validateAddressForLengthAccess method """ self.assertRaises(MemoryError, self.ma._validateAddressForLengthAccess, MEMORY_START_AT - 1, 1) self.assertRaises(MemoryError, self.ma._validateAddressForLengthAccess, MEMORY_START_AT - 1, 4) self.assertRaises(MemoryError, self.ma._validateAddressForLengthAccess, MEMORY_END_AT, 1) self.assertRaises(MemoryError, self.ma._validateAddressForLengthAccess, MEMORY_START_AT - 1, 2) def test_directMemoryCellAccess(self): """ Validates good working of the directMemoryCellAccess method for MemoryArray """ self.assertIsNotNone(self.ma.directMemoryCellAccess( MEMORY_START_AT)) # Valid memory address self.assertIsNotNone( self.ma.directMemoryCellAccess(MEMORY_END_AT - 1)) # Valid memory address self.assertRaises(MemoryError, self.ma.directMemoryCellAccess, MEMORY_START_AT - 1) # Invalid memory access self.assertRaises(MemoryError, self.ma.directMemoryCellAccess, MEMORY_END_AT) # Invalid memory access def test_computeArrayIndexFromAddress(self): """ Validates good working of the computeArrayIndexFromAddress method for MemoryArray """ self.assertTrue( self.ma._computeArrayIndexFromAddress(MEMORY_START_AT) >= 0) # Valid address self.assertTrue( self.ma._computeArrayIndexFromAddress( MEMORY_START_AT + MEMORY_ARRAY_NUMBER_OF_MEMORY_CELL - 1) >= 0) # Valid address self.assertRaises(MemoryError, self.ma._computeArrayIndexFromAddress, MEMORY_START_AT - 1) # Invalid address self.assertRaises(MemoryError, self.ma._computeArrayIndexFromAddress, MEMORY_END_AT + 1) # invalid address
class TestMemoryIOController(unittest.TestCase): ma = MemoryArray() mioc = MemoryIOController(ma) def test_init(self): """ Validates good working of the __init__ method for MemoryIOController """ ma = MemoryArray() self.assertRaises(RuntimeError, MemoryIOController) self.assertRaises(RuntimeError, MemoryIOController, "invalid") self.assertEqual(MemoryIOController, type(MemoryIOController(ma))) def test_memoryReadWriteAtAddressForLength(self): """ Validates good working of the memoryReadAtAddressForLength memoryWriteAtAddressForLength method for MemoryIOController memoryWriteAtAddressForLength(self, address=0x00, length=4, value=0x00) memoryReadAtAddressForLength(self, address=0x00, length=4) """ self.mioc.memoryWriteAtAddressForLength(MEMORY_START_AT, 1, 0x01) memValue = self.mioc.memoryReadAtAddressForLength(MEMORY_START_AT, 1) self.assertEqual(0x01, memValue) self.mioc.memoryWriteAtAddressForLength(MEMORY_START_AT, 2, 0x0202) memValue = self.mioc.memoryReadAtAddressForLength(MEMORY_START_AT, 2) self.assertEqual(0x0202, memValue) memValue = self.mioc.memoryReadAtAddressForLength( MEMORY_START_AT + 1, 1) self.assertEqual(0x02, memValue) self.mioc.memoryWriteAtAddressForLength(MEMORY_START_AT, 3, 0x030303) memValue = self.mioc.memoryReadAtAddressForLength(MEMORY_START_AT, 3) self.assertEqual(0x030303, memValue) memValue = self.mioc.memoryReadAtAddressForLength( MEMORY_START_AT + 1, 2) self.assertEqual(0x0303, memValue) self.mioc.memoryWriteAtAddressForLength(MEMORY_START_AT, 4, 0x04040404) memValue = self.mioc.memoryReadAtAddressForLength(MEMORY_START_AT, 4) self.assertEqual(0x04040404, memValue) memValue = self.mioc.memoryReadAtAddressForLength( MEMORY_START_AT + 1, 3) self.assertEqual(0x040404, memValue) memValue = self.mioc.memoryReadAtAddressForLength( MEMORY_START_AT + 1, 4) self.assertEqual(0x040404FF, memValue) def test_passMemoryReadWriteToMemoryMappedHardware(self): """ Validates good working of the passMemoryReadWriteToMemoryMappedHardware method for MemoryIOController passMemoryReadWriteToMemoryMappedHardware(self, address=0x00, length=4, value=0x00, isWrite=False) """ self.assertRaises(MemoryError, self.mioc._passMemoryReadWriteToMemoryMappedHardware) def test_prepareNumericValueToBeWrittenToMemory(self): """ Validates good working of the passMemoryReadWriteToMemoryMappedHardware method for MemoryIOController _prepareNumericValueToBeWrittenToMemory(self, length=0, value=0) """ self.assertEqual( 1, len(self.mioc._prepareNumericValueToBeWrittenToMemory(1, 0xff))) self.assertEqual( 2, len(self.mioc._prepareNumericValueToBeWrittenToMemory(2, 0xffff))) self.assertEqual( 3, len(self.mioc._prepareNumericValueToBeWrittenToMemory(3, 0xffffff))) self.assertEqual( 4, len( self.mioc._prepareNumericValueToBeWrittenToMemory( 4, 0xffffffff))) self.assertEqual( 4, len(self.mioc._prepareNumericValueToBeWrittenToMemory(4, 0xff))) self.assertRaises(ValueError, self.mioc._prepareNumericValueToBeWrittenToMemory, 1, 0xffff)
class TestMemoryArray(unittest.TestCase): ma = MemoryArray() def test_init(self): """ Validates good working of the __init__ method for MemoryArray """ ma = MemoryArray() self.assertEqual(MEMORY_ARRAY_NUMBER_OF_MEMORY_CELL, len(ma._memoryCellArray)) for mc in ma.extractMemory(MEMORY_START_AT, MEMORY_ARRAY_NUMBER_OF_MEMORY_CELL): self.assertEqual(0b111, mc._permission) self.assertEqual(MEMORY_CELL_INITIAL_VALUE, mc._value) def test_extractMemory(self): """ Validates good working of the extractMemory method for MemoryArray """ self.assertEqual(1, len(self.ma.extractMemory(MEMORY_START_AT, 1))) self.assertEqual(5, len(self.ma.extractMemory(MEMORY_START_AT, 5))) self.assertEqual( MEMORY_ARRAY_NUMBER_OF_MEMORY_CELL, len( self.ma.extractMemory(MEMORY_START_AT, MEMORY_ARRAY_NUMBER_OF_MEMORY_CELL))) self.assertEqual(1, len(self.ma.extractMemory(MEMORY_END_AT - 1, 1))) self.assertRaises(MemoryError, self.ma.extractMemory, MEMORY_END_AT - 5, 6) self.assertRaises(MemoryError, self.ma.extractMemory, MEMORY_START_AT - 1, 6) def test_directMemoryCellAccess(self): """ Validates good working of the directMemoryCellAccess method for MemoryArray """ self.assertIsNotNone(self.ma.directMemoryCellAccess( MEMORY_START_AT)) # Valid memory address self.assertIsNotNone( self.ma.directMemoryCellAccess(MEMORY_END_AT - 1)) # Valid memory address self.assertRaises(MemoryError, self.ma.directMemoryCellAccess, MEMORY_START_AT - 1) # Invalid memory access self.assertRaises(MemoryError, self.ma.directMemoryCellAccess, MEMORY_END_AT) # Invalid memory access def test_computeArrayIndexFromAddress(self): """ Validates good working of the computeArrayIndexFromAddress method for MemoryArray """ self.assertTrue( self.ma._computeArrayIndexFromAddress(MEMORY_START_AT) >= 0) # Valid address self.assertTrue( self.ma._computeArrayIndexFromAddress( MEMORY_START_AT + MEMORY_ARRAY_NUMBER_OF_MEMORY_CELL - 1) >= 0) # Valid address self.assertRaises(MemoryError, self.ma._computeArrayIndexFromAddress, MEMORY_START_AT - 1) # Invalid address self.assertRaises(MemoryError, self.ma._computeArrayIndexFromAddress, MEMORY_END_AT + 1) # invalid address
class TestExecutionUnit(unittest.TestCase): ma = MemoryArray() mioc = MemoryIOController(ma) ifu = InstructionFetchUnit(ma) eu = ExecutionUnit(mioc, ifu, "System") def test_init(self): """ Validates good working of the __init__ method for ExecutionUnit def __init__(self, mioc: MemoryIOController=None, ifu: InstructionFetchUnit=None, name: str="System"): """ self.assertRaises(RuntimeError, ExecutionUnit, None, None, None) self.assertRaises(RuntimeError, ExecutionUnit, self.mioc, self.ifu, None) self.assertRaises(RuntimeError, ExecutionUnit, self.mioc, None, "System") self.assertRaises(RuntimeError, ExecutionUnit, None, self.ifu, "System") self.assertRaises(RuntimeError, ExecutionUnit, "test", self.ifu, "System") self.assertRaises(RuntimeError, ExecutionUnit, self.mioc, "test", "System") self.assertRaises(RuntimeError, ExecutionUnit, self.mioc, self.ifu, 0x41414141) eu = ExecutionUnit(self.mioc, self.ifu, "System") self.assertEqual(self.mioc, eu.mioc) self.assertEqual(self.ifu, eu.ifu) self.assertEqual("System", eu.name) def test_setupCore(self): """ Validates good working of the setupCore method for ExecutionUnit def setupCore(self, I: int=MEMORY_START_AT): """ self.assertRaises(RuntimeError, self.eu.setupCore, (MEMORY_START_AT - 1)) self.assertRaises(RuntimeError, self.eu.setupCore, (MEMORY_END_AT + 1)) self.eu.setupCore(MEMORY_START_AT) self.assertEqual(MEMORY_START_AT, self.eu.I) def test_execute(self): """ Validates good working of the execute method for ExecutionUnit def execute(self): """ # NOP self.eu.setupCore(MEMORY_START_AT) self.eu.execute() self.assertEqual(MEMORY_START_AT + 1, self.eu.I) self.assertEqual(0, self.eu.A) self.assertEqual(0, self.eu.B) self.assertEqual(0, self.eu.C) self.assertEqual(0, self.eu.S) self.assertEqual(0, self.eu.FLAGS) self.eu.execute() # NOP self.assertEqual(MEMORY_START_AT + 2, self.eu.I) self.assertEqual(0, self.eu.A) self.assertEqual(0, self.eu.B) self.assertEqual(0, self.eu.C) self.assertEqual(0, self.eu.S) self.assertEqual(0, self.eu.FLAGS) # MOV self.mioc.memoryWriteAtAddressForLength(MEMORY_START_AT + 2, 1, 0b01100000) self.mioc.memoryWriteAtAddressForLength(MEMORY_START_AT + 3, 1, 0b00) self.mioc.memoryWriteAtAddressForLength(MEMORY_START_AT + 4, 1, 0b00) self.mioc.memoryWriteAtAddressForLength(MEMORY_START_AT + 5, 1, 0b00) self.mioc.memoryWriteAtAddressForLength(MEMORY_START_AT + 6, 1, 0b01) self.mioc.memoryWriteAtAddressForLength(MEMORY_START_AT + 7, 1, 0b00) self.eu.execute() self.assertEqual(MEMORY_START_AT + 8, self.eu.I) self.assertEqual(1, self.eu.A) self.assertEqual(0, self.eu.B) self.assertEqual(0, self.eu.C) self.assertEqual(0, self.eu.S) self.assertEqual(0, self.eu.FLAGS) # JMP self.mioc.memoryWriteAtAddressForLength(MEMORY_START_AT + 8, 1, 0b01000001) self.mioc.memoryWriteAtAddressForLength(MEMORY_START_AT + 9, 1, 0b00) self.mioc.memoryWriteAtAddressForLength(MEMORY_START_AT + 10, 4, MEMORY_START_AT) self.eu.execute() self.assertEqual(MEMORY_START_AT, self.eu.I) self.assertEqual(1, self.eu.A) self.assertEqual(0, self.eu.B) self.assertEqual(0, self.eu.C) self.assertEqual(0, self.eu.S) self.assertEqual(0, self.eu.FLAGS) # NOP self.eu.execute() self.assertEqual(MEMORY_START_AT + 1, self.eu.I) self.assertEqual(1, self.eu.A) self.assertEqual(0, self.eu.B) self.assertEqual(0, self.eu.C) self.assertEqual(0, self.eu.S) self.assertEqual(0, self.eu.FLAGS) # NOP self.eu.execute() self.assertEqual(MEMORY_START_AT + 2, self.eu.I) self.assertEqual(1, self.eu.A) self.assertEqual(0, self.eu.B) self.assertEqual(0, self.eu.C) self.assertEqual(0, self.eu.S) self.assertEqual(0, self.eu.FLAGS) # Beware, ACTI and DACTI tests are dependent one on anoter self.eu.setupCore(MEMORY_START_AT) self.mioc.memoryWriteAtAddressForLength(MEMORY_START_AT, 1, 0b11110001) # ACTI 1 self.mioc.memoryWriteAtAddressForLength(MEMORY_START_AT + 1, 1, 0b11110001) # ACTI 2 self.mioc.memoryWriteAtAddressForLength(MEMORY_START_AT + 2, 1, 0b11110010) # DACTI 1 self.mioc.memoryWriteAtAddressForLength(MEMORY_START_AT + 3, 1, 0b11110010) # DACTI 2 # ACTI self.eu.execute() self.assertEqual(MEMORY_START_AT + 1, self.eu.I) self.assertEqual( 0b1, self.eu.IS) # Interrupt State should be = 1 after ACTI self.eu.execute() self.assertEqual(MEMORY_START_AT + 2, self.eu.I) self.assertEqual( 0b1, self.eu.IS ) # Interrupt State should not change if ACTI is run when IS = 1 # DACTI self.eu.execute() self.assertEqual(MEMORY_START_AT + 3, self.eu.I) self.assertEqual( 0b0, self.eu.IS) # Interrupt State should be = 0 after DACTI self.eu.execute() self.assertEqual(MEMORY_START_AT + 4, self.eu.I) self.assertEqual( 0b0, self.eu.IS ) # Interrupt State should not change if DACTI is run when IS = 0 # SFSTOR - Data will be written self.eu.setupCore(MEMORY_START_AT) self.eu.A = MEMORY_START_AT + 20 self.eu.B = 0xCCCCCCCC self.mioc.memoryWriteAtAddressForLength(MEMORY_START_AT, 1, 0b01010010) self.mioc.memoryWriteAtAddressForLength( MEMORY_START_AT + 1, 1, 0b00110001) # Instruction goes: SFSTOR [LH] $B self.eu.FLAGS = 0b00 self.eu.execute() dataWritten = self.mioc.memoryReadAtAddressForLength(MEMORY_START_AT + 20, length=4) self.assertEqual(MEMORY_START_AT + 2, self.eu.I) self.assertEqual(0xCCCCCCCC, dataWritten) self.assertEqual(0b10, self.eu.FLAGS) # SFSTOR - Data will NOT be written self.eu.setupCore(MEMORY_START_AT) self.eu.A = MEMORY_START_AT + 20 self.eu.B = 0xBBBBBBBB self.mioc.memoryWriteAtAddressForLength(MEMORY_START_AT, 1, 0b01010010) self.mioc.memoryWriteAtAddressForLength( MEMORY_START_AT + 1, 1, 0b01000001) # Instruction goes: SFSTOR [E] $B self.mioc.memoryWriteAtAddressForLength(MEMORY_START_AT + 20, 4, 0xAAAAAAAA) self.eu.FLAGS = 0b00 self.eu.execute() dataWritten = self.mioc.memoryReadAtAddressForLength(MEMORY_START_AT + 20, length=4) self.assertEqual(MEMORY_START_AT + 2, self.eu.I) self.assertNotEqual(0xBBBBBBBB, dataWritten) self.assertEqual(0b1, self.eu.FLAGS) # SIVR self.eu.setupCore(MEMORY_START_AT) self.eu.A = 0xAAAAAAAA self.mioc.memoryWriteAtAddressForLength(MEMORY_START_AT, 1, 0b01110101) self.mioc.memoryWriteAtAddressForLength( MEMORY_START_AT + 1, 1, 0b00000000) # Instruction goes: SIVR $A self.eu.execute() self.assertEqual(MEMORY_START_AT + 2, self.eu.I) self.assertEqual(0xAAAAAAAA, self.eu.IVR) self.assertEqual(0xAAAAAAAA, self.eu.A) # HIRET - Deactivated Interrupts self.eu.setupCore(MEMORY_START_AT) self.mioc.memoryWriteAtAddressForLength(MEMORY_START_AT, 1, 0b11110011) self.mioc.memoryWriteAtAddressForLength( 0x40000100, 4, 0x40000200) # This is the return address self.mioc.memoryWriteAtAddressForLength( 0x400000FC, 4, 0x1) # This is the flag to be restored self.eu.A = 0xFF self.eu.IS = 0b0 self.eu.S = 0x40000100 self.eu.FLAGS = 0x0 sMarker = self.eu.S self.eu.execute() self.assertEqual(0xFF, self.eu.A, "HIRET broken - bad A register value") self.assertEqual(0x01, self.eu.FLAGS, "HIRET broken - bad flag restoration") self.assertEqual(0b1, self.eu.IS, "HIRET broken - IS should not be 0") self.assertEqual(0x40000200, self.eu.I, "HIRET broken - Bad I address") self.assertEqual(sMarker, self.eu.S + 8, "HIRET broken - Bad stack") # INT register - Deactivated Interrupts self.eu.setupCore(MEMORY_START_AT) self.mioc.memoryWriteAtAddressForLength(MEMORY_START_AT, 1, 0b01110110) self.mioc.memoryWriteAtAddressForLength(MEMORY_START_AT + 1, 1, 0b00000000) self.eu.A = 0x0 # Holds the interrupt number self.eu.IS = 0b0 self.eu.S = 0x40000100 self.eu.IVR = 0x40000050 self.mioc.memoryWriteAtAddressForLength(self.eu.IVR, 4, 0xAAAAAAAA) self.mioc.memoryWriteAtAddressForLength(self.eu.IVR + 4, 4, 0xBBBBBBBB) sMarker = self.eu.S self.eu.execute() self.assertEqual(self.eu.I, MEMORY_START_AT + 2, "INT when IS is 0 broken - Bad I address") self.assertEqual(self.eu.S, sMarker, "INT when IS is 0 broken - Bad stack") # INT register - Activated Interrupts self.eu.setupCore(MEMORY_START_AT) self.mioc.memoryWriteAtAddressForLength(MEMORY_START_AT, 1, 0b01110110) self.mioc.memoryWriteAtAddressForLength(MEMORY_START_AT + 1, 1, 0b00000000) self.eu.A = 0x0 # Holds the interrupt number self.eu.IS = 0b1 self.eu.S = 0x40000100 self.eu.IVR = 0x40000050 self.mioc.memoryWriteAtAddressForLength(self.eu.IVR, 4, 0xAAAAAAAA) self.mioc.memoryWriteAtAddressForLength(self.eu.IVR + 4, 4, 0xBBBBBBBB) sMarker = self.eu.S iMarker = self.eu.I self.eu.execute() self.assertEqual(self.eu.I, 0xAAAAAAAA, "INT when IS is 1 broken - Bad I address") self.assertEqual(self.eu.S, sMarker + 4, "INT when IS is 1 broken - Bad stack") topStack = self.mioc.memoryReadAtAddressForLength(self.eu.S, 4) self.assertEqual(iMarker + 2, topStack, "INT when IS is 1 broken - Bad return address") # Testing the second vector entry self.eu.setupCore(MEMORY_START_AT) self.mioc.memoryWriteAtAddressForLength(MEMORY_START_AT, 1, 0b01110110) self.mioc.memoryWriteAtAddressForLength(MEMORY_START_AT + 1, 1, 0b00000000) self.eu.A = 0x1 # Holds the interrupt number self.eu.IS = 0b1 self.eu.S = 0x40000100 self.eu.IVR = 0x40000050 self.mioc.memoryWriteAtAddressForLength(self.eu.IVR, 4, 0xAAAAAAAA) self.mioc.memoryWriteAtAddressForLength(self.eu.IVR + 4, 4, 0xBBBBBBBB) sMarker = self.eu.S iMarker = self.eu.I self.eu.execute() self.assertEqual(self.eu.I, 0xBBBBBBBB, "INT when IS is 1 broken - Bad I address") self.assertEqual(self.eu.S, sMarker + 4, "INT when IS is 1 broken - Bad stack") topStack = self.mioc.memoryReadAtAddressForLength(self.eu.S, 4) self.assertEqual(iMarker + 2, topStack, "INT when IS is 1 broken - Bad return address") # INT immediate - Deactivated Interrupts self.eu.setupCore(MEMORY_START_AT) self.mioc.memoryWriteAtAddressForLength(MEMORY_START_AT, 1, 0b10000011) self.mioc.memoryWriteAtAddressForLength( MEMORY_START_AT + 1, 1, 0b00000000) # Interrupt number is here self.eu.IS = 0b0 self.eu.S = 0x40000100 self.eu.IVR = 0x40000050 self.mioc.memoryWriteAtAddressForLength(self.eu.IVR, 4, 0xAAAAAAAA) self.mioc.memoryWriteAtAddressForLength(self.eu.IVR + 4, 4, 0xBBBBBBBB) sMarker = self.eu.S self.eu.execute() self.assertEqual(self.eu.I, MEMORY_START_AT + 5, "INT when IS is 0 broken - Bad I address") self.assertEqual(self.eu.S, sMarker, "INT when IS is 0 broken - Bad stack") # INT immediate - Activated Interrupts self.eu.setupCore(MEMORY_START_AT) self.mioc.memoryWriteAtAddressForLength(MEMORY_START_AT, 1, 0b10000011) self.mioc.memoryWriteAtAddressForLength( MEMORY_START_AT + 1, 4, 0b00000000) # Interrupt number is here self.eu.IS = 0b1 self.eu.S = 0x40000100 self.eu.IVR = 0x40000050 self.mioc.memoryWriteAtAddressForLength(self.eu.IVR, 4, 0xAAAAAAAA) self.mioc.memoryWriteAtAddressForLength(self.eu.IVR + 4, 4, 0xBBBBBBBB) sMarker = self.eu.S iMarker = self.eu.I self.eu.execute() self.assertEqual(self.eu.I, 0xAAAAAAAA, "INT when IS is 1 broken - Bad I address") self.assertEqual(self.eu.S, sMarker + 4, "INT when IS is 1 broken - Bad stack") topStack = self.mioc.memoryReadAtAddressForLength(self.eu.S, 4) self.assertEqual(iMarker + 5, topStack, "INT when IS is 1 broken - Bad return address") # testing the second vector entry self.eu.setupCore(MEMORY_START_AT) self.mioc.memoryWriteAtAddressForLength(MEMORY_START_AT, 1, 0b10000011) self.mioc.memoryWriteAtAddressForLength( MEMORY_START_AT + 1, 4, 0b00000001) # Interrupt number is here self.eu.IS = 0b1 self.eu.S = 0x40000100 self.eu.IVR = 0x40000050 self.mioc.memoryWriteAtAddressForLength(self.eu.IVR, 4, 0xAAAAAAAA) self.mioc.memoryWriteAtAddressForLength(self.eu.IVR + 4, 4, 0xBBBBBBBB) sMarker = self.eu.S iMarker = self.eu.I self.eu.execute() self.assertEqual(self.eu.I, 0xBBBBBBBB, "INT when IS is 1 broken - Bad I address") self.assertEqual(self.eu.S, sMarker + 4, "INT when IS is 1 broken - Bad stack") topStack = self.mioc.memoryReadAtAddressForLength(self.eu.S, 4) self.assertEqual(iMarker + 5, topStack, "INT when IS is 1 broken - Bad return address") def test_reset(self): self.eu.setupCore(MEMORY_START_AT) self.eu.A = 1 self.eu.B = 1 self.eu.C = 1 self.eu.D = 1 self.eu.E = 1 self.eu.F = 1 self.eu.G = 1 self.eu.A2 = 1 self.eu.B2 = 1 self.eu.C2 = 1 self.eu.D2 = 1 self.eu.E2 = 1 self.eu.F2 = 1 self.eu.G2 = 1 self.eu.S = 1 self.eu.S2 = 1 self.eu.IS = 1 self.eu.IVR = 1 self.eu.FLAGS = 1 self.eu.reset(0) # Making sure we can set I to value requested count = self.eu.A + self.eu.B + self.eu.C + self.eu.D + self.eu.E + self.eu.F + self.eu.G + \ self.eu.A2 + self.eu.B2 + self.eu.C2 + self.eu.D2 + self.eu.E2 + self.eu.F2 + self.eu.G2 + \ self.eu.S + self.eu.S2 + self.eu.IS +self.eu.IVR + self.eu.I + self.eu.FLAGS self.assertEqual(count, 0) self.eu.setupCore(MEMORY_START_AT) self.eu.A = 1 self.eu.B = 1 self.eu.C = 1 self.eu.D = 1 self.eu.E = 1 self.eu.F = 1 self.eu.G = 1 self.eu.A2 = 1 self.eu.B2 = 1 self.eu.C2 = 1 self.eu.D2 = 1 self.eu.E2 = 1 self.eu.F2 = 1 self.eu.G2 = 1 self.eu.S = 1 self.eu.S2 = 1 self.eu.IS = 1 self.eu.IVR = 1 self.eu.FLAGS = 1 self.eu.reset( ) # Making sure we can rely on the default value for I after reset count = self.eu.A + self.eu.B + self.eu.C + self.eu.D + self.eu.E + self.eu.F + self.eu.G + \ self.eu.A2 + self.eu.B2 + self.eu.C2 + self.eu.D2 + self.eu.E2 + self.eu.F2 + self.eu.G2 + \ self.eu.S + self.eu.S2 + self.eu.IS + self.eu.IVR + self.eu.I + self.eu.FLAGS self.assertEqual(count, MEMORY_START_AT)