Skip to content

Commit

Permalink
broken disassembler
Browse files Browse the repository at this point in the history
  • Loading branch information
kanzure committed Jun 11, 2012
1 parent 1e16243 commit b19db1e
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions extras/romstr.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def __init__(self, start_address=None, end_address=None, size=None, max_size=0x4
# check more edge cases
if not start_address >= 0:
raise Exception, "start_address must be at least 0"
elif not end_address >= 0:
elif end_address != None and not end_address >= 0:
raise Exception, "end_address must be at least 0"

self.rom = rom
Expand Down Expand Up @@ -157,7 +157,7 @@ def parse(self):

keep_reading = True

while offset <= end_address and keep_reading:
while (end_address != 0 and offset <= end_address) or keep_reading:
# read the current opcode byte
current_byte = ord(rom[offset])
current_byte_number = len(asm_commands.keys())
Expand All @@ -176,11 +176,19 @@ def parse(self):
# label later.
asm_command["references"] = 0

print "debug1"

# some commands have two opcodes
next_byte = ord(rom[offset+1])

print "offset: \t\t" + hex(offset)
print "current_byte: \t\t" + hex(current_byte)
print "next_byte: \t\t" + hex(next_byte)

# all two-byte opcodes also have their first byte in there somewhere
if current_byte in opt_table.keys():
if (current_byte in opt_table.keys()) or ((current_byte + (next_byte << 8)) in opt_table.keys()):
print "debug2"

# this might be a two-byte opcode
possible_opcode = current_byte + (next_byte << 8)

Expand All @@ -195,12 +203,15 @@ def parse(self):
opstr = op[0].lower()
optype = op[1]

print "opstr: " + opstr

asm_command["type"] = "op"
asm_command["id"] = op_code
asm_command["format"] = opstr
asm_command["opnumberthing"] = optype

if "x" in opstr:
print "debug3"
for x in range(0, opstr.count("x")):
insertion = ord(rom[offset + 1])

Expand All @@ -215,6 +226,7 @@ def parse(self):
offset += 1

if "?" in opstr:
print "debug4"
for y in range(0, opstr.count("?")):
byte1 = ord(rom[offset + 1])
byte2 = ord(rom[offset + 2])
Expand All @@ -234,17 +246,21 @@ def parse(self):
# Check for relative jumps, construct the formatted asm line.
# Also set the usage of labels.
if current_byte in [0x18, 0x20] or current_byte in relative_jumps: # jr or jr nz
print "debug5"

# generate a label for the byte we're jumping to
target_address = offset + 2 + c_int8(ord(rom[offset + 1])).value

if target_address in asm_commands.keys():
print "debug6"
asm_commands[target_address]["references"] += 1
remote_label = "asm_" + hex(target_address)
asm_commands[target_address]["current_label"] = remote_label
asm_command["remote_label"] = remote_label

asm_command["use_remote_label"] = True
else:
print "debug7"
remote_label = "asm_" + hex(target_address)

# This remote address might not be part of this
Expand Down Expand Up @@ -281,6 +297,7 @@ def parse(self):
used_3d97 = True

if current_byte == 0xc3 or current_byte in relative_unconditional_jumps:
print "debug8"
if current_byte == 0xc3:
if number == 0x3d97:
used_3d97 = True
Expand All @@ -291,6 +308,7 @@ def parse(self):

# stop reading at a jump, relative jump or return
if current_byte in end_08_scripts_with:
print "debug9"
is_data = False

if not has_outstanding_labels(byte_labels) and all_outstanding_labels_are_reverse(byte_labels, offset):
Expand All @@ -306,9 +324,12 @@ def parse(self):
# ROM probably doesn't represent instructions.
asm_command["type"] = "data" # db
asm_command["value"] = current_byte
keep_reading = False

# save this new command in the list
asm_commands[asm_command["address"]] = asm_command
# save this new command in the list
asm_commands[asm_command["address"]] = asm_command
self.asm_commands = asm_commands
print "debug10"

def __str__(self):
""" ASM pretty printer.
Expand Down

0 comments on commit b19db1e

Please sign in to comment.