summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/sound/ym2610.cpp
blob: 895507d4554a2d518838791d94ea85f58d44646f (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
// license:BSD-3-Clause
// copyright-holders:Aaron Giles

#include "emu.h"
#include "ym2610.h"


DEFINE_DEVICE_TYPE(YM2610, ym2610_device, "ym2610", "YM2610 OPNB")
DEFINE_DEVICE_TYPE(YM2610B, ym2610b_device, "ym2610b", "YM2610 OPNB2")


//*********************************************************
//  YM2610/YM2610B DEVICE
//*********************************************************

//-------------------------------------------------
//  ym2610_device - constructor
//-------------------------------------------------

ym2610_device::ym2610_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock, device_type type, u8 opn_mask) :
	ay8910_device(mconfig, type, tag, owner, clock, PSG_TYPE_YM, 1, 0),
	device_memory_interface(mconfig, *this),
	m_adpcm_a_config("adpcm-a", ENDIANNESS_LITTLE, 8, 24, 0),
	m_adpcm_b_config("adpcm-b", ENDIANNESS_LITTLE, 8, 24, 0),
	m_adpcm_a_region(*this, "adpcma"),
	m_adpcm_b_region(*this, "adpcmb"),
	m_opn(*this),
	m_adpcm_a(*this, read8sm_delegate(*this, FUNC(ym2610_device::adpcm_a_read)), 8),
	m_adpcm_b(*this, read8sm_delegate(*this, FUNC(ym2610_device::adpcm_b_read)), write8sm_delegate(*this), 8),
	m_stream(nullptr),
	m_busy_duration(m_opn.compute_busy_duration()),
	m_address(0),
	m_opn_mask(opn_mask),
	m_eos_status(0x00),
	m_flag_mask(0xbf)
{
}


//-------------------------------------------------
//  ym2610b_device - constructor
//-------------------------------------------------

ym2610b_device::ym2610b_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock) :
	ym2610_device(mconfig, tag, owner, clock, YM2610B, 0x3f)
{
}


//-------------------------------------------------
//  read - handle a read from the device
//-------------------------------------------------

u8 ym2610_device::read(offs_t offset)
{
	u8 result = 0;
	switch (offset & 3)
	{
		case 0: // status port, YM2203 compatible
			result = m_opn.status() & (ymopna_engine::STATUS_TIMERA | ymopna_engine::STATUS_TIMERB | ymopna_engine::STATUS_BUSY);
			break;

		case 1: // data port (only SSG)
			if (m_address < 0x10)
				result = ay8910_read_ym();
			else if (m_address == 0xff)
				result = 1;  // ID code
			break;

		case 2: // status port, extended
			result = m_eos_status & m_flag_mask;
			break;

		case 3: // ADPCM-B data
			break;
	}
	return result;
}


//-------------------------------------------------
//  write - handle a write to the register
//  interface
//-------------------------------------------------

void ym2610_device::write(offs_t offset, u8 value)
{
	switch (offset & 3)
	{
		case 0: // address port
			m_address = value;

			// write register to SSG emulator
			if (m_address < 0x10)
				ay8910_write_ym(0, m_address);
			break;

		case 1: // data port

			// ignore if paired with upper address
			if (BIT(m_address, 8))
				break;

			if (m_address < 0x10)
			{
				// write to SSG
				ay8910_write_ym(1, value);
			}
			else if (m_address < 0x1c)
			{
				// write to ADPCM-B
				m_stream->update();
				u8 address = m_address & 0x0f;

				// YM2610 effectively forces external mode on, and disables recording
				if (address == 0)
					value = (value | 0x20) & ~0x40;
				m_adpcm_b.write(address, value);
			}
			else if (m_address == 0x1c)
			{
				// EOS flag reset
				m_stream->update();
				m_flag_mask = ~value;
				m_eos_status &= ~value;
			}
			else
			{
				// write to OPN
				m_stream->update();
				m_opn.write(m_address, value);
			}

			// mark busy for a bit
			m_opn.set_busy_end(machine().time() + m_busy_duration);
			break;

		case 2: // upper address port
			m_address = 0x100 | value;
			break;

		case 3: // upper data port

			// ignore if paired with lower address
			if (!BIT(m_address, 8))
				break;

			if (m_address < 0x130)
			{
				// write to ADPCM-A
				m_stream->update();
				m_adpcm_a.write(m_address & 0x3f, value);
			}
			else
			{
				// write to OPN
				m_stream->update();
				m_opn.write(m_address, value);
			}

			// mark busy for a bit
			m_opn.set_busy_end(machine().time() + m_busy_duration);
			break;
	}
}


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

