Debian Codegraph @

thirdparty/ymfm/src/ymfm_opz.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_OPZ_H
32 #define YMFM_OPZ_H
33
34 #pragma once
35
36 #include "ymfm.h"
37 #include "ymfm_fm.h"
38
39 namespace ymfm
40 {
41
42 //*********************************************************
43 // REGISTER CLASSES
44 //*********************************************************
45
46 // ======================> opz_registers
47
48 //
49 // OPZ register map:
50 //
51 // System-wide registers:
52 // 08 -----xxx Load preset (not sure how it gets saved)
53 // 0F x------- Noise enable
54 // ---xxxxx Noise frequency
55 // 10 xxxxxxxx Timer A value (upper 8 bits)
56 // 11 ------xx Timer A value (lower 2 bits)
57 // 12 xxxxxxxx Timer B value
58 // 14 x------- CSM mode
59 // --x----- Reset timer B
60 // ---x---- Reset timer A
61 // ----x--- Enable timer B
62 // -----x-- Enable timer A
63 // ------x- Load timer B
64 // -------x Load timer A
65 // 16 xxxxxxxx LFO #2 frequency
66 // 17 0xxxxxxx AM LFO #2 depth
67 // 1xxxxxxx PM LFO #2 depth
68 // 18 xxxxxxxx LFO frequency
69 // 19 0xxxxxxx AM LFO depth
70 // 1xxxxxxx PM LFO depth
71 // 1B xx------ CT (2 output data lines)
72 // --x----- LFO #2 sync
73 // ---x---- LFO sync
74 // ----xx-- LFO #2 waveform
75 // ------xx LFO waveform
76 //
77 // Per-channel registers (channel in address bits 0-2)
78 // 00-07 xxxxxxxx Channel volume
79 // 20-27 x------- Pan right
80 // -x------ Key on (0)/off(1)
81 // --xxx--- Feedback level for operator 1 (0-7)
82 // -----xxx Operator connection algorithm (0-7)
83 // 28-2F -xxxxxxx Key code
84 // 30-37 xxxxxx-- Key fraction
85 // -------x Mono? mode
86 // 38-3F 0xxx---- LFO PM sensitivity
87 // -----0xx LFO AM shift
88 // 1xxx---- LFO #2 PM sensitivity
89 // -----1xx LFO #2 AM shift
90 //
91 // Per-operator registers (channel in address bits 0-2, operator in bits 3-4)
92 // 40-5F 0xxx---- Detune value (0-7)
93 // 0---xxxx Multiple value (0-15)
94 // 0xxx---- Fix range (0-15)
95 // 0---xxxx Fix frequency (0-15)
96 // 1xxx---- Oscillator waveform (0-7)
97 // 1---xxxx Fine? (0-15)
98 // 60-7F -xxxxxxx Total level (0-127)
99 // 80-9F xx------ Key scale rate (0-3)
100 // --x----- Fix frequency mode
101 // ---xxxxx Attack rate (0-31)
102 // A0-BF x------- LFO AM enable
103 // ---xxxxx Decay rate (0-31)
104 // C0-DF xx0----- Detune 2 value (0-3)
105 // --0xxxxx Sustain rate (0-31)
106 // xx1----- Envelope generator shift? (0-3)
107 // --1--xxx Rev? (0-7)
108 // E0-FF xxxx---- Sustain level (0-15)
109 // ----xxxx Release rate (0-15)
110 //
111 // Internal (fake) registers:
112 // 100-11F -xxx---- Oscillator waveform (0-7)
113 // ----xxxx Fine? (0-15)
114 // 120-13F xx------ Envelope generator shift (0-3)
115 // -----xxx Reverb rate (0-7)
116 // 140-15F xxxx---- Preset sustain level (0-15)
117 // ----xxxx Preset release rate (0-15)
118 // 160-17F xx------ Envelope generator shift (0-3)
119 // -----xxx Reverb rate (0-7)
120 // 180-187 -xxx---- LFO #2 PM sensitivity
121 // ---- xxx LFO #2 AM shift
122 // 188 -xxxxxxx LFO #2 PM depth
123 // 189 -xxxxxxx LFO PM depth
124 //
125
126 class opz_registers : public fm_registers_base
127 {
128 // LFO waveforms are 256 entries long
129 static constexpr uint32_t LFO_WAVEFORM_LENGTH = 256;
130
131 public:
132 // constants
133 static constexpr uint32_t OUTPUTS = 2;
134 static constexpr uint32_t CHANNELS = 8;
135 static constexpr uint32_t ALL_CHANNELS = (1 << CHANNELS) - 1;
136 static constexpr uint32_t OPERATORS = CHANNELS * 4;
137 static constexpr uint32_t WAVEFORMS = 8;
138 static constexpr uint32_t REGISTERS = 0x190;
139 static constexpr uint32_t DEFAULT_PRESCALE = 2;
140 static constexpr uint32_t EG_CLOCK_DIVIDER = 3;
141 static constexpr bool EG_HAS_REVERB = true;
142 static constexpr uint32_t CSM_TRIGGER_MASK = ALL_CHANNELS;
143 static constexpr uint32_t REG_MODE = 0x14;
144 static constexpr uint8_t STATUS_TIMERA = 0x01;
145 static constexpr uint8_t STATUS_TIMERB = 0x02;
146 static constexpr uint8_t STATUS_BUSY = 0x80;
147 static constexpr uint8_t STATUS_IRQ = 0;
148
149 // constructor
150 opz_registers();
151
152 // reset to initial state
153 void reset();
154
155 // save/restore
156 void save_restore(ymfm_saved_state &state);
157
158 // map channel number to register offset
159 static constexpr uint32_t channel_offset(uint32_t chnum)
160 {
161 assert(chnum < CHANNELS);
162 return chnum;
163 }
164
165 // map operator number to register offset
166 static constexpr uint32_t operator_offset(uint32_t opnum)
167 {
168 assert(opnum < OPERATORS);
169 return opnum;
170 }
171
172 // return an array of operator indices for each channel
173 struct operator_mapping { uint32_t chan[CHANNELS]; };
174 void operator_map(operator_mapping &dest) const;
175
176 // handle writes to the register array
177 bool write(uint16_t index, uint8_t data, uint32_t &chan, uint32_t &opmask);
178
179 // clock the noise and LFO, if present, returning LFO PM value
180 int32_t clock_noise_and_lfo();
181
182 // return the AM offset from LFO for the given channel
183 uint32_t lfo_am_offset(uint32_t choffs) const;
184
185 // return the current noise state, gated by the noise clock
186 uint32_t noise_state() const { return m_noise_state; }
187
188 // caching helpers
189 void cache_operator_data(uint32_t choffs, uint32_t opoffs, opdata_cache &cache);
190
191 // compute the phase step, given a PM value
192 uint32_t compute_phase_step(uint32_t choffs, uint32_t opoffs, opdata_cache const &cache, int32_t lfo_raw_pm);
193
194 // log a key-on event
195 std::string log_keyon(uint32_t choffs, uint32_t opoffs);
196
197 // system-wide registers
198 uint32_t noise_frequency() const { return byte(0x0f, 0, 5); }
199 uint32_t noise_enable() const { return byte(0x0f, 7, 1); }
200 uint32_t timer_a_value() const { return word(0x10, 0, 8, 0x11, 0, 2); }
201 uint32_t timer_b_value() const { return byte(0x12, 0, 8); }
202 uint32_t csm() const { return byte(0x14, 7, 1); }
203 uint32_t reset_timer_b() const { return byte(0x14, 5, 1); }
204 uint32_t reset_timer_a() const { return byte(0x14, 4, 1); }
205 uint32_t enable_timer_b() const { return byte(0x14, 3, 1); }
206 uint32_t enable_timer_a() const { return byte(0x14, 2, 1); }
207 uint32_t load_timer_b() const { return byte(0x14, 1, 1); }
208 uint32_t load_timer_a() const { return byte(0x14, 0, 1); }
209 uint32_t lfo2_pm_depth() const { return byte(0x188, 0, 7); } // fake
210 uint32_t lfo2_rate() const { return byte(0x16, 0, 8); }
211 uint32_t lfo2_am_depth() const { return byte(0x17, 0, 7); }
212 uint32_t lfo_rate() const { return byte(0x18, 0, 8); }
213 uint32_t lfo_am_depth() const { return byte(0x19, 0, 7); }
214 uint32_t lfo_pm_depth() const { return byte(0x189, 0, 7); } // fake
215 uint32_t output_bits() const { return byte(0x1b, 6, 2); }
216 uint32_t lfo2_sync() const { return byte(0x1b, 5, 1); }
217 uint32_t lfo_sync() const { return byte(0x1b, 4, 1); }
218 uint32_t lfo2_waveform() const { return byte(0x1b, 2, 2); }
219 uint32_t lfo_waveform() const { return byte(0x1b, 0, 2); }
220
221 // per-channel registers
222 uint32_t ch_volume(uint32_t choffs) const { return byte(0x00, 0, 8, choffs); }
223 uint32_t ch_output_any(uint32_t choffs) const { return byte(0x20, 7, 1, choffs) | byte(0x30, 0, 1, choffs); }
224 uint32_t ch_output_0(uint32_t choffs) const { return byte(0x30, 0, 1, choffs); }
225 uint32_t ch_output_1(uint32_t choffs) const { return byte(0x20, 7, 1, choffs) | byte(0x30, 0, 1, choffs); }
226 uint32_t ch_output_2(uint32_t choffs) const { return 0; }
227 uint32_t ch_output_3(uint32_t choffs) const { return 0; }
228 uint32_t ch_key_on(uint32_t choffs) const { return byte(0x20, 6, 1, choffs); }
229 uint32_t ch_feedback(uint32_t choffs) const { return byte(0x20, 3, 3, choffs); }
230 uint32_t ch_algorithm(uint32_t choffs) const { return byte(0x20, 0, 3, choffs); }
231 uint32_t ch_block_freq(uint32_t choffs) const { return word(0x28, 0, 7, 0x30, 2, 6, choffs); }
232 uint32_t ch_lfo_pm_sens(uint32_t choffs) const { return byte(0x38, 4, 3, choffs); }
233 uint32_t ch_lfo_am_sens(uint32_t choffs) const { return byte(0x38, 0, 2, choffs); }
234 uint32_t ch_lfo2_pm_sens(uint32_t choffs) const { return byte(0x180, 4, 3, choffs); } // fake
235 uint32_t ch_lfo2_am_sens(uint32_t choffs) const { return byte(0x180, 0, 2, choffs); } // fake
236
237 // per-operator registers
238 uint32_t op_detune(uint32_t opoffs) const { return byte(0x40, 4, 3, opoffs); }
239 uint32_t op_multiple(uint32_t opoffs) const { return byte(0x40, 0, 4, opoffs); }
240 uint32_t op_fix_range(uint32_t opoffs) const { return byte(0x40, 4, 3, opoffs); }
241 uint32_t op_fix_frequency(uint32_t opoffs) const { return byte(0x40, 0, 4, opoffs); }
242 uint32_t op_waveform(uint32_t opoffs) const { return byte(0x100, 4, 3, opoffs); } // fake
243 uint32_t op_fine(uint32_t opoffs) const { return byte(0x100, 0, 4, opoffs); } // fake
244 uint32_t op_total_level(uint32_t opoffs) const { return byte(0x60, 0, 7, opoffs); }
245 uint32_t op_ksr(uint32_t opoffs) const { return byte(0x80, 6, 2, opoffs); }
246 uint32_t op_fix_mode(uint32_t opoffs) const { return byte(0x80, 5, 1, opoffs); }
247 uint32_t op_attack_rate(uint32_t opoffs) const { return byte(0x80, 0, 5, opoffs); }
248 uint32_t op_lfo_am_enable(uint32_t opoffs) const { return byte(0xa0, 7, 1, opoffs); }
249 uint32_t op_decay_rate(uint32_t opoffs) const { return byte(0xa0, 0, 5, opoffs); }
250 uint32_t op_detune2(uint32_t opoffs) const { return byte(0xc0, 6, 2, opoffs); }
251 uint32_t op_sustain_rate(uint32_t opoffs) const { return byte(0xc0, 0, 5, opoffs); }
252 uint32_t op_eg_shift(uint32_t opoffs) const { return byte(0x120, 6, 2, opoffs); } // fake
253 uint32_t op_reverb_rate(uint32_t opoffs) const { return byte(0x120, 0, 3, opoffs); } // fake
254 uint32_t op_sustain_level(uint32_t opoffs) const { return byte(0xe0, 4, 4, opoffs); }
255 uint32_t op_release_rate(uint32_t opoffs) const { return byte(0xe0, 0, 4, opoffs); }
256
257 protected:
258 // return a bitfield extracted from a byte
259 uint32_t byte(uint32_t offset, uint32_t start, uint32_t count, uint32_t extra_offset = 0) const
260 {
261 return bitfield(m_regdata[offset + extra_offset], start, count);
262 }
263
264 // return a bitfield extracted from a pair of bytes, MSBs listed first
265 uint32_t word(uint32_t offset1, uint32_t start1, uint32_t count1, uint32_t offset2, uint32_t start2, uint32_t count2, uint32_t extra_offset = 0) const
266 {
267 return (byte(offset1, start1, count1, extra_offset) << count2) | byte(offset2, start2, count2, extra_offset);
268 }
269
270 // internal state
271 uint32_t m_lfo_counter[2]; // LFO counter
272 uint32_t m_noise_lfsr; // noise LFSR state
273 uint8_t m_noise_counter; // noise counter
274 uint8_t m_noise_state; // latched noise state
275 uint8_t m_noise_lfo; // latched LFO noise value
276 uint8_t m_lfo_am[2]; // current LFO AM value
277 uint8_t m_regdata[REGISTERS]; // register data
278 uint16_t m_phase_substep[OPERATORS]; // phase substep for fixed frequency
279 int16_t m_lfo_waveform[4][LFO_WAVEFORM_LENGTH]; // LFO waveforms; AM in low 8, PM in upper 8
280 uint16_t m_waveform[WAVEFORMS][WAVEFORM_LENGTH]; // waveforms
281 };
282
283
284
285 //*********************************************************
286 // IMPLEMENTATION CLASSES
287 //*********************************************************
288
289 // ======================> ym2414
290
291 class ym2414
292 {
293 public:
294 using fm_engine = fm_engine_base<opz_registers>;
295 static constexpr uint32_t OUTPUTS = fm_engine::OUTPUTS;
296 using output_data = fm_engine::output_data;
297
298 // constructor
299 ym2414(ymfm_interface &intf);
300
301 // reset
302 void reset();
303
304 // save/restore
305 void save_restore(ymfm_saved_state &state);
306
307 // pass-through helpers
308 uint32_t sample_rate(uint32_t input_clock) const { return m_fm.sample_rate(input_clock); }
309 void invalidate_caches() { m_fm.invalidate_caches(); }
310
311 // read access
312 uint8_t read_status();
313 uint8_t read(uint32_t offset);
314
315 // write access
316 void write_address(uint8_t data);
317 void write_data(uint8_t data);
318 void write(uint32_t offset, uint8_t data);
319
320 // generate one sample of sound
321 void generate(output_data *output, uint32_t numsamples = 1);
322
323 protected:
324 // internal state
325 uint8_t m_address; // address register
326 fm_engine m_fm; // core FM engine
327 };
328
329 }
330
331
332 #endif // YMFM_OPZ_H