]> sigrok.org Git - libsigrokdecode.git/blame - decoders/mcs48/pd.py
jtag_ejtag: Move bin2int() to common/srdhelper.
[libsigrokdecode.git] / decoders / mcs48 / pd.py
CommitLineData
426325ea 1##
2## This file is part of the libsigrokdecode project.
3##
4## Copyright (C) 2018 fenugrec <fenugrec@users.sourceforge.net>
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
426325ea 20import sigrokdecode as srd
21
22class ChannelError(Exception):
23 pass
24
25class Decoder(srd.Decoder):
26 api_version = 3
27 id = 'mcs48'
28 name = 'MCS-48'
29 longname = 'Intel MCS-48'
30 desc = 'Intel MCS-48 external memory access protocol.'
31 license = 'gplv2+'
32 inputs = ['logic']
33 outputs = ['mcs48']
52e8d8b7
GS
34 channels = (
35 {'id': 'ale', 'name': 'ALE', 'desc': 'Address latch enable'},
36 {'id': 'psen', 'name': '/PSEN', 'desc': 'Program store enable'},
37 ) + tuple({
426325ea 38 'id': 'd%d' % i,
39 'name': 'D%d' % i,
40 'desc': 'CPU data line %d' % i
52e8d8b7 41 } for i in range(0, 8)
426325ea 42 ) + tuple({
43 'id': 'a%d' % i,
44 'name': 'A%d' % i,
45 'desc': 'CPU address line %d' % i
52e8d8b7
GS
46 } for i in range(8, 12)
47 )
48 optional_channels = tuple({
49 'id': 'a%d' % i,
50 'name': 'A%d' % i,
51 'desc': 'CPU address line %d' % i
52 } for i in range(12, 13)
426325ea 53 )
54 annotations = (
55 ('romdata', 'Address:Data'),
56 )
57 binary = (
58 ('romdata', 'AAAA:DD'),
59 )
52e8d8b7
GS
60 OFF_ALE, OFF_PSEN = 0, 1
61 OFF_DATA_BOT, OFF_DATA_TOP = 2, 10
62 OFF_ADDR_BOT, OFF_ADDR_TOP = 10, 14
63 OFF_BANK_BOT, OFF_BANK_TOP = 14, 15
426325ea 64
65 def __init__(self):
52e8d8b7
GS
66 self.reset()
67
68 def reset(self):
426325ea 69 self.addr = 0
70 self.addr_s = 0
71 self.data = 0
72 self.data_s = 0
73
74 # Flag to make sure we get an ALE pulse first.
75 self.started = 0
76
77 def start(self):
78 self.out_ann = self.register(srd.OUTPUT_ANN)
79 self.out_bin = self.register(srd.OUTPUT_BINARY)
80
52e8d8b7 81 def newaddr(self, addr, data):
426325ea 82 # Falling edge on ALE: reconstruct address.
83 self.started = 1
52e8d8b7
GS
84 addr = sum([bit << i for i, bit in enumerate(addr)])
85 addr <<= len(data)
86 addr |= sum([bit << i for i, bit in enumerate(data)])
87 self.addr = addr
426325ea 88 self.addr_s = self.samplenum
89
52e8d8b7 90 def newdata(self, data):
426325ea 91 # Edge on PSEN: get data.
52e8d8b7
GS
92 data = sum([bit << i for i, bit in enumerate(data)])
93 self.data = data
426325ea 94 self.data_s = self.samplenum
95 if self.started:
52e8d8b7
GS
96 anntext = '{:04X}:{:02X}'.format(self.addr, self.data)
97 self.put(self.addr_s, self.data_s, self.out_ann, [0, [anntext]])
98 bindata = self.addr.to_bytes(2, byteorder='big')
99 bindata += self.data.to_bytes(1, byteorder='big')
100 self.put(self.addr_s, self.data_s, self.out_bin, [0, bindata])
426325ea 101
102 def decode(self):
52e8d8b7
GS
103 # Address bits above A11 are optional, and are considered to be A12+.
104 # This logic needs more adjustment when more bank address pins are
105 # to get supported. For now, having just A12 is considered sufficient.
106 has_bank = self.has_channel(self.OFF_BANK_BOT)
107 bank_pin_count = 1 if has_bank else 0
426325ea 108 # Sample address on the falling ALE edge.
109 # Save data on falling edge of PSEN.
110 while True:
52e8d8b7
GS
111 pins = self.wait([{self.OFF_ALE: 'f'}, {self.OFF_PSEN: 'r'}])
112 data = pins[self.OFF_DATA_BOT:self.OFF_DATA_TOP]
113 addr = pins[self.OFF_ADDR_BOT:self.OFF_ADDR_TOP]
114 bank = pins[self.OFF_BANK_BOT:self.OFF_BANK_TOP]
115 if has_bank:
116 addr += bank[:bank_pin_count]
426325ea 117 # Handle those conditions (one or more) that matched this time.
118 if self.matched[0]:
52e8d8b7 119 self.newaddr(addr, data)
426325ea 120 if self.matched[1]:
52e8d8b7 121 self.newdata(data)