]> sigrok.org Git - libsigrokdecode.git/blame - decoders/jtag/pd.py
jtag: Add more annotations, fix a SHIFT-IR/-DR issue.
[libsigrokdecode.git] / decoders / jtag / pd.py
CommitLineData
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
17## along with this program; if not, write to the Free Software
18## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19##
20
557a143d
UH
21import sigrokdecode as srd
22
4c3b1846 23'''
c515eed7 24OUTPUT_PYTHON format:
4c3b1846 25
bf69977d
UH
26Packet:
27[<ptype>, <pdata>]
4c3b1846 28
bf69977d
UH
29<ptype>:
30 - 'NEW STATE': <pdata> is the new state of the JTAG state machine.
4c3b1846
UH
31 Valid values: 'TEST-LOGIC-RESET', 'RUN-TEST/IDLE', 'SELECT-DR-SCAN',
32 'CAPTURE-DR', 'SHIFT-DR', 'EXIT1-DR', 'PAUSE-DR', 'EXIT2-DR', 'UPDATE-DR',
33 'SELECT-IR-SCAN', 'CAPTURE-IR', 'SHIFT-IR', 'EXIT1-IR', 'PAUSE-IR',
34 'EXIT2-IR', 'UPDATE-IR'.
f7332ee0
UH
35 - 'IR TDI BIT': Bit that was clocked into the IR register.
36 - 'IR TDO BIT': Bit that was clocked out of the IR register.
37 - 'DR TDI BIT': Bit that was clocked into the DR register.
38 - 'DR TDO BIT': Bit that was clocked out of the DR register.
4c3b1846
UH
39 - 'IR TDI': Bitstring that was clocked into the IR register.
40 - 'IR TDO': Bitstring that was clocked out of the IR register.
41 - 'DR TDI': Bitstring that was clocked into the DR register.
42 - 'DR TDO': Bitstring that was clocked out of the DR register.
4c3b1846 43
f7332ee0 44All bits are either '1' or '0' characters.
4c3b1846
UH
45All bitstrings are a sequence of '1' and '0' characters. The right-most
46character in the bitstring is the LSB. Example: '01110001' (1 is LSB).
47'''
48
0c0368d0
UH
49jtag_states = [
50 # Intro "tree"
51 'TEST-LOGIC-RESET', 'RUN-TEST/IDLE',
52 # DR "tree"
53 'SELECT-DR-SCAN', 'CAPTURE-DR', 'UPDATE-DR', 'PAUSE-DR',
54 'SHIFT-DR', 'EXIT1-DR', 'EXIT2-DR',
55 # IR "tree"
56 'SELECT-IR-SCAN', 'CAPTURE-IR', 'UPDATE-IR', 'PAUSE-IR',
57 'SHIFT-IR', 'EXIT1-IR', 'EXIT2-IR',
58]
59
557a143d 60class Decoder(srd.Decoder):
12851357 61 api_version = 2
557a143d
UH
62 id = 'jtag'
63 name = 'JTAG'
b7a7e6f5 64 longname = 'Joint Test Action Group (IEEE 1149.1)'
6e7a0087 65 desc = 'Protocol for testing, debugging, and flashing ICs.'
557a143d
UH
66 license = 'gplv2+'
67 inputs = ['logic']
68 outputs = ['jtag']
6a15597a 69 channels = (
557a143d
UH
70 {'id': 'tdi', 'name': 'TDI', 'desc': 'Test data input'},
71 {'id': 'tdo', 'name': 'TDO', 'desc': 'Test data output'},
72 {'id': 'tck', 'name': 'TCK', 'desc': 'Test clock'},
73 {'id': 'tms', 'name': 'TMS', 'desc': 'Test mode select'},
da9bcbd9 74 )
6a15597a 75 optional_channels = (
1cc590f7
UH
76 {'id': 'trst', 'name': 'TRST#', 'desc': 'Test reset'},
77 {'id': 'srst', 'name': 'SRST#', 'desc': 'System reset'},
78 {'id': 'rtck', 'name': 'RTCK', 'desc': 'Return clock signal'},
da9bcbd9 79 )
80214a11
UH
80 annotations = tuple([tuple([s.lower(), s]) for s in jtag_states]) + ( \
81 ('bit-tdi', 'Bit (TDI)'),
82 ('bit-tdo', 'Bit (TDO)'),
83 ('bitstring-tdi', 'Bitstring (TDI)'),
84 ('bitstring-tdo', 'Bitstring (TDO)'),
85 )
86 annotation_rows = (
87 ('bits-tdi', 'Bits (TDI)', (16,)),
88 ('bits-tdo', 'Bits (TDO)', (17,)),
89 ('bitstrings-tdi', 'Bitstring (TDI)', (18,)),
90 ('bitstrings-tdo', 'Bitstring (TDO)', (19,)),
91 ('states', 'States', tuple(range(15 + 1))),
92 )
557a143d
UH
93
94 def __init__(self, **kwargs):
e5edf39f
UH
95 # self.state = 'TEST-LOGIC-RESET'
96 self.state = 'RUN-TEST/IDLE'
97 self.oldstate = None
6d990fe1 98 self.oldpins = (-1, -1, -1, -1)
557a143d
UH
99 self.oldtck = -1
100 self.bits_tdi = []
101 self.bits_tdo = []
6b32f928 102 self.samplenum = 0
0c0368d0 103 self.ss_item = self.es_item = None
80214a11 104 self.ss_bitstring = self.es_bitstring = None
0c0368d0
UH
105 self.saved_item = None
106 self.first = True
80214a11 107 self.first_bit = True
557a143d 108
8915b346 109 def start(self):
c515eed7 110 self.out_python = self.register(srd.OUTPUT_PYTHON)
be465111 111 self.out_ann = self.register(srd.OUTPUT_ANN)
557a143d 112
6b32f928 113 def putx(self, data):
0c0368d0 114 self.put(self.ss_item, self.es_item, self.out_ann, data)
6b32f928
UH
115
116 def putp(self, data):
c515eed7 117 self.put(self.ss_item, self.es_item, self.out_python, data)
6b32f928 118
80214a11
UH
119 def putx_bs(self, data):
120 self.put(self.ss_bitstring, self.es_bitstring, self.out_ann, data)
121
122 def putp_bs(self, data):
123 self.put(self.ss_bitstring, self.es_bitstring, self.out_python, data)
124
557a143d 125 def advance_state_machine(self, tms):
e5edf39f
UH
126 self.oldstate = self.state
127
557a143d
UH
128 # Intro "tree"
129 if self.state == 'TEST-LOGIC-RESET':
130 self.state = 'TEST-LOGIC-RESET' if (tms) else 'RUN-TEST/IDLE'
131 elif self.state == 'RUN-TEST/IDLE':
132 self.state = 'SELECT-DR-SCAN' if (tms) else 'RUN-TEST/IDLE'
133
134 # DR "tree"
135 elif self.state == 'SELECT-DR-SCAN':
136 self.state = 'SELECT-IR-SCAN' if (tms) else 'CAPTURE-DR'
137 elif self.state == 'CAPTURE-DR':
138 self.state = 'EXIT1-DR' if (tms) else 'SHIFT-DR'
139 elif self.state == 'SHIFT-DR':
140 self.state = 'EXIT1-DR' if (tms) else 'SHIFT-DR'
141 elif self.state == 'EXIT1-DR':
142 self.state = 'UPDATE-DR' if (tms) else 'PAUSE-DR'
143 elif self.state == 'PAUSE-DR':
144 self.state = 'EXIT2-DR' if (tms) else 'PAUSE-DR'
145 elif self.state == 'EXIT2-DR':
146 self.state = 'UPDATE-DR' if (tms) else 'SHIFT-DR'
147 elif self.state == 'UPDATE-DR':
148 self.state = 'SELECT-DR-SCAN' if (tms) else 'RUN-TEST/IDLE'
149
150 # IR "tree"
151 elif self.state == 'SELECT-IR-SCAN':
152 self.state = 'TEST-LOGIC-RESET' if (tms) else 'CAPTURE-IR'
153 elif self.state == 'CAPTURE-IR':
154 self.state = 'EXIT1-IR' if (tms) else 'SHIFT-IR'
155 elif self.state == 'SHIFT-IR':
156 self.state = 'EXIT1-IR' if (tms) else 'SHIFT-IR'
157 elif self.state == 'EXIT1-IR':
158 self.state = 'UPDATE-IR' if (tms) else 'PAUSE-IR'
159 elif self.state == 'PAUSE-IR':
160 self.state = 'EXIT2-IR' if (tms) else 'PAUSE-IR'
161 elif self.state == 'EXIT2-IR':
162 self.state = 'UPDATE-IR' if (tms) else 'SHIFT-IR'
163 elif self.state == 'UPDATE-IR':
164 self.state = 'SELECT-DR-SCAN' if (tms) else 'RUN-TEST/IDLE'
165
6d990fe1 166 def handle_rising_tck_edge(self, tdi, tdo, tck, tms):
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
80214a11
UH
181 # Upon SHIFT-IR/SHIFT-DR collect the current TDI/TDO values.
182 if self.state.startswith('SHIFT-'):
183 if self.first_bit:
184 self.ss_bitstring = self.samplenum
185 self.first_bit = False
186 else:
187 self.putx([16, [str(self.bits_tdi[0])]])
188 self.putx([17, [str(self.bits_tdo[0])]])
189 self.putp([self.state[-2:] + ' TDI BIT', str(self.bits_tdi[0])])
190 self.putp([self.state[-2:] + ' TDO BIT', str(self.bits_tdo[0])])
e5edf39f
UH
191 self.bits_tdi.insert(0, tdi)
192 self.bits_tdo.insert(0, tdo)
e5edf39f
UH
193
194 # Output all TDI/TDO bits if we just switched from SHIFT-* to EXIT1-*.
195 if self.oldstate.startswith('SHIFT-') and \
196 self.state.startswith('EXIT1-'):
197
80214a11
UH
198 self.es_bitstring = self.samplenum
199
e5edf39f
UH
200 t = self.state[-2:] + ' TDI'
201 b = ''.join(map(str, self.bits_tdi))
8189738e
UH
202 h = ' (0x%x' % int('0b' + b, 2) + ')'
203 s = t + ': ' + b + h + ', ' + str(len(self.bits_tdi)) + ' bits'
80214a11
UH
204 self.putx_bs([18, [s]])
205 self.putp_bs([t, b])
206 self.putx([16, [str(self.bits_tdi[0])]]) # Last bit.
207 self.putp([t + ' BIT', str(self.bits_tdi[0])]) # Last bit.
e5edf39f 208 self.bits_tdi = []
557a143d 209
e5edf39f
UH
210 t = self.state[-2:] + ' TDO'
211 b = ''.join(map(str, self.bits_tdo))
8189738e
UH
212 h = ' (0x%x' % int('0b' + b, 2) + ')'
213 s = t + ': ' + b + h + ', ' + str(len(self.bits_tdo)) + ' bits'
80214a11
UH
214 self.putx_bs([19, [s]])
215 self.putp_bs([t, b])
216 self.putx([17, [str(self.bits_tdo[0])]]) # Last bit.
217 self.putp([t + ' BIT', str(self.bits_tdo[0])]) # Last bit.
557a143d
UH
218 self.bits_tdo = []
219
80214a11
UH
220 self.first_bit = True
221
222 self.ss_bitstring = self.samplenum
223
224 self.ss_item = self.samplenum
225
557a143d 226 def decode(self, ss, es, data):
6b32f928 227 for (self.samplenum, pins) in data:
557a143d
UH
228
229 # If none of the pins changed, there's nothing to do.
230 if self.oldpins == pins:
231 continue
232
233 # Store current pin values for the next round.
234 self.oldpins = pins
235
236 # Get individual pin values into local variables.
6a15597a 237 # Unused channels will have a value of > 1.
1cc590f7 238 (tdi, tdo, tck, tms, trst, srst, rtck) = pins
557a143d
UH
239
240 # We only care about TCK edges (either rising or falling).
241 if (self.oldtck == tck):
242 continue
243
244 # Store start/end sample for later usage.
245 self.ss, self.es = ss, es
246
247 if (self.oldtck == 0 and tck == 1):
6d990fe1 248 self.handle_rising_tck_edge(tdi, tdo, tck, tms)
557a143d
UH
249
250 self.oldtck = tck