1 // license:BSD-3-Clause
2 // copyright-holders:Philip Bennett
3 /***************************************************************************
4 
5     Tatsumi TX-1/Buggy Boy sound hardware
6 
7 ***************************************************************************/
8 
9 #ifndef MAME_AUDIO_TX1_H
10 #define MAME_AUDIO_TX1_H
11 
12 #pragma once
13 
14 #include "machine/i8255.h"
15 #include "sound/ay8910.h"
16 
17 class tx1_sound_device : public device_t, public device_sound_interface
18 {
19 public:
20 	tx1_sound_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock);
21 
22 	void z80_busreq_w(uint16_t data);
23 	uint16_t dipswitches_r();
24 	uint16_t z80_shared_r(offs_t offset);
25 	void z80_shared_w(offs_t offset, uint16_t data);
26 
27 	void z80_intreq_w(uint8_t data);
28 
29 	void ts_w(offs_t offset, uint8_t data);
30 	uint8_t ts_r(offs_t offset);
31 
32 	uint8_t pit8253_r(offs_t offset);
33 	void pit8253_w(offs_t offset, uint8_t data);
34 
35 	INTERRUPT_GEN_MEMBER( z80_irq );
36 
37 protected:
38 
39 	void tx1_ppi_latch_w(uint8_t data);
40 	void tx1_coin_cnt_w(uint8_t data);
41 	uint8_t tx1_ppi_porta_r();
42 	uint8_t tx1_ppi_portb_r();
43 
44 	void ay8910_a_w(uint8_t data);
45 	void ay8910_b_w(uint8_t data);
46 
47 	/*************************************
48 	 *
49 	 *  8253 Programmable Interval Timer
50 	 *
51 	 *************************************/
52 	struct pit8253_state
53 	{
54 		union
55 		{
56 #ifdef LSB_FIRST
57 			struct { uint8_t LSB; uint8_t MSB; } as8bit;
58 #else
59 			struct { uint8_t MSB; uint8_t LSB; } as8bit;
60 #endif
61 			uint16_t val;
62 		} counts[3];
63 
64 		int idx[3];
65 	};
66 
67 	tx1_sound_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, uint32_t clock);
68 
69 	// device-level overrides
70 	virtual ioport_constructor device_input_ports() const override;
71 	virtual void device_add_mconfig(machine_config &config) override;
72 	virtual void device_start() override;
73 	virtual void device_reset() override;
74 
75 	// sound stream update overrides
76 	virtual void sound_stream_update(sound_stream &stream, std::vector<read_stream_view> const &inputs, std::vector<write_stream_view> &outputs) override;
77 
78 	void tx1_sound_io(address_map &map);
79 	void tx1_sound_prg(address_map &map);
80 
81 	// internal state
82 	required_device<cpu_device> m_audiocpu;
83 	required_shared_ptr<uint8_t> m_z80_ram;
84 	optional_device<i8255_device> m_ppi;
85 
86 	required_ioport m_dsw;
87 	required_ioport m_steering;
88 	required_ioport m_accelerator;
89 	required_ioport m_brake;
90 	optional_ioport m_ppi_portd;
91 
92 	sound_stream *m_stream = nullptr;
93 	uint32_t m_freq_to_step = 0;
94 	uint32_t m_step0 = 0;
95 	uint32_t m_step1 = 0;
96 	uint32_t m_step2 = 0;
97 
98 	pit8253_state m_pit8253;
99 
100 	uint8_t m_ay_outputa = 0;
101 	uint8_t m_ay_outputb = 0;
102 
103 	uint8_t m_ppi_latch_a = 0;
104 	uint8_t m_ppi_latch_b = 0;
105 	uint32_t m_ts = 0;
106 
107 	s32 m_pit0 = 0;
108 	s32 m_pit1 = 0;
109 	s32 m_pit2 = 0;
110 
111 	double m_weights0[4] = { 0, 0, 0, 0 };
112 	double m_weights1[3] = { 0, 0, 0 };
113 	double m_weights2[3] = { 0, 0, 0 };
114 	int m_eng0[4] = { 0, 0, 0, 0 };
115 	int m_eng1[4] = { 0, 0, 0, 0 };
116 	int m_eng2[4] = { 0, 0, 0, 0 };
117 
118 	int m_noise_lfsra = 0;
119 	int m_noise_lfsrb = 0;
120 	int m_noise_lfsrc = 0;
121 	int m_noise_lfsrd = 0;
122 	int m_noise_counter = 0;
123 	uint8_t m_ym1_outputa = 0;
124 	uint8_t m_ym2_outputa = 0;
125 	uint8_t m_ym2_outputb = 0;
126 	uint16_t m_eng_voltages[16];
127 };
128 
129 class tx1j_sound_device : public tx1_sound_device
130 {
131 public:
132 	tx1j_sound_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock);
133 
134 protected:
135 
136 	// device-level overrides
137 	virtual ioport_constructor device_input_ports() const override;
138 };
139 
140 class buggyboy_sound_device : public tx1_sound_device
141 {
142 public:
143 	buggyboy_sound_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock);
144 
145 	void ym2_a_w(uint8_t data);
146 	void ym2_b_w(uint8_t data);
147 
148 protected:
149 	buggyboy_sound_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, uint32_t clock);
150 
151 	// device-level overrides
152 	virtual ioport_constructor device_input_ports() const override;
153 	virtual void device_add_mconfig(machine_config &config) override;
154 	virtual void device_start() override;
155 	virtual void device_reset() override;
156 
157 	// sound stream update overrides
158 	virtual void sound_stream_update(sound_stream &stream, std::vector<read_stream_view> const &inputs, std::vector<write_stream_view> &outputs) override;
159 
160 	void ym1_a_w(uint8_t data);
161 
162 	uint8_t bb_analog_r(offs_t offset);
163 	void bb_coin_cnt_w(uint8_t data);
164 
has_coin_counters()165 	virtual bool has_coin_counters() { return false; }
166 
167 	void buggyboy_sound_io(address_map &map);
168 	void buggyboy_sound_prg(address_map &map);
169 
170 	required_device_array<ym2149_device, 2> m_ym;
171 };
172 
173 class buggyboyjr_sound_device : public buggyboy_sound_device
174 {
175 public:
176 	buggyboyjr_sound_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock);
177 
178 protected:
179 	// device-level overrides
180 	virtual ioport_constructor device_input_ports() const override;
181 	virtual void device_add_mconfig(machine_config &config) override;
182 
183 	uint8_t bbjr_analog_r(offs_t offset);
184 
has_coin_counters()185 	virtual bool has_coin_counters() override { return true; }
186 
187 	void buggybjr_sound_prg(address_map &map);
188 };
189 
190 DECLARE_DEVICE_TYPE(TX1_SOUND, tx1_sound_device)
191 DECLARE_DEVICE_TYPE(TX1J_SOUND, tx1j_sound_device)
192 DECLARE_DEVICE_TYPE(BUGGYBOY_SOUND, buggyboy_sound_device)
193 DECLARE_DEVICE_TYPE(BUGGYBOYJR_SOUND, buggyboyjr_sound_device)
194 
195 #endif // MAME_AUDIO_TX1_H
196