Debian Codegraph @

src/engine/platform/sound/nds.hpp

raw
1 /*
2
3 ============================================================================
4
5 NDS sound emulator
6 by cam900
7
8 MODIFIED BY TILDEARROW!!!
9 MODIFIED BY TILDEARROW!!!
10 MODIFIED BY TILDEARROW!!!
11 MODIFIED BY TILDEARROW!!!
12 MODIFIED BY TILDEARROW!!!
13 MODIFIED BY TILDEARROW!!!
14
15 making it SUPER CLEAR to comply with the license
16 this is NOT the original version! for the original version, git checkout
17 any commit from January 2025.
18
19 This file is licensed under zlib license.
20
21 ============================================================================
22
23 zlib License
24
25 (C) 2024-present cam900 and contributors
26
27 This software is provided 'as-is', without any express or implied
28 warranty. In no event will the authors be held liable for any damages
29 arising from the use of this software.
30
31 Permission is granted to anyone to use this software for any purpose,
32 including commercial applications, and to alter it and redistribute it
33 freely, subject to the following restrictions:
34
35 1. The origin of this software must not be misrepresented; you must not
36 claim that you wrote the original software. If you use this software
37 in a product, an acknowledgment in the product documentation would be
38 appreciated but is not required.
39 2. Altered source versions must be plainly marked as such, and must not be
40 misrepresented as being the original software.
41 3. This notice may not be removed or altered from any source distribution.
42
43 ============================================================================
44 TODO:
45 - needs to further verifications from real hardware
46
47 Tech info: https://problemkaputt.de/gbatek.htm
48
49 */
50
51 #ifndef NDS_SOUND_EMU_H
52 #define NDS_SOUND_EMU_H
53
54 #include <stdlib.h>
55 #include "blip_buf.h"
56 #include "../../dispatch.h"
57
58 namespace nds_sound_emu
59 {
60 using u8 = unsigned char;
61 using u16 = unsigned short;
62 using u32 = unsigned int;
63 using u64 = unsigned long long;
64 using s8 = signed char;
65 using s16 = signed short;
66 using s32 = signed int;
67 using s64 = signed long long;
68
69 template<typename T>
70 static const inline T bitfield(const T in, const u8 pos)
71 {
72 return (in >> pos) & 1;
73 } // bitfield
74
75 template<typename T>
76 static const inline T bitfield(const T in, const u8 pos, const u8 len)
77 {
78 return (in >> pos) & ((1 << len) - 1);
79 } // bitfield
80
81 template<typename T>
82 static const inline T clamp(const T in, const T min, const T max)
83 {
84 return (in < min) ? min : ((in > max) ? max : in);
85 } // clamp
86
87 class nds_sound_intf
88 {
89 public:
90 nds_sound_intf()
91 {
92 }
93
94 virtual u8 read_byte(u32 addr) { return 0; }
95 inline u16 read_word(u32 addr) { return read_byte(addr) | (u16(read_byte(addr + 1)) << 8); }
96 inline u32 read_dword(u32 addr) { return read_word(addr) | (u16(read_word(addr + 2)) << 16); }
97
98 virtual void write_byte(u32 addr, u8 data) {}
99 inline void write_word(u32 addr, u16 data)
100 {
101 write_byte(addr, data & 0xff);
102 write_byte(addr + 1, data >> 8);
103 }
104 inline void write_dword(u32 addr, u32 data)
105 {
106 write_word(addr, data & 0xffff);
107 write_word(addr + 2, data >> 16);
108 }
109 };
110
111 class nds_sound_t
112 {
113 public:
114 nds_sound_t(nds_sound_intf &intf)
115 : m_intf(intf)
116 , m_channel{
117 channel_t(*this, false, false), channel_t(*this, false, false),
118 channel_t(*this, false, false), channel_t(*this, false, false),
119 channel_t(*this, false, false), channel_t(*this, false, false),
120 channel_t(*this, false, false), channel_t(*this, false, false),
121 channel_t(*this, true, false), channel_t(*this, true, false),
122 channel_t(*this, true, false), channel_t(*this, true, false),
123 channel_t(*this, true, false), channel_t(*this, true, false),
124 channel_t(*this, false, true), channel_t(*this, false, true)
125 }
126 , m_capture{
127 capture_t(*this, m_channel[0], m_channel[1]),
128 capture_t(*this, m_channel[2], m_channel[3])
129 }
130 , m_control(0)
131 , m_bias(0)
132 , m_lastts(0)
133 {
134 }
135
136 void reset();
137 void resetTS(u32 what);
138 void tick(s32 cycle);
139 s32 predict();
140 void set_bb(blip_buffer_t* bbLeft, blip_buffer_t* bbRight);
141 void set_oscbuf(DivDispatchOscBuffer** oscBuf);
142
143 // host accesses
144 u32 read32(u32 addr);
145 void write32(u32 addr, u32 data, u32 mask = ~0);
146
147 u16 read16(u32 addr);
148 void write16(u32 addr, u16 data, u16 mask = ~0);
149
150 u8 read8(u32 addr);
151 void write8(u32 addr, u8 data);
152
153 // for debug
154 s32 chan_lout(u8 ch) { return m_channel[ch].loutput(); }
155 s32 chan_rout(u8 ch) { return m_channel[ch].routput(); }
156
157 private:
158 // ADPCM tables
159 s8 adpcm_index_table[8] =
160 {
161 -1, -1, -1, -1, 2, 4, 6, 8
162 };
163
164 u16 adpcm_diff_table[89] =
165 {
166 0x0007, 0x0008, 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x0010,
167 0x0011, 0x0013, 0x0015, 0x0017, 0x0019, 0x001c, 0x001f, 0x0022, 0x0025,
168 0x0029, 0x002d, 0x0032, 0x0037, 0x003c, 0x0042, 0x0049, 0x0050, 0x0058,
169 0x0061, 0x006b, 0x0076, 0x0082, 0x008f, 0x009d, 0x00ad, 0x00be, 0x00d1,
170 0x00e6, 0x00fd, 0x0117, 0x0133, 0x0151, 0x0173, 0x0198, 0x01c1, 0x01ee,
171 0x0220, 0x0256, 0x0292, 0x02d4, 0x031c, 0x036c, 0x03c3, 0x0424, 0x048e,
172 0x0502, 0x0583, 0x0610, 0x06ab, 0x0756, 0x0812, 0x08e0, 0x09c3, 0x0abd,
173 0x0bd0, 0x0cff, 0x0e4c, 0x0fba, 0x114c, 0x1307, 0x14ee, 0x1706, 0x1954,
174 0x1bdc, 0x1ea5, 0x21b6, 0x2515, 0x28ca, 0x2cdf, 0x315b, 0x364b, 0x3bb9,
175 0x41b2, 0x4844, 0x4f7e, 0x5771, 0x602f, 0x69ce, 0x7462, 0x7fff
176 };
177
178 // structs
179 enum
180 {
181 STATE_ADPCM_LOAD = 0,
182 STATE_PRE_LOOP,
183 STATE_POST_LOOP
184 };
185
186 class channel_t
187 {
188 public:
189 channel_t(nds_sound_t &host, bool psg, bool noise)
190 : m_host(host)
191
192 , m_psg(psg)
193 , m_noise(noise)
194
195 , m_bb{NULL,NULL}
196 , m_oscBuf(NULL)
197
198 , m_control(0)
199 , m_sourceaddr(0)
200 , m_freq(0)
201 , m_loopstart(0)
202 , m_length(0)
203 , m_ctl_volume(0)
204 , m_ctl_voldiv(0)
205 , m_ctl_hold(0)
206 , m_ctl_pan(0)
207 , m_ctl_duty(0)
208 , m_ctl_repeat(0)
209 , m_ctl_format(0)
210 , m_ctl_busy(0)
211 , m_playing(false)
212 , m_adpcm_out(0)
213 , m_adpcm_index(0)
214 , m_prev_adpcm_out(0)
215 , m_prev_adpcm_index(0)
216 , m_cur_addr(0)
217 , m_cur_state(0)
218 , m_cur_bitaddr(0)
219 , m_delay(0)
220 , m_sample(0)
221 , m_lfsr(0x7fff)
222 , m_lfsr_out(0x7fff)
223 , m_counter(0x10000)
224 , m_output(0)
225 , m_loutput(0)
226 , m_routput(0)
227 , m_lastts(0)
228 {
229 }
230
231 void reset();
232 void write(u32 offset, u32 data, u32 mask = ~0);
233
234 void update(s32 cycle);
235 void set_bb(blip_buffer_t* bbLeft, blip_buffer_t* bbRight) { m_bb[0] = bbLeft; m_bb[1] = bbRight; }
236 void set_oscbuf(DivDispatchOscBuffer* oscBuf) { m_oscBuf = oscBuf; }
237 void resetTS(u32 what) { m_lastts = what; }
238 s32 predict();
239
240 // getters
241 // control word
242 u32 control() const { return m_control; }
243 u32 freq() const { return m_freq; }
244
245 // outputs
246 s32 output() const { return m_output; }
247 s32 loutput() const { return m_loutput; }
248 s32 routput() const { return m_routput; }
249
250 private:
251 // inline constants
252 const u8 m_voldiv_shift[4] = {0, 1, 2, 4};
253
254 // control bits
255 s32 volume() const { return m_ctl_volume; } // global volume
256 u32 voldiv() const { return m_ctl_voldiv; } // volume shift
257 bool hold() const { return m_ctl_hold; } // hold bit
258 u32 pan() const { return m_ctl_pan; } // panning (0...127, 0 = left, 127 = right, 64 = half)
259 u32 duty() const { return m_ctl_duty; } // PSG duty
260 u32 repeat() const { return m_ctl_repeat; } // Repeat mode (Manual, Loop infinitely, One-shot)
261 u32 format() const { return m_ctl_format; } // Sound Format (PCM8, PCM16, ADPCM, PSG/Noise when exists)
262 bool busy() const { return m_ctl_busy; } // Busy flag
263
264 // calculated values
265 s32 lvol() const { return (m_ctl_pan == 0x7f) ? 0 : 128 - m_ctl_pan; } // calculated left volume
266 s32 rvol() const { return (m_ctl_pan == 0x7f) ? 128 : m_ctl_pan; } // calculated right volume
267
268 // calculated address
269 u32 addr() const { return (m_sourceaddr & ~3) + (m_cur_bitaddr >> 3) + (m_cur_state == STATE_POST_LOOP ? ((m_loopstart + m_cur_addr) << 2) : (m_cur_addr << 2)); }
270
271 void keyon();
272 void keyoff();
273 void fetch();
274 void advance();
275
276 // interfaces
277 nds_sound_t &m_host; // host device
278
279 // configuration
280 bool m_psg = false; // PSG Enable
281 bool m_noise = false; // Noise Enable
282
283 // blip_buf
284 blip_buffer_t* m_bb[2];
285 DivDispatchOscBuffer* m_oscBuf;
286
287 // registers
288 u32 m_control = 0; // Control
289 u32 m_sourceaddr = 0; // Source Address
290 u16 m_freq = 0; // Frequency
291 u16 m_loopstart = 0; // Loop Start
292 u32 m_length = 0; // Length
293
294 // exploded control
295 s32 m_ctl_volume = 0; // Volume (0-6)
296 u32 m_ctl_voldiv = 0; // Volume Shift (8-9)
297 bool m_ctl_hold = 0; // Hold (15)
298 u32 m_ctl_pan = 0; // Panning (16-22)
299 u32 m_ctl_duty = 0; // Duty (24-26)
300 u32 m_ctl_repeat = 0; // Repeat Mode (27-28)
301 u32 m_ctl_format = 0; // Sound Format (29-30)
302 bool m_ctl_busy = 0; // Busy Flag (31)
303
304 // internal states
305 bool m_playing = false; // playing flag
306 s32 m_adpcm_out = 0; // current ADPCM sample value
307 s32 m_adpcm_index = 0; // current ADPCM step
308 s32 m_prev_adpcm_out = 0; // previous ADPCM sample value
309 s32 m_prev_adpcm_index = 0; // previous ADPCM step
310 u32 m_cur_addr = 0; // current address
311 s32 m_cur_state = 0; // current state
312 s32 m_cur_bitaddr = 0; // bit address
313 s32 m_delay = 0; // delay
314 s16 m_sample = 0; // current sample
315 u32 m_lfsr = 0x7fff; // noise LFSR
316 s16 m_lfsr_out = 0x7fff; // LFSR output
317 s32 m_counter = 0x10000; // clock counter
318 s32 m_output = 0; // current output
319 s32 m_loutput = 0; // current left output
320 s32 m_routput = 0; // current right output
321 u32 m_lastts = 0; // running timestamp
322 };
323
324 class capture_t
325 {
326 public:
327 capture_t(nds_sound_t &host, channel_t &input, channel_t &output)
328 : m_host(host)
329 , m_input(input)
330 , m_output(output)
331
332 , m_control(0)
333 , m_dstaddr(0)
334 , m_length(0)
335
336 , m_counter(0x10000)
337 , m_cur_addr(0)
338 , m_cur_waddr(0)
339 , m_cur_bitaddr(0)
340 , m_enable(0)
341
342 , m_fifo{
343 fifo_data_t(), fifo_data_t(), fifo_data_t(), fifo_data_t(),
344 fifo_data_t(), fifo_data_t(), fifo_data_t(), fifo_data_t()
345 }
346 , m_fifo_head(0)
347 , m_fifo_tail(0)
348 , m_fifo_empty(true)
349 , m_fifo_full(false)
350 {
351 }
352
353 void reset();
354 void update(s32 mix, s32 cycle);
355
356 void control_w(u8 data);
357 void addrlen_w(u32 offset, u32 data, u32 mask = ~0);
358
359 // getters
360 u32 control() const { return m_control; }
361 u32 dstaddr() const { return m_dstaddr; }
362
363 private:
364 // inline constants
365 // control bits
366 bool addmode() const { return bitfield(m_control, 0); } // Add mode (add channel 1/3 output with channel 0/2)
367 bool get_source() const { return bitfield(m_control, 1); } // Select source (left or right mixer, channel 0/2)
368 bool repeat() const { return bitfield(m_control, 2); } // repeat flag
369 bool format() const { return bitfield(m_control, 3); } // store format (PCM16, PCM8)
370 bool busy() const { return bitfield(m_control, 7); } // busy flag
371
372 // FIFO offset mask
373 u32 fifo_mask() const { return format() ? 7 : 3; }
374
375 // calculated address
376 u32 waddr() const { return (m_dstaddr & ~3) + (m_cur_waddr << 2); }
377
378 void capture_on();
379 void capture_off();
380 bool fifo_write();
381
382 // interfaces
383 nds_sound_t &m_host; // host device
384 channel_t &m_input; // Input channel
385 channel_t &m_output; // Output channel
386
387 // registers
388 u8 m_control = 0; // Control
389 u32 m_dstaddr = 0; // Destination Address
390 u32 m_length = 0; // Buffer Length
391
392 // internal states
393 u32 m_counter = 0x10000; // clock counter
394 u32 m_cur_addr = 0; // current address
395 u32 m_cur_waddr = 0; // current write address
396 s32 m_cur_bitaddr = 0; // bit address
397 bool m_enable = false; // capture enable
398
399 // FIFO
400 class fifo_data_t
401 {
402 public:
403 fifo_data_t()
404 : m_data(0)
405 {
406 }
407
408 void reset()
409 {
410 m_data = 0;
411 }
412
413 // accessors
414 void write_byte(const u8 bit, const u8 data)
415 {
416 u32 input = u32(data) << bit;
417 u32 mask = (0xff << bit);
418 m_data = (m_data & ~mask) | (input & mask);
419 }
420
421 void write_word(const u8 bit, const u16 data)
422 {
423 u32 input = u32(data) << bit;
424 u32 mask = (0xffff << bit);
425 m_data = (m_data & ~mask) | (input & mask);
426 }
427
428 // getters
429 u32 data() const { return m_data; }
430
431 private:
432 u32 m_data = 0;
433 };
434
435 fifo_data_t m_fifo[8]; // FIFO (8 word, for 16 sample delay)
436 u32 m_fifo_head = 0; // FIFO head
437 u32 m_fifo_tail = 0; // FIFO tail
438 bool m_fifo_empty = true; // FIFO empty flag
439 bool m_fifo_full = false; // FIFO full flag
440 };
441
442 nds_sound_intf &m_intf; // memory interface
443
444 channel_t m_channel[16]; // 16 channels
445 capture_t m_capture[2]; // 2 capture channels
446
447 inline u8 mvol() const { return bitfield(m_control, 0, 7); } // master volume
448 inline u8 lout_from() const { return bitfield(m_control, 8, 2); } // left output source (mixer, channel 1, channel 3, channel 1+3)
449 inline u8 rout_from() const { return bitfield(m_control, 10, 2); } // right output source (mixer, channel 1, channel 3, channel 1+3)
450 inline bool mix_ch1() const { return bitfield(m_control, 12); } // mix/bypass channel 1
451 inline bool mix_ch3() const { return bitfield(m_control, 13); } // mix/bypass channel 3
452 inline bool enable() const { return bitfield(m_control, 15); } // global enable
453
454 u32 m_control = 0; // global control
455 u32 m_bias = 0; // output bias
456 u32 m_lastts = 0; // running timestamp
457 };
458 }; // namespace nds_sound_emu
459
460 #endif // NDS_SOUND_EMU_H