Debian Codegraph @

android/app/jni/extern/vgsound_emu-modified/vgsound_emu/src/es550x/es550x.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 Ensoniq ES5504/ES5505/ES5506 emulation core
7 */
8
9 #ifndef _VGSOUND_EMU_SRC_ES550X_HPP
10 #define _VGSOUND_EMU_SRC_ES550X_HPP
11
12 #pragma once
13
14 #include "../core/core.hpp"
15 #include "../core/util/clock_pulse.hpp"
16
17 #include <string.h>
18
19 using namespace vgsound_emu;
20
21 // ES5504/ES5505/ES5506 interface
22 class es550x_intf : public vgsound_emu_core
23 {
24 public:
25 es550x_intf()
26 : vgsound_emu_core("es550x_intf")
27 {
28 }
29
30 virtual void e_pin(bool state) {} // E output
31
32 virtual void bclk(bool state) {} // BCLK output (serial specific)
33
34 virtual void lrclk(bool state) {} // LRCLK output (serial specific)
35
36 virtual void wclk(bool state) {} // WCLK output (serial specific)
37
38 virtual void irqb(bool state) {} // irqb output
39
40 virtual u16 adc_r() { return 0; } // ADC input
41
42 virtual void adc_w(u16 data) {} // ADC output
43
44 virtual s16 read_sample(u8 bank, u32 address) { return 0; }
45 };
46
47 // Shared functions for ES5504/ES5505/ES5506
48 class es550x_shared_core : public vgsound_emu_core
49 {
50 friend class es550x_intf; // es550x specific memory interface
51
52 private:
53 const u8 m_max_voices = 32;
54
55 protected:
56 // Interrupt bits
57 class es550x_irq_t : public vgsound_emu_core
58 {
59 public:
60 es550x_irq_t()
61 : vgsound_emu_core("es550x_irq")
62 , m_voice(0)
63 , m_irqb(1)
64 {
65 }
66
67 void reset()
68 {
69 m_voice = 0;
70 m_irqb = 1;
71 }
72
73 // setter
74 void set(u8 index)
75 {
76 m_irqb = 0;
77 m_voice = index & 0x1f;
78 }
79
80 void clear()
81 {
82 m_irqb = 1;
83 m_voice = 0;
84 }
85
86 // getter
87 inline bool irqb() { return m_irqb; }
88
89 inline u8 voice() { return m_voice; }
90
91 inline u8 get() { return (m_irqb << 7) | (m_voice & 0x1f); }
92
93 u8 m_voice : 5;
94 u8 m_irqb : 1;
95 };
96
97 // Common voice class
98 class es550x_voice_t : public vgsound_emu_core
99 {
100 private:
101 // Common control bits
102 class es550x_control_t : public vgsound_emu_core
103 {
104 public:
105 es550x_control_t()
106 : vgsound_emu_core("es550x_voice_control")
107 , m_ca(0)
108 , m_adc(0)
109 , m_bs(0)
110 , m_cmpd(0)
111 {
112 }
113
114 void reset()
115 {
116 m_ca = 0;
117 m_adc = 0;
118 m_bs = 0;
119 m_cmpd = 0;
120 }
121
122 // setters
123 inline void set_ca(u8 ca) { m_ca = ca & 0xf; }
124
125 inline void set_adc(bool adc) { m_adc = adc ? 1 : 0; }
126
127 inline void set_bs(u8 bs) { m_bs = bs & 0x3; }
128
129 inline void set_cmpd(bool cmpd) { m_cmpd = cmpd ? 1 : 0; }
130
131 // getters
132 inline u8 ca() { return m_ca; }
133
134 inline bool adc() { return m_adc; }
135
136 inline u8 bs() { return m_bs; }
137
138 inline bool cmpd() { return m_cmpd; }
139
140 public:
141 // Channel assign -
142 // 4 bit (16 channel or Bank) for ES5504
143 // 2 bit (4 stereo channels) for ES5505
144 // 3 bit (6 stereo channels) for ES5506
145 u8 m_ca : 4;
146
147 // ES5504 Specific
148 u8 m_adc : 1; // Start ADC
149
150 // ES5505/ES5506 Specific
151 u8 m_bs : 2; // Bank bit (1 bit for ES5505, 2 bit for ES5506)
152 u8 m_cmpd : 1; // Use compressed sample format (ES5506)
153 };
154
155 // Accumulator
156 class es550x_alu_t : public vgsound_emu_core
157 {
158 public:
159 es550x_alu_t(u8 integer, u8 fraction, bool transwave)
160 : vgsound_emu_core("es550x_voice_alu")
161 , m_integer(integer)
162 , m_fraction(fraction)
163 , m_fraction_m9(std::max<s8>(0, m_fraction - 9))
164 , m_total_bits(integer + fraction)
165 , m_accum_mask(
166 u32(std::min<u64>(~0, u64(u64(1) << u64(integer + fraction)) - 1)))
167 , m_transwave(transwave)
168 , m_fc(0)
169 , m_start(0)
170 , m_end(0)
171 , m_accum(0)
172 , m_last_accum(0)
173 , m_sample{0,0}
174 {
175 }
176
177 // configurations
178 const u8 m_integer = 21;
179 const u8 m_fraction = 11;
180 const u8 m_fraction_m9 = 2;
181 const u8 m_total_bits = 32;
182 const u32 m_accum_mask = 0xffffffff;
183 const bool m_transwave = true;
184
185 // internal states
186 void reset();
187 bool tick();
188
189 void loop_exec();
190 bool busy();
191 s32 interpolation();
192 u32 get_accum_integer();
193
194 void irq_exec(es550x_intf &intf, es550x_irq_t &irqv, u8 index);
195
196 void irq_update(es550x_intf &intf, es550x_irq_t &irqv)
197 {
198 intf.irqb(irqv.irqb() ? false : true);
199 }
200
201 // setters
202 inline void set_stop0(bool stop0) { m_cr.set_stop0(stop0); }
203
204 inline void set_stop1(bool stop1) { m_cr.set_stop1(stop1); }
205
206 inline void set_lpe(bool lpe) { m_cr.set_lpe(lpe); }
207
208 inline void set_ble(bool ble) { m_cr.set_ble(ble); }
209
210 inline void set_irqe(bool irqe) { m_cr.set_irqe(irqe); }
211
212 inline void set_dir(bool dir) { m_cr.set_dir(dir); }
213
214 inline void set_irq(bool irq) { m_cr.set_irq(irq); }
215
216 inline void set_lei(bool lei) { m_cr.set_lei(lei); }
217
218 inline void set_stop(u8 stop) { m_cr.set_stop(stop); }
219
220 inline void set_loop(u8 loop) { m_cr.set_loop(loop); }
221
222 inline void set_fc(u32 fc) { m_fc = fc; }
223
224 inline void set_start(u32 start, u32 mask = ~0)
225 {
226 m_start = (m_start & ~mask) | (start & mask);
227 }
228
229 inline void set_end(u32 end, u32 mask = ~0)
230 {
231 m_end = (m_end & ~mask) | (end & mask);
232 }
233
234 inline void set_accum(u32 accum, u32 mask = ~0)
235 {
236 m_accum = (m_accum & ~mask) | (accum & mask);
237 }
238
239 inline void set_sample(u8 slot, s32 sample) { m_sample[slot & 1] = sample; }
240
241 // getters
242 inline bool stop0() { return m_cr.stop0(); }
243
244 inline bool stop1() { return m_cr.stop1(); }
245
246 inline bool lpe() { return m_cr.lpe(); }
247
248 inline bool ble() { return m_cr.ble(); }
249
250 inline bool irqe() { return m_cr.irqe(); }
251
252 inline bool dir() { return m_cr.dir(); }
253
254 inline bool irq() { return m_cr.irq(); }
255
256 inline bool lei() { return m_cr.lei(); }
257
258 inline u8 stop() { return m_cr.stop(); }
259
260 inline u8 loop() { return m_cr.loop(); }
261
262 inline u32 fc() { return m_fc; }
263
264 inline u32 start() { return m_start; }
265
266 inline u32 end() { return m_end; }
267
268 inline u32 accum() { return m_accum; }
269
270 inline s32 sample(u8 slot) { return m_sample[slot & 1]; }
271
272 public:
273 class es550x_alu_cr_t : public vgsound_emu_core
274 {
275 public:
276 es550x_alu_cr_t()
277 : vgsound_emu_core("es550x_voice_alu_cr")
278 , m_stop0(0)
279 , m_stop1(0)
280 , m_lpe(0)
281 , m_ble(0)
282 , m_irqe(0)
283 , m_dir(0)
284 , m_irq(0)
285 , m_lei(0)
286 {
287 }
288
289 void reset()
290 {
291 m_stop0 = 0;
292 m_stop1 = 0;
293 m_lpe = 0;
294 m_ble = 0;
295 m_irqe = 0;
296 m_dir = 0;
297 m_irq = 0;
298 m_lei = 0;
299 }
300
301 // setters
302 inline void set_stop0(bool stop0) { m_stop0 = stop0 ? 1 : 0; }
303
304 inline void set_stop1(bool stop1) { m_stop1 = stop1 ? 1 : 0; }
305
306 inline void set_lpe(bool lpe) { m_lpe = lpe ? 1 : 0; }
307
308 inline void set_ble(bool ble) { m_ble = ble ? 1 : 0; }
309
310 inline void set_irqe(bool irqe) { m_irqe = irqe ? 1 : 0; }
311
312 inline void set_dir(bool dir) { m_dir = dir ? 1 : 0; }
313
314 inline void set_irq(bool irq) { m_irq = irq ? 1 : 0; }
315
316 inline void set_lei(bool lei) { m_lei = lei ? 1 : 0; }
317
318 inline void set_stop(u8 stop)
319 {
320 m_stop0 = (stop >> 0) & 1;
321 m_stop1 = (stop >> 1) & 1;
322 }
323
324 inline void set_loop(u8 loop)
325 {
326 m_lpe = (loop >> 0) & 1;
327 m_ble = (loop >> 1) & 1;
328 }
329
330 // getters
331 inline bool stop0() { return m_stop0; }
332
333 inline bool stop1() { return m_stop1; }
334
335 inline bool lpe() { return m_lpe; }
336
337 inline bool ble() { return m_ble; }
338
339 inline bool irqe() { return m_irqe; }
340
341 inline bool dir() { return m_dir; }
342
343 inline bool irq() { return m_irq; }
344
345 inline bool lei() { return m_lei; }
346
347 inline u8 stop() { return (m_stop0 << 0) | (m_stop1 << 1); }
348
349 inline u8 loop() { return (m_lpe << 0) | (m_ble << 1); }
350
351 public:
352 u8 m_stop0 : 1; // Stop with ALU
353 u8 m_stop1 : 1; // Stop with processor
354 u8 m_lpe : 1; // Loop enable
355 u8 m_ble : 1; // Bidirectional loop enable
356 u8 m_irqe : 1; // IRQ enable
357 u8 m_dir : 1; // Playback direction
358 u8 m_irq : 1; // IRQ bit
359 u8 m_lei : 1; // Loop end ignore (ES5506 specific)
360 };
361
362 es550x_alu_cr_t m_cr;
363 // Frequency -
364 // 6 integer, 9 fraction for ES5504/ES5505
365 // 6 integer, 11 fraction for ES5506
366 u32 m_fc = 0;
367 u32 m_start = 0; // Start register
368 u32 m_end = 0; // End register
369
370 // Accumulator -
371 // 20 integer, 9 fraction for ES5504/ES5505
372 // 21 integer, 11 fraction for ES5506
373 u32 m_accum = 0;
374 u32 m_last_accum = 0;
375 // Samples
376 s32 m_sample[2];
377 };
378
379 // Filter
380 class es550x_filter_t : public vgsound_emu_core
381 {
382 public:
383 es550x_filter_t()
384 : vgsound_emu_core("es550x_voice_filter")
385 , m_lp(0)
386 , m_k2(0)
387 , m_k1(0)
388 {
389 memset(m_o,0,2*5*sizeof(s32));
390 }
391
392 void reset();
393 void tick(s32 in);
394
395 // setters
396 inline void set_lp(u8 lp) { m_lp = lp & 3; }
397
398 inline void set_k2(s32 k2) { m_k2 = k2; }
399
400 inline void set_k1(s32 k1) { m_k1 = k1; }
401
402 inline void set_o1_1(s32 o1_1) { m_o[1][0] = o1_1; }
403
404 inline void set_o2_1(s32 o2_1) { m_o[2][0] = o2_1; }
405
406 inline void set_o2_2(s32 o2_2) { m_o[2][1] = o2_2; }
407
408 inline void set_o3_1(s32 o3_1) { m_o[3][0] = o3_1; }
409
410 inline void set_o3_2(s32 o3_2) { m_o[3][1] = o3_2; }
411
412 inline void set_o4_1(s32 o4_1) { m_o[4][0] = o4_1; }
413
414 // getters
415 inline u8 lp() { return m_lp; }
416
417 inline s32 k2() { return m_k2; }
418
419 inline s32 k1() { return m_k1; }
420
421 inline s32 o1_1() { return m_o[1][0]; }
422
423 inline s32 o2_1() { return m_o[2][0]; }
424
425 inline s32 o2_2() { return m_o[2][1]; }
426
427 inline s32 o3_1() { return m_o[3][0]; }
428
429 inline s32 o3_2() { return m_o[3][1]; }
430
431 inline s32 o4_1() { return m_o[4][0]; }
432
433 private:
434
435 // Registers
436 u8 m_lp = 0; // Filter mode
437 // Filter coefficient registers
438 // 12 bit for filter calculation, 4
439 // LSBs are used for fine control of ramp increment for
440 // hardware envelope (ES5506)
441 s32 m_k2 = 0; // Filter coefficient 2
442 s32 m_k1 = 0; // Filter coefficient 1
443
444 // Filter storage registers
445 s32 m_o[5][2];
446 };
447
448 public:
449 es550x_voice_t(std::string tag, u8 integer, u8 fraction, bool transwave)
450 : vgsound_emu_core(tag)
451 , m_cr(es550x_control_t())
452 , m_alu(integer, fraction, transwave)
453 , m_filter(es550x_filter_t())
454 {
455 }
456
457 // internal state
458 virtual void reset();
459 virtual void fetch(u8 cycle) = 0;
460 virtual void tick(u8 voice) = 0;
461
462 void irq_update(es550x_intf &intf, es550x_irq_t &irqv)
463 {
464 m_alu.irq_update(intf, irqv);
465 }
466
467 // Getters
468 es550x_control_t &cr() { return m_cr; }
469
470 es550x_alu_t &alu() { return m_alu; }
471
472 es550x_filter_t &filter() { return m_filter; }
473
474 protected:
475 es550x_control_t m_cr;
476 es550x_alu_t m_alu;
477 es550x_filter_t m_filter;
478 };
479
480 // Host interfaces
481 class host_interface_flag_t : public vgsound_emu_core
482 {
483 public:
484 host_interface_flag_t()
485 : vgsound_emu_core("es550x_host_interface_flag")
486 , m_host_access(0)
487 , m_host_access_strobe(0)
488 , m_rw(0)
489 , m_rw_strobe(0)
490 {
491 }
492
493 // Accessors
494 void reset()
495 {
496 m_host_access = 0;
497 m_host_access_strobe = 0;
498 m_rw = 0;
499 m_rw_strobe = 0;
500 }
501
502 // Setters
503 void set_strobe(bool rw)
504 {
505 m_rw_strobe = rw ? 1 : 0;
506 m_host_access_strobe = 1;
507 }
508
509 void clear_strobe() { m_host_access_strobe = 0; }
510
511 void clear_host_access() { m_host_access = 0; }
512
513 void update_strobe()
514 {
515 m_rw = m_rw_strobe;
516 m_host_access = m_host_access_strobe;
517 }
518
519
520 public:
521 u8 m_host_access ; // Host access trigger
522 u8 m_host_access_strobe ; // Host access strobe
523 u8 m_rw ; // R/W state
524 u8 m_rw_strobe ; // R/W strobe
525 };
526
527 public:
528 // internal state
529 virtual void reset();
530
531 virtual void tick() {}
532
533 // clock outputs
534 inline bool _cas() { return m_cas.current_edge(); }
535
536 inline bool _cas_rising_edge() { return m_cas.rising_edge(); }
537
538 inline bool _cas_falling_edge() { return m_cas.falling_edge(); }
539
540 inline bool e() { return m_e.current_edge(); }
541
542 inline bool e_rising_edge() { return m_e.rising_edge(); }
543
544 inline bool e_falling_edge() { return m_e.falling_edge(); }
545
546 //-----------------------------------------------------------------
547 //
548 // for preview/debug purpose only, not for serious emulators
549 //
550 //-----------------------------------------------------------------
551
552 // voice cycle
553 inline u8 voice_cycle() { return m_voice_cycle; }
554
555 // voice update flag
556 inline bool voice_update() { return m_voice_update; }
557
558 inline bool voice_end() { return m_voice_end; }
559
560 protected:
561 // constructor
562 es550x_shared_core(std::string tag, const u8 voice, es550x_intf &intf)
563 : vgsound_emu_core(tag)
564 , m_max_voices(voice)
565 , m_intf(intf)
566 , m_host_intf(host_interface_flag_t())
567 , m_ha(0)
568 , m_hd(0)
569 , m_page(0)
570 , m_irqv(es550x_irq_t())
571 , m_active(m_max_voices - 1)
572 , m_voice_cycle(0)
573 , m_voice_fetch(0)
574 , m_voice_update(0)
575 , m_voice_end(0)
576 , m_clkin(clock_pulse_t<s8>(1, 0))
577 , m_cas(clock_pulse_t<s8>(2, 1))
578 , m_e(clock_pulse_t<s8>(4, 0))
579 {
580 }
581
582 // Constants
583 virtual inline u8 max_voices() { return m_max_voices; }
584
585 // Shared registers, functions
586 virtual void voice_tick() {} // voice tick
587
588 es550x_intf &m_intf; // es550x specific memory interface
589 host_interface_flag_t m_host_intf; // Host interface flag
590 u8 m_ha = 0; // Host address (4 bit)
591 u16 m_hd = 0; // Host data (16 bit for ES5504/ES5505, 8 bit for ES5506)
592 u8 m_page = 0; // Page
593 es550x_irq_t m_irqv; // Voice interrupt vector registers
594
595 // Internal states
596 u8 m_active = 31; // Activated voices
597 // -1, ~25 for ES5504,
598 // ~32 for ES5505/ES5506
599 u8 m_voice_cycle = 0; // Voice cycle
600 u8 m_voice_fetch = 0; // Voice fetch cycle
601 bool m_voice_update = false; // Voice update flag
602 bool m_voice_end = false; // End of one voice cycle flag
603 clock_pulse_t<s8> m_clkin; // CLKIN clock
604 clock_pulse_t<s8> m_cas; // /CAS clock (CLKIN / 4), falling edge of
605 // CLKIN trigger this clock
606 clock_pulse_t<s8> m_e; // E clock (CLKIN / 8),
607 // falling edge of CLKIN trigger this clock
608 };
609
610 #endif