]> sigrok.org Git - libsigrokdecode.git/blame_incremental - decoders/eeprom93xx/pd.py
seven_segment: Simplify two code snippets.
[libsigrokdecode.git] / decoders / eeprom93xx / pd.py
... / ...
CommitLineData
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):
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 = []
31 tags = ['IC', 'Memory']
32 options = (
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')},
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 )
47
48 def __init__(self):
49 self.reset()
50
51 def reset(self):
52 self.frame = []
53
54 def start(self):
55 self.out_ann = self.register(srd.OUTPUT_ANN)
56 self.addresssize = self.options['addresssize']
57 self.wordsize = self.options['wordsize']
58
59 def put_address(self, data):
60 # Get address (MSb first).
61 a = 0
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]])
66
67 def put_word(self, si, data):
68 # Decode word (MSb first).
69 word = 0
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))
73 idx = 0 if si else 1
74
75 if self.options['format'] == 'ascii':
76 word_str = ''
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
81 else:
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]])
85 else:
86 self.put(data[0].ss, data[-1].es,
87 self.out_ann, [idx, ['Data: 0x%x' % word, '0x%x' % word]])
88
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']])
92 return
93
94 opcode = (data[0].si << 1) + (data[1].si << 0)
95
96 if opcode == 2:
97 # READ instruction.
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])
101
102 # Get all words.
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']])
109 break
110 self.put_word(False, data[word_start:word_start + self.wordsize])
111 # Go to next word.
112 word_start += self.wordsize
113 elif opcode == 1:
114 # WRITE instruction.
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])
118 # Get word.
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']])
123 else:
124 self.put_word(True, data[2 + self.addresssize:2 + self.addresssize + self.wordsize])
125 elif opcode == 3:
126 # ERASE instruction.
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])
130 elif opcode == 0:
131 if data[2].si == 1 and data[3].si == 1:
132 # WEN instruction.
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:
136 # WDS instruction.
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:
140 # ERAL instruction.
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:
145 # WRAL instruction.
146 self.put(data[0].ss, data[2 + self.addresssize - 1].es,
147 self.out_ann, [0, ['Write all memory',
148 'Write all', 'WRAL']])
149 # Get word.
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']])
154 else:
155 self.put_word(True, data[2 + self.addresssize:2 + self.addresssize + self.wordsize])