summaryrefslogtreecommitdiff
path: root/source/notes/pic-mcu/pickit2/pk2cmd-stuff/pic24.py
blob: e0adef034239cac8ae112c41854fca88f63dc0fc (plain)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
def render(instruction):
    return "{} {}".format(instruction['mnemonic'], instruction['op'])


def Table_5_12(nibble):
    return "Table 5.12 TODO"

def Table_5_11(nibble):
    return "Table 5.11 TODO"

def Table_5_10(nibble):
    return "Table 5.10 TODO"

def YDataPrefetchDest_5_9(nibble):
    return "Table 5.9 TODO"

def YDataPrefetchOperation_5_8(nibble):
    return "Table 5.8 TODO"

def XDataPrefetchDest_5_7(nibble):
    return "Table 5.7 TODO"

def XDataPrefetchOperation_5_6(nibble):
    return "Table 5.6 TODO"

def readWd_5_5(octet):
    return [
        "Wd",
        "[Wd]",
        "[Wd--]",
        "[Wd++]",
        "[--Wd]",
        "[++Wd]",
        "[Wd + Wb]",
        "[Wd + Wb]"
    ][octet]

def readWs_5_4(octet):
    return [
        "Ws",
        "[Ws]",
        "[Ws--]",
        "[Ws++]",
        "[--Ws]",
        "[++Ws]",
        "[Ws + Wb]",
        "[Ws + Wb]"
    ][octet]

def readWd_5_3(octet):
    return [
        "Wd",
        "[Wd]",
        "[Wd--]",
        "[Wd++]",
        "[--Wd]",
        "[++Wd]",
        "ERR",
        "ERR"
    ][octet]

def readWs_5_2(octet):
    return [
        "Ws",
        "[Ws]",
        "[Ws--]",
        "[Ws++]",
        "[--Ws]",
        "[++Ws]",
        "ERR",
        "ERR"
    ][octet]

def disassemble(blob, offset):
#    print ("Disassembling " + str(blob))
    instrbytes = blob[offset:offset + 3]
#    instrbytes.reverse()
    print("Disassembling {} {} {}".format(hex(instrbytes[0]), hex(instrbytes[1]), hex(instrbytes[2])))
    instr = {}
    instr['op'] = ''
    size = 1

    if instrbytes[2] == 0x0:
        instr['mnemonic'] = 'nop'
    if instrbytes[2] == 0x1:
        instr['mnemonic'] = 'br*'
    if instrbytes[2] == 0x2:
        instr['mnemonic'] = 'call'
    if instrbytes[2] == 0x3:
        instr['mnemonic'] = '-'
    if instrbytes[2] == 0x4:
        instr['mnemonic'] = 'goto'
        size = 2
        w2 = blob[offset + 3:offset + 6]
