]> sigrok.org Git - libsigrokdecode.git/blame - decoders/parallel/pd.py
Use the new Decoder.register() API
[libsigrokdecode.git] / decoders / parallel / pd.py
CommitLineData
25e1418a
UH
1##
2## This file is part of the libsigrokdecode project.
3##
4## Copyright (C) 2013 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, write to the Free Software
18## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19##
20
21# Parallel (sync) bus protocol decoder
22
23import sigrokdecode as srd
24
25'''
26Protocol output format:
27
28Packet:
29[<ptype>, <pdata>]
30
31<ptype>, <pdata>
32 - 'ITEM', [<item>, <itembitsize>]
33 - 'WORD', [<word>, <wordbitsize>, <worditemcount>]
34
35<item>:
36 - A single item (a number). It can be of arbitrary size. The max. number
37 of bits in this item is specified in <itembitsize>.
38
39<itembitsize>:
40 - The size of an item (in bits). For a 4-bit parallel bus this is 4,
41 for a 16-bit parallel bus this is 16, and so on.
42
43<word>:
44 - A single word (a number). It can be of arbitrary size. The max. number
45 of bits in this word is specified in <wordbitsize>. The (exact) number
46 of items in this word is specified in <worditemcount>.
47
48<wordbitsize>:
49 - The size of a word (in bits). For a 2-item word with 8-bit items
50 <wordbitsize> is 16, for a 3-item word with 4-bit items <wordbitsize>
51 is 12, and so on.
52
53<worditemcount>:
54 - The size of a word (in number of items). For a 4-item word (no matter
55 how many bits each item consists of) <worditemcount> is 4, for a 7-item
56 word <worditemcount> is 7, and so on.
57'''
58
59def probe_list(num_probes):
60 l = []
61 for i in range(num_probes):
62 d = {'id': 'd%d' % i, 'name': 'D%d' % i, 'desc': 'Data line %d' % i}
63 l.append(d)
64 return l
65
66class Decoder(srd.Decoder):
67 api_version = 1
68 id = 'parallel'
69 name = 'Parallel'
70 longname = 'Parallel sync bus'
71 desc = 'Generic parallel synchronous bus.'
72 license = 'gplv2+'
73 inputs = ['logic']
74 outputs = ['parallel']
75 probes = [
76 {'id': 'clk', 'name': 'CLK', 'desc': 'Clock line'},
77 ]
a3b4f168 78 optional_probes = probe_list(8)
25e1418a
UH
79 options = {
80 'clock_edge': ['Clock edge to sample on', 'rising'],
81 'wordsize': ['Word size of the data', 1],
82 'endianness': ['Endianness of the data', 'little'],
83 'format': ['Data format', 'hex'],
84 }
85 annotations = [
86 ['items', 'Items'],
87 ['words', 'Words'],
88 ]
89
90 def __init__(self):
91 self.oldclk = None
92 self.items = []
93 self.itemcount = 0
94 self.saved_item = None
95 self.samplenum = 0
96 self.oldpins = None
97 self.ss_item = self.es_item = None
98 self.first = True
99 self.state = 'IDLE'
100
101 def start(self, metadata):
be465111
BV
102 self.out_proto = self.register(srd.OUTPUT_PYTHON)
103 self.out_ann = self.register(srd.OUTPUT_ANN)
25e1418a
UH
104
105 def report(self):
106 pass
107
108 def putpb(self, data):
109 self.put(self.ss_item, self.es_item, self.out_proto, data)
110
111 def putb(self, data):
112 self.put(self.ss_item, self.es_item, self.out_ann, data)
113
114 def putpw(self, data):
115 self.put(self.ss_word, self.es_word, self.out_proto, data)
116
117 def putw(self, data):
118 self.put(self.ss_word, self.es_word, self.out_ann, data)
119
120 def handle_bits(self, datapins):
121 # If this is the first item in a word, save its sample number.
122 if self.itemcount == 0:
123 self.ss_word = self.samplenum
124
125 # Get the bits for this item.
126 item, used_pins = 0, datapins.count(b'\x01') + datapins.count(b'\x00')
127 for i in range(used_pins):
128 item |= datapins[i] << i
129
130 self.items.append(item)
131 self.itemcount += 1
132
133 if self.first == True:
134 # Save the start sample and item for later (no output yet).
135 self.ss_item = self.samplenum
136 self.first = False
137 self.saved_item = item
138 else:
139 # Output the saved item (from the last CLK edge to the current).
140 self.es_item = self.samplenum
141 self.putpb(['ITEM', self.saved_item])
142 self.putb([0, ['%X' % self.saved_item]])
143 self.ss_item = self.samplenum
144 self.saved_item = item
145
146 endian, ws = self.options['endianness'], self.options['wordsize']
147
148 # Get as many items as the configured wordsize says.
149 if self.itemcount < ws:
150 return
151
152 # Output annotations/proto for a word (a collection of items).
153 word = 0
154 for i in range(ws):
155 if endian == 'little':
156 word |= self.items[i] << ((ws - 1 - i) * used_pins)
157 elif endian == 'big':
158 word |= self.items[i] << (i * used_pins)
159
160 self.es_word = self.samplenum
161 # self.putpw(['WORD', word])
162 # self.putw([1, ['%X' % word]])
163 self.ss_word = self.samplenum
164
165 self.itemcount, self.items = 0, []
166
167 def find_clk_edge(self, clk, datapins):
168 # Ignore sample if the clock pin hasn't changed.
169 if clk == self.oldclk:
170 return
171 self.oldclk = clk
172
173 # Sample data on rising/falling clock edge (depends on config).
174 c = self.options['clock_edge']
175 if c == 'rising' and clk == 0: # Sample on rising clock edge.
176 return
177 elif c == 'falling' and clk == 1: # Sample on falling clock edge.
178 return
179
180 # Found the correct clock edge, now get the bits.
181 self.handle_bits(datapins)
182
183 def decode(self, ss, es, data):
184 for (self.samplenum, pins) in data:
185
186 # Ignore identical samples early on (for performance reasons).
187 if self.oldpins == pins:
188 continue
189 self.oldpins = pins
190
191 # State machine.
192 if self.state == 'IDLE':
193 self.find_clk_edge(pins[0], pins[1:])
194 else:
195 raise Exception('Invalid state: %s' % self.state)
196