]>
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'] | |
8eb06a59 | 78 | optional_channels = channel_list(NUM_CHANNELS) |
84c1c0b5 BV |
79 | options = ( |
80 | {'id': 'clock_edge', 'desc': 'Clock edge to sample on', | |
81 | 'default': 'rising', 'values': ('rising', 'falling')}, | |
b0918d40 UH |
82 | {'id': 'wordsize', 'desc': 'Data wordsize', 'default': 1}, |
83 | {'id': 'endianness', 'desc': 'Data endianness', | |
84c1c0b5 BV |
84 | 'default': 'little', 'values': ('little', 'big')}, |
85 | ) | |
da9bcbd9 BV |
86 | annotations = ( |
87 | ('items', 'Items'), | |
88 | ('words', 'Words'), | |
89 | ) | |
25e1418a UH |
90 | |
91 | def __init__(self): | |
10aeb8ea GS |
92 | self.reset() |
93 | ||
94 | def reset(self): | |
25e1418a | 95 | self.items = [] |
25e1418a | 96 | self.saved_item = None |
25e1418a UH |
97 | self.ss_item = self.es_item = None |
98 | self.first = True | |
25e1418a | 99 | |
b098b820 | 100 | def start(self): |
c515eed7 | 101 | self.out_python = self.register(srd.OUTPUT_PYTHON) |
be465111 | 102 | self.out_ann = self.register(srd.OUTPUT_ANN) |
25e1418a | 103 | |
25e1418a | 104 | def putpb(self, data): |
c515eed7 | 105 | self.put(self.ss_item, self.es_item, self.out_python, data) |
25e1418a UH |
106 | |
107 | def putb(self, data): | |
108 | self.put(self.ss_item, self.es_item, self.out_ann, data) | |
109 | ||
110 | def putpw(self, data): | |
c515eed7 | 111 | self.put(self.ss_word, self.es_word, self.out_python, data) |
25e1418a UH |
112 | |
113 | def putw(self, data): | |
114 | self.put(self.ss_word, self.es_word, self.out_ann, data) | |
115 | ||
a0b7e07f | 116 | def handle_bits(self, item, used_pins): |
6f7dd46d GS |
117 | # Save the item, and its sample number if it's the first part of a word. |
118 | if not self.items: | |
119 | self.ss_word = self.samplenum | |
25e1418a | 120 | self.items.append(item) |
25e1418a | 121 | |
35b380b1 | 122 | if self.first: |
25e1418a UH |
123 | # Save the start sample and item for later (no output yet). |
124 | self.ss_item = self.samplenum | |
125 | self.first = False | |
126 | self.saved_item = item | |
127 | else: | |
128 | # Output the saved item (from the last CLK edge to the current). | |
129 | self.es_item = self.samplenum | |
130 | self.putpb(['ITEM', self.saved_item]) | |
131 | self.putb([0, ['%X' % self.saved_item]]) | |
132 | self.ss_item = self.samplenum | |
133 | self.saved_item = item | |
134 | ||
25e1418a | 135 | # Get as many items as the configured wordsize says. |
6f7dd46d GS |
136 | ws = self.options['wordsize'] |
137 | if len(self.items) < ws: | |
25e1418a UH |
138 | return |
139 | ||
c515eed7 | 140 | # Output annotations/python for a word (a collection of items). |
6f7dd46d GS |
141 | # NOTE that this feature is currently not effective. The emission |
142 | # of Python annotations is commented out. | |
143 | endian = self.options['endianness'] | |
144 | if endian == 'little': | |
145 | self.items.reverse() | |
25e1418a UH |
146 | word = 0 |
147 | for i in range(ws): | |
6f7dd46d | 148 | word |= self.items[i] << (i * used_pins) |
25e1418a UH |
149 | |
150 | self.es_word = self.samplenum | |
151 | # self.putpw(['WORD', word]) | |
152 | # self.putw([1, ['%X' % word]]) | |
153 | self.ss_word = self.samplenum | |
154 | ||
6f7dd46d | 155 | self.items = [] |
25e1418a | 156 | |
8eb06a59 | 157 | def decode(self): |
a0b7e07f GS |
158 | # Determine which (optional) channels have input data. Insist in |
159 | # a non-empty input data set. Cope with sparse connection maps. | |
160 | # Store enough state to later "compress" sampled input data. | |
161 | max_possible = len(self.optional_channels) | |
162 | idx_channels = [ | |
163 | idx if self.has_channel(idx) else None | |
164 | for idx in range(max_possible) | |
165 | ] | |
166 | has_channels = [idx for idx in idx_channels if idx is not None] | |
167 | if not has_channels: | |
8eb06a59 | 168 | raise ChannelError('At least one channel has to be supplied.') |
a0b7e07f GS |
169 | max_connected = max(has_channels) |
170 | idx_strip = max_connected + 1 | |
171 | ||
172 | # Determine .wait() conditions, depending on the presence of a | |
173 | # clock signal. Either inspect samples on the configured edge of | |
174 | # the clock, or inspect samples upon ANY edge of ANY of the pins | |
175 | # which provide input data. | |
176 | if self.has_channel(0): | |
b0ac80e2 | 177 | edge = self.options['clock_edge'][0] |
a0b7e07f GS |
178 | conds = {0: edge} |
179 | else: | |
180 | conds = [{idx: 'e'} for idx in has_channels] | |
b0ac80e2 | 181 | |
a0b7e07f GS |
182 | # Keep processing the input stream. Assume "always zero" for |
183 | # not-connected input lines. Pass data bits (all inputs except | |
184 | # clock) to the handle_bits() method. | |
b0ac80e2 GS |
185 | while True: |
186 | pins = self.wait(conds) | |
a0b7e07f GS |
187 | bits = [0 if idx is None else pins[idx] for idx in idx_channels] |
188 | bits = bits[1:idx_strip] | |
189 | self.handle_bits(bitpack(bits), len(bits)) |