]> sigrok.org Git - libsigrokdecode.git/blob - decoders/signature/pd.py
Add decoder: signature analysis
[libsigrokdecode.git] / decoders / signature / pd.py
1 ##
2 ## This file is part of the libsigrokdecode project.
3 ##
4 ## Copyright (C) 2019 Shirow Miura <shirowmiura@gmail.com>
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, see <http://www.gnu.org/licenses/>.
18 ##
19
20 import sigrokdecode as srd
21
22 symbol_map = {
23     0b0000: '0',
24     0b1000: '1',
25     0b0100: '2',
26     0b1100: '3',
27     0b0010: '4',
28     0b1010: '5',
29     0b0110: '6',
30     0b1110: '7',
31     0b0001: '8',
32     0b1001: '9',
33     0b0101: 'A',
34     0b1101: 'C',
35     0b0011: 'F',
36     0b1011: 'H',
37     0b0111: 'P',
38     0b1111: 'U',
39 }
40
41 START, STOP, CLOCK, DATA = range(4)
42
43 class Decoder(srd.Decoder):
44     api_version = 3
45     id = 'signature'
46     name = 'Signature'
47     longname = 'Signature analysis'
48     desc = 'Annotate signature of logic patterns.'
49     license = 'gplv3+'
50     inputs = ['logic']
51     outputs = []
52     tags = ['Debug/trace', 'Util', 'Encoding']
53     channels = (
54         dict(id='start', name='START', desc='START channel'),
55         dict(id='stop', name='STOP', desc='STOP channel'),
56         dict(id='clk', name='CLOCK', desc='CLOCK channel'),
57         dict(id='data', name='DATA', desc='DATA channel')
58     )
59     options = (
60         dict(id='start_edge', desc='Edge-selection for START channel',
61              default='r', values=('r', 'f')),
62         dict(id='stop_edge', desc='Edge-selection for STOP channel',
63              default='r', values=('r', 'f')),
64         dict(id='clk_edge', desc='Edge-selection for CLOCK channel',
65              default='f', values=('r', 'f')),
66         dict(id='annbits', desc='Enable bit level annotation',
67              default='no', values=('yes', 'no'))
68     )
69     annotations = (
70         ('bit0', 'Bit0'),
71         ('bit1', 'Bit1'),
72         ('start', 'START'),
73         ('stop', 'STOP'),
74         ('sig', 'Sig')
75     )
76     annotation_rows = (
77         ('bits', 'Bits', (0, 1, 2, 3)),
78         ('sig', 'Sig', (4,))
79     )
80
81     def __init__(self):
82         pass
83
84     def start(self):
85         self.out_ann = self.register(srd.OUTPUT_ANN)
86
87     def putsig(self, ss, es, signature):
88         s = ''.join([symbol_map[(signature >>  0) & 0x0f],
89                      symbol_map[(signature >>  4) & 0x0f],
90                      symbol_map[(signature >>  8) & 0x0f],
91                      symbol_map[(signature >> 12) & 0x0f]])
92         self.put(ss, es, self.out_ann, [4, [s]])
93
94     def putb(self, ss, ann):
95         self.put(ss, self.samplenum, self.out_ann, ann)
96
97     def decode(self):
98         opt = self.options
99         start_edge_mode_rising = opt['start_edge'] == 'r'
100         stop_edge_mode_rising = opt['stop_edge'] == 'r'
101         annbits = opt['annbits'] == 'yes'
102         gate_is_open = False
103         sample_start = None
104         started = False
105         last_samplenum = 0
106         prev_start = 0 if start_edge_mode_rising else 1
107         prev_stop = 0 if stop_edge_mode_rising else 1
108         shiftreg = 0
109
110         while True:
111             start, stop, _, data = self.wait({CLOCK: opt['clk_edge']})
112             if start != prev_start and not gate_is_open:
113                 gate_is_open = (start == 1) if start_edge_mode_rising else (start == 0)
114                 if gate_is_open:
115                     # Start sampling.
116                     sample_start = self.samplenum
117                     started = True
118             elif stop != prev_stop and gate_is_open:
119                 gate_is_open = not ((stop == 1) if stop_edge_mode_rising else (stop == 0))
120                 if not gate_is_open:
121                     # Stop sampling.
122                     if annbits:
123                         self.putb(last_samplenum, [3, ['STOP', 'STP', 'P']])
124                     self.putsig(sample_start, self.samplenum, shiftreg)
125                     shiftreg = 0
126                     sample_start = None
127             if gate_is_open:
128                 if annbits:
129                     if started:
130                         s = '<{}>'.format(data)
131                         self.putb(last_samplenum, [2, ['START' + s, 'STR' + s, 'S' + s]])
132                         started = False
133                     else:
134                         self.putb(last_samplenum, [data, [str(data)]])
135                 incoming = (bin(shiftreg & 0b0000_0010_1001_0001).count('1') + data) & 1
136                 shiftreg = (incoming << 15) | (shiftreg >> 1)
137             prev_start = start
138             prev_stop = stop
139             last_samplenum = self.samplenum