Debian Codegraph @

thirdparty/ymfm/src/ymfm_opq.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_OPQ_H
32 #define YMFM_OPQ_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 // ======================> opq_registers
47
48 //
49 // OPQ register map:
50 //
51 // System-wide registers:
52 // 03 xxxxxxxx Timer control (unknown; 0x71 causes interrupts at ~10ms)
53 // 04 ----x--- LFO disable
54 // -----xxx LFO frequency (0=~4Hz, 6=~10Hz, 7=~47Hz)
55 // 05 -x------ Key on/off operator 4
56 // --x----- Key on/off operator 3
57 // ---x---- Key on/off operator 2
58 // ----x--- Key on/off operator 1
59 // -----xxx Channel select
60 //
61 // Per-channel registers (channel in address bits 0-2)
62 // 10-17 x------- Pan right
63 // -x------ Pan left
64 // --xxx--- Feedback level for operator 1 (0-7)
65 // -----xxx Operator connection algorithm (0-7)
66 // 18-1F x------- Reverb
67 // -xxx---- PM sensitivity
68 // ------xx AM shift
69 // 20-27 -xxx---- Block (0-7), Operator 2 & 4
70 // ----xxxx Frequency number upper 4 bits, Operator 2 & 4
71 // 28-2F -xxx---- Block (0-7), Operator 1 & 3
72 // ----xxxx Frequency number upper 4 bits, Operator 1 & 3
73 // 30-37 xxxxxxxx Frequency number lower 8 bits, Operator 2 & 4
74 // 38-3F xxxxxxxx Frequency number lower 8 bits, Operator 1 & 3
75 //
76 // Per-operator registers (channel in address bits 0-2, operator in bits 3-4)
77 // 40-5F 0-xxxxxx Detune value (0-63)
78 // 1---xxxx Multiple value (0-15)
79 // 60-7F -xxxxxxx Total level (0-127)
80 // 80-9F xx------ Key scale rate (0-3)
81 // ---xxxxx Attack rate (0-31)
82 // A0-BF x------- LFO AM enable, retrigger disable
83 // x------ Waveform select
84 // ---xxxxx Decay rate (0-31)
85 // C0-DF ---xxxxx Sustain rate (0-31)
86 // E0-FF xxxx---- Sustain level (0-15)
87 // ----xxxx Release rate (0-15)
88 //
89 // Diffs from OPM:
90 // - 2 frequencies/channel
91 // - retrigger disable
92 // - 2 waveforms
93 // - uses FNUM
94 // - reverb behavior
95 // - larger detune range
96 //
97 // Questions:
98 // - timer information is pretty light
99 // - how does echo work?
100 // -
101
102 class opq_registers : public fm_registers_base
103 {
104 public:
105 // constants
106 static constexpr uint32_t OUTPUTS = 2;
107 static constexpr uint32_t CHANNELS = 8;
108 static constexpr uint32_t ALL_CHANNELS = (1 << CHANNELS) - 1;
109 static constexpr uint32_t OPERATORS = CHANNELS * 4;
110 static constexpr uint32_t WAVEFORMS = 2;
111 static constexpr uint32_t REGISTERS = 0x120;
112 static constexpr uint32_t REG_MODE = 0x03;
113 static constexpr uint32_t DEFAULT_PRESCALE = 2;
114 static constexpr uint32_t EG_CLOCK_DIVIDER = 3;
115 static constexpr bool EG_HAS_REVERB = true;
116 static constexpr bool MODULATOR_DELAY = false;
117 static constexpr uint32_t CSM_TRIGGER_MASK = ALL_CHANNELS;
118 static constexpr uint8_t STATUS_TIMERA = 0;
119 static constexpr uint8_t STATUS_TIMERB = 0x04;
120 static constexpr uint8_t STATUS_BUSY = 0x80;
121 static constexpr uint8_t STATUS_IRQ = 0;
122
123 // constructor
124 opq_registers();
125
126 // reset to initial state
127 void reset();
128
129 // save/restore
130 void save_restore(ymfm_saved_state &state);
131
132 // map channel number to register offset
133 static constexpr uint32_t channel_offset(uint32_t chnum)
134 {
135 assert(chnum < CHANNELS);
136 return chnum;
137 }
138
139 // map operator number to register offset
140 static constexpr uint32_t operator_offset(uint32_t opnum)
141 {
142 assert(opnum < OPERATORS);
143 return opnum;
144 }
145
146 // return an array of operator indices for each channel
147 struct operator_mapping { uint32_t chan[CHANNELS]; };
148 void operator_map(operator_mapping &dest) const;
149
150 // handle writes to the register array
151 bool write(uint16_t index, uint8_t data, uint32_t &chan, uint32_t &opmask);
152
153 // clock the noise and LFO, if present, returning LFO PM value
154 int32_t clock_noise_and_lfo();
155
156 // reset the LFO
157 void reset_lfo() { m_lfo_counter = 0; }
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 0; }
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 timer_a_value() const { return 0; }
176 uint32_t timer_b_value() const { return byte(0x03, 2, 6) | 0xc0; } // ???
177 uint32_t csm() const { return 0; }
178 uint32_t reset_timer_b() const { return byte(0x03, 0, 1); } // ???
179 uint32_t reset_timer_a() const { return 0; }
180 uint32_t enable_timer_b() const { return byte(0x03, 0, 1); } // ???
181 uint32_t enable_timer_a() const { return 0; }
182 uint32_t load_timer_b() const { return byte(0x03, 0, 1); } // ???
183 uint32_t load_timer_a() const { return 0; }
184 uint32_t lfo_enable() const { return byte(0x04, 3, 1) ^ 1; }
185 uint32_t lfo_rate() const { return byte(0x04, 0, 3); }
186
187 // per-channel registers
188 uint32_t ch_output_any(uint32_t choffs) const { return byte(0x10, 6, 2, choffs); }
189 uint32_t ch_output_0(uint32_t choffs) const { return byte(0x10, 6, 1, choffs); }
190 uint32_t ch_output_1(uint32_t choffs) const { return byte(0x10, 7, 1, choffs); }
191 uint32_t ch_output_2(uint32_t choffs) const { return 0; }
192 uint32_t ch_output_3(uint32_t choffs) const { return 0; }
193 uint32_t ch_feedback(uint32_t choffs) const { return byte(0x10, 3, 3, choffs); }
194 uint32_t ch_algorithm(uint32_t choffs) const { return byte(0x10, 0, 3, choffs); }
195 uint32_t ch_reverb(uint32_t choffs) const { return byte(0x18, 7, 1, choffs); }
196 uint32_t ch_lfo_pm_sens(uint32_t choffs) const { return byte(0x18, 4, 3, choffs); }
197 uint32_t ch_lfo_am_sens(uint32_t choffs) const { return byte(0x18, 0, 2, choffs); }
198 uint32_t ch_block_freq_24(uint32_t choffs) const { return word(0x20, 0, 7, 0x30, 0, 8, choffs); }
199 uint32_t ch_block_freq_13(uint32_t choffs) const { return word(0x28, 0, 7, 0x38, 0, 8, choffs); }
200
201 // per-operator registers
202 uint32_t op_detune(uint32_t opoffs) const { return byte(0x40, 0, 6, opoffs); }
203 uint32_t op_multiple(uint32_t opoffs) const { return byte(0x100, 0, 4, opoffs); }
204 uint32_t op_total_level(uint32_t opoffs) const { return byte(0x60, 0, 7, opoffs); }
205 uint32_t op_ksr(uint32_t opoffs) const { return byte(0x80, 6, 2, opoffs); }
206 uint32_t op_attack_rate(uint32_t opoffs) const { return byte(0x80, 0, 5, opoffs); }
207 uint32_t op_lfo_am_enable(uint32_t opoffs) const { return byte(0xa0, 7, 1, opoffs); }
208 uint32_t op_waveform(uint32_t opoffs) const { return byte(0xa0, 6, 1, opoffs); }
209 uint32_t op_decay_rate(uint32_t opoffs) const { return byte(0xa0, 0, 5, opoffs); }
210 uint32_t op_sustain_rate(uint32_t opoffs) const { return byte(0xc0, 0, 5, opoffs); }
211 uint32_t op_sustain_level(uint32_t opoffs) const { return byte(0xe0, 4, 4, opoffs); }
212 uint32_t op_release_rate(uint32_t opoffs) const { return byte(0xe0, 0, 4, opoffs); }
213
214 protected:
215 // return a bitfield extracted from a byte
216 uint32_t byte(uint32_t offset, uint32_t start, uint32_t count, uint32_t extra_offset = 0) const
217 {
218 return bitfield(m_regdata[offset + extra_offset], start, count);
219 }
220
221 // return a bitfield extracted from a pair of bytes, MSBs listed first
222 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
223 {
224 return (byte(offset1, start1, count1, extra_offset) << count2) | byte(offset2, start2, count2, extra_offset);
225 }
226
227 // internal state
228 uint32_t m_lfo_counter; // LFO counter
229 uint8_t m_lfo_am; // current LFO AM value
230 uint8_t m_regdata[REGISTERS]; // register data
231 uint16_t m_waveform[WAVEFORMS][WAVEFORM_LENGTH]; // waveforms
232 };
233
234
235
236 //*********************************************************
237 // IMPLEMENTATION CLASSES
238 //*********************************************************
239
240 // ======================> ym3806
241
242 class ym3806
243 {
244 public:
245 using fm_engine = fm_engine_base<opq_registers>;
246 static constexpr uint32_t OUTPUTS = fm_engine::OUTPUTS;
247 using output_data = fm_engine::output_data;
248
249 // constructor
250 ym3806(ymfm_interface &intf);
251
252 // reset
253 void reset();
254
255 // save/restore
256 void save_restore(ymfm_saved_state &state);
257
258 // pass-through helpers
259 uint32_t sample_rate(uint32_t input_clock) const { return m_fm.sample_rate(input_clock); }
260 void invalidate_caches() { m_fm.invalidate_caches(); }
261
262 // read access
263 uint8_t read_status();
264 uint8_t read(uint32_t offset);
265
266 // write access
267 void write_address(uint8_t data) { /* not supported; only direct writes */ }
268 void write_data(uint8_t data) { /* not supported; only direct writes */ }
269 void write(uint32_t offset, uint8_t data);
270
271 // generate one sample of sound
272 void generate(output_data *output, uint32_t numsamples = 1);
273
274 protected:
275 // internal state
276 fm_engine m_fm; // core FM engine
277 };
278
279
280 // ======================> ym3533
281
282 class ym3533 : public ym3806
283 {
284 public:
285 // constructor
286 ym3533(ymfm_interface &intf) :
287 ym3806(intf) { }
288 };
289
290 }
291
292
293 #endif // YMFM_OPQ_H