]> sigrok.org Git - libsigrokdecode.git/blob - decoders/eeprom93xx/pd.py
avr_isp: Add more parts
[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 = []
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     binary = (
48         ('address', 'Address'),
49         ('data', 'Data'),
50     )
51
52     def __init__(self):
53         self.reset()
54
55     def reset(self):
56         self.frame = []
57
58     def start(self):
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']
63
64     def put_address(self, data):
65         # Get address (MSb first).
66         a = 0
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])])
72
73     def put_word(self, si, data):
74         # Decode word (MSb first).
75         word = 0
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))
79         idx = 0 if si else 1
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])])
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
102         opcode = (data[0].si << 1) + (data[1].si << 0)
103
104         if opcode == 2:
105             # READ instruction.
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])
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:
115                     self.put(data[word_start].ss, data[len(data) - 1].es,
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.
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])
126             # Get word.
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']])
131             else:
132                 self.put_word(True, data[2 + self.addresssize:2 + self.addresssize + self.wordsize])
133         elif opcode == 3:
134             # ERASE instruction.
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])
138         elif opcode == 0:
139             if data[2].si == 1 and data[3].si == 1:
140                 # WEN instruction.
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:
144                 # WDS instruction.
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:
148                 # ERAL instruction.
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:
153                 # WRAL instruction.
154                 self.put(data[0].ss, data[2 + self.addresssize - 1].es,
155                          self.out_ann, [0, ['Write all memory',
156                                             'Write all', 'WRAL']])
157                 # Get word.
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']])
162                 else:
163                     self.put_word(True, data[2 + self.addresssize:2 + self.addresssize + self.wordsize])