summaryrefslogtreecommitdiff
path: root/decoders/eeprom93xx/pd.py
blob: 68649b5cfba1283cbc70c3314574d0a9428eefb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
##
## This file is part of the libsigrokdecode project.
##
## Copyright (C) 2017 Kevin Redon <kingkevin@cuvoodoo.info>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, see <http://www.gnu.org/licenses/>.
##

import sigrokdecode as srd

class Decoder(srd.Decoder):
    api_version = 3
    id = 'eeprom93xx'
    name = '93xx EEPROM'
    longname = '93xx Microwire EEPROM'
    desc = '93xx series Microwire EEPROM protocol.'
    license = 'gplv2+'
    inputs = ['microwire']
    outputs = []
    tags = ['IC', 'Memory']
    options = (
        {'id': 'addresssize', 'desc': 'Address size', 'default': 8},
        {'id': 'wordsize', 'desc': 'Word size', 'default': 16},
        {'id': 'format', 'desc': 'Data format', 'default': 'hex',
            'values': ('ascii', 'hex')},
    )
    annotations = (
        ('si-data', 'SI data'),
        ('so-data', 'SO data'),
        ('warning', 'Warning'),
    )
    annotation_rows = (
        ('data', 'Data', (0, 1)),
        ('warnings', 'Warnings', (2,)),
    )

    def __init__(self):
        self.reset()

    def reset(self):
        self.frame = []

    def start(self):
        self.out_ann = self.register(srd.OUTPUT_ANN)
        self.addresssize = self.options['addresssize']
        self.wordsize = self.options['wordsize']

    def put_address(self, data):
        # Get address (MSb first).
        a = 0
        for b in range(len(data)):
            a += (data[b].si << (len(data) - b - 1))
        self.put(data[0].ss, data[-1].es, self.out_ann,
                 [0, ['Address: 0x%x' % a, 'Addr: 0x%x' % a, '0x%x' % a]])

    def put_word(self, si, data):
        # Decode word (MSb first).
        word = 0
        for b in range(len(data)):
            d = data[b].si if si else data[b].so
            word += (d << (len(data) - b - 1))
        idx = 0 if si else 1

        if self.options['format'] == 'ascii':
            word_str = ''
            for s in range(0, len(data), 8):
                c = 0xff & (word >> s)
                if c in range(32, 126 + 1):
                    word_str = chr(c) + word_str
                else:
                    word_str = '[{:02X}]'.format(c) + word_str
            self.put(data[0].ss, data[-1].es,
                    self.out_ann, [idx, ['Data: %s' % word_str, '%s' % word_str]])
        else:
            self.put(data[0].ss, data[-1].es,
                    self.out_ann, [idx, ['Data: 0x%x' % word, '0x%x' % word]])

    def decode(self, ss, es, data):
        if len(data) < (2 + self.addresssize):
            self.put(ss, es, self.out_ann, [2, ['Not enough packet bits']])
            return

        opcode = (data[0].si << 1) + (data[1].si << 0)

        if opcode == 2:
            # READ instruction.
            self.put(data[0].ss, data[1].es,
                     self.out_ann, [0, ['Read word', 'READ']])
            self.put_address(data[2:2 + self.addresssize])

            # Get all words.
            word_start = 2 + self.addresssize
            while len(data) - word_start > 0:
                # Check if there are enough bits for a word.
                if len(data) - word_start < self.wordsize:
                    self.put(data[word_start].ss, data[len(data) - 1].es,
                             self.out_ann, [2, ['Not enough word bits']])
                    break
                self.put_word(False, data[word_start:word_start + self.wordsize])
                # Go to next word.
                word_start += self.wordsize
        elif opcode == 1:
            # WRITE instruction.
            self.put(data[0].ss, data[1].es,
                     self.out_ann, [0, ['Write word', 'WRITE']])
            self.put_address(data[2:2 + self.addresssize])
            # Get word.
            if len(data) < 2 + self.addresssize + self.wordsize:
                self.put(data[2 + self.addresssize].ss,
                         data[len(data) - 1].ss,
                         self.out_ann, [2, ['Not enough word bits']])
            else:
                self.put_word(True, data[2 + self.addresssize:2 + self.addresssize + self.wordsize])
        elif opcode == 3:
            # ERASE instruction.
            self.put(data[0].ss, data[1].es,
                     self.out_ann, [0, ['Erase word', 'ERASE']])
            self.put_address(data[2:2 + self.addresssize])
        elif opcode == 0:
            if data[2].si == 1 and data[3].si == 1:
                # WEN instruction.
                self.put(data[0].ss, data[2 + self.addresssize - 1].es,
                         self.out_ann, [0, ['Write enable', 'WEN']])
            elif data[2].si == 0 and data[3].si == 0:
                # WDS instruction.
                self.put(data[0].ss, data[2 + self.addresssize - 1].es,
                         self.out_ann, [0, ['Write disable', 'WDS']])
            elif data[2].si == 1 and data[3].si == 0:
                # ERAL instruction.
                self.put(data[0].ss, data[2 + self.addresssize - 1].es,
                         self.out_ann, [0, ['Erase all memory',
                                            'Erase all', 'ERAL']])
            elif data[2].si == 0 and data[3].si == 1:
                # WRAL instruction.
                self.put(data[0].ss, data[2 + self.addresssize - 1].es,
                         self.out_ann, [0, ['Write all memory',
                                            'Write all', 'WRAL']])
                # Get word.
                if len(data) < 2 + self.addresssize + self.wordsize:
                    self.put(data[2 + self.addresssize].ss,
                             data[len(data) - 1].ss,
                             self.out_ann, [2, ['Not enough word bits']])
                else:
                    self.put_word(True, data[2 + self.addresssize:2 + self.addresssize + self.wordsize])