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