Debian Codegraph @

BambooTracker/chip/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) const;
150
151 private:
152 // internal state
153 uint32_t const m_choffs; // channel offset
154 uint32_t const m_address_shift; // address bits shift-left
155 uint32_t m_playing; // currently playing?
156 uint32_t m_curnibble; // index of the current nibble
157 uint32_t m_curbyte; // current byte of data
158 uint32_t m_curaddress; // current address
159 int32_t m_accumulator; // accumulator
160 int32_t m_step_index; // index in the stepping table
161 adpcm_a_registers &m_regs; // reference to registers
162 adpcm_a_engine &m_owner; // reference to our owner
163 };
164
165
166 // ======================> adpcm_a_engine
167
168 class adpcm_a_engine
169 {
170 public:
171 static constexpr int CHANNELS = adpcm_a_registers::CHANNELS;
172
173 // constructor
174 adpcm_a_engine(ymfm_interface &intf, uint32_t addrshift);
175
176 // reset our status
177 void reset();
178
179 // save/restore
180 void save_restore(ymfm_saved_state &state);
181
182 // master clocking function
183 uint32_t clock(uint32_t chanmask);
184
185 // compute sum of channel outputs
186 template<int NumOutputs>
187 void output(ymfm_output<NumOutputs> &output, uint32_t chanmask);
188
189 // write to the ADPCM-A registers
190 void write(uint32_t regnum, uint8_t data);
191
192 // set the start/end address for a channel (for hardcoded YM2608 percussion)
193 void set_start_end(uint8_t chnum, uint16_t start, uint16_t end)
194 {
195 uint32_t choffs = adpcm_a_registers::channel_offset(chnum);
196 m_regs.write_start(choffs, start);
197 m_regs.write_end(choffs, end);
198 }
199
200 // return a reference to our interface
201 ymfm_interface &intf() { return m_intf; }
202
203 // return a reference to our registers
204 adpcm_a_registers &regs() { return m_regs; }
205
206 private:
207 // internal state
208 ymfm_interface &m_intf; // reference to the interface
209 std::unique_ptr<adpcm_a_channel> m_channel[CHANNELS]; // array of channels
210 adpcm_a_registers m_regs; // registers
211 };
212
213
214 // ======================> adpcm_b_registers
215
216 //
217 // ADPCM-B register map:
218 //
219 // System-wide registers:
220 // 00 x------- Start of synthesis/analysis
221 // -x------ Record
222 // --x----- External/manual driving
223 // ---x---- Repeat playback
224 // ----x--- Speaker off
225 // -------x Reset
226 // 01 x------- Pan left
227 // -x------ Pan right
228 // ----x--- Start conversion
229 // -----x-- DAC enable
230 // ------x- DRAM access (1=8-bit granularity; 0=1-bit)
231 // -------x RAM/ROM (1=ROM, 0=RAM)
232 // 02 xxxxxxxx Start address (low)
233 // 03 xxxxxxxx Start address (high)
234 // 04 xxxxxxxx End address (low)
235 // 05 xxxxxxxx End address (high)
236 // 06 xxxxxxxx Prescale value (low)
237 // 07 -----xxx Prescale value (high)
238 // 08 xxxxxxxx CPU data/buffer
239 // 09 xxxxxxxx Delta-N frequency scale (low)
240 // 0a xxxxxxxx Delta-N frequency scale (high)
241 // 0b xxxxxxxx Level control
242 // 0c xxxxxxxx Limit address (low)
243 // 0d xxxxxxxx Limit address (high)
244 // 0e xxxxxxxx DAC data [YM2608/10]
245 // 0f xxxxxxxx PCM data [YM2608/10]
246 // 0e xxxxxxxx DAC data high [Y8950]
247 // 0f xx------ DAC data low [Y8950]
248 // 10 -----xxx DAC data exponent [Y8950]
249 //
250 class adpcm_b_registers
251 {
252 public:
253 // constants
254 static constexpr uint32_t REGISTERS = 0x11;
255
256 // constructor
257 adpcm_b_registers() { }
258
259 // reset to initial state
260 void reset();
261
262 // save/restore
263 void save_restore(ymfm_saved_state &state);
264
265 // direct read/write access
266 void write(uint32_t index, uint8_t data) { m_regdata[index] = data; }
267
268 // system-wide registers
269 uint32_t execute() const { return bitfield(m_regdata[0x00], 7); }
270 uint32_t record() const { return bitfield(m_regdata[0x00], 6); }
271 uint32_t external() const { return bitfield(m_regdata[0x00], 5); }
272 uint32_t repeat() const { return bitfield(m_regdata[0x00], 4); }
273 uint32_t speaker() const { return bitfield(m_regdata[0x00], 3); }
274 uint32_t resetflag() const { return bitfield(m_regdata[0x00], 0); }
275 uint32_t pan_left() const { return bitfield(m_regdata[0x01], 7); }
276 uint32_t pan_right() const { return bitfield(m_regdata[0x01], 6); }
277 uint32_t start_conversion() const { return bitfield(m_regdata[0x01], 3); }
278 uint32_t dac_enable() const { return bitfield(m_regdata[0x01], 2); }
279 uint32_t dram_8bit() const { return bitfield(m_regdata[0x01], 1); }
280 uint32_t rom_ram() const { return bitfield(m_regdata[0x01], 0); }
281 uint32_t start() const { return m_regdata[0x02] | (m_regdata[0x03] << 8); }
282 uint32_t end() const { return m_regdata[0x04] | (m_regdata[0x05] << 8); }
283 uint32_t prescale() const { return m_regdata[0x06] | (bitfield(m_regdata[0x07], 0, 3) << 8); }
284 uint32_t cpudata() const { return m_regdata[0x08]; }
285 uint32_t delta_n() const { return m_regdata[0x09] | (m_regdata[0x0a] << 8); }
286 uint32_t level() const { return m_regdata[0x0b]; }
287 uint32_t limit() const { return m_regdata[0x0c] | (m_regdata[0x0d] << 8); }
288 uint32_t dac() const { return m_regdata[0x0e]; }
289 uint32_t pcm() const { return m_regdata[0x0f]; }
290
291 private:
292 // internal state
293 uint8_t m_regdata[REGISTERS]; // register data
294 };
295
296
297 // ======================> adpcm_b_channel
298
299 class adpcm_b_channel
300 {
301 static constexpr int32_t STEP_MIN = 127;
302 static constexpr int32_t STEP_MAX = 24576;
303
304 public:
305 static constexpr uint8_t STATUS_EOS = 0x01;
306 static constexpr uint8_t STATUS_BRDY = 0x02;
307 static constexpr uint8_t STATUS_PLAYING = 0x04;
308
309 // constructor
310 adpcm_b_channel(adpcm_b_engine &owner, uint32_t addrshift);
311
312 // reset the channel state
313 void reset();
314
315 // save/restore
316 void save_restore(ymfm_saved_state &state);
317
318 // signal key on/off
319 void keyonoff(bool on);
320
321 // master clocking function
322 void clock();
323
324 // return the computed output value, with panning applied
325 template<int NumOutputs>
326 void output(ymfm_output<NumOutputs> &output, uint32_t rshift) const;
327
328 // return the status register
329 uint8_t status() const { return m_status; }
330
331 // handle special register reads
332 uint8_t read(uint32_t regnum);
333
334 // handle special register writes
335 void write(uint32_t regnum, uint8_t value);
336
337 private:
338 // helper - return the current address shift
339 uint32_t address_shift() const;
340
341 // load the start address
342 void load_start();
343
344 // limit checker; stops at the last byte of the chunk described by address_shift()
345 bool at_limit() const { return (m_curaddress == (((m_regs.limit() + 1) << address_shift()) - 1)); }
346
347 // end checker; stops at the last byte of the chunk described by address_shift()
348 bool at_end() const { return (m_curaddress == (((m_regs.end() + 1) << address_shift()) - 1)); }
349
350 // internal state
351 uint32_t const m_address_shift; // address bits shift-left
352 uint32_t m_status; // currently playing?
353 uint32_t m_curnibble; // index of the current nibble
354 uint32_t m_curbyte; // current byte of data
355 uint32_t m_dummy_read; // dummy read tracker
356 uint32_t m_position; // current fractional position
357 uint32_t m_curaddress; // current address
358 int32_t m_accumulator; // accumulator
359 int32_t m_prev_accum; // previous accumulator (for linear interp)
360 int32_t m_adpcm_step; // next forecast
361 adpcm_b_registers &m_regs; // reference to registers
362 adpcm_b_engine &m_owner; // reference to our owner
363 };
364
365
366 // ======================> adpcm_b_engine
367
368 class adpcm_b_engine
369 {
370 public:
371 // constructor
372 adpcm_b_engine(ymfm_interface &intf, uint32_t addrshift = 0);
373
374 // reset our status
375 void reset();
376
377 // save/restore
378 void save_restore(ymfm_saved_state &state);
379
380 // master clocking function
381 void clock();
382
383 // compute sum of channel outputs
384 template<int NumOutputs>
385 void output(ymfm_output<NumOutputs> &output, uint32_t rshift);
386
387 // read from the ADPCM-B registers
388 uint32_t read(uint32_t regnum) { return m_channel->read(regnum); }
389
390 // write to the ADPCM-B registers
391 void write(uint32_t regnum, uint8_t data);
392
393 // status
394 uint8_t status() const { return m_channel->status(); }
395
396 // return a reference to our interface
397 ymfm_interface &intf() { return m_intf; }
398
399 // return a reference to our registers
400 adpcm_b_registers &regs() { return m_regs; }
401
402 private:
403 // internal state
404 ymfm_interface &m_intf; // reference to our interface
405 std::unique_ptr<adpcm_b_channel> m_channel; // channel pointer
406 adpcm_b_registers m_regs; // registers
407 };
408
409 }
410
411 #endif // YMFM_ADPCM_H