Debian Codegraph @

src/engine/platform/sound/ymf278b/ymf278.h

raw
1 #ifndef YMF278_HH
2 #define YMF278_HH
3
4 #include <stdint.h>
5 #include <vector>
6 #include <string>
7 #include <algorithm>
8
9 #define UNREACHABLE while (1) assert(false)
10
11 using byte = uint8_t;
12
13 template<typename T>
14 const T& YMF_clamp(const T& value, const T& min, const T& max) {
15 return std::min(std::max(value, min), max);
16 }
17
18 class MemoryInterface {
19 public:
20 virtual byte operator[](unsigned address) const = 0;
21 virtual unsigned getSize() const = 0;
22 virtual void write(unsigned address, byte value) = 0;
23 virtual void clear(byte value) = 0;
24 virtual void setMemoryType(bool memoryType) {};
25 };
26
27 class YMF278Base
28 {
29 public:
30 YMF278Base(MemoryInterface& memory, int channelCount, int clockDivider, double clockFrequency);
31 ~YMF278Base();
32 int getChannelCount();
33 int getClockDivider();
34 double getClockFrequency();
35 void setClockFrequency(double clockFrequency);
36 double getSampleRate();
37 virtual void reset();
38
39 void generate(short& fleft, short& fright, short& rleft, short& rright, short* channelBufs = nullptr);
40
41 class Slot final {
42 public:
43 Slot();
44 void reset();
45 int compute_rate(int val) const;
46 int compute_decay_rate(int val) const;
47 unsigned decay_rate(int num, int sample_rate);
48 void envelope_next(int sample_rate);
49 int16_t compute_vib() const;
50 uint16_t compute_am() const;
51
52 template<typename Archive>
53 void serialize(Archive& ar, unsigned version);
54
55 uint32_t startaddr;
56 uint16_t loopaddr;
57 uint16_t endaddr; // Note: stored in 2s complement (0x0000 = 0, 0x0001 = -65536, 0xffff = -1)
58 uint32_t step; // fixed-point frequency step
59 // invariant: step == calcStep(OCT, FN)
60 uint32_t stepptr; // fixed-point pointer into the sample
61 uint16_t pos;
62
63 int16_t env_vol;
64
65 uint32_t lfo_cnt;
66
67 int16_t DL;
68 uint16_t wave; // wavetable number
69 uint16_t FN; // f-number TODO store 'FN | 1024'?
70 int8_t OCT; // octave [-8..+7]
71 bool PRVB; // pseudo-reverb
72 uint8_t TLdest; // destination total level
73 uint8_t TL; // total level (goes towards TLdest)
74 uint8_t pan; // panpot 0..15
75 bool ch; // channel select
76 bool keyon; // slot keyed on
77 bool DAMP;
78 uint8_t lfo; // LFO speed 0..7
79 uint8_t vib; // vibrato 0..7
80 uint8_t AM; // AM level 0..7
81 uint8_t AR; // 0..15
82 uint8_t D1R; // 0..15
83 uint8_t D2R; // 0..15
84 uint8_t RC; // rate correction 0..15
85 uint8_t RR; // 0..15
86
87 uint8_t bits; // width of the samples
88
89 uint8_t state; // envelope generator state
90 bool lfo_active;
91 };
92
93 protected:
94 void keyOnHelper(Slot& slot);
95
96 MemoryInterface& memory;
97 std::vector<Slot> slots;
98
99 private:
100 int16_t getSample(Slot& slot, uint16_t pos) const;
101 static uint16_t nextPos(Slot& slot, uint16_t pos, uint16_t increment);
102 void advance();
103 bool anyActive();
104
105 /** Global envelope generator counter. */
106 unsigned eg_cnt;
107
108 unsigned channelCount, clockDivider;
109 double clockFrequency;
110 };
111
112 class YMF278 final : public YMF278Base {
113 public:
114 YMF278(MemoryInterface& memory);
115 void reset() override;
116 void writeReg(byte reg, byte data);
117 byte readReg(byte reg);
118 byte peekReg(byte reg) const;
119
120 void generateMix(short fmL, short fmR, short& bufFL, short& bufFR, short& bufRL, short& bufRR, short* channelBufs = nullptr) {
121 generate(bufFL, bufFR, bufRL, bufRR, channelBufs);
122 bufFL = std::min(std::max((pcmMixL * bufFL + fmMixL * fmL) >> 4, -0x8000), 0x7fff);
123 bufFR = std::min(std::max((pcmMixR * bufFR + fmMixR * fmR) >> 4, -0x8000), 0x7fff);;
124 }
125
126 private:
127 int fmMixL, fmMixR, pcmMixL, pcmMixR;
128 int memAdr;
129 byte regs[256];
130 };
131
132 class YMW258 final : public YMF278Base {
133 public:
134 YMW258(MemoryInterface& memory);
135 void writeReg(byte channel, byte reg, byte data);
136 };
137
138 class MemoryMoonSound : MemoryInterface {
139 public:
140 MemoryMoonSound(MemoryInterface& rom, MemoryInterface& ram);
141 byte operator[](unsigned address) const override;
142 unsigned getSize() const override;
143 void write(unsigned address, byte value) override;
144 void clear(byte value) override;
145 void setMemoryType(bool memoryType) override;
146
147 private:
148 unsigned getRamAddress(unsigned addr) const;
149
150 MemoryInterface& rom;
151 MemoryInterface& ram;
152
153 bool memoryType;
154 };
155
156 #endif