openMSX
YMF278.hh
Go to the documentation of this file.
1#ifndef YMF278_HH
2#define YMF278_HH
3
5#include "SimpleDebuggable.hh"
6#include "Rom.hh"
7#include "TrackedRam.hh"
8#include "EmuTime.hh"
9#include "serialize_meta.hh"
10#include <array>
11#include <cstdint>
12#include <string>
13
14namespace openmsx {
15
16class DeviceConfig;
17
18class YMF278 final : public ResampledSoundDevice
19{
20public:
21 YMF278(const std::string& name, int ramSizeInKb,
22 const DeviceConfig& config);
23 ~YMF278();
24 void clearRam();
25 void reset(EmuTime::param time);
26 void writeReg(uint8_t reg, uint8_t data, EmuTime::param time);
27 [[nodiscard]] uint8_t readReg(uint8_t reg);
28 [[nodiscard]] uint8_t peekReg(uint8_t reg) const;
29
30 [[nodiscard]] uint8_t readMem(unsigned address) const;
31 void writeMem(unsigned address, uint8_t value);
32
33 void setMixLevel(uint8_t x, EmuTime::param time);
34
35 template<typename Archive>
36 void serialize(Archive& ar, unsigned version);
37
38private:
39 class Slot {
40 public:
41 Slot();
42 void reset();
43 [[nodiscard]] uint8_t compute_rate(int val) const;
44 [[nodiscard]] uint8_t compute_decay_rate(int val) const;
45 [[nodiscard]] unsigned decay_rate(int num, int sample_rate);
46 void envelope_next(int sample_rate);
47 [[nodiscard]] int16_t compute_vib() const;
48 [[nodiscard]] uint16_t compute_am() const;
49
50 template<typename Archive>
51 void serialize(Archive& ar, unsigned version);
52
53 uint32_t startAddr;
54 uint16_t loopAddr;
55 uint16_t endAddr; // Note: stored in 2s complement (0x0000 = 0, 0x0001 = -65536, 0xffff = -1)
56 uint32_t step; // fixed-point frequency step
57 // invariant: step == calcStep(OCT, FN)
58 uint32_t stepPtr; // fixed-point pointer into the sample
59 uint16_t pos;
60
61 int16_t env_vol;
62
63 uint32_t lfo_cnt;
64
65 int16_t DL;
66 uint16_t wave; // wave table number
67 uint16_t FN; // f-number TODO store 'FN | 1024'?
68 int8_t OCT; // octave [-8..+7]
69 bool PRVB; // pseudo-reverb
70 uint8_t TLdest; // destination total level
71 uint8_t TL; // total level (goes towards TLdest)
72 uint8_t pan; // pan-pot 0..15
73 bool keyon; // slot keyed on
74 bool DAMP;
75 uint8_t lfo; // LFO speed 0..7
76 uint8_t vib; // vibrato 0..7
77 uint8_t AM; // AM level 0..7
78 uint8_t AR; // 0..15
79 uint8_t D1R; // 0..15
80 uint8_t D2R; // 0..15
81 uint8_t RC; // rate correction 0..15
82 uint8_t RR; // 0..15
83
84 uint8_t bits; // width of the samples
85
86 uint8_t state; // envelope generator state
87 bool lfo_active;
88 };
89
90 // SoundDevice
91 void generateChannels(std::span<float*> bufs, unsigned num) override;
92
93 void writeRegDirect(uint8_t reg, uint8_t data, EmuTime::param time);
94 [[nodiscard]] unsigned getRamAddress(unsigned addr) const;
95 [[nodiscard]] int16_t getSample(Slot& slot, uint16_t pos) const;
96 [[nodiscard]] static uint16_t nextPos(Slot& slot, uint16_t pos, uint16_t increment);
97 void advance();
98 [[nodiscard]] bool anyActive();
99 void keyOnHelper(Slot& slot);
100
101 MSXMotherBoard& motherBoard;
102
103 struct DebugRegisters final : SimpleDebuggable {
104 DebugRegisters(MSXMotherBoard& motherBoard, const std::string& name);
105 [[nodiscard]] uint8_t read(unsigned address) override;
106 void write(unsigned address, uint8_t value, EmuTime::param time) override;
107 } debugRegisters;
108
109 struct DebugMemory final : SimpleDebuggable {
110 DebugMemory(MSXMotherBoard& motherBoard, const std::string& name);
111 [[nodiscard]] uint8_t read(unsigned address) override;
112 void write(unsigned address, uint8_t value) override;
113 } debugMemory;
114
115 std::array<Slot, 24> slots;
116
118 unsigned eg_cnt;
119
120 int memAdr;
121
122 Rom rom;
123 TrackedRam ram;
124
125 std::array<uint8_t, 256> regs;
126};
127SERIALIZE_CLASS_VERSION(YMF278::Slot, 6);
129
130} // namespace openmsx
131
132#endif
uint8_t readReg(uint8_t reg)
Definition YMF278.cc:754
void writeReg(uint8_t reg, uint8_t data, EmuTime::param time)
Definition YMF278.cc:579
void setMixLevel(uint8_t x, EmuTime::param time)
Definition YMF278.cc:489
void reset(EmuTime::param time)
Definition YMF278.cc:841
void writeMem(unsigned address, uint8_t value)
Definition YMF278.cc:968
uint8_t readMem(unsigned address) const
Definition YMF278.cc:950
void clearRam()
Definition YMF278.cc:836
void serialize(Archive &ar, unsigned version)
Definition YMF278.cc:1086
uint8_t peekReg(uint8_t reg) const
Definition YMF278.cc:769
This file implemented 3 utility functions:
Definition Autofire.cc:9
void serialize(Archive &ar, T &t, unsigned version)
#define SERIALIZE_CLASS_VERSION(CLASS, VERSION)