summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/sound/ym2154.cpp
blob: 2ad1bbac1fa92627da102cf29e5d56e68bad26c7 (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
// license:BSD-3-Clause
// copyright-holders:Aaron Giles
#include "emu.h"
#include "ym2154.h"


//**************************************************************************
//  YM2154 DEVICE
//**************************************************************************

DEFINE_DEVICE_TYPE(YM2154, ym2154_device, "ym2154", "YM2154 (RYP4)")

//-------------------------------------------------
//  ym2154_device - constructor
//-------------------------------------------------

ym2154_device::ym2154_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock) :
	device_t(mconfig, YM2154, tag, owner, clock),
	device_sound_interface(mconfig, *this),
	device_memory_interface(mconfig, *this),
	m_stream(nullptr),
	m_timer(nullptr),
	m_update_irq(*this),
	m_io_read(*this),
	m_io_write(*this),
	m_group0_config("group0", ENDIANNESS_LITTLE, 8, 18, 0),
	m_group1_config("group1", ENDIANNESS_LITTLE, 8, 18, 0),
	m_group0_region(*this, "group0"),
	m_group1_region(*this, "group1")
{
}


//-------------------------------------------------
//  read - handle a device read
//-------------------------------------------------

u8 ym2154_device::read(offs_t offset)
{
	u8 result = 0xff;
	switch (offset & 0x7f)
	{
		case 0x01: // A/D converter registers
		case 0x02: // A/D converter registers
		case 0x03: // A/D converter registers
		case 0x04: // A/D converter registers
		case 0x05: // A/D converter registers
		case 0x06: // A/D converter registers
		case 0x07: // A/D converter registers
		case 0x08: // A/D converter registers
		case 0x09: // A/D converter registers
		case 0x0a: // A/D converter registers
			result = m_io_read.isnull() ? 0 : m_io_read(offset - 1);
			break;

		case 0x0e: // IRQ ack
			update_irq_state(0);
			result = m_irq_count;
			break;
	}
	return result;
}


//-------------------------------------------------
//  write - handle a device write
//-------------------------------------------------

void ym2154_device::write(offs_t offset, u8 data)
{
	m_stream->update();

	u8 chan = BIT(offset, 0, 3);
	u8 old;
	switch (offset & 0x7f)
	{
		// timer count
		case 0x02:
			if (BIT(data, 7) != 0)
				m_timer_count = (m_timer_count & 0x0f) | (BIT(data, 0, 7) << 4);
			else
				m_timer_count = (m_timer_count & 0x7f0) | BIT(data, 0, 4);
			break;

		// timer enable/output
		case 0x03:
			old = m_timer_enable;
			m_timer_enable = BIT(data, 2);
			if (!m_timer_enable)
				m_timer->enable(false);
			else if (m_timer_enable && !old)
				m_timer->adjust((2048 - m_timer_count) * attotime::from_hz(sample_rate()));
			if (!m_io_write.isnull())
				m_io_write(0, BIT(data, 4, 4) ^ 0x0f);
			break;

		// output level
		case 0x04:
			m_total_level = BIT(data, 0, 6);
			break;

		// group 1 trigger
		case 0x05:
			for (int chan = 0; chan < 6; chan++)
				if (BIT(data, chan))
					m_channel[6 + chan].start();
			break;

		// group 0 trigger
		case 0x06:
			for (int chan = 0; chan < 6; chan++)
				if (BIT(data, chan))
					m_channel[0 + chan].start();
			break;

		// DAC mode (not implemented)
		case 0x07:
			break;

		// pan
		case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d:
			m_channel[0 + chan].m_panpot = BIT(data, 0, 4);
			m_channel[6 + chan].m_panpot = BIT(data, 4, 4);
			break;

		// rate/level, group 0
		case 0x10: case 0x11: case 0x12: case 0x13: case 0x14: case 0x15:
			m_channel[0 + chan].m_output_level = BIT(data, 0, 5);
			m_channel[0 + chan].m_rate = BIT(data, 5, 2);
			break;

		// rate/level, group 1
		case 0x18: case 0x19: case 0x1a: case 0x1b: case 0x1c: case 0x1d:
			m_channel[6 + chan].m_output_level = BIT(data, 0, 5);
			m_channel[6 + chan].m_rate = BIT(data, 5, 2);
			break;

		// sample position A, group 0
		case 0x20: case 0x21: case 0x22: case 0x23: case 0x24: case 0x25:
			m_channel[0 + chan].m_start = (m_channel[0 + chan].m_start & 0x0f) | (data << 4);
			break;

		// sample position B, group 0
		case 0x28: case 0x29: case 0x2a: case 0x2b: case 0x2c: case 0x2d:
			m_channel[0 + chan].m_start = (m_channel[0 + chan].m_start & 0xff0) | (data >> 4);
			m_channel[0 + chan].m_end = (m_channel[0 + chan].m_end & 0xff) | ((data & 0xf) << 8);
			break;

		// sample position C, group 0
		case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: case 0x35:
			m_channel[0 + chan].m_end = (m_channel[0 + chan].m_end & 0xf00) | data;
			break;

		// sample position A, group 1
		case 0x38: case 0x39: case 0x3a: case 0x3b: case 0x3c: case 0x3d:
			m_channel[6 + chan].m_start = (m_channel[6 + chan].m_start & 0x0f) | (data << 4);
			break;

		// sample position B, group 1
		case 0x40: case 0x41: case 0x42: case 0x43: case 0x44: case 0x45:
			m_channel[6 + chan].m_start = (m_channel[6 + chan].m_start & 0xff0) | (data >> 4);
			m_channel[6 + chan].m_end = (m_channel[6 + chan].m_end & 0xff) | ((data & 0xf) << 8);
			break;

		// sample position C, group 1
		case 0x48: case 0x49: case 0x4a: case 0x4b: case 0x4c: case 0x4d:
			m_channel[6 + chan].m_end = (m_channel[6 + chan].m_end & 0xf00) | data;
			break;
	}
}


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

