]> sigrok.org Git - libsigrokdecode.git/blame - decoders/parallel/pd.py
parallel: rephrase handling of data lines, symbolic upper bound
[libsigrokdecode.git] / decoders / parallel / pd.py
CommitLineData
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 20import sigrokdecode as srd
a0b7e07f 21from common.srdhelper import bitpack
25e1418a
UH
22
23'''
c515eed7 24OUTPUT_PYTHON format:
25e1418a
UH
25
26Packet:
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
57NUM_CHANNELS = 8
58
59class Pin:
60 CLOCK = 0
615f86f6
GS
61 DATA_0 = CLOCK + 1
62 DATA_N = DATA_0 + NUM_CHANNELS
9a41127e
GS
63
64class Ann:
65 ITEM, WORD = range(2)
66
a573d394
UH
67class ChannelError(Exception):
68 pass
69
25e1418a 70class Decoder(srd.Decoder):
8eb06a59 71 api_version = 3
25e1418a
UH
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']
d6d8a8a4 79 tags = ['Util']
615f86f6
GS
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 )
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.
615f86f6 179 data_indices = [
a0b7e07f 180 idx if self.has_channel(idx) else None
615f86f6 181 for idx in range(Pin.DATA_0, Pin.DATA_N)
a0b7e07f 182 ]
615f86f6
GS
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)
a0b7e07f
GS
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.
9a41127e
GS
205 has_clock = self.has_channel(Pin.CLOCK)
206 if has_clock:
b0ac80e2 207 edge = self.options['clock_edge'][0]
9a41127e 208 conds = [{Pin.CLOCK: edge}]
a0b7e07f 209 else:
615f86f6 210 conds = [{idx: 'e'} for idx in has_data]
fcdd8c68 211
a0b7e07f
GS
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.
b0ac80e2
GS
215 while True:
216 pins = self.wait(conds)
615f86f6
GS
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)
fcdd8c68 220 self.handle_bits(item, num_item_bits)