1 /***********************************************************************************
3 emu2413.c -- YM2413 emulator written by Mitsutaka Okazaki 2001
5 2001 01-08 : Version 0.10 -- 1st version.
6 2001 01-15 : Version 0.20 -- semi-public version.
7 2001 01-16 : Version 0.30 -- 1st public version.
8 2001 01-17 : Version 0.31 -- Fixed bassdrum problem.
9 : Version 0.32 -- LPF implemented.
10 2001 01-18 : Version 0.33 -- Fixed the drum problem, refine the mix-down method.
12 2001 01-24 : Version 0.35 -- Fixed the drum problem,
13 support undocumented EG behavior.
14 2001 02-02 : Version 0.38 -- Improved the performance.
15 Fixed the hi-hat and cymbal model.
16 Fixed the default percussive datas.
18 Fixed the feedback problem.
19 2001 03-03 : Version 0.39 -- Fixed some drum bugs.
20 Improved the performance.
21 2001 03-04 : Version 0.40 -- Improved the feedback.
22 Change the default table size.
23 Clock and Rate can be changed during play.
24 2001 06-24 : Version 0.50 -- Improved the hi-hat and the cymbal tone.
25 Added VRC7 patch (OPLL_reset_patch is changed).
26 Fixed OPLL_reset() bug.
27 Added OPLL_setMask, OPLL_getMask and OPLL_toggleMask.
29 2001 09-28 : Version 0.51 -- Removed the noise table.
30 2002 01-28 : Version 0.52 -- Added Stereo mode.
31 2002 02-07 : Version 0.53 -- Fixed some drum bugs.
32 2002 02-20 : Version 0.54 -- Added the best quality mode.
33 2002 03-02 : Version 0.55 -- Removed OPLL_init & OPLL_close.
34 2002 05-30 : Version 0.60 -- Fixed HH&CYM generator and all voice datas.
36 2004 01-24 : Modified by xodnizel to remove code not needed for the VRC7, among other things.
39 fmopl.c -- 1999,2000 written by Tatsuyuki Satoh (MAME development).
40 fmopl.c(fixed) -- (C) 2002 Jarek Burczynski.
41 s_opl.c -- 2001 written by Mamiya (NEZplug development).
42 fmgen.cpp -- 1999,2000 written by cisc.
43 fmpac.ill -- 2000 created by NARUTO.
48 **************************************************************************************/
55 static const unsigned char default_inst[15][8] = {
59 /* Size of Sintable ( 8 -- 18 can be used. 9 recommended.)*/
61 #define PG_WIDTH (1<<PG_BITS)
63 /* Phase increment counter */
65 #define DP_WIDTH (1<<DP_BITS)
66 #define DP_BASE_BITS (DP_BITS - PG_BITS)
68 /* Dynamic range (Accuracy of sin table) */
70 #define DB_STEP (48.0/(1<<DB_BITS))
71 #define DB_MUTE (1<<DB_BITS)
73 /* Dynamic range of envelope */
76 #define EG_MUTE (1<<EG_BITS)
78 /* Dynamic range of total level */
81 #define TL_MUTE (1<<TL_BITS)
83 /* Dynamic range of sustine level */
86 #define SL_MUTE (1<<SL_BITS)
88 #define EG2DB(d) ((d)*(e_int32)(EG_STEP/DB_STEP))
89 #define TL2EG(d) ((d)*(e_int32)(TL_STEP/EG_STEP))
90 #define SL2EG(d) ((d)*(e_int32)(SL_STEP/EG_STEP))
92 #define DB_POS(x) (e_uint32)((x)/DB_STEP)
93 #define DB_NEG(x) (e_uint32)(DB_MUTE+DB_MUTE+(x)/DB_STEP)
95 /* Bits for liner value */
96 #define DB2LIN_AMP_BITS 11
97 #define SLOT_AMP_BITS (DB2LIN_AMP_BITS)
99 /* Bits for envelope phase incremental counter */
100 #define EG_DP_BITS 22
101 #define EG_DP_WIDTH (1<<EG_DP_BITS)
103 /* Bits for Pitch and Amp modulator */
105 #define PM_PG_WIDTH (1<<PM_PG_BITS)
106 #define PM_DP_BITS 16
107 #define PM_DP_WIDTH (1<<PM_DP_BITS)
109 #define AM_PG_WIDTH (1<<AM_PG_BITS)
110 #define AM_DP_BITS 16
111 #define AM_DP_WIDTH (1<<AM_DP_BITS)
113 /* PM table is calcurated by PM_AMP * pow(2,PM_DEPTH*sin(x)/1200) */
114 #define PM_AMP_BITS 8
115 #define PM_AMP (1<<PM_AMP_BITS)
117 /* PM speed(Hz) and depth(cent) */
119 #define PM_DEPTH 13.75
121 /* AM speed(Hz) and depth(dB) */
123 //#define AM_DEPTH 4.8
126 /* Cut the lower b bit(s) off. */
127 #define HIGHBITS(c,b) ((c)>>(b))
129 /* Leave the lower b bit(s). */
130 #define LOWBITS(c,b) ((c)&((1<<(b))-1))
132 /* Expand x which is s bits to d bits. */
133 #define EXPAND_BITS(x,s,d) ((x)<<((d)-(s)))
135 /* Expand x which is s bits to d bits and fill expanded bits '1' */
136 #define EXPAND_BITS_X(x,s,d) (((x)<<((d)-(s)))|((1<<((d)-(s)))-1))
138 /* Adjust envelope speed which depends on sampling rate. */
139 #define rate_adjust(x) (rate==49716?x:(e_uint32)((double)(x)*clk/72/rate + 0.5)) /* added 0.5 to round the value*/
141 #define MOD(o,x) (&(o)->slot[(x)<<1])
142 #define CAR(o,x) (&(o)->slot[((x)<<1)|1])
144 #define BIT(s,b) (((s)>>(b))&1)
147 static e_uint32 clk = 844451141;
149 static e_uint32 rate = 3354932;
151 /* WaveTable for each envelope amp */
152 static e_uint16 fullsintable[PG_WIDTH];
153 static e_uint16 halfsintable[PG_WIDTH];
155 static e_uint16 *waveform[2] = { fullsintable, halfsintable };
158 static e_int32 pmtable[PM_PG_WIDTH];
159 static e_int32 amtable[AM_PG_WIDTH];
161 /* Phase delta for LFO */
162 static e_uint32 pm_dphase;
163 static e_uint32 am_dphase;
165 /* dB to Liner table */
166 static e_int16 DB2LIN_TABLE[(DB_MUTE + DB_MUTE) * 2];
168 /* Liner to Log curve conversion table (for Attack rate). */
169 static e_uint16 AR_ADJUST_TABLE[1 << EG_BITS];
171 /* Definition of envelope mode */
173 { SETTLE, ATTACK, DECAY, SUSHOLD, SUSTINE, RELEASE, FINISH };
175 /* Phase incr table for Attack */
176 static e_uint32 dphaseARTable[16][16];
177 /* Phase incr table for Decay and Release */
178 static e_uint32 dphaseDRTable[16][16];
181 static e_uint32 tllTable[16][8][1 << TL_BITS][4];
182 static e_int32 rksTable[2][8][2];
184 /* Phase incr table for PG */
185 static e_uint32 dphaseTable[512][8][16];
187 /***************************************************
191 ****************************************************/
192 INLINE static e_int32
193 Min (e_int32 i, e_int32 j)
201 /* Table for AR to LogCurve. */
203 makeAdjustTable (void)
207 AR_ADJUST_TABLE[0] = (1 << EG_BITS);
208 for (i = 1; i < 128; i++)
209 AR_ADJUST_TABLE[i] = (e_uint16) ((double) (1 << EG_BITS) - 1 - (1 << EG_BITS) * log (i) / log (128));
213 /* Table for dB(0 -- (1<<DB_BITS)-1) to Liner(0 -- DB2LIN_AMP_WIDTH) */
215 makeDB2LinTable (void)
219 for (i = 0; i < DB_MUTE + DB_MUTE; i++)
221 DB2LIN_TABLE[i] = (e_int16) ((double) ((1 << DB2LIN_AMP_BITS) - 1) * pow (10, -(double) i * DB_STEP / 20));
222 if(i >= DB_MUTE) DB2LIN_TABLE[i] = 0;
223 DB2LIN_TABLE[i + DB_MUTE + DB_MUTE] = (e_int16) (-DB2LIN_TABLE[i]);
227 /* Liner(+0.0 - +1.0) to dB((1<<DB_BITS) - 1 -- 0) */
232 return (DB_MUTE - 1);
234 return Min (-(e_int32) (20.0 * log10 (d) / DB_STEP), DB_MUTE-1); /* 0 -- 127 */
244 for (i = 0; i < PG_WIDTH / 4; i++)
246 fullsintable[i] = (e_uint32) lin2db (sin (2.0 * PI * i / PG_WIDTH) );
249 for (i = 0; i < PG_WIDTH / 4; i++)
251 fullsintable[PG_WIDTH / 2 - 1 - i] = fullsintable[i];
254 for (i = 0; i < PG_WIDTH / 2; i++)
256 fullsintable[PG_WIDTH / 2 + i] = (e_uint32) (DB_MUTE + DB_MUTE + fullsintable[i]);
259 for (i = 0; i < PG_WIDTH / 2; i++)
260 halfsintable[i] = fullsintable[i];
261 for (i = PG_WIDTH / 2; i < PG_WIDTH; i++)
262 halfsintable[i] = fullsintable[0];
265 /* Table for Pitch Modulator */
271 for (i = 0; i < PM_PG_WIDTH; i++)
272 pmtable[i] = (e_int32) ((double) PM_AMP * pow (2, (double) PM_DEPTH * sin (2.0 * PI * i / PM_PG_WIDTH) / 1200));
275 /* Table for Amp Modulator */
281 for (i = 0; i < AM_PG_WIDTH; i++)
282 amtable[i] = (e_int32) ((double) AM_DEPTH / 2 / DB_STEP * (1.0 + sin (2.0 * PI * i / PM_PG_WIDTH)));
285 /* Phase increment counter table */
287 makeDphaseTable (void)
289 e_uint32 fnum, block, ML;
290 e_uint32 mltable[16] =
291 { 1, 1 * 2, 2 * 2, 3 * 2, 4 * 2, 5 * 2, 6 * 2, 7 * 2, 8 * 2, 9 * 2, 10 * 2, 10 * 2, 12 * 2, 12 * 2, 15 * 2, 15 * 2 };
293 for (fnum = 0; fnum < 512; fnum++)
294 for (block = 0; block < 8; block++)
295 for (ML = 0; ML < 16; ML++)
296 dphaseTable[fnum][block][ML] = rate_adjust (((fnum * mltable[ML]) << block) >> (20 - DP_BITS));
302 #define dB2(x) ((x)*2)
304 static double kltable[16] = {
305 dB2 (0.000), dB2 (9.000), dB2 (12.000), dB2 (13.875), dB2 (15.000), dB2 (16.125), dB2 (16.875), dB2 (17.625),
306 dB2 (18.000), dB2 (18.750), dB2 (19.125), dB2 (19.500), dB2 (19.875), dB2 (20.250), dB2 (20.625), dB2 (21.000)
310 e_int32 fnum, block, TL, KL;
312 for (fnum = 0; fnum < 16; fnum++)
313 for (block = 0; block < 8; block++)
314 for (TL = 0; TL < 64; TL++)
315 for (KL = 0; KL < 4; KL++)
319 tllTable[fnum][block][TL][KL] = TL2EG (TL);
323 tmp = (e_int32) (kltable[fnum] - dB2 (3.000) * (7 - block));
325 tllTable[fnum][block][TL][KL] = TL2EG (TL);
327 tllTable[fnum][block][TL][KL] = (e_uint32) ((tmp >> (3 - KL)) / EG_STEP) + TL2EG (TL);
332 #ifdef USE_SPEC_ENV_SPEED
333 static double attacktime[16][4] = {
335 {1730.15, 1400.60, 1153.43, 988.66},
336 {865.08, 700.30, 576.72, 494.33},
337 {432.54, 350.15, 288.36, 247.16},
338 {216.27, 175.07, 144.18, 123.58},
339 {108.13, 87.54, 72.09, 61.79},
340 {54.07, 43.77, 36.04, 30.90},
341 {27.03, 21.88, 18.02, 15.45},
342 {13.52, 10.94, 9.01, 7.72},
343 {6.76, 5.47, 4.51, 3.86},
344 {3.38, 2.74, 2.25, 1.93},
345 {1.69, 1.37, 1.13, 0.97},
346 {0.84, 0.70, 0.60, 0.54},
347 {0.50, 0.42, 0.34, 0.30},
348 {0.28, 0.22, 0.18, 0.14},
349 {0.00, 0.00, 0.00, 0.00}
352 static double decaytime[16][4] = {
354 {20926.60, 16807.20, 14006.00, 12028.60},
355 {10463.30, 8403.58, 7002.98, 6014.32},
356 {5231.64, 4201.79, 3501.49, 3007.16},
357 {2615.82, 2100.89, 1750.75, 1503.58},
358 {1307.91, 1050.45, 875.37, 751.79},
359 {653.95, 525.22, 437.69, 375.90},
360 {326.98, 262.61, 218.84, 187.95},
361 {163.49, 131.31, 109.42, 93.97},
362 {81.74, 65.65, 54.71, 46.99},
363 {40.87, 32.83, 27.36, 23.49},
364 {20.44, 16.41, 13.68, 11.75},
365 {10.22, 8.21, 6.84, 5.87},
366 {5.11, 4.10, 3.42, 2.94},
367 {2.55, 2.05, 1.71, 1.47},
368 {1.27, 1.27, 1.27, 1.27}
372 /* Rate Table for Attack */
374 makeDphaseARTable (void)
376 e_int32 AR, Rks, RM, RL;
377 #ifdef USE_SPEC_ENV_SPEED
378 e_uint32 attacktable[16][4];
380 for (RM = 0; RM < 16; RM++)
381 for (RL = 0; RL < 4; RL++)
384 attacktable[RM][RL] = 0;
386 attacktable[RM][RL] = EG_DP_WIDTH;
388 attacktable[RM][RL] = (e_uint32) ((double) (1 << EG_DP_BITS) / (attacktime[RM][RL] * 3579545 / 72000));
393 for (AR = 0; AR < 16; AR++)
394 for (Rks = 0; Rks < 16; Rks++)
396 RM = AR + (Rks >> 2);
403 dphaseARTable[AR][Rks] = 0;
406 dphaseARTable[AR][Rks] = 0;/*EG_DP_WIDTH;*/
409 #ifdef USE_SPEC_ENV_SPEED
410 dphaseARTable[AR][Rks] = rate_adjust (attacktable[RM][RL]);
412 dphaseARTable[AR][Rks] = rate_adjust ((3 * (RL + 4) << (RM + 1)));
419 /* Rate Table for Decay and Release */
421 makeDphaseDRTable (void)
423 e_int32 DR, Rks, RM, RL;
425 #ifdef USE_SPEC_ENV_SPEED
426 e_uint32 decaytable[16][4];
428 for (RM = 0; RM < 16; RM++)
429 for (RL = 0; RL < 4; RL++)
431 decaytable[RM][RL] = 0;
433 decaytable[RM][RL] = (e_uint32) ((double) (1 << EG_DP_BITS) / (decaytime[RM][RL] * 3579545 / 72000));
436 for (DR = 0; DR < 16; DR++)
437 for (Rks = 0; Rks < 16; Rks++)
439 RM = DR + (Rks >> 2);
446 dphaseDRTable[DR][Rks] = 0;
449 #ifdef USE_SPEC_ENV_SPEED
450 dphaseDRTable[DR][Rks] = rate_adjust (decaytable[RM][RL]);
452 dphaseDRTable[DR][Rks] = rate_adjust ((RL + 4) << (RM - 1));
463 e_int32 fnum8, block, KR;
465 for (fnum8 = 0; fnum8 < 2; fnum8++)
466 for (block = 0; block < 8; block++)
467 for (KR = 0; KR < 2; KR++)
470 rksTable[fnum8][block][KR] = (block << 1) + fnum8;
472 rksTable[fnum8][block][KR] = block >> 1;
476 /************************************************************
480 ************************************************************/
482 INLINE static e_uint32
483 calc_eg_dphase (OPLL_SLOT * slot)
486 switch (slot->eg_mode)
489 return dphaseARTable[slot->patch.AR][slot->rks];
492 return dphaseDRTable[slot->patch.DR][slot->rks];
498 return dphaseDRTable[slot->patch.RR][slot->rks];
502 return dphaseDRTable[5][slot->rks];
503 else if(slot->patch.EG)
504 return dphaseDRTable[slot->patch.RR][slot->rks];
506 return dphaseDRTable[7][slot->rks];
516 /*************************************************************
518 OPLL internal interfaces
520 *************************************************************/
522 #define UPDATE_PG(S) (S)->dphase = dphaseTable[(S)->fnum][(S)->block][(S)->patch.ML]
523 #define UPDATE_TLL(S)\
525 ((S)->tll = tllTable[((S)->fnum)>>5][(S)->block][(S)->patch.TL][(S)->patch.KL]):\
526 ((S)->tll = tllTable[((S)->fnum)>>5][(S)->block][(S)->volume][(S)->patch.KL]))
527 #define UPDATE_RKS(S) (S)->rks = rksTable[((S)->fnum)>>8][(S)->block][(S)->patch.KR]
528 #define UPDATE_WF(S) (S)->sintbl = waveform[(S)->patch.WF]
529 #define UPDATE_EG(S) (S)->eg_dphase = calc_eg_dphase(S)
530 #define UPDATE_ALL(S)\
535 UPDATE_EG(S) /* EG should be updated last. */
540 slotOn (OPLL_SLOT * slot)
542 slot->eg_mode = ATTACK;
547 /* Slot key on without reseting the phase */
549 slotOn2 (OPLL_SLOT * slot)
551 slot->eg_mode = ATTACK;
557 slotOff (OPLL_SLOT * slot)
559 if(slot->eg_mode == ATTACK)
560 slot->eg_phase = EXPAND_BITS (AR_ADJUST_TABLE[HIGHBITS (slot->eg_phase, EG_DP_BITS - EG_BITS)], EG_BITS, EG_DP_BITS);
561 slot->eg_mode = RELEASE;
566 keyOn (OPLL * opll, e_int32 i)
568 if(!opll->slot_on_flag[i * 2])
569 slotOn (MOD(opll,i));
570 if(!opll->slot_on_flag[i * 2 + 1])
571 slotOn (CAR(opll,i));
572 opll->key_status[i] = 1;
575 /* Channel key off */
577 keyOff (OPLL * opll, e_int32 i)
579 if(opll->slot_on_flag[i * 2 + 1])
580 slotOff (CAR(opll,i));
581 opll->key_status[i] = 0;
584 /* Set sustine parameter */
586 setSustine (OPLL * opll, e_int32 c, e_int32 sustine)
588 CAR(opll,c)->sustine = sustine;
589 if(MOD(opll,c)->type)
590 MOD(opll,c)->sustine = sustine;
593 /* Volume : 6bit ( Volume register << 2 ) */
595 setVolume (OPLL * opll, e_int32 c, e_int32 volume)
597 CAR(opll,c)->volume = volume;
601 setSlotVolume (OPLL_SLOT * slot, e_int32 volume)
603 slot->volume = volume;
606 /* Set F-Number ( fnum : 9bit ) */
608 setFnumber (OPLL * opll, e_int32 c, e_int32 fnum)
610 CAR(opll,c)->fnum = fnum;
611 MOD(opll,c)->fnum = fnum;
614 /* Set Block data (block : 3bit ) */
616 setBlock (OPLL * opll, e_int32 c, e_int32 block)
618 CAR(opll,c)->block = block;
619 MOD(opll,c)->block = block;
622 INLINE static void update_key_status (OPLL * opll)
626 for (ch = 0; ch < 6; ch++)
627 opll->slot_on_flag[ch * 2] = opll->slot_on_flag[ch * 2 + 1] = (opll->HiFreq[ch]) & 0x10;
630 /***********************************************************
634 ***********************************************************/
637 OPLL_SLOT_reset (OPLL_SLOT * slot, int type)
640 slot->sintbl = waveform[0];
646 slot->eg_mode = SETTLE;
647 slot->eg_phase = EG_DP_WIDTH;
660 internal_refresh (void)
663 makeDphaseARTable ();
664 makeDphaseDRTable ();
665 pm_dphase = (e_uint32) rate_adjust (PM_SPEED * PM_DP_WIDTH / (clk / 72));
666 am_dphase = (e_uint32) rate_adjust (AM_SPEED * AM_DP_WIDTH / (clk / 72));
670 maketables (e_uint32 c, e_uint32 r)
682 //makeDefaultPatch ();
692 OPLL *OPLL_new (e_uint32 clk, e_uint32 rate)
696 maketables (clk, rate);
698 opll = (OPLL *) calloc (sizeof (OPLL), 1);
711 OPLL_delete (OPLL * opll)
716 /* Reset whole of OPLL except patch datas. */
718 OPLL_reset (OPLL * opll)
733 for (i = 0; i < 12; i++)
734 OPLL_SLOT_reset(&opll->slot[i], i%2);
736 for (i = 0; i < 6; i++)
738 opll->key_status[i] = 0;
739 //setPatch (opll, i, 0);
742 for (i = 0; i < 0x40; i++)
743 OPLL_writeReg (opll, i, 0);
745 #ifndef EMU2413_COMPACTION
746 opll->realstep = (e_uint32) ((1 << 31) / rate);
747 opll->opllstep = (e_uint32) ((1 << 31) / (clk / 72));
752 /* Force Refresh (When external program changes some parameters). */
754 OPLL_forceRefresh (OPLL * opll)
761 for (i = 0; i < 12; i++)
763 UPDATE_PG (&opll->slot[i]);
764 UPDATE_RKS (&opll->slot[i]);
765 UPDATE_TLL (&opll->slot[i]);
766 UPDATE_WF (&opll->slot[i]);
767 UPDATE_EG (&opll->slot[i]);
772 OPLL_set_rate (OPLL * opll, e_uint32 r)
783 OPLL_set_quality (OPLL * opll, e_uint32 q)
786 OPLL_set_rate (opll, rate);
789 /*********************************************************
793 *********************************************************/
794 /* Convert Amp(0 to EG_HEIGHT) to Phase(0 to 2PI). */
795 #if( SLOT_AMP_BITS - PG_BITS ) > 0
796 #define wave2_2pi(e) ( (e) >> ( SLOT_AMP_BITS - PG_BITS ))
798 #define wave2_2pi(e) ( (e) << ( PG_BITS - SLOT_AMP_BITS ))
801 /* Convert Amp(0 to EG_HEIGHT) to Phase(0 to 4PI). */
802 #if( SLOT_AMP_BITS - PG_BITS - 1 ) == 0
803 #define wave2_4pi(e) (e)
804 #elif( SLOT_AMP_BITS - PG_BITS - 1 ) > 0
805 #define wave2_4pi(e) ( (e) >> ( SLOT_AMP_BITS - PG_BITS - 1 ))
807 #define wave2_4pi(e) ( (e) << ( 1 + PG_BITS - SLOT_AMP_BITS ))
810 /* Convert Amp(0 to EG_HEIGHT) to Phase(0 to 8PI). */
811 #if( SLOT_AMP_BITS - PG_BITS - 2 ) == 0
812 #define wave2_8pi(e) (e)
813 #elif( SLOT_AMP_BITS - PG_BITS - 2 ) > 0
814 #define wave2_8pi(e) ( (e) >> ( SLOT_AMP_BITS - PG_BITS - 2 ))
816 #define wave2_8pi(e) ( (e) << ( 2 + PG_BITS - SLOT_AMP_BITS ))
821 /* Update AM, PM unit */
823 update_ampm (OPLL * opll)
825 opll->pm_phase = (opll->pm_phase + pm_dphase) & (PM_DP_WIDTH - 1);
826 opll->am_phase = (opll->am_phase + am_dphase) & (AM_DP_WIDTH - 1);
827 opll->lfo_am = amtable[HIGHBITS (opll->am_phase, AM_DP_BITS - AM_PG_BITS)];
828 opll->lfo_pm = pmtable[HIGHBITS (opll->pm_phase, PM_DP_BITS - PM_PG_BITS)];
833 calc_phase (OPLL_SLOT * slot, e_int32 lfo)
836 slot->phase += (slot->dphase * lfo) >> PM_AMP_BITS;
838 slot->phase += slot->dphase;
840 slot->phase &= (DP_WIDTH - 1);
842 slot->pgout = HIGHBITS (slot->phase, DP_BASE_BITS);
847 calc_envelope (OPLL_SLOT * slot, e_int32 lfo)
849 #define S2E(x) (SL2EG((e_int32)(x/SL_STEP))<<(EG_DP_BITS-EG_BITS))
851 static e_uint32 SL[16] = {
852 S2E (0.0), S2E (3.0), S2E (6.0), S2E (9.0), S2E (12.0), S2E (15.0), S2E (18.0), S2E (21.0),
853 S2E (24.0), S2E (27.0), S2E (30.0), S2E (33.0), S2E (36.0), S2E (39.0), S2E (42.0), S2E (48.0)
858 switch (slot->eg_mode)
862 egout = AR_ADJUST_TABLE[HIGHBITS (slot->eg_phase, EG_DP_BITS - EG_BITS)];
863 slot->eg_phase += slot->eg_dphase;
864 if((EG_DP_WIDTH & slot->eg_phase)||(slot->patch.AR==15))
868 slot->eg_mode = DECAY;
874 egout = HIGHBITS (slot->eg_phase, EG_DP_BITS - EG_BITS);
875 slot->eg_phase += slot->eg_dphase;
876 if(slot->eg_phase >= SL[slot->patch.SL])
880 slot->eg_phase = SL[slot->patch.SL];
881 slot->eg_mode = SUSHOLD;
886 slot->eg_phase = SL[slot->patch.SL];
887 slot->eg_mode = SUSTINE;
894 egout = HIGHBITS (slot->eg_phase, EG_DP_BITS - EG_BITS);
895 if(slot->patch.EG == 0)
897 slot->eg_mode = SUSTINE;
904 egout = HIGHBITS (slot->eg_phase, EG_DP_BITS - EG_BITS);
905 slot->eg_phase += slot->eg_dphase;
906 if(egout >= (1 << EG_BITS))
908 slot->eg_mode = FINISH;
909 egout = (1 << EG_BITS) - 1;
914 egout = (1 << EG_BITS) - 1;
918 egout = (1 << EG_BITS) - 1;
923 egout = EG2DB (egout + slot->tll) + lfo;
925 egout = EG2DB (egout + slot->tll);
934 INLINE static e_int32
935 calc_slot_car (OPLL_SLOT * slot, e_int32 fm)
937 slot->output[1] = slot->output[0];
939 if(slot->egout >= (DB_MUTE - 1))
945 slot->output[0] = DB2LIN_TABLE[slot->sintbl[(slot->pgout+wave2_8pi(fm))&(PG_WIDTH-1)] + slot->egout];
948 return (slot->output[1] + slot->output[0]) >> 1;
952 INLINE static e_int32
953 calc_slot_mod (OPLL_SLOT * slot)
957 slot->output[1] = slot->output[0];
959 if(slot->egout >= (DB_MUTE - 1))
963 else if(slot->patch.FB != 0)
965 fm = wave2_4pi (slot->feedback) >> (7 - slot->patch.FB);
966 slot->output[0] = DB2LIN_TABLE[slot->sintbl[(slot->pgout + fm)&(PG_WIDTH-1)] + slot->egout];
970 slot->output[0] = DB2LIN_TABLE[slot->sintbl[slot->pgout] + slot->egout];
973 slot->feedback = (slot->output[1] + slot->output[0]) >> 1;
975 return slot->feedback;
979 static INLINE e_int16 calc (OPLL * opll)
981 e_int32 inst = 0, out = 0;
986 for (i = 0; i < 12; i++)
988 calc_phase(&opll->slot[i],opll->lfo_pm);
989 calc_envelope(&opll->slot[i],opll->lfo_am);
992 for (i = 0; i < 6; i++)
993 if(!(opll->mask & OPLL_MASK_CH (i)) && (CAR(opll,i)->eg_mode != FINISH))
994 inst += calc_slot_car (CAR(opll,i), calc_slot_mod(MOD(opll,i)));
997 return (e_int16) out;
1000 void moocow(OPLL* opll, e_int32 *buf, e_int32 len, int shift)
1004 *buf+=(calc(opll)+32768)<<shift;
1010 #ifdef EMU2413_COMPACTION
1012 OPLL_calc (OPLL * opll)
1018 OPLL_calc (OPLL * opll)
1023 while (opll->realstep > opll->oplltime)
1025 opll->oplltime += opll->opllstep;
1026 opll->prev = opll->next;
1027 opll->next = calc (opll);
1030 opll->oplltime -= opll->realstep;
1031 opll->out = (e_int16) (((double) opll->next * (opll->opllstep - opll->oplltime)
1032 + (double) opll->prev * opll->oplltime) / opll->opllstep);
1034 return (e_int16) opll->out;
1039 OPLL_setMask (OPLL * opll, e_uint32 mask)
1054 OPLL_toggleMask (OPLL * opll, e_uint32 mask)
1068 /****************************************************
1072 *****************************************************/
1074 static void setInstrument(OPLL * opll, e_uint i, e_uint inst)
1077 OPLL_PATCH *modp, *carp;
1079 opll->patch_number[i]=inst;
1082 src=default_inst[inst-1];
1086 modp=&MOD(opll,i)->patch;
1087 carp=&CAR(opll,i)->patch;
1089 modp->AM=(src[0]>>7)&1;
1090 modp->PM=(src[0]>>6)&1;
1091 modp->EG=(src[0]>>5)&1;
1092 modp->KR=(src[0]>>4)&1;
1093 modp->ML=(src[0]&0xF);
1095 carp->AM=(src[1]>>7)&1;
1096 carp->PM=(src[1]>>6)&1;
1097 carp->EG=(src[1]>>5)&1;
1098 carp->KR=(src[1]>>4)&1;
1099 carp->ML=(src[1]&0xF);
1101 modp->KL=(src[2]>>6)&3;
1102 modp->TL=(src[2]&0x3F);
1104 carp->KL = (src[3] >> 6) & 3;
1105 carp->WF = (src[3] >> 4) & 1;
1107 modp->WF = (src[3] >> 3) & 1;
1109 modp->FB = (src[3]) & 7;
1111 modp->AR = (src[4]>>4)&0xF;
1112 modp->DR = (src[4]&0xF);
1114 carp->AR = (src[5]>>4)&0xF;
1115 carp->DR = (src[5]&0xF);
1117 modp->SL = (src[6]>>4)&0xF;
1118 modp->RR = (src[6]&0xF);
1120 carp->SL = (src[7]>>4)&0xF;
1121 carp->RR = (src[7]&0xF);
1126 OPLL_writeReg (OPLL * opll, e_uint32 reg, e_uint32 data)
1137 opll->CustInst[0]=data;
1138 for (i = 0; i < 6; i++)
1140 if(opll->patch_number[i] == 0)
1142 setInstrument(opll, i, 0);
1143 UPDATE_PG (MOD(opll,i));
1144 UPDATE_RKS (MOD(opll,i));
1145 UPDATE_EG (MOD(opll,i));
1151 opll->CustInst[1]=data;
1152 for (i = 0; i < 6; i++)
1154 if(opll->patch_number[i] == 0)
1156 setInstrument(opll, i, 0);
1157 UPDATE_PG (CAR(opll,i));
1158 UPDATE_RKS (CAR(opll,i));
1159 UPDATE_EG (CAR(opll,i));
1165 opll->CustInst[2]=data;
1166 for (i = 0; i < 6; i++)
1168 if(opll->patch_number[i] == 0)
1170 setInstrument(opll, i, 0);
1171 UPDATE_TLL(MOD(opll,i));
1177 opll->CustInst[3]=data;
1178 for (i = 0; i < 6; i++)
1180 if(opll->patch_number[i] == 0)
1182 setInstrument(opll, i, 0);
1183 UPDATE_WF(MOD(opll,i));
1184 UPDATE_WF(CAR(opll,i));
1190 opll->CustInst[4]=data;
1191 for (i = 0; i < 6; i++)
1193 if(opll->patch_number[i] == 0)
1195 setInstrument(opll, i, 0);
1196 UPDATE_EG (MOD(opll,i));
1202 opll->CustInst[5]=data;
1203 for (i = 0; i < 6; i++)
1205 if(opll->patch_number[i] == 0)
1207 setInstrument(opll, i, 0);
1208 UPDATE_EG(CAR(opll,i));
1214 opll->CustInst[6]=data;
1215 for (i = 0; i < 6; i++)
1217 if(opll->patch_number[i] == 0)
1219 setInstrument(opll, i, 0);
1220 UPDATE_EG (MOD(opll,i));
1226 opll->CustInst[7]=data;
1227 for (i = 0; i < 6; i++)
1229 if(opll->patch_number[i] == 0)
1231 setInstrument(opll, i, 0);
1232 UPDATE_EG (CAR(opll,i));
1244 opll->LowFreq[ch]=data;
1245 setFnumber (opll, ch, data + ((opll->HiFreq[ch] & 1) << 8));
1246 UPDATE_ALL (MOD(opll,ch));
1247 UPDATE_ALL (CAR(opll,ch));
1257 opll->HiFreq[ch]=data;
1259 setFnumber (opll, ch, ((data & 1) << 8) + opll->LowFreq[ch]);
1260 setBlock (opll, ch, (data >> 1) & 7);
1261 setSustine (opll, ch, (data >> 5) & 1);
1266 UPDATE_ALL (MOD(opll,ch));
1267 UPDATE_ALL (CAR(opll,ch));
1268 update_key_status (opll);
1277 opll->InstVol[reg-0x30]=data;
1278 i = (data >> 4) & 15;
1280 setInstrument(opll, reg-0x30, i);
1281 setVolume (opll, reg - 0x30, v << 2);
1282 UPDATE_ALL (MOD(opll,reg - 0x30));
1283 UPDATE_ALL (CAR(opll,reg - 0x30));
1293 OPLL_writeIO (OPLL * opll, e_uint32 adr, e_uint32 val)
1296 OPLL_writeReg (opll, opll->adr, val);