]> sigrok.org Git - libsigrokdecode.git/blame - decoders/jtag/pd.py
jtag: Convert to PD API version 3.
[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'.
35 - 'IR TDI': Bitstring that was clocked into the IR register.
36 - 'IR TDO': Bitstring that was clocked out of the IR register.
37 - 'DR TDI': Bitstring that was clocked into the DR register.
38 - 'DR TDO': Bitstring that was clocked out of the DR register.
4c3b1846 39
36faa6d0
UH
40All bitstrings are a list consisting of two items. The first is a sequence
41of '1' and '0' characters (the right-most character is the LSB. Example:
42'01110001', where 1 is the LSB). The second item is a list of ss/es values
43for each bit that is in the bitstring.
4c3b1846
UH
44'''
45
0c0368d0
UH
46jtag_states = [
47 # Intro "tree"
48 'TEST-LOGIC-RESET', 'RUN-TEST/IDLE',
49 # DR "tree"
50 'SELECT-DR-SCAN', 'CAPTURE-DR', 'UPDATE-DR', 'PAUSE-DR',
51 'SHIFT-DR', 'EXIT1-DR', 'EXIT2-DR',
52 # IR "tree"
53 'SELECT-IR-SCAN', 'CAPTURE-IR', 'UPDATE-IR', 'PAUSE-IR',
54 'SHIFT-IR', 'EXIT1-IR', 'EXIT2-IR',
55]
56
557a143d 57class Decoder(srd.Decoder):
2d557217 58 api_version = 3
557a143d
UH
59 id = 'jtag'
60 name = 'JTAG'
b7a7e6f5 61 longname = 'Joint Test Action Group (IEEE 1149.1)'
6e7a0087 62 desc = 'Protocol for testing, debugging, and flashing ICs.'
557a143d
UH
63 license = 'gplv2+'
64 inputs = ['logic']
65 outputs = ['jtag']
6a15597a 66 channels = (
557a143d
UH
67 {'id': 'tdi', 'name': 'TDI', 'desc': 'Test data input'},
68 {'id': 'tdo', 'name': 'TDO', 'desc': 'Test data output'},
69 {'id': 'tck', 'name': 'TCK', 'desc': 'Test clock'},
70 {'id': 'tms', 'name': 'TMS', 'desc': 'Test mode select'},
da9bcbd9 71 )
6a15597a 72 optional_channels = (
1cc590f7
UH
73 {'id': 'trst', 'name': 'TRST#', 'desc': 'Test reset'},
74 {'id': 'srst', 'name': 'SRST#', 'desc': 'System reset'},
75 {'id': 'rtck', 'name': 'RTCK', 'desc': 'Return clock signal'},
da9bcbd9 76 )
80214a11
UH
77 annotations = tuple([tuple([s.lower(), s]) for s in jtag_states]) + ( \
78 ('bit-tdi', 'Bit (TDI)'),
79 ('bit-tdo', 'Bit (TDO)'),
80 ('bitstring-tdi', 'Bitstring (TDI)'),
81 ('bitstring-tdo', 'Bitstring (TDO)'),
82 )
83 annotation_rows = (
84 ('bits-tdi', 'Bits (TDI)', (16,)),
85 ('bits-tdo', 'Bits (TDO)', (17,)),
86 ('bitstrings-tdi', 'Bitstring (TDI)', (18,)),
87 ('bitstrings-tdo', 'Bitstring (TDO)', (19,)),
88 ('states', 'States', tuple(range(15 + 1))),
89 )
557a143d 90
92b7b49f 91 def __init__(self):
e5edf39f
UH
92 # self.state = 'TEST-LOGIC-RESET'
93 self.state = 'RUN-TEST/IDLE'
94 self.oldstate = None
557a143d
UH
95 self.bits_tdi = []
96 self.bits_tdo = []
36faa6d0
UH
97 self.bits_samplenums_tdi = []
98 self.bits_samplenums_tdo = []
0c0368d0 99 self.ss_item = self.es_item = None
80214a11 100 self.ss_bitstring = self.es_bitstring = None
0c0368d0
UH
101 self.saved_item = None
102 self.first = True
80214a11 103 self.first_bit = True
557a143d 104
8915b346 105 def start(self):
c515eed7 106 self.out_python = self.register(srd.OUTPUT_PYTHON)
be465111 107 self.out_ann = self.register(srd.OUTPUT_ANN)
557a143d 108
6b32f928 109 def putx(self, data):
0c0368d0 110 self.put(self.ss_item, self.es_item, self.out_ann, data)
6b32f928
UH
111
112 def putp(self, data):
c515eed7 113 self.put(self.ss_item, self.es_item, self.out_python, data)
6b32f928 114
80214a11
UH
115 def putx_bs(self, data):
116 self.put(self.ss_bitstring, self.es_bitstring, self.out_ann, data)
117
118 def putp_bs(self, data):
119 self.put(self.ss_bitstring, self.es_bitstring, self.out_python, data)
120
557a143d 121 def advance_state_machine(self, tms):
e5edf39f
UH
122 self.oldstate = self.state
123
557a143d
UH
124 # Intro "tree"
125 if self.state == 'TEST-LOGIC-RESET':
126 self.state = 'TEST-LOGIC-RESET' if (tms) else 'RUN-TEST/IDLE'
127 elif self.state == 'RUN-TEST/IDLE':
128 self.state = 'SELECT-DR-SCAN' if (tms) else 'RUN-TEST/IDLE'
129
130 # DR "tree"
131 elif self.state == 'SELECT-DR-SCAN':
132 self.state = 'SELECT-IR-SCAN' if (tms) else 'CAPTURE-DR'
133 elif self.state == 'CAPTURE-DR':
134 self.state = 'EXIT1-DR' if (tms) else 'SHIFT-DR'
135 elif self.state == 'SHIFT-DR':
136 self.state = 'EXIT1-DR' if (tms) else 'SHIFT-DR'
137 elif self.state == 'EXIT1-DR':
138 self.state = 'UPDATE-DR' if (tms) else 'PAUSE-DR'
139 elif self.state == 'PAUSE-DR':
140 self.state = 'EXIT2-DR' if (tms) else 'PAUSE-DR'
141 elif self.state == 'EXIT2-DR':
142 self.state = 'UPDATE-DR' if (tms) else 'SHIFT-DR'
143 elif self.state == 'UPDATE-DR':
144 self.state = 'SELECT-DR-SCAN' if (tms) else 'RUN-TEST/IDLE'
145
146 # IR "tree"
147 elif self.state == 'SELECT-IR-SCAN':
148 self.state = 'TEST-LOGIC-RESET' if (tms) else 'CAPTURE-IR'
149 elif self.state == 'CAPTURE-IR':
150 self.state = 'EXIT1-IR' if (tms) else 'SHIFT-IR'
151 elif self.state == 'SHIFT-IR':
152 self.state = 'EXIT1-IR' if (tms) else 'SHIFT-IR'
153 elif self.state == 'EXIT1-IR':
154 self.state = 'UPDATE-IR' if (tms) else 'PAUSE-IR'
155 elif self.state == 'PAUSE-IR':
156 self.state = 'EXIT2-IR' if (tms) else 'PAUSE-IR'
157 elif self.state == 'EXIT2-IR':
158 self.state = 'UPDATE-IR' if (tms) else 'SHIFT-IR'
159 elif self.state == 'UPDATE-IR':
160 self.state = 'SELECT-DR-SCAN' if (tms) else 'RUN-TEST/IDLE'
161
2d557217
UH
162 def handle_rising_tck_edge(self, pins):
163 (tdi, tdo, tck, tms, trst, srst, rtck) = pins
164
557a143d
UH
165 # Rising TCK edges always advance the state machine.
166 self.advance_state_machine(tms)
167
35b380b1 168 if self.first:
0c0368d0
UH
169 # Save the start sample and item for later (no output yet).
170 self.ss_item = self.samplenum
171 self.first = False
0c0368d0
UH
172 else:
173 # Output the saved item (from the last CLK edge to the current).
174 self.es_item = self.samplenum
80214a11
UH
175 # Output the old state (from last rising TCK edge to current one).
176 self.putx([jtag_states.index(self.oldstate), [self.oldstate]])
0c0368d0 177 self.putp(['NEW STATE', self.state])
e5edf39f 178
80214a11
UH
179 # Upon SHIFT-IR/SHIFT-DR collect the current TDI/TDO values.
180 if self.state.startswith('SHIFT-'):
181 if self.first_bit:
182 self.ss_bitstring = self.samplenum
183 self.first_bit = False
184 else:
185 self.putx([16, [str(self.bits_tdi[0])]])
186 self.putx([17, [str(self.bits_tdo[0])]])
36faa6d0
UH
187 # Use self.samplenum as ES of the previous bit.
188 self.bits_samplenums_tdi[0][1] = self.samplenum
189 self.bits_samplenums_tdo[0][1] = self.samplenum
190
e5edf39f
UH
191 self.bits_tdi.insert(0, tdi)
192 self.bits_tdo.insert(0, tdo)
e5edf39f 193
36faa6d0
UH
194 # Use self.samplenum as SS of the current bit.
195 self.bits_samplenums_tdi.insert(0, [self.samplenum, -1])
196 self.bits_samplenums_tdo.insert(0, [self.samplenum, -1])
197
e5edf39f
UH
198 # Output all TDI/TDO bits if we just switched from SHIFT-* to EXIT1-*.
199 if self.oldstate.startswith('SHIFT-') and \
200 self.state.startswith('EXIT1-'):
201
80214a11
UH
202 self.es_bitstring = self.samplenum
203
e5edf39f
UH
204 t = self.state[-2:] + ' TDI'
205 b = ''.join(map(str, self.bits_tdi))
8189738e
UH
206 h = ' (0x%x' % int('0b' + b, 2) + ')'
207 s = t + ': ' + b + h + ', ' + str(len(self.bits_tdi)) + ' bits'
80214a11 208 self.putx_bs([18, [s]])
36faa6d0
UH
209 self.bits_samplenums_tdi[0][1] = self.samplenum # ES of last bit.
210 self.putp_bs([t, [b, self.bits_samplenums_tdi]])
80214a11 211 self.putx([16, [str(self.bits_tdi[0])]]) # Last bit.
e5edf39f 212 self.bits_tdi = []
36faa6d0 213 self.bits_samplenums_tdi = []
557a143d 214
e5edf39f
UH
215 t = self.state[-2:] + ' TDO'
216 b = ''.join(map(str, self.bits_tdo))
8189738e
UH
217 h = ' (0x%x' % int('0b' + b, 2) + ')'
218 s = t + ': ' + b + h + ', ' + str(len(self.bits_tdo)) + ' bits'
80214a11 219 self.putx_bs([19, [s]])
36faa6d0
UH
220 self.bits_samplenums_tdo[0][1] = self.samplenum # ES of last bit.
221 self.putp_bs([t, [b, self.bits_samplenums_tdo]])
80214a11 222 self.putx([17, [str(self.bits_tdo[0])]]) # Last bit.
557a143d 223 self.bits_tdo = []
36faa6d0 224 self.bits_samplenums_tdo = []
557a143d 225
80214a11
UH
226 self.first_bit = True
227
228 self.ss_bitstring = self.samplenum
229
230 self.ss_item = self.samplenum
231
2d557217
UH
232 def decode(self):
233 while True:
234 # Wait for a rising edge on TCK.
235 self.handle_rising_tck_edge(self.wait({2: 'r'}))