-
-
Notifications
You must be signed in to change notification settings - Fork 315
/
snd_emu8k.c
2379 lines (2122 loc) · 107 KB
/
snd_emu8k.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <inttypes.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#define _USE_MATH_DEFINES
#include <math.h>
#define HAVE_STDARG_H
#include <86box/86box.h>
#include <86box/device.h>
#include <86box/io.h>
#include <86box/mem.h>
#include <86box/rom.h>
#include <86box/sound.h>
#include <86box/snd_emu8k.h>
#include <86box/timer.h>
#include <86box/plat_unused.h>
#if !defined FILTER_INITIAL && !defined FILTER_MOOG && !defined FILTER_CONSTANT
#if 0
#define FILTER_INITIAL
#endif
# define FILTER_MOOG
#if 0
#define FILTER_CONSTANT
#endif
#endif
#if !defined RESAMPLER_LINEAR && !defined RESAMPLER_CUBIC
#if 0
#define RESAMPLER_LINEAR
#endif
# define RESAMPLER_CUBIC
#endif
#if 0
#define EMU8K_DEBUG_REGISTERS
#endif
char *PORT_NAMES[][8] = {
/* Data 0 ( 0x620/0x622) */
{
"AWE_CPF",
"AWE_PTRX",
"AWE_CVCF",
"AWE_VTFT",
"Unk-620-4",
"Unk-620-5",
"AWE_PSST",
"AWE_CSL",
},
/* Data 1 0xA20 */
{
"AWE_CCCA",
0,
/*
"AWE_HWCF4"
"AWE_HWCF5"
"AWE_HWCF6"
"AWE_HWCF7"
"AWE_SMALR"
"AWE_SMARR"
"AWE_SMALW"
"AWE_SMARW"
"AWE_SMLD"
"AWE_SMRD"
"AWE_WC"
"AWE_HWCF1"
"AWE_HWCF2"
"AWE_HWCF3"
*/
0, //"AWE_INIT1",
0, //"AWE_INIT3",
"AWE_ENVVOL",
"AWE_DCYSUSV",
"AWE_ENVVAL",
"AWE_DCYSUS",
},
/* Data 2 0xA22 */
{
"AWE_CCCA",
0,
0, //"AWE_INIT2",
0, //"AWE_INIT4",
"AWE_ATKHLDV",
"AWE_LFO1VAL",
"AWE_ATKHLD",
"AWE_LFO2VAL",
},
/* Data 3 0xE20 */
{
"AWE_IP",
"AWE_IFATN",
"AWE_PEFE",
"AWE_FMMOD",
"AWE_TREMFRQ",
"AWE_FM2FRQ2",
0,
0,
},
};
enum {
ENV_STOPPED = 0,
ENV_DELAY = 1,
ENV_ATTACK = 2,
ENV_HOLD = 3,
// ENV_DECAY = 4,
ENV_SUSTAIN = 5,
// ENV_RELEASE = 6,
ENV_RAMP_DOWN = 7,
ENV_RAMP_UP = 8
};
static int random_helper = 0;
int dmareadbit = 0;
int dmawritebit = 0;
/* cubic and linear tables resolution. Note: higher than 10 does not improve the result. */
#define CUBIC_RESOLUTION_LOG 10
#define CUBIC_RESOLUTION (1 << CUBIC_RESOLUTION_LOG)
/* cubic_table coefficients. */
static float cubic_table[CUBIC_RESOLUTION * 4];
/* conversion from current pitch to linear frequency change (in 32.32 fixed point). */
static int64_t freqtable[65536];
/* Conversion from initial attenuation to 16 bit unsigned lineal amplitude (currently only a way to update volume target register) */
static int32_t attentable[256];
/* Conversion from envelope dbs (once rigth shifted) (0 = 0dBFS, 65535 = -96dbFS and silence ) to 16 bit unsigned lineal amplitude,
* to convert to current volume. (0 to 65536) */
static int32_t env_vol_db_to_vol_target[65537];
/* Same as above, but to convert amplitude (once rigth shifted) (0 to 65536) to db (0 = 0dBFS, 65535 = -96dbFS and silence ).
* it is needed so that the delay, attack and hold phase can be added to initial attenuation and tremolo */
static int32_t env_vol_amplitude_to_db[65537];
/* Conversion from envelope herts (once right shifted) to octave . it is needed so that the delay, attack and hold phase can be
* added to initial pitch ,lfos pitch , initial filter and lfo filter */
static int32_t env_mod_hertz_to_octave[65537];
/* Conversion from envelope amount to time in samples. */
static int32_t env_attack_to_samples[128];
/* This table has been generated using the following formula:
* Get the amount of dBs that have to be added each sample to reach 96dBs in the amount
* of time determined by the encoded value "i".
* float d = 1.0/((env_decay_to_millis[i]/96.0)*44.1);
* int result = round(d*21845);
* The multiplication by 21845 gives a minimum value of 1, and a maximum accumulated value of 1<<21
* The accumulated value has to be converted to amplitude, and that can be done with the
* env_vol_db_to_vol_target and shifting by 8
* In other words, the unit of the table is the 1/21845th of a dB per sample frame, to be added or
* substracted to the accumulating value_db of the envelope. */
static int32_t env_decay_to_dbs_or_oct[128] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 20, 21, 22, 23, 24, 25, 27, 28, 29, 30, 32,
33, 34, 36, 38, 39, 41, 43, 45, 49, 51, 53, 55, 58, 60, 63, 66,
69, 72, 75, 78, 82, 85, 89, 93, 97, 102, 106, 111, 116, 121, 126, 132,
138, 144, 150, 157, 164, 171, 179, 186, 195, 203, 212, 222, 232, 243, 253, 264,
276, 288, 301, 315, 328, 342, 358, 374, 390, 406, 425, 444, 466, 485, 506, 528,
553, 580, 602, 634, 660, 689, 721, 755, 780, 820, 849, 897, 932, 970, 1012, 1057,
1106, 1160, 1219, 1285, 1321, 1399, 1441, 1534, 1585, 1640, 1698, 1829, 1902, 1981, 2068, 2162
};
/* The table "env_decay_to_millis" is based on the table "decay_time_tbl" found in the freebsd/linux
* AWE32 driver.
* I tried calculating it using the instructions in awe32p10 from Judge Dredd, but the formula there
* is wrong.
*
*/
#if 0
static int32_t env_decay_to_millis[128] = {
0, 45120, 22614, 15990, 11307, 9508, 7995, 6723, 5653, 5184, 4754, 4359, 3997, 3665, 3361, 3082,
2828, 2765, 2648, 2535, 2428, 2325, 2226, 2132, 2042, 1955, 1872, 1793, 1717, 1644, 1574, 1507,
1443, 1382, 1324, 1267, 1214, 1162, 1113, 1066, 978, 936, 897, 859, 822, 787, 754, 722,
691, 662, 634, 607, 581, 557, 533, 510, 489, 468, 448, 429, 411, 393, 377, 361,
345, 331, 317, 303, 290, 278, 266, 255, 244, 234, 224, 214, 205, 196, 188, 180,
172, 165, 158, 151, 145, 139, 133, 127, 122, 117, 112, 107, 102, 98, 94, 90,
86, 82, 79, 75, 72, 69, 66, 63, 61, 58, 56, 53, 51, 49, 47, 45,
43, 41, 39, 37, 36, 34, 33, 31, 30, 29, 28, 26, 25, 24, 23, 22,
};
#endif
/* Table represeting the LFO waveform (signed 16bits with 32768 max int. >> 15 to move back to +/-1 range). */
static int32_t lfotable[65536];
/* Table to transform the speed parameter to emu8k_mem_internal_t range. */
static int64_t lfofreqtospeed[256];
/* LFO used for the chorus. a sine wave.(signed 16bits with 32768 max int. >> 15 to move back to +/-1 range). */
static double chortable[65536];
static const int REV_BUFSIZE_STEP = 242;
/* These lines come from the awe32faq, describing the NRPN control for the initial filter
* where it describes a linear increment filter instead of an octave-incremented one.
* NRPN LSB 21 (Initial Filter Cutoff)
* Range : [0, 127]
* Unit : 62Hz
* Filter cutoff from 100Hz to 8000Hz
* This table comes from the awe32faq, describing the NRPN control for the filter Q.
* I don't know if is meant to be interpreted as the actual measured output of the
* filter or what. Especially, I don't understand the "low" and "high" ranges.
* What is otherwise documented is that the Q ranges from 0dB to 24dB and the attenuation
* is half of the Q ( i.e. for 12dB Q, attenuate the input signal with -6dB)
Coeff Low Fc(Hz)Low Q(dB)High Fc(kHz)High Q(dB)DC Attenuation(dB)
* 0 92 5 Flat Flat -0.0
* 1 93 6 8.5 0.5 -0.5
* 2 94 8 8.3 1 -1.2
* 3 95 10 8.2 2 -1.8
* 4 96 11 8.1 3 -2.5
* 5 97 13 8.0 4 -3.3
* 6 98 14 7.9 5 -4.1
* 7 99 16 7.8 6 -5.5
* 8 100 17 7.7 7 -6.0
* 9 100 19 7.5 9 -6.6
* 10 100 20 7.4 10 -7.2
* 11 100 22 7.3 11 -7.9
* 12 100 23 7.2 13 -8.5
* 13 100 25 7.1 15 -9.3
* 14 100 26 7.1 16 -10.1
* 15 100 28 7.0 18 -11.0
*
* Attenuation as above, codified in amplitude.*/
static int32_t filter_atten[16] = {
65536, 61869, 57079, 53269, 49145, 44820, 40877, 34792, 32845, 30653, 28607,
26392, 24630, 22463, 20487, 18470
};
/*Coefficients for the filters for a defined Q and cutoff.*/
static int32_t filt_coeffs[16][256][3];
#define READ16_SWITCH(addr, var) \
switch ((addr) &2) { \
case 0: \
ret = (var) &0xffff; \
break; \
case 2: \
ret = ((var) >> 16) & 0xffff; \
break; \
}
#define WRITE16_SWITCH(addr, var, val) \
switch ((addr) &2) { \
case 0: \
var = (var & 0xffff0000) | (val); \
break; \
case 2: \
var = (var & 0x0000ffff) | ((val) << 16); \
break; \
}
#ifdef EMU8K_DEBUG_REGISTERS
uint32_t dw_value = 0;
uint32_t last_read = 0;
uint32_t last_write = 0;
uint32_t rep_count_r = 0;
uint32_t rep_count_w = 0;
# define READ16(addr, var) \
READ16_SWITCH(addr, var) \
{ \
const char *name = 0; \
switch (addr & 0xF02) { \
case 0x600: \
case 0x602: \
name = PORT_NAMES[0][emu8k->cur_reg]; \
break; \
case 0xA00: \
name = PORT_NAMES[1][emu8k->cur_reg]; \
break; \
case 0xA02: \
name = PORT_NAMES[2][emu8k->cur_reg]; \
break; \
} \
if (name == 0) { \
/*emu8k_log("EMU8K READ %04X-%02X(%d): %04X\n",addr,(emu8k->cur_reg)<<5|emu8k->cur_voice, emu8k->cur_voice,ret);*/ \
} else { \
emu8k_log("EMU8K READ %s(%d) (%d): %04X\n", name, (addr & 0x2), emu8k->cur_voice, ret); \
} \
}
# define WRITE16(addr, var, val) \
WRITE16_SWITCH(addr, var, val) \
{ \
const char *name = 0; \
switch (addr & 0xF02) { \
case 0x600: \
case 0x602: \
name = PORT_NAMES[0][emu8k->cur_reg]; \
break; \
case 0xA00: \
name = PORT_NAMES[1][emu8k->cur_reg]; \
break; \
case 0xA02: \
name = PORT_NAMES[2][emu8k->cur_reg]; \
break; \
} \
if (name == 0) { \
/*emu8k_log("EMU8K WRITE %04X-%02X(%d): %04X\n",addr,(emu8k->cur_reg)<<5|emu8k->cur_voice,emu8k->cur_voice, val);*/ \
} else { \
emu8k_log("EMU8K WRITE %s(%d) (%d): %04X\n", name, (addr & 0x2), emu8k->cur_voice, val); \
} \
}
#else
# define READ16(addr, var) READ16_SWITCH(addr, var)
# define WRITE16(addr, var, val) WRITE16_SWITCH(addr, var, val)
#endif // EMU8K_DEBUG_REGISTERS
#ifdef ENABLE_EMU8K_LOG
int emu8k_do_log = ENABLE_EMU8K_LOG;
static void
emu8k_log(const char *fmt, ...)
{
va_list ap;
if (emu8k_do_log) {
va_start(ap, fmt);
pclog_ex(fmt, ap);
va_end(ap);
}
}
#else
# define emu8k_log(fmt, ...)
#endif
static inline int16_t
EMU8K_READ(emu8k_t *emu8k, uint32_t addr)
{
const register emu8k_mem_pointers_t addrmem = { { addr } };
return emu8k->ram_pointers[addrmem.hb_address][addrmem.lw_address];
}
#if NOTUSED
static inline int16_t
EMU8K_READ_INTERP_LINEAR(emu8k_t *emu8k, uint32_t int_addr, uint16_t fract)
{
/* The interpolation in AWE32 used a so-called patented 3-point interpolation
* ( I guess some sort of spline having one point before and one point after).
* Also, it has the consequence that the playback is delayed by one sample.
* I simulate the "one sample later" than the address with addr+1 and addr+2
* instead of +0 and +1 */
int16_t dat1 = EMU8K_READ(emu8k, int_addr + 1);
int32_t dat2 = EMU8K_READ(emu8k, int_addr + 2);
dat1 += ((dat2 - (int32_t) dat1) * fract) >> 16;
return dat1;
}
#endif
static inline int32_t
EMU8K_READ_INTERP_CUBIC(emu8k_t *emu8k, uint32_t int_addr, uint16_t fract)
{
/*Since there are four floats in the table for each fraction, the position is 16byte aligned. */
fract >>= 16 - CUBIC_RESOLUTION_LOG;
fract <<= 2;
/* TODO: I still have to verify how this works, but I think that
* the card could use two oscillators (usually 31 and 32) where it would
* be writing the OPL3 output, and to which, chorus and reverb could be applied to get
* those effects for OPL3 sounds.*/
#if 0
if ((addr & EMU8K_FM_MEM_ADDRESS) == EMU8K_FM_MEM_ADDRESS) {}
#endif
/* This is cubic interpolation.
* Not the same than 3-point interpolation, but a better approximation than linear
* interpolation.
* Also, it takes into account the "Note that the actual audio location is the point
* 1 word higher than this value due to interpolation offset".
* That's why the pointers are 0, 1, 2, 3 and not -1, 0, 1, 2 */
int32_t dat2 = EMU8K_READ(emu8k, int_addr + 1);
const float *table = &cubic_table[fract];
const int32_t dat1 = EMU8K_READ(emu8k, int_addr);
const int32_t dat3 = EMU8K_READ(emu8k, int_addr + 2);
const int32_t dat4 = EMU8K_READ(emu8k, int_addr + 3);
/* Note: I've ended using float for the table values to avoid some cases of integer overflow. */
dat2 = dat1 * table[0] + dat2 * table[1] + dat3 * table[2] + dat4 * table[3];
return dat2;
}
static inline void
EMU8K_WRITE(emu8k_t *emu8k, uint32_t addr, uint16_t val)
{
addr &= EMU8K_MEM_ADDRESS_MASK;
if (!emu8k->ram || addr < EMU8K_RAM_MEM_START || addr >= EMU8K_FM_MEM_ADDRESS)
return;
/* It looks like if an application writes to a memory part outside of the available
* amount on the card, it wraps, and opencubicplayer uses that to detect the amount
* of memory, as opposed to simply check at the address that it has just tried to write. */
while (addr >= emu8k->ram_end_addr)
addr -= emu8k->ram_end_addr - EMU8K_RAM_MEM_START;
emu8k->ram[addr - EMU8K_RAM_MEM_START] = val;
}
uint16_t
emu8k_inw(uint16_t addr, void *priv)
{
emu8k_t *emu8k = (emu8k_t *) priv;
uint16_t ret = 0xffff;
#ifdef EMU8K_DEBUG_REGISTERS
if (addr == 0xE22) {
emu8k_log("EMU8K READ POINTER: %d\n",
((0x80 | ((random_helper + 1) & 0x1F)) << 8) | (emu8k->cur_reg << 5) | emu8k->cur_voice);
} else if ((addr & 0xF00) == 0x600) {
/* These are automatically reported by READ16 */
if (rep_count_r > 1) {
emu8k_log("EMU8K ...... for %d times\n", rep_count_r);
rep_count_r = 0;
}
last_read = 0;
} else if ((addr & 0xF00) == 0xA00 && emu8k->cur_reg == 0) {
/* These are automatically reported by READ16 */
if (rep_count_r > 1) {
emu8k_log("EMU8K ...... for %d times\n", rep_count_r);
rep_count_r = 0;
}
last_read = 0;
} else if ((addr & 0xF00) == 0xA00 && emu8k->cur_reg == 1) {
uint32_t tmpz = ((addr & 0xF00) << 16) | (emu8k->cur_reg << 5);
if (tmpz != last_read) {
if (rep_count_r > 1) {
emu8k_log("EMU8K ...... for %d times\n", rep_count_r);
rep_count_r = 0;
}
last_read = tmpz;
emu8k_log("EMU8K READ RAM I/O or configuration or clock \n");
}
// emu8k_log("EMU8K READ %04X-%02X(%d/%d)\n",addr,(emu8k->cur_reg)<<5|emu8k->cur_voice, emu8k->cur_reg, emu8k->cur_voice);
} else if ((addr & 0xF00) == 0xA00 && (emu8k->cur_reg == 2 || emu8k->cur_reg == 3)) {
uint32_t tmpz = ((addr & 0xF00) << 16);
if (tmpz != last_read) {
if (rep_count_r > 1) {
emu8k_log("EMU8K ...... for %d times\n", rep_count_r);
rep_count_r = 0;
}
last_read = tmpz;
emu8k_log("EMU8K READ INIT \n");
}
// emu8k_log("EMU8K READ %04X-%02X(%d/%d)\n",addr,(emu8k->cur_reg)<<5|emu8k->cur_voice, emu8k->cur_reg, emu8k->cur_voice);
} else {
uint32_t tmpz = (addr << 16) | (emu8k->cur_reg << 5) | emu8k->cur_voice;
if (tmpz != last_read) {
char *name = 0;
uint16_t val = 0xBAAD;
if (addr == 0xA20) {
name = PORT_NAMES[1][emu8k->cur_reg];
switch (emu8k->cur_reg) {
case 2:
val = emu8k->init1[emu8k->cur_voice];
break;
case 3:
val = emu8k->init3[emu8k->cur_voice];
break;
case 4:
val = emu8k->voice[emu8k->cur_voice].envvol;
break;
case 5:
val = emu8k->voice[emu8k->cur_voice].dcysusv;
break;
case 6:
val = emu8k->voice[emu8k->cur_voice].envval;
break;
case 7:
val = emu8k->voice[emu8k->cur_voice].dcysus;
break;
}
} else if (addr == 0xA22) {
name = PORT_NAMES[2][emu8k->cur_reg];
switch (emu8k->cur_reg) {
case 2:
val = emu8k->init2[emu8k->cur_voice];
break;
case 3:
val = emu8k->init4[emu8k->cur_voice];
break;
case 4:
val = emu8k->voice[emu8k->cur_voice].atkhldv;
break;
case 5:
val = emu8k->voice[emu8k->cur_voice].lfo1val;
break;
case 6:
val = emu8k->voice[emu8k->cur_voice].atkhld;
break;
case 7:
val = emu8k->voice[emu8k->cur_voice].lfo2val;
break;
}
} else if (addr == 0xE20) {
name = PORT_NAMES[3][emu8k->cur_reg];
switch (emu8k->cur_reg) {
case 0:
val = emu8k->voice[emu8k->cur_voice].ip;
break;
case 1:
val = emu8k->voice[emu8k->cur_voice].ifatn;
break;
case 2:
val = emu8k->voice[emu8k->cur_voice].pefe;
break;
case 3:
val = emu8k->voice[emu8k->cur_voice].fmmod;
break;
case 4:
val = emu8k->voice[emu8k->cur_voice].tremfrq;
break;
case 5:
val = emu8k->voice[emu8k->cur_voice].fm2frq2;
break;
case 6:
val = 0xffff;
break;
case 7:
val = 0x1c | ((emu8k->id & 0x0002) ? 0xff02 : 0);
break;
}
}
if (rep_count_r > 1) {
emu8k_log("EMU8K ...... for %d times\n", rep_count_r);
}
if (name == 0) {
emu8k_log("EMU8K READ %04X-%02X(%d/%d): %04X\n", addr, (emu8k->cur_reg) << 5 | emu8k->cur_voice, emu8k->cur_reg, emu8k->cur_voice, val);
} else {
emu8k_log("EMU8K READ %s (%d): %04X\n", name, emu8k->cur_voice, val);
}
rep_count_r = 0;
last_read = tmpz;
}
rep_count_r++;
}
#endif // EMU8K_DEBUG_REGISTERS
switch (addr & 0xF02) {
case 0x600:
case 0x602: /*Data0. also known as BLASTER+0x400 and EMU+0x000 */
switch (emu8k->cur_reg) {
case 0:
READ16(addr, emu8k->voice[emu8k->cur_voice].cpf);
return ret;
case 1:
READ16(addr, emu8k->voice[emu8k->cur_voice].ptrx);
return ret;
case 2:
READ16(addr, emu8k->voice[emu8k->cur_voice].cvcf);
return ret;
case 3:
READ16(addr, emu8k->voice[emu8k->cur_voice].vtft);
return ret;
case 4:
READ16(addr, emu8k->voice[emu8k->cur_voice].unknown_data0_4);
return ret;
case 5:
READ16(addr, emu8k->voice[emu8k->cur_voice].unknown_data0_5);
return ret;
case 6:
READ16(addr, emu8k->voice[emu8k->cur_voice].psst);
return ret;
case 7:
READ16(addr, emu8k->voice[emu8k->cur_voice].csl);
return ret;
default:
break;
}
break;
case 0xA00: /*Data1. also known as BLASTER+0x800 and EMU+0x400 */
switch (emu8k->cur_reg) {
case 0:
READ16(addr, emu8k->voice[emu8k->cur_voice].ccca);
return ret;
case 1:
switch (emu8k->cur_voice) {
case 9:
READ16(addr, emu8k->hwcf4);
return ret;
case 10:
READ16(addr, emu8k->hwcf5);
return ret;
/* Actually, these two might be command words rather than registers, or some LFO position/buffer reset.*/
case 13:
READ16(addr, emu8k->hwcf6);
return ret;
case 14:
READ16(addr, emu8k->hwcf7);
return ret;
case 20:
READ16(addr, emu8k->smalr);
return ret;
case 21:
READ16(addr, emu8k->smarr);
return ret;
case 22:
READ16(addr, emu8k->smalw);
return ret;
case 23:
READ16(addr, emu8k->smarw);
return ret;
case 26:
{
uint16_t val = emu8k->smld_buffer;
emu8k->smld_buffer = EMU8K_READ(emu8k, emu8k->smalr);
emu8k->smalr = (emu8k->smalr + 1) & EMU8K_MEM_ADDRESS_MASK;
return val;
}
/*The EMU8000 PGM describes the return values of these registers as 'a VLSI error'*/
case 29: /*Configuration Word 1*/
return (emu8k->hwcf1 & 0xfe) | (emu8k->hwcf3 & 0x01);
case 30: /*Configuration Word 2*/
return ((emu8k->hwcf2 >> 4) & 0x0e) | (emu8k->hwcf1 & 0x01) | ((emu8k->hwcf3 & 0x02) ? 0x10 : 0) | ((emu8k->hwcf3 & 0x04) ? 0x40 : 0)
| ((emu8k->hwcf3 & 0x08) ? 0x20 : 0) | ((emu8k->hwcf3 & 0x10) ? 0x80 : 0);
case 31: /*Configuration Word 3*/
return emu8k->hwcf2 & 0x1f;
default:
break;
}
break;
case 2:
return emu8k->init1[emu8k->cur_voice];
case 3:
return emu8k->init3[emu8k->cur_voice];
case 4:
return emu8k->voice[emu8k->cur_voice].envvol;
case 5:
return emu8k->voice[emu8k->cur_voice].dcysusv;
case 6:
return emu8k->voice[emu8k->cur_voice].envval;
case 7:
return emu8k->voice[emu8k->cur_voice].dcysus;
default:
break;
}
break;
case 0xA02: /*Data2. also known as BLASTER+0x802 and EMU+0x402 */
switch (emu8k->cur_reg) {
case 0:
READ16(addr, emu8k->voice[emu8k->cur_voice].ccca);
return ret;
case 1:
switch (emu8k->cur_voice) {
case 9:
READ16(addr, emu8k->hwcf4);
return ret;
case 10:
READ16(addr, emu8k->hwcf5);
return ret;
/* Actually, these two might be command words rather than registers, or some LFO position/buffer reset. */
case 13:
READ16(addr, emu8k->hwcf6);
return ret;
case 14:
READ16(addr, emu8k->hwcf7);
return ret;
/* Simulating empty/full bits by unsetting it once read. */
case 20:
READ16(addr, emu8k->smalr | dmareadbit);
/* xor with itself to set to zero faster. */
dmareadbit ^= dmareadbit;
return ret;
case 21:
READ16(addr, emu8k->smarr | dmareadbit);
/* xor with itself to set to zero faster.*/
dmareadbit ^= dmareadbit;
return ret;
case 22:
READ16(addr, emu8k->smalw | dmawritebit);
/*xor with itself to set to zero faster.*/
dmawritebit ^= dmawritebit;
return ret;
case 23:
READ16(addr, emu8k->smarw | dmawritebit);
/*xor with itself to set to zero faster.*/
dmawritebit ^= dmawritebit;
return ret;
case 26:
{
uint16_t val = emu8k->smrd_buffer;
emu8k->smrd_buffer = EMU8K_READ(emu8k, emu8k->smarr);
emu8k->smarr = (emu8k->smarr + 1) & EMU8K_MEM_ADDRESS_MASK;
return val;
}
/*TODO: We need to improve the precision of this clock, since
it is used by programs to wait. Not critical, but should help reduce
the amount of calls and wait time */
case 27: /*Sample Counter ( 44Khz clock) */
return emu8k->wc;
default:
break;
}
break;
case 2:
return emu8k->init2[emu8k->cur_voice];
case 3:
return emu8k->init4[emu8k->cur_voice];
case 4:
return emu8k->voice[emu8k->cur_voice].atkhldv;
case 5:
return emu8k->voice[emu8k->cur_voice].lfo1val;
case 6:
return emu8k->voice[emu8k->cur_voice].atkhld;
case 7:
return emu8k->voice[emu8k->cur_voice].lfo2val;
default:
break;
}
break;
case 0xE00: /*Data3. also known as BLASTER+0xC00 and EMU+0x800 */
switch (emu8k->cur_reg) {
case 0:
return emu8k->voice[emu8k->cur_voice].ip;
case 1:
return emu8k->voice[emu8k->cur_voice].ifatn;
case 2:
return emu8k->voice[emu8k->cur_voice].pefe;
case 3:
return emu8k->voice[emu8k->cur_voice].fmmod;
case 4:
return emu8k->voice[emu8k->cur_voice].tremfrq;
case 5:
return emu8k->voice[emu8k->cur_voice].fm2frq2;
case 6:
return 0xffff;
case 7: /*ID?*/
return 0x1c | ((emu8k->id & 0x0002) ? 0xff02 : 0);
default:
break;
}
break;
case 0xE02: /* Pointer. also known as BLASTER+0xC02 and EMU+0x802 */
/* LS five bits = channel number, next 3 bits = register number
* and MS 8 bits = VLSI test register.
* Impulse tracker tests the non variability of the LS byte that it has set, and the variability
* of the MS byte to determine that it really is an AWE32.
* cubic player has a similar code, where it waits until value & 0x1000 is nonzero, and then waits again until it changes to zero.*/
random_helper = (random_helper + 1) & 0x1F;
return ((0x80 | random_helper) << 8) | (emu8k->cur_reg << 5) | emu8k->cur_voice;
default:
break;
}
emu8k_log("EMU8K READ : Unknown register read: %04X-%02X(%d/%d) \n", addr, (emu8k->cur_reg << 5) | emu8k->cur_voice, emu8k->cur_reg, emu8k->cur_voice);
return 0xffff;
}
void
emu8k_outw(uint16_t addr, uint16_t val, void *priv)
{
emu8k_t *emu8k = (emu8k_t *) priv;
/*TODO: I would like to not call this here, but i found it was needed or else cubic player would not finish opening (take a looot more of time than usual).
* Basically, being here means that the audio is generated in the emulation thread, instead of the audio thread.*/
emu8k_update(emu8k);
#ifdef EMU8K_DEBUG_REGISTERS
if (addr == 0xE22) {
// emu8k_log("EMU8K WRITE POINTER: %d\n", val);
} else if ((addr & 0xF00) == 0x600) {
/* These are automatically reported by WRITE16 */
if (rep_count_w > 1) {
emu8k_log("EMU8K ...... for %d times\n", rep_count_w);
rep_count_w = 0;
}
last_write = 0;
} else if ((addr & 0xF00) == 0xA00 && emu8k->cur_reg == 0) {
/* These are automatically reported by WRITE16 */
if (rep_count_w > 1) {
emu8k_log("EMU8K ...... for %d times\n", rep_count_w);
rep_count_w = 0;
}
last_write = 0;
} else if ((addr & 0xF00) == 0xA00 && emu8k->cur_reg == 1) {
uint32_t tmpz = ((addr & 0xF00) << 16) | (emu8k->cur_reg << 5);
if (tmpz != last_write) {
if (rep_count_w > 1) {
emu8k_log("EMU8K ...... for %d times\n", rep_count_w);
rep_count_w = 0;
}
last_write = tmpz;
emu8k_log("EMU8K WRITE RAM I/O or configuration \n");
}
// emu8k_log("EMU8K WRITE %04X-%02X(%d/%d): %04X\n",addr,(emu8k->cur_reg)<<5|emu8k->cur_voice,emu8k->cur_reg,emu8k->cur_voice, val);
} else if ((addr & 0xF00) == 0xA00 && (emu8k->cur_reg == 2 || emu8k->cur_reg == 3)) {
uint32_t tmpz = ((addr & 0xF00) << 16);
if (tmpz != last_write) {
if (rep_count_w > 1) {
emu8k_log("EMU8K ...... for %d times\n", rep_count_w);
rep_count_w = 0;
}
last_write = tmpz;
emu8k_log("EMU8K WRITE INIT \n");
}
// emu8k_log("EMU8K WRITE %04X-%02X(%d/%d): %04X\n",addr,(emu8k->cur_reg)<<5|emu8k->cur_voice,emu8k->cur_reg,emu8k->cur_voice, val);
} else if (addr != 0xE22) {
uint32_t tmpz = (addr << 16) | (emu8k->cur_reg << 5) | emu8k->cur_voice;
// if (tmpz != last_write)
if (1) {
char *name = 0;
if (addr == 0xA20) {
name = PORT_NAMES[1][emu8k->cur_reg];
} else if (addr == 0xA22) {
name = PORT_NAMES[2][emu8k->cur_reg];
} else if (addr == 0xE20) {
name = PORT_NAMES[3][emu8k->cur_reg];
}
if (rep_count_w > 1) {
emu8k_log("EMU8K ...... for %d times\n", rep_count_w);
}
if (name == 0) {
emu8k_log("EMU8K WRITE %04X-%02X(%d/%d): %04X\n", addr, (emu8k->cur_reg) << 5 | emu8k->cur_voice, emu8k->cur_reg, emu8k->cur_voice, val);
} else {
emu8k_log("EMU8K WRITE %s (%d): %04X\n", name, emu8k->cur_voice, val);
}
rep_count_w = 0;
last_write = tmpz;
}
rep_count_w++;
}
#endif // EMU8K_DEBUG_REGISTERS
switch (addr & 0xF02) {
case 0x600:
case 0x602: /*Data0. also known as BLASTER+0x400 and EMU+0x000 */
switch (emu8k->cur_reg) {
case 0:
/* The docs says that this value is constantly updating, and it should have no actual effect. Actions should be done over ptrx */
WRITE16(addr, emu8k->voice[emu8k->cur_voice].cpf, val);
return;
case 1:
WRITE16(addr, emu8k->voice[emu8k->cur_voice].ptrx, val);
return;
case 2:
/* The docs says that this value is constantly updating, and it should have no actual effect. Actions should be done over vtft */
WRITE16(addr, emu8k->voice[emu8k->cur_voice].cvcf, val);
return;
case 3:
WRITE16(addr, emu8k->voice[emu8k->cur_voice].vtft, val);
return;
case 4:
WRITE16(addr, emu8k->voice[emu8k->cur_voice].unknown_data0_4, val);
return;
case 5:
WRITE16(addr, emu8k->voice[emu8k->cur_voice].unknown_data0_5, val);
return;
case 6:
{
emu8k_voice_t *emu_voice = &emu8k->voice[emu8k->cur_voice];
WRITE16(addr, emu_voice->psst, val);
/* TODO: Should we update only on MSB update, or this could be used as some sort of hack by applications? */
emu_voice->loop_start.int_address = emu_voice->psst & EMU8K_MEM_ADDRESS_MASK;
if (addr & 2) {
emu_voice->vol_l = emu_voice->psst_pan;
emu_voice->vol_r = 255 - (emu_voice->psst_pan);
}
}
return;
case 7:
WRITE16(addr, emu8k->voice[emu8k->cur_voice].csl, val);
/* TODO: Should we update only on MSB update, or this could be used as some sort of hack by applications? */
emu8k->voice[emu8k->cur_voice].loop_end.int_address = emu8k->voice[emu8k->cur_voice].csl & EMU8K_MEM_ADDRESS_MASK;
return;
default:
break;
}
break;
case 0xA00: /*Data1. also known as BLASTER+0x800 and EMU+0x400 */
switch (emu8k->cur_reg) {
case 0:
WRITE16(addr, emu8k->voice[emu8k->cur_voice].ccca, val);
/* TODO: Should we update only on MSB update, or this could be used as some sort of hack by applications? */
emu8k->voice[emu8k->cur_voice].addr.int_address = emu8k->voice[emu8k->cur_voice].ccca & EMU8K_MEM_ADDRESS_MASK;
return;
case 1:
switch (emu8k->cur_voice) {
case 9:
WRITE16(addr, emu8k->hwcf4, val);
return;
case 10:
WRITE16(addr, emu8k->hwcf5, val);
return;
/* Actually, these two might be command words rather than registers, or some LFO position/buffer reset. */
case 13:
WRITE16(addr, emu8k->hwcf6, val);
return;
case 14:
WRITE16(addr, emu8k->hwcf7, val);
return;
case 20:
WRITE16(addr, emu8k->smalr, val);
return;
case 21:
WRITE16(addr, emu8k->smarr, val);
return;
case 22:
WRITE16(addr, emu8k->smalw, val);
return;
case 23:
WRITE16(addr, emu8k->smarw, val);
return;
case 26:
EMU8K_WRITE(emu8k, emu8k->smalw, val);
emu8k->smalw = (emu8k->smalw + 1) & EMU8K_MEM_ADDRESS_MASK;
return;
case 29:
emu8k->hwcf1 = val;
return;
case 30:
emu8k->hwcf2 = val;
return;
case 31:
emu8k->hwcf3 = val;
return;
default:
break;
}
break;
case 2:
emu8k->init1[emu8k->cur_voice] = val;
/* Skip if in first/second initialization step */
if (emu8k->init1[0] != 0x03FF) {
switch (emu8k->cur_voice) {
case 0x3:
emu8k->reverb_engine.out_mix = val & 0xFF;
break;
case 0x5:
{
for (uint8_t c = 0; c < 8; c++) {
emu8k->reverb_engine.allpass[c].feedback = (val & 0xFF) / ((float) 0xFF);
}
}
break;
case 0x7:
emu8k->reverb_engine.link_return_type = (val == 0x8474) ? 1 : 0;
break;
case 0xF:
emu8k->reverb_engine.reflections[0].output_gain = ((val & 0xF0) >> 4) / 15.0;
break;
case 0x17: