Skip to content
This repository
Browse code

broken disassembler

  • Loading branch information...
commit b19db1e6041d593f88a9fb935ca4cf152b39e34c 1 parent 1e16243
Bryan Bishop authored June 10, 2012

Showing 1 changed file with 26 additions and 5 deletions. Show diff stats Hide diff stats

  1. 31  extras/romstr.py
31  extras/romstr.py
@@ -121,7 +121,7 @@ def __init__(self, start_address=None, end_address=None, size=None, max_size=0x4
121 121
         # check more edge cases
122 122
         if not start_address >= 0:
123 123
             raise Exception, "start_address must be at least 0"
124  
-        elif not end_address >= 0:
  124
+        elif end_address != None and not end_address >= 0:
125 125
             raise Exception, "end_address must be at least 0"
126 126
 
127 127
         self.rom           = rom
@@ -157,7 +157,7 @@ def parse(self):
157 157
 
158 158
         keep_reading    = True
159 159
 
160  
-        while offset <= end_address and keep_reading:
  160
+        while (end_address != 0 and offset <= end_address) or keep_reading:
161 161
             # read the current opcode byte
162 162
             current_byte = ord(rom[offset])
163 163
             current_byte_number = len(asm_commands.keys())
@@ -176,11 +176,19 @@ def parse(self):
176 176
                 # label later.
177 177
                 asm_command["references"] = 0
178 178
 
  179
+                print "debug1"
  180
+
179 181
             # some commands have two opcodes
180 182
             next_byte = ord(rom[offset+1])
181 183
 
  184
+            print "offset: \t\t" + hex(offset)
  185
+            print "current_byte: \t\t" + hex(current_byte)
  186
+            print "next_byte: \t\t" + hex(next_byte)
  187
+
182 188
             # all two-byte opcodes also have their first byte in there somewhere
183  
-            if current_byte in opt_table.keys():
  189
+            if (current_byte in opt_table.keys()) or ((current_byte + (next_byte << 8)) in opt_table.keys()):
  190
+                print "debug2"
  191
+
184 192
                 # this might be a two-byte opcode
185 193
                 possible_opcode = current_byte + (next_byte << 8)
186 194
 
@@ -195,12 +203,15 @@ def parse(self):
195 203
                 opstr = op[0].lower()
196 204
                 optype = op[1]
197 205
 
  206
+                print "opstr: " + opstr
  207
+
198 208
                 asm_command["type"] = "op"
199 209
                 asm_command["id"] = op_code
200 210
                 asm_command["format"] = opstr
201 211
                 asm_command["opnumberthing"] = optype
202 212
 
203 213
                 if "x" in opstr:
  214
+                    print "debug3"
204 215
                     for x in range(0, opstr.count("x")):
205 216
                         insertion = ord(rom[offset + 1])
206 217
 
@@ -215,6 +226,7 @@ def parse(self):
215 226
                         offset += 1
216 227
 
217 228
                 if "?" in opstr:
  229
+                    print "debug4"
218 230
                     for y in range(0, opstr.count("?")):
219 231
                         byte1 = ord(rom[offset + 1])
220 232
                         byte2 = ord(rom[offset + 2])
@@ -234,10 +246,13 @@ def parse(self):
234 246
                 # Check for relative jumps, construct the formatted asm line.
235 247
                 # Also set the usage of labels.
236 248
                 if current_byte in [0x18, 0x20] or current_byte in relative_jumps: # jr or jr nz
  249
+                    print "debug5"
  250
+
237 251
                     # generate a label for the byte we're jumping to
238 252
                     target_address = offset + 2 + c_int8(ord(rom[offset + 1])).value
239 253
 
240 254
                     if target_address in asm_commands.keys():
  255
+                        print "debug6"
241 256
                         asm_commands[target_address]["references"] += 1
242 257
                         remote_label = "asm_" + hex(target_address)
243 258
                         asm_commands[target_address]["current_label"] = remote_label
@@ -245,6 +260,7 @@ def parse(self):
245 260
 
246 261
                         asm_command["use_remote_label"] = True
247 262
                     else:
  263
+                        print "debug7"
248 264
                         remote_label = "asm_" + hex(target_address)
249 265
 
250 266
                         # This remote address might not be part of this
@@ -281,6 +297,7 @@ def parse(self):
281 297
                         used_3d97 = True
282 298
 
283 299
                 if current_byte == 0xc3 or current_byte in relative_unconditional_jumps:
  300
+                    print "debug8"
284 301
                     if current_byte == 0xc3:
285 302
                         if number == 0x3d97:
286 303
                             used_3d97 = True
@@ -291,6 +308,7 @@ def parse(self):
291 308
 
292 309
                 # stop reading at a jump, relative jump or return
293 310
                 if current_byte in end_08_scripts_with:
  311
+                    print "debug9"
294 312
                     is_data = False
295 313
 
296 314
                     if not has_outstanding_labels(byte_labels) and all_outstanding_labels_are_reverse(byte_labels, offset):
@@ -306,9 +324,12 @@ def parse(self):
306 324
                 # ROM probably doesn't represent instructions.
307 325
                 asm_command["type"] = "data" # db
308 326
                 asm_command["value"] = current_byte
  327
+                keep_reading = False
309 328
 
310  
-            # save this new command in the list
311  
-            asm_commands[asm_command["address"]] = asm_command
  329
+        # save this new command in the list
  330
+        asm_commands[asm_command["address"]] = asm_command
  331
+        self.asm_commands = asm_commands
  332
+        print "debug10"
312 333
 
313 334
     def __str__(self):
314 335
         """ ASM pretty printer.

0 notes on commit b19db1e

Please sign in to comment.
Something went wrong with that request. Please try again.