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 "openmsx.hh"
10 #include "serialize_meta.hh"
11 #include <string>
12 
13 namespace openmsx {
14 
15 class DeviceConfig;
16 
17 class YMF278 final : public ResampledSoundDevice
18 {
19 public:
20  YMF278(const std::string& name, int ramSize,
21  const DeviceConfig& config);
22  ~YMF278();
23  void clearRam();
24  void reset(EmuTime::param time);
25  void writeReg(byte reg, byte data, EmuTime::param time);
26  byte readReg(byte reg);
27  byte peekReg(byte reg) const;
28 
29  byte readMem(unsigned address) const;
30  void writeMem(unsigned address, byte value);
31 
32  void setMixLevel(uint8_t x, EmuTime::param time);
33 
34  template<typename Archive>
35  void serialize(Archive& ar, unsigned version);
36 
37 private:
38  class Slot {
39  public:
40  Slot();
41  void reset();
42  int compute_rate(int val) const;
43  int compute_decay_rate(int val) const;
44  unsigned decay_rate(int num, int sample_rate);
45  void envelope_next(int sample_rate);
46  int16_t compute_vib() const;
47  uint16_t compute_am() const;
48 
49  template<typename Archive>
50  void serialize(Archive& ar, unsigned version);
51 
52  uint32_t startaddr;
53  uint16_t loopaddr;
54  uint16_t endaddr; // Note: stored in 2s complement (0x0000 = 0, 0x0001 = -65536, 0xffff = -1)
55  uint32_t step; // fixed-point frequency step
56  // invariant: step == calcStep(OCT, FN)
57  uint32_t stepptr; // fixed-point pointer into the sample
58  uint16_t pos;
59  int16_t sample1, sample2;
60 
61  int16_t env_vol;
62 
63  uint32_t lfo_cnt;
64 
65  int16_t DL;
66  uint16_t wave; // wavetable 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; // panpot 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(int** bufs, unsigned num) override;
92 
93  void writeRegDirect(byte reg, byte data, EmuTime::param time);
94  unsigned getRamAddress(unsigned addr) const;
95  int16_t getSample(Slot& op);
96  void advance();
97  bool anyActive();
98  void keyOnHelper(Slot& slot);
99 
100  MSXMotherBoard& motherBoard;
101 
102  struct DebugRegisters final : SimpleDebuggable {
103  DebugRegisters(MSXMotherBoard& motherBoard, const std::string& name);
104  byte read(unsigned address) override;
105  void write(unsigned address, byte value, EmuTime::param time) override;
106  } debugRegisters;
107 
108  struct DebugMemory final : SimpleDebuggable {
109  DebugMemory(MSXMotherBoard& motherBoard, const std::string& name);
110  byte read(unsigned address) override;
111  void write(unsigned address, byte value) override;
112  } debugMemory;
113 
114  Slot slots[24];
115 
117  unsigned eg_cnt;
118 
119  int memadr;
120 
121  Rom rom;
122  TrackedRam ram;
123 
124  byte regs[256];
125 };
126 SERIALIZE_CLASS_VERSION(YMF278::Slot, 5);
128 
129 } // namespace openmsx
130 
131 #endif
void clearRam()
Definition: YMF278.cc:834
byte readReg(byte reg)
Definition: YMF278.cc:753
uint8_t byte
8 bit unsigned integer
Definition: openmsx.hh:26
void writeReg(byte reg, byte data, EmuTime::param time)
Definition: YMF278.cc:581
void writeMem(unsigned address, byte value)
Definition: YMF278.cc:966
void serialize(Archive &ar, unsigned version)
Definition: YMF278.cc:1084
void setMixLevel(uint8_t x, EmuTime::param time)
Definition: YMF278.cc:479
SERIALIZE_CLASS_VERSION(CassettePlayer, 2)
byte readMem(unsigned address) const
Definition: YMF278.cc:948
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
YMF278(const std::string &name, int ramSize, const DeviceConfig &config)
Definition: YMF278.cc:792
byte peekReg(byte reg) const
Definition: YMF278.cc:768
void reset(EmuTime::param time)
Definition: YMF278.cc:839