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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
def render(instr):
if 'op' in instr:
return "{} {}".format(instr['mnemonic'], instr['op'])
else:
return instr['mnemonic']
def disassemble(blob, offset):
instr = {}
instr['length'] = 1
instrbytes = blob[offset:offset + 2]
# instrbytes.reverse()
print("Decoding {}{}...".format(hex(instrbytes[0])[2:], hex(instrbytes[1])[2:]))
if instrbytes[0] == 0x00:
print(hex(instrbytes[1]))
# there's a few instructions here...
if instrbytes[1] == 0xff:
instr['mnemonic'] = 'reset'
else:
instr['mnemonic'] = [
'nop',
'BAD',
'BAD',
'sleep',
'clrwdt',
'push',
'pop',
'daw',
'tblrd*',
'tblrd*+',
'tblrd*-',
'tblrd+*',
'tblwr*',
'tblwr*+',
'tblwr*-',
'tblwr+*',
'retfie',
'retfie fast',
'return',
'return fast',
'callw*'][instrbytes[1]]
if instrbytes[0] == 0x01:
if instrbytes[1] > 0xf:
instr['mnemonic'] = 'BAD'
else:
instr['mnemonic'] = 'movlb'
instr['op'] = "#" + str(instrbytes[1])
elif instrbytes[0] == 0x02 or instrbytes[0] == 0x03:
instr['mnemonic'] = 'mulwf'
instr['op'] = 'TODO'
elif instrbytes[0] == 0x04 or instrbytes[0] == 0x05 or instrbytes[0] == 0x06 or instrbytes[0] == 0x07:
instr['mnemonic'] = 'decf'
instr['op'] = 'TODO'
elif instrbytes[0] == 0x08:
instr['mnemonic'] = 'sublw'
instr['op'] = '#' + hex(instrbytes[1])
elif instrbytes[0] == 0x09:
instr['mnemonic'] = 'iorlw'
instr['op'] = '#' + hex(instrbytes[1])
elif instrbytes[0] == 0x0a:
instr['mnemonic'] = 'xorlw'
instr['op'] = '#' + hex(instrbytes[1])
elif instrbytes[0] == 0x0b:
instr['mnemonic'] = 'andlw'
instr['op'] = '#' + hex(instrbytes[1])
elif instrbytes[0] == 0x0c:
instr['mnemonic'] = 'retlw'
instr['op'] = '#' + hex(instrbytes[1])
elif instrbytes[0] == 0x0d:
instr['mnemonic'] = 'mullw'
instr['op'] = '#' + hex(instrbytes[1])
elif instrbytes[0] == 0x0e:
instr['mnemonic'] = 'movlw'
instr['op'] = '#' + hex(instrbytes[1])
elif instrbytes[0] == 0x0f:
instr['mnemonic'] = 'addlw'
instr['op'] = '#' + hex(instrbytes[1])
elif instrbytes[0] >= 0x20 and instrbytes[0] <= 0x5f:
mnemonicSel = instrbytes[0] >> 2
instr['mnemonic'] = [
'iorwf',
'andwf',
'xorwf',
'comf',
'addwfc',
'addwf',
'incf',
'decfsz',
'rrcf',
'rlcf',
'swapf',
'incfsz',
'rrncf',
'rlncf',
'infsnz',
'dcfsnz',
'movf',
'subwfb',
'subwfb',
'subwf'
][mnemonicSel]
instr['op'] = 'TODO'
elif instrbytes[0] >= 0x60 and instrbytes[0] < 0x70:
mnemonicSel = instrbytes[0] >> 1
instr['mnemonic'] = [
'cpfslt',
'cpfseq',
'cpfsgt',
'tstfsz',
'setf',
'clrf',
'negf',
'movwf'
][mnemonicSel]
instr['op'] = 'TODO'
elif instrbytes[0] >= 0x70 and instrbytes[0] < 0xc0:
mnemonicSel = instrbytes[0] >> 4
instr['mnemonic'] = [
'BAD', 'BAD', 'BAD', 'BAD', 'BAD', 'BAD', 'BAD',
'BTG',
'BSF',
'BCF',
'BTFSS',
'BTFSC'
][mnemonicSel]
instr['op'] = 'TODO'
elif instrbytes[0] >= 0xc0 and instrbytes[0] < 0xd0:
instr['mnemonic'] = 'MOVFF'
instr['op'] = 'TODO'
elif instrbytes[0] >= 0xd0 and instrbytes[0] < 0xd8:
instr['mnemonic'] = 'BRA'
instr['op'] = 'TODO'
elif instrbytes[0] >= 0xd8 and instrbytes[0] < 0xe0:
instr['mnemonic'] = 'RCALL'
instr['op'] = 'TODO'
elif instrbytes[0] >= 0xf0 and instrbytes[0] <= 0xff:
instr['mnemonic'] = 'MOVFF'
instr['op'] = 'TODO'
elif instrbytes[0] == 0xec or instrbytes[0] == 0xed:
instr['mnemonic'] = 'CALL'
instr['op'] = 'TODO'
instr['length'] = 2
elif instrbytes[0] == 0xee:
instr['mnemonic'] = 'LFSR'
instr['op'] = 'TODO'
instr['length'] = 2
elif instrbytes[0] == 0xef:
instr['mnemonic'] = 'GOTO'
instr['op'] = 'TODO'
instr['length'] = 2
else:
instr['mnemonic'] = 'TODO'
return (offset + instr['length'] * 2, instr)
|