1 // license:BSD-3-Clause
2 // copyright-holders:Juergen Buchmueller
3 #ifndef MAME_CPU_Z80_Z80_H
4 #define MAME_CPU_Z80_Z80_H
5 
6 #pragma once
7 
8 #include "machine/z80daisy.h"
9 
10 enum
11 {
12 	NSC800_RSTA = INPUT_LINE_IRQ0 + 1,
13 	NSC800_RSTB,
14 	NSC800_RSTC,
15 	Z80_INPUT_LINE_WAIT,
16 	Z80_INPUT_LINE_BOGUSWAIT, /* WAIT pin implementation used to be nonexistent, please remove this when all drivers are updated with Z80_INPUT_LINE_WAIT */
17 	Z80_INPUT_LINE_BUSRQ
18 };
19 
20 enum
21 {
22 	Z80_PC = STATE_GENPC, Z80_SP = 1,
23 	Z80_A, Z80_B, Z80_C, Z80_D, Z80_E, Z80_H, Z80_L,
24 	Z80_AF, Z80_BC, Z80_DE, Z80_HL,
25 	Z80_IX, Z80_IY, Z80_AF2, Z80_BC2, Z80_DE2, Z80_HL2,
26 	Z80_R, Z80_I, Z80_IM, Z80_IFF1, Z80_IFF2, Z80_HALT,
27 	Z80_DC0, Z80_DC1, Z80_DC2, Z80_DC3, Z80_WZ
28 };
29 
30 class z80_device : public cpu_device, public z80_daisy_chain_interface
31 {
32 public:
33 	z80_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock);
34 
35 	void z80_set_cycle_tables(const uint8_t *op, const uint8_t *cb, const uint8_t *ed, const uint8_t *xy, const uint8_t *xycb, const uint8_t *ex);
set_memory_map(T &&...args)36 	template <typename... T> void set_memory_map(T &&... args) { set_addrmap(AS_PROGRAM, std::forward<T>(args)...); }
set_m1_map(T &&...args)37 	template <typename... T> void set_m1_map(T &&... args) { set_addrmap(AS_OPCODES, std::forward<T>(args)...); }
set_io_map(T &&...args)38 	template <typename... T> void set_io_map(T &&... args) { set_addrmap(AS_IO, std::forward<T>(args)...); }
irqack_cb()39 	auto irqack_cb() { return m_irqack_cb.bind(); }
refresh_cb()40 	auto refresh_cb() { return m_refresh_cb.bind(); }
halt_cb()41 	auto halt_cb() { return m_halt_cb.bind(); }
42 
43 protected:
44 	z80_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, uint32_t clock);
45 
46 	// device-level overrides
47 	virtual void device_start() override;
48 	virtual void device_reset() override;
49 
50 	// device_execute_interface overrides
execute_min_cycles()51 	virtual uint32_t execute_min_cycles() const noexcept override { return 2; }
execute_max_cycles()52 	virtual uint32_t execute_max_cycles() const noexcept override { return 16; }
execute_input_lines()53 	virtual uint32_t execute_input_lines() const noexcept override { return 4; }
execute_default_irq_vector(int inputnum)54 	virtual uint32_t execute_default_irq_vector(int inputnum) const noexcept override { return 0xff; }
execute_input_edge_triggered(int inputnum)55 	virtual bool execute_input_edge_triggered(int inputnum) const noexcept override { return inputnum == INPUT_LINE_NMI; }
56 	virtual void execute_run() override;
57 	virtual void execute_set_input(int inputnum, int state) override;
58 
59 	// device_memory_interface overrides
60 	virtual space_config_vector memory_space_config() const override;
61 
62 	// device_state_interface overrides
63 	virtual void state_import(const device_state_entry &entry) override;
64 	virtual void state_export(const device_state_entry &entry) override;
65 	virtual void state_string_export(const device_state_entry &entry, std::string &str) const override;
66 
67 	// device_disasm_interface overrides
68 	virtual std::unique_ptr<util::disasm_interface> create_disassembler() override;
69 
70 #undef PROTOTYPES
71 #define PROTOTYPES(prefix) \
72 	void prefix##_00(); void prefix##_01(); void prefix##_02(); void prefix##_03(); \
73 	void prefix##_04(); void prefix##_05(); void prefix##_06(); void prefix##_07(); \
74 	void prefix##_08(); void prefix##_09(); void prefix##_0a(); void prefix##_0b(); \
75 	void prefix##_0c(); void prefix##_0d(); void prefix##_0e(); void prefix##_0f(); \
76 	void prefix##_10(); void prefix##_11(); void prefix##_12(); void prefix##_13(); \
77 	void prefix##_14(); void prefix##_15(); void prefix##_16(); void prefix##_17(); \
78 	void prefix##_18(); void prefix##_19(); void prefix##_1a(); void prefix##_1b(); \
79 	void prefix##_1c(); void prefix##_1d(); void prefix##_1e(); void prefix##_1f(); \
80 	void prefix##_20(); void prefix##_21(); void prefix##_22(); void prefix##_23(); \
81 	void prefix##_24(); void prefix##_25(); void prefix##_26(); void prefix##_27(); \
82 	void prefix##_28(); void prefix##_29(); void prefix##_2a(); void prefix##_2b(); \
83 	void prefix##_2c(); void prefix##_2d(); void prefix##_2e(); void prefix##_2f(); \
84 	void prefix##_30(); void prefix##_31(); void prefix##_32(); void prefix##_33(); \
85 	void prefix##_34(); void prefix##_35(); void prefix##_36(); void prefix##_37(); \
86 	void prefix##_38(); void prefix##_39(); void prefix##_3a(); void prefix##_3b(); \
87 	void prefix##_3c(); void prefix##_3d(); void prefix##_3e(); void prefix##_3f(); \
88 	void prefix##_40(); void prefix##_41(); void prefix##_42(); void prefix##_43(); \
89 	void prefix##_44(); void prefix##_45(); void prefix##_46(); void prefix##_47(); \
90 	void prefix##_48(); void prefix##_49(); void prefix##_4a(); void prefix##_4b(); \
91 	void prefix##_4c(); void prefix##_4d(); void prefix##_4e(); void prefix##_4f(); \
92 	void prefix##_50(); void prefix##_51(); void prefix##_52(); void prefix##_53(); \
93 	void prefix##_54(); void prefix##_55(); void prefix##_56(); void prefix##_57(); \
94 	void prefix##_58(); void prefix##_59(); void prefix##_5a(); void prefix##_5b(); \
95 	void prefix##_5c(); void prefix##_5d(); void prefix##_5e(); void prefix##_5f(); \
96 	void prefix##_60(); void prefix##_61(); void prefix##_62(); void prefix##_63(); \
97 	void prefix##_64(); void prefix##_65(); void prefix##_66(); void prefix##_67(); \
98 	void prefix##_68(); void prefix##_69(); void prefix##_6a(); void prefix##_6b(); \
99 	void prefix##_6c(); void prefix##_6d(); void prefix##_6e(); void prefix##_6f(); \
100 	void prefix##_70(); void prefix##_71(); void prefix##_72(); void prefix##_73(); \
101 	void prefix##_74(); void prefix##_75(); void prefix##_76(); void prefix##_77(); \
102 	void prefix##_78(); void prefix##_79(); void prefix##_7a(); void prefix##_7b(); \
103 	void prefix##_7c(); void prefix##_7d(); void prefix##_7e(); void prefix##_7f(); \
104 	void prefix##_80(); void prefix##_81(); void prefix##_82(); void prefix##_83(); \
105 	void prefix##_84(); void prefix##_85(); void prefix##_86(); void prefix##_87(); \
106 	void prefix##_88(); void prefix##_89(); void prefix##_8a(); void prefix##_8b(); \
107 	void prefix##_8c(); void prefix##_8d(); void prefix##_8e(); void prefix##_8f(); \
108 	void prefix##_90(); void prefix##_91(); void prefix##_92(); void prefix##_93(); \
109 	void prefix##_94(); void prefix##_95(); void prefix##_96(); void prefix##_97(); \
110 	void prefix##_98(); void prefix##_99(); void prefix##_9a(); void prefix##_9b(); \
111 	void prefix##_9c(); void prefix##_9d(); void prefix##_9e(); void prefix##_9f(); \
112 	void prefix##_a0(); void prefix##_a1(); void prefix##_a2(); void prefix##_a3(); \
113 	void prefix##_a4(); void prefix##_a5(); void prefix##_a6(); void prefix##_a7(); \
114 	void prefix##_a8(); void prefix##_a9(); void prefix##_aa(); void prefix##_ab(); \
115 	void prefix##_ac(); void prefix##_ad(); void prefix##_ae(); void prefix##_af(); \
116 	void prefix##_b0(); void prefix##_b1(); void prefix##_b2(); void prefix##_b3(); \
117 	void prefix##_b4(); void prefix##_b5(); void prefix##_b6(); void prefix##_b7(); \
118 	void prefix##_b8(); void prefix##_b9(); void prefix##_ba(); void prefix##_bb(); \
119 	void prefix##_bc(); void prefix##_bd(); void prefix##_be(); void prefix##_bf(); \
120 	void prefix##_c0(); void prefix##_c1(); void prefix##_c2(); void prefix##_c3(); \
121 	void prefix##_c4(); void prefix##_c5(); void prefix##_c6(); void prefix##_c7(); \
122 	void prefix##_c8(); void prefix##_c9(); void prefix##_ca(); void prefix##_cb(); \
123 	void prefix##_cc(); void prefix##_cd(); void prefix##_ce(); void prefix##_cf(); \
124 	void prefix##_d0(); void prefix##_d1(); void prefix##_d2(); void prefix##_d3(); \
125 	void prefix##_d4(); void prefix##_d5(); void prefix##_d6(); void prefix##_d7(); \
126 	void prefix##_d8(); void prefix##_d9(); void prefix##_da(); void prefix##_db(); \
127 	void prefix##_dc(); void prefix##_dd(); void prefix##_de(); void prefix##_df(); \
128 	void prefix##_e0(); void prefix##_e1(); void prefix##_e2(); void prefix##_e3(); \
129 	void prefix##_e4(); void prefix##_e5(); void prefix##_e6(); void prefix##_e7(); \
130 	void prefix##_e8(); void prefix##_e9(); void prefix##_ea(); void prefix##_eb(); \
131 	void prefix##_ec(); void prefix##_ed(); void prefix##_ee(); void prefix##_ef(); \
132 	void prefix##_f0(); void prefix##_f1(); void prefix##_f2(); void prefix##_f3(); \
133 	void prefix##_f4(); void prefix##_f5(); void prefix##_f6(); void prefix##_f7(); \
134 	void prefix##_f8(); void prefix##_f9(); void prefix##_fa(); void prefix##_fb(); \
135 	void prefix##_fc(); void prefix##_fd(); void prefix##_fe(); void prefix##_ff();
136 
137 	void illegal_1();
138 	void illegal_2();
139 
140 	PROTOTYPES(op)
141 	PROTOTYPES(cb)
142 	PROTOTYPES(dd)
143 	PROTOTYPES(ed)
144 	PROTOTYPES(fd)
145 	PROTOTYPES(xycb)
146 
147 	void halt();
148 	void leave_halt();
149 	uint8_t in(uint16_t port);
150 	void out(uint16_t port, uint8_t value);
151 	virtual uint8_t rm(uint16_t addr);
152 	void rm16(uint16_t addr, PAIR &r);
153 	virtual void wm(uint16_t addr, uint8_t value);
154 	void wm16(uint16_t addr, PAIR &r);
155 	virtual uint8_t rop();
156 	virtual uint8_t arg();
157 	virtual uint16_t arg16();
158 	void eax();
159 	void eay();
160 	void pop(PAIR &r);
161 	void push(PAIR &r);
162 	void jp(void);
163 	void jp_cond(bool cond);
164 	void jr();
165 	void jr_cond(bool cond, uint8_t opcode);
166 	void call();
167 	void call_cond(bool cond, uint8_t opcode);
168 	void ret_cond(bool cond, uint8_t opcode);
169 	void retn();
170 	void reti();
171 	void ld_r_a();
172 	void ld_a_r();
173 	void ld_i_a();
174 	void ld_a_i();
175 	void rst(uint16_t addr);
176 	uint8_t inc(uint8_t value);
177 	uint8_t dec(uint8_t value);
178 	void rlca();
179 	void rrca();
180 	void rla();
181 	void rra();
182 	void rrd();
183 	void rld();
184 	void add_a(uint8_t value);
185 	void adc_a(uint8_t value);
186 	void sub(uint8_t value);
187 	void sbc_a(uint8_t value);
188 	void neg();
189 	void daa();
190 	void and_a(uint8_t value);
191 	void or_a(uint8_t value);
192 	void xor_a(uint8_t value);
193 	void cp(uint8_t value);
194 	void ex_af();
195 	void ex_de_hl();
196 	void exx();
197 	void ex_sp(PAIR &r);
198 	void add16(PAIR &dr, PAIR &sr);
199 	void adc_hl(PAIR &r);
200 	void sbc_hl(PAIR &r);
201 	uint8_t rlc(uint8_t value);
202 	uint8_t rrc(uint8_t value);
203 	uint8_t rl(uint8_t value);
204 	uint8_t rr(uint8_t value);
205 	uint8_t sla(uint8_t value);
206 	uint8_t sra(uint8_t value);
207 	uint8_t sll(uint8_t value);
208 	uint8_t srl(uint8_t value);
209 	void bit(int bit, uint8_t value);
210 	void bit_hl(int bit, uint8_t value);
211 	void bit_xy(int bit, uint8_t value);
212 	uint8_t res(int bit, uint8_t value);
213 	uint8_t set(int bit, uint8_t value);
214 	void ldi();
215 	void cpi();
216 	void ini();
217 	void outi();
218 	void ldd();
219 	void cpd();
220 	void ind();
221 	void outd();
222 	void ldir();
223 	void cpir();
224 	void inir();
225 	void otir();
226 	void lddr();
227 	void cpdr();
228 	void indr();
229 	void otdr();
230 	void ei();
231 
232 	virtual void check_interrupts();
233 	void take_interrupt();
234 	void take_nmi();
235 
236 	// address spaces
237 	const address_space_config m_program_config;
238 	const address_space_config m_opcodes_config;
239 	const address_space_config m_io_config;
240 	memory_access<16, 0, 0, ENDIANNESS_LITTLE>::cache m_args;
241 	memory_access<16, 0, 0, ENDIANNESS_LITTLE>::cache m_opcodes;
242 	memory_access<16, 0, 0, ENDIANNESS_LITTLE>::specific m_data;
243 	memory_access<16, 0, 0, ENDIANNESS_LITTLE>::specific m_io;
244 
245 	devcb_write_line m_irqack_cb;
246 	devcb_write8 m_refresh_cb;
247 	devcb_write_line m_halt_cb;
248 
249 	PAIR            m_prvpc;
250 	PAIR            m_pc;
251 	PAIR            m_sp;
252 	PAIR            m_af;
253 	PAIR            m_bc;
254 	PAIR            m_de;
255 	PAIR            m_hl;
256 	PAIR            m_ix;
257 	PAIR            m_iy;
258 	PAIR            m_wz;
259 	PAIR            m_af2;
260 	PAIR            m_bc2;
261 	PAIR            m_de2;
262 	PAIR            m_hl2;
263 	uint8_t           m_r;
264 	uint8_t           m_r2;
265 	uint8_t           m_iff1;
266 	uint8_t           m_iff2;
267 	uint8_t           m_halt;
268 	uint8_t           m_im;
269 	uint8_t           m_i;
270 	uint8_t           m_nmi_state;          /* nmi line state */
271 	uint8_t           m_nmi_pending;        /* nmi pending */
272 	uint8_t           m_irq_state;          /* irq line state */
273 	int             m_wait_state;         // wait line state
274 	int             m_busrq_state;        // bus request line state
275 	uint8_t           m_after_ei;           /* are we in the EI shadow? */
276 	uint8_t           m_after_ldair;        /* same, but for LD A,I or LD A,R */
277 	uint32_t          m_ea;
278 
279 	int             m_icount;
280 	uint8_t           m_rtemp;
281 	const uint8_t *   m_cc_op;
282 	const uint8_t *   m_cc_cb;
283 	const uint8_t *   m_cc_ed;
284 	const uint8_t *   m_cc_xy;
285 	const uint8_t *   m_cc_xycb;
286 	const uint8_t *   m_cc_ex;
287 };
288 
DECLARE_DEVICE_TYPE(Z80,z80_device)289 DECLARE_DEVICE_TYPE(Z80, z80_device)
290 
291 class nsc800_device : public z80_device
292 {
293 public:
294 	nsc800_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock);
295 
296 protected:
297 	// device-level overrides
298 	virtual void device_start() override;
299 	virtual void device_reset() override;
300 
301 	// device_execute_interface overrides
302 	virtual uint32_t execute_input_lines() const noexcept override { return 7; }
303 	virtual void execute_set_input(int inputnum, int state) override;
304 
305 	virtual void check_interrupts() override;
306 	void take_interrupt_nsc800();
307 	uint8_t m_nsc800_irq_state[4]; /* state of NSC800 restart interrupts A, B, C */
308 };
309 
310 DECLARE_DEVICE_TYPE(NSC800, nsc800_device)
311 
312 
313 #endif // MAME_CPU_Z80_Z80_H
314