fixed disassembler to compute the correct branch target

This commit is contained in:
Wei-ju Wu 2014-12-04 21:17:55 -08:00
parent 2bcaf5188a
commit 7fd8edb194
5 changed files with 60 additions and 29 deletions

View File

@ -1,25 +1,25 @@
ASM_FLAGS = -Fhunk -devpac -I/home/weiju/Development/NDK_3.9/Include/include_i
ASM = vasmm68k_mot
.SUFFIXES : .o .asm
all: test1 hello test2
all: test1 hello test2 mouse raster1
.asm.o:
$(ASM) $(ASM_FLAGS) -o $@ $<
clean:
rm -f test1 test2 hello *.o
rm -f test1 test2 hello mouse *.o
test1: test1.o
vlink -bamigahunk -o test1 -s test1.o
test1.o: test1.asm
$(ASM) $(ASM_FLAGS) -o test1.o test1.asm
vlink -bamigahunk -o $@ -s $<
test2: test2.o
vlink -bamigahunk -o test2 -s test2.o
test2.o: test2.asm
$(ASM) $(ASM_FLAGS) -o test2.o test2.asm
vlink -bamigahunk -o $@ -s $<
hello: hello.o
vlink -bamigahunk -o hello -s hello.o
vlink -bamigahunk -o $@ -s $<
hello.o: hello.asm
$(ASM) $(ASM_FLAGS) -o hello.o hello.asm
mouse: mouse.o
vlink -bamigahunk -o $@ -s $<
raster1: raster1.o
vlink -bamigahunk -o $@ -s $<

5
assembler/mouse.asm Normal file
View File

@ -0,0 +1,5 @@
waitmouse
btst #6,$bfe001
bne waitmouse
rts

15
assembler/raster1.asm Normal file
View File

@ -0,0 +1,15 @@
mainloop:
waitras1:
cmp.b #$ac,$dff006
bne waitras1
move.w #$fff,$dff180
waitras2:
cmp.b #$ac,$dff006
beq waitras2
move.w #$116,$dff180
btst #6,$bfe001
bne mainloop
rts

View File

@ -16,7 +16,7 @@ OPCODE_CATEGORIES = {
'0000': 'bitops_movep_imm', '0001': 'move.b', '0010': 'move.l', '0011': 'move.w',
'0100': 'misc', '0101': 'addq_subq', '1001': 'sub_subx',
'0110': 'bcc_bsr_bra', '0111': 'moveq',
'1101': 'add_addx'
'1101': 'add_addx', '1110': 'shift_rotate'
}
ADD_SUB_OPMODES = {
@ -187,7 +187,7 @@ def next_word(size, data, data_offset):
return (value, added)
def operand(size, mode_bits, reg_bits, data, offset):
def operand(size, mode_bits, reg_bits, data, offset, skip=0):
result = ""
added = 0
mode = ADDR_MODES[mode_bits]
@ -197,19 +197,19 @@ def operand(size, mode_bits, reg_bits, data, offset):
mode = ADDR_MODES_EXT[reg_bits]
regnum = int(reg_bits, 2)
if mode == '#<data>':
imm_value, added = next_word(size, data, offset + 2)
imm_value, added = next_word(size, data, offset + 2 + skip)
result = IntConstant(imm_value)
elif mode in {'(xxx).L', '(xxx).W'}: # absolute
addr, added = next_word(mode[-1], data, offset + 2)
addr, added = next_word(mode[-1], data, offset + 2 + skip)
result = "%d.%s" % (addr, mode[-1])
elif mode == '(d16,PC)':
disp16, added = next_word('W', data, offset + 2)
disp16, added = next_word('W', data, offset + 2 + skip)
result = "%d(PC)" % disp16
else:
raise Exception("unsupported ext mode: '%s'" % mode)
elif mode == '(d16,An)':
regnum = int(reg_bits, 2)
disp16, added = next_word('W', data, offset + 2)
disp16, added = next_word('W', data, offset + 2 + skip)
result = AddressRegisterIndirectDisplacement(regnum, disp16)
elif mode == 'An':
result = AddressRegister(int(reg_bits, 2))
@ -338,9 +338,19 @@ def _disassemble(data, offset):
instr = Operation2('addq', added + 2, IntConstant(value), ea)
else:
raise Exception('TODO addq_subq etc')
elif category == 'bitops_movep_imm':
if bits[0:10] == '0000100000':
ea, added1 = operand('l', bits[10:13], bits[13:16], data, offset, skip=2)
bitnum, added2 = next_word('W', data, offset + 2)
instr = Operation2('btst', added1 + added2 + 2, bitnum & 0xff, ea)
else:
detail = bits[8:11]
print("bits at offset: %d -> %s" % (offset, bits))
raise Exception('TODO: bitops, detail: ' + detail)
else:
print("\nUnknown instruction\nCategory: ", category, " Bits: ", bits)
raise Exception('TODO')
#print("%d: %s" % (offset, instr))
return instr
@ -386,7 +396,9 @@ def disassemble(code):
# the problem is that we need to be able to tell local
# from global branches
if instr.is_local_branch():
branch_dest = offset + instr.size + instr.displacement
# note that the branch target is computed based on the address after the
# 16 bit opcode, ignoring additional extension words in the displacement
branch_dest = offset + 2 + instr.displacement
if not branch_dest in seen:
reachable.append(branch_dest)

View File

@ -173,22 +173,21 @@ def parse_hunkfile(hunkfile):
names = ','.join([block[0] for block in group])
print("Group %d -> %s" % (i, names))
"""
for i, block in enumerate(blocks):
block = group[0]
if block[0] == 'NAME':
print("%d: '%s' -> '%s'" % (i, block[0], block[1]))
elif block[0] == 'BSS':
print("%d: '%s' -> %d" % (i, block[0], block[1]))
elif block[0] == 'CODE':
print("%d: '%s', size = %d" % (i, block[0], len(block[1])))
#print("----------------------------\n")
#code = block[1]
#disassemble(code)
#print("\n---------------------------\n")
elif block[0] == 'RELOC32':
print("%d: '%s': %s" % (i, block[0], block[1]))
print("----------------------------\n")
code = block[1]
disassemble(code)
print("\n---------------------------\n")
#elif block[0] == 'RELOC32':
# print("%d: '%s': %s" % (i, block[0], block[1]))
else:
print("%d: '%s'" % (i, block[0]))"""
print("%d: '%s'" % (i, block[0]))
if __name__ == '__main__':