summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/audio/flower.cpp
blob: 99806a0c6b69b58bd9fa855dfded1b6e6e2fdd8c (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// license:BSD-3-Clause
// copyright-holders:Angelo Salese
/***************************************************************************

    Flower custom sound chip

    Similar to Wiping and Namco 15xx designs

    TODO:
    - several unknown registers (effects and unknown register tied to repeat port);
    - repeat certainly needs a cutoff, which is unknown about how it works;

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

#include "emu.h"
#include "flower.h"


//**************************************************************************
//  GLOBAL VARIABLES
//**************************************************************************

// device type definition
DEFINE_DEVICE_TYPE(FLOWER_CUSTOM, flower_sound_device, "flower_sound", "Flower Custom Sound")

// TODO: select() unsupported by DEVICE_ADDRESS_MAP, so we need a trampoline here
void flower_sound_device::regs_map(address_map &map)
{
	map(0x00, 0x03).select(0x38).w(FUNC(flower_sound_device::frequency_w));
	map(0x04, 0x04).select(0x38).w(FUNC(flower_sound_device::repeat_w));
	map(0x05, 0x05).select(0x38).w(FUNC(flower_sound_device::unk_w));
	map(0x07, 0x07).select(0x38).w(FUNC(flower_sound_device::volume_w));
	map(0x40, 0x45).select(0x38).w(FUNC(flower_sound_device::start_address_w));
	map(0x47, 0x47).select(0x38).w(FUNC(flower_sound_device::sample_trigger_w));
}

//**************************************************************************
//  LIVE DEVICE
//**************************************************************************

//-------------------------------------------------
//  flower_sound_device - constructor
//-------------------------------------------------

flower_sound_device::flower_sound_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: device_t(mconfig, FLOWER_CUSTOM, tag, owner, clock),
	  device_sound_interface(mconfig, *this),
	  device_memory_interface(mconfig, *this),
	  m_io_space_config("io", ENDIANNESS_LITTLE, 8, 7, 0, address_map_constructor(FUNC(flower_sound_device::regs_map), this)),
	  m_stream(nullptr),
	  m_mixer_lookup(nullptr),
	  m_last_channel(nullptr)
{
}


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

void flower_sound_device::device_start()
{
	m_iospace = &space(AS_IO);
	m_stream = stream_alloc(0, 1, clock()/2);

	m_mixer_buffer.resize(clock()/50);
	make_mixer_table(MAX_VOICES, defgain);

	m_last_channel = m_channel_list + MAX_VOICES;

	m_sample_rom = machine().root_device().memregion("samples")->base();
	m_volume_rom = machine().root_device().memregion("soundvol")->base();

	for (int i = 0; i < MAX_VOICES; i++)
	{
		save_item(NAME(m_channel_list[i].start_address), i);
		save_item(NAME(m_channel_list[i].position), i);
		save_item(NAME(m_channel_list[i].frequency), i);
		save_item(NAME(m_channel_list[i].volume), i);
		save_item(NAME(m_channel_list[i].volume_bank), i);
		save_item(NAME(m_channel_list[i].effect), i);
		save_item(NAME(m_channel_list[i].enable), i);
		save_item(NAME(m_channel_list[i].repeat), i);

		// assign a channel number (debugger aid)
		m_channel_list[i].channel_number = i;
	}
}


/* build a table to divide by the number of voices; gain is specified as gain*16 */
void flower_sound_device::make_mixer_table(int voices, int gain)
{
	/* allocate memory */
	m_mixer_table.resize(256 * voices);

	/* find the middle of the table */
	m_mixer_lookup = &m_mixer_table[128 * voices];

	/* fill in the table - 16 bit case */
	for (int i = 0; i < voices * 128; i++)
	{
		int val = i * gain * 16 / voices;
		if (val > 32767) val = 32767;
		m_mixer_lookup[ i] = val;
		m_mixer_lookup[-i] = -val;
	}
}



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

void flower_sound_device::device_reset()
{
	for (fl_sound_channel *voice = m_channel_list; voice < m_last_channel; voice++)
	{
		voice->start_address = 0;
		voice->position = 0;
		voice->volume = 0;
		voice->enable = false;
		voice->repeat = false;
	}
}

void flower_sound_device::sound_stream_update(sound_stream &stream, std::vector<read_stream_view> const &inputs, std::vector<write_stream_view> &outputs)
{
	auto &buffer = outputs[0];
	short *mix;
	uint8_t raw_sample;

	std::fill_n(&m_mixer_buffer[0], buffer.samples(), 0);

	for (fl_sound_channel *voice = m_channel_list; voice < m_last_channel; voice++)
	{
		int ch_volume = voice->volume;
		int ch_frequency = voice->frequency;

		if (voice->enable == false)
			continue;

		mix = &m_mixer_buffer[0];

		for (int i = 0; i < buffer.samples(); i++)
		{
			if (voice->repeat == true)
			{
				raw_sample = m_sample_rom[((voice->start_address >> 7) & 0x7e00) | ((voice->position >> 7) & 0x1ff)];
				// guess: cut off after a number of repetitions
				if ((voice->position >> 7) & 0x20000)
				{
					voice->enable = false;
					break;
				}
			}
			else
			{
				raw_sample = m_sample_rom[((voice->start_address + voice->position) >> 7) & 0x7fff];
				if (raw_sample == 0xff)
				{
					voice->enable = false;
					break;
				}
			}
			ch_volume |= voice->volume_bank;

			*mix++ += m_volume_rom[(ch_volume << 8 | raw_sample) & 0x3fff] - 0x80;
			voice->position += ch_frequency;
		}
	}

	/* mix it down */
	mix = &m_mixer_buffer[0];
	for (int i = 0; i < buffer.samples(); i++)
		buffer.put_int(i, m_mixer_lookup[*mix++], 32768);
}

//-------------------------------------------------
//  memory_space_config - return a description of
//  any address spaces owned by this device
//-------------------------------------------------

device_memory_interface::space_config_vector flower_sound_device::memory_space_config() const
{
	return space_config_vector {
		std::make_pair(AS_IO, &m_io_space_config)
	};
}

//**************************************************************************
//  READ/WRITE HANDLERS
//**************************************************************************

void flower_sound_device::lower_write(offs_t offset, uint8_t data)
{
	m_stream->update();
	m_iospace->write_byte(offset,data);
}

void flower_sound_device::upper_write(offs_t offset, uint8_t data)
{
	m_stream->update();
	m_iospace->write_byte(offset|0x40,data);
}

void flower_sound_device::frequency_w(offs_t offset, uint8_t data)
{
	uint8_t ch = (offset >> 3) & 0x7;
	fl_sound_channel *voice;

	voice = &m_channel_list[ch];

	voice->raw_frequency[offset & 3] = data & 0xf;

	voice->frequency = voice->raw_frequency[2] << 12;
	voice->frequency|= voice->raw_frequency[3] << 8;
	voice->frequency|= voice->raw_frequency[0] << 4;
	voice->frequency|= voice->raw_frequency[1] << 0;
}

void flower_sound_device::repeat_w(offs_t offset, uint8_t data)
{
	uint8_t ch = (offset >> 3) & 0x7;
	fl_sound_channel *voice;

	voice = &m_channel_list[ch];
	voice->repeat = BIT(data,4);
}

void flower_sound_device::unk_w(offs_t offset, uint8_t data)
{
	// same as above?
}

void flower_sound_device::volume_w(offs_t offset, uint8_t data)
{
	uint8_t ch = (offset >> 3) & 0x7;
	fl_sound_channel *voice;

	voice = &m_channel_list[ch];
	voice->volume = data >> 4;
}

void flower_sound_device::start_address_w(offs_t offset, uint8_t data)
{
	uint8_t ch = (offset >> 3) & 0x7;
	fl_sound_channel *voice;

	voice = &m_channel_list[ch];
	voice->start_nibbles[offset & 7] = data & 0xf;
	if ((offset & 7) == 4)
		voice->effect = data >> 4;
}

void flower_sound_device::sample_trigger_w(offs_t offset, uint8_t data)
{
	uint8_t ch = (offset >> 3) & 0x7;
	fl_sound_channel *voice;

	voice = &m_channel_list[ch];

	voice->enable = true;
	voice->volume_bank = (data & 3) << 4;
	voice->start_address = 0;
	voice->position = 0;
	for (int i = 5; i >= 0; i--)
	{
		voice->start_address = (voice->start_address << 4) | voice->start_nibbles[i];
	}
}