summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/sound/8950intf.cpp
blob: adc51009a0c538a9f40aeff3b7a934b59153888b (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
// license:BSD-3-Clause
// copyright-holders:Ernesto Corvi
/******************************************************************************
* FILE
*   Yamaha 3812 emulator interface - MAME VERSION
*
* CREATED BY
*   Ernesto Corvi
*
* UPDATE LOG
*   JB  28-04-2002  Fixed simultaneous usage of all three different chip types.
*                       Used real sample rate when resample filter is active.
*       AAT 12-28-2001  Protected Y8950 from accessing unmapped port and keyboard handlers.
*   CHS 1999-01-09  Fixes new ym3812 emulation interface.
*   CHS 1998-10-23  Mame streaming sound chip update
*   EC  1998        Created Interface
*
* NOTES
*
******************************************************************************/
#include "8950intf.h"
#include "fmopl.h"


static void IRQHandler(void *param,int irq)
{
	y8950_device *y8950 = (y8950_device *) param;
	y8950->_IRQHandler(irq);
}

void y8950_device::_IRQHandler(int irq)
{
	m_irq_handler(irq);
}

void y8950_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
{
	switch(id)
	{
	case 0:
		y8950_timer_over(m_chip,0);
		break;

	case 1:
		y8950_timer_over(m_chip,1);
		break;
	}
}

static void timer_handler(void *param, int c, const attotime &period)
{
	y8950_device *y8950 = (y8950_device *) param;
	y8950->_timer_handler(c, period);
}

void y8950_device::_timer_handler(int c, const attotime &period)
{
	if( period == attotime::zero )
	{   /* Reset FM Timer */
		m_timer[c]->enable(false);
	}
	else
	{   /* Start FM Timer */
		m_timer[c]->adjust(period);
	}
}


static unsigned char Y8950PortHandler_r(void *param)
{
	y8950_device *y8950 = (y8950_device *) param;
	return y8950->_Y8950PortHandler_r();
}

unsigned char y8950_device::_Y8950PortHandler_r()
{
	return m_io_read_handler(0);
}

static void Y8950PortHandler_w(void *param,unsigned char data)
{
	y8950_device *y8950 = (y8950_device *) param;
	y8950->_Y8950PortHandler_w(data);
}

void y8950_device::_Y8950PortHandler_w(unsigned char data)
{
	m_io_write_handler((offs_t)0,data);
}

static unsigned char Y8950KeyboardHandler_r(void *param)
{
	y8950_device *y8950 = (y8950_device *) param;
	return y8950->_Y8950KeyboardHandler_r();
}

unsigned char y8950_device::_Y8950KeyboardHandler_r()
{
	return m_keyboard_read_handler(0);
}

static void Y8950KeyboardHandler_w(void *param,unsigned char data)
{
	y8950_device *y8950 = (y8950_device *) param;
	y8950->_Y8950KeyboardHandler_w(data);
}

void y8950_device::_Y8950KeyboardHandler_w(unsigned char data)
{
	m_keyboard_write_handler((offs_t)0,data);
}

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

void y8950_device::sound_stream_update(sound_stream &stream, stream_sample_t **inputs, stream_sample_t **outputs, int samples)
{
	y8950_update_one(m_chip, outputs[0], samples);
}

static void y8950_update_request(void *param, int interval)
{
	y8950_device *y8950 = (y8950_device *) param;
	y8950->_y8950_update_request();
}

void y8950_device::_y8950_update_request()
{
	m_stream->update();
}


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

void y8950_device::device_start()
{
	int rate = clock()/72;

	m_irq_handler.resolve_safe();
	m_keyboard_read_handler.resolve_safe(0);
	m_keyboard_write_handler.resolve_safe();
	m_io_read_handler.resolve_safe(0);
	m_io_write_handler.resolve_safe();

	/* stream system initialize */
	m_chip = y8950_init(this,clock(),rate);
	assert_always(m_chip != nullptr, "Error creating Y8950 chip");

	/* ADPCM ROM data */
	y8950_set_delta_t_memory(m_chip, m_region->base(), m_region->bytes());

	m_stream = machine().sound().stream_alloc(*this,0,1,rate);
	/* port and keyboard handler */
	y8950_set_port_handler(m_chip, Y8950PortHandler_w, Y8950PortHandler_r, this);
	y8950_set_keyboard_handler(m_chip, Y8950KeyboardHandler_w, Y8950KeyboardHandler_r, this);

	/* Y8950 setup */
	y8950_set_timer_handler (m_chip, timer_handler, this);
	y8950_set_irq_handler   (m_chip, IRQHandler, this);
	y8950_set_update_handler(m_chip, y8950_update_request, this);

	m_timer[0] = timer_alloc(0);
	m_timer[1] = timer_alloc(1);
}

//-------------------------------------------------
//  device_stop - device-specific stop
//-------------------------------------------------

void y8950_device::device_stop()
{
	y8950_shutdown(m_chip);
}

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

void y8950_device::device_reset()
{
	y8950_reset_chip(m_chip);
}


READ8_MEMBER( y8950_device::read )
{
	return y8950_read(m_chip, offset & 1);
}

WRITE8_MEMBER( y8950_device::write )
{
	y8950_write(m_chip, offset & 1, data);
}

READ8_MEMBER( y8950_device::status_port_r ) { return read(space, 0); }
READ8_MEMBER( y8950_device::read_port_r ) { return read(space, 1); }
WRITE8_MEMBER( y8950_device::control_port_w ) { write(space, 0, data); }
WRITE8_MEMBER( y8950_device::write_port_w ) { write(space, 1, data); }


const device_type Y8950 = &device_creator<y8950_device>;

y8950_device::y8950_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
	: device_t(mconfig, Y8950, "Y8950", tag, owner, clock, "y8950", __FILE__),
		device_sound_interface(mconfig, *this),
		m_stream(nullptr),
		m_chip(nullptr),
		m_irq_handler(*this),
		m_keyboard_read_handler(*this),
		m_keyboard_write_handler(*this),
		m_io_read_handler(*this),
		m_io_write_handler(*this),
		m_region(*this, DEVICE_SELF)
{
}

//-------------------------------------------------
//  device_config_complete - perform any
//  operations now that the configuration is
//  complete
//-------------------------------------------------

void y8950_device::device_config_complete()
{
}