Пример #1
0
class TestFastDisasm(object):
    def get_disasm(self):
        return Basic6502Disassembler()

    def setup(self):
        self.editor = MockHexEditor()
        guess = FileGuess("../test_data/pytest.atr")
        self.editor.load(guess)
        self.disasm = self.get_disasm()
        self.fast = self.disasm.fast

    def test_ranges(self):
        # force the use of the normal disassembler for the other style
        self.disasm.fast.chunk_type_processor[
            64] = self.disasm.fast.chunk_processor
        self.editor.find_segment("02: robots I")
        s = self.editor.segment
        r = fast_get_entire_style_ranges(s,
                                         user=user_bit_mask,
                                         split_comments=[])
        print(r)
        assert r == [((0, 497), 0), ((497, 524), 1), ((524, 602), 0),
                     ((602, 690), 1), ((690, 1004), 0), ((1004, 1024), 1),
                     ((1024, 1536), 0), ((1536, 1710), 1), ((1710, 1792), 0),
                     ((1792, 1954), 1), ((1954, 2048), 0)]
        info_all = self.fast.get_all(s.rawdata.unindexed_data, s.origin, 0)
        #print info_all.instructions[0:20]
        info_sections = self.fast.get_all(s.rawdata.unindexed_data, s.origin,
                                          0, r)
        # for i in range(info_sections.num_instructions):
        #     print info_sections[i].instruction
        assert len(info_all.instructions) == len(info_sections.instructions)
        assert np.all(info_all.index_to_row - info_sections.index_to_row == 0)
Пример #2
0
class TestXex(object):
    def setup(self):
        self.editor = MockHexEditor()
        guess = FileGuess("../test_data/air_defense_v18.atr")
        self.editor.load(guess)

    def test_simple(self):
        d = self.editor.document
        print(d, len(d), d.segments)
        for s in d.segments:
            print(s)
        source = []
        code_seg = d.find_segment_by_name("program code")
        runad_seg = d.find_segment_by_name("runad")
        source.append(code_seg)
        source.append(runad_seg)
        main, sub = get_xex(source)
        print(main, sub)
        print(len(code_seg))
        assert len(sub[0]) == len(code_seg) + 4
        assert len(sub[1]) == len(runad_seg) + 4
        print(list(d.container_segment.iter_comments_in_segment()))
        for i, c in code_seg.iter_comments_in_segment():
            print(i, c)
            assert c == sub[0].get_comment(i + 4)

        newdoc = SegmentedDocument.create_from_segments(main, sub)
        d = {}
        newdoc.serialize_extra_to_dict(d)
        print(d)
        for i, c in d['comments']:
            print(i, c)
            assert c == main.get_comment(i)
        self.editor.save("out.air_defense.atr", document=newdoc)
Пример #3
0
class TestFastDisasmMulti(object):
    def get_disasm(self):
        z = BasicZ80Disassembler()
        parent = Basic6502Disassembler()
        # Use the Z80 processor for style type 64
        parent.fast.chunk_type_processor[1] = z.fast.chunk_processor
        return parent

    def setup(self):
        self.editor = MockHexEditor()
        guess = FileGuess("../test_data/pytest.atr")
        self.editor.load(guess)
        self.disasm = self.get_disasm()
        self.fast = self.disasm.fast

    def test_ranges(self):
        self.editor.find_segment("boot code at $0800")
        s = self.editor.segment
        r = fast_get_entire_style_ranges(s, user=user_bit_mask, split_comments=[])
        print(r)
        assert r == [((0, 238), 0),
           ((238, 268), 2),
           ((268, 332), 0),
           ((332, 464), 1),
           ((464, 512), 0)]

        info_sections = self.fast.get_all(s.rawdata.unindexed_data, s.origin, 0, r)
