summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/sound/3526intf.c
blob: 4a905b0b086b5c4bcfed76341b851d084aabdc6b (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
/******************************************************************************
* 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 "emu.h"
#include "3526intf.h"
#include "fm.h"
#include "sound/fmopl.h"


struct ym3526_state
{
	sound_stream *	stream;
	emu_timer *		timer[2];
	void *			chip;
	const ym3526_interface *intf;
	device_t *device;

	devcb_resolved_write_line out_int_func;
};


INLINE ym3526_state *get_safe_token(device_t *device)
{
	assert(device != NULL);
	assert(device->type() == YM3526);
	return (ym3526_state *)downcast<ym3526_device *>(device)->token();
}


/* IRQ Handler */
static void IRQHandler(void *param,int irq)
{
	ym3526_state *info = (ym3526_state *)param;
	info->out_int_func(irq ? ASSERT_LINE : CLEAR_LINE);
}
/* Timer overflow callback from timer.c */
static TIMER_CALLBACK( timer_callback_0 )
{
	ym3526_state *info = (ym3526_state *)ptr;
	ym3526_timer_over(info->chip,0);
}
static TIMER_CALLBACK( timer_callback_1 )
{
	ym3526_state *info = (ym3526_state *)ptr;
	ym3526_timer_over(info->chip,1);
}
/* TimerHandler from fm.c */
static void TimerHandler(void *param,int c,attotime period)
{
	ym3526_state *info = (ym3526_state *)param;
	if( period == attotime::zero )
	{	/* Reset FM Timer */
		info->timer[c]->enable(false);
	}
	else
	{	/* Start FM Timer */
		info->timer[c]->adjust(period);
	}
}


static STREAM_UPDATE( ym3526_stream_update )
{
	ym3526_state *info = (ym3526_state *)param;
	ym3526_update_one(info->chip, outputs[0], samples);
}

static void _stream_update(void *param, int interval)
{
	ym3526_state *info = (ym3526_state *)param;
	info->stream->update();
}


static DEVICE_START( ym3526 )
{
	static const ym3526_interface dummy = { DEVCB_NULL };
	ym3526_state *info = get_safe_token(device);
	int rate = device->clock()/72;

	info->intf = device->static_config() ? (const ym3526_interface *)device->static_config() : &dummy;
	info->device = device;

	// resolve callbacks
	info->out_int_func.resolve(info->intf->out_int_func, *device);

	/* stream system initialize */
	info->chip = ym3526_init(device,device->clock(),rate);
	assert_always(info->chip != NULL, "Error creating YM3526 chip");

	info->stream = device->machine().sound().stream_alloc(*device,0,1,rate,info,ym3526_stream_update);
	/* YM3526 setup */
	ym3526_set_timer_handler (info->chip, TimerHandler, info);
	ym3526_set_irq_handler   (info->chip, IRQHandler, info);
	ym3526_set_update_handler(info->chip, _stream_update, info);

	info->timer[0] = device->machine().scheduler().timer_alloc(FUNC(timer_callback_0), info);
	info->timer[1] = device->machine().scheduler().timer_alloc(FUNC(timer_callback_1), info);
}

static DEVICE_STOP( ym3526 )
{
	ym3526_state *info = get_safe_token(device);
	ym3526_shutdown(info->chip);
}

static DEVICE_RESET( ym3526 )
{
	ym3526_state *info = get_safe_token(device);
	ym3526_reset_chip(info->chip);
}


READ8_DEVICE_HANDLER( ym3526_r )
{
	ym3526_state *info = get_safe_token(device);
	return ym3526_read(info->chip, offset & 1);
}

WRITE8_DEVICE_HANDLER( ym3526_w )
{
	ym3526_state *info = get_safe_token(device);
	ym3526_write(info->chip, offset & 1, data);
}

READ8_DEVICE_HANDLER( ym3526_status_port_r ) { return ym3526_r(device, space, 0); }
READ8_DEVICE_HANDLER( ym3526_read_port_r ) { return ym3526_r(device, space, 1); }
WRITE8_DEVICE_HANDLER( ym3526_control_port_w ) { ym3526_w(device, space, 0, data); }
WRITE8_DEVICE_HANDLER( ym3526_write_port_w ) { ym3526_w(device, space, 1, data); }


const device_type YM3526 = &device_creator<ym3526_device>;

ym3526_device::ym3526_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
	: device_t(mconfig, YM3526, "YM3526", tag, owner, clock),
	  device_sound_interface(mconfig, *this)
{
	m_token = global_alloc_array_clear(UINT8, sizeof(ym3526_state));
}

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

void ym3526_device::device_config_complete()
{
}

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

void ym3526_device::device_start()
{
	DEVICE_START_NAME( ym3526 )(this);
}

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

void ym3526_device::device_reset()
{
	DEVICE_RESET_NAME( ym3526 )(this);
}

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

void ym3526_device::device_stop()
{
	DEVICE_STOP_NAME( ym3526 )(this);
}

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

void ym3526_device::sound_stream_update(sound_stream &stream, stream_sample_t **inputs, stream_sample_t **outputs, int samples)
{
	// should never get here
	fatalerror("sound_stream_update called; not applicable to legacy sound devices\n");
}