def test_value(self): operands, operand_buffer_indices, opcode_length = parse_operand_buffer([ _get_opbyte(OpLen.WORD, OpType.VALUE), 0xFF, 0xFF, 0xFF, 0xFF, ], 1) self.assertEqual(len(operands), 1) self.assertEqual(operands[0].oplen, OpLen.WORD) self.assertEqual(operands[0].optype, OpType.VALUE) self.assertIsNone(operands[0].opreg) self.assertEqual(operands[0].opvalue, 65535) self.assertIsNone(operands[0].opbase) self.assertIsNone(operands[0].opoffset) self.assertListEqual(operand_buffer_indices, [3]) self.assertEqual(opcode_length, 4) operands, operand_buffer_indices, opcode_length = parse_operand_buffer([ _get_opbyte(OpLen.BYTE, OpType.VALUE), 0xFF, 0xFF, 0xFF, 0xFF, ], 1) self.assertEqual(len(operands), 1) self.assertEqual(operands[0].oplen, OpLen.BYTE) self.assertEqual(operands[0].optype, OpType.VALUE) self.assertIsNone(operands[0].opreg) self.assertEqual(operands[0].opvalue, 255) self.assertIsNone(operands[0].opbase) self.assertIsNone(operands[0].opoffset) self.assertListEqual(operand_buffer_indices, [2]) self.assertEqual(opcode_length, 3)
def test_value(self): operands, opcode_length = parse_operand_buffer([ _get_opbyte(OpLen.WORD, OpType.VALUE), 0xFF, 0xFF, 0xFF, 0xFF, ], 1) self.assertEqual(len(operands), 1) self.assertEqual(operands[0].oplen, OpLen.WORD) self.assertEqual(operands[0].optype, OpType.VALUE) self.assertIsNone(operands[0].opreg) self.assertEqual(operands[0].opvalue, 65535) self.assertIsNone(operands[0].opbase) self.assertIsNone(operands[0].opoffset) self.assertEqual(opcode_length, 4) operands, opcode_length = parse_operand_buffer([ _get_opbyte(OpLen.BYTE, OpType.VALUE), 0xFF, 0xFF, 0xFF, 0xFF, ], 1) self.assertEqual(len(operands), 1) self.assertEqual(operands[0].oplen, OpLen.BYTE) self.assertEqual(operands[0].optype, OpType.VALUE) self.assertIsNone(operands[0].opreg) self.assertEqual(operands[0].opvalue, 255) self.assertIsNone(operands[0].opbase) self.assertIsNone(operands[0].opoffset) self.assertEqual(opcode_length, 3)
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_register(self): self.assertListEqual(get_operand_opcode(Token(TokenType.WORD_REGISTER, 'AX', 0)), [ _get_opbyte(OpLen.WORD, OpType.REGISTER, 'AX'), ]) self.assertListEqual(get_operand_opcode(Token(TokenType.BYTE_REGISTER, 'AL', 0)), [ _get_opbyte(OpLen.BYTE, OpType.REGISTER, 'AL'), ]) self.assertListEqual(get_operand_opcode(Token(TokenType.BYTE_REGISTER, 'AH', 0)), [ _get_opbyte(OpLen.BYTE, OpType.REGISTER, 'AH'), ]) with self.assertRaises(InvalidRegisterNameError): get_operand_opcode(Token(TokenType.WORD_REGISTER, 'XX', 0))
def test_abs_ref(self): self.assertListEqual(get_operand_opcode(Token(TokenType.ABS_REF_REG, Reference('BX', 0, 'B'), 0)), [ _get_opbyte(OpLen.BYTE, OpType.ABS_REF_REG, 'BX'), 0x00, ]) self.assertListEqual(get_operand_opcode(Token(TokenType.ABS_REF_REG, Reference('BX', 1, 'B'), 0)), [ _get_opbyte(OpLen.BYTE, OpType.ABS_REF_REG, 'BX'), 0x01, ]) self.assertListEqual(get_operand_opcode(Token(TokenType.ABS_REF_REG, Reference('BX', -1, 'W'), 0)), [ _get_opbyte(OpLen.WORD, OpType.ABS_REF_REG, 'BX'), 0xFF, ]) with self.assertRaises(InvalidRegisterNameError): get_operand_opcode(Token(TokenType.ABS_REF_REG, Reference('XX', 0, 'W'), 0)) with self.assertRaises(ByteOutOfRangeError): get_operand_opcode(Token(TokenType.ABS_REF_REG, Reference('BX', 150, 'W'), 0))
def test_address(self): operands, opcode_length = parse_operand_buffer([ _get_opbyte(OpLen.WORD, OpType.ADDRESS), 0xFF, 0xFF, 0xFF, 0xFF, ], 1) self.assertEqual(len(operands), 1) self.assertEqual(operands[0].oplen, OpLen.WORD) self.assertEqual(operands[0].optype, OpType.ADDRESS) self.assertIsNone(operands[0].opreg) self.assertEqual(operands[0].opvalue, -1) self.assertIsNone(operands[0].opbase) self.assertIsNone(operands[0].opoffset) self.assertEqual(opcode_length, 4) with self.assertRaises(InvalidOperandError): parse_operand_buffer([ _get_opbyte(OpLen.BYTE, OpType.ADDRESS), 0xFF, 0xFF, 0xFF, 0xFF, ], 1)
def test_literal(self): self.assertListEqual(get_operand_opcode(Token(TokenType.WORD_LITERAL, 65535, 0)), [ _get_opbyte(OpLen.WORD, OpType.VALUE), 0xFF, 0xFF, ]) with self.assertRaises(WordOutOfRangeError): get_operand_opcode(Token(TokenType.WORD_LITERAL, 65536, 0)) self.assertListEqual(get_operand_opcode(Token(TokenType.BYTE_LITERAL, 255, 0)), [ _get_opbyte(OpLen.BYTE, OpType.VALUE), 0xFF, ]) with self.assertRaises(ByteOutOfRangeError): get_operand_opcode(Token(TokenType.BYTE_LITERAL, -1, 0)) self.assertListEqual(get_operand_opcode(Token(TokenType.ADDRESS_WORD_LITERAL, -1, 0)), [ _get_opbyte(OpLen.WORD, OpType.ADDRESS), 0xFF, 0xFF, ]) with self.assertRaises(WordOutOfRangeError): get_operand_opcode(Token(TokenType.ADDRESS_WORD_LITERAL, 35000, 0))
def test_rel_ref(self): self.assertListEqual(get_operand_opcode(Token(TokenType.REL_REF_WORD, Reference(-1, 0, 'B'), 0)), [ _get_opbyte(OpLen.BYTE, OpType.REL_REF_WORD), 0xFF, 0xFF, ]) with self.assertRaises(WordOutOfRangeError): get_operand_opcode(Token(TokenType.REL_REF_WORD, Reference(35000, 0, 'B'), 0)) with self.assertRaises(WordOutOfRangeError): get_operand_opcode(Token(TokenType.REL_REF_WORD, Reference(-35000, 0, 'B'), 0)) self.assertListEqual(get_operand_opcode(Token(TokenType.REL_REF_WORD_BYTE, Reference(-1, 255, 'B'), 0)), [ _get_opbyte(OpLen.BYTE, OpType.REL_REF_WORD_BYTE), 0xFF, 0xFF, 0xFF, ]) self.assertListEqual(get_operand_opcode(Token(TokenType.REL_REF_WORD_REG, Reference(-1, 'BX', 'B'), 0)), [ _get_opbyte(OpLen.BYTE, OpType.REL_REF_WORD_REG, 'BX'), 0xFF, 0xFF, ]) with self.assertRaises(InvalidRegisterNameError): get_operand_opcode(Token(TokenType.REL_REF_WORD_REG, Reference(12000, 'XX', 'B'), 0))
def test_abs_ref(self): operands, opcode_length = parse_operand_buffer([ _get_opbyte(OpLen.WORD, OpType.ABS_REF_REG, 'BX'), 0xFF, 0xFF, 0xFF, 0xFF, ], 1) self.assertEqual(len(operands), 1) self.assertEqual(operands[0].oplen, OpLen.WORD) self.assertEqual(operands[0].optype, OpType.ABS_REF_REG) self.assertEqual(operands[0].opreg, 'BX') self.assertIsNone(operands[0].opvalue) self.assertIsNone(operands[0].opbase) self.assertEqual(operands[0].opoffset, -1) self.assertEqual(opcode_length, 3) with self.assertRaises(InvalidRegisterCodeError): parse_operand_buffer([ _get_opbyte(OpLen.WORD, OpType.ABS_REF_REG, 'AH'), 0xFF, 0xFF, 0xFF, 0xFF, ], 1)
def test_rel_ref(self): operands, operand_buffer_indices, opcode_length = parse_operand_buffer([ _get_opbyte(OpLen.WORD, OpType.REL_REF_WORD), 0xFF, 0xFF, 0xFF, 0xFF, ], 1) self.assertEqual(len(operands), 1) self.assertEqual(operands[0].oplen, OpLen.WORD) self.assertEqual(operands[0].optype, OpType.REL_REF_WORD) self.assertIsNone(operands[0].opreg) self.assertIsNone(operands[0].opvalue) self.assertEqual(operands[0].opbase, -1) self.assertIsNone(operands[0].opoffset) self.assertListEqual(operand_buffer_indices, [3]) self.assertEqual(opcode_length, 4) operands, operand_buffer_indices, opcode_length = parse_operand_buffer([ _get_opbyte(OpLen.WORD, OpType.REL_REF_WORD_BYTE), 0xFF, 0xFF, 0xFF, 0xFF, ], 1) self.assertEqual(len(operands), 1) self.assertEqual(operands[0].oplen, OpLen.WORD) self.assertEqual(operands[0].optype, OpType.REL_REF_WORD_BYTE) self.assertIsNone(operands[0].opreg) self.assertIsNone(operands[0].opvalue) self.assertEqual(operands[0].opbase, -1) self.assertEqual(operands[0].opoffset, 255) self.assertListEqual(operand_buffer_indices, [4]) self.assertEqual(opcode_length, 5) operands, operand_buffer_indices, opcode_length = parse_operand_buffer([ _get_opbyte(OpLen.WORD, OpType.REL_REF_WORD_REG, 'BX'), 0xFF, 0xFF, 0xFF, 0xFF, ], 1) self.assertEqual(len(operands), 1) self.assertEqual(operands[0].oplen, OpLen.WORD) self.assertEqual(operands[0].optype, OpType.REL_REF_WORD_REG) self.assertEqual(operands[0].opreg, 'BX') self.assertIsNone(operands[0].opvalue) self.assertEqual(operands[0].opbase, -1) self.assertIsNone(operands[0].opoffset) self.assertListEqual(operand_buffer_indices, [3]) self.assertEqual(opcode_length, 4)
def test_abs_ref(self): operands, operand_buffer_indices, opcode_length = parse_operand_buffer([ _get_opbyte(OpLen.WORD, OpType.ABS_REF_REG, 'BX'), 0xFF, 0xFF, 0xFF, 0xFF, ], 1) self.assertEqual(len(operands), 1) self.assertEqual(operands[0].oplen, OpLen.WORD) self.assertEqual(operands[0].optype, OpType.ABS_REF_REG) self.assertEqual(operands[0].opreg, 'BX') self.assertIsNone(operands[0].opvalue) self.assertIsNone(operands[0].opbase) self.assertEqual(operands[0].opoffset, -1) self.assertListEqual(operand_buffer_indices, [2]) self.assertEqual(opcode_length, 3) with self.assertRaises(InvalidRegisterCodeError): parse_operand_buffer([ _get_opbyte(OpLen.WORD, OpType.ABS_REF_REG, 'AH'), 0xFF, 0xFF, 0xFF, 0xFF, ], 1)
def test_address(self): operands, operand_buffer_indices, opcode_length = parse_operand_buffer([ _get_opbyte(OpLen.WORD, OpType.ADDRESS), 0xFF, 0xFF, 0xFF, 0xFF, ], 1) self.assertEqual(len(operands), 1) self.assertEqual(operands[0].oplen, OpLen.WORD) self.assertEqual(operands[0].optype, OpType.ADDRESS) self.assertIsNone(operands[0].opreg) self.assertEqual(operands[0].opvalue, -1) self.assertIsNone(operands[0].opbase) self.assertIsNone(operands[0].opoffset) self.assertListEqual(operand_buffer_indices, [3]) self.assertEqual(opcode_length, 4) with self.assertRaises(InvalidOperandError): parse_operand_buffer([ _get_opbyte(OpLen.BYTE, OpType.ADDRESS), 0xFF, 0xFF, 0xFF, 0xFF, ], 1)
def test_multiple_operands(self): operands, opcode_length = parse_operand_buffer([ _get_opbyte(OpLen.WORD, OpType.REGISTER, 'BX'), _get_opbyte(OpLen.WORD, OpType.VALUE), 0xFF, 0xFF, 0xFF, 0xFF, ], 2) self.assertEqual(len(operands), 2) self.assertEqual(operands[0].oplen, OpLen.WORD) self.assertEqual(operands[0].optype, OpType.REGISTER) self.assertEqual(operands[0].opreg, 'BX') self.assertIsNone(operands[0].opvalue) self.assertIsNone(operands[0].opbase) self.assertIsNone(operands[0].opoffset) self.assertEqual(operands[1].oplen, OpLen.WORD) self.assertEqual(operands[1].optype, OpType.VALUE) self.assertIsNone(operands[1].opreg) self.assertEqual(operands[1].opvalue, 65535) self.assertIsNone(operands[1].opbase) self.assertIsNone(operands[1].opoffset) self.assertEqual(opcode_length, 5)
def test_multiple_operands(self): operands, operand_buffer_indices, opcode_length = parse_operand_buffer([ _get_opbyte(OpLen.WORD, OpType.REGISTER, 'BX'), _get_opbyte(OpLen.WORD, OpType.VALUE), 0xFF, 0xFF, 0xFF, 0xFF, ], 2) self.assertEqual(len(operands), 2) self.assertEqual(operands[0].oplen, OpLen.WORD) self.assertEqual(operands[0].optype, OpType.REGISTER) self.assertEqual(operands[0].opreg, 'BX') self.assertIsNone(operands[0].opvalue) self.assertIsNone(operands[0].opbase) self.assertIsNone(operands[0].opoffset) self.assertEqual(operands[1].oplen, OpLen.WORD) self.assertEqual(operands[1].optype, OpType.VALUE) self.assertIsNone(operands[1].opreg) self.assertEqual(operands[1].opvalue, 65535) self.assertIsNone(operands[1].opbase) self.assertIsNone(operands[1].opoffset) self.assertListEqual(operand_buffer_indices, [1, 4]) self.assertEqual(opcode_length, 5)
def test_rel_ref(self): operands, opcode_length = parse_operand_buffer([ _get_opbyte(OpLen.WORD, OpType.REL_REF_WORD), 0xFF, 0xFF, 0xFF, 0xFF, ], 1) self.assertEqual(len(operands), 1) self.assertEqual(operands[0].oplen, OpLen.WORD) self.assertEqual(operands[0].optype, OpType.REL_REF_WORD) self.assertIsNone(operands[0].opreg) self.assertIsNone(operands[0].opvalue) self.assertEqual(operands[0].opbase, -1) self.assertIsNone(operands[0].opoffset) self.assertEqual(opcode_length, 4) operands, opcode_length = parse_operand_buffer([ _get_opbyte(OpLen.WORD, OpType.REL_REF_WORD_BYTE), 0xFF, 0xFF, 0xFF, 0xFF, ], 1) self.assertEqual(len(operands), 1) self.assertEqual(operands[0].oplen, OpLen.WORD) self.assertEqual(operands[0].optype, OpType.REL_REF_WORD_BYTE) self.assertIsNone(operands[0].opreg) self.assertIsNone(operands[0].opvalue) self.assertEqual(operands[0].opbase, -1) self.assertEqual(operands[0].opoffset, 255) self.assertEqual(opcode_length, 5) operands, opcode_length = parse_operand_buffer([ _get_opbyte(OpLen.WORD, OpType.REL_REF_WORD_REG, 'BX'), 0xFF, 0xFF, 0xFF, 0xFF, ], 1) self.assertEqual(len(operands), 1) self.assertEqual(operands[0].oplen, OpLen.WORD) self.assertEqual(operands[0].optype, OpType.REL_REF_WORD_REG) self.assertEqual(operands[0].opreg, 'BX') self.assertIsNone(operands[0].opvalue) self.assertEqual(operands[0].opbase, -1) self.assertIsNone(operands[0].opoffset) self.assertEqual(opcode_length, 4)
def test_register(self): operands, operand_buffer_indices, opcode_length = parse_operand_buffer([ _get_opbyte(OpLen.WORD, OpType.REGISTER, 'BX'), 0xFF, 0xFF, 0xFF, 0xFF, ], 1) self.assertEqual(len(operands), 1) self.assertEqual(operands[0].oplen, OpLen.WORD) self.assertEqual(operands[0].optype, OpType.REGISTER) self.assertEqual(operands[0].opreg, 'BX') self.assertIsNone(operands[0].opvalue) self.assertIsNone(operands[0].opbase) self.assertIsNone(operands[0].opoffset) self.assertListEqual(operand_buffer_indices, [1]) self.assertEqual(opcode_length, 2) operands, operand_buffer_indices, opcode_length = parse_operand_buffer([ _get_opbyte(OpLen.BYTE, OpType.REGISTER, 'AH'), 0xFF, 0xFF, 0xFF, 0xFF, ], 1) self.assertEqual(len(operands), 1) self.assertEqual(operands[0].oplen, OpLen.BYTE) self.assertEqual(operands[0].optype, OpType.REGISTER) self.assertEqual(operands[0].opreg, 'AH') self.assertIsNone(operands[0].opvalue) self.assertIsNone(operands[0].opbase) self.assertIsNone(operands[0].opoffset) self.assertListEqual(operand_buffer_indices, [1]) self.assertEqual(opcode_length, 2) with self.assertRaises(InvalidRegisterCodeError): parse_operand_buffer([ _get_opbyte(OpLen.BYTE, OpType.REGISTER, 'BX'), 0xFF, 0xFF, 0xFF, 0xFF, ], 1) with self.assertRaises(InvalidRegisterNameError): parse_operand_buffer([ _get_opbyte(OpLen.WORD, OpType.REGISTER, 'XX'), 0xFF, 0xFF, 0xFF, 0xFF, ], 1)
def test_register(self): operands, opcode_length = parse_operand_buffer([ _get_opbyte(OpLen.WORD, OpType.REGISTER, 'BX'), 0xFF, 0xFF, 0xFF, 0xFF, ], 1) self.assertEqual(len(operands), 1) self.assertEqual(operands[0].oplen, OpLen.WORD) self.assertEqual(operands[0].optype, OpType.REGISTER) self.assertEqual(operands[0].opreg, 'BX') self.assertIsNone(operands[0].opvalue) self.assertIsNone(operands[0].opbase) self.assertIsNone(operands[0].opoffset) self.assertEqual(opcode_length, 2) operands, opcode_length = parse_operand_buffer([ _get_opbyte(OpLen.BYTE, OpType.REGISTER, 'AH'), 0xFF, 0xFF, 0xFF, 0xFF, ], 1) self.assertEqual(len(operands), 1) self.assertEqual(operands[0].oplen, OpLen.BYTE) self.assertEqual(operands[0].optype, OpType.REGISTER) self.assertEqual(operands[0].opreg, 'AH') self.assertIsNone(operands[0].opvalue) self.assertIsNone(operands[0].opbase) self.assertIsNone(operands[0].opoffset) self.assertEqual(opcode_length, 2) with self.assertRaises(InvalidRegisterCodeError): parse_operand_buffer([ _get_opbyte(OpLen.BYTE, OpType.REGISTER, 'BX'), 0xFF, 0xFF, 0xFF, 0xFF, ], 1) with self.assertRaises(InvalidRegisterNameError): parse_operand_buffer([ _get_opbyte(OpLen.WORD, OpType.REGISTER, 'XX'), 0xFF, 0xFF, 0xFF, 0xFF, ], 1)
def test(self): self.assertEqual(_get_opbyte(OpLen.BYTE, OpType.VALUE), 0x00) self.assertEqual(_get_opbyte(OpLen.WORD, OpType.VALUE), 0x80) self.assertEqual(_get_opbyte(OpLen.BYTE, OpType.VALUE, 'AX'), 0x00) self.assertEqual(_get_opbyte(OpLen.WORD, OpType.VALUE, 'AX'), 0x80) self.assertEqual(_get_opbyte(OpLen.BYTE, OpType.VALUE, 'BX'), 0x01) self.assertEqual(_get_opbyte(OpLen.WORD, OpType.VALUE, 'BX'), 0x81) self.assertEqual(_get_opbyte(OpLen.BYTE, OpType.EXTENDED), 0x70) self.assertEqual(_get_opbyte(OpLen.WORD, OpType.EXTENDED), 0xF0) self.assertEqual(_get_opbyte(OpLen.BYTE, OpType.EXTENDED, 'AX'), 0x70) self.assertEqual(_get_opbyte(OpLen.WORD, OpType.EXTENDED, 'AX'), 0xF0) self.assertEqual(_get_opbyte(OpLen.BYTE, OpType.EXTENDED, 'BX'), 0x71) self.assertEqual(_get_opbyte(OpLen.WORD, OpType.EXTENDED, 'BX'), 0xF1)
def test_not_enough_buffer(self): with self.assertRaises(InsufficientOperandBufferError): parse_operand_buffer([ _get_opbyte(OpLen.WORD, OpType.VALUE), 0xFF, ], 1)