]>
Commit | Line | Data |
---|---|---|
557a143d | 1 | ## |
50bd5d25 | 2 | ## This file is part of the libsigrokdecode project. |
557a143d | 3 | ## |
f7332ee0 | 4 | ## Copyright (C) 2012-2015 Uwe Hermann <uwe@hermann-uwe.de> |
557a143d UH |
5 | ## |
6 | ## This program is free software; you can redistribute it and/or modify | |
7 | ## it under the terms of the GNU General Public License as published by | |
8 | ## the Free Software Foundation; either version 2 of the License, or | |
9 | ## (at your option) any later version. | |
10 | ## | |
11 | ## This program is distributed in the hope that it will be useful, | |
12 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | ## GNU General Public License for more details. | |
15 | ## | |
16 | ## You should have received a copy of the GNU General Public License | |
4539e9ca | 17 | ## along with this program; if not, see <http://www.gnu.org/licenses/>. |
557a143d UH |
18 | ## |
19 | ||
557a143d UH |
20 | import sigrokdecode as srd |
21 | ||
4c3b1846 | 22 | ''' |
c515eed7 | 23 | OUTPUT_PYTHON format: |
4c3b1846 | 24 | |
bf69977d UH |
25 | Packet: |
26 | [<ptype>, <pdata>] | |
4c3b1846 | 27 | |
bf69977d UH |
28 | <ptype>: |
29 | - 'NEW STATE': <pdata> is the new state of the JTAG state machine. | |
4c3b1846 UH |
30 | Valid values: 'TEST-LOGIC-RESET', 'RUN-TEST/IDLE', 'SELECT-DR-SCAN', |
31 | 'CAPTURE-DR', 'SHIFT-DR', 'EXIT1-DR', 'PAUSE-DR', 'EXIT2-DR', 'UPDATE-DR', | |
32 | 'SELECT-IR-SCAN', 'CAPTURE-IR', 'SHIFT-IR', 'EXIT1-IR', 'PAUSE-IR', | |
33 | 'EXIT2-IR', 'UPDATE-IR'. | |
34 | - 'IR TDI': Bitstring that was clocked into the IR register. | |
35 | - 'IR TDO': Bitstring that was clocked out of the IR register. | |
36 | - 'DR TDI': Bitstring that was clocked into the DR register. | |
37 | - 'DR TDO': Bitstring that was clocked out of the DR register. | |
4c3b1846 | 38 | |
36faa6d0 UH |
39 | All bitstrings are a list consisting of two items. The first is a sequence |
40 | of '1' and '0' characters (the right-most character is the LSB. Example: | |
41 | '01110001', where 1 is the LSB). The second item is a list of ss/es values | |
42 | for each bit that is in the bitstring. | |
4c3b1846 UH |
43 | ''' |
44 | ||
0c0368d0 UH |
45 | jtag_states = [ |
46 | # Intro "tree" | |
47 | 'TEST-LOGIC-RESET', 'RUN-TEST/IDLE', | |
48 | # DR "tree" | |
49 | 'SELECT-DR-SCAN', 'CAPTURE-DR', 'UPDATE-DR', 'PAUSE-DR', | |
50 | 'SHIFT-DR', 'EXIT1-DR', 'EXIT2-DR', | |
51 | # IR "tree" | |
52 | 'SELECT-IR-SCAN', 'CAPTURE-IR', 'UPDATE-IR', 'PAUSE-IR', | |
53 | 'SHIFT-IR', 'EXIT1-IR', 'EXIT2-IR', | |
54 | ] | |
55 | ||
557a143d | 56 | class Decoder(srd.Decoder): |
2d557217 | 57 | api_version = 3 |
557a143d UH |
58 | id = 'jtag' |
59 | name = 'JTAG' | |
b7a7e6f5 | 60 | longname = 'Joint Test Action Group (IEEE 1149.1)' |
6e7a0087 | 61 | desc = 'Protocol for testing, debugging, and flashing ICs.' |
557a143d UH |
62 | license = 'gplv2+' |
63 | inputs = ['logic'] | |
64 | outputs = ['jtag'] | |
6a15597a | 65 | channels = ( |
557a143d UH |
66 | {'id': 'tdi', 'name': 'TDI', 'desc': 'Test data input'}, |
67 | {'id': 'tdo', 'name': 'TDO', 'desc': 'Test data output'}, | |
68 | {'id': 'tck', 'name': 'TCK', 'desc': 'Test clock'}, | |
69 | {'id': 'tms', 'name': 'TMS', 'desc': 'Test mode select'}, | |
da9bcbd9 | 70 | ) |
6a15597a | 71 | optional_channels = ( |
1cc590f7 UH |
72 | {'id': 'trst', 'name': 'TRST#', 'desc': 'Test reset'}, |
73 | {'id': 'srst', 'name': 'SRST#', 'desc': 'System reset'}, | |
74 | {'id': 'rtck', 'name': 'RTCK', 'desc': 'Return clock signal'}, | |
da9bcbd9 | 75 | ) |
80214a11 UH |
76 | annotations = tuple([tuple([s.lower(), s]) for s in jtag_states]) + ( \ |
77 | ('bit-tdi', 'Bit (TDI)'), | |
78 | ('bit-tdo', 'Bit (TDO)'), | |
79 | ('bitstring-tdi', 'Bitstring (TDI)'), | |
80 | ('bitstring-tdo', 'Bitstring (TDO)'), | |
81 | ) | |
82 | annotation_rows = ( | |
83 | ('bits-tdi', 'Bits (TDI)', (16,)), | |
84 | ('bits-tdo', 'Bits (TDO)', (17,)), | |
85 | ('bitstrings-tdi', 'Bitstring (TDI)', (18,)), | |
86 | ('bitstrings-tdo', 'Bitstring (TDO)', (19,)), | |
87 | ('states', 'States', tuple(range(15 + 1))), | |
88 | ) | |
557a143d | 89 | |
92b7b49f | 90 | def __init__(self): |
10aeb8ea GS |
91 | self.reset() |
92 | ||
93 | def reset(self): | |
e5edf39f UH |
94 | # self.state = 'TEST-LOGIC-RESET' |
95 | self.state = 'RUN-TEST/IDLE' | |
96 | self.oldstate = None | |
557a143d UH |
97 | self.bits_tdi = [] |
98 | self.bits_tdo = [] | |
36faa6d0 UH |
99 | self.bits_samplenums_tdi = [] |
100 | self.bits_samplenums_tdo = [] | |
0c0368d0 | 101 | self.ss_item = self.es_item = None |
80214a11 | 102 | self.ss_bitstring = self.es_bitstring = None |
0c0368d0 UH |
103 | self.saved_item = None |
104 | self.first = True | |
80214a11 | 105 | self.first_bit = True |
557a143d | 106 | |
8915b346 | 107 | def start(self): |
c515eed7 | 108 | self.out_python = self.register(srd.OUTPUT_PYTHON) |
be465111 | 109 | self.out_ann = self.register(srd.OUTPUT_ANN) |
557a143d | 110 | |
6b32f928 | 111 | def putx(self, data): |
0c0368d0 | 112 | self.put(self.ss_item, self.es_item, self.out_ann, data) |
6b32f928 UH |
113 | |
114 | def putp(self, data): | |
c515eed7 | 115 | self.put(self.ss_item, self.es_item, self.out_python, data) |
6b32f928 | 116 | |
80214a11 UH |
117 | def putx_bs(self, data): |
118 | self.put(self.ss_bitstring, self.es_bitstring, self.out_ann, data) | |
119 | ||
120 | def putp_bs(self, data): | |
121 | self.put(self.ss_bitstring, self.es_bitstring, self.out_python, data) | |
122 | ||
557a143d | 123 | def advance_state_machine(self, tms): |
e5edf39f UH |
124 | self.oldstate = self.state |
125 | ||
557a143d UH |
126 | # Intro "tree" |
127 | if self.state == 'TEST-LOGIC-RESET': | |
128 | self.state = 'TEST-LOGIC-RESET' if (tms) else 'RUN-TEST/IDLE' | |
129 | elif self.state == 'RUN-TEST/IDLE': | |
130 | self.state = 'SELECT-DR-SCAN' if (tms) else 'RUN-TEST/IDLE' | |
131 | ||
132 | # DR "tree" | |
133 | elif self.state == 'SELECT-DR-SCAN': | |
134 | self.state = 'SELECT-IR-SCAN' if (tms) else 'CAPTURE-DR' | |
135 | elif self.state == 'CAPTURE-DR': | |
136 | self.state = 'EXIT1-DR' if (tms) else 'SHIFT-DR' | |
137 | elif self.state == 'SHIFT-DR': | |
138 | self.state = 'EXIT1-DR' if (tms) else 'SHIFT-DR' | |
139 | elif self.state == 'EXIT1-DR': | |
140 | self.state = 'UPDATE-DR' if (tms) else 'PAUSE-DR' | |
141 | elif self.state == 'PAUSE-DR': | |
142 | self.state = 'EXIT2-DR' if (tms) else 'PAUSE-DR' | |
143 | elif self.state == 'EXIT2-DR': | |
144 | self.state = 'UPDATE-DR' if (tms) else 'SHIFT-DR' | |
145 | elif self.state == 'UPDATE-DR': | |
146 | self.state = 'SELECT-DR-SCAN' if (tms) else 'RUN-TEST/IDLE' | |
147 | ||
148 | # IR "tree" | |
149 | elif self.state == 'SELECT-IR-SCAN': | |
150 | self.state = 'TEST-LOGIC-RESET' if (tms) else 'CAPTURE-IR' | |
151 | elif self.state == 'CAPTURE-IR': | |
152 | self.state = 'EXIT1-IR' if (tms) else 'SHIFT-IR' | |
153 | elif self.state == 'SHIFT-IR': | |
154 | self.state = 'EXIT1-IR' if (tms) else 'SHIFT-IR' | |
155 | elif self.state == 'EXIT1-IR': | |
156 | self.state = 'UPDATE-IR' if (tms) else 'PAUSE-IR' | |
157 | elif self.state == 'PAUSE-IR': | |
158 | self.state = 'EXIT2-IR' if (tms) else 'PAUSE-IR' | |
159 | elif self.state == 'EXIT2-IR': | |
160 | self.state = 'UPDATE-IR' if (tms) else 'SHIFT-IR' | |
161 | elif self.state == 'UPDATE-IR': | |
162 | self.state = 'SELECT-DR-SCAN' if (tms) else 'RUN-TEST/IDLE' | |
163 | ||
2d557217 UH |
164 | def handle_rising_tck_edge(self, pins): |
165 | (tdi, tdo, tck, tms, trst, srst, rtck) = pins | |
166 | ||
557a143d UH |
167 | # Rising TCK edges always advance the state machine. |
168 | self.advance_state_machine(tms) | |
169 | ||
35b380b1 | 170 | if self.first: |
0c0368d0 UH |
171 | # Save the start sample and item for later (no output yet). |
172 | self.ss_item = self.samplenum | |
173 | self.first = False | |
0c0368d0 UH |
174 | else: |
175 | # Output the saved item (from the last CLK edge to the current). | |
176 | self.es_item = self.samplenum | |
80214a11 UH |
177 | # Output the old state (from last rising TCK edge to current one). |
178 | self.putx([jtag_states.index(self.oldstate), [self.oldstate]]) | |
0c0368d0 | 179 | self.putp(['NEW STATE', self.state]) |
e5edf39f | 180 | |
9f111fa9 GH |
181 | # Upon SHIFT-*/EXIT1-* collect the current TDI/TDO values. |
182 | if self.oldstate.startswith('SHIFT-') or \ | |
183 | self.oldstate.startswith('EXIT1-'): | |
80214a11 UH |
184 | if self.first_bit: |
185 | self.ss_bitstring = self.samplenum | |
186 | self.first_bit = False | |
187 | else: | |
188 | self.putx([16, [str(self.bits_tdi[0])]]) | |
189 | self.putx([17, [str(self.bits_tdo[0])]]) | |
36faa6d0 UH |
190 | # Use self.samplenum as ES of the previous bit. |
191 | self.bits_samplenums_tdi[0][1] = self.samplenum | |
192 | self.bits_samplenums_tdo[0][1] = self.samplenum | |
193 | ||
e5edf39f UH |
194 | self.bits_tdi.insert(0, tdi) |
195 | self.bits_tdo.insert(0, tdo) | |
e5edf39f | 196 | |
36faa6d0 UH |
197 | # Use self.samplenum as SS of the current bit. |
198 | self.bits_samplenums_tdi.insert(0, [self.samplenum, -1]) | |
199 | self.bits_samplenums_tdo.insert(0, [self.samplenum, -1]) | |
200 | ||
9f111fa9 GH |
201 | # Output all TDI/TDO bits if we just switched to UPDATE-*. |
202 | if self.state.startswith('UPDATE-'): | |
e5edf39f | 203 | |
80214a11 UH |
204 | self.es_bitstring = self.samplenum |
205 | ||
e5edf39f | 206 | t = self.state[-2:] + ' TDI' |
9f111fa9 GH |
207 | b = ''.join(map(str, self.bits_tdi[1:])) |
208 | h = ' (0x%x' % int('0b0' + b, 2) + ')' | |
209 | s = t + ': ' + b + h + ', ' + str(len(self.bits_tdi[1:])) + ' bits' | |
80214a11 | 210 | self.putx_bs([18, [s]]) |
9f111fa9 | 211 | self.putp_bs([t, [b, self.bits_samplenums_tdi[1:]]]) |
e5edf39f | 212 | self.bits_tdi = [] |
36faa6d0 | 213 | self.bits_samplenums_tdi = [] |
557a143d | 214 | |
e5edf39f | 215 | t = self.state[-2:] + ' TDO' |
9f111fa9 GH |
216 | b = ''.join(map(str, self.bits_tdo[1:])) |
217 | h = ' (0x%x' % int('0b0' + b, 2) + ')' | |
218 | s = t + ': ' + b + h + ', ' + str(len(self.bits_tdo[1:])) + ' bits' | |
80214a11 | 219 | self.putx_bs([19, [s]]) |
9f111fa9 | 220 | self.putp_bs([t, [b, self.bits_samplenums_tdo[1:]]]) |
557a143d | 221 | self.bits_tdo = [] |
36faa6d0 | 222 | self.bits_samplenums_tdo = [] |
557a143d | 223 | |
80214a11 UH |
224 | self.first_bit = True |
225 | ||
226 | self.ss_bitstring = self.samplenum | |
227 | ||
228 | self.ss_item = self.samplenum | |
229 | ||
2d557217 UH |
230 | def decode(self): |
231 | while True: | |
232 | # Wait for a rising edge on TCK. | |
233 | self.handle_rising_tck_edge(self.wait({2: 'r'})) |