summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/machine/8364_paula.cpp
blob: a06eed90ca4a8f2cdc37e6563d4e68a82523ab34 (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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// license: BSD-3-Clause
// copyright-holders: Aaron Giles, Dirk Best
/******************************************************************************

    MOS Technology 8364 "Paula"

    TODO:
    - Inherit FDC, serial and irq controller to here;
    - Move Agnus "location" logic from here;
    - low-pass filter;
    - convert volume values to non-linear dB scale (cfr. )
    - Verify ADKCON modulation;
    - Verify manual mode:
      \- AGA roadkill during gameplay, which also has very long period setups,
         extremely aliased;
    - When a DMA stop occurs, is the correlated channel playback stopped
      at the end of the current cycle or as soon as possible like current
      implementation?

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

#include "emu.h"
#include "8364_paula.h"

#define LIVE_AUDIO_VIEW 0

//#define VERBOSE 1
#include "logmacro.h"
#include <cstring>


//**************************************************************************
//  DEVICE DEFINITIONS
//**************************************************************************

DEFINE_DEVICE_TYPE(PAULA_8364, paula_8364_device, "paula_8364", "MOS 8364 \"Paula\"")


//*************************************************************************
//  LIVE DEVICE
//**************************************************************************

//-------------------------------------------------
//  paula_8364_device - constructor
//-------------------------------------------------

paula_8364_device::paula_8364_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: device_t(mconfig, PAULA_8364, tag, owner, clock)
	, device_sound_interface(mconfig, *this)
	, m_chipmem_r(*this, 0)
	, m_int_w(*this)
	, m_stream(nullptr)
{
}

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

void paula_8364_device::device_start()
{
	// initialize channels
	for (int i = 0; i < 4; i++)
	{
		m_channel[i].index = i;
		m_channel[i].irq_timer = timer_alloc(FUNC(paula_8364_device::signal_irq), this);
	}

	// create the stream
	m_stream = stream_alloc(0, 4, clock() / CLOCK_DIVIDER);
}

void paula_8364_device::device_reset()
{
	m_dma_master_enable = false;
	for (auto &chan : m_channel)
	{
		chan.loc = 0;
		chan.len = 0;
		chan.per = 0;
		chan.vol = 0;
		chan.curticks = 0;
		chan.manualmode = false;
		chan.curlocation = 0;
		chan.curlength = 0;
		chan.dma_enabled = false;
		chan.atper = false;
		chan.atvol = false;
	}
}

//-------------------------------------------------
//  update - stream updater
//-------------------------------------------------

void paula_8364_device::update()
{
	m_stream->update();
}


//*************************************************************************
//  IMPLEMENTATION
//**************************************************************************

template <u8 ch> void paula_8364_device::audio_channel_map(address_map &map)
{
	// TODO: location addresses belongs to Agnus
	map(0x00, 0x01).w(FUNC(paula_8364_device::audxlch_w<ch>));
	map(0x02, 0x03).w(FUNC(paula_8364_device::audxlcl_w<ch>));
	map(0x04, 0x05).w(FUNC(paula_8364_device::audxlen_w<ch>));
	map(0x06, 0x07).w(FUNC(paula_8364_device::audxper_w<ch>));
	map(0x08, 0x09).w(FUNC(paula_8364_device::audxvol_w<ch>));
	map(0x0a, 0x0b).w(FUNC(paula_8364_device::audxdat_w<ch>));
}

// Instantiate channel maps
template void paula_8364_device::audio_channel_map<0>(address_map &map);
template void paula_8364_device::audio_channel_map<1>(address_map &map);
template void paula_8364_device::audio_channel_map<2>(address_map &map);
template void paula_8364_device::audio_channel_map<3>(address_map &map);

template <u8 ch> void paula_8364_device::audxlch_w(u16 data)
{
	m_stream->update();
	// TODO: chipmem mask
	m_channel[ch].loc = (m_channel[ch].loc & 0x0000ffff) | ((data & 0x001f) << 16);
}

template <u8 ch> void paula_8364_device::audxlcl_w(u16 data)
{
	m_stream->update();
	m_channel[ch].loc = (m_channel[ch].loc & 0xffff0000) | ((data & 0xfffe) <<  0);
}

template <u8 ch> void paula_8364_device::audxlen_w(u16 data)
{
	m_stream->update();
	m_channel[ch].len = data;
}

template <u8 ch> void paula_8364_device::audxper_w(u16 data)
{
	m_stream->update();
	m_channel[ch].per = data;
}

template <u8 ch> void paula_8364_device::audxvol_w(u16 data)
{
	m_stream->update();
	m_channel[ch].vol = data & 0x7f;
}

template <u8 ch> void paula_8364_device::audxdat_w(u16 data)
{
	m_stream->update();
	m_channel[ch].dat = data;
	m_channel[ch].manualmode = true;
}

void paula_8364_device::dmacon_set(u16 data)
{
	m_stream->update();

	m_dma_master_enable = bool(BIT(data, 9));

	// update the DMA latches on each channel and reload if fresh
	// This holds true particularly for Ocean games (bchvolly, lostpatr, pang) and waylildr:
	// they sets a DMA length for a channel then enable DMA finally resets that length to 1
	// after a short delay loop.
	for (int channum = 0; channum < 4; channum++)
	{
		audio_channel *chan = &m_channel[channum];
		if (!chan->dma_enabled && ((data >> channum) & 1))
			dma_reload(chan, true);

		chan->dma_enabled = bool(BIT(data, channum));
	}
}

void paula_8364_device::adkcon_set(u16 data)
{
	m_stream->update();

	for (int channum = 0; channum < 4; channum++)
	{
		audio_channel *chan = &m_channel[channum];

		chan->atper = bool(BIT(data, channum + 4));
		chan->atvol = bool(BIT(data, channum));
	}
}

//-------------------------------------------------
//  signal_irq - irq signaling
//-------------------------------------------------

TIMER_CALLBACK_MEMBER( paula_8364_device::signal_irq )
{
	m_int_w(param, 1);
}

//-------------------------------------------------
//  dma_reload
//-------------------------------------------------

void paula_8364_device::dma_reload(audio_channel *chan, bool startup)
{
	chan->curlocation = chan->loc;
	// TODO: Unconfirmed, assume max size if length is 0.
	// cfr. wrestlmn and videokid, where they pratically never get even close to buffer exhaustion.
	chan->curlength = chan->len == 0 ? 0x10000 : chan->len;
	// TODO: on startup=false irq should be delayed two cycles
	if (startup)
		chan->irq_timer->adjust(attotime::from_hz(15750), chan->index); // clock() / 227
	else
		signal_irq(chan->index);

	LOG("dma_reload(%d): offs=%06X len=%04X\n", chan->index, chan->curlocation, chan->curlength);
}

std::string paula_8364_device::print_audio_state()
{
	std::ostringstream outbuffer;

	util::stream_format(outbuffer, "DMA master %d\n", m_dma_master_enable);
	for (auto &chan : m_channel)
	{
		util::stream_format(outbuffer, "%d DMA (%d) ADK (%d%d) REGS: %06x %04x %03x %02x %d LIVE: %06x %04x %d\n"
			, chan.index
			, chan.dma_enabled
			, chan.atper
			, chan.atvol
			, chan.loc
			, chan.len
			, chan.per
			, chan.vol
			, chan.manualmode
			, chan.curlocation
			, chan.curlength
			, chan.dma_enabled
		);
	}

	return outbuffer.str();
}

//-------------------------------------------------
//  sound_stream_update - handle a stream update
//-------------------------------------------------

void paula_8364_device::sound_stream_update(sound_stream &stream, std::vector<read_stream_view> const &inputs, std::vector<write_stream_view> &outputs)
{
	int channum, sampoffs = 0;

	// if all DMA off, disable all channels
	if (m_dma_master_enable == false)
	{
		m_channel[0].dma_enabled =
		m_channel[1].dma_enabled =
		m_channel[2].dma_enabled =
		m_channel[3].dma_enabled = false;

		// clear the sample data to 0
		for (channum = 0; channum < 4; channum++)
			outputs[channum].fill(0);
		return;
	}

	int samples = outputs[0].samples() * CLOCK_DIVIDER;

	if (LIVE_AUDIO_VIEW)
		popmessage(print_audio_state());

	// loop until done
	while (samples > 0)
	{
		int nextper, nextvol;
		int ticks = samples;

		// determine the number of ticks we can do in this chunk
		if (ticks > m_channel[0].curticks)
			ticks = m_channel[0].curticks;
		if (ticks > m_channel[1].curticks)
			ticks = m_channel[1].curticks;
		if (ticks > m_channel[2].curticks)
			ticks = m_channel[2].curticks;
		if (ticks > m_channel[3].curticks)
			ticks = m_channel[3].curticks;

		// loop over channels
		nextper = nextvol = -1;
		for (channum = 0; channum < 4; channum++)
		{
			audio_channel *chan = &m_channel[channum];
			int volume = (nextvol == -1) ? chan->vol : nextvol;
			int period = (nextper == -1) ? chan->per : nextper;
			s32 sample;
			int i;

			// normalize the volume value
			// FIXME: definitely not linear
			volume = (volume & 0x40) ? 64 : (volume & 0x3f);
			volume *= 4;

			// are we modulating the period of the next channel?
			if (chan->atper)
			{
				nextper = chan->dat;
				nextvol = -1;
				sample = 0;
			}

			// are we modulating the volume of the next channel?
			else if (chan->atvol)
			{
				nextper = -1;
				nextvol = chan->dat;
				sample = 0;
			}

			// otherwise, we are generating data
			else
			{
				nextper = nextvol = -1;
				sample = chan->latched * volume;
			}

			// fill the buffer with the sample
			for (i = 0; i < ticks; i += CLOCK_DIVIDER)
				outputs[channum].put_int((sampoffs + i) / CLOCK_DIVIDER, sample, 32768);

			// account for the ticks; if we hit 0, advance
			chan->curticks -= ticks;
			if (chan->curticks == 0)
			{
				// reset the clock and ensure we're above the minimum ticks
				chan->curticks = period;
				if (chan->curticks < 124)
					chan->curticks = 124;

				// move forward one byte; if we move to an even byte, fetch new
				if (chan->dma_enabled || chan->manualmode)
					chan->curlocation++;
				if (chan->dma_enabled && !(chan->curlocation & 1))
				{
					chan->dat = m_chipmem_r(chan->curlocation);

					if (chan->curlength != 0)
						chan->curlength--;

					// if we run out of data, reload the dma and signal an IRQ,
					// gpmaster/asparmgp definitely expects this
					// (uses channel 3 as a sequencer, changing the start address on the fly)
					if (chan->curlength == 0)
					{
						dma_reload(chan, false);
						// reload the data pointer, otherwise aliasing / buzzing outside the given buffer will be heard
						// For example: Xenon 2 sets up location=0x63298 length=0x20
						// for silencing channels on-the-fly without relying on irqs.
						// Without this the location will read at 0x632d8 (data=0x7a7d), causing annoying buzzing.
						chan->dat = m_chipmem_r(chan->curlocation);
					}
				}

				// latch the next byte of the sample
				if (!(chan->curlocation & 1))
					chan->latched = chan->dat >> 8;
				else
					chan->latched = chan->dat >> 0;

				// if we're in manual mode, signal an interrupt once we latch the low byte
				if (!chan->dma_enabled && chan->manualmode && (chan->curlocation & 1))
				{
					signal_irq(channum);
					chan->manualmode = false;
				}
			}
		}

		// bump ourselves forward by the number of ticks
		sampoffs += ticks;
		samples -= ticks;
	}
}