-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisasm.py
36 lines (30 loc) · 1.01 KB
/
disasm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from collections import defaultdict
from sys import argv
from sugarfree import *
cpu = SugarFreeCore()
tracefile = None
if len(argv) > 2:
tracefile = open(argv[2])
if len(argv) > 1:
kernel_path = argv[1]
elif len(argv) == 0:
print "Need an assembled kernel path to disassemble"
print "e.g. core/src/tb/miner_tb/miner (omit suffixes)"
rf = defaultdict(int)
for l in open(kernel_path + '_r.hex'):
rf[int(l[:2], 16)] = int(l[2:], 16)
totals = defaultdict(int)
if tracefile:
for l in tracefile:
cycle, instr, pc, rs, rd = map(int, l.split())
if pc not in totals:
totals[pc] = 0
totals[pc] += 1
for addr, l in enumerate(open(kernel_path + '_i.hex')):
instr = Instruction.decode(int(l, 16))
instr.rd_str = reg_str(instr.rd, rf[instr.rd])
instr.rs_str = reg_str(instr.rs, rf[instr.rs])
if tracefile:
print '%6d %04x:\t%s' % (totals[addr], addr, str(instr).replace(' ', '\t'))
else:
print '%04x:\t%s' % (addr, str(instr).replace(' ', '\t'))