]> sigrok.org Git - libsigrokdecode.git/blob - decoders/eeprom93xx/pd.py
Add PD tags handling and some tags
[libsigrokdecode.git] / decoders / eeprom93xx / pd.py
1 ##
2 ## This file is part of the libsigrokdecode project.
3 ##
4 ## Copyright (C) 2017 Kevin Redon <kingkevin@cuvoodoo.info>
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 class Decoder(srd.Decoder):
23     api_version = 3
24     id = 'eeprom93xx'
25     name = '93xx EEPROM'
26     longname = '93xx Microwire EEPROM'
27     desc = '93xx series Microwire EEPROM protocol.'
28     license = 'gplv2+'
29     inputs = ['microwire']
30     outputs = ['eeprom93xx']
31     tags = ['Logic', 'Memory']
32     options = (
33         {'id': 'addresssize', 'desc': 'Address size', 'default': 8},
34         {'id': 'wordsize', 'desc': 'Word size', 'default': 16},
35     )
36     annotations = (
37         ('si-data', 'SI data'),
38         ('so-data', 'SO data'),
39         ('warning', 'Warning'),
40     )
41     annotation_rows = (
42         ('data', 'Data', (0, 1)),
43         ('warnings', 'Warnings', (2,)),
44     )
45
46     def __init__(self):
47         self.reset()
48
49     def reset(self):
50         self.frame = []
51
52     def start(self):
53         self.out_ann = self.register(srd.OUTPUT_ANN)
54         self.addresssize = self.options['addresssize']
55         self.wordsize = self.options['wordsize']
56
57     def put_address(self, data):
58         # Get address (MSb first).
59         a = 0
60         for b in range(len(data)):
61             a += (data[b].si << (len(data) - b - 1))
62         self.put(data[0].ss, data[-1].es, self.out_ann,
63                  [0, ['Address: 0x%x' % a, 'Addr: 0x%x' % a, '0x%x' % a]])
64
65     def put_word(self, si, data):
66         # Decode word (MSb first).
67         word = 0
68         for b in range(len(data)):
69             d = data[b].si if si else data[b].so
70             word += (d << (len(data) - b - 1))
71         idx = 0 if si else 1
72         self.put(data[0].ss, data[-1].es,
73                  self.out_ann, [idx, ['Data: 0x%x' % word, '0x%x' % word]])
74
75     def decode(self, ss, es, data):
76         if len(data) < (2 + self.addresssize):
77             self.put(ss, es, self.out_ann, [2, ['Not enough packet bits']])
78             return
79
80         opcode = (data[0].si << 1) + (data[1].si << 0)
81
82         if opcode == 2:
83             # READ instruction.
84             self.put(data[0].ss, data[1].es,
85                      self.out_ann, [0, ['Read word', 'READ']])
86             self.put_address(data[2:2 + self.addresssize])
87
88             # Get all words.
89             word_start = 2 + self.addresssize
90             while len(data) - word_start > 0:
91                 # Check if there are enough bits for a word.
92                 if len(data) - word_start < self.wordsize:
93                     self.put(data[word_start].ss, data[len(data) - 1].es,
94                              self.out_ann, [2, ['Not enough word bits']])
95                     break
96                 self.put_word(False, data[word_start:word_start + self.wordsize])
97                 # Go to next word.
98                 word_start += self.wordsize
99         elif opcode == 1:
100             # WRITE instruction.
101             self.put(data[0].ss, data[1].es,
102                      self.out_ann, [0, ['Write word', 'WRITE']])
103             self.put_address(data[2:2 + self.addresssize])
104             # Get word.
105             if len(data) < 2 + self.addresssize + self.wordsize:
106                 self.put(data[2 + self.addresssize].ss,
107                          data[len(data) - 1].ss,
108                          self.out_ann, [2, ['Not enough word bits']])
109             else:
110                 self.put_word(True, data[2 + self.addresssize:2 + self.addresssize + self.wordsize])
111         elif opcode == 3:
112             # ERASE instruction.
113             self.put(data[0].ss, data[1].es,
114                      self.out_ann, [0, ['Erase word', 'ERASE']])
115             self.put_address(data[2:2 + self.addresssize])
116         elif opcode == 0:
117             if data[2].si == 1 and data[3].si == 1:
118                 # WEN instruction.
119                 self.put(data[0].ss, data[2 + self.addresssize - 1].es,
120                          self.out_ann, [0, ['Write enable', 'WEN']])
121             elif data[2].si == 0 and data[3].si == 0:
122                 # WDS instruction.
123                 self.put(data[0].ss, data[2 + self.addresssize - 1].es,
124                          self.out_ann, [0, ['Write disable', 'WDS']])
125             elif data[2].si == 1 and data[3].si == 0:
126                 # ERAL instruction.
127                 self.put(data[0].ss, data[2 + self.addresssize - 1].es,
128                          self.out_ann, [0, ['Erase all memory',
129                                             'Erase all', 'ERAL']])
130             elif data[2].si == 0 and data[3].si == 1:
131                 # WRAL instruction.
132                 self.put(data[0].ss, data[2 + self.addresssize - 1].es,
133                          self.out_ann, [0, ['Write all memory',
134                                             'Write all', 'WRAL']])
135                 # Get word.
136                 if len(data) < 2 + self.addresssize + self.wordsize:
137                     self.put(data[2 + self.addresssize].ss,
138                              data[len(data) - 1].ss,
139                              self.out_ann, [2, ['Not enough word bits']])
140                 else:
141                     self.put_word(True, data[2 + self.addresssize:2 + self.addresssize + self.wordsize])