Debian Codegraph @

src/engine/platform/sound/oki/okim6258.cpp

raw
1 // license:BSD-3-Clause
2 // copyright-holders:Barry Rodewald
3 /**********************************************************************************************
4 *
5 * OKI MSM6258 ADPCM
6 *
7 * TODO:
8 * 3-bit ADPCM support
9 * Recording?
10 *
11 **********************************************************************************************/
12
13
14 #include "okim6258.h"
15 #include <stdio.h>
16 #include <string.h>
17 #include <math.h>
18
19 #define COMMAND_STOP (1 << 0)
20 #define COMMAND_PLAY (1 << 1)
21 #define COMMAND_RECORD (1 << 2)
22
23 #define STATUS_PLAYING (1 << 1)
24 #define STATUS_RECORDING (1 << 2)
25
26 static const int dividers[4] = { 1024, 768, 512, 512 };
27
28 /* step size index shift table */
29 static const int index_shift[8] = { -1, -1, -1, -1, 2, 4, 6, 8 };
30
31 /* lookup table for the precomputed difference */
32 static int diff_lookup[49*16];
33
34 /* tables computed? */
35 static int tables_computed = 0;
36
37
38
39
40 //**************************************************************************
41 // LIVE DEVICE
42 //**************************************************************************
43
44 //-------------------------------------------------
45 // okim6258_device - constructor
46 //-------------------------------------------------
47
48 okim6258_device::okim6258_device(uint32_t clock)
49 :
50 m_status(0),
51 m_start_divider(0),
52 m_divider(512),
53 m_adpcm_type(0),
54 m_data_in(0),
55 m_nibble_shift(0),
56 m_output_bits(0),
57 m_signal(0),
58 m_step(0),
59 m_clock(clock)
60 {
61 }
62
63
64
65 /**********************************************************************************************
66
67 compute_tables -- compute the difference tables
68
69 ***********************************************************************************************/
70
71 static void compute_tables()
72 {
73 /* nibble to bit map */
74 static const int nbl2bit[16][4] =
75 {
76 { 1, 0, 0, 0}, { 1, 0, 0, 1}, { 1, 0, 1, 0}, { 1, 0, 1, 1},
77 { 1, 1, 0, 0}, { 1, 1, 0, 1}, { 1, 1, 1, 0}, { 1, 1, 1, 1},
78 {-1, 0, 0, 0}, {-1, 0, 0, 1}, {-1, 0, 1, 0}, {-1, 0, 1, 1},
79 {-1, 1, 0, 0}, {-1, 1, 0, 1}, {-1, 1, 1, 0}, {-1, 1, 1, 1}
80 };
81
82 int step, nib;
83
84 /* loop over all possible steps */
85 for (step = 0; step <= 48; step++)
86 {
87 /* compute the step value */
88 int stepval = floor(16.0 * pow(11.0 / 10.0, (double)step));
89
90 /* loop over all nibbles and compute the difference */
91 for (nib = 0; nib < 16; nib++)
92 {
93 diff_lookup[step*16 + nib] = nbl2bit[nib][0] *
94 (stepval * nbl2bit[nib][1] +
95 stepval/2 * nbl2bit[nib][2] +
96 stepval/4 * nbl2bit[nib][3] +
97 stepval/8);
98 }
99 }
100
101 tables_computed = 1;
102 }
103
104
105 //-------------------------------------------------
106 // device_start - device-specific startup
107 //-------------------------------------------------
108
109 void okim6258_device::device_start()
110 {
111 compute_tables();
112
113 m_divider = dividers[m_start_divider];
114
115 m_signal = -2;
116 m_step = 0;
117 m_has_data = false;
118 }
119
120
121 //-------------------------------------------------
122 // device_reset - device-specific reset
123 //-------------------------------------------------
124
125 void okim6258_device::device_reset()
126 {
127 m_signal = -2;
128 m_step = 0;
129 m_status = 0;
130 m_has_data = false;
131 }
132
133
134 //-------------------------------------------------
135 // sound_stream_update - handle a stream update
136 //-------------------------------------------------
137
138 void okim6258_device::sound_stream_update(short* output, int len)
139 {
140 short* buffer = output;
141
142 if (m_status & STATUS_PLAYING)
143 {
144 int nibble_shift = m_nibble_shift;
145
146 for (int sampindex = 0; sampindex < len; sampindex++)
147 {
148 /* Compute the new amplitude and update the current step */
149 int nibble = (m_data_in >> nibble_shift) & 0xf;
150
151 /* Output to the buffer */
152 int16_t sample = clock_adpcm(nibble);
153
154 nibble_shift ^= 4;
155
156 if (nibble_shift==0) m_has_data=false;
157
158 buffer[sampindex]=sample;
159 }
160
161 /* Update the parameters */
162 m_nibble_shift = nibble_shift;
163 }
164 else
165 {
166 memset(buffer,0,len*sizeof(short));
167 }
168 }
169
170
171
172 int16_t okim6258_device::clock_adpcm(uint8_t nibble)
173 {
174 int32_t max = (1 << (m_output_bits - 1)) - 1;
175 int32_t min = -(1 << (m_output_bits - 1));
176
177 m_signal += diff_lookup[m_step * 16 + (nibble & 15)];
178
179 /* clamp to the maximum */
180 if (m_signal > max)
181 m_signal = max;
182 else if (m_signal < min)
183 m_signal = min;
184
185 /* adjust the step size and clamp */
186 m_step += index_shift[nibble & 7];
187 if (m_step > 48)
188 m_step = 48;
189 else if (m_step < 0)
190 m_step = 0;
191
192 /* return the signal scaled up to 32767 */
193 return m_signal << 4;
194 }
195
196
197 /**********************************************************************************************
198
199 okim6258::set_divider -- set the master clock divider
200
201 ***********************************************************************************************/
202
203 void okim6258_device::set_divider(int val)
204 {
205 m_divider = dividers[val];
206 }
207
208
209 /**********************************************************************************************
210
211 okim6258::set_clock -- set the master clock
212
213 ***********************************************************************************************/
214
215 void okim6258_device::device_clock_changed()
216 {
217 }
218
219
220 /**********************************************************************************************
221
222 okim6258::get_vclk -- get the VCLK/sampling frequency
223
224 ***********************************************************************************************/
225
226 int okim6258_device::get_vclk()
227 {
228 return (m_clock / m_divider);
229 }
230
231
232 /**********************************************************************************************
233
234 okim6258_status_r -- read the status port of an OKIM6258-compatible chip
235
236 ***********************************************************************************************/
237
238 uint8_t okim6258_device::status_r()
239 {
240 return (m_status & STATUS_PLAYING) ? 0x00 : 0x80;
241 }
242
243
244 /**********************************************************************************************
245
246 okim6258_data_w -- write to the control port of an OKIM6258-compatible chip
247
248 ***********************************************************************************************/
249 bool okim6258_device::data_w(uint8_t data)
250 {
251 if (m_has_data) return false;
252 m_data_in = data;
253 m_nibble_shift = 0;
254 m_has_data = true;
255 return true;
256 }
257
258
259 /**********************************************************************************************
260
261 okim6258_ctrl_w -- write to the control port of an OKIM6258-compatible chip
262
263 ***********************************************************************************************/
264
265 void okim6258_device::ctrl_w(uint8_t data)
266 {
267 if (data & COMMAND_STOP)
268 {
269 m_status &= ~(STATUS_PLAYING | STATUS_RECORDING);
270 m_has_data = false;
271 return;
272 }
273
274 if (data & COMMAND_PLAY)
275 {
276 if (!(m_status & STATUS_PLAYING))
277 {
278 m_status |= STATUS_PLAYING;
279
280 /* Also reset the ADPCM parameters */
281 m_signal = -2;
282 m_step = 0;
283 m_nibble_shift = 0;
284 }
285 }
286 else
287 {
288 m_status &= ~STATUS_PLAYING;
289 }
290
291 if (data & COMMAND_RECORD)
292 {
293 //logerror("M6258: Record enabled\n");
294 m_status |= STATUS_RECORDING;
295 }
296 else
297 {
298 m_status &= ~STATUS_RECORDING;
299 }
300 }