]>
Commit | Line | Data |
---|---|---|
25e1418a UH |
1 | ## |
2 | ## This file is part of the libsigrokdecode project. | |
3 | ## | |
8eb06a59 | 4 | ## Copyright (C) 2013-2016 Uwe Hermann <uwe@hermann-uwe.de> |
25e1418a 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 | |
4539e9ca | 17 | ## along with this program; if not, see <http://www.gnu.org/licenses/>. |
25e1418a UH |
18 | ## |
19 | ||
25e1418a | 20 | import sigrokdecode as srd |
a0b7e07f | 21 | from common.srdhelper import bitpack |
25e1418a UH |
22 | |
23 | ''' | |
c515eed7 | 24 | OUTPUT_PYTHON format: |
25e1418a UH |
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 | ||
3f77dc2a | 57 | NUM_CHANNELS = 16 |
9a41127e GS |
58 | |
59 | class Pin: | |
60 | CLOCK = 0 | |
615f86f6 GS |
61 | DATA_0 = CLOCK + 1 |
62 | DATA_N = DATA_0 + NUM_CHANNELS | |
76b64d3a GS |
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 | |
9a41127e GS |
66 | |
67 | class Ann: | |
4f4e0353 | 68 | ITEM, WORD, WARN = range(3) |
9a41127e | 69 | |
a573d394 UH |
70 | class ChannelError(Exception): |
71 | pass | |
72 | ||
25e1418a | 73 | class Decoder(srd.Decoder): |
8eb06a59 | 74 | api_version = 3 |
25e1418a UH |
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'] | |
d6d8a8a4 | 82 | tags = ['Util'] |
615f86f6 GS |
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) | |
76b64d3a GS |
88 | ] + |
89 | [{'id': 'rst', 'name': 'RST', 'desc': 'RESET line'}] | |
615f86f6 | 90 | ) |
84c1c0b5 BV |
91 | options = ( |
92 | {'id': 'clock_edge', 'desc': 'Clock edge to sample on', | |
e2317ec4 | 93 | 'default': 'rising', 'values': ('rising', 'falling', 'either')}, |
76b64d3a GS |
94 | {'id': 'reset_polarity', 'desc': 'Reset line polarity', |
95 | 'default': 'low-active', 'values': ('low-active', 'high-active')}, | |
e710d006 GS |
96 | {'id': 'wordsize', 'desc': 'Data wordsize (# bus cycles)', |
97 | 'default': 0}, | |
b0918d40 | 98 | {'id': 'endianness', 'desc': 'Data endianness', |
84c1c0b5 BV |
99 | 'default': 'little', 'values': ('little', 'big')}, |
100 | ) | |
da9bcbd9 | 101 | annotations = ( |
e144452b UH |
102 | ('item', 'Item'), |
103 | ('word', 'Word'), | |
4f4e0353 | 104 | ('warning', 'Warning'), |
da9bcbd9 | 105 | ) |
ca24954f | 106 | annotation_rows = ( |
9a41127e GS |
107 | ('items', 'Items', (Ann.ITEM,)), |
108 | ('words', 'Words', (Ann.WORD,)), | |
4f4e0353 | 109 | ('warnings', 'Warnings', (Ann.WARN,)), |
ca24954f | 110 | ) |
82f3f4d4 SA |
111 | binary = ( |
112 | ('binary', 'Binary'), | |
113 | ) | |
25e1418a UH |
114 | |
115 | def __init__(self): | |
10aeb8ea GS |
116 | self.reset() |
117 | ||
118 | def reset(self): | |
4f4e0353 GS |
119 | self.pend_item = None |
120 | self.word_items = [] | |
25e1418a | 121 | |
b098b820 | 122 | def start(self): |
c515eed7 | 123 | self.out_python = self.register(srd.OUTPUT_PYTHON) |
82f3f4d4 | 124 | self.out_binary = self.register(srd.OUTPUT_BINARY) |
be465111 | 125 | self.out_ann = self.register(srd.OUTPUT_ANN) |
25e1418a | 126 | |
4f4e0353 GS |
127 | def putg(self, ss, es, ann, txts): |
128 | self.put(ss, es, self.out_ann, [ann, txts]) | |
129 | ||
130 | def putpy(self, ss, es, ann, data): | |
131 | self.put(ss, es, self.out_python, [ann, data]) | |
132 | ||
82f3f4d4 SA |
133 | def putbin(self, ss, es, ann_class, data): |
134 | self.put(ss, es, self.out_binary, [ann_class, data]) | |
135 | ||
4f4e0353 GS |
136 | def flush_word(self, bus_width): |
137 | if not self.word_items: | |
138 | return | |
139 | word_size = self.options['wordsize'] | |
140 | ||
141 | items = self.word_items | |
142 | ss, es = items[0][0], items[-1][1] | |
143 | items = [i[2] for i in items] | |
144 | if self.options['endianness'] == 'big': | |
145 | items.reverse() | |
146 | word = sum([d << (i * bus_width) for i, d in enumerate(items)]) | |
147 | ||
148 | txts = [self.fmt_word.format(word)] | |
149 | self.putg(ss, es, Ann.WORD, txts) | |
239e15df | 150 | self.putpy(ss, es, 'WORD', (word, bus_width, word_size)) |
4f4e0353 GS |
151 | |
152 | if len(items) != word_size: | |
153 | txts = ['incomplete word size', 'word size', 'ws'] | |
154 | self.putg(ss, es, Ann.WARN, txts) | |
155 | ||
156 | self.word_items.clear() | |
157 | ||
158 | def queue_word(self, now, item, bus_width): | |
159 | wordsize = self.options['wordsize'] | |
160 | if not wordsize: | |
25e1418a UH |
161 | return |
162 | ||
4f4e0353 GS |
163 | # Terminate a previously seen item of a word first. Emit the |
164 | # word's annotation when the last item's end was seen. | |
165 | if self.word_items: | |
166 | ss, _, data = self.word_items[-1] | |
167 | es = now | |
168 | self.word_items[-1] = (ss, es, data) | |
169 | if len(self.word_items) == wordsize: | |
170 | self.flush_word(bus_width) | |
171 | ||
172 | # Start tracking the currently seen item (yet unknown end time). | |
173 | if item is not None: | |
174 | pend = (now, None, item) | |
175 | self.word_items.append(pend) | |
176 | ||
177 | def handle_bits(self, now, item, bus_width): | |
178 | ||
179 | # Optionally flush a previously started item. | |
180 | if self.pend_item: | |
181 | ss, _, data = self.pend_item | |
182 | self.pend_item = None | |
183 | es = now | |
184 | txts = [self.fmt_item.format(data)] | |
185 | self.putg(ss, es, Ann.ITEM, txts) | |
239e15df | 186 | self.putpy(ss, es, 'ITEM', (data, bus_width)) |
82f3f4d4 | 187 | self.putbin(ss, es, 0, data.to_bytes(1, byteorder='big')) |
4f4e0353 GS |
188 | |
189 | # Optionally queue the currently seen item. | |
190 | if item is not None: | |
191 | self.pend_item = (now, None, item) | |
192 | ||
193 | # Pass the current item to the word accumulation logic. | |
194 | self.queue_word(now, item, bus_width) | |
25e1418a | 195 | |
8eb06a59 | 196 | def decode(self): |
a0b7e07f GS |
197 | # Determine which (optional) channels have input data. Insist in |
198 | # a non-empty input data set. Cope with sparse connection maps. | |
199 | # Store enough state to later "compress" sampled input data. | |
615f86f6 | 200 | data_indices = [ |
a0b7e07f | 201 | idx if self.has_channel(idx) else None |
615f86f6 | 202 | for idx in range(Pin.DATA_0, Pin.DATA_N) |
a0b7e07f | 203 | ] |
615f86f6 GS |
204 | has_data = [idx for idx in data_indices if idx is not None] |
205 | if not has_data: | |
206 | raise ChannelError('Need at least one data channel.') | |
207 | max_connected = max(has_data) | |
208 | ||
209 | # Pre-determine which input data to strip off, the width of | |
210 | # individual items and multiplexed words, as well as format | |
211 | # strings here. This simplifies call sites which run in tight | |
212 | # loops later. | |
213 | upper_data_bound = max_connected + 1 | |
214 | num_item_bits = upper_data_bound - Pin.DATA_0 | |
215 | num_word_items = self.options['wordsize'] | |
216 | num_word_bits = num_item_bits * num_word_items | |
217 | num_digits = (num_item_bits + 4 - 1) // 4 | |
218 | self.fmt_item = "{{:0{}x}}".format(num_digits) | |
219 | num_digits = (num_word_bits + 4 - 1) // 4 | |
220 | self.fmt_word = "{{:0{}x}}".format(num_digits) | |
a0b7e07f GS |
221 | |
222 | # Determine .wait() conditions, depending on the presence of a | |
223 | # clock signal. Either inspect samples on the configured edge of | |
224 | # the clock, or inspect samples upon ANY edge of ANY of the pins | |
225 | # which provide input data. | |
76b64d3a GS |
226 | conds = [] |
227 | cond_idx_clock = None | |
228 | cond_idx_data_0 = None | |
229 | cond_idx_data_N = None | |
230 | cond_idx_reset = None | |
9a41127e GS |
231 | has_clock = self.has_channel(Pin.CLOCK) |
232 | if has_clock: | |
76b64d3a | 233 | cond_idx_clock = len(conds) |
e2317ec4 GS |
234 | edge = { |
235 | 'rising': 'r', | |
236 | 'falling': 'f', | |
237 | 'either': 'e', | |
238 | }.get(self.options['clock_edge']) | |
76b64d3a | 239 | conds.append({Pin.CLOCK: edge}) |
a0b7e07f | 240 | else: |
76b64d3a GS |
241 | cond_idx_data_0 = len(conds) |
242 | conds.extend([{idx: 'e'} for idx in has_data]) | |
243 | cond_idx_data_N = len(conds) | |
244 | has_reset = self.has_channel(Pin.RESET) | |
245 | if has_reset: | |
246 | cond_idx_reset = len(conds) | |
247 | conds.append({Pin.RESET: 'e'}) | |
248 | reset_active = { | |
249 | 'low-active': 0, | |
250 | 'high-active': 1, | |
251 | }.get(self.options['reset_polarity']) | |
fcdd8c68 | 252 | |
a0b7e07f GS |
253 | # Keep processing the input stream. Assume "always zero" for |
254 | # not-connected input lines. Pass data bits (all inputs except | |
76b64d3a GS |
255 | # clock and reset) to the handle_bits() method. Handle reset |
256 | # edges first and data changes then, within the same iteration. | |
257 | # This results in robust operation for low-oversampled input. | |
258 | in_reset = False | |
b0ac80e2 | 259 | while True: |
e303fd42 GS |
260 | try: |
261 | pins = self.wait(conds) | |
262 | except EOFError as e: | |
263 | break | |
76b64d3a GS |
264 | clock_edge = cond_idx_clock is not None and self.matched[cond_idx_clock] |
265 | 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]] | |
266 | reset_edge = cond_idx_reset is not None and self.matched[cond_idx_reset] | |
267 | ||
268 | if reset_edge: | |
269 | in_reset = pins[Pin.RESET] == reset_active | |
270 | if in_reset: | |
4f4e0353 GS |
271 | self.handle_bits(self.samplenum, None, num_item_bits) |
272 | self.flush_word(num_item_bits) | |
76b64d3a GS |
273 | if in_reset: |
274 | continue | |
275 | ||
276 | if clock_edge or data_edge: | |
277 | data_bits = [0 if idx is None else pins[idx] for idx in data_indices] | |
278 | data_bits = data_bits[:num_item_bits] | |
279 | item = bitpack(data_bits) | |
4f4e0353 | 280 | self.handle_bits(self.samplenum, item, num_item_bits) |
e303fd42 GS |
281 | |
282 | self.handle_bits(self.samplenum, None, num_item_bits) | |
283 | # TODO Determine whether a WARN annotation needs to get emitted. | |
284 | # The decoder has not seen the end of the last accumulated item. | |
285 | # Instead it just ran out of input data. |