2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2017 Marcus Comstedt <marcus@mc.pp.se>
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.
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.
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/>.
20 import sigrokdecode as srd
31 class Decoder(srd.Decoder):
35 longname = 'SEGA Maple bus'
36 desc = 'Maple bus peripheral protocol for SEGA Dreamcast.'
39 outputs = ['maple_bus']
41 {'id': 'sdcka', 'name': 'SDCKA', 'desc': 'Data/clock line A'},
42 {'id': 'sdckb', 'name': 'SDCKB', 'desc': 'Data/clock line B'},
45 ('start', 'Start pattern'),
46 ('end', 'End pattern'),
47 ('start-with-crc', 'Start pattern with CRC'),
48 ('occupancy', 'SDCKB occupancy pattern'),
49 ('reset', 'RESET pattern'),
51 ('size', 'Data size'),
52 ('source', 'Source AP'),
53 ('dest', 'Destination AP'),
54 ('command', 'Command'),
56 ('checksum', 'Checksum'),
57 ('frame-error', 'Frame error'),
58 ('checksum-error', 'Checksum error'),
59 ('size-error', 'Size error'),
62 ('bits', 'Bits', (0, 1, 2, 3, 4, 5)),
63 ('fields', 'Fields', (6, 7, 8, 9, 10, 11)),
64 ('warnings', 'Warnings', (12, 13, 14)),
67 ('size', 'Data size'),
68 ('source', 'Source AP'),
69 ('dest', 'Destination AP'),
70 ('command', 'Command code'),
72 ('checksum', 'Checksum'),
79 self.out_ann = self.register(srd.OUTPUT_ANN)
80 self.out_binary = self.register(srd.OUTPUT_BINARY)
81 self.pending_bit_pos = None
84 self.put(self.ss, self.es, self.out_ann, data)
87 self.put(self.ss, self.es, self.out_binary, data)
89 def byte_annotation(self, bintype, d):
91 ['%s: %02X' % (name, d) for name in ann[bintype]] + ['%02X' % d]]
94 self.putx([0, ['Start pattern', 'Start', 'S']])
97 self.putx([1, ['End pattern', 'End', 'E']])
98 if self.length != self.expected_length + 1:
99 self.putx([14, ['Size error', 'L error', 'LE']])
101 def got_start_with_crc(self):
102 self.putx([2, ['Start pattern with CRC', 'Start CRC', 'SC']])
104 def got_occupancy(self):
105 self.putx([3, ['SDCKB occupancy pattern', 'Occupancy', 'O']])
108 self.putx([4, ['RESET pattern', 'RESET', 'R']])
110 def output_pending_bit(self):
111 if self.pending_bit_pos:
112 self.put(self.pending_bit_pos, self.pending_bit_pos, self.out_ann, [5, ['Bit: %d' % self.pending_bit, '%d' % self.pending_bit]])
114 def got_bit(self, n):
115 self.output_pending_bit()
116 self.data = self.data * 2 + n
118 self.pending_bit_pos = self.samplenum
121 self.output_pending_bit()
125 self.expected_length = 4 * (self.data + 1)
126 bintype = self.length
127 elif self.length == self.expected_length:
129 if self.data != self.checksum:
130 self.putx([13, ['Cksum error', 'K error', 'KE']])
131 self.length = self.length + 1
132 self.checksum = self.checksum ^ self.data
133 self.putx(self.byte_annotation(bintype, self.data))
134 self.putb([bintype, bytes([self.data])])
135 self.pending_bit_pos = None
137 def frame_error(self):
138 self.putx([7, ['Frame error', 'F error', 'FE']])
140 def handle_start(self):
141 self.wait({0: 'l', 1: 'h'})
142 self.ss = self.samplenum
145 sdcka, sdckb = self.wait([{1: 'f'}, {0: 'r'}])
149 self.es = self.samplenum
155 self.got_start_with_crc()
166 def handle_byte_or_stop(self):
167 self.ss = self.samplenum
168 self.pending_bit_pos = None
174 sdcka, sdckb = self.wait([{0: 'f'}, {1: 'f'}])
175 self.es = self.samplenum
180 elif counta == 1 and countb == 0 and self.data == 0 and sdckb == 0:
181 self.wait([{0: 'h', 1: 'h'}, {0: 'f'}, {1: 'f'}])
182 self.es = self.samplenum
191 elif self.matched[1]:
192 if counta == countb + 1:
195 elif counta == 0 and countb == 0 and sdcka == 1 and initial:
196 self.ss = self.samplenum
202 self.es = self.samplenum
208 while not self.handle_start():
211 self.expected_length = 4
213 while self.handle_byte_or_stop():