summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/sound/2151intf.c
blob: 996dafd1eea5000f19e96d71e03033f60b420cf9 (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
/***************************************************************************

  2151intf.c

  Support interface YM2151(OPM)

***************************************************************************/

#include "emu.h"
#include "streams.h"
#include "fm.h"
#include "2151intf.h"
#include "ym2151.h"


typedef struct _ym2151_state ym2151_state;
struct _ym2151_state
{
	sound_stream *			stream;
	emu_timer *				timer[2];
	void *					chip;
	UINT8					lastreg;
	const ym2151_interface *intf;
};


INLINE ym2151_state *get_safe_token(running_device *device)
{
	assert(device != NULL);
	assert(device->type() == SOUND_YM2151);
	return (ym2151_state *)downcast<legacy_device_base *>(device)->token();
}


static STREAM_UPDATE( ym2151_update )
{
	ym2151_state *info = (ym2151_state *)param;
	ym2151_update_one(info->chip, outputs, samples);
}


static STATE_POSTLOAD( ym2151intf_postload )
{
	ym2151_state *info = (ym2151_state *)param;
	ym2151_postload(machine, info->chip);
}


static DEVICE_START( ym2151 )
{
	static const ym2151_interface dummy = { 0 };
	ym2151_state *info = get_safe_token(device);
	int rate;

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

	rate = device->clock()/64;

	/* stream setup */
	info->stream = stream_create(device,0,2,rate,info,ym2151_update);

	info->chip = ym2151_init(device,device->clock(),rate);
	assert_always(info->chip != NULL, "Error creating YM2151 chip");

	state_save_register_postload(device->machine, ym2151intf_postload, info);

	ym2151_set_irq_handler(info->chip,info->intf->irqhandler);
	ym2151_set_port_write_handler(info->chip,info->intf->portwritehandler);
}


static DEVICE_STOP( ym2151 )
{
	ym2151_state *info = get_safe_token(device);
	ym2151_shutdown(info->chip);
}

static DEVICE_RESET( ym2151 )
{
	ym2151_state *info = get_safe_token(device);
	ym2151_reset_chip(info->chip);
}


READ8_DEVICE_HANDLER( ym2151_r )
{
	ym2151_state *token = get_safe_token(device);

	if (offset & 1)
	{
		stream_update(token->stream);
		return ym2151_read_status(token->chip);
	}
	else
		return 0xff;	/* confirmed on a real YM2151 */
}

WRITE8_DEVICE_HANDLER( ym2151_w )
{
	ym2151_state *token = get_safe_token(device);

	if (offset & 1)
	{
		stream_update(token->stream);
		ym2151_write_reg(token->chip, token->lastreg, data);
	}
	else
		token->lastreg = data;
}


READ8_DEVICE_HANDLER( ym2151_status_port_r ) { return ym2151_r(device, 1); }

WRITE8_DEVICE_HANDLER( ym2151_register_port_w ) { ym2151_w(device, 0, data); }
WRITE8_DEVICE_HANDLER( ym2151_data_port_w ) { ym2151_w(device, 1, data); }



/**************************************************************************
 * Generic get_info
 **************************************************************************/

DEVICE_GET_INFO( ym2151 )
{
	switch (state)
	{
		/* --- the following bits of info are returned as 64-bit signed integers --- */
		case DEVINFO_INT_TOKEN_BYTES:					info->i = sizeof(ym2151_state);					break;

		/* --- the following bits of info are returned as pointers to data or functions --- */
		case DEVINFO_FCT_START:							info->start = DEVICE_START_NAME( ym2151 );		break;
		case DEVINFO_FCT_STOP:							info->stop = DEVICE_STOP_NAME( ym2151 );		break;
		case DEVINFO_FCT_RESET:							info->reset = DEVICE_RESET_NAME( ym2151 );		break;

		/* --- the following bits of info are returned as NULL-terminated strings --- */
		case DEVINFO_STR_NAME:							strcpy(info->s, "YM2151");						break;
		case DEVINFO_STR_FAMILY:					strcpy(info->s, "Yamaha FM");					break;
		case DEVINFO_STR_VERSION:					strcpy(info->s, "1.0");							break;
		case DEVINFO_STR_SOURCE_FILE:						strcpy(info->s, __FILE__);						break;
		case DEVINFO_STR_CREDITS:					strcpy(info->s, "Copyright Nicola Salmoria and the MAME Team"); break;
	}
}


DEFINE_LEGACY_SOUND_DEVICE(YM2151, ym2151);