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,)),
48 ('address', 'Address'),
59 self.out_ann = self.register(srd.OUTPUT_ANN)
60 self.out_binary = self.register(srd.OUTPUT_BINARY)
61 self.addresssize = self.options['addresssize']
62 self.wordsize = self.options['wordsize']
64 def put_address(self, data):
65 # Get address (MSb first).
67 for b in range(len(data)):
68 a += (data[b].si << (len(data) - b - 1))
69 self.put(data[0].ss, data[-1].es, self.out_ann,
70 [0, ['Address: 0x%04x' % a, 'Addr: 0x%04x' % a, '0x%04x' % a]])
71 self.put(data[0].ss, data[-1].es, self.out_binary, [0, bytes([a])])
73 def put_word(self, si, data):
74 # Decode word (MSb first).
76 for b in range(len(data)):
77 d = data[b].si if si else data[b].so
78 word += (d << (len(data) - b - 1))
81 if self.options['format'] == 'ascii':
83 for s in range(0, len(data), 8):
84 c = 0xff & (word >> s)
85 if c in range(32, 126 + 1):
86 word_str = chr(c) + word_str
88 word_str = '[{:02X}]'.format(c) + word_str
89 self.put(data[0].ss, data[-1].es,
90 self.out_ann, [idx, ['Data: %s' % word_str, '%s' % word_str]])
92 self.put(data[0].ss, data[-1].es,
93 self.out_ann, [idx, ['Data: 0x%04x' % word, '0x%04x' % word]])
94 self.put(data[0].ss, data[-1].es, self.out_binary,
95 [1, bytes([(word & 0xff00) >> 8, word & 0xff])])
97 def decode(self, ss, es, data):
98 if len(data) < (2 + self.addresssize):
99 self.put(ss, es, self.out_ann, [2, ['Not enough packet bits']])
102 opcode = (data[0].si << 1) + (data[1].si << 0)
106 self.put(data[0].ss, data[1].es,
107 self.out_ann, [0, ['Read word', 'READ']])
108 self.put_address(data[2:2 + self.addresssize])
111 word_start = 2 + self.addresssize
112 while len(data) - word_start > 0:
113 # Check if there are enough bits for a word.
114 if len(data) - word_start < self.wordsize:
115 self.put(data[word_start].ss, data[len(data) - 1].es,
116 self.out_ann, [2, ['Not enough word bits']])
118 self.put_word(False, data[word_start:word_start + self.wordsize])
120 word_start += self.wordsize
123 self.put(data[0].ss, data[1].es,
124 self.out_ann, [0, ['Write word', 'WRITE']])
125 self.put_address(data[2:2 + self.addresssize])
127 if len(data) < 2 + self.addresssize + self.wordsize:
128 self.put(data[2 + self.addresssize].ss,
129 data[len(data) - 1].ss,
130 self.out_ann, [2, ['Not enough word bits']])
132 self.put_word(True, data[2 + self.addresssize:2 + self.addresssize + self.wordsize])
135 self.put(data[0].ss, data[1].es,
136 self.out_ann, [0, ['Erase word', 'ERASE']])
137 self.put_address(data[2:2 + self.addresssize])
139 if data[2].si == 1 and data[3].si == 1:
141 self.put(data[0].ss, data[2 + self.addresssize - 1].es,
142 self.out_ann, [0, ['Write enable', 'WEN']])
143 elif data[2].si == 0 and data[3].si == 0:
145 self.put(data[0].ss, data[2 + self.addresssize - 1].es,
146 self.out_ann, [0, ['Write disable', 'WDS']])
147 elif data[2].si == 1 and data[3].si == 0:
149 self.put(data[0].ss, data[2 + self.addresssize - 1].es,
150 self.out_ann, [0, ['Erase all memory',
151 'Erase all', 'ERAL']])
152 elif data[2].si == 0 and data[3].si == 1:
154 self.put(data[0].ss, data[2 + self.addresssize - 1].es,
155 self.out_ann, [0, ['Write all memory',
156 'Write all', 'WRAL']])
158 if len(data) < 2 + self.addresssize + self.wordsize:
159 self.put(data[2 + self.addresssize].ss,
160 data[len(data) - 1].ss,
161 self.out_ann, [2, ['Not enough word bits']])
163 self.put_word(True, data[2 + self.addresssize:2 + self.addresssize + self.wordsize])