]>
Commit | Line | Data |
---|---|---|
1 | ## | |
2 | ## This file is part of the libsigrokdecode project. | |
3 | ## | |
4 | ## Copyright (C) 2013-2016 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, see <http://www.gnu.org/licenses/>. | |
18 | ## | |
19 | ||
20 | import sigrokdecode as srd | |
21 | from common.srdhelper import bitpack | |
22 | ||
23 | ''' | |
24 | OUTPUT_PYTHON format: | |
25 | ||
26 | Packet: | |
27 | [<ptype>, <pdata>] | |
28 | ||
29 | <ptype>, <pdata> | |
30 | - 'ITEM', [<item>, <itembitsize>] | |
31 | - 'WORD', [<word>, <wordbitsize>, <worditemcount>] | |
32 | ||
33 | <item>: | |
34 | - A single item (a number). It can be of arbitrary size. The max. number | |
35 | of bits in this item is specified in <itembitsize>. | |
36 | ||
37 | <itembitsize>: | |
38 | - The size of an item (in bits). For a 4-bit parallel bus this is 4, | |
39 | for a 16-bit parallel bus this is 16, and so on. | |
40 | ||
41 | <word>: | |
42 | - A single word (a number). It can be of arbitrary size. The max. number | |
43 | of bits in this word is specified in <wordbitsize>. The (exact) number | |
44 | of items in this word is specified in <worditemcount>. | |
45 | ||
46 | <wordbitsize>: | |
47 | - The size of a word (in bits). For a 2-item word with 8-bit items | |
48 | <wordbitsize> is 16, for a 3-item word with 4-bit items <wordbitsize> | |
49 | is 12, and so on. | |
50 | ||
51 | <worditemcount>: | |
52 | - The size of a word (in number of items). For a 4-item word (no matter | |
53 | how many bits each item consists of) <worditemcount> is 4, for a 7-item | |
54 | word <worditemcount> is 7, and so on. | |
55 | ''' | |
56 | ||
57 | NUM_CHANNELS = 8 | |
58 | ||
59 | class Pin: | |
60 | CLOCK = 0 | |
61 | DATA_0 = CLOCK + 1 | |
62 | DATA_N = DATA_0 + NUM_CHANNELS | |
63 | # BEWARE! DATA_N points _beyond_ the data partition (Python range(3) | |
64 | # semantics, useful to have to simplify other code locations). | |
65 | RESET = DATA_N | |
66 | ||
67 | class Ann: | |
68 | ITEM, WORD = range(2) | |
69 | ||
70 | class ChannelError(Exception): | |
71 | pass | |
72 | ||
73 | class Decoder(srd.Decoder): | |
74 | api_version = 3 | |
75 | id = 'parallel' | |
76 | name = 'Parallel' | |
77 | longname = 'Parallel sync bus' | |
78 | desc = 'Generic parallel synchronous bus.' | |
79 | license = 'gplv2+' | |
80 | inputs = ['logic'] | |
81 | outputs = ['parallel'] | |
82 | tags = ['Util'] | |
83 | optional_channels = tuple( | |
84 | [{'id': 'clk', 'name': 'CLK', 'desc': 'Clock line'}] + | |
85 | [ | |
86 | {'id': 'd%d' % i, 'name': 'D%d' % i, 'desc': 'Data line %d' % i} | |
87 | for i in range(NUM_CHANNELS) | |
88 | ] + | |
89 | [{'id': 'rst', 'name': 'RST', 'desc': 'RESET line'}] | |
90 | ) | |
91 | options = ( | |
92 | {'id': 'clock_edge', 'desc': 'Clock edge to sample on', | |
93 | 'default': 'rising', 'values': ('rising', 'falling', 'either')}, | |
94 | {'id': 'reset_polarity', 'desc': 'Reset line polarity', | |
95 | 'default': 'low-active', 'values': ('low-active', 'high-active')}, | |
96 | {'id': 'wordsize', 'desc': 'Data wordsize (# bus cycles)', | |
97 | 'default': 0}, | |
98 | {'id': 'endianness', 'desc': 'Data endianness', | |
99 | 'default': 'little', 'values': ('little', 'big')}, | |
100 | ) | |
101 | annotations = ( | |
102 | ('item', 'Item'), | |
103 | ('word', 'Word'), | |
104 | ) | |
105 | annotation_rows = ( | |
106 | ('items', 'Items', (Ann.ITEM,)), | |
107 | ('words', 'Words', (Ann.WORD,)), | |
108 | ) | |
109 | ||
110 | def __init__(self): | |
111 | self.reset() | |
112 | ||
113 | def reset(self): | |
114 | self.items = [] | |
115 | self.saved_item = None | |
116 | self.ss_item = self.es_item = None | |
117 | self.saved_word = None | |
118 | self.ss_word = self.es_word = None | |
119 | self.first = True | |
120 | ||
121 | def start(self): | |
122 | self.out_python = self.register(srd.OUTPUT_PYTHON) | |
123 | self.out_ann = self.register(srd.OUTPUT_ANN) | |
124 | ||
125 | def putpb(self, data): | |
126 | self.put(self.ss_item, self.es_item, self.out_python, data) | |
127 | ||
128 | def putb(self, data): | |
129 | self.put(self.ss_item, self.es_item, self.out_ann, data) | |
130 | ||
131 | def putpw(self, data): | |
132 | self.put(self.ss_word, self.es_word, self.out_python, data) | |
133 | ||
134 | def putw(self, data): | |
135 | self.put(self.ss_word, self.es_word, self.out_ann, data) | |
136 | ||
137 | def handle_bits(self, item, used_pins): | |
138 | ||
139 | # If a word was previously accumulated, then emit its annotation | |
140 | # now after its end samplenumber became available. | |
141 | if self.saved_word is not None: | |
142 | if self.options['wordsize'] > 0: | |
143 | self.es_word = self.samplenum | |
144 | self.putw([Ann.WORD, [self.fmt_word.format(self.saved_word)]]) | |
145 | self.putpw(['WORD', self.saved_word]) | |
146 | self.saved_word = None | |
147 | ||
148 | # Defer annotations for individual items until the next sample | |
149 | # is taken, and the previous sample's end samplenumber has | |
150 | # become available. | |
151 | if self.first: | |
152 | # Save the start sample and item for later (no output yet). | |
153 | self.ss_item = self.samplenum | |
154 | self.first = False | |
155 | self.saved_item = item | |
156 | elif self.saved_item is None: | |
157 | pass | |
158 | else: | |
159 | # Output the saved item (from the last CLK edge to the current). | |
160 | self.es_item = self.samplenum | |
161 | self.putpb(['ITEM', self.saved_item]) | |
162 | self.putb([Ann.ITEM, [self.fmt_item.format(self.saved_item)]]) | |
163 | self.ss_item = self.samplenum | |
164 | self.saved_item = item | |
165 | ||
166 | # Get as many items as the configured wordsize specifies. | |
167 | if not self.items: | |
168 | self.ss_word = self.samplenum | |
169 | self.items.append(item) | |
170 | ws = self.options['wordsize'] | |
171 | if len(self.items) < ws: | |
172 | return | |
173 | ||
174 | # Collect words and prepare annotation details, but defer emission | |
175 | # until the end samplenumber becomes available. | |
176 | endian = self.options['endianness'] | |
177 | if endian == 'big': | |
178 | self.items.reverse() | |
179 | word = sum([self.items[i] << (i * used_pins) for i in range(ws)]) | |
180 | self.saved_word = word | |
181 | self.items = [] | |
182 | ||
183 | def decode(self): | |
184 | # Determine which (optional) channels have input data. Insist in | |
185 | # a non-empty input data set. Cope with sparse connection maps. | |
186 | # Store enough state to later "compress" sampled input data. | |
187 | data_indices = [ | |
188 | idx if self.has_channel(idx) else None | |
189 | for idx in range(Pin.DATA_0, Pin.DATA_N) | |
190 | ] | |
191 | has_data = [idx for idx in data_indices if idx is not None] | |
192 | if not has_data: | |
193 | raise ChannelError('Need at least one data channel.') | |
194 | max_connected = max(has_data) | |
195 | ||
196 | # Pre-determine which input data to strip off, the width of | |
197 | # individual items and multiplexed words, as well as format | |
198 | # strings here. This simplifies call sites which run in tight | |
199 | # loops later. | |
200 | upper_data_bound = max_connected + 1 | |
201 | num_item_bits = upper_data_bound - Pin.DATA_0 | |
202 | num_word_items = self.options['wordsize'] | |
203 | num_word_bits = num_item_bits * num_word_items | |
204 | num_digits = (num_item_bits + 4 - 1) // 4 | |
205 | self.fmt_item = "{{:0{}x}}".format(num_digits) | |
206 | num_digits = (num_word_bits + 4 - 1) // 4 | |
207 | self.fmt_word = "{{:0{}x}}".format(num_digits) | |
208 | ||
209 | # Determine .wait() conditions, depending on the presence of a | |
210 | # clock signal. Either inspect samples on the configured edge of | |
211 | # the clock, or inspect samples upon ANY edge of ANY of the pins | |
212 | # which provide input data. | |
213 | conds = [] | |
214 | cond_idx_clock = None | |
215 | cond_idx_data_0 = None | |
216 | cond_idx_data_N = None | |
217 | cond_idx_reset = None | |
218 | has_clock = self.has_channel(Pin.CLOCK) | |
219 | if has_clock: | |
220 | cond_idx_clock = len(conds) | |
221 | edge = { | |
222 | 'rising': 'r', | |
223 | 'falling': 'f', | |
224 | 'either': 'e', | |
225 | }.get(self.options['clock_edge']) | |
226 | conds.append({Pin.CLOCK: edge}) | |
227 | else: | |
228 | cond_idx_data_0 = len(conds) | |
229 | conds.extend([{idx: 'e'} for idx in has_data]) | |
230 | cond_idx_data_N = len(conds) | |
231 | has_reset = self.has_channel(Pin.RESET) | |
232 | if has_reset: | |
233 | cond_idx_reset = len(conds) | |
234 | conds.append({Pin.RESET: 'e'}) | |
235 | reset_active = { | |
236 | 'low-active': 0, | |
237 | 'high-active': 1, | |
238 | }.get(self.options['reset_polarity']) | |
239 | ||
240 | # Keep processing the input stream. Assume "always zero" for | |
241 | # not-connected input lines. Pass data bits (all inputs except | |
242 | # clock and reset) to the handle_bits() method. Handle reset | |
243 | # edges first and data changes then, within the same iteration. | |
244 | # This results in robust operation for low-oversampled input. | |
245 | in_reset = False | |
246 | while True: | |
247 | pins = self.wait(conds) | |
248 | clock_edge = cond_idx_clock is not None and self.matched[cond_idx_clock] | |
249 | data_edge = cond_idx_data_0 is not None and [idx for idx in range(cond_idx_data_0, cond_idx_data_N) if self.matched[idx]] | |
250 | reset_edge = cond_idx_reset is not None and self.matched[cond_idx_reset] | |
251 | ||
252 | if reset_edge: | |
253 | in_reset = pins[Pin.RESET] == reset_active | |
254 | if in_reset: | |
255 | self.handle_bits(None, num_item_bits) | |
256 | self.saved_item = None | |
257 | self.saved_word = None | |
258 | self.first = True | |
259 | if in_reset: | |
260 | continue | |
261 | ||
262 | if clock_edge or data_edge: | |
263 | data_bits = [0 if idx is None else pins[idx] for idx in data_indices] | |
264 | data_bits = data_bits[:num_item_bits] | |
265 | item = bitpack(data_bits) | |
266 | self.handle_bits(item, num_item_bits) |