device_memory_interface::space_config_vector ym2610_device::memory_space_config() const
{
	return space_config_vector{
		std::make_pair(0, &m_adpcm_a_config),
		std::make_pair(1, &m_adpcm_b_config)
	};
}


//-------------------------------------------------
//  device_start - start of emulation
//-------------------------------------------------

void ym2610_device::device_start()
{
	// call our parent
	ay8910_device::device_start();

	// create our stream
	m_stream = stream_alloc(0, 2, clock() / (4 * 6 * 6));

	// save our data
	save_item(YMFM_NAME(m_address));
	save_item(YMFM_NAME(m_eos_status));
	save_item(YMFM_NAME(m_flag_mask));

	// save the engines
	m_opn.save(*this);
	m_adpcm_a.save(*this);
	m_adpcm_b.save(*this);

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

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


//-------------------------------------------------
//  device_reset - start of emulation
//-------------------------------------------------

void ym2610_device::device_reset()
{
	// call our parent
	ay8910_device::device_reset();

	// reset the engines
	m_opn.reset();
	m_adpcm_a.reset();
	m_adpcm_b.reset();

	// initialize our special interrupt states
	m_eos_status = 0x00;
	m_flag_mask = 0xbf;
}


//-------------------------------------------------
//  device_clock_changed - update if clock changes
//-------------------------------------------------

void ym2610_device::device_clock_changed()
{
	m_stream->set_sample_rate(clock() / (4 * 6 * 6));
	ay_set_clock(clock() / 4);

	// recompute the busy duration
	m_busy_duration = m_opn.compute_busy_duration();
}


//-------------------------------------------------
//  sound_stream_update - update the sound stream
//-------------------------------------------------

void ym2610_device::sound_stream_update(sound_stream &stream, std::vector<read_stream_view> const &inputs, std::vector<write_stream_view> &outputs)
{
	// if this is not our stream, pass it on
	if (&stream != m_stream)
	{
		ay8910_device::sound_stream_update(stream, inputs, outputs);
		return;
	}

	// iterate over all target samples
	for (int sampindex = 0; sampindex < outputs[0].samples(); sampindex++)
	{
		// clock the OPN
		u32 env_counter = m_opn.clock(m_opn_mask);

		// clock the ADPCM-A engine on every envelope cycle
		if (BIT(env_counter, 0, 2) == 0)
			m_eos_status |= m_adpcm_a.clock(0x3f);

		// clock the ADPCM-B engine every cycle
		m_adpcm_b.clock(0x01);
		if ((m_adpcm_b.status(0) & ymadpcm_b_channel::STATUS_EOS) != 0)
			m_eos_status |= 0x80;

		// update the OPN content; OPNB is 13-bit with no intermediate clipping
		s32 lsum = 0, rsum = 0;
		m_opn.output(lsum, rsum, 1, 32767, m_opn_mask);

		// mix in the ADPCM
		m_adpcm_a.output(lsum, rsum, 0x3f);
		m_adpcm_b.output(lsum, rsum, 2, 0x01);

		// YM2608 is stereo
		outputs[0].put_int_clamp(sampindex, lsum, 32768);
		outputs[1].put_int_clamp(sampindex, rsum, 32768);
	}
}


//-------------------------------------------------
//  adpcm_a_read - callback to read data for the
//  ADPCM-A engine; in this case, from address
//  space 0
//-------------------------------------------------

u8 ym2610_device::adpcm_a_read(offs_t offset)
{
	return space(0).read_byte(offset);
}


//-------------------------------------------------
//  adpcm_b_read - callback to read data for the
//  ADPCM-B engine; in this case, from address
//  space 1
//-------------------------------------------------

u8 ym2610_device::adpcm_b_read(offs_t offset)
{
	return space(1).read_byte(offset);
}