summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/sound/k054539.cpp
blob: 42bc943bee3bea2036c9750b9f5b77ad1f4960b0 (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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
// license:BSD-3-Clause
// copyright-holders:Aaron Giles
/*********************************************************

    Konami 054539 (TOP) PCM Sound Chip

    A lot of information comes from Amuse.
    Big thanks to them.

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

#include "emu.h"
#include "k054539.h"

//#define VERBOSE 1
#include "logmacro.h"


DEFINE_DEVICE_TYPE(K054539, k054539_device, "k054539", "K054539 ADPCM")

k054539_device::k054539_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: device_t(mconfig, K054539, tag, owner, clock)
	, device_sound_interface(mconfig, *this)
	, device_rom_interface(mconfig, *this, 24)
	, flags(0)
	, ram(nullptr)
	, reverb_pos(0)
	, cur_ptr(0)
	, cur_limit(0)
	, rom_addr(0)
	, stream(nullptr)
	, m_timer(nullptr)
	, m_timer_state(0)
	, m_timer_handler(*this)
	, m_apan_cb(*this)
{
}


/* Registers:
   00..ff: 20 bytes/channel, 8 channels
     00..02: pitch (lsb, mid, msb)
         03: volume (0=max, 0x40=-36dB)
         04: reverb volume (idem)
     05: pan (1-f right, 10 middle, 11-1f left)
     06..07: reverb delay (0=max, current computation non-trusted)
     08..0a: loop (lsb, mid, msb)
     0c..0e: start (lsb, mid, msb) (and current position ?)

   100.1ff: effects?
     13f: pan of the analog input (1-1f)

   200..20f: 2 bytes/channel, 8 channels
     00: type (b2-3), reverse (b5)
     01: loop (b0)

   214: Key on (b0-7 = channel 0-7)
   215: Key off          ""
   225: ?
   227: Timer frequency
   228: ?
   229: ?
   22a: ?
   22b: ?
   22c: Channel active? (b0-7 = channel 0-7)
   22d: Data read/write port
   22e: ROM/RAM select (00..7f == ROM banks, 80 = Reverb RAM)
   22f: Global control:
        .......x - Enable PCM
        ......x. - Timer related?
        ...x.... - Enable ROM/RAM readback from 0x22d
        ..x..... - Timer output enable?
        x....... - Disable register RAM updates

    The chip has an optional 0x8000 byte reverb buffer.
    The reverb delay is actually an offset in this buffer.
*/

void k054539_device::init_flags(int _flags)
{
	flags = _flags;
}

void k054539_device::set_gain(int channel, double _gain)
{
	if(_gain >= 0)
		gain[channel] = _gain;
}
//*

bool k054539_device::regupdate()
{
	return !(regs[0x22f] & 0x80);
}

void k054539_device::keyon(int channel)
{
	if(regupdate())
		regs[0x22c] |= 1 << channel;
}

void k054539_device::keyoff(int channel)
{
	if(regupdate())
		regs[0x22c] &= ~(1 << channel);
}

void k054539_device::sound_stream_update(sound_stream &stream, stream_sample_t **inputs, stream_sample_t **outputs, int samples)
{
#define VOL_CAP 1.80

	static const int16_t dpcm[16] = {
		0 * 0x100,     1 * 0x100,   4 * 0x100,   9 * 0x100,  16 * 0x100, 25 * 0x100, 36 * 0x100, 49 * 0x100,
		-64 * 0x100, -49 * 0x100, -36 * 0x100, -25 * 0x100, -16 * 0x100, -9 * 0x100, -4 * 0x100, -1 * 0x100
	};


	int16_t *rbase = (int16_t *)ram.get();

	if(!(regs[0x22f] & 1))
		return;

	for(int sample = 0; sample != samples; sample++) {
		double lval, rval;
		if(!(flags & DISABLE_REVERB))
			lval = rval = rbase[reverb_pos];
		else
			lval = rval = 0;
		rbase[reverb_pos] = 0;

		for(int ch=0; ch<8; ch++)
			if(regs[0x22c] & (1<<ch)) {
				unsigned char *base1 = regs + 0x20*ch;
				unsigned char *base2 = regs + 0x200 + 0x2*ch;
				channel *chan = channels + ch;

				int delta = base1[0x00] | (base1[0x01] << 8) | (base1[0x02] << 16);

				int vol = base1[0x03];

				int bval = vol + base1[0x04];
				if (bval > 255)
					bval = 255;

				int pan = base1[0x05];
				// DJ Main: 81-87 right, 88 middle, 89-8f left
				if (pan >= 0x81 && pan <= 0x8f)
					pan -= 0x81;
				else if (pan >= 0x11 && pan <= 0x1f)
					pan -= 0x11;
				else
					pan = 0x18 - 0x11;

				double cur_gain = gain[ch];

				double lvol = voltab[vol] * pantab[pan] * cur_gain;
				if (lvol > VOL_CAP)
					lvol = VOL_CAP;

				double rvol = voltab[vol] * pantab[0xe - pan] * cur_gain;
				if (rvol > VOL_CAP)
					rvol = VOL_CAP;

				double rbvol= voltab[bval] * cur_gain / 2;
				if (rbvol > VOL_CAP)
					rbvol = VOL_CAP;

				int rdelta = (base1[6] | (base1[7] << 8)) >> 3;
				rdelta = (rdelta + reverb_pos) & 0x3fff;

				int cur_pos = (base1[0x0c] | (base1[0x0d] << 8) | (base1[0x0e] << 16));

				int fdelta, pdelta;
				if(base2[0] & 0x20) {
					delta = -delta;
					fdelta = +0x10000;
					pdelta = -1;
				} else {
					fdelta = -0x10000;
					pdelta = +1;
				}

				int cur_pfrac, cur_val, cur_pval;
				if(cur_pos != chan->pos) {
					chan->pos = cur_pos;
					cur_pfrac = 0;
					cur_val = 0;
					cur_pval = 0;
				} else {
					cur_pfrac = chan->pfrac;
					cur_val = chan->val;
					cur_pval = chan->pval;
				}

				switch(base2[0] & 0xc) {
				case 0x0: { // 8bit pcm
					cur_pfrac += delta;
					while(cur_pfrac & ~0xffff) {
						cur_pfrac += fdelta;
						cur_pos += pdelta;

						cur_pval = cur_val;
						cur_val = (int16_t)(read_byte(cur_pos) << 8);
						if(cur_val == (int16_t)0x8000 && (base2[1] & 1)) {
							cur_pos = (base1[0x08] | (base1[0x09] << 8) | (base1[0x0a] << 16));
							cur_val = (int16_t)(read_byte(cur_pos) << 8);
						}
						if(cur_val == (int16_t)0x8000) {
							keyoff(ch);
							cur_val = 0;
							break;
						}
					}
					break;
				}

				case 0x4: { // 16bit pcm lsb first
					pdelta <<= 1;

					cur_pfrac += delta;
					while(cur_pfrac & ~0xffff) {
						cur_pfrac += fdelta;
						cur_pos += pdelta;

						cur_pval = cur_val;
						cur_val = (int16_t)(read_byte(cur_pos) | read_byte(cur_pos+1)<<8);
						if(cur_val == (int16_t)0x8000 && (base2[1] & 1)) {
							cur_pos = (base1[0x08] | (base1[0x09] << 8) | (base1[0x0a] << 16));
							cur_val = (int16_t)(read_byte(cur_pos) | read_byte(cur_pos+1)<<8);
						}
						if(cur_val == (int16_t)0x8000) {
							keyoff(ch);
							cur_val = 0;
							break;
						}
					}
					break;
				}

				case 0x8: { // 4bit dpcm
					cur_pos <<= 1;
					cur_pfrac <<= 1;
					if(cur_pfrac & 0x10000) {
						cur_pfrac &= 0xffff;
						cur_pos |= 1;
					}

					cur_pfrac += delta;
					while(cur_pfrac & ~0xffff) {
						cur_pfrac += fdelta;
						cur_pos += pdelta;

						cur_pval = cur_val;
						cur_val = read_byte(cur_pos>>1);
						if(cur_val == 0x88 && (base2[1] & 1)) {
							cur_pos = (base1[0x08] | (base1[0x09] << 8) | (base1[0x0a] << 16)) << 1;
							cur_val = read_byte(cur_pos>>1);
						}
						if(cur_val == 0x88) {
							keyoff(ch);
							cur_val = 0;
							break;
						}
						if(cur_pos & 1)
							cur_val >>= 4;
						else
							cur_val &= 15;
						cur_val = cur_pval + dpcm[cur_val];
						if(cur_val < -32768)
							cur_val = -32768;
						else if(cur_val > 32767)
							cur_val = 32767;
					}

					cur_pfrac >>= 1;
					if(cur_pos & 1)
						cur_pfrac |= 0x8000;
					cur_pos >>= 1;
					break;
				}
				default:
					LOG("Unknown sample type %x for channel %d\n", base2[0] & 0xc, ch);
					break;
				}
				lval += cur_val * lvol;
				rval += cur_val * rvol;
				rbase[(rdelta + reverb_pos) & 0x1fff] += int16_t(cur_val*rbvol);

				chan->pos = cur_pos;
				chan->pfrac = cur_pfrac;
				chan->pval = cur_pval;
				chan->val = cur_val;

				if(regupdate()) {
					base1[0x0c] = cur_pos     & 0xff;
					base1[0x0d] = cur_pos>> 8 & 0xff;
					base1[0x0e] = cur_pos>>16 & 0xff;
				}
			}
		reverb_pos = (reverb_pos + 1) & 0x1fff;
		outputs[0][sample] = int16_t(lval);
		outputs[1][sample] = int16_t(rval);
	}
}


void k054539_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
{
	if (regs[0x22f] & 0x20)
		m_timer_handler(m_timer_state ^= 1);
}

void k054539_device::init_chip()
{
	memset(regs, 0, sizeof(regs));
	memset(posreg_latch, 0, sizeof(posreg_latch)); //*
	flags |= UPDATE_AT_KEYON; //* make it default until proven otherwise

	ram = std::make_unique<uint8_t[]>(0x4000);
	reverb_pos = 0;
	cur_ptr = 0;
	memset(ram.get(), 0, 0x4000);

	stream = stream_alloc(0, 2, clock() / 384);

	save_item(NAME(voltab));
	save_item(NAME(pantab));
	save_item(NAME(gain));
	save_item(NAME(posreg_latch));
	save_item(NAME(flags));

	save_item(NAME(regs));
	save_pointer(NAME(ram), 0x4000);
	save_item(NAME(reverb_pos));
	save_item(NAME(cur_ptr));
	save_item(NAME(cur_limit));
	save_item(NAME(rom_addr));

	save_item(NAME(m_timer_state));
}

void k054539_device::write(offs_t offset, u8 data)
{
	if(0) {
		int voice, reg;

		/* The K054539 has behavior like many other wavetable chips including
		   the Ensoniq 550x and Gravis GF-1: if a voice is active, writing
		   to it's current position is silently ignored.

		   Dadandaan depends on this or the vocals go wrong.
		*/
		if (offset < 8*0x20)
		{
			voice = offset / 0x20;
			reg = offset & ~0x20;

			if(regs[0x22c] & (1<<voice))
				if (reg >= 0xc && reg <= 0xe)
					return;
		}
	}

	bool latch = (flags & UPDATE_AT_KEYON) && (regs[0x22f] & 1);

	if (latch && offset < 0x100)
	{
		int offs = (offset & 0x1f) - 0xc;
		int ch = offset >> 5;

		if (offs >= 0 && offs <= 2)
		{
			// latch writes to the position index registers
			posreg_latch[ch][offs] = data;
			return;
		}
	}

	else
		switch(offset) {
		case 0x13f: {
			int pan = data >= 0x11 && data <= 0x1f ? data - 0x11 : 0x18 - 0x11;
			if (!m_apan_cb.isnull())
				m_apan_cb(pantab[pan], pantab[0xe - pan]);
			break;
		}

		case 0x214:
			if (latch)
			{
				for(int ch=0; ch<8; ch++)
				{
					if(data & (1<<ch))
					{
						uint8_t *posptr = &posreg_latch[ch][0];
						uint8_t *regptr = regs + (ch<<5) + 0xc;

						// update the chip at key-on
						regptr[0] = posptr[0];
						regptr[1] = posptr[1];
						regptr[2] = posptr[2];

						keyon(ch);
					}
				}
			}
			else
			{
				for(int ch=0; ch<8; ch++)
					if(data & (1<<ch))
						keyon(ch);
			}
		break;

		case 0x215:
			for(int ch=0; ch<8; ch++)
				if(data & (1<<ch))
					keyoff(ch);
		break;

		case 0x227:
		{
			attotime period = attotime::from_hz((float)(38 + data) * (clock()/384.0f/14400.0f)) / 2.0f;
			logerror("TIMER %02x %g\n", data, (float)(38 + data) * (clock()/384.0f/14400.0f));
			m_timer->adjust(period, 0, period);

			m_timer_state = 0;
			m_timer_handler(m_timer_state);
		}
		break;

		case 0x22d:
			if(rom_addr == 0x80)
				ram[cur_ptr] = data;
			cur_ptr++;
			if(cur_ptr == cur_limit)
				cur_ptr = 0;
		break;

		case 0x22e:
			rom_addr = data;
			cur_limit = rom_addr == 0x80 ? 0x4000 : 0x20000;
			cur_ptr = 0;
		break;

		case 0x22f:
			if (!(data & 0x20)) // Disable timer output?
			{
				m_timer_state = 0;
				m_timer_handler(m_timer_state);
			}
		break;

		default:
#if 0
			if(regs[offset] != data) {
				if((offset & 0xff00) == 0) {
					chanoff = offset & 0x1f;
					if(chanoff < 4 || chanoff == 5 ||
						(chanoff >=8 && chanoff <= 0xa) ||
						(chanoff >= 0xc && chanoff <= 0xe))
						break;
				}
				if(1 || ((offset >= 0x200) && (offset <= 0x210)))
					break;
				logerror("K054539 %03x = %02x\n", offset, data);
			}
#endif
		break;
	}

	regs[offset] = data;
}

void k054539_device::device_post_load()
{
	cur_limit = rom_addr == 0x80 ? 0x4000 : 0x20000;
}

u8 k054539_device::read(offs_t offset)
{
	switch(offset) {
	case 0x22d:
		if(regs[0x22f] & 0x10) {
			uint8_t res = (rom_addr == 0x80) ? ram[cur_ptr] : read_byte((0x20000*rom_addr)+cur_ptr);
			cur_ptr++;
			if(cur_ptr == cur_limit)
				cur_ptr = 0;
			return res;
		} else
			return 0;
	case 0x22c:
		break;
	default:
		LOG("K054539 read %03x\n", offset);
		break;
	}
	return regs[offset];
}

void k054539_device::device_start()
{
	m_timer = timer_alloc(0);

	// resolve / bind callbacks
	m_timer_handler.resolve_safe();
	m_apan_cb.resolve();

	for (auto & elem : gain)
		elem = 1.0;

	flags = RESET_FLAGS;

	/*
	    I've tried various equations on volume control but none worked consistently.
	    The upper four channels in most MW/GX games simply need a significant boost
	    to sound right. For example, the bass and smash sound volumes in Violent Storm
	    have roughly the same values and the voices in Tokimeki Puzzledama are given
	    values smaller than those of the hihats. Needless to say the two K054539 chips
	    in Mystic Warriors are completely out of balance. Rather than forcing a
	    "one size fits all" function to the voltab the current invert exponential
	    appraoch seems most appropriate.
	*/
	// Factor the 1/4 for the number of channels in the volume (1/8 is too harsh, 1/2 gives clipping)
	// vol=0 -> no attenuation, vol=0x40 -> -36dB
	for(int i=0; i<256; i++)
		voltab[i] = pow(10.0, (-36.0 * (double)i / (double)0x40) / 20.0) / 4.0;

	// Pan table for the left channel
	// Right channel is identical with inverted index
	// Formula is such that pan[i]**2+pan[0xe-i]**2 = 1 (constant output power)
	// and pan[0xe] = 1 (full panning)
	for(int i=0; i<0xf; i++)
		pantab[i] = sqrt((double)i) / sqrt((double)0xe);

	init_chip();
}

void k054539_device::device_clock_changed()
{
	stream->set_sample_rate(clock() / 384);
}

void k054539_device::device_reset()
{
	regs[0x22c] = 0;
	regs[0x22f] = 0;
	memset(ram.get(), 0, 0x4000);
	m_timer->enable(false);
}


//-------------------------------------------------
//  rom_bank_updated - the rom bank has changed
//-------------------------------------------------

void k054539_device::rom_bank_updated()
{
	stream->update();
}