Debian Codegraph @

BambooTracker/chip/opna.hpp

raw
1 /*
2 * Copyright (C) 2018-2023 Rerrah
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use,
8 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following
11 * conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #pragma once
27
28 #include "chip.hpp"
29 #include <memory>
30 #include <deque>
31 #include "resampler.hpp"
32 #include "2608_interface.hpp"
33 #include "real_chip_interface.hpp"
34
35 namespace chip
36 {
37 enum class OpnaEmulator
38 {
39 Mame,
40 Nuked,
41 Ymfm,
42 First = Mame,
43 Last = Ymfm,
44 };
45
46 class OPNA final : public Chip
47 {
48 public:
49 // [rate]
50 // 0 = rate is 55466 (FM synthesis rate when clock is 3993600 * 2)
51 OPNA(OpnaEmulator emu, int clock, int rate, size_t maxDuration, size_t dramSize,
52 std::unique_ptr<AbstractResampler> fmResampler = std::make_unique<BlipResampler>(),
53 std::unique_ptr<AbstractResampler> ssgResampler = std::make_unique<BlipResampler>(),
54 std::shared_ptr<AbstractRegisterWriteLogger> logger = nullptr);
55 ~OPNA() override;
56
57 void setImmediateWriteMode(bool enabled) noexcept;
58 bool isImmediateWriteMode() const noexcept;
59 void setForcedWriteMode(bool enabled) noexcept { isForcedRegWrite_ = enabled; }
60 void setRegister(uint32_t offset, uint8_t value) override;
61 uint8_t getRegister(uint32_t offset) const override;
62 void setVolumeFM(double dB);
63 double getVolumeFM() const noexcept { return volumeFm_; }
64 void setVolumeSSG(double dB);
65 double getVolumeSSG() const noexcept { return volumeSsg_; }
66 size_t getDRAMSize() const noexcept;
67
68 /**
69 * @brief mix samples.
70 * @param stream buffer where mixed samples are stored.
71 * @param nSamples number of samples
72 * @return true if sample generation is success, otherwise false.
73 */
74 bool mix(int16_t* stream, size_t nSamples) override;
75
76 void setFmResampler(std::unique_ptr<AbstractResampler> resampler);
77 void setSsgResampler(std::unique_ptr<AbstractResampler> resampler);
78
79 void connectToRealChip(RealChipInterfaceType type, RealChipInterfaceGeneratorFunc* f);
80 RealChipInterfaceType getRealChipInterfaceType() const;
81 bool hasConnectedToRealChip() const;
82
83 private:
84 static size_t count_;
85
86 std::unique_ptr<Ym2608Interface> intf_;
87 double volumeFm_, volumeSsg_;
88 constexpr static int VOLUME_RATIO_MOD_ = 2;
89 size_t dramSize_;
90
91 std::unique_ptr<SimpleRealChipInterface> rcIntf_;
92
93 void resetSpecific() override;
94
95 struct RegisterWrite
96 {
97 uint32_t address;
98 uint8_t data;
99 bool isPortA_;
100 };
101
102 bool isForcedRegWrite_;
103 std::deque<RegisterWrite> regWrites_, forcedRegWrites_;
104
105 void enqueueData(uint32_t offset, uint8_t value);
106 void writeDataImmediately(uint32_t offset, uint8_t value);
107
108 size_t dequeueData();
109
110 size_t waitRestFm_, waitRestSsg2_;
111 size_t rate2_;
112 sample* tmpBuf_[2];
113 bool storeBufferForImmediate(size_t nSamples, size_t& pointFm, size_t& pointSsg);
114 bool storeBufferForWait(size_t nSamples, size_t& pointFm, size_t& pointSsg);
115 void flushWait(size_t& pointFm, size_t maxFm, size_t& pointSsg2, size_t maxSsg2);
116
117 struct WriteModeFuncs
118 {
119 void (OPNA::*setRegister)(uint32_t, uint8_t);
120 bool (OPNA::*storeBuffer)(size_t, size_t&, size_t&);
121 } writeFuncs[2];
122 WriteModeFuncs* writeFunc;
123 };
124 }