]>
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 | ||
9a41127e GS |
57 | NUM_CHANNELS = 8 |
58 | ||
59 | class Pin: | |
60 | CLOCK = 0 | |
61 | DATA = range(1, NUM_CHANNELS + 1) | |
62 | ||
63 | class Ann: | |
64 | ITEM, WORD = range(2) | |
65 | ||
66 | def channel_list(): | |
8eafa261 | 67 | l = [{'id': 'clk', 'name': 'CLK', 'desc': 'Clock line'}] |
9a41127e | 68 | for i in range(NUM_CHANNELS): |
25e1418a UH |
69 | d = {'id': 'd%d' % i, 'name': 'D%d' % i, 'desc': 'Data line %d' % i} |
70 | l.append(d) | |
da9bcbd9 | 71 | return tuple(l) |
25e1418a | 72 | |
a573d394 UH |
73 | class ChannelError(Exception): |
74 | pass | |
75 | ||
25e1418a | 76 | class Decoder(srd.Decoder): |
8eb06a59 | 77 | api_version = 3 |
25e1418a UH |
78 | id = 'parallel' |
79 | name = 'Parallel' | |
80 | longname = 'Parallel sync bus' | |
81 | desc = 'Generic parallel synchronous bus.' | |
82 | license = 'gplv2+' | |
83 | inputs = ['logic'] | |
84 | outputs = ['parallel'] | |
d6d8a8a4 | 85 | tags = ['Util'] |
9a41127e | 86 | optional_channels = channel_list() |
84c1c0b5 BV |
87 | options = ( |
88 | {'id': 'clock_edge', 'desc': 'Clock edge to sample on', | |
89 | 'default': 'rising', 'values': ('rising', 'falling')}, | |
e710d006 GS |
90 | {'id': 'wordsize', 'desc': 'Data wordsize (# bus cycles)', |
91 | 'default': 0}, | |
b0918d40 | 92 | {'id': 'endianness', 'desc': 'Data endianness', |
84c1c0b5 BV |
93 | 'default': 'little', 'values': ('little', 'big')}, |
94 | ) | |
da9bcbd9 | 95 | annotations = ( |
e144452b UH |
96 | ('item', 'Item'), |
97 | ('word', 'Word'), | |
da9bcbd9 | 98 | ) |
ca24954f | 99 | annotation_rows = ( |
9a41127e GS |
100 | ('items', 'Items', (Ann.ITEM,)), |
101 | ('words', 'Words', (Ann.WORD,)), | |
ca24954f | 102 | ) |
25e1418a UH |
103 | |
104 | def __init__(self): | |
10aeb8ea GS |
105 | self.reset() |
106 | ||
107 | def reset(self): | |
25e1418a | 108 | self.items = [] |
25e1418a | 109 | self.saved_item = None |
25e1418a | 110 | self.ss_item = self.es_item = None |
ca24954f GS |
111 | self.saved_word = None |
112 | self.ss_word = self.es_word = None | |
25e1418a | 113 | self.first = True |
25e1418a | 114 | |
b098b820 | 115 | def start(self): |
c515eed7 | 116 | self.out_python = self.register(srd.OUTPUT_PYTHON) |
be465111 | 117 | self.out_ann = self.register(srd.OUTPUT_ANN) |
25e1418a | 118 | |
25e1418a | 119 | def putpb(self, data): |
c515eed7 | 120 | self.put(self.ss_item, self.es_item, self.out_python, data) |
25e1418a UH |
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): | |
c515eed7 | 126 | self.put(self.ss_word, self.es_word, self.out_python, data) |
25e1418a UH |
127 | |
128 | def putw(self, data): | |
129 | self.put(self.ss_word, self.es_word, self.out_ann, data) | |
130 | ||
a0b7e07f | 131 | def handle_bits(self, item, used_pins): |
25e1418a | 132 | |
ca24954f GS |
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 | |
9a41127e | 138 | self.putw([Ann.WORD, [self.fmt_word.format(self.saved_word)]]) |
ca24954f GS |
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. | |
35b380b1 | 145 | if self.first: |
25e1418a UH |
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]) | |
9a41127e | 154 | self.putb([Ann.ITEM, [self.fmt_item.format(self.saved_item)]]) |
25e1418a UH |
155 | self.ss_item = self.samplenum |
156 | self.saved_item = item | |
157 | ||
ca24954f GS |
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) | |
6f7dd46d GS |
162 | ws = self.options['wordsize'] |
163 | if len(self.items) < ws: | |
25e1418a UH |
164 | return |
165 | ||
ca24954f GS |
166 | # Collect words and prepare annotation details, but defer emission |
167 | # until the end samplenumber becomes available. | |
6f7dd46d | 168 | endian = self.options['endianness'] |
ca24954f | 169 | if endian == 'big': |
6f7dd46d | 170 | self.items.reverse() |
ca24954f GS |
171 | word = sum([self.items[i] << (i * used_pins) for i in range(ws)]) |
172 | self.saved_word = word | |
6f7dd46d | 173 | self.items = [] |
25e1418a | 174 | |
8eb06a59 | 175 | def decode(self): |
a0b7e07f GS |
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 | max_possible = len(self.optional_channels) | |
180 | idx_channels = [ | |
181 | idx if self.has_channel(idx) else None | |
182 | for idx in range(max_possible) | |
183 | ] | |
184 | has_channels = [idx for idx in idx_channels if idx is not None] | |
185 | if not has_channels: | |
8eb06a59 | 186 | raise ChannelError('At least one channel has to be supplied.') |
a0b7e07f | 187 | max_connected = max(has_channels) |
a0b7e07f GS |
188 | |
189 | # Determine .wait() conditions, depending on the presence of a | |
190 | # clock signal. Either inspect samples on the configured edge of | |
191 | # the clock, or inspect samples upon ANY edge of ANY of the pins | |
192 | # which provide input data. | |
9a41127e GS |
193 | has_clock = self.has_channel(Pin.CLOCK) |
194 | if has_clock: | |
b0ac80e2 | 195 | edge = self.options['clock_edge'][0] |
9a41127e | 196 | conds = [{Pin.CLOCK: edge}] |
a0b7e07f GS |
197 | else: |
198 | conds = [{idx: 'e'} for idx in has_channels] | |
b0ac80e2 | 199 | |
fcdd8c68 GS |
200 | # Pre-determine which input data to strip off, the width of |
201 | # individual items and multiplexed words, as well as format | |
202 | # strings here. This simplifies call sites which run in tight | |
203 | # loops later. | |
204 | idx_strip = max_connected + 1 | |
205 | num_item_bits = idx_strip - 1 | |
206 | num_word_items = self.options['wordsize'] | |
207 | num_word_bits = num_item_bits * num_word_items | |
208 | num_digits = (num_item_bits + 3) // 4 | |
209 | self.fmt_item = "{{:0{}x}}".format(num_digits) | |
210 | num_digits = (num_word_bits + 3) // 4 | |
211 | self.fmt_word = "{{:0{}x}}".format(num_digits) | |
212 | ||
a0b7e07f GS |
213 | # Keep processing the input stream. Assume "always zero" for |
214 | # not-connected input lines. Pass data bits (all inputs except | |
215 | # clock) to the handle_bits() method. | |
b0ac80e2 GS |
216 | while True: |
217 | pins = self.wait(conds) | |
a0b7e07f | 218 | bits = [0 if idx is None else pins[idx] for idx in idx_channels] |
9a41127e | 219 | item = bitpack(bits[Pin.DATA[0]:idx_strip]) |
fcdd8c68 | 220 | self.handle_bits(item, num_item_bits) |