Debian Codegraph @

thirdparty/ymfm/src/ymfm_opx.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_OPX_H
32 #define YMFM_OPX_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 // ======================> opx_registers
47
48 //
49 // OPX register map:
50 //
51 // System-wide registers:
52 //
53 // Per-channel registers (channel in address bits 0-2)
54 //
55 // Per-operator registers (4 banks):
56 // 00-0F x------- Enable
57 // -xxxx--- EXT out
58 // -------x Key on
59 // 10-1F xxxxxxxx LFO frequency
60 // 20-2F xx------ AM sensitivity (0-3)
61 // --xxx--- PM sensitivity (0-7)
62 // ------xx LFO waveform (0=disable, 1=saw, 2=
63 // 30-3F -xxx---- Detune (0-7)
64 // ----xxxx Multiple (0-15)
65 // 40-4F -xxxxxxx Total level (0-127)
66 // 50-5F xxx----- Key scale (0-7)
67 // ---xxxxx Attack rate (0-31)
68 // 60-6F ---xxxxx Decay rate (0-31)
69 // 70-7F ---xxxxx Sustain rate (0-31)
70 // 80-8F xxxx---- Sustain level (0-15)
71 // ----xxxx Release rate (0-15)
72 // 90-9F xxxxxxxx Frequency number (low 8 bits)
73 // A0-AF xxxx---- Block (0-15)
74 // ----xxxx Frequency number (high 4 bits)
75 // B0-BF x------- Acc on
76 // -xxx---- Feedback level (0-7)
77 // -----xxx Waveform (0-7, 7=PCM)
78 // C0-CF ----xxxx Algorithm (0-15)
79 // D0-DF xxxx---- CH0 level (0-15)
80 // ----xxxx CH1 level (0-15)
81 // E0-EF xxxx---- CH2 level (0-15)
82 // ----xxxx CH3 level (0-15)
83 //
84
85 class opx_registers : public fm_registers_base
86 {
87 // LFO waveforms are 256 entries long
88 static constexpr uint32_t LFO_WAVEFORM_LENGTH = 256;
89
90 public:
91 // constants
92 static constexpr uint32_t OUTPUTS = 8;
93 static constexpr uint32_t CHANNELS = 24;
94 static constexpr uint32_t ALL_CHANNELS = (1 << CHANNELS) - 1;
95 static constexpr uint32_t OPERATORS = CHANNELS * 2;
96 static constexpr uint32_t WAVEFORMS = 8;
97 static constexpr uint32_t REGISTERS = 0x800;
98 static constexpr uint32_t DEFAULT_PRESCALE = 8;
99 static constexpr uint32_t EG_CLOCK_DIVIDER = 2;
100 static constexpr uint32_t CSM_TRIGGER_MASK = ALL_CHANNELS;
101 static constexpr uint32_t REG_MODE = 0x14;
102 static constexpr uint8_t STATUS_TIMERA = 0x01;
103 static constexpr uint8_t STATUS_TIMERB = 0x02;
104 static constexpr uint8_t STATUS_BUSY = 0x80;
105 static constexpr uint8_t STATUS_IRQ = 0;
106
107 // constructor
108 opz_registers();
109
110 // reset to initial state
111 void reset();
112
113 // save/restore
114 void save_restore(ymfm_saved_state &state);
115
116 // map channel number to register offset
117 static constexpr uint32_t channel_offset(uint32_t chnum)
118 {
119 assert(chnum < CHANNELS);
120 return chnum;
121 }
122
123 // map operator number to register offset
124 static constexpr uint32_t operator_offset(uint32_t opnum)
125 {
126 assert(opnum < OPERATORS);
127 return opnum;
128 }
129
130 // return an array of operator indices for each channel
131 struct operator_mapping { uint32_t chan[CHANNELS]; };
132 void operator_map(operator_mapping &dest) const;
133
134 // handle writes to the register array
135 bool write(uint16_t index, uint8_t data, uint32_t &chan, uint32_t &opmask);
136
137 // clock the noise and LFO, if present, returning LFO PM value
138 int32_t clock_noise_and_lfo();
139
140 // return the AM offset from LFO for the given channel
141 uint32_t lfo_am_offset(uint32_t choffs) const;
142
143 // return the current noise state, gated by the noise clock
144 uint32_t noise_state() const { return m_noise_state; }
145
146 // caching helpers
147 void cache_operator_data(uint32_t choffs, uint32_t opoffs, opdata_cache &cache);
148
149 // compute the phase step, given a PM value
150 uint32_t compute_phase_step(uint32_t choffs, uint32_t opoffs, opdata_cache const &cache, int32_t lfo_raw_pm);
151
152 // log a key-on event
153 std::string log_keyon(uint32_t choffs, uint32_t opoffs);
154
155 // system-wide registers
156 uint32_t noise_frequency() const { return byte(0x0f, 0, 5); }
157 uint32_t noise_enable() const { return byte(0x0f, 7, 1); }
158 uint32_t timer_a_value() const { return word(0x10, 0, 8, 0x11, 0, 2); }
159 uint32_t timer_b_value() const { return byte(0x12, 0, 8); }
160 uint32_t csm() const { return byte(0x14, 7, 1); }
161 uint32_t reset_timer_b() const { return byte(0x14, 5, 1); }
162 uint32_t reset_timer_a() const { return byte(0x14, 4, 1); }
163 uint32_t enable_timer_b() const { return byte(0x14, 3, 1); }
164 uint32_t enable_timer_a() const { return byte(0x14, 2, 1); }
165 uint32_t load_timer_b() const { return byte(0x14, 1, 1); }
166 uint32_t load_timer_a() const { return byte(0x14, 0, 1); }
167 uint32_t lfo2_pm_depth() const { return byte(0x148, 0, 7); } // fake
168 uint32_t lfo2_rate() const { return byte(0x16, 0, 8); }
169 uint32_t lfo2_am_depth() const { return byte(0x17, 0, 7); }
170 uint32_t lfo_rate() const { return byte(0x18, 0, 8); }
171 uint32_t lfo_am_depth() const { return byte(0x19, 0, 7); }
172 uint32_t lfo_pm_depth() const { return byte(0x149, 0, 7); } // fake
173 uint32_t output_bits() const { return byte(0x1b, 6, 2); }
174 uint32_t lfo2_sync() const { return byte(0x1b, 5, 1); }
175 uint32_t lfo_sync() const { return byte(0x1b, 4, 1); }
176 uint32_t lfo2_waveform() const { return byte(0x1b, 2, 2); }
177 uint32_t lfo_waveform() const { return byte(0x1b, 0, 2); }
178
179 // per-channel registers
180 uint32_t ch_volume(uint32_t choffs) const { return byte(0x00, 0, 8, choffs); }
181 uint32_t ch_output_any(uint32_t choffs) const { return byte(0x20, 7, 1, choffs) | byte(0x30, 0, 1, choffs); }
182 uint32_t ch_output_0(uint32_t choffs) const { return byte(0x30, 0, 1, choffs); }
183 uint32_t ch_output_1(uint32_t choffs) const { return byte(0x20, 7, 1, choffs) | byte(0x30, 0, 1, choffs); }
184 uint32_t ch_output_2(uint32_t choffs) const { return 0; }
185 uint32_t ch_output_3(uint32_t choffs) const { return 0; }
186 uint32_t ch_key_on(uint32_t choffs) const { return byte(0x20, 6, 1, choffs); }
187 uint32_t ch_feedback(uint32_t choffs) const { return byte(0x20, 3, 3, choffs); }
188 uint32_t ch_algorithm(uint32_t choffs) const { return byte(0x20, 0, 3, choffs); }
189 uint32_t ch_block_freq(uint32_t choffs) const { return word(0x28, 0, 7, 0x30, 2, 6, choffs); }
190 uint32_t ch_lfo_pm_sens(uint32_t choffs) const { return byte(0x38, 4, 3, choffs); }
191 uint32_t ch_lfo_am_sens(uint32_t choffs) const { return byte(0x38, 0, 2, choffs); }
192 uint32_t ch_lfo2_pm_sens(uint32_t choffs) const { return byte(0x140, 4, 3, choffs); } // fake
193 uint32_t ch_lfo2_am_sens(uint32_t choffs) const { return byte(0x140, 0, 2, choffs); } // fake
194
195 // per-operator registers
196 uint32_t op_detune(uint32_t opoffs) const { return byte(0x40, 4, 3, opoffs); }
197 uint32_t op_multiple(uint32_t opoffs) const { return byte(0x40, 0, 4, opoffs); }
198 uint32_t op_fix_range(uint32_t opoffs) const { return byte(0x40, 4, 3, opoffs); }
199 uint32_t op_fix_frequency(uint32_t opoffs) const { return byte(0x40, 0, 4, opoffs); }
200 uint32_t op_waveform(uint32_t opoffs) const { return byte(0x100, 4, 3, opoffs); } // fake
201 uint32_t op_fine(uint32_t opoffs) const { return byte(0x100, 0, 4, opoffs); } // fake
202 uint32_t op_total_level(uint32_t opoffs) const { return byte(0x60, 0, 7, opoffs); }
203 uint32_t op_ksr(uint32_t opoffs) const { return byte(0x80, 6, 2, opoffs); }
204 uint32_t op_fix_mode(uint32_t opoffs) const { return byte(0x80, 5, 1, opoffs); }
205 uint32_t op_attack_rate(uint32_t opoffs) const { return byte(0x80, 0, 5, opoffs); }
206 uint32_t op_lfo_am_enable(uint32_t opoffs) const { return byte(0xa0, 7, 1, opoffs); }
207 uint32_t op_decay_rate(uint32_t opoffs) const { return byte(0xa0, 0, 5, opoffs); }
208 uint32_t op_detune2(uint32_t opoffs) const { return byte(0xc0, 6, 2, opoffs); }
209 uint32_t op_sustain_rate(uint32_t opoffs) const { return byte(0xc0, 0, 5, opoffs); }
210 uint32_t op_eg_shift(uint32_t opoffs) const { return byte(0x120, 6, 2, opoffs); } // fake
211 uint32_t op_reverb_rate(uint32_t opoffs) const { return byte(0x120, 0, 3, opoffs); } // fake
212 uint32_t op_sustain_level(uint32_t opoffs) const { return byte(0xe0, 4, 4, opoffs); }
213 uint32_t op_release_rate(uint32_t opoffs) const { return byte(0xe0, 0, 4, opoffs); }
214
215 protected:
216 // return a bitfield extracted from a byte
217 uint32_t byte(uint32_t offset, uint32_t start, uint32_t count, uint32_t extra_offset = 0) const
218 {
219 return bitfield(m_regdata[offset + extra_offset], start, count);
220 }
221
222 // return a bitfield extracted from a pair of bytes, MSBs listed first
223 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
224 {
225 return (byte(offset1, start1, count1, extra_offset) << count2) | byte(offset2, start2, count2, extra_offset);
226 }
227
228 // internal state
229 uint32_t m_lfo_counter[2]; // LFO counter
230 uint32_t m_noise_lfsr; // noise LFSR state
231 uint8_t m_noise_counter; // noise counter
232 uint8_t m_noise_state; // latched noise state
233 uint8_t m_noise_lfo; // latched LFO noise value
234 uint8_t m_lfo_am[2]; // current LFO AM value
235 uint8_t m_regdata[REGISTERS]; // register data
236 uint16_t m_phase_substep[OPERATORS]; // phase substep for fixed frequency
237 int16_t m_lfo_waveform[4][LFO_WAVEFORM_LENGTH]; // LFO waveforms; AM in low 8, PM in upper 8
238 uint16_t m_waveform[WAVEFORMS][WAVEFORM_LENGTH]; // waveforms
239 };
240
241
242
243 //*********************************************************
244 // IMPLEMENTATION CLASSES
245 //*********************************************************
246
247 // ======================> ym2414
248
249 class ym2414
250 {
251 public:
252 using fm_engine = fm_engine_base<opz_registers>;
253 static constexpr uint32_t OUTPUTS = fm_engine::OUTPUTS;
254 using output_data = fm_engine::output_data;
255
256 // constructor
257 ym2414(ymfm_interface &intf);
258
259 // reset
260 void reset();
261
262 // save/restore
263 void save_restore(ymfm_saved_state &state);
264
265 // pass-through helpers
266 uint32_t sample_rate(uint32_t input_clock) const { return m_fm.sample_rate(input_clock); }
267 void invalidate_caches() { m_fm.invalidate_caches(); }
268
269 // read access
270 uint8_t read_status();
271 uint8_t read(uint32_t offset);
272
273 // write access
274 void write_address(uint8_t data);
275 void write_data(uint8_t data);
276 void write(uint32_t offset, uint8_t data);
277
278 // generate one sample of sound
279 void generate(output_data *output, uint32_t numsamples = 1);
280
281 protected:
282 // internal state
283 uint8_t m_address; // address register
284 fm_engine m_fm; // core FM engine
285 };
286
287 }
288
289
290 #endif // YMFM_OPZ_H