Пример #4
0
class TestFastDisasmMulti(object):
    def get_disasm(self):
        z = BasicZ80Disassembler()
        parent = Basic6502Disassembler()
        # Use the Z80 processor for style type 64
        parent.fast.chunk_type_processor[1] = z.fast.chunk_processor
        return parent

    def setup(self):
        self.editor = MockHexEditor()
        guess = FileGuess("../test_data/pytest.atr")
        self.editor.load(guess)
        self.disasm = self.get_disasm()
        self.fast = self.disasm.fast

    def test_ranges(self):
        self.editor.find_segment("boot code at $0800")
        s = self.editor.segment
        r = fast_get_entire_style_ranges(s, user=user_bit_mask, split_comments=[])
        print(r)
        assert r == [((0, 238), 0),
           ((238, 268), 2),
           ((268, 332), 0),
           ((332, 464), 1),
           ((464, 512), 0)]

        info_sections = self.fast.get_all(s.rawdata.unindexed_data, s.origin, 0, r)
Пример #5
0
 def setup(self):
     self.editor = MockHexEditor()
     guess = FileGuess("../test_data/reassembly-test.xex")
     self.editor.load(guess)
     print(self.editor.document.segments)
     self.segment = self.editor.document.segments[0][6:6 + 0x125]
     print(self.segment)
Пример #6
0
 def setup(self):
     data = np.arange(256, dtype=np.uint8)
     style = np.zeros(256, dtype=np.uint8)
     raw = SegmentData(data, style)
     s = DefaultSegment(raw, 0)
     s.set_style_ranges([(10,20),], data=True)
     s.set_style_ranges([(30,40),], user=1)
     s.set_comment([(0,5)], "comment 0-5")
     s.set_comment([(15,16)], "comment 15-16")
     s.set_comment([(18,24)], "comment 18,24")
     s.set_comment([(38,42)], "comment 38,42")
     s.set_comment([(51,55)], "comment 51,55")
     self.editor = MockHexEditor(segment=s)
Пример #7
0
 def setup(self):
     self.editor = MockHexEditor()
     guess = FileGuess("../test_data/reassembly-test.xex")
     self.editor.load(guess)
     print(self.editor.document.segments)
     self.segment = self.editor.document.segments[0][6:6 + 0x125]
     print(self.segment)
Пример #8
0
class TestCopyPaste(object):
    def setup(self):
        data = np.arange(256, dtype=np.uint8)
        style = np.zeros(256, dtype=np.uint8)
        raw = SegmentData(data, style)
        s = DefaultSegment(raw, 0)
        s.set_style_ranges([(10,20),], data=True)
        s.set_style_ranges([(30,40),], user=1)
        s.set_comment([(0,5)], "comment 0-5")
        s.set_comment([(15,16)], "comment 15-16")
        s.set_comment([(18,24)], "comment 18,24")
        s.set_comment([(38,42)], "comment 38,42")
        s.set_comment([(51,55)], "comment 51,55")
        self.editor = MockHexEditor(segment=s)

    def test_metadata(self):
        s = self.editor.segment
        indexes = np.asarray([4,5,8,9,10,11,12,13,28,29,30,31,32,40,41,50,51,52])
        j = self.editor.get_selected_index_metadata(indexes)
        style, where_comments, comments = self.editor.restore_selected_index_metadata(j)
        print(style)
        s1 = s.get_style_at_indexes(indexes)
        assert np.all((s1 - style) == 0)
        print("comments from source", where_comments, comments)

        dest = 100
        last = dest + len(style)
        dest_indexes = ranges_to_indexes([(dest, last)])
        print("dest indexes", dest, last, dest_indexes)
        assert len(dest_indexes) == len(style)
        s.style[dest_indexes] = style
        assert np.all(s.style[dest_indexes] - style == 0)
        print("dest comment indexes", dest_indexes[where_comments])
        s.set_comments_at_indexes(dest_indexes[where_comments], comments)
        j2 = self.editor.get_selected_index_metadata(dest_indexes)
        style2, where_comments2, comments2 = self.editor.restore_selected_index_metadata(j2)
        print("comments from dest", where_comments2, comments2)
        for w, w2 in zip(where_comments, where_comments2):
            assert w == w2
        assert comments == comments2

    def test_command(self):
        s = self.editor.segment
        ranges = [(100,120)]
        dest_indexes = ranges_to_indexes(ranges)
        source_indexes = ranges_to_indexes([(20, 60)])
        common = min(len(dest_indexes), len(source_indexes))
        j = self.editor.get_selected_index_metadata(source_indexes)
        style, where_comments, comments = self.editor.restore_selected_index_metadata(j)
        cmd = PasteCommand(s, ranges, -1, s[source_indexes], source_indexes, style, where_comments, comments)
        cmd.perform(self.editor)

        j2 = self.editor.get_selected_index_metadata(dest_indexes)
        style2, where_comments2, comments2 = json.loads(j2)
        assert np.all(style[0:common] - style2[0:common] == 0)
