Debian Codegraph @

src/engine/platform/sound/rf5c68.cpp

raw
1 // license:BSD-3-Clause
2 // copyright-holders:Olivier Galibert,Aaron Giles
3 /*********************************************************/
4 /* ricoh RF5C68(or clone) PCM controller */
5 /* */
6 /* TODO: Verify RF5C105,164 (Sega CD/Mega CD) */
7 /* differences */
8 /*********************************************************/
9
10 #include "rf5c68.h"
11 #include <algorithm>
12
13
14 //**************************************************************************
15 // LIVE DEVICE
16 //**************************************************************************
17
18 //-------------------------------------------------
19 // rf5c68_device - constructor
20 //-------------------------------------------------
21
22 rf5c68_device::rf5c68_device(int output_bits)
23 : m_cbank(0)
24 , m_wbank(0)
25 , m_enable(0)
26 , m_output_bits(output_bits)
27 , m_ext_mem(nullptr)
28 {
29 }
30
31 rf5c68_device::rf5c68_device()
32 : rf5c68_device(10)
33 {
34 }
35
36
37 //-------------------------------------------------
38 // rf5c164_device - constructor
39 //-------------------------------------------------
40
41 rf5c164_device::rf5c164_device()
42 : rf5c68_device(16)
43 {
44 }
45
46
47 //-------------------------------------------------
48 // device_start - device-specific startup
49 //-------------------------------------------------
50
51 void rf5c68_device::device_start(u8 *ext_mem)
52 {
53 m_ext_mem = ext_mem;
54 }
55
56 void rf5c68_device::device_reset()
57 {
58 m_cbank = 0;
59 m_wbank = 0;
60 m_enable = 0;
61
62 for (pcm_channel &chan : m_chan) {
63 chan.enable = 0;
64 chan.env = 0;
65 chan.pan = 0;
66 chan.start = 0;
67 chan.addr = 0;
68 chan.step = 0;
69 chan.loopst = 0;
70 }
71 }
72
73 //-------------------------------------------------
74 // sound_stream_update - handle a stream update
75 //-------------------------------------------------
76
77 void rf5c68_device::sound_stream_update(s16 **outputs, s16 **channel_outputs, u32 samples)
78 {
79 /* bail if not enabled */
80 if (!m_enable)
81 {
82 std::fill_n(outputs[0], samples, 0);
83 std::fill_n(outputs[1], samples, 0);
84 for (unsigned int i = 0; i < NUM_CHANNELS; i++) {
85 std::fill_n(channel_outputs[i*2], samples, 0);
86 std::fill_n(channel_outputs[i*2+1], samples, 0);
87 }
88 return;
89 }
90
91 if (m_mixleft.size() < samples)
92 m_mixleft.resize(samples);
93 if (m_mixright.size() < samples)
94 m_mixright.resize(samples);
95
96 std::fill_n(&m_mixleft[0], samples, 0);
97 std::fill_n(&m_mixright[0], samples, 0);
98
99 /* loop over channels */
100 for (unsigned int i = 0; i < NUM_CHANNELS; i++)
101 {
102 pcm_channel &chan = m_chan[i];
103 /* if this channel is active, accumulate samples */
104 if (chan.enable)
105 {
106 int lv = (chan.pan & 0x0f) * chan.env;
107 int rv = ((chan.pan >> 4) & 0x0f) * chan.env;
108
109 /* loop over the sample buffer */
110 for (u32 j = 0; j < samples; j++)
111 {
112 int sample;
113
114 /* fetch the sample and handle looping */
115 sample = m_ext_mem[(chan.addr >> 11) & 0xffff];
116 if (sample == 0xff)
117 {
118 chan.addr = chan.loopst << 11;
119 sample = m_ext_mem[(chan.addr >> 11) & 0xffff];
120
121 /* if we loop to a loop point, we're effectively dead */
122 if (sample == 0xff)
123 break;
124 }
125 chan.addr += chan.step;
126
127 /* add to the buffer */
128 s32 left_out = ((sample & 0x7f) * lv) >> 5;
129 s32 right_out = ((sample & 0x7f) * rv) >> 5;
130 if ((sample & 0x80) == 0)
131 {
132 left_out = -left_out;
133 right_out = -right_out;
134 }
135 channel_outputs[i*2][j] = (s16)left_out;
136 channel_outputs[i*2+1][j] = (s16)right_out;
137 m_mixleft[j] += left_out;
138 m_mixright[j] += right_out;
139 }
140 }
141 else
142 {
143 std::fill_n(channel_outputs[i*2], samples, 0);
144 std::fill_n(channel_outputs[i*2+1], samples, 0);
145 }
146 }
147
148 /*
149 now clamp and shift the result (output is only 10 bits for RF5C68, 16 bits for RF5C164)
150 reference: Mega CD hardware manual, RF5C68 datasheet
151 */
152 const u8 output_shift = (m_output_bits > 16) ? 0 : (16 - m_output_bits);
153 const s32 output_nandmask = (1 << output_shift) - 1;
154 for (u32 j = 0; j < samples; j++)
155 {
156 s32 outleft = std::min(std::max(m_mixleft[j], -32768), 32767);
157 s32 outright = std::min(std::max(m_mixright[j], -32768), 32767);
158 outputs[0][j] = outleft & ~output_nandmask;
159 outputs[1][j] = outright & ~output_nandmask;
160 }
161 }
162
163
164 //-------------------------------------------------
165 // RF5C68 write register
166 //-------------------------------------------------
167
168 // TODO: RF5C164 only?
169 u8 rf5c68_device::rf5c68_r(offs_t offset)
170 {
171 u8 shift;
172
173 shift = (offset & 1) ? 11 + 8 : 11;
174
175 // printf("%08x\n",(m_chan[(offset & 0x0e) >> 1].addr));
176
177 return (m_chan[(offset & 0x0e) >> 1].addr) >> (shift);
178 }
179
180 void rf5c68_device::rf5c68_w(offs_t offset, u8 data)
181 {
182 pcm_channel &chan = m_chan[m_cbank];
183 int i;
184
185 /* switch off the address */
186 switch (offset)
187 {
188 case 0x00: /* envelope */
189 chan.env = data;
190 break;
191
192 case 0x01: /* pan */
193 chan.pan = data;
194 break;
195
196 case 0x02: /* FDL */
197 chan.step = (chan.step & 0xff00) | (data & 0x00ff);
198 break;
199
200 case 0x03: /* FDH */
201 chan.step = (chan.step & 0x00ff) | ((data << 8) & 0xff00);
202 break;
203
204 case 0x04: /* LSL */
205 chan.loopst = (chan.loopst & 0xff00) | (data & 0x00ff);
206 break;
207
208 case 0x05: /* LSH */
209 chan.loopst = (chan.loopst & 0x00ff) | ((data << 8) & 0xff00);
210 break;
211
212 case 0x06: /* ST */
213 chan.start = data;
214 if (!chan.enable)
215 chan.addr = chan.start << (8 + 11);
216 break;
217
218 case 0x07: /* control reg */
219 m_enable = (data >> 7) & 1;
220 if (data & 0x40)
221 m_cbank = data & 7;
222 else
223 m_wbank = (data & 0xf) << 12;
224 break;
225
226 case 0x08: /* channel on/off reg */
227 for (i = 0; i < 8; i++)
228 {
229 m_chan[i].enable = (~data >> i) & 1;
230 if (!m_chan[i].enable)
231 m_chan[i].addr = m_chan[i].start << (8 + 11);
232 }
233 break;
234 }
235 }
236
237
238 //-------------------------------------------------
239 // RF5C68 read memory
240 //-------------------------------------------------
241
242 u8 rf5c68_device::rf5c68_mem_r(offs_t offset)
243 {
244 return m_ext_mem[m_wbank | offset];
245 }
246
247
248 //-------------------------------------------------
249 // RF5C68 write memory
250 //-------------------------------------------------
251
252 void rf5c68_device::rf5c68_mem_w(offs_t offset, u8 data)
253 {
254 m_ext_mem[m_wbank | offset] = data;
255 }