openMSX
YM2413Burczynski.hh
Go to the documentation of this file.
1#ifndef YM2413BURCZYNSKI_HH
2#define YM2413BURCZYNSKI_HH
3
4#include "YM2413Core.hh"
5
6#include "FixedPoint.hh"
7#include "serialize_meta.hh"
8
9#include <array>
10#include <span>
11
12namespace openmsx {
13namespace YM2413Burczynski {
14
15// sin wave entries
16inline constexpr int SIN_BITS = 10;
17inline constexpr size_t SIN_LEN = 1 << SIN_BITS;
18inline constexpr size_t SIN_MASK = SIN_LEN - 1;
19
20class Channel;
21
25
26class Slot
27{
28public:
29 Slot();
30
31 void resetOperators();
32
36 void updateGenerators(const Channel& channel);
37
38 [[nodiscard]] int calcOutput(const Channel& channel, unsigned eg_cnt, bool carrier,
39 unsigned lfo_am, int phase);
40 [[nodiscard]] int calc_slot_mod(const Channel& channel, unsigned eg_cnt, bool carrier,
41 unsigned lfo_pm, unsigned lfo_am);
42 [[nodiscard]] int calc_envelope(const Channel& channel, unsigned eg_cnt, bool carrier);
43 [[nodiscard]] int calc_phase(const Channel& channel, unsigned lfo_pm);
44
45 enum class KeyPart : uint8_t { MAIN = 1, RHYTHM = 2 };
46 void setKeyOn(KeyPart part);
47 void setKeyOff(KeyPart part);
48 void setKeyOnOff(KeyPart part, bool enabled);
49
52 [[nodiscard]] bool isActive() const;
53
56 void setFrequencyMultiplier(uint8_t value);
57
60 void setKeyScaleRate(bool value);
61
65 void setEnvelopeSustained(bool value);
66
69 void setVibrato(bool value);
70
73 void setAmplitudeModulation(bool value);
74
77 void setTotalLevel(const Channel& channel, uint8_t value);
78
81 void setKeyScaleLevel(const Channel& channel, uint8_t value);
82
85 void setWaveform(uint8_t value);
86
89 void setFeedbackShift(uint8_t value);
90
93 void setAttackRate(const Channel& channel, uint8_t value);
94
97 void setDecayRate(const Channel& channel, uint8_t value);
98
101 void setReleaseRate(const Channel& channel, uint8_t value);
102
105 void setSustainLevel(uint8_t value);
106
109 void updateFrequency(const Channel& channel);
110
111 template<typename Archive>
112 void serialize(Archive& ar, unsigned version);
113
114public: // public for serialization, otherwise could be private
115 enum class EnvelopeState {
117 };
118
119private:
122 void setEnvelopeState(EnvelopeState state);
123
124 void updateTotalLevel(const Channel& channel);
125 void updateAttackRate(int kcodeScaled);
126 void updateDecayRate(int kcodeScaled);
127 void updateReleaseRate(int kcodeScaled);
128
129 std::span<const unsigned, SIN_LEN> waveTable; // waveform select
130
131 // Phase Generator
132 FreqIndex phase{0}; // frequency counter
133 FreqIndex freq{0}; // frequency counter step
134
135 // Envelope Generator
136 int TL{0}; // total level: TL << 2
137 int TLL{0}; // adjusted now TL
138 int egOut{0}; // envelope counter
139 int sl{0}; // sustain level: sl_tab[SL]
140 EnvelopeState state;
141
142 std::array<int, 2> op1_out = {0, 0}; // MOD output for feedback
143 bool eg_sustain{false}; // percussive/non-percussive mode
144 uint8_t fb_shift{0}; // feedback shift value
145
146 uint8_t key{0}; // 0 = KEY OFF, >0 = KEY ON
147
148 std::span<const uint8_t, 8> eg_sel_dp;
149 std::span<const uint8_t, 8> eg_sel_ar;
150 std::span<const uint8_t, 8> eg_sel_dr;
151 std::span<const uint8_t, 8> eg_sel_rr;
152 std::span<const uint8_t, 8> eg_sel_rs;
153 unsigned eg_mask_dp{0}; // == (1 << eg_sh_dp) - 1
154 unsigned eg_mask_ar{0}; // == (1 << eg_sh_ar) - 1
155 unsigned eg_mask_dr{0}; // == (1 << eg_sh_dr) - 1
156 unsigned eg_mask_rr{0}; // == (1 << eg_sh_rr) - 1
157 unsigned eg_mask_rs{0}; // == (1 << eg_sh_rs) - 1
158 uint8_t eg_sh_dp{0}; // (dump state)
159 uint8_t eg_sh_ar{0}; // (attack state)
160 uint8_t eg_sh_dr{0}; // (decay state)
161 uint8_t eg_sh_rr{0}; // (release state for non-perc.)
162 uint8_t eg_sh_rs{0}; // (release state for perc.mode)
163
164 uint8_t ar{0}; // attack rate: AR<<2
165 uint8_t dr{0}; // decay rate: DR<<2
166 uint8_t rr{0}; // release rate:RR<<2
167 uint8_t KSR{0}; // key scale rate
168 uint8_t ksl{0}; // key scale level
169 uint8_t mul{0}; // multiple: mul_tab[ML]
170
171 // LFO
172 uint8_t AMmask{0}; // LFO Amplitude Modulation enable mask
173 uint8_t vib{0}; // LFO Phase Modulation enable flag (active high)
174};
175
177{
178public:
181 [[nodiscard]] int calcOutput(unsigned eg_cnt, unsigned lfo_pm, unsigned lfo_am, int fm);
182
185 void setFrequency(int block_fnum);
186
189 void setFrequencyLow(uint8_t value);
190
193 void setFrequencyHigh(uint8_t value);
194
199 void updateInstrumentPart(int part, uint8_t value);
200
204 void updateInstrument(std::span<const uint8_t, 8> inst);
205
206 [[nodiscard]] int getBlockFNum() const;
207 [[nodiscard]] FreqIndex getFrequencyIncrement() const;
208 [[nodiscard]] int getKeyScaleLevelBase() const;
209 [[nodiscard]] uint8_t getKeyCode() const;
210 [[nodiscard]] bool isSustained() const;
211 void setSustain(bool sustained);
212
213 template<typename Archive>
214 void serialize(Archive& ar, unsigned version);
215
218
219private:
220 // phase generator state
221 int block_fnum{0}; // block+fnum
222 FreqIndex fc{0}; // Freq. increment base
223 int ksl_base{0}; // KeyScaleLevel Base step
224 bool sus{false}; // sus on/off (release speed in percussive mode)
225};
226
227class YM2413 final : public YM2413Core
228{
229public:
230 YM2413();
231
232 // YM2413Core
233 void reset() override;
234 void writePort(bool port, uint8_t value, int offset) override;
235 void pokeReg(uint8_t reg, uint8_t value) override;
236 [[nodiscard]] uint8_t peekReg(uint8_t reg) const override;
237 void generateChannels(std::span<float*, 9 + 5> bufs, unsigned num) override;
238 [[nodiscard]] float getAmplificationFactor() const override;
239
240 template<typename Archive>
241 void serialize(Archive& ar, unsigned version);
242
243private:
244 void writeReg(uint8_t reg, uint8_t value);
245
248 void resetOperators();
249
250 [[nodiscard]] bool isRhythm() const;
251
252 [[nodiscard]] Channel& getChannelForReg(uint8_t reg);
253
258 void updateCustomInstrument(int part, uint8_t value);
259
260 void setRhythmFlags(uint8_t old);
261
262private:
264 std::array<Channel, 9> channels;
265
267 unsigned eg_cnt;
268
270 int noise_rng;
271
273 unsigned idleSamples;
274
277 LFOAMIndex lfo_am_cnt{0};
278 LFOPMIndex lfo_pm_cnt{0};
279
286 std::array<std::array<uint8_t, 8>, 19> inst_tab;
287
289 std::array<uint8_t, 0x40> reg;
290 uint8_t registerLatch;
291};
292
293} // namespace YM2413Burczynski
294
298
299} // namespace openmsx
300
301#endif
void updateInstrument(std::span< const uint8_t, 8 > inst)
Sets all synthesis parameters as specified by the instrument.
void updateInstrumentPart(int part, uint8_t value)
Sets some synthesis parameters as specified by the instrument.
void setFrequencyLow(uint8_t value)
Changes the lower 8 bits of the frequency for this channel.
void serialize(Archive &ar, unsigned version)
void setFrequency(int block_fnum)
Sets the frequency for this channel.
void setFrequencyHigh(uint8_t value)
Changes the higher 4 bits of the frequency for this channel.
int calcOutput(unsigned eg_cnt, unsigned lfo_pm, unsigned lfo_am, int fm)
Calculate the value of the current sample produced by this channel.
void setSustainLevel(uint8_t value)
Sets the sustain level [0..15].
int calc_phase(const Channel &channel, unsigned lfo_pm)
int calc_slot_mod(const Channel &channel, unsigned eg_cnt, bool carrier, unsigned lfo_pm, unsigned lfo_am)
int calc_envelope(const Channel &channel, unsigned eg_cnt, bool carrier)
void setEnvelopeSustained(bool value)
Sets the envelope type of the current instrument.
void setKeyOnOff(KeyPart part, bool enabled)
void setAttackRate(const Channel &channel, uint8_t value)
Sets the attack rate [0..15].
void setFrequencyMultiplier(uint8_t value)
Sets the frequency multiplier [0..15].
void setReleaseRate(const Channel &channel, uint8_t value)
Sets the release rate [0..15].
void setWaveform(uint8_t value)
Sets the waveform: 0 = sinus, 1 = half sinus, half silence.
void updateFrequency(const Channel &channel)
Called by Channel when block_fnum changes.
int calcOutput(const Channel &channel, unsigned eg_cnt, bool carrier, unsigned lfo_am, int phase)
void serialize(Archive &ar, unsigned version)
void setVibrato(bool value)
Enables (true) or disables (false) vibrato.
void setAmplitudeModulation(bool value)
Enables (true) or disables (false) amplitude modulation.
void setDecayRate(const Channel &channel, uint8_t value)
Sets the decay rate [0..15].
void setFeedbackShift(uint8_t value)
Sets the amount of feedback [0..7].
void setKeyScaleLevel(const Channel &channel, uint8_t value)
Sets the key scale level: 0->0 / 1->1.5 / 2->3.0 / 3->6.0 dB/OCT.
void updateGenerators(const Channel &channel)
Update phase increment counter of operator.
void setTotalLevel(const Channel &channel, uint8_t value)
Sets the total level: [0..63].
bool isActive() const
Does this slot currently produce an output signal?
void setKeyScaleRate(bool value)
Sets the key scale rate: true->0, false->2.
void generateChannels(std::span< float *, 9+5 > bufs, unsigned num) override
Generate the sound output.
void writePort(bool port, uint8_t value, int offset) override
Write to the YM2413 register/data port.
void serialize(Archive &ar, unsigned version)
void reset() override
Reset this YM2413 core.
void pokeReg(uint8_t reg, uint8_t value) override
Write to a YM2413 register (for debug).
float getAmplificationFactor() const override
Returns normalization factor.
uint8_t peekReg(uint8_t reg) const override
Read from a YM2413 register (for debug).
Abstract interface for the YM2413 core.
Definition YM2413Core.hh:28
FixedPoint< 16 > FreqIndex
16.16 fixed point type for frequency calculations.
This file implemented 3 utility functions:
Definition Autofire.cc:11
#define SERIALIZE_CLASS_VERSION(CLASS, VERSION)