]>
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 | ||
6a15597a | 57 | def channel_list(num_channels): |
8eafa261 | 58 | l = [{'id': 'clk', 'name': 'CLK', 'desc': 'Clock line'}] |
6a15597a | 59 | for i in range(num_channels): |
25e1418a UH |
60 | d = {'id': 'd%d' % i, 'name': 'D%d' % i, 'desc': 'Data line %d' % i} |
61 | l.append(d) | |
da9bcbd9 | 62 | return tuple(l) |
25e1418a | 63 | |
a573d394 UH |
64 | class ChannelError(Exception): |
65 | pass | |
66 | ||
8eb06a59 UH |
67 | NUM_CHANNELS = 8 |
68 | ||
25e1418a | 69 | class Decoder(srd.Decoder): |
8eb06a59 | 70 | api_version = 3 |
25e1418a UH |
71 | id = 'parallel' |
72 | name = 'Parallel' | |
73 | longname = 'Parallel sync bus' | |
74 | desc = 'Generic parallel synchronous bus.' | |
75 | license = 'gplv2+' | |
76 | inputs = ['logic'] | |
77 | outputs = ['parallel'] | |
d6d8a8a4 | 78 | tags = ['Util'] |
8eb06a59 | 79 | optional_channels = channel_list(NUM_CHANNELS) |
84c1c0b5 BV |
80 | options = ( |
81 | {'id': 'clock_edge', 'desc': 'Clock edge to sample on', | |
82 | 'default': 'rising', 'values': ('rising', 'falling')}, | |
e710d006 GS |
83 | {'id': 'wordsize', 'desc': 'Data wordsize (# bus cycles)', |
84 | 'default': 0}, | |
b0918d40 | 85 | {'id': 'endianness', 'desc': 'Data endianness', |
84c1c0b5 BV |
86 | 'default': 'little', 'values': ('little', 'big')}, |
87 | ) | |
da9bcbd9 | 88 | annotations = ( |
e144452b UH |
89 | ('item', 'Item'), |
90 | ('word', 'Word'), | |
da9bcbd9 | 91 | ) |
ca24954f GS |
92 | annotation_rows = ( |
93 | ('items', 'Items', (0,)), | |
94 | ('words', 'Words', (1,)), | |
95 | ) | |
25e1418a UH |
96 | |
97 | def __init__(self): | |
10aeb8ea GS |
98 | self.reset() |
99 | ||
100 | def reset(self): | |
25e1418a | 101 | self.items = [] |
25e1418a | 102 | self.saved_item = None |
25e1418a | 103 | self.ss_item = self.es_item = None |
ca24954f GS |
104 | self.saved_word = None |
105 | self.ss_word = self.es_word = None | |
25e1418a | 106 | self.first = True |
25e1418a | 107 | |
b098b820 | 108 | def start(self): |
c515eed7 | 109 | self.out_python = self.register(srd.OUTPUT_PYTHON) |
be465111 | 110 | self.out_ann = self.register(srd.OUTPUT_ANN) |
25e1418a | 111 | |
25e1418a | 112 | def putpb(self, data): |
c515eed7 | 113 | self.put(self.ss_item, self.es_item, self.out_python, data) |
25e1418a UH |
114 | |
115 | def putb(self, data): | |
116 | self.put(self.ss_item, self.es_item, self.out_ann, data) | |
117 | ||
118 | def putpw(self, data): | |
c515eed7 | 119 | self.put(self.ss_word, self.es_word, self.out_python, data) |
25e1418a UH |
120 | |
121 | def putw(self, data): | |
122 | self.put(self.ss_word, self.es_word, self.out_ann, data) | |
123 | ||
a0b7e07f | 124 | def handle_bits(self, item, used_pins): |
25e1418a | 125 | |
ca24954f GS |
126 | # If a word was previously accumulated, then emit its annotation |
127 | # now after its end samplenumber became available. | |
128 | if self.saved_word is not None: | |
129 | if self.options['wordsize'] > 0: | |
130 | self.es_word = self.samplenum | |
fcdd8c68 | 131 | self.putw([1, [self.fmt_word.format(self.saved_word)]]) |
ca24954f GS |
132 | self.putpw(['WORD', self.saved_word]) |
133 | self.saved_word = None | |
134 | ||
135 | # Defer annotations for individual items until the next sample | |
136 | # is taken, and the previous sample's end samplenumber has | |
137 | # become available. | |
35b380b1 | 138 | if self.first: |
25e1418a UH |
139 | # Save the start sample and item for later (no output yet). |
140 | self.ss_item = self.samplenum | |
141 | self.first = False | |
142 | self.saved_item = item | |
143 | else: | |
144 | # Output the saved item (from the last CLK edge to the current). | |
145 | self.es_item = self.samplenum | |
146 | self.putpb(['ITEM', self.saved_item]) | |
fcdd8c68 | 147 | self.putb([0, [self.fmt_item.format(self.saved_item)]]) |
25e1418a UH |
148 | self.ss_item = self.samplenum |
149 | self.saved_item = item | |
150 | ||
ca24954f GS |
151 | # Get as many items as the configured wordsize specifies. |
152 | if not self.items: | |
153 | self.ss_word = self.samplenum | |
154 | self.items.append(item) | |
6f7dd46d GS |
155 | ws = self.options['wordsize'] |
156 | if len(self.items) < ws: | |
25e1418a UH |
157 | return |
158 | ||
ca24954f GS |
159 | # Collect words and prepare annotation details, but defer emission |
160 | # until the end samplenumber becomes available. | |
6f7dd46d | 161 | endian = self.options['endianness'] |
ca24954f | 162 | if endian == 'big': |
6f7dd46d | 163 | self.items.reverse() |
ca24954f GS |
164 | word = sum([self.items[i] << (i * used_pins) for i in range(ws)]) |
165 | self.saved_word = word | |
6f7dd46d | 166 | self.items = [] |
25e1418a | 167 | |
8eb06a59 | 168 | def decode(self): |
a0b7e07f GS |
169 | # Determine which (optional) channels have input data. Insist in |
170 | # a non-empty input data set. Cope with sparse connection maps. | |
171 | # Store enough state to later "compress" sampled input data. | |
172 | max_possible = len(self.optional_channels) | |
173 | idx_channels = [ | |
174 | idx if self.has_channel(idx) else None | |
175 | for idx in range(max_possible) | |
176 | ] | |
177 | has_channels = [idx for idx in idx_channels if idx is not None] | |
178 | if not has_channels: | |
8eb06a59 | 179 | raise ChannelError('At least one channel has to be supplied.') |
a0b7e07f | 180 | max_connected = max(has_channels) |
a0b7e07f GS |
181 | |
182 | # Determine .wait() conditions, depending on the presence of a | |
183 | # clock signal. Either inspect samples on the configured edge of | |
184 | # the clock, or inspect samples upon ANY edge of ANY of the pins | |
185 | # which provide input data. | |
186 | if self.has_channel(0): | |
b0ac80e2 | 187 | edge = self.options['clock_edge'][0] |
a0b7e07f GS |
188 | conds = {0: edge} |
189 | else: | |
190 | conds = [{idx: 'e'} for idx in has_channels] | |
b0ac80e2 | 191 | |
fcdd8c68 GS |
192 | # Pre-determine which input data to strip off, the width of |
193 | # individual items and multiplexed words, as well as format | |
194 | # strings here. This simplifies call sites which run in tight | |
195 | # loops later. | |
196 | idx_strip = max_connected + 1 | |
197 | num_item_bits = idx_strip - 1 | |
198 | num_word_items = self.options['wordsize'] | |
199 | num_word_bits = num_item_bits * num_word_items | |
200 | num_digits = (num_item_bits + 3) // 4 | |
201 | self.fmt_item = "{{:0{}x}}".format(num_digits) | |
202 | num_digits = (num_word_bits + 3) // 4 | |
203 | self.fmt_word = "{{:0{}x}}".format(num_digits) | |
204 | ||
a0b7e07f GS |
205 | # Keep processing the input stream. Assume "always zero" for |
206 | # not-connected input lines. Pass data bits (all inputs except | |
207 | # clock) to the handle_bits() method. | |
b0ac80e2 GS |
208 | while True: |
209 | pins = self.wait(conds) | |
a0b7e07f | 210 | bits = [0 if idx is None else pins[idx] for idx in idx_channels] |
fcdd8c68 GS |
211 | item = bitpack(bits[1:idx_strip]) |
212 | self.handle_bits(item, num_item_bits) |