summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/sound/sp0250.cpp
blob: 1ced1f3ff87b1f6fc0588f9028d2143b87872c0a (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
// license:BSD-3-Clause
// copyright-holders:Olivier Galibert
/*
   GI SP0250 digital LPC sound synthesizer

   By O. Galibert.

   Unimplemented:
   - Direct Data test mode (pin 7)
*/

#include "emu.h"
#include "sp0250.h"

//
// Input clock is divided by 2 to make ROMCLOCK.
// Output is via pulse-width modulation (PWM) over the course of 39 ROMCLOCKs.
// 4 PWM periods per frame.
//
static constexpr int PWM_CLOCKS = 39;


DEFINE_DEVICE_TYPE(SP0250, sp0250_device, "sp0250", "GI SP0250 LPC")

sp0250_device::sp0250_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock) :
	device_t(mconfig, SP0250, tag, owner, clock),
	device_sound_interface(mconfig, *this),
	m_pwm_mode(false),
	m_pwm_index(PWM_CLOCKS),
	m_pwm_count(0),
	m_pwm_counts(0),
	m_voiced(0),
	m_amp(0),
	m_lfsr(0x7fff),
	m_pitch(0),
	m_pcount(0),
	m_repeat(0),
	m_rcount(0),
	m_fifo_pos(0),
	m_stream(nullptr),
	m_drq(*this)
{
	for (auto & elem : m_fifo)
	{
		elem = 0;
	}

	for (auto & elem : m_filter)
	{
		elem.F = 0;
		elem.B = 0;
		elem.z1 = 0;
		elem.z2 = 0;
	}
}

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

void sp0250_device::device_start()
{
	// output PWM data at the ROMCLOCK frequency
	int sample_rate = clock() / 2;
	int frame_rate = sample_rate / (4 * PWM_CLOCKS);
	if (!m_pwm_mode)
		m_stream = machine().sound().stream_alloc(*this, 0, 1, frame_rate);
	else
		m_stream = machine().sound().stream_alloc(*this, 0, 1, sample_rate);

	// if a DRQ callback is offered, run a timer at the frame rate
	// to ensure the DRQ gets picked up in a timely manner
	m_drq.resolve_safe();
	if (!m_drq.isnull())
	{
		m_drq(ASSERT_LINE);
		attotime period = attotime::from_hz(frame_rate);
		timer_alloc()->adjust(period, 0, period);
	}

	// PWM state
	save_item(NAME(m_pwm_index));
	save_item(NAME(m_pwm_count));
	save_item(NAME(m_pwm_counts));

	// LPC state
	save_item(NAME(m_voiced));
	save_item(NAME(m_amp));
	save_item(NAME(m_lfsr));
	save_item(NAME(m_pitch));
	save_item(NAME(m_pcount));
	save_item(NAME(m_repeat));
	save_item(NAME(m_rcount));

	save_item(STRUCT_MEMBER(m_filter, F));
	save_item(STRUCT_MEMBER(m_filter, B));
	save_item(STRUCT_MEMBER(m_filter, z1));
	save_item(STRUCT_MEMBER(m_filter, z2));

	// FIFO state
	save_item(NAME(m_fifo));
	save_item(NAME(m_fifo_pos));
}

void sp0250_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
{
	m_stream->update();
}

static uint16_t sp0250_ga(uint8_t v)
{
	return (v & 0x1f) << (v>>5);
}

static int16_t sp0250_gc(uint8_t v)
{
	// Internal ROM to the chip, cf. manual
	static const uint16_t coefs[128] =
	{
		  0,   9,  17,  25,  33,  41,  49,  57,  65,  73,  81,  89,  97, 105, 113, 121,
		129, 137, 145, 153, 161, 169, 177, 185, 193, 201, 203, 217, 225, 233, 241, 249,
		257, 265, 273, 281, 289, 297, 301, 305, 309, 313, 317, 321, 325, 329, 333, 337,
		341, 345, 349, 353, 357, 361, 365, 369, 373, 377, 381, 385, 389, 393, 397, 401,
		405, 409, 413, 417, 421, 425, 427, 429, 431, 433, 435, 437, 439, 441, 443, 445,
		447, 449, 451, 453, 455, 457, 459, 461, 463, 465, 467, 469, 471, 473, 475, 477,
		479, 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
	};
	int16_t res = coefs[v & 0x7f];

	if (!(v & 0x80))
		res = -res;
	return res;
}

