summaryrefslogtreecommitdiffstats
path: root/src/devices/bus/a2bus/a2mcms.cpp
blob: 1442d7d68bc30d25f32a794173402b332ca4de23 (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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// license:BSD-3-Clause
// copyright-holders:R. Belmont
/*********************************************************************

    a2mcms.c

    Implementation of the Mountain Computer Music System.
    This was sold standalone and also used as part of the alphaSyntauri
    and SoundChaser systems.

*********************************************************************/

#include "emu.h"
#include "a2mcms.h"
#include "speaker.h"

// the actual sound device (a slot device can't currently also be a sound device so we keep this private here)
enum
{
	CTRL_IRQS = 0,
	CTRL_DMA,
	CTRL_MASTERVOL
};


DEFINE_DEVICE_TYPE(MCMS, mcms_device, "mcmseng", "Mountain Computer Music System engine")

/***************************************************************************
    PARAMETERS
***************************************************************************/

//**************************************************************************
//  GLOBAL VARIABLES
//**************************************************************************

DEFINE_DEVICE_TYPE(A2BUS_MCMS1, a2bus_mcms1_device, "a2mcms1", "Mountain Computer Music System (card 1)")
DEFINE_DEVICE_TYPE(A2BUS_MCMS2, a2bus_mcms2_device, "a2mcms2", "Mountain Computer Music System (card 2)")

#define ENGINE_TAG  "engine"

#define MCFG_MCMS_IRQ_CALLBACK(_cb) \
	downcast<mcms_device &>(*device).set_irq_cb(DEVCB_##_cb);

/***************************************************************************
    FUNCTION PROTOTYPES
***************************************************************************/

//-------------------------------------------------
//  device_add_mconfig - add device configuration
//-------------------------------------------------

MACHINE_CONFIG_START(a2bus_mcms1_device::device_add_mconfig)
	SPEAKER(config, "mcms_l").front_left();
	SPEAKER(config, "mcms_r").front_right();

	MCFG_DEVICE_ADD(ENGINE_TAG, MCMS, 1000000)
	MCFG_MCMS_IRQ_CALLBACK(WRITELINE(*this, a2bus_mcms1_device, irq_w))

	MCFG_SOUND_ROUTE(0, "mcms_l", 1.0)
	MCFG_SOUND_ROUTE(1, "mcms_r", 1.0)
MACHINE_CONFIG_END

//**************************************************************************
//  LIVE DEVICE - Card 1
//**************************************************************************

a2bus_mcms1_device::a2bus_mcms1_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, uint32_t clock) :
	device_t(mconfig, type, tag, owner, clock),
	device_a2bus_card_interface(mconfig, *this),
	m_mcms(*this, ENGINE_TAG)
{
}

a2bus_mcms1_device::a2bus_mcms1_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock) :
	a2bus_mcms1_device(mconfig, A2BUS_MCMS1, tag, owner, clock)
{
}

//-------------------------------------------------
//  device_start - device-specific startup
//-------------------------------------------------

void a2bus_mcms1_device::device_start()
{
}

void a2bus_mcms1_device::device_reset()
{
	m_mcms->set_bus_device(this);
}

// read once at c0n0 to disable 125 Hz IRQs
// read once at c0n1 to enable 125 Hz IRQs
uint8_t a2bus_mcms1_device::read_c0nx(uint8_t offset)
{
	if (offset == 0)
	{
		m_mcms->control_w(CTRL_IRQS, 0);
	}
	else if (offset == 1)
	{
		m_mcms->control_w(CTRL_IRQS, 1);
	}

	return 0xff;
}

// read at Cn00: light gun in bit 7, bits 0-5 = 'random' number
uint8_t a2bus_mcms1_device::read_cnxx(uint8_t offset)
{
	return m_mcms->get_pen_rand();
}

// write 0-255 to Cn00 to set the master volume
void a2bus_mcms1_device::write_cnxx(uint8_t offset, uint8_t data)
{
	if (offset == 0)
	{
		m_mcms->control_w(CTRL_MASTERVOL, data);
	}
}

mcms_device *a2bus_mcms1_device::get_engine(void)
{
	return m_mcms;
}

WRITE_LINE_MEMBER(a2bus_mcms1_device::irq_w)
{
	if (state == ASSERT_LINE)
	{
		raise_slot_irq();
	}
	else
	{
		lower_slot_irq();
	}
}

//**************************************************************************
//  LIVE DEVICE - Card 2
//**************************************************************************

a2bus_mcms2_device::a2bus_mcms2_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, uint32_t clock) :
	device_t(mconfig, type, tag, owner, clock),
	device_a2bus_card_interface(mconfig, *this), m_card1(nullptr), m_engine(nullptr)
{
}

a2bus_mcms2_device::a2bus_mcms2_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock) :
	a2bus_mcms2_device(mconfig, A2BUS_MCMS2, tag, owner, clock)
{
}

//-------------------------------------------------
//  device_start - device-specific startup
//-------------------------------------------------

void a2bus_mcms2_device::device_start()
{
	if (slotno() < 2)
		fatalerror("MCMS: Card 2 must be in slot 2 or greater\n");
}

void a2bus_mcms2_device::device_reset()
{
	m_card1 = downcast<a2bus_mcms1_device *>(a2bus().m_device_list[slotno()-1]);
	m_engine = m_card1->get_engine();
}