#        w2.reverse()
        n_hi = ((instrbytes[1] << 8) + instrbytes[0])
        if w2[0] != 0 or w2[1] != 0:
            instr['mnemonic'] = 'goto [BAD]'
        n_lo = w2[0]
        n = n_hi + (n_lo << 16)
        instr["op"] = hex(n)
    if instrbytes[2] == 0x5:
        instr['mnemonic'] = 'retlw'
    if instrbytes[2] == 0x6:
        instr['mnemonic'] = 'retfie'
    if instrbytes[2] == 0x7:
        instr['mnemonic'] = 'rcall'
    if instrbytes[2] == 0x8:
        instr['mnemonic'] = 'do'
    if instrbytes[2] == 0x9:
        instr['mnemonic'] = 'repeat'
    if instrbytes[2] == 0xa:
        instr['mnemonic'] = '-'
    if instrbytes[2] == 0xb:
        instr['mnemonic'] = '-'
    if instrbytes[2] == 0xc:
        instr['mnemonic'] = 'bra(oa)'
        instr['mnemonic'] += " (dsPIC3XF only)"
    if instrbytes[2] == 0xd:
        instr['mnemonic'] = 'bra(ob)'
        instr['mnemonic'] += " (dsPIC3XF only)"
    if instrbytes[2] == 0xe:
        instr['mnemonic'] = 'bra(sa)'
        instr['mnemonic'] += " (dsPIC3XF only)"
    if instrbytes[2] == 0xf:
        instr['mnemonic'] = 'bra(sb)'
        instr['mnemonic'] += " (dsPIC3XF only)"
    if instrbytes[2] >= 0x10 and instrbytes[2] < 0x18:
        instr['mnemonic'] = 'subr'
    if instrbytes[2] >= 0x18 and instrbytes[2] < 0x20:
        instr['mnemonic'] = 'subbr'
    if instrbytes[2] >= 0x20 and instrbytes[2] < 0x30:
        instr['mnemonic'] = 'mov'
        word = instrbytes[0] | (instrbytes[1] << 8) | (instrbytes[2] << 16)
        imm = (word & 0x0ffff0) >> 4
        reg = word & 0xf
        instr['op'] = "#" + hex(imm) + ", W" + str(reg)

    if instrbytes[2] >= 0x80 and instrbytes[2] < 0x90:
        instr['mnemonic'] = 'mov'
        word = instrbytes[0] | (instrbytes[1] << 8) | (instrbytes[2] << 16)
        f = (word & 0x07fff0) >> 4
        reg = word & 0xf

        if instrbytes[2] < 0x88:
            instr['op'] = "[" + hex(f) + "], W" + str(reg)
        else:
            instr['op'] = "W" + str(reg) + ", [" + hex(f) + "]"

    if instrbytes[2] >= 0xc0 and instrbytes[2] < 0xd0:
        instr['mnemonic'] = "(dsPIC3XF only)"

    if instrbytes[2] == 0xB4 and (instrbytes[1] & 0x80) == 0x00:
        instr['mnemonic'] = "ADD"
        B = instrbytes[1] & 0x40
        D = instrbytes[1] & 0x20
        f = ((instrbytes[1] & 0x1f) << 8) + instrbytes[0]

        if B:
            instr['mnemonic'] += '.B'

        instr['op'] = "RAM:" + hex(f)

        if D:
            instr['op'] += ', W0'
    if instrbytes[2] == 0xaa:
        instr['mnemonic'] = "BTG"
        # TODO: not clear what is .B mode
        bit4 = ((instrbytes[1] >> 4) & 0x0e) + (instrbytes[0] & 0x01)
        f = ((instrbytes[1] & 0x1f) << 7) + (instrbytes[0] >> 1)
        instr['op'] = "{}, #{}".format(hex(f), hex(bit4))
    if instrbytes[2] == 0xba:
        instr['mnemonic'] = "TBLRD"
        if (instrbytes[1] & 0x80) != 0:
            instr['mnemonic'] += 'H'
        else:
            instr['mnemonic'] += 'L'

        if (instrbytes[1] & 0x40) != 0:
            instr['mnemonic'] += '.B'

        s = (instrbytes[0] & 0x0f)
        p = (instrbytes[0] >> 4) & 0x07
        d = ((instrbytes[0] >> 7) & 0x01) + ((instrbytes[1] << 1) & 0xf)
        q = (instrbytes[1] & 0x38) >> 3

        srcOp = readWs_5_2(p).replace("Ws", "W" + str(s))
        dstOp = readWd_5_3(q).replace("Wd", "W" + str(d))

        instr['op'] = "{}, {}".format(srcOp, dstOp)
    if instrbytes[2] == 0xbb:
        instr['mnemonic'] = "TBLWT"
        if (instrbytes[1] & 0x80) != 0:
            instr['mnemonic'] += 'H'
        else:
            instr['mnemonic'] += 'L'

        if (instrbytes[1] & 0x40) != 0:
            instr['mnemonic'] += '.B'

        s = (instrbytes[0] & 0x0f)
        p = (instrbytes[0] >> 4) & 0x07
        d = ((instrbytes[0] >> 7) & 0x01) + ((instrbytes[1] << 1) & 0xf)
        q = (instrbytes[1] & 0x38) >> 3

        srcOp = readWs_5_2(p).replace("Ws", "W" + str(s))
        dstOp = readWd_5_3(q).replace("Wd", "W" + str(d))

        instr['op'] = "{}, {}".format(srcOp, dstOp)

    if 'mnemonic' not in instr:
        instr = instrbytes[0] | (instrbytes[1] << 8) | (instrbytes[2] << 16)
        return (offset + 3, "Unknown instruction: " + hex(instr))

    return (offset + 3 * size, instr)