コード例 #1
0
 def test_create_from_byte_raises_on_empty_byte(self):
     with self.assertRaises(ValueTypeError) as context:
         Value.create_from_byte(b"")
     self.assertEqual("No byte available for reading", str(context.exception))
コード例 #2
0
 def test_value_create_from_str_symbol_correct(self):
     result = Value.create_from_str("symbol", self.instruction)
     self.assertTrue(result.is_symbol())
コード例 #3
0
 def test_value_create_from_str_raises_on_bad_value(self):
     with self.assertRaises(ValueTypeError) as context:
         Value.create_from_str("invalid!", self.instruction)
     self.assertEqual("[invalid!] is an invalid value", str(context.exception))
コード例 #4
0
 def test_value_create_from_str_string_correct(self):
     result = Value.create_from_str("'$DEAD'", self.fcc_instruction)
     self.assertTrue(result.is_string())
     self.assertEqual("$DEAD", result.ascii())
コード例 #5
0
 def test_value_create_from_str_numeric_correct(self):
     result = Value.create_from_str("$DEAD", self.instruction)
     self.assertTrue(result.is_numeric())
     self.assertEqual("DEAD", result.hex())
コード例 #6
0
 def test_value_get_symbol_no_symbol_table_raises(self):
     with self.assertRaises(ValueError) as context:
         Value.get_symbol("blah", {})
     self.assertEqual("[blah] not in symbol table", str(context.exception))
コード例 #7
0
 def test_create_from_string_16_bit_immediate_instruction_size_correct_string_literal(self):
     instruction = Instruction(mnemonic="ZZZ", mode=Mode(imm=0xDE, imm_sz=2), is_16_bit=True)
     result = Value.create_from_str("#'A", instruction)
     self.assertEqual(65, result.int)
     self.assertEqual(4, result.size_hint)
コード例 #8
0
    def translate(self):
        if not self.instruction.mode.ind:
            raise OperandTypeError(
                "Instruction [{}] does not support indexed addressing".format(self.instruction.mnemonic)
            )
        size = self.instruction.mode.ind_sz

        if not type(self.value) == str and self.value.is_address():
            size += 2
            return CodePackage(
                op_code=NumericValue(self.instruction.mode.ind),
                post_byte=NumericValue(0x9F),
                additional=self.value,
                size=size,
                max_size=size,
            )

        if not type(self.value) == str and self.value.is_numeric():
            size += 2
            return CodePackage(
                op_code=NumericValue(self.instruction.mode.ind),
                post_byte=NumericValue(0x9F),
                additional=self.value,
                size=size,
                max_size=size,
            )

        raw_post_byte = 0x80
        post_byte_choices = []
        size = self.instruction.mode.ind_sz
        max_size = size
        additional = NoneValue()
        additional_needs_resolution = False

        if "X" in self.right:
            raw_post_byte |= 0x00
        if "Y" in self.right:
            raw_post_byte |= 0x20
        if "U" in self.right:
            raw_post_byte |= 0x40
        if "S" in self.right:
            raw_post_byte |= 0x60

        if self.left == "":
            if "-" in self.right or "+" in self.right:
                if self.right == "X+" or self.right == "Y+" or self.right == "U+" or self.right == "S+":
                    raise OperandTypeError("[{}] not allowed as an extended indirect value".format(self.right))
                if self.right == "-X" or self.right == "-Y" or self.right == "-U" or self.right == "-S":
                    raise OperandTypeError("[{}] not allowed as an extended indirect value".format(self.right))
                if "++" in self.right:
                    raw_post_byte |= 0x11
                if "--" in self.right:
                    raw_post_byte |= 0x13
            else:
                raw_post_byte |= 0x14

        elif self.left == "A" or self.left == "B" or self.left == "D":
            if self.left == "A":
                raw_post_byte |= 0x16
            if self.left == "B":
                raw_post_byte |= 0x15
            if self.left == "D":
                raw_post_byte |= 0x1B

        else:
            if "+" in self.right or "-" in self.right:
                raise OperandTypeError("[{}] invalid indexed expression".format(self.operand_string))
            if type(self.left) == str:
                self.left = Value.create_from_str(self.left)

            if self.left.is_address():
                additional_needs_resolution = True
                self.left = NumericValue(self.left.int)

            if self.left.is_expression():
                additional_needs_resolution = True

            if self.left.is_address_expression():
                additional_needs_resolution = True

            additional = self.left

            if "PCR" in self.right:
                if additional_needs_resolution:
                    raw_post_byte |= 0x00
                    post_byte_choices = [0x9C, 0x9D]
                    max_size += 2
                else:
                    size += 2 if self.left.is_extended() else 1
                    max_size = size
                    raw_post_byte |= 0x9D if self.left.is_extended() else 0x9C
            else:
                size += additional.byte_len()
                max_size = size
                raw_post_byte |= 0x99 if self.left.is_extended() else 0x98

        return CodePackage(
            op_code=NumericValue(self.instruction.mode.ind),
            post_byte=NumericValue(raw_post_byte),
            post_byte_choices=post_byte_choices,
            additional=additional,
            size=size,
            max_size=max_size,
            additional_needs_resolution=additional_needs_resolution,
        )