| 1 |
#ifndef YM2413OKAZAKI_HH |
| 2 |
#define YM2413OKAZAKI_HH |
| 3 |
|
| 4 |
#include "YM2413Core.hh" |
| 5 |
|
| 6 |
#include "serialize_meta.hh" |
| 7 |
|
| 8 |
#include "FixedPoint.hh" |
| 9 |
|
| 10 |
#include <array> |
| 11 |
#include <cstdint> |
| 12 |
#include <span> |
| 13 |
|
| 14 |
namespace openmsx { |
| 15 |
namespace YM2413Okazaki { |
| 16 |
|
| 17 |
class YM2413; |
| 18 |
|
| 19 |
inline constexpr int EP_FP_BITS = 15; |
| 20 |
using EnvPhaseIndex = FixedPoint<EP_FP_BITS>; |
| 21 |
|
| 22 |
|
| 23 |
inline constexpr int PG_BITS = 9; |
| 24 |
inline constexpr int PG_WIDTH = 1 << PG_BITS; |
| 25 |
inline constexpr int PG_MASK = PG_WIDTH - 1; |
| 26 |
|
| 27 |
class Patch { |
| 28 |
public: |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
Patch(); |
| 33 |
|
| 34 |
void initModulator(std::span<const uint8_t, 8> data); |
| 35 |
void initCarrier (std::span<const uint8_t, 8> data); |
| 36 |
|
| 37 |
|
| 38 |
void setKR(uint8_t value); |
| 39 |
|
| 40 |
void setML(uint8_t value); |
| 41 |
|
| 42 |
void setKL(uint8_t value); |
| 43 |
|
| 44 |
void setTL(uint8_t value); |
| 45 |
|
| 46 |
void setWF(uint8_t value); |
| 47 |
|
| 48 |
void setFB(uint8_t value); |
| 49 |
|
| 50 |
void setSL(uint8_t value); |
| 51 |
|
| 52 |
std::span<const unsigned, PG_WIDTH> WF; |
| 53 |
std::span<const uint8_t, 16 * 8> KL; |
| 54 |
unsigned SL; |
| 55 |
uint8_t AMPM = 0; |
| 56 |
bool EG = false; |
| 57 |
uint8_t KR; |
| 58 |
uint8_t ML; |
| 59 |
uint8_t TL; |
| 60 |
uint8_t FB; |
| 61 |
uint8_t AR = 0; |
| 62 |
uint8_t DR = 0; |
| 63 |
uint8_t RR = 0; |
| 64 |
}; |
| 65 |
|
| 66 |
class Slot { |
| 67 |
public: |
| 68 |
enum class EnvelopeState : uint8_t { |
| 69 |
ATTACK, DECAY, SUSHOLD, SUSTAIN, RELEASE, SETTLE, FINISH |
| 70 |
}; |
| 71 |
|
| 72 |
Slot(); |
| 73 |
void reset(); |
| 74 |
|
| 75 |
void setEnvelopeState(EnvelopeState state); |
| 76 |
[[nodiscard]] bool isActive() const; |
| 77 |
|
| 78 |
void slotOn(); |
| 79 |
void slotOn2(); |
| 80 |
void slotOff(); |
| 81 |
void setPatch(const Patch& patch); |
| 82 |
void setVolume(unsigned value); |
| 83 |
|
| 84 |
[[nodiscard]] unsigned calc_phase(unsigned lfo_pm); |
| 85 |
template<bool HAS_AM, bool FIXED_ENV> |
| 86 |
[[nodiscard]] unsigned calc_envelope(int lfo_am, unsigned fixed_env); |
| 87 |
template<bool HAS_AM> [[nodiscard]] unsigned calc_fixed_env() const; |
| 88 |
void calc_envelope_outline(unsigned& out); |
| 89 |
template<bool HAS_AM, bool FIXED_ENV> |
| 90 |
[[nodiscard]] int calc_slot_car(unsigned lfo_pm, int lfo_am, int fm, unsigned fixed_env); |
| 91 |
template<bool HAS_AM, bool HAS_FB, bool FIXED_ENV> |
| 92 |
[[nodiscard]] int calc_slot_mod(unsigned lfo_pm, int lfo_am, unsigned fixed_env); |
| 93 |
|
| 94 |
[[nodiscard]] int calc_slot_tom(); |
| 95 |
[[nodiscard]] int calc_slot_snare(bool noise); |
| 96 |
[[nodiscard]] int calc_slot_cym(unsigned phase7, unsigned phase8); |
| 97 |
[[nodiscard]] int calc_slot_hat(unsigned phase7, unsigned phase8, bool noise); |
| 98 |
void updatePG(unsigned freq); |
| 99 |
void updateTLL(unsigned freq, bool actAsCarrier); |
| 100 |
void updateRKS(unsigned freq); |
| 101 |
void updateEG(); |
| 102 |
void updateAll(unsigned freq, bool actAsCarrier); |
| 103 |
|
| 104 |
template<typename Archive> |
| 105 |
void serialize(Archive& ar, unsigned version); |
| 106 |
|
| 107 |
|
| 108 |
int feedback; |
| 109 |
int output; |
| 110 |
|
| 111 |
|
| 112 |
unsigned cPhase; |
| 113 |
std::array<unsigned, 8> dPhase; |
| 114 |
|
| 115 |
|
| 116 |
unsigned volume; |
| 117 |
unsigned tll; |
| 118 |
std::span<const int, 16> dPhaseDRTableRks; |
| 119 |
EnvelopeState state; |
| 120 |
EnvPhaseIndex eg_phase; |
| 121 |
EnvPhaseIndex eg_dPhase; |
| 122 |
EnvPhaseIndex eg_phase_max; |
| 123 |
uint8_t slot_on_flag; |
| 124 |
bool sustain; |
| 125 |
|
| 126 |
Patch patch; |
| 127 |
Slot* sibling; |
| 128 |
}; |
| 129 |
|
| 130 |
class Channel { |
| 131 |
public: |
| 132 |
Channel(); |
| 133 |
void reset(YM2413& ym2413); |
| 134 |
void setPatch(unsigned num, YM2413& ym2413); |
| 135 |
void setSustain(bool sustain, bool modActAsCarrier); |
| 136 |
void keyOn(); |
| 137 |
void keyOff(); |
| 138 |
|
| 139 |
Slot mod, car; |
| 140 |
|
| 141 |
template<typename Archive> |
| 142 |
void serialize(Archive& ar, unsigned version); |
| 143 |
}; |
| 144 |
|
| 145 |
class YM2413 final : public YM2413Core |
| 146 |
{ |
| 147 |
public: |
| 148 |
YM2413(); |
| 149 |
|
| 150 |
|
| 151 |
void reset() override; |
| 152 |
void writePort(bool port, uint8_t value, int offset) override; |
| 153 |
void pokeReg(uint8_t reg, uint8_t data) override; |
| 154 |
[[nodiscard]] std::span<const uint8_t, 64>peekRegs() const override; |
| 155 |
void generateChannels(std::span<float*, 9 + 5> bufs, unsigned num) override; |
| 156 |
[[nodiscard]] float getAmplificationFactor() const override; |
| 157 |
|
| 158 |
[[nodiscard]] Patch& getPatch(unsigned instrument, bool carrier); |
| 159 |
|
| 160 |
template<typename Archive> |
| 161 |
void serialize(Archive& ar, unsigned version); |
| 162 |
|
| 163 |
private: |
| 164 |
void writeReg(uint8_t r, uint8_t data); |
| 165 |
|
| 166 |
void keyOn_BD(); |
| 167 |
void keyOn_SD(); |
| 168 |
void keyOn_TOM(); |
| 169 |
void keyOn_HH(); |
| 170 |
void keyOn_CYM(); |
| 171 |
void keyOff_BD(); |
| 172 |
void keyOff_SD(); |
| 173 |
void keyOff_TOM(); |
| 174 |
void keyOff_HH(); |
| 175 |
void keyOff_CYM(); |
| 176 |
void setRhythmFlags(uint8_t old); |
| 177 |
void update_key_status(); |
| 178 |
[[nodiscard]] bool isRhythm() const; |
| 179 |
[[nodiscard]] unsigned getFreq(unsigned channel) const; |
| 180 |
|
| 181 |
template<unsigned FLAGS> |
| 182 |
void calcChannel(Channel& ch, std::span<float> buf); |
| 183 |
|
| 184 |
private: |
| 185 |
|
| 186 |
std::array<Channel, 9> channels; |
| 187 |
|
| 188 |
|
| 189 |
unsigned pm_phase; |
| 190 |
|
| 191 |
|
| 192 |
unsigned am_phase; |
| 193 |
|
| 194 |
|
| 195 |
unsigned noise_seed; |
| 196 |
|
| 197 |
|
| 198 |
std::array<std::array<Patch, 2>, 19> patches; |
| 199 |
|
| 200 |
|
| 201 |
std::array<uint8_t, 0x40> reg; |
| 202 |
uint8_t registerLatch; |
| 203 |
}; |
| 204 |
|
| 205 |
} |
| 206 |
|
| 207 |
SERIALIZE_CLASS_VERSION(YM2413Okazaki::Slot, 4); |
| 208 |
SERIALIZE_CLASS_VERSION(YM2413Okazaki::Channel, 2); |
| 209 |
SERIALIZE_CLASS_VERSION(YM2413Okazaki::YM2413, 4); |
| 210 |
|
| 211 |
} |
| 212 |
|
| 213 |
#endif |