summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/sound/gt155.cpp
blob: bdd71a074c547e2a208ea35dc474d7f762fdae8a (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
// license:BSD-3-Clause
// copyright-holders:Devin Acker

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

    Casio GT155 (HG51B155FD)

    This is the sound generator and DSP used in various higher-end
    "A-Squared Sound Source" keyboards and pianos between roughly 1994-2001.

    TODO:
    - verify per-voice lowpass filter behavior
    - DSP (architecture/instruction set seems to be the same as the standalone
      "GD277" DSP used in other contemporary keyboards)

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

#include "emu.h"
#include "gt155.h"

#include <algorithm>

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

DEFINE_DEVICE_TYPE(GT155, gt155_device, "gt155", "Casio GT155")

gt155_device::gt155_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: device_t(mconfig, GT155, tag, owner, clock)
	, device_sound_interface(mconfig, *this)
	, device_rom_interface(mconfig, *this)
{
}

/**************************************************************************/
void gt155_device::device_start()
{
	m_stream = stream_alloc(0, 2, clock() / CLOCKS_PER_SAMPLE); // 16.384 MHz -> 32 kHz, or 24.576 MHz -> 48 kHz

	for (int i = 0; i < 0x800; i++)
	{
		const double frac = 1.0 - ((double)i / 0x7ff);
		m_volume[i] = 0x800 * pow(10, -2.15 * frac) * cos(0.5 * M_PI * frac * frac * frac);
	}

	save_item(NAME(m_data));
	save_item(NAME(m_dsp_data));
	save_item(NAME(m_rom_addr));

	save_item(STRUCT_MEMBER(m_voices, m_enable));
	save_item(STRUCT_MEMBER(m_voices, m_format));

	save_item(STRUCT_MEMBER(m_voices, m_addr));
	save_item(STRUCT_MEMBER(m_voices, m_addr_frac));
	save_item(STRUCT_MEMBER(m_voices, m_addr_end));
	save_item(STRUCT_MEMBER(m_voices, m_addr_loop));
	save_item(STRUCT_MEMBER(m_voices, m_addr_loop_frac));

	save_item(STRUCT_MEMBER(m_voices, m_pitch));

	save_item(STRUCT_MEMBER(m_voices, m_filter_gain));
	save_item(STRUCT_MEMBER(m_voices, m_filter));
	save_item(STRUCT_MEMBER(m_voices, m_filter_out));
	save_item(STRUCT_MEMBER(m_voices, m_filter_unk));

	save_item(STRUCT_MEMBER(m_voices, m_sample_last));
	save_item(STRUCT_MEMBER(m_voices, m_sample));

	save_item(STRUCT_MEMBER(m_voices, m_env_current));
	save_item(STRUCT_MEMBER(m_voices, m_env_target));
	save_item(STRUCT_MEMBER(m_voices, m_env_level));
	save_item(STRUCT_MEMBER(m_voices, m_env_scale));
	save_item(STRUCT_MEMBER(m_voices, m_env_rate));

	save_item(STRUCT_MEMBER(m_voices, m_balance));
	save_item(STRUCT_MEMBER(m_voices, m_dsp_send));

}

/**************************************************************************/
void gt155_device::device_reset()
{
	std::fill(std::begin(m_data), std::end(m_data), 0);
	std::fill(std::begin(m_dsp_data), std::end(m_dsp_data), 0);
	std::fill(std::begin(m_voices), std::end(m_voices), voice_t());
}

/**************************************************************************/
void gt155_device::device_clock_changed()
{
	m_stream->set_sample_rate(clock() / CLOCKS_PER_SAMPLE);
}

/**************************************************************************/
void gt155_device::sound_stream_update(sound_stream& stream)
{
	for (int i = 0; i < stream.samples(); i++)
	{
		s64 left = 0, right = 0;

		for (auto &voice : m_voices)
		{
			if (voice.m_enable)
			{
				mix_sample(voice, left, right);
				voice.update_envelope();
			}
		}

		stream.put_int_clamp(0, i, left >> 11, 32678);
		stream.put_int_clamp(1, i, right >> 11, 32768);
	}
}

/**************************************************************************/
void gt155_device::rom_bank_pre_change()
{
	m_stream->update();
}

/**************************************************************************/
void gt155_device::mix_sample(voice_t &voice, s64 &left, s64 &right)
{
	// update sample position
	voice.m_addr_frac += voice.m_pitch;
	if (voice.m_addr_frac >= (1 << 15))
		update_sample(voice);

	// interpolate, apply envelope + channel gain and lowpass, and mix into output
	s64 sample = voice.m_sample_last + (s64(voice.m_sample - voice.m_sample_last) * voice.m_addr_frac >> 15);
	// TODO: does this produce accurate filter output?
	sample = sample * voice.m_filter_gain;
	sample += s64(voice.m_filter_out) * (voice.m_filter ^ 0xffff);
	sample >>= 16;
	voice.m_filter_out = sample;

	const u16 env_level = voice.m_env_current >> (ENV_SHIFT + 4);
	sample *= m_volume[env_level];

	left  += (sample * voice.m_balance[0]) / 0x1f;
	right += (sample * voice.m_balance[1]) / 0x1f;
}

/**************************************************************************/
void gt155_device::voice_t::update_envelope()
{
	if (m_env_target > m_env_current
		&& (m_env_target - m_env_current) > m_env_rate)
	{
		m_env_current += m_env_rate;
	}
	else if (m_env_target < m_env_current
		&& (m_env_current - m_env_target) > m_env_rate)
	{
		m_env_current -= m_env_rate;
	}
	else
	{
		m_env_current = m_env_target;
	}
}

/**************************************************************************/
void gt155_device::update_sample(voice_t &voice)
{
	voice.m_sample_last = voice.m_sample;

	while (voice.m_addr_frac >= (1 << 15))
	{
		voice.m_addr += (voice.m_addr_frac >> 15) * (voice.m_format ? 2 : 1);
		voice.m_addr_frac &= 0x7fff;

		if (voice.m_addr >= voice.m_addr_end)
		{
			if (voice.m_addr_loop == voice.m_addr_end)
			{
				// if this is a one-shot sample, just disable it now
				voice.m_enable = 0;
				voice.m_env_current = voice.m_env_target = 0;
				return;
			}

			// apply the fractional component of the loop
			if (voice.m_format)
			{
				voice.m_addr -= (voice.m_addr_end - (voice.m_addr_loop & ~1));
				voice.m_addr_frac += (voice.m_addr_loop_frac >> 1);
				if (BIT(voice.m_addr_loop, 0))
					voice.m_addr_frac += 1 << 14;
			}
			else
			{
				voice.m_addr -= (voice.m_addr_end - voice.m_addr_loop);
				voice.m_addr_frac += voice.m_addr_loop_frac;
			}
		}
	}

	if (voice.m_format)
	{
		voice.m_sample = read_word(voice.m_addr & ~1);
	}
	else
	{
		// wk1800 apparently expects 8bit samples to only be expanded to 9 bits
		// (with m_filter_gain then boosted to compensate)
		voice.m_sample = s16(s8(read_byte(voice.m_addr))) << 1;
	}
}

/**************************************************************************/
void gt155_device::write(offs_t offset, u8 data)
{
	offset &= 0xf;

	if (offset < 6)
	{
		m_data[offset] = data;
	}
	else if (offset == 6)
	{
		switch (data)
		{
		case 0x00:
			m_rom_addr = (m_data[0] << 1) | (m_data[1] << 9) | (m_data[2] << 17);
			break;

		case 0x06:
			m_dsp_data[m_data[5] & 0x7f] &= 0xffff;
			m_dsp_data[m_data[5] & 0x7f] |= ((m_data[0] << 16) | (m_data[1] << 24));
			break;

		case 0x07:
			m_dsp_data[m_data[5] & 0x7f] &= 0xffff0000;
			m_dsp_data[m_data[5] & 0x7f] |= (m_data[0] | (m_data[1] << 8));
			return;

		default:
			voice_command(data);
			break;
		}

	}
	else
	{
		logerror("%s: write offset %u = %02x\n", machine().describe_context(), offset, data);
	}
}

/**************************************************************************/
u8 gt155_device::read(offs_t offset)
{
	u8 data = 0;
	offset &= 0xf;

	if (offset < 0x6)
	{
		data = m_data[offset];
	}
	else if (offset == 0xe)
	{
		data = read_byte(m_rom_addr);
	}
	else if (offset == 0xf)
	{
		data = read_byte(m_rom_addr + 1);
		if (!machine().side_effects_disabled())
			m_rom_addr += 2;
	}
	else if (!machine().side_effects_disabled())
	{
		logerror("%s: read offset %u\n", machine().describe_context(), offset);
	}

	return data;
}

/**************************************************************************/
u16 gt155_device::reg16(u8 num) const
{
	return m_data[num] | (m_data[num+1] << 8);
}

/**************************************************************************/
u32 gt155_device::reg24(u8 num) const
{
	return m_data[num] | (m_data[num+1] << 8) | (m_data[num+2] << 16);
}

/**************************************************************************/
u32 gt155_device::reg32(u8 num) const
{
	return m_data[num] | (m_data[num+1] << 8) | (m_data[num+2] << 16) | (m_data[num+3] << 24);
}

/**************************************************************************/
void gt155_device::voice_command(u8 data)
{
	m_stream->update();

	voice_t &voice = m_voices[m_data[5] & 0x1f];
	const u16 cmd = data | ((m_data[5] & 0xe0) << 8);

	switch (cmd)
	{
	case 0x0001: // sample start address
		voice.m_addr = reg32(1) >> 7;
		voice.m_addr_frac = reg16(0) & 0x7fff;
		break;

	case 0x2001: // sample loop address
		voice.m_addr_loop = reg32(1) >> 7;
		voice.m_addr_loop_frac = reg16(0) & 0x7fff;
		break;

	case 0x0002: // sample envelope step
	case 0x2002: // sample envelope step (double rate?) used when forcing a voice off
		voice.m_env_rate = reg16(0) & 0x7fff;
		if (cmd == 0x2002)
			voice.m_env_rate <<= 1;
		voice.m_env_level = reg16(2);
		voice.m_env_target = u32(voice.m_env_level) * voice.m_env_scale;
		break;

	case 0x0003: // sample end address and envelope scale
		voice.m_addr_end = reg24(0) << 1;
		voice.m_env_scale = m_data[3];
		voice.m_env_target = u32(voice.m_env_level) * voice.m_env_scale;
		break;

	case 0x0004:
		/*
		* params used by wk1800:
		* ff ff 00 00 (on boot)
		* fe 07 0c 00 (before note on)
		* f5 0d 0a 02 (starting 8-bit sample)
		* f5 05 0a 0a (starting 16-bit sample)
		* 00 20 00 00 (before note off)
		* 14 00 08 02 (after envelope update)
		*
		* TODO: is any of this related to how 8bit samples are expanded?
		* (see comment at bottom of update_sample)
		*/
		if (!voice.m_enable && !BIT(m_data[0], 1))
		{
			voice.m_format = BIT(m_data[3], 3);
			voice.m_sample_last = voice.m_sample = 0;
		}
		voice.m_enable = BIT(~m_data[0], 1);
		break;

	case 0x0005: // sample pitch
		voice.m_pitch = reg16(0);
		break;

	case 0x2005:
		{
			// this is actually gain premultiplied by filter level, so the sound HW does one fewer mult when applying the filter.
			const u16 gain = reg16(0);
			voice.m_filter_gain = (gain & 0x1fff) << (3 + (gain >> 13));
		}
		break;

	case 0x4005:
		/*
		filter coefficient (bit 15 inverted) - this is also premultiplied into the value written to m_filter_gain,
		with this value probably only being used as a coefficient for the previous filter output?
		*/
		voice.m_filter = reg16(0) ^ 0x8000;
		break;

	case 0x6005:
		voice.m_filter_unk = reg16(0);
		break;

	case 0x000e:
		voice.m_balance[0] = m_data[0] & 0x1f;
		voice.m_balance[1] = m_data[1] & 0x1f;
		break;

	case 0x200e:
		voice.m_dsp_send[0] = m_data[0] & 0x1f;
		voice.m_dsp_send[1] = m_data[1] & 0x1f;
		break;

	case 0x0014: // read envelope ready status
		if (voice.m_env_current == voice.m_env_target)
			m_data[2] = 0x00;
		else
			m_data[2] = 0x08;
		break;

	case 0x2018: // read envelope output & current sample output
		m_data[1] = voice.m_env_current >> ENV_SHIFT;
		m_data[2] = voice.m_env_current >> (ENV_SHIFT + 8);
		m_data[3] = voice.m_sample;
		m_data[4] = voice.m_sample >> 8;
		break;

	default:
		logerror("%s: sound cmd %02x%02x, param = %02x %02x %02x %02x %02x\n", machine().describe_context(),
				m_data[5], data,
				m_data[0], m_data[1], m_data[2], m_data[3], m_data[4]);
		break;
	}
}