void ym2154_device::device_start()
{
	// allocate our timer
	m_timer = timer_alloc(0);

	// resolve the handlers
	m_update_irq.resolve();
	m_io_read.resolve();
	m_io_write.resolve();

	// allocate our stream
	m_stream = stream_alloc(0, 2, sample_rate());

	// now register the blob for save, on the assumption the size won't change
	save_item(NAME(m_timer_count));
	save_item(NAME(m_timer_enable));
	save_item(NAME(m_irq_state));
	save_item(NAME(m_irq_count));
	save_item(NAME(m_total_level));
	save_item(STRUCT_MEMBER(m_channel, m_pos));
	save_item(STRUCT_MEMBER(m_channel, m_start));
	save_item(STRUCT_MEMBER(m_channel, m_end));
	save_item(STRUCT_MEMBER(m_channel, m_panpot));
	save_item(STRUCT_MEMBER(m_channel, m_output_level));
	save_item(STRUCT_MEMBER(m_channel, m_rate));

	// automatically map memory regions if not configured externally
	if (!has_configured_map(0) && !has_configured_map(1))
	{
		if (m_group0_region)
			space(0).install_rom(0, m_group0_region->bytes() - 1, m_group0_region->base());

		if (m_group1_region)
			space(1).install_rom(0, m_group1_region->bytes() - 1, m_group1_region->base());
		else if (m_group0_region)
			space(1).install_rom(0, m_group0_region->bytes() - 1, m_group0_region->base());
	}
}


//-------------------------------------------------
//  device_reset - device-specific reset
//-------------------------------------------------

void ym2154_device::device_reset()
{
	for (int chan = 0; chan < 12; chan++)
		m_channel[chan].m_pos = 0xfffffff;
}


//-------------------------------------------------
//  device_clock_changed - clock changed signal
//-------------------------------------------------

void ym2154_device::device_clock_changed()
{
	if (m_stream != nullptr)
		m_stream->set_sample_rate(sample_rate());
}


//-------------------------------------------------
//  sound_stream_update - generate sound data
//-------------------------------------------------

void ym2154_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
{
	update_irq_state(1);
	m_timer->adjust((2048 - m_timer_count) * attotime::from_hz(sample_rate()));
}


//-------------------------------------------------
//  sound_stream_update - generate sound data
//-------------------------------------------------

void ym2154_device::sound_stream_update(sound_stream &stream, std::vector<read_stream_view> const &inputs, std::vector<write_stream_view> &outputs)
{
	static const uint16_t voltable[8] = { 0x7fa,0x751,0x6b5,0x627,0x5a4,0x52c,0x4be,0x45a };

	auto &outl = outputs[0];
	auto &outr = outputs[1];

	outl.fill(0);
	outr.fill(0);

	for (int chan = 0; chan < 12; chan++)
	{
		auto &channel = m_channel[chan];

		// not sure how the "rate" really works but it's used to stop a sample from
		// playing so just treat it as such
		if (channel.m_rate == 3)
			m_channel[chan].m_pos = 0xfffffff;

		if ((channel.m_pos >> ADDR_SHIFT) <= channel.m_end && channel.m_panpot != 0)
		{
			uint32_t vol = (channel.m_output_level ^ 0x1f) + (m_total_level ^ 0x3f);

			uint32_t lvol = vol;
			if (channel.m_panpot > 8)
				lvol += 4 * (channel.m_panpot - 8);
			lvol = voltable[lvol & 7] >> (lvol >> 3);

			uint32_t rvol = vol;
			if (channel.m_panpot < 7)
				rvol += 4 * (7 - channel.m_panpot);
			rvol = voltable[rvol & 7] >> (rvol >> 3);

			for (int sampindex = 0; sampindex < outl.samples() && (channel.m_pos >> ADDR_SHIFT) <= channel.m_end; sampindex++)
			{
				// unsure what the real data is; for now guessing 8-bit PCM but could
				// be ADPCM-A or some other format
				int8_t data = space(chan / 6).read_byte(channel.m_pos++);
				outl.add_int(sampindex, data * lvol, 0x80 * 0x800);
				outr.add_int(sampindex, data * rvol, 0x80 * 0x800);
			}
		}
	}
}


//-------------------------------------------------
//  memory_space_config - return a description of
//  any address spaces owned by this device
//-------------------------------------------------

device_memory_interface::space_config_vector ym2154_device::memory_space_config() const
{
	return space_config_vector{
		std::make_pair(0, &m_group0_config),
		std::make_pair(1, &m_group1_config)
	};
}