Пример #9
0
class TestCopyPaste(object):
    def setup(self):
        data = np.arange(256, dtype=np.uint8)
        style = np.zeros(256, dtype=np.uint8)
        raw = SegmentData(data, style)
        s = DefaultSegment(raw, 0)
        s.set_style_ranges([(10,20),], data=True)
        s.set_style_ranges([(30,40),], user=1)
        s.set_comment([(0,5)], "comment 0-5")
        s.set_comment([(15,16)], "comment 15-16")
        s.set_comment([(18,24)], "comment 18,24")
        s.set_comment([(38,42)], "comment 38,42")
        s.set_comment([(51,55)], "comment 51,55")
        self.editor = MockHexEditor(segment=s)

    def test_metadata(self):
        s = self.editor.segment
        indexes = np.asarray([4,5,8,9,10,11,12,13,28,29,30,31,32,40,41,50,51,52])
        j = self.editor.get_selected_index_metadata(indexes)
        style, where_comments, comments = self.editor.restore_selected_index_metadata(j)
        print style
        s1 = s.get_style_at_indexes(indexes)
        assert np.all((s1 - style) == 0)
        print "comments from source", where_comments, comments

        dest = 100
        last = dest + len(style)
        dest_indexes = ranges_to_indexes([(dest, last)])
        print "dest indexes", dest, last, dest_indexes
        assert len(dest_indexes) == len(style)
        s.style[dest_indexes] = style
        assert np.all(s.style[dest_indexes] - style == 0)
        print "dest comment indexes", dest_indexes[where_comments]
        s.set_comments_at_indexes(dest_indexes[where_comments], comments)
        j2 = self.editor.get_selected_index_metadata(dest_indexes)
        style2, where_comments2, comments2 = self.editor.restore_selected_index_metadata(j2)
        print "comments from dest", where_comments2, comments2
        for w, w2 in zip(where_comments, where_comments2):
            assert w == w2
        assert comments == comments2

    def test_command(self):
        s = self.editor.segment
        ranges = [(100,120)]
        dest_indexes = ranges_to_indexes(ranges)
        source_indexes = ranges_to_indexes([(20, 60)])
        common = min(len(dest_indexes), len(source_indexes))
        j = self.editor.get_selected_index_metadata(source_indexes)
        style, where_comments, comments = self.editor.restore_selected_index_metadata(j)
        cmd = PasteCommand(s, ranges, -1, s[source_indexes], source_indexes, style, where_comments, comments)
        cmd.perform(self.editor)

        j2 = self.editor.get_selected_index_metadata(dest_indexes)
        style2, where_comments2, comments2 = json.loads(j2)
        assert np.all(style[0:common] - style2[0:common] == 0)
Пример #10
0
class TestAssemble(object):
    def setup(self):
        self.editor = MockHexEditor()
        guess = FileGuess("../test_data/reassembly-test.xex")
        self.editor.load(guess)
        print(self.editor.document.segments)
        self.segment = self.editor.document.segments[0][6:6 + 0x125]
        print(self.segment)

    def test_reassemble(self):
        source = "../test_data/reassembly-test.s"
        asm = Assemble(source)
        assert asm is not None
        assert len(asm) == 1
        assert len(asm.segments) == 1
        start, end, source_bytes = asm.segments[0]
        assert len(source_bytes) == len(self.segment)
        print(source_bytes)
        source_data = np.asarray(source_bytes, dtype=np.uint8)
        print(source_data)
        segment_data = np.asarray(self.segment, dtype=np.uint8)
        assert np.all(source_data - segment_data == 0)
