openMSX
SN76489.hh
Go to the documentation of this file.
1 #ifndef SN76489_HH
2 #define SN76489_HH
3 
6 
7 namespace openmsx {
8 
9 /** This class implements the Texas Instruments SN76489 sound chip.
10  * Unlike the AY-3-8910, this chip only performs sound synthesis.
11  *
12  * Resources used:
13  * - SN76489AN data sheet
14  * http://map.grauw.nl/resources/sound/texas_instruments_sn76489an.pdf
15  * - Developer documentation on SMS Power!
16  * http://www.smspower.org/Development/SN76489
17  * - John Kortink's reverse engineering results
18  * http://www.zeridajh.org/articles/various/sn76489/index.htm
19  * - MAME's implementation
20  * https://github.com/mamedev/mame/blob/master/src/devices/sound/sn76496.cpp
21  * - blueMSX's implementation
22  * https://sourceforge.net/p/bluemsx/code/HEAD/tree/trunk/blueMSX/Src/SoundChips/SN76489.c
23  */
24 class SN76489 final : public ResampledSoundDevice
25 {
26 public:
27  SN76489(const DeviceConfig& config);
28  ~SN76489();
29 
30  // ResampledSoundDevice
31  void generateChannels(float** buffers, unsigned num) override;
32 
33  void reset(EmuTime::param time);
34  void write(byte value, EmuTime::param time);
35 
36  template<typename Archive>
37  void serialize(Archive& ar, unsigned version);
38 
39 private:
40  class NoiseShifter {
41  public:
42  /** Sets the feedback pattern and resets the shift register to the
43  * initial state that matches that pattern.
44  */
45  inline void initState(unsigned pattern, unsigned period);
46 
47  /** Gets the current output of this shifter: 0 or 1.
48  */
49  inline unsigned getOutput() const;
50 
51  /** Advances the shift register one step, to the next output.
52  */
53  inline void advance();
54 
55  /** Records that the shift register should be advanced multiple steps
56  * before the next output is used.
57  */
58  inline void queueAdvance(unsigned steps);
59 
60  /** Advances the shift register by the number of steps that were queued.
61  */
62  void catchUp();
63 
64  template<typename Archive>
65  void serialize(Archive& ar, unsigned version);
66 
67  private:
68  unsigned pattern;
69  unsigned period;
70  unsigned random;
71  unsigned stepsBehind;
72  };
73 
74  /** Initialize 'volTable'.
75  */
76  void initVolumeTable(int volume);
77 
78  /** Initialize registers, counters and other chip state.
79  */
80  void initState();
81 
82  /** Initialize noise shift register according to chip variant and noise
83  * mode.
84  */
85  void initNoise();
86 
87  word peekRegister(unsigned reg, EmuTime::param time) const;
88  void writeRegister(unsigned reg, word value, EmuTime::param time);
89  template <bool NOISE> void synthesizeChannel(
90  float*& buffer, unsigned num, unsigned generator);
91 
92  unsigned volTable[16];
93 
94  NoiseShifter noiseShifter;
95 
96  /** Register bank. The tone period registers (0, 2, 4) are 12 bits wide,
97  * all other registers are 4 bits wide.
98  */
99  word regs[8];
100 
101  /** The last register written to (0-7).
102  */
103  byte registerLatch;
104 
105  /** Frequency counter for each channel.
106  * These count down and when they reach zero, the output flips and the
107  * counter is re-loaded with the value of the period register.
108  */
109  word counters[4];
110 
111  /** Output flip-flop state (0 or 1) for each channel.
112  */
113  byte outputs[4];
114 
115  struct Debuggable final : SimpleDebuggable {
116  Debuggable(MSXMotherBoard& motherBoard, const std::string& name);
117  byte read(unsigned address, EmuTime::param time) override;
118  void write(unsigned address, byte value, EmuTime::param time) override;
119  } debuggable;
120 };
121 
122 } // namespace openmsx
123 
124 #endif
bool getEnum() const noexcept
Definition: EnumSetting.hh:96
void reset(EmuTime::param time)
Definition: SN76489.cc:135
void serialize(Archive &ar, unsigned version)
Definition: SN76489.cc:303
void write(byte value, EmuTime::param time)
Definition: SN76489.cc:141
void generateChannels(float **buffers, unsigned num) override
Abstract method to generate the actual sound data.
Definition: SN76489.cc:281
SN76489(const DeviceConfig &config)
Definition: SN76489.cc:74