1 // license:BSD-3-Clause
2 // copyright-holders:Couriersud
3 /*
4 
5 Battlezone sound info, courtesy of Al Kossow:
6 
7 D7  motor enable            this enables the engine sound
8 D6  start LED
9 D5  sound enable            this enables ALL sound outputs
10                             including the POKEY output
11 D4  engine rev en           this controls the engine speed
12                             the engine sound is an integrated square
13                             wave (saw tooth) that is frequency modulated
14                             by engine rev.
15 D3  shell loud, soft/       explosion volume
16 D2  shell enable
17 D1  explosion loud, soft/   explosion volume
18 D0  explosion enable        gates a noise generator
19 
20 */
21 
22 #include "emu.h"
23 #include "includes/bzone.h"
24 
25 #include "sound/discrete.h"
26 #include "sound/pokey.h"
27 
28 #include "speaker.h"
29 
30 
31 /* This sets an amount of gain boost to apply to the final signal
32  * that will drive it into clipping.  The slider is ajusted by the
33  * reverse factor, so that the final result is not clipped.
34  * This allows for the user to easily adjust the sound into the clipping
35  * range so it sounds more like a real cabinet.
36  */
37 #define BZ_FINAL_GAIN   2
38 
39 #define BZ_NOISE_CLOCK      12000
40 
41 #define TTL_OUT 3.4
42 
43 /*************************************
44  *
45  *  Discrete Sound Defines
46  *
47  *************************************/
48 
49 /* Discrete Sound Input Nodes */
50 #define BZ_INPUT            NODE_01     /* at M2 LS273 */
51 #define BZ_INP_EXPLO        NODE_10_00
52 #define BZ_INP_EXPLOLS      NODE_10_01
53 #define BZ_INP_SHELL        NODE_10_02
54 #define BZ_INP_SHELLLS      NODE_10_03
55 #define BZ_INP_ENGREV       NODE_10_04
56 #define BZ_INP_SOUNDEN      NODE_10_05
57 #define BZ_INP_STARTLED     NODE_10_06
58 #define BZ_INP_MOTEN        NODE_10_07
59 
60 /* Adjusters */
61 #define BZ_R11_POT          NODE_11
62 
63 /* Discrete Sound Output Nodes */
64 #define BZ_NOISE            NODE_20
65 #define BZ_SHELL_SND        NODE_21
66 #define BZ_EXPLOSION_SND    NODE_22
67 #define BZ_ENGINE_SND       NODE_23
68 #define BZ_POKEY_SND        NODE_24
69 
70 /* Parts List - Resistors */
71 #define BZ_R5           RES_K(1)
72 #define BZ_R6           RES_K(4.7)
73 #define BZ_R7           RES_K(1)
74 #define BZ_R8           RES_K(100)
75 #define BZ_R9           RES_K(22)
76 #define BZ_R10          RES_K(100)
77 #define BZ_R11          RES_K(250)
78 #define BZ_R12          RES_K(33)
79 #define BZ_R13          RES_K(10)
80 #define BZ_R14          RES_K(22)
81 #define BZ_R15          RES_K(1)
82 #define BZ_R16          RES_K(1)
83 #define BZ_R17          RES_K(22)
84 #define BZ_R18          RES_K(10)
85 #define BZ_R19          RES_K(33)
86 #define BZ_R20          RES_K(33)
87 #define BZ_R21          RES_K(33)
88 #define BZ_R25          RES_K(100)
89 #define BZ_R26          RES_K(33)
90 #define BZ_R27          RES_K(330)
91 #define BZ_R28          RES_K(100)
92 #define BZ_R29          RES_K(22)
93 #define BZ_R30          RES_K(10)
94 #define BZ_R31          RES_K(100)
95 #define BZ_R32          RES_K(330)
96 #define BZ_R33          RES_K(330)
97 #define BZ_R34          RES_K(33)
98 #define BZ_R35          RES_K(33)
99 
100 /* Parts List - Capacitors */
101 #define BZ_C9           CAP_U(4.7)
102 #define BZ_C11          CAP_U(0.015)
103 #define BZ_C13          CAP_U(10)
104 #define BZ_C14          CAP_U(10)
105 #define BZ_C20          CAP_U(0.1)
106 #define BZ_C21          CAP_U(0.0047)
107 #define BZ_C22          CAP_U(0.0047)
108 #define BZ_C29          CAP_U(0.47)
109 
110 /*************************************
111  *
112  *  Discrete Sound static structs
113  *
114  *************************************/
115 
116 
117 static const discrete_lfsr_desc bzone_lfsr =
118 {
119 	DISC_CLK_IS_FREQ,
120 	16,                     /* Bit Length */
121 	0,                      /* Reset Value */
122 	3,                      /* Use Bit 10 (QC of second LS164) as F0 input 0 */
123 	14,                     /* Use Bit 23 (QH of third LS164) as F0 input 1 */
124 	DISC_LFSR_XOR,          /* F0 is XOR */
125 	DISC_LFSR_NOT_IN0,      /* F1 is inverted F0*/
126 	DISC_LFSR_REPLACE,      /* F2 replaces the shifted register contents */
127 	0x000001,               /* Everything is shifted into the first bit only */
128 	DISC_LFSR_FLAG_OUTPUT_SR_SN1, /* output the complete shift register to sub node 1*/
129 	15                  /* Output bit */
130 };
131 
132 #if 0
133 static const discrete_op_amp_filt_info bzone_explo_0 =
134 {
135 		BZ_R18 + BZ_R19, 0, 0, 0,       /* r1, r2, r3, r4 */
136 		BZ_R33,                         /* rF */
137 		BZ_C22, 0, 0,                   /* c1, c2, c3 */
138 		0,                              /* vRef - not used */
139 		22, 0                           /* vP, vN */
140 };
141 
142 static const discrete_op_amp_filt_info bzone_explo_1 =
143 {
144 		BZ_R18, 0, 0, 0,                /* r1, r2, r3, r4 */
145 		BZ_R33,                         /* rF */
146 		BZ_C22, 0, 0,                   /* c1, c2, c3 */
147 		0,                              /* vRef - not used */
148 		22, 0                           /* vP, vN */
149 };
150 
151 static const discrete_op_amp_filt_info bzone_shell_0 =
152 {
153 		BZ_R13 + BZ_R12, 0, 0, 0,       /* r1, r2, r3, r4 */
154 		BZ_R32,                         /* rF */
155 		BZ_C21, 0, 0,                   /* c1, c2, c3 */
156 		0,                              /* vRef - not used */
157 		22, 0                           /* vP, vN */
158 };
159 
160 static const discrete_op_amp_filt_info bzone_shell_1 =
161 {
162 		BZ_R13, 0, 0, 0,                /* r1, r2, r3, r4 */
163 		BZ_R32,                         /* rF */
164 		BZ_C21, 0, 0,                   /* c1, c2, c3 */
165 		0,                              /* vRef - not used */
166 		22, 0                           /* vP, vN */
167 };
168 #endif
169 
170 static const discrete_555_desc bzone_vco_desc =
171 {
172 	DISC_555_OUT_DC,
173 	5.0,
174 	DEFAULT_555_CHARGE,
175 	1.0 // Logic output
176 };
177 
178 static const discrete_mixer_desc bzone_eng_mixer_desc =
179 {
180 	DISC_MIXER_IS_RESISTOR,
181 	{BZ_R20, BZ_R21, BZ_R34, BZ_R35},
182 	{0, 0, 0, 0},
183 	{0, 0, 0, 0},
184 	0, 0,
185 	BZ_C29,
186 	0, /* no out cap */
187 	0, TTL_OUT      /* inputs are logic */
188 };
189 
190 static const discrete_mixer_desc bzone_final_mixer_desc =
191 {
192 	DISC_MIXER_IS_RESISTOR,
193 	{BZ_R25, BZ_R28, BZ_R26 + BZ_R20 / 4, BZ_R27},
194 	{0, 0, 0, 0},
195 	{0, 0, 0, 0},
196 	0, BZ_R29,
197 	0,
198 	BZ_C20, /* The speakers are driven by a +/- signal, just using the cap is good enough */
199 	0, 1
200 };
201 
202 
203 /************************************************************************
204  *
205  * Custom Battlezone filter
206  *
207  *         .------.         r2           c
208  *         |     O|-----+--ZZZZ--+-------||---------.
209  *         | 4066 |     |        |                  |
210  *  IN0 >--|c    I|-.   Z r1     |       r5         |
211  *         '------' |   Z        +------ZZZZ--------+
212  *                  |   Z        |                  |
213  *                 gnd  |        |           |\     |
214  *                     gnd       |           | \    |
215  *                               '-----------|- \   |
216  *            r3                             |   >--+----> Netlist Node
217  *  IN1 >----ZZZZ----------------+-----------|+ /
218  *                               |           | /
219  *                               Z r4        |/
220  *                               Z
221  *                               Z
222  *                               |           VP = B+
223  *                              gnd
224  *
225  ************************************************************************/
226 #define BZONE_CUSTOM_FILTER__IN0    DISCRETE_INPUT(0)
227 #define BZONE_CUSTOM_FILTER__IN1    DISCRETE_INPUT(1)
228 #define BZONE_CUSTOM_FILTER__R1     DISCRETE_INPUT(2)
229 #define BZONE_CUSTOM_FILTER__R2     DISCRETE_INPUT(3)
230 #define BZONE_CUSTOM_FILTER__R3     DISCRETE_INPUT(4)
231 #define BZONE_CUSTOM_FILTER__R4     DISCRETE_INPUT(5)
232 #define BZONE_CUSTOM_FILTER__R5     DISCRETE_INPUT(6)
233 #define BZONE_CUSTOM_FILTER__C      DISCRETE_INPUT(7)
234 #define BZONE_CUSTOM_FILTER__VP     DISCRETE_INPUT(8)
235 
236 #define CD4066_R_ON     270
237 
238 DISCRETE_CLASS_STEP_RESET(bzone_custom_filter, 1,
239 	double  m_v_in1_gain;
240 	double  m_v_p;
241 	double  m_exponent;
242 	double  m_gain[2];
243 	double  m_out_v;
244 );
245 
DISCRETE_STEP(bzone_custom_filter)246 DISCRETE_STEP(bzone_custom_filter)
247 {
248 	int     in0 = (BZONE_CUSTOM_FILTER__IN0 == 0) ? 0 : 1;
249 	double  v;
250 
251 	if (BZONE_CUSTOM_FILTER__IN1 > 0)
252 		v = 0;
253 
254 	v = BZONE_CUSTOM_FILTER__IN1 * m_v_in1_gain * m_gain[in0];
255 	if (v > m_v_p) v = m_v_p;
256 	if (v < 0) v = 0;
257 
258 	m_out_v += (v - m_out_v) * m_exponent;
259 	set_output(0, m_out_v);
260 }
261 
DISCRETE_RESET(bzone_custom_filter)262 DISCRETE_RESET(bzone_custom_filter)
263 {
264 	m_gain[0] = BZONE_CUSTOM_FILTER__R1 + BZONE_CUSTOM_FILTER__R2;
265 	m_gain[0] = BZONE_CUSTOM_FILTER__R5 / m_gain[0] + 1;
266 	m_gain[1] = RES_2_PARALLEL(CD4066_R_ON, BZONE_CUSTOM_FILTER__R1) + BZONE_CUSTOM_FILTER__R2;
267 	m_gain[1] = BZONE_CUSTOM_FILTER__R5 / m_gain[1] + 1;
268 	m_v_in1_gain = RES_VOLTAGE_DIVIDER(BZONE_CUSTOM_FILTER__R3, BZONE_CUSTOM_FILTER__R4);
269 	m_v_p = BZONE_CUSTOM_FILTER__VP - OP_AMP_VP_RAIL_OFFSET;
270 	m_exponent = RC_CHARGE_EXP(BZONE_CUSTOM_FILTER__R5 * BZONE_CUSTOM_FILTER__C);
271 	m_out_v = 0.0;
272 }
273 
274 /*************************************
275  *
276  *  Discrete Sound Blocks
277  *
278  *************************************/
279 
280 static DISCRETE_SOUND_START(bzone_discrete)
281 
282 	/************************************************/
283 	/* Input register mapping for Battlezone        */
284 	/************************************************/
DISCRETE_INPUT_DATA(BZ_INPUT)285 	DISCRETE_INPUT_DATA(BZ_INPUT)
286 	/* decode the bits */
287 	DISCRETE_BITS_DECODE(NODE_10, BZ_INPUT, 0, 7, 1)             /* IC M2, bits 0 - 7 */
288 
289 	/* the pot is 250K, but we will use a smaller range to get a better adjustment range */
290 	DISCRETE_ADJUSTMENT(BZ_R11_POT, RES_K(75), RES_K(10), DISC_LINADJ, "R11")
291 
292 
293 	/************************************************/
294 	/* NOISE                                        */
295 	/************************************************/
296 
297 	/* 12Khz clock is divided by two by B4 74LS109 */
298 	DISCRETE_LFSR_NOISE(BZ_NOISE,                               /* IC H4, pin 13 */
299 		1, 1, BZ_NOISE_CLOCK / 2, 1.0, 0, 0.5, &bzone_lfsr)
300 
301 	/* divide by 2 */
302 	DISCRETE_COUNTER(NODE_31,                                   /* IC J5, pin 8 */
303 		1, 0, BZ_NOISE, 0, 1, DISC_COUNT_UP, 0, DISC_CLK_ON_R_EDGE)
304 
305 	DISCRETE_BITS_DECODE(NODE_32, NODE_SUB(BZ_NOISE, 1), 11, 14, 1)     /* IC H4, pins 6, 10, 11, 12 */
306 	DISCRETE_LOGIC_NAND4(NODE_33,                               /* IC J4, pin 8 */
307 		NODE_32_00, NODE_32_01, NODE_32_02, NODE_32_03)         /* LSFR bits 11-14 */
308 	/* divide by 2 */
309 	DISCRETE_COUNTER(NODE_34,                                   /* IC J5, pin 6 */
310 		1, 0, NODE_33, 0, 1, DISC_COUNT_UP, 0, DISC_CLK_ON_R_EDGE)
311 
312 	/************************************************/
313 	/* Shell                                        */
314 	/************************************************/
315 	DISCRETE_RC_CIRCUIT_1(NODE_40,                  /* IC J3, pin 9 */
316 		BZ_INP_SHELL, NODE_31,                      /* INP0, INP1 */
317 		BZ_R14 + BZ_R15, BZ_C9)
318 	DISCRETE_CUSTOM9(BZ_SHELL_SND, bzone_custom_filter,                 /* IC K5, pin 1 */
319 		BZ_INP_EXPLOLS, NODE_40,                    /* IN0, IN1 */
320 		BZ_R12, BZ_R13, BZ_R14, BZ_R15, BZ_R32,
321 		BZ_C21,
322 		22,                                         /* B+ of op-amp */
323 		nullptr)
324 
325 	/************************************************/
326 	/* Explosion                                    */
327 	/************************************************/
328 
329 	DISCRETE_RC_CIRCUIT_1(NODE_50,                  /* IC J3, pin 3 */
330 		BZ_INP_EXPLO, NODE_34,                      /* INP0, INP1 */
331 		BZ_R17 + BZ_R16, BZ_C14)
332 	DISCRETE_CUSTOM9(BZ_EXPLOSION_SND, bzone_custom_filter,             /* IC K5, pin 1 */
333 		BZ_INP_EXPLOLS, NODE_50,                    /* IN0, IN1 */
334 		BZ_R19, BZ_R18, BZ_R17, BZ_R16, BZ_R33,
335 		BZ_C22,
336 		22,                                         /* B+ of op-amp */
337 		nullptr)
338 	/************************************************/
339 	/* Engine                                       */
340 	/************************************************/
341 
342 	DISCRETE_SWITCH(NODE_61,                                /* effect of IC L4, pin 2 */
343 		1, BZ_INP_ENGREV,                                   /* ENAB, SWITCH */
344 		5.0 * RES_VOLTAGE_DIVIDER(BZ_R7, BZ_R6),            /* INP0 */
345 		5.0 * RES_VOLTAGE_DIVIDER(BZ_R7, RES_2_PARALLEL(CD4066_R_ON + BZ_R5, BZ_R6)))   /* INP1 */
346 	/* R5, R6, R7 all affect the following circuit charge discharge rates */
347 	/* they are not emulated as their effect is less than the 5% component tolerance */
348 	DISCRETE_RCDISC3(NODE_62,                               /* IC K5, pin 7 */
349 		1, NODE_61, BZ_R8, BZ_R9, BZ_C13, -0.5)
350 
351 	DISCRETE_555_ASTABLE_CV(NODE_63,                        /* IC F3, pin 3 */
352 		1,                                                  /* RESET */
353 		BZ_R10, BZ_R11_POT, BZ_C11,
354 		NODE_62,                                            /* CV - IC F3, pin 5 */
355 		&bzone_vco_desc)
356 
357 	DISCRETE_LOGIC_INVERT(NODE_64, BZ_INP_MOTEN)
358 	DISCRETE_COUNTER(NODE_65,                               /* IC F4 */
359 		1, NODE_64, NODE_63,                                /* ENAB, RESET, CLK */
360 		4, 15, DISC_COUNT_UP, 0, DISC_CLK_ON_R_EDGE)        /* MIN, MAX, DIR, INIT, CLKTYPE */
361 	DISCRETE_TRANSFORM2(NODE_66, NODE_65, 7, "01>")         /* QD - IC F4, pin 11 */
362 	DISCRETE_TRANSFORM2(NODE_67, NODE_65, 15, "01=")        /* Ripple - IC F4, pin 15 */
363 
364 	DISCRETE_COUNTER(NODE_68,                               /* IC F5 */
365 		1, NODE_64, NODE_63,                                /* ENAB, RESET, CLK */
366 		6, 15, DISC_COUNT_UP, 0, DISC_CLK_ON_R_EDGE)        /* MIN, MAX, DIR, INIT, CLKTYPE */
367 	DISCRETE_TRANSFORM2(NODE_69, NODE_68, 7, "01>")         /* QD - IC F5, pin 11 */
368 	DISCRETE_TRANSFORM2(NODE_70, NODE_68, 15, "01=")        /* Ripple - IC F5, pin 15 */
369 
370 	DISCRETE_MIXER4(BZ_ENGINE_SND, 1, NODE_66, NODE_67, NODE_69, NODE_70, &bzone_eng_mixer_desc)
371 
372 	/************************************************/
373 	/* FINAL MIX                                    */
374 	/************************************************/
375 	/* We won't bother emulating the final gain of op-amp IC K5, pin 14.
376 	 * This signal never reaches a value where it clips, so we will
377 	 * just output the final 16-bit level.
378 	 */
379 
380 	/* Convert Pokey output to 5V Signal */
381 	DISCRETE_INPUTX_STREAM(BZ_POKEY_SND, 0, 5.0 / 32768, 0)
382 
383 	DISCRETE_MIXER4(NODE_280,
384 		BZ_INP_SOUNDEN,
385 		BZ_SHELL_SND, BZ_EXPLOSION_SND, BZ_ENGINE_SND, BZ_POKEY_SND,
386 		&bzone_final_mixer_desc)
387 	DISCRETE_OUTPUT(NODE_280, 48000)
388 
389 DISCRETE_SOUND_END
390 
391 void bzone_state::bzone_sounds_w(uint8_t data)
392 {
393 	m_discrete->write(BZ_INPUT, data);
394 
395 	m_startled = BIT(data, 6);
396 	machine().sound().system_enable(data & 0x20);
397 }
398 
bzone_audio(machine_config & config)399 void bzone_state::bzone_audio(machine_config &config)
400 {
401 	SPEAKER(config, "mono").front_center();
402 
403 	pokey_device &pokey(POKEY(config, "pokey", BZONE_MASTER_CLOCK / 8));
404 	pokey.allpot_r().set_ioport("IN3");
405 	pokey.set_output_rc(RES_K(10), CAP_U(0.015), 5.0);
406 	pokey.add_route(0, "discrete", 1.0, 0);
407 
408 	DISCRETE(config, "discrete", bzone_discrete).add_route(ALL_OUTPUTS, "mono", 1.0);
409 }
410