2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2014 Daniel Elstner <daniel.kitta@gmail.com>
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.
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
21 from functools import reduce
22 from .tables import instr_table_by_prefix
26 ADDR, MEMRD, MEMWR, IORD, IOWR, INSTR, ROP, WOP, WARN = range(9)
28 ADDRBUS, DATABUS, INSTRUCTIONS, OPERANDS, WARNINGS = range(5)
31 M1, RD, WR, MREQ, IORQ = range(8, 13)
34 NONE, MEMRD, MEMWR, IORD, IOWR, FETCH, INTACK = range(7)
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
44 return format(value, format_spec)
46 formatter = AsmFormatter()
48 ann_data_cycle_map = {
49 Cycle.MEMRD: Ann.MEMRD,
50 Cycle.MEMWR: Ann.MEMWR,
53 Cycle.FETCH: Ann.MEMRD,
54 Cycle.INTACK: Ann.IORD,
59 return None # unassigned bus probes
61 return reduce(lambda a, b: (a << 1) | b, reversed(bus))
63 def signed_byte(byte):
64 return byte if byte < 128 else byte - 256
66 class Decoder(srd.Decoder):
70 longname = 'Zilog Z80 CPU'
71 desc = 'Zilog Z80 microprocessor disassembly.'
76 {'id': 'd%d' % i, 'name': 'D%d' % i, 'desc': 'Data bus line %d' % i}
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'},
84 {'id': 'mreq', 'name': '/MREQ', 'desc': 'Memory request'},
85 {'id': 'iorq', 'name': '/IORQ', 'desc': 'I/O request'},
87 {'id': 'a%d' % i, 'name': 'A%d' % i, 'desc': 'Address bus line %d' % i}
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'],
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,))
109 def __init__(self, **kwargs):
110 self.prev_cycle = Cycle.NONE
111 self.op_state = self.state_IDLE
114 self.out_ann = self.register(srd.OUTPUT_ANN)
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
124 self.prev_cycle = Cycle.NONE
125 self.op_state = self.state_IDLE
128 def decode(self, ss, es, data):
129 for (self.samplenum, pins) in data:
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:
136 elif pins[Pin.IORQ] == 0: # default to not asserted
137 if pins[Pin.M1] == 0:
139 elif pins[Pin.RD] == 0:
141 elif pins[Pin.WR] == 0:
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:
152 self.on_cycle_trans()
153 self.prev_cycle = cycle
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
162 def on_cycle_end(self):
164 self.op_state = self.op_state()
165 if self.ann_dasm is not None:
167 if self.op_state == self.state_RESTART:
168 self.op_state = self.state_IDLE()
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]
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
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)
190 self.dasm_start = self.samplenum
192 def put_text(self, ss, ann_idx, ann_text):
193 self.put(ss, self.samplenum, self.out_ann, [ann_idx, [ann_text]])
195 def state_RESTART(self):
196 return self.state_IDLE
198 def state_IDLE(self):
199 if self.prev_cycle != Cycle.FETCH:
200 return self.state_IDLE
205 self.want_wr_be = False
206 self.op_repeat = False
213 self.instr_pend = False
214 self.read_pend = False
215 self.write_pend = False
216 self.dasm_start = self.samplenum
219 if self.bus_data in (0xCB, 0xED, 0xDD, 0xFD):
220 return self.state_PRE1
222 return self.state_OPCODE
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
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
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
253 def state_OPCODE(self):
254 (table, self.arg_reg) = instr_table_by_prefix[self.op_prefix]
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
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
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
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
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
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
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
341 def state_WOP2(self):
343 self.arg_write = (self.arg_write << 8) | self.pend_data
345 self.arg_write |= self.pend_data << 8
346 self.mnemonic = '{wo:04X}'
347 self.ann_dasm = Ann.WOP
348 return self.state_RESTART