]> sigrok.org Git - libsigrokdecode.git/blob - decoders/jtag/jtag.py
srd: Remove TODOs from annotation format names.
[libsigrokdecode.git] / decoders / jtag / jtag.py
1 ##
2 ## This file is part of the sigrok project.
3 ##
4 ## Copyright (C) 2012 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 # JTAG protocol decoder
22
23 import sigrokdecode as srd
24
25 class Decoder(srd.Decoder):
26     api_version = 1
27     id = 'jtag'
28     name = 'JTAG'
29     longname = 'Joint Test Action Group'
30     desc = 'Protocol for testing, debugging, and flashing ICs.'
31     license = 'gplv2+'
32     inputs = ['logic']
33     outputs = ['jtag']
34     probes = [
35         {'id': 'tdi',  'name': 'TDI',  'desc': 'Test data input'},
36         {'id': 'tdo',  'name': 'TDO',  'desc': 'Test data output'},
37         {'id': 'tck',  'name': 'TCK',  'desc': 'Test clock'},
38         {'id': 'tms',  'name': 'TMS',  'desc': 'Test mode select'},
39     ]
40     optional_probes = [
41         # {'id': 'trst', 'name': 'TRST#', 'desc': 'Test reset'},
42         # {'id': 'srst', 'name': 'SRST#', 'desc': 'System reset'},
43     ]
44     options = {}
45     annotations = [
46         ['Text', 'Human-readable text'],
47     ]
48
49     def __init__(self, **kwargs):
50         # self.state = 'TEST-LOGIC-RESET'
51         self.state = 'RUN-TEST/IDLE'
52         self.oldstate = None
53         self.oldpins = (-1, -1, -1, -1)
54         self.oldtck = -1
55         self.bits_tdi = []
56         self.bits_tdo = []
57
58     def start(self, metadata):
59         self.out_proto = self.add(srd.OUTPUT_PROTO, 'jtag')
60         self.out_ann = self.add(srd.OUTPUT_ANN, 'jtag')
61
62     def report(self):
63         pass
64
65     def advance_state_machine(self, tms):
66         self.oldstate = self.state
67
68         # Intro "tree"
69         if self.state == 'TEST-LOGIC-RESET':
70             self.state = 'TEST-LOGIC-RESET' if (tms) else 'RUN-TEST/IDLE'
71         elif self.state == 'RUN-TEST/IDLE':
72             self.state = 'SELECT-DR-SCAN' if (tms) else 'RUN-TEST/IDLE'
73
74         # DR "tree"
75         elif self.state == 'SELECT-DR-SCAN':
76             self.state = 'SELECT-IR-SCAN' if (tms) else 'CAPTURE-DR'
77         elif self.state == 'CAPTURE-DR':
78             self.state = 'EXIT1-DR' if (tms) else 'SHIFT-DR'
79         elif self.state == 'SHIFT-DR':
80             self.state = 'EXIT1-DR' if (tms) else 'SHIFT-DR'
81         elif self.state == 'EXIT1-DR':
82             self.state = 'UPDATE-DR' if (tms) else 'PAUSE-DR'
83         elif self.state == 'PAUSE-DR':
84             self.state = 'EXIT2-DR' if (tms) else 'PAUSE-DR'
85         elif self.state == 'EXIT2-DR':
86             self.state = 'UPDATE-DR' if (tms) else 'SHIFT-DR'
87         elif self.state == 'UPDATE-DR':
88             self.state = 'SELECT-DR-SCAN' if (tms) else 'RUN-TEST/IDLE'
89
90         # IR "tree"
91         elif self.state == 'SELECT-IR-SCAN':
92             self.state = 'TEST-LOGIC-RESET' if (tms) else 'CAPTURE-IR'
93         elif self.state == 'CAPTURE-IR':
94             self.state = 'EXIT1-IR' if (tms) else 'SHIFT-IR'
95         elif self.state == 'SHIFT-IR':
96             self.state = 'EXIT1-IR' if (tms) else 'SHIFT-IR'
97         elif self.state == 'EXIT1-IR':
98             self.state = 'UPDATE-IR' if (tms) else 'PAUSE-IR'
99         elif self.state == 'PAUSE-IR':
100             self.state = 'EXIT2-IR' if (tms) else 'PAUSE-IR'
101         elif self.state == 'EXIT2-IR':
102             self.state = 'UPDATE-IR' if (tms) else 'SHIFT-IR'
103         elif self.state == 'UPDATE-IR':
104             self.state = 'SELECT-DR-SCAN' if (tms) else 'RUN-TEST/IDLE'
105
106         else:
107             raise Exception('Invalid state: %s' % self.state)
108
109     def handle_rising_tck_edge(self, tdi, tdo, tck, tms):
110         # Rising TCK edges always advance the state machine.
111         self.advance_state_machine(tms)
112
113         # Output the state we just switched to.
114         self.put(self.ss, self.es, self.out_ann,
115                  [0, ['New state: %s' % self.state]])
116         self.put(self.ss, self.es, self.out_proto,
117                  ['NEW STATE', self.state])
118
119         # If we went from SHIFT-IR to SHIFT-IR, or SHIFT-DR to SHIFT-DR,
120         # collect the current TDI/TDO values (upon rising TCK edge).
121         if self.state.startswith('SHIFT-') and self.oldstate == self.state:
122             self.bits_tdi.insert(0, tdi)
123             self.bits_tdo.insert(0, tdo)
124             # TODO: ANN/PROTO output.
125             # self.put(self.ss, self.es, self.out_ann,
126             #          [0, ['TDI add: ' + str(tdi)]])
127             # self.put(self.ss, self.es, self.out_ann,
128             #          [0, ['TDO add: ' + str(tdo)]])
129
130         # Output all TDI/TDO bits if we just switched from SHIFT-* to EXIT1-*.
131         if self.oldstate.startswith('SHIFT-') and \
132            self.state.startswith('EXIT1-'):
133
134             t = self.state[-2:] + ' TDI'
135             b = ''.join(map(str, self.bits_tdi))
136             h = ' (0x%x' % int('0b' + b, 2) + ')'
137             s = t + ': ' + b + h + ', ' + str(len(self.bits_tdi)) + ' bits'
138             self.put(self.ss, self.es, self.out_ann, [0, [s]])
139             self.put(self.ss, self.es, self.out_proto, [t, b])
140             self.bits_tdi = []
141
142             t = self.state[-2:] + ' TDO'
143             b = ''.join(map(str, self.bits_tdo))
144             h = ' (0x%x' % int('0b' + b, 2) + ')'
145             s = t + ': ' + b + h + ', ' + str(len(self.bits_tdo)) + ' bits'
146             self.put(self.ss, self.es, self.out_ann, [0, [s]])
147             self.put(self.ss, self.es, self.out_proto, [t, b])
148             self.bits_tdo = []
149
150     def decode(self, ss, es, data):
151         for (samplenum, pins) in data:
152
153             # If none of the pins changed, there's nothing to do.
154             if self.oldpins == pins:
155                 continue
156
157             # Store current pin values for the next round.
158             self.oldpins = pins
159
160             # Get individual pin values into local variables.
161             # TODO: Handle optional pins (TRST, SRST).
162             (tdi, tdo, tck, tms) = pins
163
164             # We only care about TCK edges (either rising or falling).
165             if (self.oldtck == tck):
166                 continue
167
168             # Store start/end sample for later usage.
169             self.ss, self.es = ss, es
170
171             # self.put(self.ss, self.es, self.out_ann,
172             #     [0, ['tdi:%s, tdo:%s, tck:%s, tms:%s' \
173             #          % (tdi, tdo, tck, tms)]])
174
175             if (self.oldtck == 0 and tck == 1):
176                 self.handle_rising_tck_edge(tdi, tdo, tck, tms)
177
178             self.oldtck = tck
179