| e8555c89 Andy McCall |
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 <cstdint> |
| 11 |
#include <span> |
| 12 |
|
| 13 |
namespace openmsx { |
| 14 |
namespace YM2413Burczynski { |
| 15 |
|
| 16 |
|
| 17 |
inline constexpr int SIN_BITS = 10; |
| 18 |
inline constexpr size_t SIN_LEN = 1 << SIN_BITS; |
| 19 |
inline constexpr size_t SIN_MASK = SIN_LEN - 1; |
| 20 |
|
| 21 |
class Channel; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
using FreqIndex = FixedPoint<16>; |
| 26 |
|
| 27 |
class Slot |
| 28 |
{ |
| 29 |
public: |
| 30 |
Slot(); |
| 31 |
|
| 32 |
void resetOperators(); |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
void updateGenerators(const Channel& channel); |
| 38 |
|
| 39 |
[[nodiscard]] int calcOutput(const Channel& channel, unsigned eg_cnt, bool carrier, |
| 40 |
unsigned lfo_am, int phase); |
| 41 |
[[nodiscard]] int calc_slot_mod(const Channel& channel, unsigned eg_cnt, bool carrier, |
| 42 |
unsigned lfo_pm, unsigned lfo_am); |
| 43 |
[[nodiscard]] int calc_envelope(const Channel& channel, unsigned eg_cnt, bool carrier); |
| 44 |
[[nodiscard]] int calc_phase(const Channel& channel, unsigned lfo_pm); |
| 45 |
|
| 46 |
enum class KeyPart : uint8_t { MAIN = 1, RHYTHM = 2 }; |
| 47 |
void setKeyOn(KeyPart part); |
| 48 |
void setKeyOff(KeyPart part); |
| 49 |
void setKeyOnOff(KeyPart part, bool enabled); |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
[[nodiscard]] bool isActive() const; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
void setFrequencyMultiplier(uint8_t value); |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
void setKeyScaleRate(bool value); |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
void setEnvelopeSustained(bool value); |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
void setVibrato(bool value); |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
void setAmplitudeModulation(bool value); |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
void setTotalLevel(const Channel& channel, uint8_t value); |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
void setKeyScaleLevel(const Channel& channel, uint8_t value); |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
void setWaveform(uint8_t value); |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
void setFeedbackShift(uint8_t value); |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
void setAttackRate(const Channel& channel, uint8_t value); |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
void setDecayRate(const Channel& channel, uint8_t value); |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
void setReleaseRate(const Channel& channel, uint8_t value); |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
void setSustainLevel(uint8_t value); |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
void updateFrequency(const Channel& channel); |
| 111 |
|
| 112 |
template<typename Archive> |
| 113 |
void serialize(Archive& ar, unsigned version); |
| 114 |
|
| 115 |
public: |
| 116 |
enum class EnvelopeState : uint8_t { |
| 117 |
DUMP, ATTACK, DECAY, SUSTAIN, RELEASE, OFF |
| 118 |
}; |
| 119 |
|
| 120 |
private: |
| 121 |
|
| 122 |
|
| 123 |
void setEnvelopeState(EnvelopeState state); |
| 124 |
|
| 125 |
void updateTotalLevel(const Channel& channel); |
| 126 |
void updateAttackRate(int kcodeScaled); |
| 127 |
void updateDecayRate(int kcodeScaled); |
| 128 |
void updateReleaseRate(int kcodeScaled); |
| 129 |
|
| 130 |
std::span<const unsigned, SIN_LEN> waveTable; |
| 131 |
|
| 132 |
|
| 133 |
FreqIndex phase{0}; |
| 134 |
FreqIndex freq{0}; |
| 135 |
|
| 136 |
|
| 137 |
int TL{0}; |
| 138 |
int TLL{0}; |
| 139 |
int egOut{0}; |
| 140 |
int sl{0}; |
| 141 |
EnvelopeState state; |
| 142 |
|
| 143 |
std::array<int, 2> op1_out = {0, 0}; |
| 144 |
bool eg_sustain{false}; |
| 145 |
uint8_t fb_shift{0}; |
| 146 |
|
| 147 |
uint8_t key{0}; |
| 148 |
|
| 149 |
std::span<const uint8_t, 8> eg_sel_dp; |
| 150 |
std::span<const uint8_t, 8> eg_sel_ar; |
| 151 |
std::span<const uint8_t, 8> eg_sel_dr; |
| 152 |
std::span<const uint8_t, 8> eg_sel_rr; |
| 153 |
std::span<const uint8_t, 8> eg_sel_rs; |
| 154 |
unsigned eg_mask_dp{0}; |
| 155 |
unsigned eg_mask_ar{0}; |
| 156 |
unsigned eg_mask_dr{0}; |
| 157 |
unsigned eg_mask_rr{0}; |
| 158 |
unsigned eg_mask_rs{0}; |
| 159 |
uint8_t eg_sh_dp{0}; |
| 160 |
uint8_t eg_sh_ar{0}; |
| 161 |
uint8_t eg_sh_dr{0}; |
| 162 |
uint8_t eg_sh_rr{0}; |
| 163 |
uint8_t eg_sh_rs{0}; |
| 164 |
|
| 165 |
uint8_t ar{0}; |
| 166 |
uint8_t dr{0}; |
| 167 |
uint8_t rr{0}; |
| 168 |
uint8_t KSR{0}; |
| 169 |
uint8_t ksl{0}; |
| 170 |
uint8_t mul{0}; |
| 171 |
|
| 172 |
|
| 173 |
uint8_t AMmask{0}; |
| 174 |
uint8_t vib{0}; |
| 175 |
}; |
| 176 |
|
| 177 |
class Channel |
| 178 |
{ |
| 179 |
public: |
| 180 |
|
| 181 |
|
| 182 |
[[nodiscard]] int calcOutput(unsigned eg_cnt, unsigned lfo_pm, unsigned lfo_am, int fm); |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
void setFrequency(int block_fnum); |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
void setFrequencyLow(uint8_t value); |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
void setFrequencyHigh(uint8_t value); |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
void updateInstrumentPart(int part, uint8_t value); |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
void updateInstrument(std::span<const uint8_t, 8> inst); |
| 206 |
|
| 207 |
[[nodiscard]] int getBlockFNum() const; |
| 208 |
[[nodiscard]] FreqIndex getFrequencyIncrement() const; |
| 209 |
[[nodiscard]] int getKeyScaleLevelBase() const; |
| 210 |
[[nodiscard]] uint8_t getKeyCode() const; |
| 211 |
[[nodiscard]] bool isSustained() const; |
| 212 |
void setSustain(bool sustained); |
| 213 |
|
| 214 |
template<typename Archive> |
| 215 |
void serialize(Archive& ar, unsigned version); |
| 216 |
|
| 217 |
Slot mod; |
| 218 |
Slot car; |
| 219 |
|
| 220 |
private: |
| 221 |
|
| 222 |
int block_fnum{0}; |
| 223 |
FreqIndex fc{0}; |
| 224 |
int ksl_base{0}; |
| 225 |
bool sus{false}; |
| 226 |
}; |
| 227 |
|
| 228 |
class YM2413 final : public YM2413Core |
| 229 |
{ |
| 230 |
public: |
| 231 |
YM2413(); |
| 232 |
|
| 233 |
|
| 234 |
void reset() override; |
| 235 |
void writePort(bool port, uint8_t value, int offset) override; |
| 236 |
void pokeReg(uint8_t reg, uint8_t value) override; |
| 237 |
[[nodiscard]] std::span<const uint8_t, 64> peekRegs() const override; |
| 238 |
void generateChannels(std::span<float*, 9 + 5> bufs, unsigned num) override; |
| 239 |
[[nodiscard]] float getAmplificationFactor() const override; |
| 240 |
|
| 241 |
template<typename Archive> |
| 242 |
void serialize(Archive& ar, unsigned version); |
| 243 |
|
| 244 |
private: |
| 245 |
void writeReg(uint8_t reg, uint8_t value); |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
void resetOperators(); |
| 250 |
|
| 251 |
[[nodiscard]] bool isRhythm() const; |
| 252 |
|
| 253 |
[[nodiscard]] Channel& getChannelForReg(uint8_t reg); |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
void updateCustomInstrument(int part, uint8_t value); |
| 260 |
|
| 261 |
void setRhythmFlags(uint8_t old); |
| 262 |
|
| 263 |
private: |
| 264 |
|
| 265 |
std::array<Channel, 9> channels; |
| 266 |
|
| 267 |
|
| 268 |
unsigned eg_cnt; |
| 269 |
|
| 270 |
|
| 271 |
int noise_rng; |
| 272 |
|
| 273 |
|
| 274 |
unsigned idleSamples; |
| 275 |
|
| 276 |
using LFOAMIndex = FixedPoint< 6>; |
| 277 |
using LFOPMIndex = FixedPoint<10>; |
| 278 |
LFOAMIndex lfo_am_cnt{0}; |
| 279 |
LFOPMIndex lfo_pm_cnt{0}; |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
std::array<std::array<uint8_t, 8>, 19> inst_tab; |
| 288 |
|
| 289 |
|
| 290 |
std::array<uint8_t, 0x40> reg; |
| 291 |
uint8_t registerLatch; |
| 292 |
}; |
| 293 |
|
| 294 |
} |
| 295 |
|
| 296 |
SERIALIZE_CLASS_VERSION(YM2413Burczynski::YM2413, 4); |
| 297 |
SERIALIZE_CLASS_VERSION(YM2413Burczynski::Channel, 3); |
| 298 |
SERIALIZE_CLASS_VERSION(YM2413Burczynski::Slot, 2); |
| 299 |
|
| 300 |
} |
| 301 |
|
| 302 |
#endif |