summaryrefslogtreecommitdiffstats
path: root/src/mame/audio/cps3.cpp
blob: 7c15634738caa76918834dc7f9f7c27470df7b35 (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
// license:BSD-3-Clause
// copyright-holders:David Haywood, Andreas Naive, Tomasz Slanina, ElSemi
/***************************************************************************

    Capcom CPS-3 Sound Hardware

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

#include "emu.h"
#include "cps3.h"


// device type definition
DEFINE_DEVICE_TYPE(CPS3, cps3_sound_device, "cps3_custom", "CPS3 Custom Sound")


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

//-------------------------------------------------
//  cps3_sound_device - constructor
//-------------------------------------------------

cps3_sound_device::cps3_sound_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: device_t(mconfig, CPS3, tag, owner, clock)
	, device_sound_interface(mconfig, *this)
	, m_stream(nullptr)
	, m_key(0)
	, m_base(nullptr)
{
}


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

void cps3_sound_device::device_start()
{
	/* Allocate the stream */
	m_stream = stream_alloc(0, 2, clock() / 384);

	save_item(NAME(m_key));
	for (int i = 0; i < 16; i++)
	{
		save_item(NAME(m_voice[i].regs), i);
		save_item(NAME(m_voice[i].pos), i);
		save_item(NAME(m_voice[i].frac), i);
	}
}


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

void cps3_sound_device::sound_stream_update(sound_stream &stream, stream_sample_t **inputs, stream_sample_t **outputs, int samples)
{
	/* Clear the buffers */
	memset(outputs[0], 0, samples*sizeof(*outputs[0]));
	memset(outputs[1], 0, samples*sizeof(*outputs[1]));

	for (int i = 0; i < 16; i ++)
	{
		if (m_key & (1 << i))
		{
			/* note: unmarked bits are presumed to have no function (always 0)
			0  -
			1  xxxxxxxx xxxxxxxx  -------- --------  start address low
			   -------- --------  xxxxxxxx xxxxxxxx  start address high
			2  -------- --------  -------- -------x  loop enable
			3  xxxxxxxx xxxxxxxx  -------- --------  frequency
			   -------- --------  xxxxxxxx xxxxxxxx  loop address low
			4  -------- --------  xxxxxxxx xxxxxxxx  loop address high
			5  xxxxxxxx xxxxxxxx  -------- --------  end address low (*)
			   -------- --------  xxxxxxxx xxxxxxxx  end address high
			6  xxxxxxxx xxxxxxxx  -------- --------  end address low (*)
			   -------- --------  xxxxxxxx xxxxxxxx  end address high
			7  xxxxxxxx xxxxxxxx  -------- --------  volume right (signed?)
			   -------- --------  xxxxxxxx xxxxxxxx  volume left (signed?)

			(*) reg 5 and 6 are always the same. One of them probably means loop-end address,
			    but we won't know which until we do tests on real hw.
			*/
			cps3_voice *vptr = &m_voice[i];

			uint32_t start = (vptr->regs[1] >> 16 & 0x0000ffff) | (vptr->regs[1] << 16 & 0xffff0000);
			uint32_t end   = (vptr->regs[5] >> 16 & 0x0000ffff) | (vptr->regs[5] << 16 & 0xffff0000);
			uint32_t loop  = (vptr->regs[3] & 0x0000ffff) | (vptr->regs[4] << 16 & 0xffff0000);
			bool loop_enable = (vptr->regs[2] & 1) ? true : false;
			uint32_t step  = vptr->regs[3] >> 16 & 0xffff;

			int16_t vol_l = (vptr->regs[7] & 0xffff);
			int16_t vol_r = (vptr->regs[7] >> 16 & 0xffff);

			uint32_t pos = vptr->pos;
			uint32_t frac = vptr->frac;

			/* TODO */
			start -= 0x400000;
			end -= 0x400000;
			loop -= 0x400000;

			/* Go through the buffer and add voice contributions */
			for (int j = 0; j < samples; j++)
			{
				int32_t sample;

				pos += (frac >> 12);
				frac &= 0xfff;

				if ((start + pos) >= end)
				{
					if (loop_enable)
					{
						// loop
						pos = (pos + loop) - end;
					}
					else
					{
						// sample end (don't force key off)
						break;
					}
				}

				sample = m_base[BYTE4_XOR_LE(start + pos)];
				frac += step;

				outputs[0][j] += ((sample * vol_l) >> 8);
				outputs[1][j] += ((sample * vol_r) >> 8);
			}

			vptr->pos = pos;
			vptr->frac = frac;
		}
	}
}


WRITE32_MEMBER( cps3_sound_device::cps3_sound_w )
{
	m_stream->update();

	if (offset < 0x80)
	{
		COMBINE_DATA(&m_voice[offset / 8].regs[offset & 7]);
	}
	else if (offset == 0x80)
	{
		assert((mem_mask & 0xffff0000) == 0xffff0000); // doesn't happen

		uint16_t key = data >> 16;

		for (int i = 0; i < 16; i++)
		{
			// Key off -> Key on
			if ((key & (1 << i)) && !(m_key & (1 << i)))
			{
				m_voice[i].frac = 0;
				m_voice[i].pos = 0;
			}
		}
		m_key = key;
	}
	else
	{
		// during boot: cps3_sound_w [84] = 230000
		logerror("cps3_sound_w [%x] = %x & %x\n", offset, data, mem_mask);
	}
}


READ32_MEMBER( cps3_sound_device::cps3_sound_r )
{
	m_stream->update();

	if (offset < 0x80)
	{
		return m_voice[offset / 8].regs[offset & 7] & mem_mask;
	}
	else if (offset == 0x80)
	{
		return m_key << 16;
	}
	else
	{
		logerror("cps3_sound_r unknown %x & %x\n", offset, mem_mask);
		return 0;
	}
}