1 // license:BSD-3-Clause
2 // copyright-holders:AJR
3 /***************************************************************************
4 
5     Kawasaki Steel (Kawatetsu) KP63(A) Timer/Counter
6 
7 ***************************************************************************/
8 
9 #ifndef MAME_CPU_Z80_KP63_H
10 #define MAME_CPU_Z80_KP63_H
11 
12 #pragma once
13 
14 
15 //**************************************************************************
16 //  TYPE DEFINITIONS
17 //**************************************************************************
18 
19 class kp63_device : public device_t
20 {
21 public:
22 	// callback configuration
outp_callback()23 	template <int N> auto outp_callback() { return m_out_pulse_callback[N].bind(); }
outs_callback()24 	template <int N> auto outs_callback() { return m_out_strobe_callback[N].bind(); }
25 
26 	// register interface
27 	u8 read(offs_t offset);
28 	void write(offs_t offset, u8 data);
29 
30 	// input line interface
DECLARE_WRITE_LINE_MEMBER(gate_w)31 	template <int N> DECLARE_WRITE_LINE_MEMBER(gate_w) { write_gate(N, state); }
32 
33 protected:
34 	// construction/destruction
35 	kp63_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, u32 clock, u8 num_counters, u8 mode_mask);
36 
37 	// device-level overrides
38 	virtual void device_resolve_objects() override;
39 	virtual void device_start() override;
40 	virtual void device_reset() override;
41 
42 private:
43 	static const char *const s_count_modes[4];
44 
45 	// timer callbacks
46 	template <int N> TIMER_CALLBACK_MEMBER(timer_expired);
47 	template <int N> TIMER_CALLBACK_MEMBER(strobe_off);
48 	template <int N> TIMER_CALLBACK_MEMBER(pwm_off);
49 
50 	// internal helpers
51 	void timer_pulse(unsigned n);
52 	void timer_reload(unsigned n);
53 	void timer_resume_count(unsigned n);
54 	u16 timer_get_count(unsigned n) const;
55 	void write_gate(unsigned n, bool state);
56 
57 	// callback objects
58 	devcb_write_line::array<4> m_out_pulse_callback;
59 	devcb_write_line::array<4> m_out_strobe_callback;
60 
61 	// constant parameters
62 	const u8 c_num_counters;
63 	const u8 c_mode_mask;
64 
65 	// internal timers
66 	emu_timer *m_timer[4];
67 	emu_timer *m_strobe_timer[4];
68 	emu_timer *m_pwm_timer[4];
69 
70 	// internal state
71 	u16 m_cr[4];
72 	u16 m_last_count[4];
73 	u8 m_count_tmp[4];
74 	u8 m_status[4];
75 	u8 m_rw_seq;
76 	u8 m_timer_started;
77 	u8 m_gate_input;
78 };
79 
80 class kp63_3channel_device : public kp63_device
81 {
82 public:
83 	// device type constructor
84 	kp63_3channel_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock);
85 };
86 
87 class kp63a_device : public kp63_device
88 {
89 public:
90 	// device type constructor
91 	kp63a_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock);
92 };
93 
94 // device type declarations
95 DECLARE_DEVICE_TYPE(KP63_3CHANNEL, kp63_3channel_device)
96 DECLARE_DEVICE_TYPE(KP63A, kp63a_device)
97 
98 #endif // MAME_CPU_Z80_KP63_H
99