void sp0250_device::load_values()
{
	m_filter[0].B = sp0250_gc(m_fifo[ 0]);
	m_filter[0].F = sp0250_gc(m_fifo[ 1]);
	m_amp         = sp0250_ga(m_fifo[ 2]);
	m_filter[1].B = sp0250_gc(m_fifo[ 3]);
	m_filter[1].F = sp0250_gc(m_fifo[ 4]);
	m_pitch       =           m_fifo[ 5];
	m_filter[2].B = sp0250_gc(m_fifo[ 6]);
	m_filter[2].F = sp0250_gc(m_fifo[ 7]);
	m_repeat      =           m_fifo[ 8] & 0x3f;
	m_voiced      =           m_fifo[ 8] & 0x40;
	m_filter[3].B = sp0250_gc(m_fifo[ 9]);
	m_filter[3].F = sp0250_gc(m_fifo[10]);
	m_filter[4].B = sp0250_gc(m_fifo[11]);
	m_filter[4].F = sp0250_gc(m_fifo[12]);
	m_filter[5].B = sp0250_gc(m_fifo[13]);
	m_filter[5].F = sp0250_gc(m_fifo[14]);
	m_fifo_pos = 0;
	m_drq(ASSERT_LINE);
	m_pcount = 0;
	m_rcount = 0;

	for (int f = 0; f < 6; f++)
		m_filter[f].reset();
}

void sp0250_device::write(uint8_t data)
{
	m_stream->update();
	if (m_fifo_pos != 15)
	{
		m_fifo[m_fifo_pos++] = data;
		if (m_fifo_pos == 15)
			m_drq(CLEAR_LINE);
	}
	else
		logerror("%s: overflow SP0250 FIFO\n", machine().describe_context());
}


uint8_t sp0250_device::drq_r()
{
	m_stream->update();
	return (m_fifo_pos == 15) ? CLEAR_LINE : ASSERT_LINE;
}

int8_t sp0250_device::next()
{
	if (m_rcount >= m_repeat)
	{
		if (m_fifo_pos == 15)
			load_values();
		else
		{
			// According to http://www.cpcwiki.eu/index.php/SP0256_Measured_Timings
			// the SP0250 executes "NOPs" with a repeat count of 1 and unchanged
			// pitch while waiting for input
			m_repeat = 1;
			m_pcount = 0;
			m_rcount = 0;
		}
	}

	// 15-bit LFSR algorithm verified by dump from actual hardware
	// clocks every cycle regardless of voiced/unvoiced setting
	m_lfsr ^= (m_lfsr ^ (m_lfsr >> 1)) << 15;
	m_lfsr >>= 1;

	int16_t z0;
	if (m_voiced)
		z0 = (m_pcount == 0) ? m_amp : 0;
	else
		z0 = (m_lfsr & 1) ? m_amp : -m_amp;

	for (int f = 0; f < 6; f++)
		z0 = m_filter[f].apply(z0);

	// maximum amp value is effectively 13 bits
	// reduce to 7 bits; due to filter effects it
	// may occasionally clip
	int dac = z0 >> 6;
	if (dac < -64)
		dac = -64;
	if (dac > 63)
		dac = 63;

	// PWM is divided into 4x 5-bit sections; the lower
	// bits of the original 7-bit value are added to only
	// some of the pulses in the following pattern:
	//
	//    DAC -64 -> 1,1,1,1
	//    DAC -63 -> 2,1,1,1
	//    DAC -62 -> 2,1,2,1
	//    DAC -61 -> 2,2,2,1
	//    DAC -60 -> 2,2,2,2
	//    ...
	//    DAC  -1 -> 17,17,17,16
	//    DAC   0 -> 17,17,17,17
	//    DAC   1 -> 18,17,17,17
	//    ...
	//    DAC  60 -> 32,32,32,32
	//    DAC  61 -> 33,32,32,32
	//    DAC  62 -> 33,32,33,32
	//    DAC  63 -> 33,33,33,32
	m_pwm_counts = (((dac + 68 + 3) >> 2) << 0) +
				   (((dac + 68 + 1) >> 2) << 8) +
				   (((dac + 68 + 2) >> 2) << 16) +
				   (((dac + 68 + 0) >> 2) << 24);

	if (m_pcount++ == m_pitch)
	{
		m_pcount = 0;
		m_rcount++;
	}
	return dac;
}

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

void sp0250_device::sound_stream_update(sound_stream &stream, stream_sample_t **inputs, stream_sample_t **outputs, int samples)
{
	stream_sample_t *output = outputs[0];
	if (!m_pwm_mode)
	{
		while (samples-- != 0)
			*output++ = next() << 8;
	}
	else
	{
		while (samples != 0)
		{
			// see where we're at in the current PWM cycle
			if (m_pwm_index >= PWM_CLOCKS)
			{
				m_pwm_index = 0;
				if (m_pwm_counts == 0)
					next();
				m_pwm_count = m_pwm_counts & 0xff;
				m_pwm_counts >>= 8;
			}

			// determine the value to fill and the number of samples remaining
			// until it changes
			stream_sample_t value;
			int remaining;
			if (m_pwm_index < m_pwm_count)
			{
				value = 32767;
				remaining = m_pwm_count - m_pwm_index;
			}
			else
			{
				value = 0;
				remaining = PWM_CLOCKS - m_pwm_index;
			}

			// clamp to the number of samples requested and advance the counters
			if (remaining > samples)
				remaining = samples;
			m_pwm_index += remaining;
			samples -= remaining;

			// fill the output
			while (remaining-- != 0)
				*output++ = value;
		}
	}
}