Debian Codegraph @

android/app/jni/extern/vgsound_emu-modified/vgsound_emu/src/scc/scc.hpp

raw
1 /*
2 License: Zlib
3 see https://gitlab.com/cam900/vgsound_emu/-/blob/main/LICENSE for more details
4
5 Copyright holder(s): cam900
6 Contributor(s): Natt Akuma, James Alan Nguyen, Laurens Holst
7 Konami SCC emulation core
8
9 modified by tildearrow...
10 */
11
12 #ifndef _VGSOUND_EMU_SRC_SCC_HPP
13 #define _VGSOUND_EMU_SRC_SCC_HPP
14
15 #pragma once
16
17 #include "blip_buf.h"
18 #include "../core/core.hpp"
19 #include "../core/util/mem_intf.hpp"
20 #include <string.h>
21
22 struct DivDispatchOscBuffer;
23
24 using namespace vgsound_emu;
25
26 // shared for SCCs
27 class scc_core : public vgsound_emu_core
28 {
29 private:
30 // classes
31 class voice_t : public vgsound_emu_core
32 {
33 public:
34 // constructor
35 voice_t(scc_core &host)
36 : vgsound_emu_core("scc_voice")
37 , m_host(host)
38 , m_enable(false)
39 , m_pitch(0)
40 , m_volume(0)
41 , m_addr(0)
42 , m_counter(0)
43 , m_out(0)
44 {
45 memset(m_wave,0,32);
46 }
47
48 // internal state
49 void reset();
50 void tick(const int cycles, blip_buffer_t* bb, DivDispatchOscBuffer* oscBuf);
51 void updateOut(const int pos);
52
53 // accessors
54 inline void reset_addr() { m_addr = 0; }
55
56 // setters
57 inline void set_wave(u8 addr, s8 wave) { m_wave[addr & 0x1f] = wave; }
58
59 inline void set_enable(bool enable) { m_enable = enable; }
60
61 inline void set_pitch(u16 pitch, u16 mask = 0xfff)
62 {
63 m_pitch = (m_pitch & ~(mask & 0xfff)) | (pitch & (mask & 0xfff));
64 m_counter = m_pitch;
65 }
66
67 inline void set_volume(u8 volume) { m_volume = volume & 0xf; }
68
69 // getters
70 inline s8 wave(u8 addr) { return m_wave[addr & 0x1f]; }
71
72 inline u8 addr() { return m_addr; }
73
74 inline s32 out() { return m_out; }
75
76 private:
77 // registers
78 scc_core &m_host;
79 blip_buffer_t* m_bb;
80 DivDispatchOscBuffer* m_oscBuf;
81 s8 m_wave[32]; // internal waveform
82 bool m_enable = false; // output enable flag
83 u16 m_pitch : 12; // pitch
84 u16 m_volume : 4; // volume
85 u8 m_addr = 0; // waveform pointer
86 u16 m_counter = 0; // frequency counter
87 s32 m_out = 0; // current output
88 };
89
90 class test_t : public vgsound_emu_core
91 {
92 public:
93 // constructor
94 test_t()
95 : vgsound_emu_core("scc_test")
96 , m_freq_4bit(0)
97 , m_freq_8bit(0)
98 , m_resetpos(0)
99 , m_rotate(0)
100 , m_rotate4(0)
101 {
102 }
103
104 void reset()
105 {
106 m_freq_4bit = 0;
107 m_freq_8bit = 0;
108 m_resetpos = 0;
109 m_rotate = 0;
110 m_rotate4 = 0;
111 }
112
113 // setters
114 inline void set_freq_4bit(bool freq_4bit) { m_freq_4bit = freq_4bit; }
115
116 inline void set_freq_8bit(bool freq_8bit) { m_freq_8bit = freq_8bit; }
117
118 inline void set_resetpos(bool resetpos) { m_resetpos = resetpos; }
119
120 inline void set_rotate(bool rotate) { m_rotate = rotate; }
121
122 inline void set_rotate4(bool rotate4) { m_rotate4 = rotate4; }
123
124 // getters
125 inline bool freq_4bit() { return m_freq_4bit; }
126
127 inline bool freq_8bit() { return m_freq_8bit; }
128
129 inline bool resetpos() { return m_resetpos; }
130
131 inline bool rotate() { return m_rotate; }
132
133 inline bool rotate4() { return m_rotate4; }
134
135 private:
136 u8 m_freq_4bit : 1; // 4 bit frequency
137 u8 m_freq_8bit : 1; // 8 bit frequency
138 u8 m_resetpos : 1; // reset counter after pitch writes
139 u8 m_rotate : 1; // rotate and write protect waveform for all channels
140 u8 m_rotate4 : 1; // same as above but for channel 4 only
141 };
142
143 public:
144 // constructor
145 scc_core(std::string tag)
146 : vgsound_emu_core(tag)
147 , m_voice{*this, *this, *this, *this, *this}
148 , m_test(test_t())
149 , m_out(0)
150 {
151 memset(m_reg,0,256);
152 }
153
154 // destructor
155 virtual ~scc_core() {}
156
157 // accessors
158 virtual u8 scc_r(bool is_sccplus, u8 address) = 0;
159 virtual void scc_w(bool is_sccplus, u8 address, u8 data) = 0;
160
161 // internal state
162 virtual void reset();
163 void tick(const int cycles, blip_buffer_t* bb, DivDispatchOscBuffer** oscBuf);
164
165 // getters
166 inline s32 out() { return m_out; } // output to DA0...DA10 pin
167
168 inline u8 reg(u8 address) { return m_reg[address]; }
169
170 // for preview
171 inline s32 voice_out(u8 voice) { return (voice < 5) ? m_voice[voice].out() : 0; }
172
173 protected:
174 // accessor
175 u8 wave_r(bool is_sccplus, u8 address);
176 void wave_w(bool is_sccplus, u8 address, u8 data);
177 void freq_vol_enable_w(u8 address, u8 data);
178
179 // internal values
180 voice_t m_voice[5]; // 5 voices
181
182 test_t m_test; // test register
183 s32 m_out = 0; // output to DA0...10
184
185 u8 m_reg[256]; // register pool
186 };
187
188 // SCC core
189 class k051649_scc_core : public scc_core
190 {
191 public:
192 // constructor
193 k051649_scc_core(std::string tag = "k051649_scc")
194 : scc_core(tag)
195 {
196 }
197
198 // accessors
199 virtual u8 scc_r(bool is_sccplus, u8 address) override;
200 virtual void scc_w(bool is_sccplus, u8 address, u8 data) override;
201 };
202
203 class k052539_scc_core : public k051649_scc_core
204 {
205 public:
206 // constructor
207 k052539_scc_core(std::string tag = "k052539_scc")
208 : k051649_scc_core(tag)
209 {
210 }
211
212 // accessors
213 virtual u8 scc_r(bool is_sccplus, u8 address) override;
214 virtual void scc_w(bool is_sccplus, u8 address, u8 data) override;
215 };
216
217 // MegaROM Mapper with SCC
218 class k051649_core : public k051649_scc_core
219 {
220 friend class vgsound_emu_mem_intf; // for megaROM mapper
221
222 private:
223 // mapper classes
224 class k051649_mapper_t : public vgsound_emu_core
225 {
226 public:
227 k051649_mapper_t()
228 : vgsound_emu_core("k051649_mapper")
229 , m_bank{0, 1, 2, 3}
230 {
231 }
232
233 // internal state
234 void reset();
235
236 // setters
237 inline void set_bank(u8 slot, u8 bank) { m_bank[slot & 3] = bank; }
238
239 // getters
240 inline u8 bank(u8 slot) { return m_bank[slot & 3]; }
241
242 private:
243 // registers
244 u8 m_bank[4] = {0, 1, 2, 3};
245 };
246
247 public:
248 // constructor
249 k051649_core(vgsound_emu_mem_intf &intf)
250 : k051649_scc_core("k051649")
251 , m_intf(intf)
252 , m_mapper(k051649_mapper_t())
253 , m_scc_enable(false)
254 {
255 }
256
257 // accessors
258 u8 read(u16 address);
259 void write(u16 address, u8 data);
260
261 virtual void reset() override;
262
263 private:
264 vgsound_emu_mem_intf m_intf;
265 k051649_mapper_t m_mapper;
266 bool m_scc_enable = false;
267 };
268
269 // MegaRAM Mapper with SCC
270 class k052539_core : public k052539_scc_core
271 {
272 friend class vgsound_emu_mem_intf; // for megaRAM mapper
273
274 private:
275 // mapper classes
276 class k052539_mapper_t : public vgsound_emu_core
277 {
278 public:
279 k052539_mapper_t()
280 : vgsound_emu_core("k052539_mapper")
281 {
282 m_bank[0] = 0;
283 m_bank[1] = 1;
284 m_bank[2] = 2;
285 m_bank[3] = 3;
286 m_ram_enable.fill(false);
287 }
288
289 // internal state
290 void reset();
291
292 // setters
293 inline void set_bank(u8 slot, u8 bank) { m_bank[slot & 3] = bank; }
294
295 inline void set_ram_enable(u8 slot, bool ram_enable)
296 {
297 m_ram_enable[slot & 3] = ram_enable;
298 }
299
300 // getters
301 inline u8 bank(u8 slot) { return m_bank[slot & 3]; }
302
303 inline bool ram_enable(u8 slot) { return m_ram_enable[slot & 3]; }
304
305 private:
306 // registers
307 std::array<u8, 4> m_bank;
308 std::array<bool, 4> m_ram_enable;
309 };
310
311 public:
312 // constructor
313 k052539_core(vgsound_emu_mem_intf &intf)
314 : k052539_scc_core("k052539")
315 , m_intf(intf)
316 , m_mapper(k052539_mapper_t())
317 , m_scc_enable(false)
318 , m_is_sccplus(false)
319 {
320 }
321
322 // accessors
323 u8 read(u16 address);
324 void write(u16 address, u8 data);
325
326 virtual void reset() override;
327
328 private:
329 vgsound_emu_mem_intf m_intf;
330 k052539_mapper_t m_mapper;
331 bool m_scc_enable = false;
332 bool m_is_sccplus = false;
333 };
334
335 #endif