]>
Commit | Line | Data |
---|---|---|
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 | ||
20 | import sigrokdecode as srd | |
21 | ||
22 | class Decoder(srd.Decoder): | |
b197383c | 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'] | |
e1eacaab | 30 | outputs = ['eeprom93xx'] |
3a9c517e KR |
31 | options = ( |
32 | {'id': 'addresssize', 'desc': 'Address size', 'default': 8}, | |
33 | {'id': 'wordsize', 'desc': 'Word size', 'default': 16}, | |
34 | ) | |
35 | annotations = ( | |
36 | ('si-data', 'SI data'), | |
37 | ('so-data', 'SO data'), | |
38 | ('warning', 'Warning'), | |
39 | ) | |
40 | annotation_rows = ( | |
41 | ('data', 'Data', (0, 1)), | |
42 | ('warnings', 'Warnings', (2,)), | |
43 | ) | |
44 | ||
45 | def __init__(self): | |
10aeb8ea GS |
46 | self.reset() |
47 | ||
48 | def reset(self): | |
3a9c517e KR |
49 | self.frame = [] |
50 | ||
51 | def start(self): | |
52 | self.out_ann = self.register(srd.OUTPUT_ANN) | |
53 | self.addresssize = self.options['addresssize'] | |
54 | self.wordsize = self.options['wordsize'] | |
55 | ||
56 | def put_address(self, data): | |
57 | # Get address (MSb first). | |
58 | a = 0 | |
59 | for b in range(len(data)): | |
941e7a64 | 60 | a += (data[b].si << (len(data) - b - 1)) |
5f914c47 | 61 | self.put(data[0].ss, data[-1].es, self.out_ann, |
3a9c517e KR |
62 | [0, ['Address: 0x%x' % a, 'Addr: 0x%x' % a, '0x%x' % a]]) |
63 | ||
64 | def put_word(self, si, data): | |
65 | # Decode word (MSb first). | |
66 | word = 0 | |
67 | for b in range(len(data)): | |
941e7a64 UH |
68 | d = data[b].si if si else data[b].so |
69 | word += (d << (len(data) - b - 1)) | |
b13ced31 | 70 | idx = 0 if si else 1 |
5f914c47 | 71 | self.put(data[0].ss, data[-1].es, |
b13ced31 | 72 | self.out_ann, [idx, ['Data: 0x%x' % word, '0x%x' % word]]) |
3a9c517e KR |
73 | |
74 | def decode(self, ss, es, data): | |
75 | if len(data) < (2 + self.addresssize): | |
76 | self.put(ss, es, self.out_ann, [2, ['Not enough packet bits']]) | |
77 | return | |
78 | ||
941e7a64 | 79 | opcode = (data[0].si << 1) + (data[1].si << 0) |
3a9c517e KR |
80 | |
81 | if opcode == 2: | |
82 | # READ instruction. | |
5f914c47 | 83 | self.put(data[0].ss, data[1].es, |
3a9c517e KR |
84 | self.out_ann, [0, ['Read word', 'READ']]) |
85 | self.put_address(data[2:2 + self.addresssize]) | |
86 | ||
87 | # Get all words. | |
88 | word_start = 2 + self.addresssize | |
89 | while len(data) - word_start > 0: | |
90 | # Check if there are enough bits for a word. | |
91 | if len(data) - word_start < self.wordsize: | |
5f914c47 | 92 | self.put(data[word_start].ss, data[len(data) - 1].es, |
3a9c517e KR |
93 | self.out_ann, [2, ['Not enough word bits']]) |
94 | break | |
95 | self.put_word(False, data[word_start:word_start + self.wordsize]) | |
96 | # Go to next word. | |
97 | word_start += self.wordsize | |
98 | elif opcode == 1: | |
99 | # WRITE instruction. | |
5f914c47 | 100 | self.put(data[0].ss, data[1].es, |
3a9c517e KR |
101 | self.out_ann, [0, ['Write word', 'WRITE']]) |
102 | self.put_address(data[2:2 + self.addresssize]) | |
103 | # Get word. | |
104 | if len(data) < 2 + self.addresssize + self.wordsize: | |
941e7a64 UH |
105 | self.put(data[2 + self.addresssize].ss, |
106 | data[len(data) - 1].ss, | |
3a9c517e KR |
107 | self.out_ann, [2, ['Not enough word bits']]) |
108 | else: | |
109 | self.put_word(True, data[2 + self.addresssize:2 + self.addresssize + self.wordsize]) | |
110 | elif opcode == 3: | |
111 | # ERASE instruction. | |
5f914c47 | 112 | self.put(data[0].ss, data[1].es, |
3a9c517e KR |
113 | self.out_ann, [0, ['Erase word', 'ERASE']]) |
114 | self.put_address(data[2:2 + self.addresssize]) | |
115 | elif opcode == 0: | |
941e7a64 | 116 | if data[2].si == 1 and data[3].si == 1: |
3a9c517e | 117 | # WEN instruction. |
5f914c47 | 118 | self.put(data[0].ss, data[2 + self.addresssize - 1].es, |
3a9c517e | 119 | self.out_ann, [0, ['Write enable', 'WEN']]) |
941e7a64 | 120 | elif data[2].si == 0 and data[3].si == 0: |
3a9c517e | 121 | # WDS instruction. |
5f914c47 | 122 | self.put(data[0].ss, data[2 + self.addresssize - 1].es, |
3a9c517e | 123 | self.out_ann, [0, ['Write disable', 'WDS']]) |
941e7a64 | 124 | elif data[2].si == 1 and data[3].si == 0: |
3a9c517e | 125 | # ERAL instruction. |
5f914c47 | 126 | self.put(data[0].ss, data[2 + self.addresssize - 1].es, |
3a9c517e KR |
127 | self.out_ann, [0, ['Erase all memory', |
128 | 'Erase all', 'ERAL']]) | |
941e7a64 | 129 | elif data[2].si == 0 and data[3].si == 1: |
3a9c517e | 130 | # WRAL instruction. |
5f914c47 | 131 | self.put(data[0].ss, data[2 + self.addresssize - 1].es, |
3a9c517e KR |
132 | self.out_ann, [0, ['Write all memory', |
133 | 'Write all', 'WRAL']]) | |
134 | # Get word. | |
135 | if len(data) < 2 + self.addresssize + self.wordsize: | |
941e7a64 UH |
136 | self.put(data[2 + self.addresssize].ss, |
137 | data[len(data) - 1].ss, | |
3a9c517e KR |
138 | self.out_ann, [2, ['Not enough word bits']]) |
139 | else: | |
140 | self.put_word(True, data[2 + self.addresssize:2 + self.addresssize + self.wordsize]) |