]>
Commit | Line | Data |
---|---|---|
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 | ||
25e1418a UH |
21 | import sigrokdecode as srd |
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 | ||
57 | def probe_list(num_probes): | |
8eafa261 | 58 | l = [{'id': 'clk', 'name': 'CLK', 'desc': 'Clock line'}] |
25e1418a UH |
59 | for i in range(num_probes): |
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 UH |
63 | |
64 | class Decoder(srd.Decoder): | |
65 | api_version = 1 | |
66 | id = 'parallel' | |
67 | name = 'Parallel' | |
68 | longname = 'Parallel sync bus' | |
69 | desc = 'Generic parallel synchronous bus.' | |
70 | license = 'gplv2+' | |
71 | inputs = ['logic'] | |
72 | outputs = ['parallel'] | |
a3b4f168 | 73 | optional_probes = probe_list(8) |
84c1c0b5 BV |
74 | options = ( |
75 | {'id': 'clock_edge', 'desc': 'Clock edge to sample on', | |
76 | 'default': 'rising', 'values': ('rising', 'falling')}, | |
b0918d40 UH |
77 | {'id': 'wordsize', 'desc': 'Data wordsize', 'default': 1}, |
78 | {'id': 'endianness', 'desc': 'Data endianness', | |
84c1c0b5 BV |
79 | 'default': 'little', 'values': ('little', 'big')}, |
80 | ) | |
da9bcbd9 BV |
81 | annotations = ( |
82 | ('items', 'Items'), | |
83 | ('words', 'Words'), | |
84 | ) | |
25e1418a UH |
85 | |
86 | def __init__(self): | |
87 | self.oldclk = None | |
88 | self.items = [] | |
89 | self.itemcount = 0 | |
90 | self.saved_item = None | |
91 | self.samplenum = 0 | |
92 | self.oldpins = None | |
93 | self.ss_item = self.es_item = None | |
94 | self.first = True | |
95 | self.state = 'IDLE' | |
96 | ||
b098b820 | 97 | def start(self): |
c515eed7 | 98 | self.out_python = self.register(srd.OUTPUT_PYTHON) |
be465111 | 99 | self.out_ann = self.register(srd.OUTPUT_ANN) |
25e1418a | 100 | |
25e1418a | 101 | def putpb(self, data): |
c515eed7 | 102 | self.put(self.ss_item, self.es_item, self.out_python, data) |
25e1418a UH |
103 | |
104 | def putb(self, data): | |
105 | self.put(self.ss_item, self.es_item, self.out_ann, data) | |
106 | ||
107 | def putpw(self, data): | |
c515eed7 | 108 | self.put(self.ss_word, self.es_word, self.out_python, data) |
25e1418a UH |
109 | |
110 | def putw(self, data): | |
111 | self.put(self.ss_word, self.es_word, self.out_ann, data) | |
112 | ||
113 | def handle_bits(self, datapins): | |
114 | # If this is the first item in a word, save its sample number. | |
115 | if self.itemcount == 0: | |
116 | self.ss_word = self.samplenum | |
117 | ||
118 | # Get the bits for this item. | |
119 | item, used_pins = 0, datapins.count(b'\x01') + datapins.count(b'\x00') | |
120 | for i in range(used_pins): | |
121 | item |= datapins[i] << i | |
122 | ||
123 | self.items.append(item) | |
124 | self.itemcount += 1 | |
125 | ||
126 | if self.first == True: | |
127 | # Save the start sample and item for later (no output yet). | |
128 | self.ss_item = self.samplenum | |
129 | self.first = False | |
130 | self.saved_item = item | |
131 | else: | |
132 | # Output the saved item (from the last CLK edge to the current). | |
133 | self.es_item = self.samplenum | |
134 | self.putpb(['ITEM', self.saved_item]) | |
135 | self.putb([0, ['%X' % self.saved_item]]) | |
136 | self.ss_item = self.samplenum | |
137 | self.saved_item = item | |
138 | ||
139 | endian, ws = self.options['endianness'], self.options['wordsize'] | |
140 | ||
141 | # Get as many items as the configured wordsize says. | |
142 | if self.itemcount < ws: | |
143 | return | |
144 | ||
c515eed7 | 145 | # Output annotations/python for a word (a collection of items). |
25e1418a UH |
146 | word = 0 |
147 | for i in range(ws): | |
148 | if endian == 'little': | |
149 | word |= self.items[i] << ((ws - 1 - i) * used_pins) | |
150 | elif endian == 'big': | |
151 | word |= self.items[i] << (i * used_pins) | |
152 | ||
153 | self.es_word = self.samplenum | |
154 | # self.putpw(['WORD', word]) | |
155 | # self.putw([1, ['%X' % word]]) | |
156 | self.ss_word = self.samplenum | |
157 | ||
158 | self.itemcount, self.items = 0, [] | |
159 | ||
160 | def find_clk_edge(self, clk, datapins): | |
161 | # Ignore sample if the clock pin hasn't changed. | |
162 | if clk == self.oldclk: | |
163 | return | |
164 | self.oldclk = clk | |
165 | ||
166 | # Sample data on rising/falling clock edge (depends on config). | |
167 | c = self.options['clock_edge'] | |
168 | if c == 'rising' and clk == 0: # Sample on rising clock edge. | |
169 | return | |
170 | elif c == 'falling' and clk == 1: # Sample on falling clock edge. | |
171 | return | |
172 | ||
173 | # Found the correct clock edge, now get the bits. | |
174 | self.handle_bits(datapins) | |
175 | ||
176 | def decode(self, ss, es, data): | |
177 | for (self.samplenum, pins) in data: | |
178 | ||
179 | # Ignore identical samples early on (for performance reasons). | |
180 | if self.oldpins == pins: | |
181 | continue | |
182 | self.oldpins = pins | |
183 | ||
184 | # State machine. | |
185 | if self.state == 'IDLE': | |
8eafa261 UH |
186 | if pins[0] not in (0, 1): |
187 | self.handle_bits(pins[1:]) | |
188 | else: | |
189 | self.find_clk_edge(pins[0], pins[1:]) | |
25e1418a UH |
190 | else: |
191 | raise Exception('Invalid state: %s' % self.state) | |
192 |