Пример #11
0
class TestChunkBreak(object):
    def get_disasm(self):
        disasm = Basic6502Disassembler()
        disasm.add_chunk_processor("data", 1)
        disasm.add_chunk_processor("antic_dl", 2)
        disasm.add_chunk_processor("jumpman_level", 3)
        disasm.add_chunk_processor("jumpman_harvest", 4)
        return disasm

    def setup(self):
        self.disasm = self.get_disasm()
        self.editor = MockHexEditor()
        guess = FileGuess("../test_data/pytest.atr")
        self.editor.load(guess)

    def test_simple(self):
        self.editor.find_segment("chunk type changes")
        s = self.editor.segment
        r = fast_get_entire_style_ranges(s, user=user_bit_mask)
        print(r)
        info = self.disasm.disassemble_segment(s)
        inst = info.instructions
        for i in range(info.num_instructions):
            print(info[i].instruction)

        assert info[0].instruction.startswith("DEX")
        assert info[2].instruction.startswith("RTS")
        assert info[4].instruction == "00"
        assert info[5].instruction.startswith("7070707070;")

    def test_recompile(self):
        self.editor.find_segment("modified boot")
        s = self.editor.segment
        info = self.disasm.disassemble_segment(s)
        disasm = self.disasm.get_disassembled_text()
        with open("%s.s" % s.name, "w") as fh:
            fh.write("\n".join(disasm) + "\n")
        text1 = self.disasm.get_atasm_lst_text()
        with open("%s.omnivore-lst" % s.name, "w") as fh:
            fh.write("\n".join(text1) + "\n")

    def test_bad(self):
        data = np.fromstring(
            "\x8dp0L\xaa8\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00pppM\x00p\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\x8d\x8d\x06\x16\x8e\r",
            dtype=np.uint8)
        style = np.empty(len(data), dtype=np.uint8)
        style[0:17] = 0
        style[17:] = 2
        raw = SegmentData(data, style)
        s = DefaultSegment(raw, 0x3bef)
        info = self.disasm.disassemble_segment(s)
        inst = info.instructions
        for i in range(info.num_instructions):
            print(info[i].instruction)
        text = self.disasm.get_disassembled_text()
        print("\n".join(text))

        text = self.disasm.get_atasm_lst_text()
        print("\n".join(text))
Пример #12
0
class TestAssemble(object):
    def setup(self):
        self.editor = MockHexEditor()
        guess = FileGuess("../test_data/reassembly-test.xex")
        self.editor.load(guess)
        print(self.editor.document.segments)
        self.segment = self.editor.document.segments[0][6:6 + 0x125]
        print(self.segment)

    def test_reassemble(self):
        source = "../test_data/reassembly-test.s"
        asm = Assemble(source)
        assert asm is not None
        assert len(asm) == 1
        assert len(asm.segments) == 1
        start, end, source_bytes = asm.segments[0]
        assert len(source_bytes) == len(self.segment)
        print(source_bytes)
        source_data = np.asarray(source_bytes, dtype=np.uint8)
        print(source_data)
        segment_data = np.asarray(self.segment, dtype=np.uint8)
        assert np.all(source_data - segment_data == 0)
Пример #13
0
 def setup(self):
     data = np.arange(256, dtype=np.uint8)
     style = np.zeros(256, dtype=np.uint8)
     raw = SegmentData(data, style)
     s = DefaultSegment(raw, 0)
     s.set_style_ranges([(10,20),], data=True)
     s.set_style_ranges([(30,40),], user=1)
     s.set_comment([(0,5)], "comment 0-5")
     s.set_comment([(15,16)], "comment 15-16")
     s.set_comment([(18,24)], "comment 18,24")
     s.set_comment([(38,42)], "comment 38,42")
     s.set_comment([(51,55)], "comment 51,55")
     self.editor = MockHexEditor(segment=s)
