+def parseArgs():
+ global phy
+ global binary
+ global dasmopt
+ global dumpShm
+ global dumpShmBin
+
+ phy = None # Autodetect
+ binary = None # No instruction dump
+ dasmopt = ""
+ dumpShm = False
+ dumpShmBin = False
+
+ try:
+ (opts, args) = getopt.getopt(sys.argv[1:],
+ "hp:b:d:sS",
+ [ "help", "phy=", "binary=", "dasmopt=", "shm", "shmbin" ])
+ except getopt.GetoptError:
+ usage()
+ sys.exit(1)
+
+ for (o, v) in opts:
+ if o in ("-h", "--help"):
+ usage()
+ sys.exit(0)
+ if o in ("-p", "--phy"):
+ phy = v
+ if o in ("-b", "--binary"):
+ binary = v
+ if o in ("-d", "--dasmopt"):
+ dasmopt = v
+ if o in ("-s", "--shm"):
+ dumpShm = True
+ if o in ("-S", "--shmbin"):
+ dumpShmBin = True
+ return
+
+
+def dump_regs(prefix, regs):
+ if len(regs) >= 10:
+ template = "%s%02u: %04X "
+ else:
+ template = "%s%01u: %04X "
+ for i in range(0, len(regs)):
+ if i != 0 and i % 4 == 0:
+ stdout.write("\n")
+ stdout.write(template % (prefix, i, regs[i]))
+ stdout.write("\n")
+ return
+
+def dasmLineIsPC(line, pc):
+ m = re.match(r'.*/\*\s+([0-9a-fA-F]+)\s+\*/.*', line, re.DOTALL)
+ if not m:
+ return False
+ linePC = int(m.group(1), 16)
+ return pc == linePC
+
+def makeShortDump(dasm, pc):
+ dasm = dasm.splitlines()
+ i = 0
+ for line in dasm:
+ if dasmLineIsPC(line, pc):
+ break
+ i += 1
+ else:
+ return "<Could not find PC in the binary>"
+ ret = ""
+ pos = max(i - 8, 0)
+ end = min(i + 8, len(dasm) - 1)
+ while pos != end:
+ ret += dasm[pos]
+ if dasmLineIsPC(dasm[pos], pc):
+ ret += "\t\t<<<<<<<<<<<"
+ ret += "\n"
+ pos += 1
+ return ret
+
+def toAscii(char):
+ if char >= 32 and char <= 126:
+ return chr(char)
+ return "."
+
+def main():
+ parseArgs()