summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/audio/gridlee.c
blob: 132ad2bf0c09f1f69da10e4e18b90ef5d672ba99 (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
232
233
234
235
236
237
238
/*************************************************************************

    Basic Gridlee sound driver

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

#include "emu.h"
#include "includes/gridlee.h"
#include "sound/samples.h"


/*************************************
 *
 *  Structures
 *
 *************************************/

struct gridlee_sound_state
{
	/* tone variables */
	UINT32 m_tone_step;
	UINT32 m_tone_fraction;
	UINT8 m_tone_volume;

	/* sound streaming variables */
	sound_stream *m_stream;
	samples_device *m_samples;
	double m_freq_to_step;
	UINT8 m_sound_data[24];
};


/*************************************
 *
 *  Core sound generation
 *
 *************************************/

INLINE gridlee_sound_state *get_safe_token( device_t *device )
{
	assert(device != NULL);
	assert(device->type() == GRIDLEE);

	return (gridlee_sound_state *)downcast<gridlee_sound_device *>(device)->token();
}

static STREAM_UPDATE( gridlee_stream_update )
{
	gridlee_sound_state *state = get_safe_token(device);
	stream_sample_t *buffer = outputs[0];

	/* loop over samples */
	while (samples--)
	{
		/* tone channel */
		state->m_tone_fraction += state->m_tone_step;
		*buffer++ = (state->m_tone_fraction & 0x0800000) ? (state->m_tone_volume << 6) : 0;
	}
}



/*************************************
 *
 *  Sound startup routines
 *
 *************************************/

static DEVICE_START( gridlee_sound )
{
	gridlee_sound_state *state = get_safe_token(device);
	running_machine &machine = device->machine();

	/* allocate the stream */
	state->m_stream = device->machine().sound().stream_alloc(*device, 0, 1, machine.sample_rate(), NULL, gridlee_stream_update);

	state->m_samples = device->machine().device<samples_device>("samples");

	state->m_freq_to_step = (double)(1 << 24) / (double)machine.sample_rate();
}


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

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

		/* --- the following bits of info are returned as NULL-terminated strings --- */
		case DEVINFO_STR_NAME:							strcpy(info->s, "Gridlee Custom");				break;
		case DEVINFO_STR_SOURCE_FILE:						strcpy(info->s, __FILE__);						break;
	}
}



WRITE8_DEVICE_HANDLER( gridlee_sound_w )
{
	gridlee_sound_state *state = get_safe_token(device);
	UINT8 *sound_data = state->m_sound_data;
	samples_device *samples = state->m_samples;

	state->m_stream->update();

	switch (offset)
	{
		case 0x04:
			if (data == 0xef && sound_data[offset] != 0xef)
				samples->start(4, 1);
			else if (data != 0xef && sound_data[offset] == 0xef)
				samples->stop(4);
//          if (!(data & 0x01) && (sound_data[offset] & 0x01))
//              samples->start(5, 1);
//          else if ((data & 0x01) && !(sound_data[offset] & 0x01))
//              samples->stop(5);
			break;

		case 0x0c:
		case 0x0d:
		case 0x0e:
		case 0x0f:
			if ((data & 1) && !(sound_data[offset] & 1))
				samples->start(offset - 0x0c, 1 - sound_data[offset - 4]);
			else if (!(data & 1) && (sound_data[offset] & 1))
				samples->stop(offset - 0x0c);
			break;

		case 0x08+0x08:
			if (data)
				state->m_tone_step = state->m_freq_to_step * (double)(data * 5);
			else
				state->m_tone_step = 0;
			break;

		case 0x09+0x08:
			state->m_tone_volume = data;
			break;

		case 0x0b+0x08:
//          tone_volume = (data | sound_data[0x0c+0x08]) ? 0xff : 0x00;
			break;

		case 0x0c+0x08:
//          tone_volume = (data | sound_data[0x0b+0x08]) ? 0xff : 0x00;
			break;

		case 0x0d+0x08:
//          if (data)
//              tone_step = freq_to_step * (double)(data * 11);
//          else
//              tone_step = 0;
			break;
	}
	sound_data[offset] = data;



#if 0
{
static int first = 1;
FILE *f;
f = fopen("sound.log", first ? "w" : "a");
first = 0;
fprintf(f, "[%02x=%02x] %02x %02x %02x %02x %02x %02x %02x %02x - %02x %02x %02x %02x %02x %02x %02x %02x - %02x %02x %02x %02x %02x %02x %02x %02x\n",
	offset,data,
	sound_data[0],
	sound_data[1],
	sound_data[2],
	sound_data[3],
	sound_data[4],
	sound_data[5],
	sound_data[6],
	sound_data[7],
	sound_data[8],
	sound_data[9],
	sound_data[10],
	sound_data[11],
	sound_data[12],
	sound_data[13],
	sound_data[14],
	sound_data[15],
	sound_data[16],
	sound_data[17],
	sound_data[18],
	sound_data[19],
	sound_data[20],
	sound_data[21],
	sound_data[22],
	sound_data[23]);
fclose(f);
}
#endif
}


const device_type GRIDLEE = &device_creator<gridlee_sound_device>;

gridlee_sound_device::gridlee_sound_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
	: device_t(mconfig, GRIDLEE, "Gridlee Custom", tag, owner, clock),
	  device_sound_interface(mconfig, *this)
{
	m_token = global_alloc_array_clear(UINT8, sizeof(gridlee_sound_state));
}

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

void gridlee_sound_device::device_config_complete()
{
}

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

void gridlee_sound_device::device_start()
{
	DEVICE_START_NAME( gridlee_sound )(this);
}

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

void gridlee_sound_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");
}