]> sigrok.org Git - libsigrokdecode.git/blame - decoders/jtag/pd.py
All PDs: More consistent OUTPUT_PYTHON format docs.
[libsigrokdecode.git] / decoders / jtag / pd.py
CommitLineData
557a143d 1##
50bd5d25 2## This file is part of the libsigrokdecode project.
557a143d 3##
6b32f928 4## Copyright (C) 2012-2013 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.
39 - ...
40
41All bitstrings are a sequence of '1' and '0' characters. The right-most
42character in the bitstring is the LSB. Example: '01110001' (1 is LSB).
43'''
44
0c0368d0
UH
45jtag_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 56class Decoder(srd.Decoder):
12851357 57 api_version = 2
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
BV
75 )
76 annotations = tuple([tuple([s.lower(), s]) for s in jtag_states])
557a143d
UH
77
78 def __init__(self, **kwargs):
e5edf39f
UH
79 # self.state = 'TEST-LOGIC-RESET'
80 self.state = 'RUN-TEST/IDLE'
81 self.oldstate = None
6d990fe1 82 self.oldpins = (-1, -1, -1, -1)
557a143d
UH
83 self.oldtck = -1
84 self.bits_tdi = []
85 self.bits_tdo = []
6b32f928 86 self.samplenum = 0
0c0368d0
UH
87 self.ss_item = self.es_item = None
88 self.saved_item = None
89 self.first = True
557a143d 90
8915b346 91 def start(self):
c515eed7 92 self.out_python = self.register(srd.OUTPUT_PYTHON)
be465111 93 self.out_ann = self.register(srd.OUTPUT_ANN)
557a143d 94
6b32f928 95 def putx(self, data):
0c0368d0 96 self.put(self.ss_item, self.es_item, self.out_ann, data)
6b32f928
UH
97
98 def putp(self, data):
c515eed7 99 self.put(self.ss_item, self.es_item, self.out_python, data)
6b32f928 100
557a143d 101 def advance_state_machine(self, tms):
e5edf39f
UH
102 self.oldstate = self.state
103
557a143d
UH
104 # Intro "tree"
105 if self.state == 'TEST-LOGIC-RESET':
106 self.state = 'TEST-LOGIC-RESET' if (tms) else 'RUN-TEST/IDLE'
107 elif self.state == 'RUN-TEST/IDLE':
108 self.state = 'SELECT-DR-SCAN' if (tms) else 'RUN-TEST/IDLE'
109
110 # DR "tree"
111 elif self.state == 'SELECT-DR-SCAN':
112 self.state = 'SELECT-IR-SCAN' if (tms) else 'CAPTURE-DR'
113 elif self.state == 'CAPTURE-DR':
114 self.state = 'EXIT1-DR' if (tms) else 'SHIFT-DR'
115 elif self.state == 'SHIFT-DR':
116 self.state = 'EXIT1-DR' if (tms) else 'SHIFT-DR'
117 elif self.state == 'EXIT1-DR':
118 self.state = 'UPDATE-DR' if (tms) else 'PAUSE-DR'
119 elif self.state == 'PAUSE-DR':
120 self.state = 'EXIT2-DR' if (tms) else 'PAUSE-DR'
121 elif self.state == 'EXIT2-DR':
122 self.state = 'UPDATE-DR' if (tms) else 'SHIFT-DR'
123 elif self.state == 'UPDATE-DR':
124 self.state = 'SELECT-DR-SCAN' if (tms) else 'RUN-TEST/IDLE'
125
126 # IR "tree"
127 elif self.state == 'SELECT-IR-SCAN':
128 self.state = 'TEST-LOGIC-RESET' if (tms) else 'CAPTURE-IR'
129 elif self.state == 'CAPTURE-IR':
130 self.state = 'EXIT1-IR' if (tms) else 'SHIFT-IR'
131 elif self.state == 'SHIFT-IR':
132 self.state = 'EXIT1-IR' if (tms) else 'SHIFT-IR'
133 elif self.state == 'EXIT1-IR':
134 self.state = 'UPDATE-IR' if (tms) else 'PAUSE-IR'
135 elif self.state == 'PAUSE-IR':
136 self.state = 'EXIT2-IR' if (tms) else 'PAUSE-IR'
137 elif self.state == 'EXIT2-IR':
138 self.state = 'UPDATE-IR' if (tms) else 'SHIFT-IR'
139 elif self.state == 'UPDATE-IR':
140 self.state = 'SELECT-DR-SCAN' if (tms) else 'RUN-TEST/IDLE'
141
142 else:
143 raise Exception('Invalid state: %s' % self.state)
144
6d990fe1 145 def handle_rising_tck_edge(self, tdi, tdo, tck, tms):
557a143d
UH
146 # Rising TCK edges always advance the state machine.
147 self.advance_state_machine(tms)
148
0c0368d0
UH
149 if self.first == True:
150 # Save the start sample and item for later (no output yet).
151 self.ss_item = self.samplenum
152 self.first = False
153 self.saved_item = self.state
154 else:
155 # Output the saved item (from the last CLK edge to the current).
156 self.es_item = self.samplenum
157 # Output the state we just switched to.
158 self.putx([jtag_states.index(self.state), [self.state]])
159 self.putp(['NEW STATE', self.state])
160 self.ss_item = self.samplenum
161 self.saved_item = self.state
e5edf39f
UH
162
163 # If we went from SHIFT-IR to SHIFT-IR, or SHIFT-DR to SHIFT-DR,
164 # collect the current TDI/TDO values (upon rising TCK edge).
165 if self.state.startswith('SHIFT-') and self.oldstate == self.state:
166 self.bits_tdi.insert(0, tdi)
167 self.bits_tdo.insert(0, tdo)
168 # TODO: ANN/PROTO output.
6b32f928
UH
169 # self.putx([0, ['TDI add: ' + str(tdi)]])
170 # self.putp([0, ['TDO add: ' + str(tdo)]])
e5edf39f
UH
171
172 # Output all TDI/TDO bits if we just switched from SHIFT-* to EXIT1-*.
173 if self.oldstate.startswith('SHIFT-') and \
174 self.state.startswith('EXIT1-'):
175
176 t = self.state[-2:] + ' TDI'
177 b = ''.join(map(str, self.bits_tdi))
8189738e
UH
178 h = ' (0x%x' % int('0b' + b, 2) + ')'
179 s = t + ': ' + b + h + ', ' + str(len(self.bits_tdi)) + ' bits'
6b32f928
UH
180 # self.putx([0, [s]])
181 # self.putp([t, b])
e5edf39f 182 self.bits_tdi = []
557a143d 183
e5edf39f
UH
184 t = self.state[-2:] + ' TDO'
185 b = ''.join(map(str, self.bits_tdo))
8189738e
UH
186 h = ' (0x%x' % int('0b' + b, 2) + ')'
187 s = t + ': ' + b + h + ', ' + str(len(self.bits_tdo)) + ' bits'
6b32f928
UH
188 # self.putx([0, [s]])
189 # self.putp([t, b])
557a143d
UH
190 self.bits_tdo = []
191
192 def decode(self, ss, es, data):
6b32f928 193 for (self.samplenum, pins) in data:
557a143d
UH
194
195 # If none of the pins changed, there's nothing to do.
196 if self.oldpins == pins:
197 continue
198
199 # Store current pin values for the next round.
200 self.oldpins = pins
201
202 # Get individual pin values into local variables.
6a15597a 203 # Unused channels will have a value of > 1.
1cc590f7 204 (tdi, tdo, tck, tms, trst, srst, rtck) = pins
557a143d
UH
205
206 # We only care about TCK edges (either rising or falling).
207 if (self.oldtck == tck):
208 continue
209
210 # Store start/end sample for later usage.
211 self.ss, self.es = ss, es
212
6b32f928
UH
213 # self.putx([0, ['tdi:%s, tdo:%s, tck:%s, tms:%s' \
214 # % (tdi, tdo, tck, tms)]])
e5edf39f 215
557a143d 216 if (self.oldtck == 0 and tck == 1):
6d990fe1 217 self.handle_rising_tck_edge(tdi, tdo, tck, tms)
557a143d
UH
218
219 self.oldtck = tck
220