summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/audio/carnival.cpp
blob: 88c1b90195f07cef39dfd9b96d5b424ddc1e0cdd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// license:BSD-3-Clause
// copyright-holders:Zsolt Vasvari, hap
/*
 *  Carnival audio routines
 */

#include "emu.h"
#include "includes/vicdual.h"


/* output port 0x01 definitions - sound effect drive outputs */
#define OUT_PORT_1_RIFLE        0x01
#define OUT_PORT_1_CLANG        0x02
#define OUT_PORT_1_DUCK1        0x04
#define OUT_PORT_1_DUCK2        0x08
#define OUT_PORT_1_DUCK3        0x10
#define OUT_PORT_1_PIPEHIT      0x20
#define OUT_PORT_1_BONUS1       0x40
#define OUT_PORT_1_BONUS2       0x80

/* output port 0x02 definitions - sound effect drive outputs */
#define OUT_PORT_2_BEAR         0x04
#define OUT_PORT_2_RANKING      0x20


#define PLAY(samp,id,loop)      samp->start( id, id, loop )
#define STOP(samp,id)           samp->stop( id )


/* sample file names */
static const char *const carnival_sample_names[] =
{
	"*carnival",
	"bear",
	"bonus1",
	"bonus2",
	"clang",
	"duck1",
	"duck2",
	"duck3",
	"pipehit",
	"ranking",
	"rifle",
	nullptr
};


/* sample IDs - must match sample file name table above */
enum
{
	SND_BEAR = 0,
	SND_BONUS1,
	SND_BONUS2,
	SND_CLANG,
	SND_DUCK1,
	SND_DUCK2,
	SND_DUCK3,
	SND_PIPEHIT,
	SND_RANKING,
	SND_RIFLE
};


void carnival_state::carnival_audio_1_w(uint8_t data)
{
	int bitsChanged;
	int bitsGoneHigh;
	int bitsGoneLow;

	bitsChanged  = m_port1State ^ data;
	bitsGoneHigh = bitsChanged & data;
	bitsGoneLow  = bitsChanged & ~data;

	m_port1State = data;

	if ( bitsGoneLow & OUT_PORT_1_RIFLE )
	{
		PLAY( m_samples, SND_RIFLE, 0 );
	}

	if ( bitsGoneLow & OUT_PORT_1_CLANG )
	{
		PLAY( m_samples, SND_CLANG, 0 );
	}

	if ( bitsGoneLow & OUT_PORT_1_DUCK1 )
	{
		PLAY( m_samples, SND_DUCK1, 1 );
	}
	if ( bitsGoneHigh & OUT_PORT_1_DUCK1 )
	{
		STOP( m_samples, SND_DUCK1 );
	}

	if ( bitsGoneLow & OUT_PORT_1_DUCK2 )
	{
		PLAY( m_samples, SND_DUCK2, 1 );
	}
	if ( bitsGoneHigh & OUT_PORT_1_DUCK2 )
	{
		STOP( m_samples, SND_DUCK2 );
	}

	if ( bitsGoneLow & OUT_PORT_1_DUCK3 )
	{
		PLAY( m_samples, SND_DUCK3, 1 );
	}
	if ( bitsGoneHigh & OUT_PORT_1_DUCK3 )
	{
		STOP( m_samples, SND_DUCK3 );
	}

	if ( bitsGoneLow & OUT_PORT_1_PIPEHIT )
	{
		PLAY( m_samples, SND_PIPEHIT, 0 );
	}

	if ( bitsGoneLow & OUT_PORT_1_BONUS1 )
	{
		PLAY( m_samples, SND_BONUS1, 0 );
	}

	if ( bitsGoneLow & OUT_PORT_1_BONUS2 )
	{
		PLAY( m_samples, SND_BONUS2, 0 );
	}
}

void carnival_state::carnival_audio_2_w(uint8_t data)
{
	int bitsChanged;
	//int bitsGoneHigh;
	int bitsGoneLow;

	bitsChanged  = m_port2State ^ data;
	//bitsGoneHigh = bitsChanged & data;
	bitsGoneLow  = bitsChanged & ~data;

	m_port2State = data;

	if ( bitsGoneLow & OUT_PORT_2_BEAR )
	{
		PLAY( m_samples, SND_BEAR, 0 );
	}

	if ( bitsGoneLow & OUT_PORT_2_RANKING )
	{
		PLAY( m_samples, SND_RANKING, 0 );
	}

	// d4: music board MCU reset
	m_audiocpu->set_input_line(INPUT_LINE_RESET, (data & 0x10) ? CLEAR_LINE : ASSERT_LINE);
}