// here to soak up false reads from indexed accesses
uint8_t a2bus_mcms2_device::read_c0nx(uint8_t offset)
{
	return 0xff;
}

// write once to c0n0 to disable the card (reset also disables)
// write twice to c0n1 to enable the card (value doesn't matter)
void a2bus_mcms2_device::write_c0nx(uint8_t offset, uint8_t data)
{
	if (offset == 0)
	{
		m_engine->control_w(CTRL_DMA, 0);
	}
	else if (offset == 1)
	{
		m_engine->control_w(CTRL_DMA, 1);
	}
}

void a2bus_mcms2_device::write_cnxx(uint8_t offset, uint8_t data)
{
	m_engine->voiceregs_w(offset, data);
}


/*
    Sound device implementation
*/

mcms_device::mcms_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: device_t(mconfig, MCMS, tag, owner, clock),
	device_sound_interface(mconfig, *this),
	m_write_irq(*this), m_stream(nullptr), m_timer(nullptr), m_clrtimer(nullptr), m_pBusDevice(nullptr), m_enabled(false), m_mastervol(0), m_rand(0)
{
}

void mcms_device::device_start()
{
	m_write_irq.resolve();
	m_stream = machine().sound().stream_alloc(*this, 0, 2, 31250);
	m_timer = timer_alloc(0, nullptr);
	m_clrtimer = timer_alloc(1, nullptr);
	m_enabled = false;
	memset(m_vols, 0, sizeof(m_vols));
	memset(m_table, 0, sizeof(m_table));
	memset(m_freq, 0, sizeof(m_freq));
	memset(m_acc, 0, sizeof(m_acc));

	// the card detect programs volumes and wavetable page but not freq and expects the accumulator to increment
	for (auto & elem : m_freq)
	{
		elem = 0x0040;
	}

	save_item(NAME(m_enabled));
	save_item(NAME(m_vols));
	save_item(NAME(m_table));
	save_item(NAME(m_freq));
	save_item(NAME(m_acc));
	save_item(NAME(m_mastervol));
	save_item(NAME(m_rand));
}

void mcms_device::device_reset()
{
	m_write_irq(CLEAR_LINE);
	m_timer->adjust(attotime::never);
	m_clrtimer->adjust(attotime::never);
	m_enabled = false;
}

void mcms_device::device_timer(emu_timer &timer, device_timer_id tid, int param, void *ptr)
{
	if (tid == 0)
	{
		m_write_irq(ASSERT_LINE);
		// clear this IRQ in 10 cycles (?)
		m_clrtimer->adjust(attotime::from_usec(10), 0);
	}
	else if (tid == 1)
	{
		m_write_irq(CLEAR_LINE);
	}
}

void mcms_device::sound_stream_update(sound_stream &stream, stream_sample_t **inputs, stream_sample_t **outputs, int samples)
{
	stream_sample_t *outL, *outR;
	int i, v;
	uint16_t wptr;
	int8_t sample;
	int32_t mixL, mixR;

	outL = outputs[1];
	outR = outputs[0];

	if (m_enabled)
	{
		for (i = 0; i < samples; i++)
		{
			mixL = mixR = 0;

			for (v = 0; v < 16; v++)
			{
				m_acc[v] += m_freq[v];
				wptr = (m_table[v]<<8) | (m_acc[v]>>8);
				m_rand = (m_acc[v]>>8) & 0x1f;

				sample = (m_pBusDevice->slot_dma_read_no_space(wptr) ^ 0x80);
				if (v & 1)
				{
					mixL += sample * m_vols[v];
				}
				else
				{
					mixR += sample * m_vols[v];
				}
			}

			outL[i] = (mixL * m_mastervol)>>9;
			outR[i] = (mixR * m_mastervol)>>9;
		}
	}
	else
	{
		for (i = 0; i < samples; i++)
		{
			outL[i] = outR[i] = 0;
		}
	}
}

void mcms_device::voiceregs_w(offs_t offset, uint8_t data)
{
	m_stream->update();
	if (offset >= 0x20)
	{
		if (offset & 1) // amp
		{
			m_vols[(offset-0x21)/2] = data;
		}
		else    // wavetable page
		{
			m_table[(offset-0x20)/2] = data;
		}
	}
	else
	{
		if (offset & 1) // freq L
		{
			if (offset == 0x1f)
			{
				m_freq[0] &= 0xff00;
				m_freq[0] |= data;
			}
			else
			{
				int reg = (offset/2)+1;
				m_freq[reg] &= 0xff00;
				m_freq[reg] |= data;
			}
		}
		else    // freq H
		{
			int reg = (offset/2);
			m_freq[reg] &= 0x00ff;
			m_freq[reg] |= (data<<8);
		}
	}
}

void mcms_device::control_w(offs_t offset, uint8_t data)
{
	m_stream->update();

	switch (offset)
	{
		case CTRL_IRQS:
			if (data == 0)
			{
				m_timer->adjust(attotime::never);
			}
			else
			{
				m_timer->adjust(attotime::zero, 0, attotime::from_hz(125));
			}
			break;

		case CTRL_DMA:
			m_enabled = (data == 0) ? false : true;
			break;

		case CTRL_MASTERVOL:
			m_mastervol = data;
			break;
	}
}