Debian Codegraph @

src/engine/platform/sound/ymfm/ymfm_adpcm.h

raw
1 // BSD 3-Clause License
2 //
3 // Copyright (c) 2021, Aaron Giles
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 //
9 // 1. Redistributions of source code must retain the above copyright notice, this
10 // list of conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright notice,
13 // this list of conditions and the following disclaimer in the documentation
14 // and/or other materials provided with the distribution.
15 //
16 // 3. Neither the name of the copyright holder nor the names of its
17 // contributors may be used to endorse or promote products derived from
18 // this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 #ifndef YMFM_ADPCM_H
32 #define YMFM_ADPCM_H
33
34 #pragma once
35
36 #include "ymfm.h"
37
38 namespace ymfm
39 {
40
41 //*********************************************************
42 // INTERFACE CLASSES
43 //*********************************************************
44
45 // forward declarations
46 class adpcm_a_engine;
47 class adpcm_b_engine;
48
49
50 // ======================> adpcm_a_registers
51
52 //
53 // ADPCM-A register map:
54 //
55 // System-wide registers:
56 // 00 x------- Dump (disable=1) or keyon (0) control
57 // --xxxxxx Mask of channels to dump or keyon
58 // 01 --xxxxxx Total level
59 // 02 xxxxxxxx Test register
60 // 08-0D x------- Pan left
61 // -x------ Pan right
62 // ---xxxxx Instrument level
63 // 10-15 xxxxxxxx Start address (low)
64 // 18-1D xxxxxxxx Start address (high)
65 // 20-25 xxxxxxxx End address (low)
66 // 28-2D xxxxxxxx End address (high)
67 //
68 class adpcm_a_registers
69 {
70 public:
71 // constants
72 static constexpr uint32_t OUTPUTS = 2;
73 static constexpr uint32_t CHANNELS = 6;
74 static constexpr uint32_t REGISTERS = 0x30;
75 static constexpr uint32_t ALL_CHANNELS = (1 << CHANNELS) - 1;
76
77 // constructor
78 adpcm_a_registers() { }
79
80 // reset to initial state
81 void reset();
82
83 // save/restore
84 void save_restore(ymfm_saved_state &state);
85
86 // map channel number to register offset
87 static constexpr uint32_t channel_offset(uint32_t chnum)
88 {
89 assert(chnum < CHANNELS);
90 return chnum;
91 }
92
93 // direct read/write access
94 void write(uint32_t index, uint8_t data) { m_regdata[index] = data; }
95
96 // system-wide registers
97 uint32_t dump() const { return bitfield(m_regdata[0x00], 7); }
98 uint32_t dump_mask() const { return bitfield(m_regdata[0x00], 0, 6); }
99 uint32_t total_level() const { return bitfield(m_regdata[0x01], 0, 6); }
100 uint32_t test() const { return m_regdata[0x02]; }
101
102 // per-channel registers
103 uint32_t ch_pan_left(uint32_t choffs) const { return bitfield(m_regdata[choffs + 0x08], 7); }
104 uint32_t ch_pan_right(uint32_t choffs) const { return bitfield(m_regdata[choffs + 0x08], 6); }
105 uint32_t ch_instrument_level(uint32_t choffs) const { return bitfield(m_regdata[choffs + 0x08], 0, 5); }
106 uint32_t ch_start(uint32_t choffs) const { return m_regdata[choffs + 0x10] | (m_regdata[choffs + 0x18] << 8); }
107 uint32_t ch_end(uint32_t choffs) const { return m_regdata[choffs + 0x20] | (m_regdata[choffs + 0x28] << 8); }
108
109 // per-channel writes
110 void write_start(uint32_t choffs, uint32_t address)
111 {
112 write(choffs + 0x10, address);
113 write(choffs + 0x18, address >> 8);
114 }
115 void write_end(uint32_t choffs, uint32_t address)
116 {
117 write(choffs + 0x20, address);
118 write(choffs + 0x28, address >> 8);
119 }
120
121 private:
122 // internal state
123 uint8_t m_regdata[REGISTERS]; // register data
124 };
125
126
127 // ======================> adpcm_a_channel
128
129 class adpcm_a_channel
130 {
131 public:
132 // constructor
133 adpcm_a_channel(adpcm_a_engine &owner, uint32_t choffs, uint32_t addrshift);
134
135 // reset the channel state
136 void reset();
137
138 // save/restore
139 void save_restore(ymfm_saved_state &state);
140
141 // signal key on/off
142 void keyonoff(bool on);
143
144 // master clockingfunction
145 bool clock();
146
147 // return the computed output value, with panning applied
148 template<int NumOutputs>
149 void output(ymfm_output<NumOutputs> &output);
150
151 // return the last output
152 int32_t get_last_out(int ch) { return m_lastOut[ch]; }
153
154 private:
155 // internal state
156 uint32_t const m_choffs; // channel offset
157 uint32_t const m_address_shift; // address bits shift-left
158 uint32_t m_playing; // currently playing?
159 uint32_t m_curnibble; // index of the current nibble
160 uint32_t m_curbyte; // current byte of data
161 uint32_t m_curaddress; // current address
162 int32_t m_accumulator; // accumulator
163 int32_t m_step_index; // index in the stepping table
164 int32_t m_lastOut[2]; // last output
165 adpcm_a_registers &m_regs; // reference to registers
166 adpcm_a_engine &m_owner; // reference to our owner
167 };
168
169
170 // ======================> adpcm_a_engine
171
172 class adpcm_a_engine
173 {
174 public:
175 static constexpr int CHANNELS = adpcm_a_registers::CHANNELS;
176
177 // constructor
178 adpcm_a_engine(ymfm_interface &intf, uint32_t addrshift);
179
180 // reset our status
181 void reset();
182
183 // save/restore
184 void save_restore(ymfm_saved_state &state);
185
186 // master clocking function
187 uint32_t clock(uint32_t chanmask);
188
189 // compute sum of channel outputs
190 template<int NumOutputs>
191 void output(ymfm_output<NumOutputs> &output, uint32_t chanmask);
192
193 // write to the ADPCM-A registers
194 void write(uint32_t regnum, uint8_t data);
195
196 // set the start/end address for a channel (for hardcoded YM2608 percussion)
197 void set_start_end(uint8_t chnum, uint16_t start, uint16_t end)
198 {
199 uint32_t choffs = adpcm_a_registers::channel_offset(chnum);
200 m_regs.write_start(choffs, start);
201 m_regs.write_end(choffs, end);
202 }
203
204 // return a reference to our interface
205 ymfm_interface &intf() { return m_intf; }
206
207 // return a reference to our registers
208 adpcm_a_registers &regs() { return m_regs; }
209
210 // debug functions
211 adpcm_a_channel* debug_channel(uint32_t index) const { return m_channel[index].get(); }
212
213 private:
214 // internal state
215 ymfm_interface &m_intf; // reference to the interface
216 std::unique_ptr<adpcm_a_channel> m_channel[CHANNELS]; // array of channels
217 adpcm_a_registers m_regs; // registers
218 };
219
220
221 // ======================> adpcm_b_registers
222
223 //
224 // ADPCM-B register map:
225 //
226 // System-wide registers:
227 // 00 x------- Start of synthesis/analysis
228 // -x------ Record
229 // --x----- External/manual driving
230 // ---x---- Repeat playback
231 // ----x--- Speaker off
232 // -------x Reset
233 // 01 x------- Pan left
234 // -x------ Pan right
235 // ----x--- Start conversion
236 // -----x-- DAC enable
237 // ------x- DRAM access (1=8-bit granularity; 0=1-bit)
238 // -------x RAM/ROM (1=ROM, 0=RAM)
239 // 02 xxxxxxxx Start address (low)
240 // 03 xxxxxxxx Start address (high)
241 // 04 xxxxxxxx End address (low)
242 // 05 xxxxxxxx End address (high)
243 // 06 xxxxxxxx Prescale value (low)
244 // 07 -----xxx Prescale value (high)
245 // 08 xxxxxxxx CPU data/buffer
246 // 09 xxxxxxxx Delta-N frequency scale (low)
247 // 0a xxxxxxxx Delta-N frequency scale (high)
248 // 0b xxxxxxxx Level control
249 // 0c xxxxxxxx Limit address (low)
250 // 0d xxxxxxxx Limit address (high)
251 // 0e xxxxxxxx DAC data [YM2608/10]
252 // 0f xxxxxxxx PCM data [YM2608/10]
253 // 0e xxxxxxxx DAC data high [Y8950]
254 // 0f xx------ DAC data low [Y8950]
255 // 10 -----xxx DAC data exponent [Y8950]
256 //
257 class adpcm_b_registers
258 {
259 public:
260 // constants
261 static constexpr uint32_t REGISTERS = 0x11;
262
263 // constructor
264 adpcm_b_registers() { }
265
266 // reset to initial state
267 void reset();
268
269 // save/restore
270 void save_restore(ymfm_saved_state &state);
271
272 // direct read/write access
273 void write(uint32_t index, uint8_t data) { m_regdata[index] = data; }
274
275 // system-wide registers
276 uint32_t execute() const { return bitfield(m_regdata[0x00], 7); }
277 uint32_t record() const { return bitfield(m_regdata[0x00], 6); }
278 uint32_t external() const { return bitfield(m_regdata[0x00], 5); }
279 uint32_t repeat() const { return bitfield(m_regdata[0x00], 4); }
280 uint32_t speaker() const { return bitfield(m_regdata[0x00], 3); }
281 uint32_t resetflag() const { return bitfield(m_regdata[0x00], 0); }
282 uint32_t pan_left() const { return bitfield(m_regdata[0x01], 7); }
283 uint32_t pan_right() const { return bitfield(m_regdata[0x01], 6); }
284 uint32_t start_conversion() const { return bitfield(m_regdata[0x01], 3); }
285 uint32_t dac_enable() const { return bitfield(m_regdata[0x01], 2); }
286 uint32_t dram_8bit() const { return bitfield(m_regdata[0x01], 1); }
287 uint32_t rom_ram() const { return bitfield(m_regdata[0x01], 0); }
288 uint32_t start() const { return m_regdata[0x02] | (m_regdata[0x03] << 8); }
289 uint32_t end() const { return m_regdata[0x04] | (m_regdata[0x05] << 8); }
290 uint32_t prescale() const { return m_regdata[0x06] | (bitfield(m_regdata[0x07], 0, 3) << 8); }
291 uint32_t cpudata() const { return m_regdata[0x08]; }
292 uint32_t delta_n() const { return m_regdata[0x09] | (m_regdata[0x0a] << 8); }
293 uint32_t level() const { return m_regdata[0x0b]; }
294 uint32_t limit() const { return m_regdata[0x0c] | (m_regdata[0x0d] << 8); }
295 uint32_t dac() const { return m_regdata[0x0e]; }
296 uint32_t pcm() const { return m_regdata[0x0f]; }
297
298 private:
299 // internal state
300 uint8_t m_regdata[REGISTERS]; // register data
301 };
302
303
304 // ======================> adpcm_b_channel
305
306 class adpcm_b_channel
307 {
308 static constexpr int32_t STEP_MIN = 127;
309 static constexpr int32_t STEP_MAX = 24576;
310
311 public:
312 static constexpr uint8_t STATUS_EOS = 0x01;
313 static constexpr uint8_t STATUS_BRDY = 0x02;
314 static constexpr uint8_t STATUS_PLAYING = 0x04;
315
316 // constructor
317 adpcm_b_channel(adpcm_b_engine &owner, uint32_t addrshift);
318
319 // reset the channel state
320 void reset();
321
322 // save/restore
323 void save_restore(ymfm_saved_state &state);
324
325 // signal key on/off
326 void keyonoff(bool on);
327
328 // master clocking function
329 void clock();
330
331 // return the computed output value, with panning applied
332 template<int NumOutputs>
333 void output(ymfm_output<NumOutputs> &output, uint32_t rshift);
334
335 // return the status register
336 uint8_t status() const { return m_status; }
337
338 // handle special register reads
339 uint8_t read(uint32_t regnum);
340
341 // handle special register writes
342 void write(uint32_t regnum, uint8_t value);
343
344 // return the last output
345 int32_t get_last_out(int ch) { return m_lastOut[ch]; }
346
347 private:
348 // helper - return the current address shift
349 uint32_t address_shift() const;
350
351 // load the start address
352 void load_start();
353
354 // limit checker; stops at the last byte of the chunk described by address_shift()
355 bool at_limit() const { return (m_curaddress == (((m_regs.limit() + 1) << address_shift()) - 1)); }
356
357 // end checker; stops at the last byte of the chunk described by address_shift()
358 bool at_end() const { return (m_curaddress == (((m_regs.end() + 1) << address_shift()) - 1)); }
359
360 // internal state
361 uint32_t const m_address_shift; // address bits shift-left
362 uint32_t m_status; // currently playing?
363 uint32_t m_curnibble; // index of the current nibble
364 uint32_t m_curbyte; // current byte of data
365 uint32_t m_dummy_read; // dummy read tracker
366 uint32_t m_position; // current fractional position
367 uint32_t m_curaddress; // current address
368 int32_t m_accumulator; // accumulator
369 int32_t m_prev_accum; // previous accumulator (for linear interp)
370 int32_t m_adpcm_step; // next forecast
371 int32_t m_lastOut[2]; // last output
372 adpcm_b_registers &m_regs; // reference to registers
373 adpcm_b_engine &m_owner; // reference to our owner
374 };
375
376
377 // ======================> adpcm_b_engine
378
379 class adpcm_b_engine
380 {
381 public:
382 // constructor
383 adpcm_b_engine(ymfm_interface &intf, uint32_t addrshift = 0);
384
385 // reset our status
386 void reset();
387
388 // save/restore
389 void save_restore(ymfm_saved_state &state);
390
391 // master clocking function
392 void clock();
393
394 // compute sum of channel outputs
395 template<int NumOutputs>
396 void output(ymfm_output<NumOutputs> &output, uint32_t rshift);
397
398 // get last output
399 int32_t get_last_out(int ch) { return m_channel->get_last_out(ch); }
400
401 // read from the ADPCM-B registers
402 uint32_t read(uint32_t regnum) { return m_channel->read(regnum); }
403
404 // write to the ADPCM-B registers
405 void write(uint32_t regnum, uint8_t data);
406
407 // status
408 uint8_t status() const { return m_channel->status(); }
409
410 // return a reference to our interface
411 ymfm_interface &intf() { return m_intf; }
412
413 // return a reference to our registers
414 adpcm_b_registers &regs() { return m_regs; }
415
416 private:
417 // internal state
418 ymfm_interface &m_intf; // reference to our interface
419 std::unique_ptr<adpcm_b_channel> m_channel; // channel pointer
420 adpcm_b_registers m_regs; // registers
421 };
422
423 }
424
425 #endif // YMFM_ADPCM_H