summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/sound/dac.c
blob: 04febd88d4063b5e1c31b08a134b944ceacef781 (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
#include "emu.h"
#include "streams.h"
#include "dac.h"


/* default to 4x oversampling */
#define DEFAULT_SAMPLE_RATE (48000 * 4)


typedef struct _dac_state dac_state;
struct _dac_state
{
	sound_stream	*channel;
	INT16			output;
	INT16			UnsignedVolTable[256];
	INT16			SignedVolTable[256];
};


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


static STREAM_UPDATE( DAC_update )
{
	dac_state *info = (dac_state *)param;
	stream_sample_t *buffer = outputs[0];
	INT16 out = info->output;

	while (samples--) *(buffer++) = out;
}


void dac_data_w(running_device *device, UINT8 data)
{
	dac_state *info = get_safe_token(device);
	INT16 out = info->UnsignedVolTable[data];

	if (info->output != out)
	{
		/* update the output buffer before changing the registers */
		stream_update(info->channel);
		info->output = out;
	}
}


void dac_signed_data_w(running_device *device, UINT8 data)
{
	dac_state *info = get_safe_token(device);
	INT16 out = info->SignedVolTable[data];

	if (info->output != out)
	{
		/* update the output buffer before changing the registers */
		stream_update(info->channel);
		info->output = out;
	}
}


void dac_data_16_w(running_device *device, UINT16 data)
{
	dac_state *info = get_safe_token(device);
	INT16 out = data >> 1;		/* range      0..32767 */

	if (info->output != out)
	{
		/* update the output buffer before changing the registers */
		stream_update(info->channel);
		info->output = out;
	}
}


void dac_signed_data_16_w(running_device *device, UINT16 data)
{
	dac_state *info = get_safe_token(device);
	INT16 out = (INT32)data - (INT32)0x08000;	/* range -32768..32767 */
						/* casts avoid potential overflow on some ABIs */

	if (info->output != out)
	{
		/* update the output buffer before changing the registers */
		stream_update(info->channel);
		info->output = out;
	}
}


static void DAC_build_voltable(dac_state *info)
{
	int i;

	/* build volume table (linear) */
	for (i = 0;i < 256;i++)
	{
		info->UnsignedVolTable[i] = i * 0x101 / 2;	/* range      0..32767 */
		info->SignedVolTable[i] = i * 0x101 - 0x8000;	/* range -32768..32767 */
	}
}


static DEVICE_START( dac )
{
	dac_state *info = get_safe_token(device);

	DAC_build_voltable(info);

	info->channel = stream_create(device,0,1,device->clock() ? device->clock() : DEFAULT_SAMPLE_RATE,info,DAC_update);
	info->output = 0;

	state_save_register_device_item(device, 0, info->output);
}



WRITE8_DEVICE_HANDLER( dac_w )
{
	dac_data_w(device, data);
}

WRITE8_DEVICE_HANDLER( dac_signed_w )
{
	dac_signed_data_w(device, data);
}



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

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

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

		/* --- the following bits of info are returned as NULL-terminated strings --- */
		case DEVINFO_STR_NAME:							strcpy(info->s, "DAC");						break;
		case DEVINFO_STR_FAMILY:					strcpy(info->s, "DAC");						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;
	}
}