Debian Codegraph @

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