2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2013-2016 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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/>.
20 import sigrokdecode as srd
21 from common.srdhelper import bitpack
30 - 'ITEM', [<item>, <itembitsize>]
31 - 'WORD', [<word>, <wordbitsize>, <worditemcount>]
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>.
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.
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>.
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>
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.
62 DATA_N = DATA_0 + NUM_CHANNELS
67 class ChannelError(Exception):
70 class Decoder(srd.Decoder):
74 longname = 'Parallel sync bus'
75 desc = 'Generic parallel synchronous bus.'
78 outputs = ['parallel']
80 optional_channels = tuple(
81 [{'id': 'clk', 'name': 'CLK', 'desc': 'Clock line'}] +
83 {'id': 'd%d' % i, 'name': 'D%d' % i, 'desc': 'Data line %d' % i}
84 for i in range(NUM_CHANNELS)
88 {'id': 'clock_edge', 'desc': 'Clock edge to sample on',
89 'default': 'rising', 'values': ('rising', 'falling', 'either')},
90 {'id': 'wordsize', 'desc': 'Data wordsize (# bus cycles)',
92 {'id': 'endianness', 'desc': 'Data endianness',
93 'default': 'little', 'values': ('little', 'big')},
100 ('items', 'Items', (Ann.ITEM,)),
101 ('words', 'Words', (Ann.WORD,)),
109 self.saved_item = None
110 self.ss_item = self.es_item = None
111 self.saved_word = None
112 self.ss_word = self.es_word = None
116 self.out_python = self.register(srd.OUTPUT_PYTHON)
117 self.out_ann = self.register(srd.OUTPUT_ANN)
119 def putpb(self, data):
120 self.put(self.ss_item, self.es_item, self.out_python, data)
122 def putb(self, data):
123 self.put(self.ss_item, self.es_item, self.out_ann, data)
125 def putpw(self, data):
126 self.put(self.ss_word, self.es_word, self.out_python, data)
128 def putw(self, data):
129 self.put(self.ss_word, self.es_word, self.out_ann, data)
131 def handle_bits(self, item, used_pins):
133 # If a word was previously accumulated, then emit its annotation
134 # now after its end samplenumber became available.
135 if self.saved_word is not None:
136 if self.options['wordsize'] > 0:
137 self.es_word = self.samplenum
138 self.putw([Ann.WORD, [self.fmt_word.format(self.saved_word)]])
139 self.putpw(['WORD', self.saved_word])
140 self.saved_word = None
142 # Defer annotations for individual items until the next sample
143 # is taken, and the previous sample's end samplenumber has
146 # Save the start sample and item for later (no output yet).
147 self.ss_item = self.samplenum
149 self.saved_item = item
151 # Output the saved item (from the last CLK edge to the current).
152 self.es_item = self.samplenum
153 self.putpb(['ITEM', self.saved_item])
154 self.putb([Ann.ITEM, [self.fmt_item.format(self.saved_item)]])
155 self.ss_item = self.samplenum
156 self.saved_item = item
158 # Get as many items as the configured wordsize specifies.
160 self.ss_word = self.samplenum
161 self.items.append(item)
162 ws = self.options['wordsize']
163 if len(self.items) < ws:
166 # Collect words and prepare annotation details, but defer emission
167 # until the end samplenumber becomes available.
168 endian = self.options['endianness']
171 word = sum([self.items[i] << (i * used_pins) for i in range(ws)])
172 self.saved_word = word
176 # Determine which (optional) channels have input data. Insist in
177 # a non-empty input data set. Cope with sparse connection maps.
178 # Store enough state to later "compress" sampled input data.
180 idx if self.has_channel(idx) else None
181 for idx in range(Pin.DATA_0, Pin.DATA_N)
183 has_data = [idx for idx in data_indices if idx is not None]
185 raise ChannelError('Need at least one data channel.')
186 max_connected = max(has_data)
188 # Pre-determine which input data to strip off, the width of
189 # individual items and multiplexed words, as well as format
190 # strings here. This simplifies call sites which run in tight
192 upper_data_bound = max_connected + 1
193 num_item_bits = upper_data_bound - Pin.DATA_0
194 num_word_items = self.options['wordsize']
195 num_word_bits = num_item_bits * num_word_items
196 num_digits = (num_item_bits + 4 - 1) // 4
197 self.fmt_item = "{{:0{}x}}".format(num_digits)
198 num_digits = (num_word_bits + 4 - 1) // 4
199 self.fmt_word = "{{:0{}x}}".format(num_digits)
201 # Determine .wait() conditions, depending on the presence of a
202 # clock signal. Either inspect samples on the configured edge of
203 # the clock, or inspect samples upon ANY edge of ANY of the pins
204 # which provide input data.
205 has_clock = self.has_channel(Pin.CLOCK)
211 }.get(self.options['clock_edge'])
212 conds = [{Pin.CLOCK: edge}]
214 conds = [{idx: 'e'} for idx in has_data]
216 # Keep processing the input stream. Assume "always zero" for
217 # not-connected input lines. Pass data bits (all inputs except
218 # clock) to the handle_bits() method.
220 pins = self.wait(conds)
221 data_bits = [0 if idx is None else pins[idx] for idx in data_indices]
222 data_bits = data_bits[:num_item_bits]
223 item = bitpack(data_bits)
224 self.handle_bits(item, num_item_bits)