summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/sound/cdp1863.c
blob: 47927f00e091f9e0a61c7ec6734c71aa1e3d452a (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
/**********************************************************************

    RCA CDP1863 CMOS 8-Bit Programmable Frequency Generator emulation

    Copyright MESS Team.
    Visit http://mamedev.org for licensing and usage restrictions.

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

/*

    TODO:

    - what happens if you connect both clocks?

*/

#include "driver.h"
#include "sndintrf.h"
#include "streams.h"
#include "cpu/cdp1802/cdp1802.h"
#include "cdp1863.h"

/***************************************************************************
    PARAMETERS
***************************************************************************/

#define CDP1863_DEFAULT_LATCH	0x35

/***************************************************************************
    TYPE DEFINITIONS
***************************************************************************/

typedef struct _cdp1863_t cdp1863_t;
struct _cdp1863_t
{
	int clock1;						/* clock 1 */
	int clock2;						/* clock 2 */

	sound_stream *stream;			/* sound output */

	/* sound state */
	int oe;							/* output enable */
	int latch;						/* sound latch */
	INT16 signal;					/* current signal */
	int incr;						/* initial wave state */
};

/***************************************************************************
    INLINE FUNCTIONS
***************************************************************************/

INLINE cdp1863_t *get_safe_token(const device_config *device)
{
	assert(device != NULL);
	assert(device->token != NULL);
	return (cdp1863_t *)device->token;
}

INLINE cdp1863_config *get_safe_config(const device_config *device)
{
	assert(device != NULL);
	return (cdp1863_config *)device->inline_config;
}

/***************************************************************************
    IMPLEMENTATION
***************************************************************************/

/*-------------------------------------------------
    cdp1863_str_w - tone latch write
-------------------------------------------------*/

WRITE8_DEVICE_HANDLER( cdp1863_str_w )
{
	cdp1863_t *cdp1863 = get_safe_token(device);

	cdp1863->latch = data;
}

/*-------------------------------------------------
    cdp1863_oe_w - output enable write
------------------------------------------------*/

WRITE_LINE_DEVICE_HANDLER( cdp1863_oe_w )
{
	cdp1863_t *cdp1863 = get_safe_token(device);

	cdp1863->oe = state;
}

/*-------------------------------------------------
    cdp1863_set_clk1 - set clock 1 frequency
------------------------------------------------*/

void cdp1863_set_clk1(const device_config *device, int frequency)
{
	cdp1863_t *cdp1863 = get_safe_token(device);

	cdp1863->clock1 = frequency;
}

/*-------------------------------------------------
    cdp1863_set_clk2 - set clock 2 frequency
------------------------------------------------*/

void cdp1863_set_clk2(const device_config *device, int frequency)
{
	cdp1863_t *cdp1863 = get_safe_token(device);

	cdp1863->clock2 = frequency;
}

/*-------------------------------------------------
    STREAM_UPDATE( cdp1863_stream_update )
-------------------------------------------------*/

static STREAM_UPDATE( cdp1863_stream_update )
{
	cdp1863_t *cdp1863 = get_safe_token(device);

	INT16 signal = cdp1863->signal;
	stream_sample_t *buffer = outputs[0];

	memset( buffer, 0, samples * sizeof(*buffer) );

	if (cdp1863->oe)
	{
		double frequency;
		int rate = device->machine->sample_rate / 2;

		/* get progress through wave */
		int incr = cdp1863->incr;

		if (cdp1863->clock1 > 0)
		{
			/* CLK1 is pre-divided by 4 */
			frequency = cdp1863->clock1 / 4 / (cdp1863->latch + 1) / 2;
		}
		else
		{
			/* CLK2 is pre-divided by 8 */
			frequency = cdp1863->clock2 / 8 / (cdp1863->latch + 1) / 2;
		}

		if (signal < 0)
		{
			signal = -0x7fff;
		}
		else
		{
			signal = 0x7fff;
		}

		while( samples-- > 0 )
		{
			*buffer++ = signal;
			incr -= frequency;
			while( incr < 0 )
			{
				incr += rate;
				signal = -signal;
			}
		}

		/* store progress through wave */
		cdp1863->incr = incr;
		cdp1863->signal = signal;
	}
}

/*-------------------------------------------------
    DEVICE_START( cdp1863 )
-------------------------------------------------*/

static DEVICE_START( cdp1863 )
{
	cdp1863_t *cdp1863 = get_safe_token(device);
	const cdp1863_config *config = get_safe_config(device);

	/* set initial values */
	cdp1863->stream = stream_create(device, 0, 1, device->machine->sample_rate, cdp1863, cdp1863_stream_update);
	cdp1863->clock1 = device->clock;
	cdp1863->clock2 = config->clock2;
	cdp1863->oe = 1;

	/* register for state saving */
	state_save_register_device_item(device, 0, cdp1863->clock1);
	state_save_register_device_item(device, 0, cdp1863->clock2);
	state_save_register_device_item(device, 0, cdp1863->oe);
	state_save_register_device_item(device, 0, cdp1863->latch);
	state_save_register_device_item(device, 0, cdp1863->signal);
	state_save_register_device_item(device, 0, cdp1863->incr);
}

/*-------------------------------------------------
    DEVICE_RESET( cdp1863 )
-------------------------------------------------*/

static DEVICE_RESET( cdp1863 )
{
	cdp1863_t *cdp1863 = get_safe_token(device);

	cdp1863->latch = CDP1863_DEFAULT_LATCH;
}

/*-------------------------------------------------
    DEVICE_GET_INFO( cdp1863 )
-------------------------------------------------*/

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

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

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