]>
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 | ||
64 | class Ann: | |
65 | ITEM, WORD = range(2) | |
66 | ||
67 | class ChannelError(Exception): | |
68 | pass | |
69 | ||
70 | class Decoder(srd.Decoder): | |
71 | api_version = 3 | |
72 | id = 'parallel' | |
73 | name = 'Parallel' | |
74 | longname = 'Parallel sync bus' | |
75 | desc = 'Generic parallel synchronous bus.' | |
76 | license = 'gplv2+' | |
77 | inputs = ['logic'] | |
78 | outputs = ['parallel'] | |
79 | tags = ['Util'] | |
80 | optional_channels = tuple( | |
81 | [{'id': 'clk', 'name': 'CLK', 'desc': 'Clock line'}] + | |
82 | [ | |
83 | {'id': 'd%d' % i, 'name': 'D%d' % i, 'desc': 'Data line %d' % i} | |
84 | for i in range(NUM_CHANNELS) | |
85 | ] | |
86 | ) | |
87 | options = ( | |
88 | {'id': 'clock_edge', 'desc': 'Clock edge to sample on', | |
89 | 'default': 'rising', 'values': ('rising', 'falling')}, | |
90 | {'id': 'wordsize', 'desc': 'Data wordsize (# bus cycles)', | |
91 | 'default': 0}, | |
92 | {'id': 'endianness', 'desc': 'Data endianness', | |
93 | 'default': 'little', 'values': ('little', 'big')}, | |
94 | ) | |
95 | annotations = ( | |
96 | ('item', 'Item'), | |
97 | ('word', 'Word'), | |
98 | ) | |
99 | annotation_rows = ( | |
100 | ('items', 'Items', (Ann.ITEM,)), | |
101 | ('words', 'Words', (Ann.WORD,)), | |
102 | ) | |
103 | ||
104 | def __init__(self): | |
105 | self.reset() | |
106 | ||
107 | def reset(self): | |
108 | self.items = [] | |
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 | |
113 | self.first = True | |
114 | ||
115 | def start(self): | |
116 | self.out_python = self.register(srd.OUTPUT_PYTHON) | |
117 | self.out_ann = self.register(srd.OUTPUT_ANN) | |
118 | ||
119 | def putpb(self, data): | |
120 | self.put(self.ss_item, self.es_item, self.out_python, data) | |
121 | ||
122 | def putb(self, data): | |
123 | self.put(self.ss_item, self.es_item, self.out_ann, data) | |
124 | ||
125 | def putpw(self, data): | |
126 | self.put(self.ss_word, self.es_word, self.out_python, data) | |
127 | ||
128 | def putw(self, data): | |
129 | self.put(self.ss_word, self.es_word, self.out_ann, data) | |
130 | ||
131 | def handle_bits(self, item, used_pins): | |
132 | ||
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 | |
141 | ||
142 | # Defer annotations for individual items until the next sample | |
143 | # is taken, and the previous sample's end samplenumber has | |
144 | # become available. | |
145 | if self.first: | |
146 | # Save the start sample and item for later (no output yet). | |
147 | self.ss_item = self.samplenum | |
148 | self.first = False | |
149 | self.saved_item = item | |
150 | else: | |
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 | |
157 | ||
158 | # Get as many items as the configured wordsize specifies. | |
159 | if not self.items: | |
160 | self.ss_word = self.samplenum | |
161 | self.items.append(item) | |
162 | ws = self.options['wordsize'] | |
163 | if len(self.items) < ws: | |
164 | return | |
165 | ||
166 | # Collect words and prepare annotation details, but defer emission | |
167 | # until the end samplenumber becomes available. | |
168 | endian = self.options['endianness'] | |
169 | if endian == 'big': | |
170 | self.items.reverse() | |
171 | word = sum([self.items[i] << (i * used_pins) for i in range(ws)]) | |
172 | self.saved_word = word | |
173 | self.items = [] | |
174 | ||
175 | def decode(self): | |
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. | |
179 | data_indices = [ | |
180 | idx if self.has_channel(idx) else None | |
181 | for idx in range(Pin.DATA_0, Pin.DATA_N) | |
182 | ] | |
183 | has_data = [idx for idx in data_indices if idx is not None] | |
184 | if not has_data: | |
185 | raise ChannelError('Need at least one data channel.') | |
186 | max_connected = max(has_data) | |
187 | ||
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 | |
191 | # loops later. | |
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) | |
200 | ||
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) | |
206 | if has_clock: | |
207 | edge = self.options['clock_edge'][0] | |
208 | conds = [{Pin.CLOCK: edge}] | |
209 | else: | |
210 | conds = [{idx: 'e'} for idx in has_data] | |
211 | ||
212 | # Keep processing the input stream. Assume "always zero" for | |
213 | # not-connected input lines. Pass data bits (all inputs except | |
214 | # clock) to the handle_bits() method. | |
215 | while True: | |
216 | pins = self.wait(conds) | |
217 | data_bits = [0 if idx is None else pins[idx] for idx in data_indices] | |
218 | data_bits = data_bits[:num_item_bits] | |
219 | item = bitpack(data_bits) | |
220 | self.handle_bits(item, num_item_bits) |