summaryrefslogtreecommitdiff
path: root/source/notes/pic-mcu/pickit2/pk2cmd-stuff/pk2cmd_script.py
blob: f1de6ce0c612bc7c1edd95b14dcd598cf7b3b45b (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
import pic24

NoOperands = [
    0xD5, 0xD6, 0xD7, 0xD8,
    0xEF, # comment on this one: Read & toss 2 response words
    0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB
]

ScriptCommandTable = {
    0xB4: 'SCMD_JT2_WAIT_PE_RESP',
    0xB5: 'SCMD_JT2_GET_PE_RESP',
    0xB6: 'SCMD_JT2_XFERINST_BUF',
    0xB7: 'SCMD_JT2_XFRFASTDAT_BUF',
    0xB8: 'SCMD_JT2_XFRFASTDAT_LIT',
    0xB9: 'SCMD_JT2_XFERDATA32_LIT',
    0xBA: 'SCMD_JT2_XFERDATA8_LIT',
    0xBB: 'SCMD_JT2_SENDCMD',
    0xBC: 'SCMD_JT2_SETMODE',
    0xBD: 'SCMD_UNIO_TX_RX',
    0xBE: 'SCMD_UNIO_TX',
    0xBF: 'SCMD_MEASURE_PULSE',
    0xC0: 'SCMD_ICDSLAVE_TX_BUF_BL',
    0xC1: 'SCMD_ICDSLAVE_TX_LIT_BL',
    0xC2: 'SCMD_ICDSLAVE_RX_BL',
    0xC3: 'SCMD_SPI_RDWR_BYTE_BUF',
    0xC4: 'SCMD_SPI_RDWR_BYTE_LIT',
    0xC5: 'SCMD_SPI_RD_BYTE_BUF',
    0xC6: 'SCMD_SPI_WR_BYTE_BUF',
    0xC7: 'SCMD_SPI_WR_BYTE_LIT',
    0xC8: 'SCMD_I2C_RD_BYTE_NACK',
    0xC9: 'SCMD_I2C_RD_BYTE_ACK',
    0xCA: 'SCMD_I2C_WR_BYTE_BUF',
    0xCB: 'SCMD_I2C_WR_BYTE_LIT',
    0xCC: 'SCMD_I2C_STOP',
    0xCD: 'SCMD_I2C_START',
    0xCE: 'SCMD_AUX_STATE_BUFFER',
    0xCF: 'SCMD_SET_AUX',
    0xD0: 'SCMD_WRITE_BITS_BUF_HLD',
    0xD1: 'SCMD_WRITE_BITS_LIT_HLD',
    0xD2: 'SCMD_CONST_WRITE_DL',
    0xD3: 'SCMD_WRITE_BUFBYTE_W',
    0xD4: 'SCMD_WRITE_BUFWORD_W',
    0xD5: 'SCMD_RD2_BITS_BUFFER',
    0xD6: 'SCMD_RD2_BYTE_BUFFER',
    0xD7: 'SCMD_VISI24',
    0xD8: 'SCMD_NOP24',
    0xD9: 'SCMD_COREINST24',
    0xDA: 'SCMD_COREINST18',
    0xDB: 'SCMD_POP_DOWNLOAD',
    0xDC: 'SCMD_ICSP_STATES_BUFFER',
    0xDD: 'SCMD_LOOP_BUFFER',
    0xDE: 'SCMD_ICDSLAVE_TX_BUF',
    0xDF: 'SCMD_ICDSLAVE_TX_LIT',
    0xE0: 'SCMD_ICDSLAVE_RX',
    0xE1: 'SCMD_POKE_SFR',
    0xE2: 'SCMD_PEEK_SFR',
    0xE3: 'SCMD_EXIT_SCRIPT',
    0xE4: 'SCMD_GOTO_INDEX',
    0xE5: 'SCMD_IF_GT_GOTO',
    0xE6: 'SCMD_IF_EQ_GOTO',
    0xE7: 'SCMD_DELAY_SHORT',
    0xE8: 'SCMD_DELAY_LONG',
    0xE9: 'SCMD_LOOP',
    0xEA: 'SCMD_SET_ICSP_SPEED',
    0xEB: 'SCMD_READ_BITS',
    0xEC: 'SCMD_READ_BITS_BUFFER',
    0xED: 'SCMD_WRITE_BITS_BUFFER',
    0xEE: 'SCMD_WRITE_BITS_LITERAL',
    0xEF: 'SCMD_READ_BYTE',
    0xF0: 'SCMD_READ_BYTE_BUFFER',
    0xF1: 'SCMD_WRITE_BYTE_BUFFER',
    0xF2: 'SCMD_WRITE_BYTE_LITERAL',
    0xF3: 'SCMD_SET_ICSP_PINS',
    0xF4: 'SCMD_BUSY_LED_OFF',
    0xF5: 'SCMD_BUSY_LED_ON',
    0xF6: 'SCMD_MCLR_GND_OFF',
    0xF7: 'SCMD_MCLR_GND_ON',
    0xF8: 'SCMD_VPP_PWM_OFF',
    0xF9: 'SCMD_VPP_PWM_ON',
    0xFA: 'SCMD_VPP_OFF',
    0xFB: 'SCMD_VPP_ON',
    0xFC: 'SCMD_VDD_GND_OFF',
    0xFD: 'SCMD_VDD_GND_ON',
    0xFE: 'SCMD_VDD_OFF',
    0xFF: 'SCMD_VDD_ON'
}

def disassemble(blob, offset):
    code = blob[offset]
    length = 1
    command = ScriptCommandTable[code]
    result = {}
    result['mnemonic'] = command
    if code == 0xCF:
        length += 1
        result['op'] = hex(blob[offset + 1])
    elif code == 0xD3 or code == 0xD4:
        length += 1
        immediate = blob[offset + 1]
    elif code == 0xD9:
        if blob[offset + 1 + 2] == 0x04:
            # it's a goto, and there's a second instruction. this OUGHT to be
            # followed by a SCMD_NOP24...
            if blob[offset + 1 + 3] == 0xD8:
                # it is! whew
                instrbytes = blob[offset + 1:offset + 1 + 3]
                instrbytes.append(0)
                instrbytes.append(0)
                instrbytes.append(0)

                (postinstr_offset, instr) = pic24.disassemble(instrbytes, 0)
                length = 1 + 3 + 1 # the command, three real bytes + the nop
                result['op'] = instr #pic24.render(instr)
            else:
                (postinstr_offset, instr) = pic24.disassemble(blob, offset + 1)
#                length = postinstr_offset - offset
                length = 4
                result['op'] = instr #pic24.render(instr)
                print("Goto not followed by nop, uhh what's " + hex(blob[offset + 3 + 1]))
        else:
            (postinstr_offset, instr) = pic24.disassemble(blob, offset + 1)
            length = postinstr_offset - offset
            result['op'] = instr #pic24.render(instr)
    elif code in NoOperands:
        pass
    elif code == 0xE7 or code == 0xE8:
        length += 1
        result['op'] = hex(blob[offset + 1])
    elif code == 0xEE:
        length += 2
# not sure if this is really two operands
        result['op'] = hex(blob[offset + 1]) + ", " + hex(blob[offset + 2])
    elif code == 0xE9:
        length += 2
        result['op'] = hex((blob[offset + 2] << 8) + blob[offset + 1])
    elif code == 0xF2:
        length += 1
        result['op'] = hex(blob[offset + 1])
    elif code == 0xF3:
        length += 1
        result['op'] = hex(blob[offset + 1])
    else:
        print("Unknown: " + command)
        return None

    return (offset + length, result)