Debian Codegraph @

thirdparty/ymfm/src/ymfm_pcm.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_PCM_H
32 #define YMFM_PCM_H
33
34 #pragma once
35
36 #include "ymfm.h"
37
38 namespace ymfm
39 {
40
41 /*
42 Note to self: Sega "Multi-PCM" is almost identical to this
43
44 28 channels
45
46 Writes:
47 00 = data reg, causes write
48 01 = target slot = data - (data / 8)
49 02 = address (clamped to 7)
50
51 Slot data (registers with ADSR/KSR seem to be inaccessible):
52 0: xxxx---- panpot
53 1: xxxxxxxx wavetable low
54 2: xxxxxx-- pitch low
55 -------x wavetable high
56 3: xxxx---- octave
57 ----xxxx pitch hi
58 4: x------- key on
59 5: xxxxxxx- total level
60 -------x level direct (0=interpolate)
61 6: --xxx--- LFO frequency
62 -----xxx PM sensitivity
63 7: -----xxx AM sensitivity
64
65 Sample data:
66 +00: start hi
67 +01: start mid
68 +02: start low
69 +03: loop hi
70 +04: loop low
71 +05: -end hi
72 +06: -end low
73 +07: vibrato (reg 6)
74 +08: attack/decay
75 +09: sustain level/rate
76 +0A: ksr/release
77 +0B: LFO amplitude (reg 7)
78
79 */
80
81 //*********************************************************
82 // INTERFACE CLASSES
83 //*********************************************************
84
85 class pcm_engine;
86
87
88 // ======================> pcm_cache
89
90 // this class holds data that is computed once at the start of clocking
91 // and remains static during subsequent sound generation
92 struct pcm_cache
93 {
94 uint32_t step; // sample position step, as a .16 value
95 uint32_t total_level; // target total level, as a .10 value
96 uint32_t pan_left; // left panning attenuation
97 uint32_t pan_right; // right panning attenuation
98 uint32_t eg_sustain; // sustain level, shifted up to envelope values
99 uint8_t eg_rate[EG_STATES]; // envelope rate, including KSR
100 uint8_t lfo_step; // stepping value for LFO
101 uint8_t am_depth; // scale value for AM LFO
102 uint8_t pm_depth; // scale value for PM LFO
103 };
104
105
106 // ======================> pcm_registers
107
108 //
109 // PCM register map:
110 //
111 // System-wide registers:
112 // 00-01 xxxxxxxx LSI Test
113 // 02 -------x Memory access mode (0=sound gen, 1=read/write)
114 // ------x- Memory type (0=ROM, 1=ROM+SRAM)
115 // ---xxx-- Wave table header
116 // xxx----- Device ID (=1 for YMF278B)
117 // 03 --xxxxxx Memory address high
118 // 04 xxxxxxxx Memory address mid
119 // 05 xxxxxxxx Memory address low
120 // 06 xxxxxxxx Memory data
121 // F8 --xxx--- Mix control (FM_R)
122 // -----xxx Mix control (FM_L)
123 // F9 --xxx--- Mix control (PCM_R)
124 // -----xxx Mix control (PCM_L)
125 //
126 // Channel-specific registers:
127 // 08-1F xxxxxxxx Wave table number low
128 // 20-37 -------x Wave table number high
129 // xxxxxxx- F-number low
130 // 38-4F -----xxx F-number high
131 // ----x--- Pseudo-reverb
132 // xxxx---- Octave
133 // 50-67 xxxxxxx- Total level
134 // -------x Level direct
135 // 68-7F x------- Key on
136 // -x------ Damp
137 // --x----- LFO reset
138 // ---x---- Output channel
139 // ----xxxx Panpot
140 // 80-97 --xxx--- LFO speed
141 // -----xxx Vibrato
142 // 98-AF xxxx---- Attack rate
143 // ----xxxx Decay rate
144 // B0-C7 xxxx---- Sustain level
145 // ----xxxx Sustain rate
146 // C8-DF xxxx---- Rate correction
147 // ----xxxx Release rate
148 // E0-F7 -----xxx AM depth
149
150 class pcm_registers
151 {
152 public:
153 // constants
154 static constexpr uint32_t OUTPUTS = 4;
155 static constexpr uint32_t CHANNELS = 24;
156 static constexpr uint32_t REGISTERS = 0x100;
157 static constexpr uint32_t ALL_CHANNELS = (1 << CHANNELS) - 1;
158
159 // constructor
160 pcm_registers() { }
161
162 // save/restore
163 void save_restore(ymfm_saved_state &state);
164
165 // reset to initial state
166 void reset();
167
168 // update cache information
169 void cache_channel_data(uint32_t choffs, pcm_cache &cache);
170
171 // direct read/write access
172 uint8_t read(uint32_t index ) { return m_regdata[index]; }
173 void write(uint32_t index, uint8_t data) { m_regdata[index] = data; }
174
175 // system-wide registers
176 uint32_t memory_access_mode() const { return bitfield(m_regdata[0x02], 0); }
177 uint32_t memory_type() const { return bitfield(m_regdata[0x02], 1); }
178 uint32_t wave_table_header() const { return bitfield(m_regdata[0x02], 2, 3); }
179 uint32_t device_id() const { return bitfield(m_regdata[0x02], 5, 3); }
180 uint32_t memory_address() const { return (bitfield(m_regdata[0x03], 0, 6) << 16) | (m_regdata[0x04] << 8) | m_regdata[0x05]; }
181 uint32_t memory_data() const { return m_regdata[0x06]; }
182 uint32_t mix_fm_r() const { return bitfield(m_regdata[0xf8], 3, 3); }
183 uint32_t mix_fm_l() const { return bitfield(m_regdata[0xf8], 0, 3); }
184 uint32_t mix_pcm_r() const { return bitfield(m_regdata[0xf9], 3, 3); }
185 uint32_t mix_pcm_l() const { return bitfield(m_regdata[0xf9], 0, 3); }
186
187 // per-channel registers
188 uint32_t ch_wave_table_num(uint32_t choffs) const { return m_regdata[choffs + 0x08] | (bitfield(m_regdata[choffs + 0x20], 0) << 8); }
189 uint32_t ch_fnumber(uint32_t choffs) const { return bitfield(m_regdata[choffs + 0x20], 1, 7) | (bitfield(m_regdata[choffs + 0x38], 0, 3) << 7); }
190 uint32_t ch_pseudo_reverb(uint32_t choffs) const { return bitfield(m_regdata[choffs + 0x38], 3); }
191 uint32_t ch_octave(uint32_t choffs) const { return bitfield(m_regdata[choffs + 0x38], 4, 4); }
192 uint32_t ch_total_level(uint32_t choffs) const { return bitfield(m_regdata[choffs + 0x50], 1, 7); }
193 uint32_t ch_level_direct(uint32_t choffs) const { return bitfield(m_regdata[choffs + 0x50], 0); }
194 uint32_t ch_keyon(uint32_t choffs) const { return bitfield(m_regdata[choffs + 0x68], 7); }
195 uint32_t ch_damp(uint32_t choffs) const { return bitfield(m_regdata[choffs + 0x68], 6); }
196 uint32_t ch_lfo_reset(uint32_t choffs) const { return bitfield(m_regdata[choffs + 0x68], 5); }
197 uint32_t ch_output_channel(uint32_t choffs) const { return bitfield(m_regdata[choffs + 0x68], 4); }
198 uint32_t ch_panpot(uint32_t choffs) const { return bitfield(m_regdata[choffs + 0x68], 0, 4); }
199 uint32_t ch_lfo_speed(uint32_t choffs) const { return bitfield(m_regdata[choffs + 0x80], 3, 3); }
200 uint32_t ch_vibrato(uint32_t choffs) const { return bitfield(m_regdata[choffs + 0x80], 0, 3); }
201 uint32_t ch_attack_rate(uint32_t choffs) const { return bitfield(m_regdata[choffs + 0x98], 4, 4); }
202 uint32_t ch_decay_rate(uint32_t choffs) const { return bitfield(m_regdata[choffs + 0x98], 0, 4); }
203 uint32_t ch_sustain_level(uint32_t choffs) const { return bitfield(m_regdata[choffs + 0xb0], 4, 4); }
204 uint32_t ch_sustain_rate(uint32_t choffs) const { return bitfield(m_regdata[choffs + 0xb0], 0, 4); }
205 uint32_t ch_rate_correction(uint32_t choffs) const { return bitfield(m_regdata[choffs + 0xc8], 4, 4); }
206 uint32_t ch_release_rate(uint32_t choffs) const { return bitfield(m_regdata[choffs + 0xc8], 0, 4); }
207 uint32_t ch_am_depth(uint32_t choffs) const { return bitfield(m_regdata[choffs + 0xe0], 0, 3); }
208
209 // return the memory address and increment it
210 uint32_t memory_address_autoinc()
211 {
212 uint32_t result = memory_address();
213 uint32_t newval = result + 1;
214 m_regdata[0x05] = newval >> 0;
215 m_regdata[0x04] = newval >> 8;
216 m_regdata[0x03] = (newval >> 16) & 0x3f;
217 return result;
218 }
219
220 private:
221 // internal helpers
222 uint32_t effective_rate(uint32_t raw, uint32_t correction);
223
224 // internal state
225 uint8_t m_regdata[REGISTERS]; // register data
226 };
227
228
229 // ======================> pcm_channel
230
231 class pcm_channel
232 {
233 static constexpr uint8_t KEY_ON = 0x01;
234 static constexpr uint8_t KEY_PENDING_ON = 0x02;
235 static constexpr uint8_t KEY_PENDING = 0x04;
236
237 // "quiet" value, used to optimize when we can skip doing working
238 static constexpr uint32_t EG_QUIET = 0x200;
239
240 public:
241 using output_data = ymfm_output<pcm_registers::OUTPUTS>;
242
243 // constructor
244 pcm_channel(pcm_engine &owner, uint32_t choffs);
245
246 // save/restore
247 void save_restore(ymfm_saved_state &state);
248
249 // reset the channel state
250 void reset();
251
252 // return the channel offset
253 uint32_t choffs() const { return m_choffs; }
254
255 // prepare prior to clocking
256 bool prepare();
257
258 // master clocking function
259 void clock(uint32_t env_counter);
260
261 // return the computed output value, with panning applied
262 void output(output_data &output) const;
263
264 // signal key on/off
265 void keyonoff(bool on);
266
267 // load a new wavetable entry
268 void load_wavetable();
269
270 private:
271 // internal helpers
272 void start_attack();
273 void start_release();
274 void clock_envelope(uint32_t env_counter);
275 int16_t fetch_sample() const;
276 uint8_t read_pcm(uint32_t address) const;
277
278 // internal state
279 uint32_t const m_choffs; // channel offset
280 uint32_t m_baseaddr; // base address
281 uint32_t m_endpos; // ending position
282 uint32_t m_looppos; // loop position
283 uint32_t m_curpos; // current position
284 uint32_t m_nextpos; // next position
285 uint32_t m_lfo_counter; // LFO counter
286 envelope_state m_eg_state; // envelope state
287 uint16_t m_env_attenuation; // computed envelope attenuation
288 uint32_t m_total_level; // total level with as 7.10 for interp
289 uint8_t m_format; // sample format
290 uint8_t m_key_state; // current key state
291 pcm_cache m_cache; // cached data
292 pcm_registers &m_regs; // reference to registers
293 pcm_engine &m_owner; // reference to our owner
294 };
295
296
297 // ======================> pcm_engine
298
299 class pcm_engine
300 {
301 public:
302 static constexpr int OUTPUTS = pcm_registers::OUTPUTS;
303 static constexpr int CHANNELS = pcm_registers::CHANNELS;
304 static constexpr uint32_t ALL_CHANNELS = pcm_registers::ALL_CHANNELS;
305 using output_data = pcm_channel::output_data;
306
307 // constructor
308 pcm_engine(ymfm_interface &intf);
309
310 // reset our status
311 void reset();
312
313 // save/restore
314 void save_restore(ymfm_saved_state &state);
315
316 // master clocking function
317 void clock(uint32_t chanmask);
318
319 // compute sum of channel outputs
320 void output(output_data &output, uint32_t chanmask);
321
322 // read from the PCM registers
323 uint8_t read(uint32_t regnum);
324
325 // write to the PCM registers
326 void write(uint32_t regnum, uint8_t data);
327
328 // return a reference to our interface
329 ymfm_interface &intf() { return m_intf; }
330
331 // return a reference to our registers
332 pcm_registers &regs() { return m_regs; }
333
334 private:
335 // internal state
336 ymfm_interface &m_intf; // reference to the interface
337 uint32_t m_env_counter; // envelope counter
338 uint32_t m_modified_channels; // bitmask of modified channels
339 uint32_t m_active_channels; // bitmask of active channels
340 uint32_t m_prepare_count; // counter to do periodic prepare sweeps
341 std::unique_ptr<pcm_channel> m_channel[CHANNELS]; // array of channels
342 pcm_registers m_regs; // registers
343 };
344
345 }
346
347 #endif // YMFM_PCM_H