openMSX
YM2413Okazaki.hh
Go to the documentation of this file.
1 #ifndef YM2413OKAZAKI_HH
2 #define YM2413OKAZAKI_HH
3 
4 #include "YM2413Core.hh"
5 #include "FixedPoint.hh"
6 #include "serialize_meta.hh"
7 
8 namespace openmsx {
9 namespace YM2413Okazaki {
10 
11 class YM2413;
12 
13 constexpr int EP_FP_BITS = 15;
15 
18 };
19 
20 class Patch {
21 public:
25  Patch();
26 
27  void initModulator(const uint8_t* data);
28  void initCarrier (const uint8_t* data);
29 
31  inline void setKR(uint8_t value);
33  inline void setML(uint8_t value);
35  inline void setKL(uint8_t value);
37  inline void setTL(uint8_t value);
39  inline void setWF(uint8_t value);
41  inline void setFB(uint8_t value);
43  inline void setSL(uint8_t value);
44 
45  const unsigned* WF; // 0-1 transformed to waveform[0-1]
46  const uint8_t* KL; // 0-3 transformed to tllTable[0-3]
47  unsigned SL; // 0-15 transformed to slTable[0-15]
48  uint8_t AMPM; // 0-3 2 packed booleans
49  bool EG; // 0-1
50  uint8_t KR; // 0-1 transformed to 10,8
51  uint8_t ML; // 0-15 transformed to mlTable[0-15]
52  uint8_t TL; // 0-63 transformed to TL2EG(0..63) == [0..252]
53  uint8_t FB; // 0,1-7 transformed to 0,7-1
54  uint8_t AR; // 0-15
55  uint8_t DR; // 0-15
56  uint8_t RR; // 0-15
57 };
58 
59 class Slot {
60 public:
61  void reset();
62 
64  inline bool isActive() const;
65 
66  inline void slotOn();
67  inline void slotOn2();
68  inline void slotOff();
69  inline void setPatch(const Patch& patch);
70  inline void setVolume(unsigned value);
71 
72  inline unsigned calc_phase(unsigned lfo_pm);
73  template <bool HAS_AM, bool FIXED_ENV>
74  inline unsigned calc_envelope(int lfo_am, unsigned fixed_env);
75  template <bool HAS_AM> unsigned calc_fixed_env() const;
76  void calc_envelope_outline(unsigned& out);
77  template<bool HAS_AM, bool FIXED_ENV>
78  inline int calc_slot_car(unsigned lfo_pm, int lfo_am, int fm, unsigned fixed_env);
79  template<bool HAS_AM, bool HAS_FB, bool FIXED_ENV>
80  inline int calc_slot_mod(unsigned lfo_pm, int lfo_am, unsigned fixed_env);
81 
82  inline int calc_slot_tom();
83  inline int calc_slot_snare(bool noise);
84  inline int calc_slot_cym(unsigned phase7, unsigned phase8);
85  inline int calc_slot_hat(unsigned phase7, unsigned phase8, bool noise);
86  inline void updatePG(unsigned freq);
87  inline void updateTLL(unsigned freq, bool actAsCarrier);
88  inline void updateRKS(unsigned freq);
89  inline void updateEG();
90  inline void updateAll(unsigned freq, bool actAsCarrier);
91 
92  template<typename Archive>
93  void serialize(Archive& ar, unsigned version);
94 
95  // OUTPUT
96  int feedback;
97  int output; // Output value of slot
98 
99  // for Phase Generator (PG)
100  unsigned cphase; // Phase counter
101  unsigned dphase[8]; // Phase increment
102 
103  // for Envelope Generator (EG)
104  unsigned volume; // Current volume
105  unsigned tll; // Total Level + Key scale level
106  const int* dphaseDRTableRks; // (converted to EnvPhaseIndex)
107  EnvelopeState state; // Current state
109  EnvPhaseIndex eg_dphase; // Phase increment amount
111  uint8_t slot_on_flag;
112  bool sustain; // Sustain
113 
115  Slot* sibling; // pointer to sibling slot (only valid for car -> mod)
116 };
117 
118 class Channel {
119 public:
120  Channel();
121  void reset(YM2413& ym2413);
122  inline void setPatch(unsigned num, YM2413& ym2413);
123  inline void setSustain(bool sustain, bool modActAsCarrier);
124  inline void keyOn();
125  inline void keyOff();
126 
128 
129  template<typename Archive>
130  void serialize(Archive& ar, unsigned version);
131 };
132 
133 class YM2413 final : public YM2413Core
134 {
135 public:
136  YM2413();
137 
138  // YM2413Core
139  void reset() override;
140  void writePort(bool port, uint8_t value, int offset) override;
141  void pokeReg(uint8_t reg, uint8_t data) override;
142  uint8_t peekReg(uint8_t reg) const override;
143  void generateChannels(float* bufs[9 + 5], unsigned num) override;
144  float getAmplificationFactor() const override;
145 
146  Patch& getPatch(unsigned instrument, bool carrier);
147 
148  template<typename Archive>
149  void serialize(Archive& ar, unsigned version);
150 
151 private:
152  void writeReg(uint8_t r, uint8_t data);
153 
154  inline void keyOn_BD();
155  inline void keyOn_SD();
156  inline void keyOn_TOM();
157  inline void keyOn_HH();
158  inline void keyOn_CYM();
159  inline void keyOff_BD();
160  inline void keyOff_SD();
161  inline void keyOff_TOM();
162  inline void keyOff_HH();
163  inline void keyOff_CYM();
164  inline void setRhythmFlags(uint8_t old);
165  inline void update_key_status();
166  inline bool isRhythm() const;
167  inline unsigned getFreq(unsigned channel) const;
168 
169  template <unsigned FLAGS>
170  inline void calcChannel(Channel& ch, float* buf, unsigned num);
171 
172 private:
174  Channel channels[9];
175 
177  unsigned pm_phase;
178 
180  unsigned am_phase;
181 
183  unsigned noise_seed;
184 
186  Patch patches[19][2];
187 
189  uint8_t reg[0x40];
190  uint8_t registerLatch;
191 };
192 
193 } // namespace YM2413Okazaki
194 
198 
199 } // namespace openmsx
200 
201 #endif
openmsx::YM2413Okazaki::Patch::ML
uint8_t ML
Definition: YM2413Okazaki.hh:51
openmsx::YM2413Okazaki::DECAY
Definition: YM2413Okazaki.hh:17
openmsx::YM2413Okazaki::Slot::updateAll
void updateAll(unsigned freq, bool actAsCarrier)
Definition: YM2413Okazaki.cc:519
openmsx::YM2413Okazaki::Slot::dphase
unsigned dphase[8]
Definition: YM2413Okazaki.hh:101
openmsx::YM2413Okazaki::Slot::output
int output
Definition: YM2413Okazaki.hh:97
openmsx::YM2413Okazaki::Slot::slotOff
void slotOff()
Definition: YM2413Okazaki.cc:574
openmsx::YM2413Okazaki::SUSTAIN
Definition: YM2413Okazaki.hh:17
openmsx::YM2413Okazaki::Patch::setKL
void setKL(uint8_t value)
Sets Key scale level [0..3].
Definition: YM2413Okazaki.cc:398
openmsx::YM2413Okazaki::YM2413
Definition: YM2413Okazaki.hh:133
openmsx::YM2413Okazaki::Channel::serialize
void serialize(Archive &ar, unsigned version)
Definition: YM2413Okazaki.cc:1672
openmsx::YM2413Okazaki::YM2413::serialize
void serialize(Archive &ar, unsigned version)
Definition: YM2413Okazaki.cc:1684
openmsx::YM2413Okazaki::Slot::reset
void reset()
Definition: YM2413Okazaki.cc:425
openmsx::YM2413Okazaki::Patch::initCarrier
void initCarrier(const uint8_t *data)
Definition: YM2413Okazaki.cc:374
openmsx::YM2413Okazaki::Slot::feedback
int feedback
Definition: YM2413Okazaki.hh:96
openmsx::YM2413Okazaki::Channel::car
Slot car
Definition: YM2413Okazaki.hh:127
openmsx::YM2413Okazaki::SETTLE
Definition: YM2413Okazaki.hh:17
openmsx::YM2413Okazaki::Channel::reset
void reset(YM2413 &ym2413)
Definition: YM2413Okazaki.cc:612
openmsx::YM2413Okazaki::Slot::setPatch
void setPatch(const Patch &patch)
Definition: YM2413Okazaki.cc:585
openmsx::YM2413Okazaki::Slot::volume
unsigned volume
Definition: YM2413Okazaki.hh:104
openmsx::YM2413Okazaki::Patch::RR
uint8_t RR
Definition: YM2413Okazaki.hh:56
serialize_meta.hh
openmsx::YM2413Okazaki::SUSHOLD
Definition: YM2413Okazaki.hh:17
openmsx::YM2413Okazaki::ATTACK
Definition: YM2413Okazaki.hh:17
openmsx::YM2413Okazaki::Slot::calc_fixed_env
unsigned calc_fixed_env() const
Definition: YM2413Okazaki.cc:994
openmsx::YM2413Okazaki::Patch::AR
uint8_t AR
Definition: YM2413Okazaki.hh:54
openmsx::YM2413Okazaki::Slot::calc_envelope_outline
void calc_envelope_outline(unsigned &out)
Definition: YM2413Okazaki.cc:934
openmsx::YM2413Okazaki::Slot::eg_phase
EnvPhaseIndex eg_phase
Definition: YM2413Okazaki.hh:108
openmsx::YM2413Okazaki::Slot::slotOn2
void slotOn2()
Definition: YM2413Okazaki.cc:567
openmsx::YM2413Okazaki::YM2413::YM2413
YM2413()
Definition: YM2413Okazaki.cc:689
openmsx::YM2413Okazaki::YM2413::getAmplificationFactor
float getAmplificationFactor() const override
Returns normalization factor.
Definition: YM2413Okazaki.cc:1079
openmsx::YM2413Okazaki::Patch::setML
void setML(uint8_t value)
Sets the frequency multiplier factor [0..15].
Definition: YM2413Okazaki.cc:394
openmsx::YM2413Okazaki::Channel
Definition: YM2413Okazaki.hh:118
openmsx::YM2413Okazaki::Channel::Channel
Channel()
Definition: YM2413Okazaki.cc:606
openmsx::YM2413Okazaki::Slot::serialize
void serialize(Archive &ar, unsigned version)
Definition: YM2413Okazaki.cc:1652
openmsx::YM2413Okazaki::YM2413::getPatch
Patch & getPatch(unsigned instrument, bool carrier)
Definition: YM2413Okazaki.cc:1096
openmsx::YM2413Okazaki::Patch::setWF
void setWF(uint8_t value)
Set waveform [0..1].
Definition: YM2413Okazaki.cc:408
openmsx::YM2413Okazaki::Patch::DR
uint8_t DR
Definition: YM2413Okazaki.hh:55
openmsx::YM2413Okazaki::Slot::calc_slot_cym
int calc_slot_cym(unsigned phase7, unsigned phase8)
Definition: YM2413Okazaki.cc:1052
openmsx::YM2413Okazaki::Patch::KR
uint8_t KR
Definition: YM2413Okazaki.hh:50
openmsx::SERIALIZE_CLASS_VERSION
SERIALIZE_CLASS_VERSION(CassettePlayer, 2)
openmsx::YM2413Okazaki::Slot::calc_slot_tom
int calc_slot_tom()
Definition: YM2413Okazaki.cc:1034
openmsx::YM2413Okazaki::Patch::FB
uint8_t FB
Definition: YM2413Okazaki.hh:53
openmsx::YM2413Okazaki::Slot::sustain
bool sustain
Definition: YM2413Okazaki.hh:112
openmsx::YM2413Okazaki::Channel::mod
Slot mod
Definition: YM2413Okazaki.hh:127
openmsx::YM2413Okazaki::Channel::keyOn
void keyOn()
Definition: YM2413Okazaki.cc:636
openmsx::YM2413Okazaki::Slot::updatePG
void updatePG(unsigned freq)
Definition: YM2413Okazaki.cc:439
openmsx::YM2413Okazaki::YM2413::generateChannels
void generateChannels(float *bufs[9+5], unsigned num) override
Definition: YM2413Okazaki.cc:1156
openmsx::YM2413Okazaki::Patch::Patch
Patch()
Creates an uninitialized Patch object; call initXXX() before use.
Definition: YM2413Okazaki.cc:346
openmsx::YM2413Okazaki::Slot::slot_on_flag
uint8_t slot_on_flag
Definition: YM2413Okazaki.hh:111
openmsx::YM2413Okazaki::Patch::SL
unsigned SL
Definition: YM2413Okazaki.hh:47
openmsx::YM2413Okazaki::Slot::calc_slot_hat
int calc_slot_hat(unsigned phase7, unsigned phase8, bool noise)
Definition: YM2413Okazaki.cc:1066
openmsx::YM2413Okazaki::YM2413::peekReg
uint8_t peekReg(uint8_t reg) const override
Read from a YM2413 register (for debug).
Definition: YM2413Okazaki.cc:1626
openmsx::YM2413Okazaki::Slot::slotOn
void slotOn()
Definition: YM2413Okazaki.cc:559
openmsx::YM2413Okazaki::Slot::eg_dphase
EnvPhaseIndex eg_dphase
Definition: YM2413Okazaki.hh:109
openmsx::YM2413Okazaki::EnvelopeState
EnvelopeState
Definition: YM2413Okazaki.hh:16
openmsx::YM2413Okazaki::Slot::calc_slot_mod
int calc_slot_mod(unsigned lfo_pm, int lfo_am, unsigned fixed_env)
openmsx::YM2413Okazaki::Slot::eg_phase_max
EnvPhaseIndex eg_phase_max
Definition: YM2413Okazaki.hh:110
openmsx::YM2413Okazaki::YM2413::writePort
void writePort(bool port, uint8_t value, int offset) override
Write to the YM2413 register/data port.
Definition: YM2413Okazaki.cc:1399
openmsx::YM2413Okazaki::Patch::TL
uint8_t TL
Definition: YM2413Okazaki.hh:52
openmsx::YM2413Okazaki::Patch::AMPM
uint8_t AMPM
Definition: YM2413Okazaki.hh:48
openmsx::YM2413Okazaki::Slot::dphaseDRTableRks
const int * dphaseDRTableRks
Definition: YM2413Okazaki.hh:106
openmsx::YM2413Okazaki::Slot::isActive
bool isActive() const
Definition: YM2413Okazaki.cc:553
openmsx::YM2413Okazaki::Slot::sibling
Slot * sibling
Definition: YM2413Okazaki.hh:115
FixedPoint.hh
openmsx::YM2413Okazaki::RELEASE
Definition: YM2413Okazaki.hh:17
openmsx::YM2413Okazaki::Channel::setSustain
void setSustain(bool sustain, bool modActAsCarrier)
Definition: YM2413Okazaki.cc:627
openmsx::YM2413Okazaki::Slot::setVolume
void setVolume(unsigned value)
Definition: YM2413Okazaki.cc:595
openmsx::YM2413Okazaki::Slot::calc_slot_car
int calc_slot_car(unsigned lfo_pm, int lfo_am, int fm, unsigned fixed_env)
openmsx::YM2413Core
Abstract interface for the YM2413 core.
Definition: YM2413Core.hh:26
openmsx::YM2413Okazaki::Slot::calc_slot_snare
int calc_slot_snare(bool noise)
Definition: YM2413Okazaki.cc:1042
openmsx::YM2413Okazaki::Slot::cphase
unsigned cphase
Definition: YM2413Okazaki.hh:100
openmsx::YM2413Okazaki::Patch::setKR
void setKR(uint8_t value)
Sets the Key Scale of Rate (0 or 1).
Definition: YM2413Okazaki.cc:390
openmsx::YM2413Okazaki::FINISH
Definition: YM2413Okazaki.hh:17
YM2413Core.hh
openmsx::YM2413Okazaki::Slot::calc_envelope
unsigned calc_envelope(int lfo_am, unsigned fixed_env)
openmsx::YM2413Okazaki::Slot::state
EnvelopeState state
Definition: YM2413Okazaki.hh:107
openmsx::YM2413
YM2413
Definition: YM2413.cc:119
openmsx::YM2413Okazaki::Channel::setPatch
void setPatch(unsigned num, YM2413 &ym2413)
Definition: YM2413Okazaki.cc:620
openmsx::YM2413Okazaki::YM2413::pokeReg
void pokeReg(uint8_t reg, uint8_t data) override
Write to a YM2413 register (for debug).
Definition: YM2413Okazaki.cc:1408
openmsx::YM2413Okazaki::Patch::setFB
void setFB(uint8_t value)
Sets the amount of feedback [0..7].
Definition: YM2413Okazaki.cc:412
openmsx::YM2413Okazaki::Slot::updateRKS
void updateRKS(unsigned freq)
Definition: YM2413Okazaki.cc:460
openmsx::YM2413Okazaki::Channel::keyOff
void keyOff()
Definition: YM2413Okazaki.cc:650
openmsx::YM2413Okazaki::Patch::KL
const uint8_t * KL
Definition: YM2413Okazaki.hh:46
openmsx::YM2413Okazaki::Patch
Definition: YM2413Okazaki.hh:20
openmsx::YM2413Okazaki::YM2413::reset
void reset() override
Reset this YM2413 core.
Definition: YM2413Okazaki.cc:734
openmsx::YM2413Okazaki::Slot::calc_phase
unsigned calc_phase(unsigned lfo_pm)
Definition: YM2413Okazaki.cc:927
openmsx::YM2413Okazaki::EP_FP_BITS
constexpr int EP_FP_BITS
Definition: YM2413Okazaki.hh:13
openmsx::YM2413Okazaki::Slot
Definition: YM2413Okazaki.hh:59
openmsx::YM2413Okazaki::Slot::updateTLL
void updateTLL(unsigned freq, bool actAsCarrier)
Definition: YM2413Okazaki.cc:455
openmsx::YM2413Okazaki::Slot::tll
unsigned tll
Definition: YM2413Okazaki.hh:105
openmsx::YM2413Okazaki::Slot::patch
Patch patch
Definition: YM2413Okazaki.hh:114
openmsx::YM2413Okazaki::Patch::setTL
void setTL(uint8_t value)
Set volume (total level) [0..63].
Definition: YM2413Okazaki.cc:402
openmsx
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
openmsx::YM2413Okazaki::Patch::setSL
void setSL(uint8_t value)
Sets sustain level [0..15].
Definition: YM2413Okazaki.cc:416
openmsx::YM2413Okazaki::Patch::WF
const unsigned * WF
Definition: YM2413Okazaki.hh:45
openmsx::YM2413Okazaki::Slot::setEnvelopeState
void setEnvelopeState(EnvelopeState state)
Definition: YM2413Okazaki.cc:527
openmsx::YM2413Okazaki::Patch::EG
bool EG
Definition: YM2413Okazaki.hh:49
openmsx::YM2413Okazaki::Patch::initModulator
void initModulator(const uint8_t *data)
Definition: YM2413Okazaki.cc:358
openmsx::YM2413Okazaki::Slot::updateEG
void updateEG()
Definition: YM2413Okazaki.cc:467
openmsx::FixedPoint< EP_FP_BITS >