openMSX
YMF278.hh
Go to the documentation of this file.
1#ifndef YMF278_HH
2#define YMF278_HH
3
5
6#include "EmuTime.hh"
7#include "Rom.hh"
8#include "SimpleDebuggable.hh"
9#include "TrackedRam.hh"
10#include "serialize_meta.hh"
11
12#include <array>
13#include <cstdint>
14#include <string>
15
16namespace openmsx {
17
18class DeviceConfig;
19
20class YMF278 final : public ResampledSoundDevice
21{
22public:
23 YMF278(const std::string& name, int ramSizeInKb,
24 const DeviceConfig& config);
25 ~YMF278();
26 void clearRam();
27 void reset(EmuTime::param time);
28 void writeReg(uint8_t reg, uint8_t data, EmuTime::param time);
29 [[nodiscard]] uint8_t readReg(uint8_t reg);
30 [[nodiscard]] uint8_t peekReg(uint8_t reg) const;
31
32 [[nodiscard]] uint8_t readMem(unsigned address) const;
33 void writeMem(unsigned address, uint8_t value);
34
35 void setMixLevel(uint8_t x, EmuTime::param time);
36
37 template<typename Archive>
38 void serialize(Archive& ar, unsigned version);
39
40private:
41 class Slot {
42 public:
43 Slot();
44 void reset();
45 [[nodiscard]] uint8_t compute_rate(int val) const;
46 [[nodiscard]] uint8_t compute_decay_rate(int val) const;
47 [[nodiscard]] unsigned decay_rate(int num, int sample_rate);
48 void envelope_next(int sample_rate);
49 [[nodiscard]] int16_t compute_vib() const;
50 [[nodiscard]] 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; // wave table 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; // pan-pot 0..15
75 bool keyon; // slot keyed on
76 bool DAMP;
77 uint8_t lfo; // LFO speed 0..7
78 uint8_t vib; // vibrato 0..7
79 uint8_t AM; // AM level 0..7
80 uint8_t AR; // 0..15
81 uint8_t D1R; // 0..15
82 uint8_t D2R; // 0..15
83 uint8_t RC; // rate correction 0..15
84 uint8_t RR; // 0..15
85
86 uint8_t bits; // width of the samples
87
88 uint8_t state; // envelope generator state
89 bool lfo_active;
90 };
91
92 // SoundDevice
93 void generateChannels(std::span<float*> bufs, unsigned num) override;
94
95 void writeRegDirect(uint8_t reg, uint8_t data, EmuTime::param time);
96 [[nodiscard]] unsigned getRamAddress(unsigned addr) const;
97 [[nodiscard]] int16_t getSample(const Slot& slot, uint16_t pos) const;
98 [[nodiscard]] static uint16_t nextPos(const Slot& slot, uint16_t pos, uint16_t increment);
99 void advance();
100 [[nodiscard]] bool anyActive();
101 void keyOnHelper(Slot& slot) const;
102
103 MSXMotherBoard& motherBoard;
104
105 struct DebugRegisters final : SimpleDebuggable {
106 DebugRegisters(MSXMotherBoard& motherBoard, const std::string& name);
107 [[nodiscard]] uint8_t read(unsigned address) override;
108 void write(unsigned address, uint8_t value, EmuTime::param time) override;
109 } debugRegisters;
110
111 struct DebugMemory final : SimpleDebuggable {
112 DebugMemory(MSXMotherBoard& motherBoard, const std::string& name);
113 [[nodiscard]] uint8_t read(unsigned address) override;
114 void write(unsigned address, uint8_t value) override;
115 } debugMemory;
116
117 std::array<Slot, 24> slots;
118
120 unsigned eg_cnt;
121
122 int memAdr;
123
124 Rom rom;
125 TrackedRam ram;
126
127 std::array<uint8_t, 256> regs;
128};
129SERIALIZE_CLASS_VERSION(YMF278::Slot, 6);
131
132} // namespace openmsx
133
134#endif
uint8_t readReg(uint8_t reg)
Definition YMF278.cc:757
void writeReg(uint8_t reg, uint8_t data, EmuTime::param time)
Definition YMF278.cc:582
void setMixLevel(uint8_t x, EmuTime::param time)
Definition YMF278.cc:492
void reset(EmuTime::param time)
Definition YMF278.cc:844
void writeMem(unsigned address, uint8_t value)
Definition YMF278.cc:971
uint8_t readMem(unsigned address) const
Definition YMF278.cc:953
void clearRam()
Definition YMF278.cc:839
void serialize(Archive &ar, unsigned version)
Definition YMF278.cc:1089
uint8_t peekReg(uint8_t reg) const
Definition YMF278.cc:772
This file implemented 3 utility functions:
Definition Autofire.cc:11
void serialize(Archive &ar, T &t, unsigned version)
#define SERIALIZE_CLASS_VERSION(CLASS, VERSION)