def setUp(self): self.instruction_class_1 = Mock() self.instruction_class_1.__name__ = 'INST1' self.instruction_object_1 = Mock() self.instruction_class_1.return_value = self.instruction_object_1 self.instruction_object_1.operands = [ Operand(OpLen.WORD, OpType.VALUE, None, 0xAABB, None, None), ] self.instruction_class_2 = Mock() self.instruction_class_2.__name__ = 'INST2' self.instruction_object_2 = Mock() self.instruction_class_2.return_value = self.instruction_object_2 self.instruction_object_2.operands = [ Operand(OpLen.BYTE, OpType.REGISTER, 'AH', None, None, None), ] self.instruction_set = [ (0x01, self.instruction_class_1), (0x02, self.instruction_class_2), ] self.program = [] self.program += [ self.instruction_set[0][0], _get_opbyte(OpLen.WORD, OpType.VALUE), 0xAA, 0xBB, ] self.program += [ self.instruction_set[1][0], _get_opbyte(OpLen.BYTE, OpType.REGISTER, 'AH'), ] self.system_addresses = { 'entry_point': 0x1234, 'bottom_of_stack': 0xABCD, 'IVT': 0xF000, } self.operand_buffer_size = 16 self.halt_freq = 10000 self.cpu = CPU(self.system_addresses, self.instruction_set, self.operand_buffer_size, self.halt_freq) self.registers = Mock() self.ram = RAM(0x10000) for idx, opcode in enumerate(self.program): self.ram.write_byte(self.system_addresses['entry_point'] + idx, opcode) self.stack = Mock() self.interrupt_controller = Mock() self.device_controller = Mock() self.timer = Mock() self.debugger = None self.cpu.register_architecture( self.registers, self.stack, self.ram, self.interrupt_controller, self.device_controller, self.timer, self.debugger, )
def test_rel_ref_word_byte(self): self.assertEqual(_get_reference_address( Operand(OpLen.BYTE, OpType.REL_REF_WORD_BYTE, None, None, 0x1111, 0x22), self.cpu, 0x1234, ), 0x2367) self.assertEqual(_get_reference_address( Operand(OpLen.BYTE, OpType.REL_REF_WORD_BYTE, None, None, -0x1111, 0x22), self.cpu, 0x1234, ), 0x0145)
def test_rel_ref_word_reg(self): self.cpu.registers.get_register.return_value = 0xA0B0 self.assertEqual(_get_reference_address( Operand(OpLen.BYTE, OpType.REL_REF_WORD_REG, 'AX', None, 0x1111, None), self.cpu, 0x1234, ), 0xC3F5) self.assertEqual(_get_reference_address( Operand(OpLen.BYTE, OpType.REL_REF_WORD_REG, 'AX', None, -0x1111, None), self.cpu, 0x1234, ), 0xA1D3)
def test_address(self): self.assertEqual(get_operand_value( Operand(OpLen.WORD, OpType.ADDRESS, None, 1, None, None), self.cpu, self.ram, 0x1234, ), 0x1235) self.assertEqual(get_operand_value( Operand(OpLen.WORD, OpType.ADDRESS, None, -1, None, None), self.cpu, self.ram, 0x1234, ), 0x1233) self.assertEqual(self.cpu.registers.get_register.call_count, 0) self.assertEqual(self.ram.read_byte.call_count, 0) self.assertEqual(self.ram.read_word.call_count, 0)
def test_abs_ref_reg(self): self.cpu.registers.get_register.return_value = 0xA0B0 self.assertEqual(_get_reference_address( Operand(OpLen.BYTE, OpType.ABS_REF_REG, 'AX', None, None, 0x01), self.cpu, 0x1234, ), 0xA0B1) self.assertEqual(_get_reference_address( Operand(OpLen.BYTE, OpType.ABS_REF_REG, 'AX', None, None, -0x01), self.cpu, 0x1234, ), 0xA0AF)
def test_value(self): self.assertEqual(get_operand_value( Operand(OpLen.BYTE, OpType.VALUE, None, 255, None, None), self.cpu, self.ram, 0x1234, ), 255) self.assertEqual(get_operand_value( Operand(OpLen.WORD, OpType.VALUE, None, 65535, None, None), self.cpu, self.ram, 0x1234, ), 65535) self.assertEqual(self.cpu.registers.get_register.call_count, 0) self.assertEqual(self.ram.read_byte.call_count, 0) self.assertEqual(self.ram.read_word.call_count, 0)
def test_readonly(self): with self.assertRaises(InvalidWriteOperationError): set_operand_value( Operand(OpLen.BYTE, OpType.VALUE, None, 255, None, None), 0x44, self.cpu, self.ram, 0x1234, ) with self.assertRaises(InvalidWriteOperationError): set_operand_value( Operand(OpLen.WORD, OpType.ADDRESS, None, -1, None, None), 0x3344, self.cpu, self.ram, 0x1234, )
def test_rel_ref_word_byte(self): self.assertEqual(get_operand_value( Operand(OpLen.BYTE, OpType.REL_REF_WORD_BYTE, None, None, -0x1111, 0x22), self.cpu, self.ram, 0x1234, ), 0xCC) self.assertEqual(self.cpu.registers.get_register.call_count, 0) self.assertEqual(self.ram.read_byte.call_count, 1) self.assertEqual(self.ram.read_byte.call_args_list[0][0][0], 0x0145) self.assertEqual(self.ram.read_word.call_count, 0)
def test_register(self): self.assertEqual(get_operand_value( Operand(OpLen.WORD, OpType.REGISTER, 'AX', 1, None, None), self.cpu, self.ram, 0x1234, ), 0xA0B0) self.assertEqual(self.cpu.registers.get_register.call_count, 1) self.assertEqual(self.cpu.registers.get_register.call_args_list[0][0][0], 'AX') self.assertEqual(self.ram.read_byte.call_count, 0) self.assertEqual(self.ram.read_word.call_count, 0)
def test_abs_ref_reg_w(self): self.assertEqual(get_operand_value( Operand(OpLen.WORD, OpType.ABS_REF_REG, 'AX', None, None, 0x01), self.cpu, self.ram, 0x1234, ), 0xCCDD) self.assertEqual(self.cpu.registers.get_register.call_count, 1) self.assertEqual(self.cpu.registers.get_register.call_args_list[0][0][0], 'AX') self.assertEqual(self.ram.read_byte.call_count, 0) self.assertEqual(self.ram.read_word.call_count, 1) self.assertEqual(self.ram.read_word.call_args_list[0][0][0], 0xA0B1)
def test_rel_ref_word(self): set_operand_value( Operand(OpLen.BYTE, OpType.REL_REF_WORD, None, None, -0x1111, None), 0x33, self.cpu, self.ram, 0x1234, ) self.assertEqual(self.cpu.registers.get_register.call_count, 0) self.assertEqual(self.ram.write_byte.call_count, 1) self.assertEqual(self.ram.write_byte.call_args_list[0][0][0], 0x0123) self.assertEqual(self.ram.write_byte.call_args_list[0][0][1], 0x33) self.assertEqual(self.ram.write_word.call_count, 0)
def test_abs_ref_reg_b(self): set_operand_value( Operand(OpLen.BYTE, OpType.ABS_REF_REG, 'AX', None, None, 0x01), 0x33, self.cpu, self.ram, 0x1234, ) self.assertEqual(self.cpu.registers.get_register.call_count, 1) self.assertEqual(self.cpu.registers.get_register.call_args_list[0][0][0], 'AX') self.assertEqual(self.ram.write_byte.call_count, 1) self.assertEqual(self.ram.write_byte.call_args_list[0][0][0], 0xA0B1) self.assertEqual(self.ram.write_byte.call_args_list[0][0][1], 0x33) self.assertEqual(self.ram.write_word.call_count, 0)