/* Music board */

// common

void carnival_state::mboard_map(address_map &map)
{
	map(0x0000, 0x03ff).rom();
}

READ_LINE_MEMBER( carnival_state::carnival_music_port_t1_r )
{
	// T1: comms from audio port 2 d3
	return ~m_port2State >> 3 & 1;
}


// AY8912 music

void carnival_state::carnival_psg_latch()
{
	if (m_musicBus & 1)
	{
		// BDIR W, BC1 selects address or data
		if (m_musicBus & 2)
			m_psg->address_w(m_musicData);
		else
			m_psg->data_w(m_musicData);
	}
}

void carnival_state::carnivala_music_port_1_w(uint8_t data)
{
	// P1: AY8912 d0-d7
	m_musicData = data;
	carnival_psg_latch();
}

void carnival_state::carnivala_music_port_2_w(uint8_t data)
{
	// P2 d6: AY8912 BDIR(R/W)
	// P2 d7: AY8912 BC1
	m_musicBus = data >> 6 & 3;
	carnival_psg_latch();
}

void carnival_state::carnivala_audio(machine_config &config)
{
	/* music board */
	I8035(config, m_audiocpu, XTAL(3'579'545));
	m_audiocpu->set_addrmap(AS_PROGRAM, &carnival_state::mboard_map);
	m_audiocpu->p1_out_cb().set(FUNC(carnival_state::carnivala_music_port_1_w));
	m_audiocpu->p2_out_cb().set(FUNC(carnival_state::carnivala_music_port_2_w));
	m_audiocpu->t1_in_cb().set(FUNC(carnival_state::carnival_music_port_t1_r));

	AY8912(config, m_psg, XTAL(3'579'545)/3).add_route(ALL_OUTPUTS, "mono", 0.25); // also seen with AY-3-8910, daughterboard has place for either chip

	/* samples */
	SAMPLES(config, m_samples);
	m_samples->set_channels(10);
	m_samples->set_samples_names(carnival_sample_names);
	m_samples->add_route(ALL_OUTPUTS, "mono", 0.5);
}


// PIT8253 music

void carnival_state::carnivalb_music_port_1_w(uint8_t data)
{
	// P1: PIT8253 d0-d7
	m_musicData = data;
}

void carnival_state::carnivalb_music_port_2_w(uint8_t data)
{
	// P2 d7: PIT8253 write strobe
	// P2 d5,d6: PIT8253 A0,A1
	if (~m_musicBus & data & 0x80)
		m_pit->write(data >> 5 & 3, m_musicData);

	m_musicBus = data;
}

void carnival_state::carnivalb_audio(machine_config &config)
{
	// already inherited carnivala_audio
	config.device_remove("psg");

	m_audiocpu->p1_out_cb().set(FUNC(carnival_state::carnivalb_music_port_1_w));
	m_audiocpu->p2_out_cb().set(FUNC(carnival_state::carnivalb_music_port_2_w));

	PIT8253(config, m_pit, 0);
	m_pit->set_clk<0>(XTAL(3'579'545)/3);
	m_pit->set_clk<1>(XTAL(3'579'545)/3);
	m_pit->set_clk<2>(XTAL(3'579'545)/3);

	m_pit->out_handler<0>().set(m_dac[0], FUNC(dac_bit_interface::write));
	m_pit->out_handler<1>().set(m_dac[1], FUNC(dac_bit_interface::write));
	m_pit->out_handler<2>().set(m_dac[2], FUNC(dac_bit_interface::write));

	for (int i = 0; i < 3; i++)
	{
		DAC_1BIT(config, m_dac[i]).add_route(ALL_OUTPUTS, "mono", 0.15);
		VOLTAGE_REGULATOR(config, m_vref[i]).add_route(0, m_dac[i], 1.0, DAC_VREF_POS_INPUT);
	}
}