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