2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2017 Kevin Redon <kingkevin@cuvoodoo.info>
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.
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.
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/>.
20 import sigrokdecode as srd
22 class Decoder(srd.Decoder):
26 longname = '93xx Microwire EEPROM'
27 desc = '93xx series Microwire EEPROM protocol.'
29 inputs = ['microwire']
31 tags = ['IC', 'Memory']
33 {'id': 'addresssize', 'desc': 'Address size', 'default': 8},
34 {'id': 'wordsize', 'desc': 'Word size', 'default': 16},
35 {'id': 'format', 'desc': 'Data format', 'default': 'hex',
36 'values': ('ascii', 'hex')},
39 ('si-data', 'SI data'),
40 ('so-data', 'SO data'),
41 ('warning', 'Warning'),
44 ('data', 'Data', (0, 1)),
45 ('warnings', 'Warnings', (2,)),
55 self.out_ann = self.register(srd.OUTPUT_ANN)
56 self.addresssize = self.options['addresssize']
57 self.wordsize = self.options['wordsize']
59 def put_address(self, data):
60 # Get address (MSb first).
62 for b in range(len(data)):
63 a += (data[b].si << (len(data) - b - 1))
64 self.put(data[0].ss, data[-1].es, self.out_ann,
65 [0, ['Address: 0x%x' % a, 'Addr: 0x%x' % a, '0x%x' % a]])
67 def put_word(self, si, data):
68 # Decode word (MSb first).
70 for b in range(len(data)):
71 d = data[b].si if si else data[b].so
72 word += (d << (len(data) - b - 1))
75 if self.options['format'] == 'ascii':
77 for s in range(0, len(data), 8):
78 c = 0xff & (word >> s)
79 if c in range(32, 126 + 1):
80 word_str = chr(c) + word_str
82 word_str = '[{:02X}]'.format(c) + word_str
83 self.put(data[0].ss, data[-1].es,
84 self.out_ann, [idx, ['Data: %s' % word_str, '%s' % word_str]])
86 self.put(data[0].ss, data[-1].es,
87 self.out_ann, [idx, ['Data: 0x%x' % word, '0x%x' % word]])
89 def decode(self, ss, es, data):
90 if len(data) < (2 + self.addresssize):
91 self.put(ss, es, self.out_ann, [2, ['Not enough packet bits']])
94 opcode = (data[0].si << 1) + (data[1].si << 0)
98 self.put(data[0].ss, data[1].es,
99 self.out_ann, [0, ['Read word', 'READ']])
100 self.put_address(data[2:2 + self.addresssize])
103 word_start = 2 + self.addresssize
104 while len(data) - word_start > 0:
105 # Check if there are enough bits for a word.
106 if len(data) - word_start < self.wordsize:
107 self.put(data[word_start].ss, data[len(data) - 1].es,
108 self.out_ann, [2, ['Not enough word bits']])
110 self.put_word(False, data[word_start:word_start + self.wordsize])
112 word_start += self.wordsize
115 self.put(data[0].ss, data[1].es,
116 self.out_ann, [0, ['Write word', 'WRITE']])
117 self.put_address(data[2:2 + self.addresssize])
119 if len(data) < 2 + self.addresssize + self.wordsize:
120 self.put(data[2 + self.addresssize].ss,
121 data[len(data) - 1].ss,
122 self.out_ann, [2, ['Not enough word bits']])
124 self.put_word(True, data[2 + self.addresssize:2 + self.addresssize + self.wordsize])
127 self.put(data[0].ss, data[1].es,
128 self.out_ann, [0, ['Erase word', 'ERASE']])
129 self.put_address(data[2:2 + self.addresssize])
131 if data[2].si == 1 and data[3].si == 1:
133 self.put(data[0].ss, data[2 + self.addresssize - 1].es,
134 self.out_ann, [0, ['Write enable', 'WEN']])
135 elif data[2].si == 0 and data[3].si == 0:
137 self.put(data[0].ss, data[2 + self.addresssize - 1].es,
138 self.out_ann, [0, ['Write disable', 'WDS']])
139 elif data[2].si == 1 and data[3].si == 0:
141 self.put(data[0].ss, data[2 + self.addresssize - 1].es,
142 self.out_ann, [0, ['Erase all memory',
143 'Erase all', 'ERAL']])
144 elif data[2].si == 0 and data[3].si == 1:
146 self.put(data[0].ss, data[2 + self.addresssize - 1].es,
147 self.out_ann, [0, ['Write all memory',
148 'Write all', 'WRAL']])
150 if len(data) < 2 + self.addresssize + self.wordsize:
151 self.put(data[2 + self.addresssize].ss,
152 data[len(data) - 1].ss,
153 self.out_ann, [2, ['Not enough word bits']])
155 self.put_word(True, data[2 + self.addresssize:2 + self.addresssize + self.wordsize])