]> sigrok.org Git - libsigrokdecode.git/blame - decoders/eeprom93xx/pd.py
Backport recent changes from mainline.
[libsigrokdecode.git] / decoders / eeprom93xx / pd.py
CommitLineData
3a9c517e
KR
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
20import sigrokdecode as srd
21
22class Decoder(srd.Decoder):
2ba06442 23 api_version = 3
e1eacaab
UH
24 id = 'eeprom93xx'
25 name = '93xx EEPROM'
26 longname = '93xx Microwire EEPROM'
27 desc = '93xx series Microwire EEPROM protocol.'
3a9c517e
KR
28 license = 'gplv2+'
29 inputs = ['microwire']
3f5f3a92
UH
30 outputs = []
31 tags = ['IC', 'Memory']
3a9c517e
KR
32 options = (
33 {'id': 'addresssize', 'desc': 'Address size', 'default': 8},
34 {'id': 'wordsize', 'desc': 'Word size', 'default': 16},
3f5f3a92
UH
35 {'id': 'format', 'desc': 'Data format', 'default': 'hex',
36 'values': ('ascii', 'hex')},
3a9c517e
KR
37 )
38 annotations = (
39 ('si-data', 'SI data'),
40 ('so-data', 'SO data'),
41 ('warning', 'Warning'),
42 )
43 annotation_rows = (
44 ('data', 'Data', (0, 1)),
45 ('warnings', 'Warnings', (2,)),
46 )
3f5f3a92
UH
47 binary = (
48 ('address', 'Address'),
49 ('data', 'Data'),
50 )
3a9c517e
KR
51
52 def __init__(self):
2ba06442
UH
53 self.reset()
54
55 def reset(self):
3a9c517e
KR
56 self.frame = []
57
58 def start(self):
59 self.out_ann = self.register(srd.OUTPUT_ANN)
3f5f3a92 60 self.out_binary = self.register(srd.OUTPUT_BINARY)
3a9c517e
KR
61 self.addresssize = self.options['addresssize']
62 self.wordsize = self.options['wordsize']
63
64 def put_address(self, data):
65 # Get address (MSb first).
66 a = 0
67 for b in range(len(data)):
941e7a64 68 a += (data[b].si << (len(data) - b - 1))
5f914c47 69 self.put(data[0].ss, data[-1].es, self.out_ann,
3f5f3a92
UH
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])])
3a9c517e
KR
72
73 def put_word(self, si, data):
74 # Decode word (MSb first).
75 word = 0
76 for b in range(len(data)):
941e7a64
UH
77 d = data[b].si if si else data[b].so
78 word += (d << (len(data) - b - 1))
b13ced31 79 idx = 0 if si else 1
3f5f3a92
UH
80
81 if self.options['format'] == 'ascii':
82 word_str = ''
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
87 else:
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]])
91 else:
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])])
3a9c517e
KR
96
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']])
100 return
101
941e7a64 102 opcode = (data[0].si << 1) + (data[1].si << 0)
3a9c517e
KR
103
104 if opcode == 2:
105 # READ instruction.
5f914c47 106 self.put(data[0].ss, data[1].es,
3a9c517e
KR
107 self.out_ann, [0, ['Read word', 'READ']])
108 self.put_address(data[2:2 + self.addresssize])
109
110 # Get all words.
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:
5f914c47 115 self.put(data[word_start].ss, data[len(data) - 1].es,
3a9c517e
KR
116 self.out_ann, [2, ['Not enough word bits']])
117 break
118 self.put_word(False, data[word_start:word_start + self.wordsize])
119 # Go to next word.
120 word_start += self.wordsize
121 elif opcode == 1:
122 # WRITE instruction.
5f914c47 123 self.put(data[0].ss, data[1].es,
3a9c517e
KR
124 self.out_ann, [0, ['Write word', 'WRITE']])
125 self.put_address(data[2:2 + self.addresssize])
126 # Get word.
127 if len(data) < 2 + self.addresssize + self.wordsize:
941e7a64
UH
128 self.put(data[2 + self.addresssize].ss,
129 data[len(data) - 1].ss,
3a9c517e
KR
130 self.out_ann, [2, ['Not enough word bits']])
131 else:
132 self.put_word(True, data[2 + self.addresssize:2 + self.addresssize + self.wordsize])
133 elif opcode == 3:
134 # ERASE instruction.
5f914c47 135 self.put(data[0].ss, data[1].es,
3a9c517e
KR
136 self.out_ann, [0, ['Erase word', 'ERASE']])
137 self.put_address(data[2:2 + self.addresssize])
138 elif opcode == 0:
941e7a64 139 if data[2].si == 1 and data[3].si == 1:
3a9c517e 140 # WEN instruction.
5f914c47 141 self.put(data[0].ss, data[2 + self.addresssize - 1].es,
3a9c517e 142 self.out_ann, [0, ['Write enable', 'WEN']])
941e7a64 143 elif data[2].si == 0 and data[3].si == 0:
3a9c517e 144 # WDS instruction.
5f914c47 145 self.put(data[0].ss, data[2 + self.addresssize - 1].es,
3a9c517e 146 self.out_ann, [0, ['Write disable', 'WDS']])
941e7a64 147 elif data[2].si == 1 and data[3].si == 0:
3a9c517e 148 # ERAL instruction.
5f914c47 149 self.put(data[0].ss, data[2 + self.addresssize - 1].es,
3a9c517e
KR
150 self.out_ann, [0, ['Erase all memory',
151 'Erase all', 'ERAL']])
941e7a64 152 elif data[2].si == 0 and data[3].si == 1:
3a9c517e 153 # WRAL instruction.
5f914c47 154 self.put(data[0].ss, data[2 + self.addresssize - 1].es,
3a9c517e
KR
155 self.out_ann, [0, ['Write all memory',
156 'Write all', 'WRAL']])
157 # Get word.
158 if len(data) < 2 + self.addresssize + self.wordsize:
941e7a64
UH
159 self.put(data[2 + self.addresssize].ss,
160 data[len(data) - 1].ss,
3a9c517e
KR
161 self.out_ann, [2, ['Not enough word bits']])
162 else:
163 self.put_word(True, data[2 + self.addresssize:2 + self.addresssize + self.wordsize])