Пример #14
0
class TestFastDisasm(object):
    def get_disasm(self):
        return Basic6502Disassembler()

    def setup(self):
        self.editor = MockHexEditor()
        guess = FileGuess("../test_data/pytest.atr")
        self.editor.load(guess)
        self.disasm = self.get_disasm()
        self.fast = self.disasm.fast

    def test_ranges(self):
        # force the use of the normal disassembler for the other style
        self.disasm.fast.chunk_type_processor[64] = self.disasm.fast.chunk_processor
        self.editor.find_segment("02: robots I")
        s = self.editor.segment
        r = fast_get_entire_style_ranges(s, user=user_bit_mask, split_comments=[])
        print(r)
        assert r == [
        ((0, 497), 0),
        ((497, 524), 1),
        ((524, 602), 0),
        ((602, 690), 1),
        ((690, 1004), 0),
        ((1004, 1024), 1),
        ((1024, 1536), 0),
        ((1536, 1710), 1),
        ((1710, 1792), 0),
        ((1792, 1954), 1),
        ((1954, 2048), 0)]
        info_all = self.fast.get_all(s.rawdata.unindexed_data, s.origin, 0)
        #print info_all.instructions[0:20]
        info_sections = self.fast.get_all(s.rawdata.unindexed_data, s.origin, 0, r)
        # for i in range(info_sections.num_instructions):
        #     print info_sections[i].instruction
        assert len(info_all.instructions) == len(info_sections.instructions)
        assert np.all(info_all.index_to_row - info_sections.index_to_row == 0)
Пример #15
0
class TestChunkBreak(object):
    def get_disasm(self):
        disasm = Basic6502Disassembler()
        disasm.add_chunk_processor("data", 1)
        disasm.add_chunk_processor("antic_dl", 2)
        disasm.add_chunk_processor("jumpman_level", 3)
        disasm.add_chunk_processor("jumpman_harvest", 4)
        return disasm

    def setup(self):
        self.disasm = self.get_disasm()
        self.editor = MockHexEditor()
        guess = FileGuess("../test_data/pytest.atr")
        self.editor.load(guess)

    def test_simple(self):
        self.editor.find_segment("chunk type changes")
        s = self.editor.segment
        r = fast_get_entire_style_ranges(s, user=user_bit_mask)
        print(r)
        info = self.disasm.disassemble_segment(s)
        inst = info.instructions
        for i in range(info.num_instructions):
            print(info[i].instruction)

        assert info[0].instruction.startswith("DEX")
        assert info[2].instruction.startswith("RTS")
        assert info[4].instruction == "00"
        assert info[5].instruction.startswith("7070707070;")

    def test_recompile(self):
        self.editor.find_segment("modified boot")
        s = self.editor.segment
        info = self.disasm.disassemble_segment(s)
        disasm = self.disasm.get_disassembled_text()
        with open("%s.s" % s.name, "w") as fh:
            fh.write("\n".join(disasm) + "\n")
        text1 = self.disasm.get_atasm_lst_text()
        with open("%s.omnivore-lst" % s.name, "w") as fh:
            fh.write("\n".join(text1) + "\n")

    def test_bad(self):
        data = np.fromstring("\x8dp0L\xaa8\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00pppM\x00p\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\x8d\x8d\x06\x16\x8e\r", dtype=np.uint8)
        style = np.empty(len(data), dtype=np.uint8)
        style[0:17] = 0
        style[17:] = 2
        raw = SegmentData(data, style)
        s = DefaultSegment(raw, 0x3bef)
        info = self.disasm.disassemble_segment(s)
        inst = info.instructions
        for i in range(info.num_instructions):
            print(info[i].instruction)
        text = self.disasm.get_disassembled_text()
        print("\n".join(text))

        text = self.disasm.get_atasm_lst_text()
        print("\n".join(text))
Пример #16
0
 def setup(self):
     self.editor = MockHexEditor()
     guess = FileGuess("../test_data/air_defense_v18.atr")
     self.editor.load(guess)
Пример #17
0
 def setup(self):
     data = np.arange(32, dtype=np.uint8)
     style = np.zeros(32, dtype=np.uint8)
     raw = SegmentData(data, style)
     segment = DefaultSegment(raw, 0)
     self.editor = MockHexEditor(segment=segment)
Пример #18
0
 def setup(self):
     self.editor = MockHexEditor()
     guess = FileGuess("../test_data/pytest.atr")
     self.editor.load(guess)
     self.disasm = self.get_disasm()
     self.fast = self.disasm.fast
Пример #19
0
 def setup(self):
     self.disasm = self.get_disasm()
     self.editor = MockHexEditor()
Пример #20
0
 def setup(self):
     self.editor = MockHexEditor()
     guess = FileGuess("../test_data/pytest.atr")
     self.editor.load(guess)
     self.disasm = self.get_disasm()
     self.fast = self.disasm.fast