| e8555c89 Andy McCall |
1 |
#ifndef YMF262_HH |
| 2 |
#define YMF262_HH |
| 3 |
|
| 4 |
#include "ResampledSoundDevice.hh" |
| 5 |
|
| 6 |
#include "EmuTimer.hh" |
| 7 |
|
| 8 |
#include "EmuTime.hh" |
| 9 |
#include "FixedPoint.hh" |
| 10 |
#include "IRQHelper.hh" |
| 11 |
#include "SimpleDebuggable.hh" |
| 12 |
#include "serialize_meta.hh" |
| 13 |
|
| 14 |
#include <array> |
| 15 |
#include <cstdint> |
| 16 |
#include <memory> |
| 17 |
#include <span> |
| 18 |
#include <string> |
| 19 |
|
| 20 |
namespace openmsx { |
| 21 |
|
| 22 |
class DeviceConfig; |
| 23 |
|
| 24 |
class YMF262 final : private ResampledSoundDevice, private EmuTimerCallback |
| 25 |
{ |
| 26 |
public: |
| 27 |
|
| 28 |
static constexpr int SIN_BITS = 10; |
| 29 |
static constexpr int SIN_LEN = 1 << SIN_BITS; |
| 30 |
static constexpr int SIN_MASK = SIN_LEN - 1; |
| 31 |
|
| 32 |
public: |
| 33 |
YMF262(const std::string& name, const DeviceConfig& config, |
| 34 |
bool isYMF278); |
| 35 |
~YMF262(); |
| 36 |
|
| 37 |
void reset(EmuTime time); |
| 38 |
void writeReg (unsigned r, uint8_t v, EmuTime time); |
| 39 |
void writeReg512(unsigned r, uint8_t v, EmuTime time); |
| 40 |
[[nodiscard]] uint8_t readReg(unsigned reg) const; |
| 41 |
[[nodiscard]] uint8_t peekReg(unsigned reg) const; |
| 42 |
[[nodiscard]] uint8_t readStatus(); |
| 43 |
[[nodiscard]] uint8_t peekStatus() const; |
| 44 |
|
| 45 |
void setMixLevel(uint8_t x, EmuTime time); |
| 46 |
|
| 47 |
template<typename Archive> |
| 48 |
void serialize(Archive& ar, unsigned version); |
| 49 |
|
| 50 |
public: |
| 51 |
|
| 52 |
using FreqIndex = FixedPoint<16>; |
| 53 |
|
| 54 |
enum class EnvelopeState : uint8_t { |
| 55 |
ATTACK, DECAY, SUSTAIN, RELEASE, OFF |
| 56 |
}; |
| 57 |
|
| 58 |
private: |
| 59 |
class Channel; |
| 60 |
|
| 61 |
class Slot { |
| 62 |
public: |
| 63 |
Slot(); |
| 64 |
[[nodiscard]] int op_calc(unsigned phase, unsigned lfo_am) const; |
| 65 |
void FM_KEYON(uint8_t key_set); |
| 66 |
void FM_KEYOFF(uint8_t key_clr); |
| 67 |
void advanceEnvelopeGenerator(unsigned egCnt); |
| 68 |
void advancePhaseGenerator(const Channel& ch, unsigned lfo_pm); |
| 69 |
void update_ar_dr(); |
| 70 |
void update_rr(); |
| 71 |
void calc_fc(const Channel& ch); |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
void setFeedbackShift(uint8_t value) { |
| 76 |
fb_shift = value ? 9 - value : 0; |
| 77 |
} |
| 78 |
|
| 79 |
template<typename Archive> |
| 80 |
void serialize(Archive& ar, unsigned version); |
| 81 |
|
| 82 |
|
| 83 |
FreqIndex Cnt{0}; |
| 84 |
FreqIndex Incr{0}; |
| 85 |
int* connect{nullptr}; |
| 86 |
std::array<int, 2> op1_out{0, 0}; |
| 87 |
|
| 88 |
|
| 89 |
unsigned TL{0}; |
| 90 |
unsigned TLL{0}; |
| 91 |
int volume{0}; |
| 92 |
int sl{0}; |
| 93 |
|
| 94 |
std::span<const unsigned, SIN_LEN> waveTable; |
| 95 |
|
| 96 |
EnvelopeState state{EnvelopeState::OFF}; |
| 97 |
unsigned eg_m_ar{0}; |
| 98 |
unsigned eg_m_dr{0}; |
| 99 |
unsigned eg_m_rr{0}; |
| 100 |
uint8_t eg_sh_ar{0}; |
| 101 |
uint8_t eg_sel_ar{0}; |
| 102 |
uint8_t eg_sh_dr{0}; |
| 103 |
uint8_t eg_sel_dr{0}; |
| 104 |
uint8_t eg_sh_rr{0}; |
| 105 |
uint8_t eg_sel_rr{0}; |
| 106 |
|
| 107 |
uint8_t key{0}; |
| 108 |
|
| 109 |
uint8_t fb_shift{0}; |
| 110 |
bool CON{false}; |
| 111 |
bool eg_type{false}; |
| 112 |
|
| 113 |
|
| 114 |
uint8_t AMmask{0}; |
| 115 |
bool vib{false}; |
| 116 |
|
| 117 |
uint8_t ar{0}; |
| 118 |
uint8_t dr{0}; |
| 119 |
uint8_t rr{0}; |
| 120 |
uint8_t KSR{0}; |
| 121 |
uint8_t ksl{0}; |
| 122 |
uint8_t ksr{0}; |
| 123 |
uint8_t mul{0}; |
| 124 |
}; |
| 125 |
|
| 126 |
class Channel { |
| 127 |
public: |
| 128 |
void chan_calc(unsigned lfo_am); |
| 129 |
void chan_calc_ext(unsigned lfo_am); |
| 130 |
|
| 131 |
template<typename Archive> |
| 132 |
void serialize(Archive& ar, unsigned version); |
| 133 |
|
| 134 |
std::array<Slot, 2> slot; |
| 135 |
|
| 136 |
int block_fnum{0}; |
| 137 |
FreqIndex fc{0}; |
| 138 |
unsigned ksl_base{0}; |
| 139 |
uint8_t kcode{0}; |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
bool extended{false}; |
| 150 |
|
| 151 |
|
| 152 |
}; |
| 153 |
|
| 154 |
|
| 155 |
[[nodiscard]] float getAmplificationFactorImpl() const override; |
| 156 |
void generateChannels(std::span<float*> bufs, unsigned num) override; |
| 157 |
|
| 158 |
void callback(uint8_t flag) override; |
| 159 |
|
| 160 |
void writeRegDirect(unsigned r, uint8_t v, EmuTime time); |
| 161 |
void init_tables(); |
| 162 |
void setStatus(uint8_t flag); |
| 163 |
void resetStatus(uint8_t flag); |
| 164 |
void changeStatusMask(uint8_t flag); |
| 165 |
void advance(); |
| 166 |
|
| 167 |
[[nodiscard]] unsigned genPhaseHighHat(); |
| 168 |
[[nodiscard]] unsigned genPhaseSnare(); |
| 169 |
[[nodiscard]] unsigned genPhaseCymbal(); |
| 170 |
|
| 171 |
void chan_calc_rhythm(unsigned lfo_am); |
| 172 |
void set_mul(unsigned sl, uint8_t v); |
| 173 |
void set_ksl_tl(unsigned sl, uint8_t v); |
| 174 |
void set_ar_dr(unsigned sl, uint8_t v); |
| 175 |
void set_sl_rr(unsigned sl, uint8_t v); |
| 176 |
[[nodiscard]] bool checkMuteHelper() const; |
| 177 |
|
| 178 |
[[nodiscard]] bool isExtended(unsigned ch) const; |
| 179 |
[[nodiscard]] Channel& getFirstOfPair(unsigned ch); |
| 180 |
[[nodiscard]] Channel& getSecondOfPair(unsigned ch); |
| 181 |
|
| 182 |
struct Debuggable final : SimpleDebuggable { |
| 183 |
Debuggable(MSXMotherBoard& motherBoard, const std::string& name); |
| 184 |
[[nodiscard]] uint8_t read(unsigned address) override; |
| 185 |
void write(unsigned address, uint8_t value, EmuTime time) override; |
| 186 |
} debuggable; |
| 187 |
|
| 188 |
|
| 189 |
static constexpr int R04_ST1 = 0x01; |
| 190 |
static constexpr int R04_ST2 = 0x02; |
| 191 |
static constexpr int R04_MASK_T2 = 0x20; |
| 192 |
static constexpr int R04_MASK_T1 = 0x40; |
| 193 |
static constexpr int R04_IRQ_RESET = 0x80; |
| 194 |
|
| 195 |
|
| 196 |
static constexpr int STATUS_T2 = R04_MASK_T2; |
| 197 |
static constexpr int STATUS_T1 = R04_MASK_T1; |
| 198 |
|
| 199 |
const std::unique_ptr<EmuTimer> timer1; |
| 200 |
const std::unique_ptr<EmuTimer> timer2; |
| 201 |
|
| 202 |
IRQHelper irq; |
| 203 |
|
| 204 |
std::array<int, 18> chanOut = {}; |
| 205 |
|
| 206 |
std::array<uint8_t, 512> reg = {}; |
| 207 |
std::array<Channel, 18> channel; |
| 208 |
|
| 209 |
std::array<int, 18 * 4> pan; |
| 210 |
|
| 211 |
unsigned eg_cnt{0}; |
| 212 |
unsigned noise_rng{1}; |
| 213 |
|
| 214 |
|
| 215 |
using LFOAMIndex = FixedPoint< 6>; |
| 216 |
using LFOPMIndex = FixedPoint<10>; |
| 217 |
LFOAMIndex lfo_am_cnt{0}; |
| 218 |
LFOPMIndex lfo_pm_cnt{0}; |
| 219 |
bool lfo_am_depth{false}; |
| 220 |
uint8_t lfo_pm_depth_range{0}; |
| 221 |
|
| 222 |
uint8_t rhythm{0}; |
| 223 |
bool nts{false}; |
| 224 |
bool OPL3_mode{false}; |
| 225 |
|
| 226 |
uint8_t status{0}; |
| 227 |
uint8_t status2{0}; |
| 228 |
uint8_t statusMask{0}; |
| 229 |
|
| 230 |
bool alreadySignaledNEW2{false}; |
| 231 |
const bool isYMF278; |
| 232 |
|
| 233 |
}; |
| 234 |
SERIALIZE_CLASS_VERSION(YMF262, 2); |
| 235 |
|
| 236 |
} |
| 237 |
|
| 238 |
#endif |