]> sigrok.org Git - libsigrokdecode.git/blob - decoders/jtag/pd.py
b44ab527421d1e3f2dfdc5912755929d5190685a
[libsigrokdecode.git] / decoders / jtag / pd.py
1 ##
2 ## This file is part of the libsigrokdecode project.
3 ##
4 ## Copyright (C) 2012-2015 Uwe Hermann <uwe@hermann-uwe.de>
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
21 import sigrokdecode as srd
22
23 '''
24 OUTPUT_PYTHON format:
25
26 Packet:
27 [<ptype>, <pdata>]
28
29 <ptype>:
30  - 'NEW STATE': <pdata> is the new state of the JTAG state machine.
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 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.
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.
43
44 All bits are either '1' or '0' characters.
45 All bitstrings are a sequence of '1' and '0' characters. The right-most
46 character in the bitstring is the LSB. Example: '01110001' (1 is LSB).
47 '''
48
49 jtag_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
60 class Decoder(srd.Decoder):
61     api_version = 2
62     id = 'jtag'
63     name = 'JTAG'
64     longname = 'Joint Test Action Group (IEEE 1149.1)'
65     desc = 'Protocol for testing, debugging, and flashing ICs.'
66     license = 'gplv2+'
67     inputs = ['logic']
68     outputs = ['jtag']
69     channels = (
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'},
74     )
75     optional_channels = (
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'},
79     )
80     annotations = tuple([tuple([s.lower(), s]) for s in jtag_states])
81
82     def __init__(self, **kwargs):
83         # self.state = 'TEST-LOGIC-RESET'
84         self.state = 'RUN-TEST/IDLE'
85         self.oldstate = None
86         self.oldpins = (-1, -1, -1, -1)
87         self.oldtck = -1
88         self.bits_tdi = []
89         self.bits_tdo = []
90         self.samplenum = 0
91         self.ss_item = self.es_item = None
92         self.saved_item = None
93         self.first = True
94
95     def start(self):
96         self.out_python = self.register(srd.OUTPUT_PYTHON)
97         self.out_ann = self.register(srd.OUTPUT_ANN)
98
99     def putx(self, data):
100         self.put(self.ss_item, self.es_item, self.out_ann, data)
101
102     def putp(self, data):
103         self.put(self.ss_item, self.es_item, self.out_python, data)
104
105     def advance_state_machine(self, tms):
106         self.oldstate = self.state
107
108         # Intro "tree"
109         if self.state == 'TEST-LOGIC-RESET':
110             self.state = 'TEST-LOGIC-RESET' if (tms) else 'RUN-TEST/IDLE'
111         elif self.state == 'RUN-TEST/IDLE':
112             self.state = 'SELECT-DR-SCAN' if (tms) else 'RUN-TEST/IDLE'
113
114         # DR "tree"
115         elif self.state == 'SELECT-DR-SCAN':
116             self.state = 'SELECT-IR-SCAN' if (tms) else 'CAPTURE-DR'
117         elif self.state == 'CAPTURE-DR':
118             self.state = 'EXIT1-DR' if (tms) else 'SHIFT-DR'
119         elif self.state == 'SHIFT-DR':
120             self.state = 'EXIT1-DR' if (tms) else 'SHIFT-DR'
121         elif self.state == 'EXIT1-DR':
122             self.state = 'UPDATE-DR' if (tms) else 'PAUSE-DR'
123         elif self.state == 'PAUSE-DR':
124             self.state = 'EXIT2-DR' if (tms) else 'PAUSE-DR'
125         elif self.state == 'EXIT2-DR':
126             self.state = 'UPDATE-DR' if (tms) else 'SHIFT-DR'
127         elif self.state == 'UPDATE-DR':
128             self.state = 'SELECT-DR-SCAN' if (tms) else 'RUN-TEST/IDLE'
129
130         # IR "tree"
131         elif self.state == 'SELECT-IR-SCAN':
132             self.state = 'TEST-LOGIC-RESET' if (tms) else 'CAPTURE-IR'
133         elif self.state == 'CAPTURE-IR':
134             self.state = 'EXIT1-IR' if (tms) else 'SHIFT-IR'
135         elif self.state == 'SHIFT-IR':
136             self.state = 'EXIT1-IR' if (tms) else 'SHIFT-IR'
137         elif self.state == 'EXIT1-IR':
138             self.state = 'UPDATE-IR' if (tms) else 'PAUSE-IR'
139         elif self.state == 'PAUSE-IR':
140             self.state = 'EXIT2-IR' if (tms) else 'PAUSE-IR'
141         elif self.state == 'EXIT2-IR':
142             self.state = 'UPDATE-IR' if (tms) else 'SHIFT-IR'
143         elif self.state == 'UPDATE-IR':
144             self.state = 'SELECT-DR-SCAN' if (tms) else 'RUN-TEST/IDLE'
145
146     def handle_rising_tck_edge(self, tdi, tdo, tck, tms):
147         # Rising TCK edges always advance the state machine.
148         self.advance_state_machine(tms)
149
150         if self.first:
151             # Save the start sample and item for later (no output yet).
152             self.ss_item = self.samplenum
153             self.first = False
154             self.saved_item = self.state
155         else:
156             # Output the saved item (from the last CLK edge to the current).
157             self.es_item = self.samplenum
158             # Output the state we just switched to.
159             self.putx([jtag_states.index(self.state), [self.state]])
160             self.putp(['NEW STATE', self.state])
161             self.ss_item = self.samplenum
162             self.saved_item = self.state
163
164         # If we went from SHIFT-IR to SHIFT-IR, or SHIFT-DR to SHIFT-DR,
165         # collect the current TDI/TDO values (upon rising TCK edge).
166         if self.state.startswith('SHIFT-') and self.oldstate == self.state:
167             self.bits_tdi.insert(0, tdi)
168             self.bits_tdo.insert(0, tdo)
169             self.putx([0, [self.state[-2:] + ' TDI BIT: ' + str(tdi)]])
170             self.putx([0, [self.state[-2:] + ' TDO BIT: ' + str(tdo)]])
171             self.putp([self.state[-2:] + ' TDI BIT', str(tdi)])
172             self.putp([self.state[-2:] + ' TDO BIT', str(tdo)])
173
174         # Output all TDI/TDO bits if we just switched from SHIFT-* to EXIT1-*.
175         if self.oldstate.startswith('SHIFT-') and \
176            self.state.startswith('EXIT1-'):
177
178             t = self.state[-2:] + ' TDI'
179             b = ''.join(map(str, self.bits_tdi))
180             h = ' (0x%x' % int('0b' + b, 2) + ')'
181             s = t + ': ' + b + h + ', ' + str(len(self.bits_tdi)) + ' bits'
182             self.putx([0, [s]])
183             self.putp([t, b])
184             self.bits_tdi = []
185
186             t = self.state[-2:] + ' TDO'
187             b = ''.join(map(str, self.bits_tdo))
188             h = ' (0x%x' % int('0b' + b, 2) + ')'
189             s = t + ': ' + b + h + ', ' + str(len(self.bits_tdo)) + ' bits'
190             self.putx([0, [s]])
191             self.putp([t, b])
192             self.bits_tdo = []
193
194     def decode(self, ss, es, data):
195         for (self.samplenum, pins) in data:
196
197             # If none of the pins changed, there's nothing to do.
198             if self.oldpins == pins:
199                 continue
200
201             # Store current pin values for the next round.
202             self.oldpins = pins
203
204             # Get individual pin values into local variables.
205             # Unused channels will have a value of > 1.
206             (tdi, tdo, tck, tms, trst, srst, rtck) = pins
207
208             # We only care about TCK edges (either rising or falling).
209             if (self.oldtck == tck):
210                 continue
211
212             # Store start/end sample for later usage.
213             self.ss, self.es = ss, es
214
215             if (self.oldtck == 0 and tck == 1):
216                 self.handle_rising_tck_edge(tdi, tdo, tck, tms)
217
218             self.oldtck = tck