]> sigrok.org Git - libsigrokdecode.git/blob - decoders/z80/pd.py
130b147de718711c737bf00c744a30f5836855c7
[libsigrokdecode.git] / decoders / z80 / pd.py
1 ##
2 ## This file is part of the libsigrokdecode project.
3 ##
4 ## Copyright (C) 2014 Daniel Elstner <daniel.kitta@gmail.com>
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 3 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 functools import reduce
22 from .tables import instr_table_by_prefix
23 import string
24
25 class Ann:
26     ADDR, MEMRD, MEMWR, IORD, IOWR, INSTR, ROP, WOP, WARN = range(9)
27 class Row:
28     ADDRBUS, DATABUS, INSTRUCTIONS, OPERANDS, WARNINGS = range(5)
29 class Pin:
30     D0, D7 = 0, 7
31     M1, RD, WR, MREQ, IORQ = range(8, 13)
32     A0, A15 = 13, 28
33 class Cycle:
34     NONE, MEMRD, MEMWR, IORD, IOWR, FETCH, INTACK = range(7)
35
36 # Provide custom format type 'H' for hexadecimal output
37 # with leading decimal digit (assembler syntax).
38 class AsmFormatter(string.Formatter):
39     def format_field(self, value, format_spec):
40         if format_spec.endswith('H'):
41             result = format(value, format_spec[:-1] + 'X')
42             return result if result[0] in string.digits else '0' + result
43         else:
44             return format(value, format_spec)
45
46 formatter = AsmFormatter()
47
48 ann_data_cycle_map = {
49     Cycle.MEMRD:  Ann.MEMRD,
50     Cycle.MEMWR:  Ann.MEMWR,
51     Cycle.IORD:   Ann.IORD,
52     Cycle.IOWR:   Ann.IOWR,
53     Cycle.FETCH:  Ann.MEMRD,
54     Cycle.INTACK: Ann.IORD,
55 }
56
57 def reduce_bus(bus):
58     if 0xFF in bus:
59         return None # unassigned bus probes
60     else:
61         return reduce(lambda a, b: (a << 1) | b, reversed(bus))
62
63 def signed_byte(byte):
64     return byte if byte < 128 else byte - 256
65
66 class Decoder(srd.Decoder):
67     api_version = 1
68     id       = 'z80'
69     name     = 'Z80'
70     longname = 'Zilog Z80 CPU'
71     desc     = 'Zilog Z80 microprocessor disassembly.'
72     license  = 'gplv2+'
73     inputs   = ['logic']
74     outputs  = ['z80']
75     probes = [
76         {'id': 'd%d' % i, 'name': 'D%d' % i, 'desc': 'Data bus line %d' % i}
77             for i in range(8)
78     ] + [
79         {'id': 'm1', 'name': '/M1', 'desc': 'Machine cycle 1'},
80         {'id': 'rd', 'name': '/RD', 'desc': 'Memory or I/O read'},
81         {'id': 'wr', 'name': '/WR', 'desc': 'Memory or I/O write'},
82     ]
83     optional_probes = [
84         {'id': 'mreq', 'name': '/MREQ', 'desc': 'Memory request'},
85         {'id': 'iorq', 'name': '/IORQ', 'desc': 'I/O request'},
86     ] + [
87         {'id': 'a%d' % i, 'name': 'A%d' % i, 'desc': 'Address bus line %d' % i}
88             for i in range(16)
89     ]
90     annotations = [
91         ['addr',  'Memory or I/O address'],
92         ['memrd', 'Byte read from memory'],
93         ['memwr', 'Byte written to memory'],
94         ['iord',  'Byte read from I/O port'],
95         ['iowr',  'Byte written to I/O port'],
96         ['instr', 'Z80 CPU instruction'],
97         ['rop',   'Value of input operand'],
98         ['wop',   'Value of output operand'],
99         ['warn',  'Warning message'],
100     ]
101     annotation_rows = (
102         ('addrbus', 'Address bus', (Ann.ADDR,)),
103         ('databus', 'Data bus', (Ann.MEMRD, Ann.MEMWR, Ann.IORD, Ann.IOWR)),
104         ('instructions', 'Instructions', (Ann.INSTR,)),
105         ('operands', 'Operands', (Ann.ROP, Ann.WOP)),
106         ('warnings', 'Warnings', (Ann.WARN,))
107     )
108
109     def __init__(self, **kwargs):
110         self.prev_cycle = Cycle.NONE
111         self.op_state   = self.state_IDLE
112
113     def start(self):
114         self.out_ann    = self.register(srd.OUTPUT_ANN)
115         self.bus_data   = None
116         self.samplenum  = None
117         self.addr_start = None
118         self.data_start = None
119         self.dasm_start = None
120         self.pend_addr  = None
121         self.pend_data  = None
122         self.ann_data   = None
123         self.ann_dasm   = None
124         self.prev_cycle = Cycle.NONE
125         self.op_state   = self.state_IDLE
126         self.instr_len  = 0
127
128     def decode(self, ss, es, data):
129         for (self.samplenum, pins) in data:
130             cycle = Cycle.NONE
131             if pins[Pin.MREQ] != 1: # default to asserted
132                 if pins[Pin.RD] == 0:
133                     cycle = Cycle.FETCH if pins[Pin.M1] == 0 else Cycle.MEMRD
134                 elif pins[Pin.WR] == 0:
135                     cycle = Cycle.MEMWR
136             elif pins[Pin.IORQ] == 0: # default to not asserted
137                 if pins[Pin.M1] == 0:
138                     cycle = Cycle.INTACK
139                 elif pins[Pin.RD] == 0:
140                     cycle = Cycle.IORD
141                 elif pins[Pin.WR] == 0:
142                     cycle = Cycle.IOWR
143
144             if cycle != Cycle.NONE:
145                 self.bus_data = reduce_bus(pins[Pin.D0:Pin.D7+1])
146             if cycle != self.prev_cycle:
147                 if self.prev_cycle == Cycle.NONE:
148                     self.on_cycle_begin(reduce_bus(pins[Pin.A0:Pin.A15+1]))
149                 elif cycle == Cycle.NONE:
150                     self.on_cycle_end()
151                 else:
152                     self.on_cycle_trans()
153             self.prev_cycle = cycle
154
155     def on_cycle_begin(self, bus_addr):
156         if self.pend_addr is not None:
157             self.put_text(self.addr_start, Ann.ADDR,
158                           '{:04X}'.format(self.pend_addr))
159         self.addr_start = self.samplenum
160         self.pend_addr  = bus_addr
161
162     def on_cycle_end(self):
163         self.instr_len += 1
164         self.op_state = self.op_state()
165         if self.ann_dasm is not None:
166             self.put_disasm()
167         if self.op_state == self.state_RESTART:
168             self.op_state = self.state_IDLE()
169
170         if self.ann_data is not None:
171             self.put_text(self.data_start, self.ann_data,
172                           '{:02X}'.format(self.pend_data))
173         self.data_start = self.samplenum
174         self.pend_data  = self.bus_data
175         self.ann_data   = ann_data_cycle_map[self.prev_cycle]
176
177     def on_cycle_trans(self):
178         self.put_text(self.samplenum - 1, Ann.WARN,
179                       'Illegal transition between control states')
180         self.pend_addr = None
181         self.ann_data  = None
182         self.ann_dasm  = None
183
184     def put_disasm(self):
185         text = formatter.format(self.mnemonic, r=self.arg_reg, d=self.arg_dis,
186                                 j=self.arg_dis+self.instr_len, i=self.arg_imm,
187                                 ro=self.arg_read, wo=self.arg_write)
188         self.put_text(self.dasm_start, self.ann_dasm, text)
189         self.ann_dasm   = None
190         self.dasm_start = self.samplenum
191
192     def put_text(self, ss, ann_idx, ann_text):
193         self.put(ss, self.samplenum, self.out_ann, [ann_idx, [ann_text]])
194
195     def state_RESTART(self):
196         return self.state_IDLE
197
198     def state_IDLE(self):
199         if self.prev_cycle != Cycle.FETCH:
200             return self.state_IDLE
201         self.want_dis   = 0
202         self.want_imm   = 0
203         self.want_read  = 0
204         self.want_write = 0
205         self.want_wr_be = False
206         self.op_repeat  = False
207         self.arg_dis    = 0
208         self.arg_imm    = 0
209         self.arg_read   = 0
210         self.arg_write  = 0
211         self.arg_reg    = ''
212         self.mnemonic   = ''
213         self.instr_pend = False
214         self.read_pend  = False
215         self.write_pend = False
216         self.dasm_start = self.samplenum
217         self.op_prefix  = 0
218         self.instr_len  = 0
219         if self.bus_data in (0xCB, 0xED, 0xDD, 0xFD):
220             return self.state_PRE1
221         else:
222             return self.state_OPCODE
223
224     def state_PRE1(self):
225         if self.prev_cycle != Cycle.FETCH:
226             self.mnemonic = 'Prefix not followed by fetch'
227             self.ann_dasm = Ann.WARN
228             return self.state_RESTART
229         self.op_prefix = self.pend_data
230         if self.op_prefix in (0xDD, 0xFD):
231             if self.bus_data == 0xCB:
232                 return self.state_PRE2
233             if self.bus_data in (0xDD, 0xED, 0xFD):
234                 return self.state_PRE1
235         return self.state_OPCODE
236
237     def state_PRE2(self):
238         if self.prev_cycle != Cycle.MEMRD:
239             self.mnemonic = 'Missing displacement'
240             self.ann_dasm = Ann.WARN
241             return self.state_RESTART
242         self.op_prefix = (self.op_prefix << 8) | self.pend_data
243         return self.state_PREDIS
244
245     def state_PREDIS(self):
246         if self.prev_cycle != Cycle.MEMRD:
247             self.mnemonic = 'Missing opcode'
248             self.ann_dasm = Ann.WARN
249             return self.state_RESTART
250         self.arg_dis = signed_byte(self.pend_data)
251         return self.state_OPCODE
252
253     def state_OPCODE(self):
254         (table, self.arg_reg) = instr_table_by_prefix[self.op_prefix]
255         self.op_prefix = 0
256         instruction = table.get(self.pend_data, None)
257         if instruction is None:
258             self.mnemonic = 'Invalid instruction'
259             self.ann_dasm = Ann.WARN
260             return self.state_RESTART
261         (self.want_dis, self.want_imm, self.want_read, want_write,
262                 self.op_repeat, self.mnemonic) = instruction
263         self.want_write = abs(want_write)
264         self.want_wr_be = (want_write < 0)
265         if self.want_dis > 0:
266             return self.state_POSTDIS
267         if self.want_imm > 0:
268             return self.state_IMM1
269         self.ann_dasm = Ann.INSTR
270         if self.want_read > 0 and self.prev_cycle in (Cycle.MEMRD, Cycle.IORD):
271             return self.state_ROP1
272         if self.want_write > 0 and self.prev_cycle in (Cycle.MEMWR, Cycle.IOWR):
273             return self.state_WOP1
274         return self.state_RESTART
275
276     def state_POSTDIS(self):
277         self.arg_dis = signed_byte(self.pend_data)
278         if self.want_imm > 0:
279             return self.state_IMM1
280         self.ann_dasm = Ann.INSTR
281         if self.want_read > 0 and self.prev_cycle in (Cycle.MEMRD, Cycle.IORD):
282             return self.state_ROP1
283         if self.want_write > 0 and self.prev_cycle in (Cycle.MEMWR, Cycle.IOWR):
284             return self.state_WOP1
285         return self.state_RESTART
286
287     def state_IMM1(self):
288         self.arg_imm = self.pend_data
289         if self.want_imm > 1:
290             return self.state_IMM2
291         self.ann_dasm = Ann.INSTR
292         if self.want_read > 0 and self.prev_cycle in (Cycle.MEMRD, Cycle.IORD):
293             return self.state_ROP1
294         if self.want_write > 0 and self.prev_cycle in (Cycle.MEMWR, Cycle.IOWR):
295             return self.state_WOP1
296         return self.state_RESTART
297
298     def state_IMM2(self):
299         self.arg_imm |= self.pend_data << 8
300         self.ann_dasm = Ann.INSTR
301         if self.want_read > 0 and self.prev_cycle in (Cycle.MEMRD, Cycle.IORD):
302             return self.state_ROP1
303         if self.want_write > 0 and self.prev_cycle in (Cycle.MEMWR, Cycle.IOWR):
304             return self.state_WOP1
305         return self.state_RESTART
306
307     def state_ROP1(self):
308         self.arg_read = self.pend_data
309         if self.want_read < 2:
310             self.mnemonic = '{ro:02X}'
311             self.ann_dasm = Ann.ROP
312         if self.want_write > 0:
313             return self.state_WOP1
314         if self.want_read > 1:
315             return self.state_ROP2
316         if self.op_repeat and self.prev_cycle in (Cycle.MEMRD, Cycle.IORD):
317             return self.state_ROP1
318         return self.state_RESTART
319
320     def state_ROP2(self):
321         self.arg_read |= self.pend_data << 8
322         self.mnemonic = '{ro:04X}'
323         self.ann_dasm = Ann.ROP
324         if self.want_write > 0 and self.prev_cycle in (Cycle.MEMWR, Cycle.IOWR):
325             return self.state_WOP1
326         return self.state_RESTART
327
328     def state_WOP1(self):
329         self.arg_write = self.pend_data
330         if self.want_read > 1:
331             return self.state_ROP2
332         if self.want_write > 1:
333             return self.state_WOP2
334         self.mnemonic = '{wo:02X}'
335         self.ann_dasm = Ann.WOP
336         if self.want_read > 0 and self.op_repeat and \
337                 self.prev_cycle in (Cycle.MEMRD, Cycle.IORD):
338             return self.state_ROP1
339         return self.state_RESTART
340
341     def state_WOP2(self):
342         if self.want_wr_be:
343             self.arg_write = (self.arg_write << 8) | self.pend_data
344         else:
345             self.arg_write |= self.pend_data << 8
346         self.mnemonic = '{wo:04X}'
347         self.ann_dasm = Ann.WOP
348         return self.state_RESTART