3 ** File: fmopl.c -- software implementation of FM sound generator
5 ** Copyright (C) 1999,2000 Tatsuyuki Satoh , MultiArcadeMachineEmurator development
17 /* This version of fmopl.c is a fork of the MAME one, relicensed under the LGPL.
19 * This library is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU Lesser General Public
21 * License as published by the Free Software Foundation; either
22 * version 2.1 of the License, or (at your option) any later version.
24 * This library is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * Lesser General Public License for more details.
29 * You should have received a copy of the GNU Lesser General Public
30 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
33 #include "qemu/osdep.h"
35 //#include "driver.h" /* use M.A.M.E. */
39 #define PI 3.14159265358979323846
43 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
46 /* -------------------- for debug --------------------- */
47 /* #define OPL_OUTPUT_LOG */
49 static FILE *opl_dbg_fp
= NULL
;
50 static FM_OPL
*opl_dbg_opl
[16];
51 static int opl_dbg_maxchip
,opl_dbg_chip
;
54 /* -------------------- preliminary define section --------------------- */
55 /* attack/decay rate time rate */
56 #define OPL_ARRATE 141280 /* RATE 4 = 2826.24ms @ 3.6MHz */
57 #define OPL_DRRATE 1956000 /* RATE 4 = 39280.64ms @ 3.6MHz */
59 #define DELTAT_MIXING_LEVEL (1) /* DELTA-T ADPCM MIXING LEVEL */
61 #define FREQ_BITS 24 /* frequency turn */
63 /* counter bits = 20 , octerve 7 */
64 #define FREQ_RATE (1<<(FREQ_BITS-20))
65 #define TL_BITS (FREQ_BITS+2)
67 /* final output shift , limit minimum and maximum */
68 #define OPL_OUTSB (TL_BITS+3-16) /* OPL output final shift 16bit */
69 #define OPL_MAXOUT (0x7fff<<OPL_OUTSB)
70 #define OPL_MINOUT (-0x8000<<OPL_OUTSB)
72 /* -------------------- quality selection --------------------- */
75 /* used static memory = SIN_ENT * 4 (byte) */
78 /* output level entries (envelope,sinwave) */
79 /* envelope counter lower bits */
81 /* envelope output entries */
83 /* used dynamic memory = EG_ENT*4*4(byte)or EG_ENT*6*4(byte) */
84 /* used static memory = EG_ENT*4 (byte) */
86 #define EG_OFF ((2*EG_ENT)<<ENV_BITS) /* OFF */
88 #define EG_DST (EG_ENT<<ENV_BITS) /* DECAY START */
90 #define EG_AST 0 /* ATTACK START */
92 #define EG_STEP (96.0/EG_ENT) /* OPL is 0.1875 dB step */
94 /* LFO table entries */
96 #define VIB_SHIFT (32-9)
98 #define AMS_SHIFT (32-9)
102 /* -------------------- local defines , macros --------------------- */
104 /* register number to channel number , slot offset */
109 #define ENV_MOD_RR 0x00
110 #define ENV_MOD_DR 0x01
111 #define ENV_MOD_AR 0x02
113 /* -------------------- tables --------------------- */
114 static const int slot_array
[32]=
116 0, 2, 4, 1, 3, 5,-1,-1,
117 6, 8,10, 7, 9,11,-1,-1,
118 12,14,16,13,15,17,-1,-1,
119 -1,-1,-1,-1,-1,-1,-1,-1
122 /* key scale level */
123 /* table is 3dB/OCT , DV converts this in TL step at 6dB/OCT */
124 #define DV (EG_STEP/2)
125 static const uint32_t KSL_TABLE
[8*16]=
128 0.000/DV
, 0.000/DV
, 0.000/DV
, 0.000/DV
,
129 0.000/DV
, 0.000/DV
, 0.000/DV
, 0.000/DV
,
130 0.000/DV
, 0.000/DV
, 0.000/DV
, 0.000/DV
,
131 0.000/DV
, 0.000/DV
, 0.000/DV
, 0.000/DV
,
133 0.000/DV
, 0.000/DV
, 0.000/DV
, 0.000/DV
,
134 0.000/DV
, 0.000/DV
, 0.000/DV
, 0.000/DV
,
135 0.000/DV
, 0.750/DV
, 1.125/DV
, 1.500/DV
,
136 1.875/DV
, 2.250/DV
, 2.625/DV
, 3.000/DV
,
138 0.000/DV
, 0.000/DV
, 0.000/DV
, 0.000/DV
,
139 0.000/DV
, 1.125/DV
, 1.875/DV
, 2.625/DV
,
140 3.000/DV
, 3.750/DV
, 4.125/DV
, 4.500/DV
,
141 4.875/DV
, 5.250/DV
, 5.625/DV
, 6.000/DV
,
143 0.000/DV
, 0.000/DV
, 0.000/DV
, 1.875/DV
,
144 3.000/DV
, 4.125/DV
, 4.875/DV
, 5.625/DV
,
145 6.000/DV
, 6.750/DV
, 7.125/DV
, 7.500/DV
,
146 7.875/DV
, 8.250/DV
, 8.625/DV
, 9.000/DV
,
148 0.000/DV
, 0.000/DV
, 3.000/DV
, 4.875/DV
,
149 6.000/DV
, 7.125/DV
, 7.875/DV
, 8.625/DV
,
150 9.000/DV
, 9.750/DV
,10.125/DV
,10.500/DV
,
151 10.875/DV
,11.250/DV
,11.625/DV
,12.000/DV
,
153 0.000/DV
, 3.000/DV
, 6.000/DV
, 7.875/DV
,
154 9.000/DV
,10.125/DV
,10.875/DV
,11.625/DV
,
155 12.000/DV
,12.750/DV
,13.125/DV
,13.500/DV
,
156 13.875/DV
,14.250/DV
,14.625/DV
,15.000/DV
,
158 0.000/DV
, 6.000/DV
, 9.000/DV
,10.875/DV
,
159 12.000/DV
,13.125/DV
,13.875/DV
,14.625/DV
,
160 15.000/DV
,15.750/DV
,16.125/DV
,16.500/DV
,
161 16.875/DV
,17.250/DV
,17.625/DV
,18.000/DV
,
163 0.000/DV
, 9.000/DV
,12.000/DV
,13.875/DV
,
164 15.000/DV
,16.125/DV
,16.875/DV
,17.625/DV
,
165 18.000/DV
,18.750/DV
,19.125/DV
,19.500/DV
,
166 19.875/DV
,20.250/DV
,20.625/DV
,21.000/DV
170 /* sustain lebel table (3db per step) */
171 /* 0 - 15: 0, 3, 6, 9,12,15,18,21,24,27,30,33,36,39,42,93 (dB)*/
172 #define SC(db) (db*((3/EG_STEP)*(1<<ENV_BITS)))+EG_DST
173 static const int32_t SL_TABLE
[16]={
174 SC( 0),SC( 1),SC( 2),SC(3 ),SC(4 ),SC(5 ),SC(6 ),SC( 7),
175 SC( 8),SC( 9),SC(10),SC(11),SC(12),SC(13),SC(14),SC(31)
179 #define TL_MAX (EG_ENT*2) /* limit(tl + ksr + envelope) + sinwave */
180 /* TotalLevel : 48 24 12 6 3 1.5 0.75 (dB) */
181 /* TL_TABLE[ 0 to TL_MAX ] : plus section */
182 /* TL_TABLE[ TL_MAX to TL_MAX+TL_MAX-1 ] : minus section */
183 static int32_t *TL_TABLE
;
185 /* pointers to TL_TABLE with sinwave output offset */
186 static int32_t **SIN_TABLE
;
189 static int32_t *AMS_TABLE
;
190 static int32_t *VIB_TABLE
;
192 /* envelope output curve table */
193 /* attack + decay + OFF */
194 static int32_t ENV_CURVE
[2*EG_ENT
+1];
198 static const uint32_t MUL_TABLE
[16]= {
199 /* 1/2, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15 */
200 0.50*ML
, 1.00*ML
, 2.00*ML
, 3.00*ML
, 4.00*ML
, 5.00*ML
, 6.00*ML
, 7.00*ML
,
201 8.00*ML
, 9.00*ML
,10.00*ML
,10.00*ML
,12.00*ML
,12.00*ML
,15.00*ML
,15.00*ML
205 /* dummy attack / decay rate ( when rate == 0 ) */
206 static int32_t RATE_0
[16]=
207 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
209 /* -------------------- static state --------------------- */
211 /* lock level of common table */
212 static int num_lock
= 0;
215 static void *cur_chip
= NULL
; /* current chip point */
216 /* currenct chip state */
217 /* static OPLSAMPLE *bufL,*bufR; */
220 static OPL_SLOT
*SLOT7_1
, *SLOT7_2
, *SLOT8_1
, *SLOT8_2
;
222 static int32_t outd
[1];
225 static int32_t *ams_table
;
226 static int32_t *vib_table
;
227 static int32_t amsIncr
;
228 static int32_t vibIncr
;
229 static int32_t feedback2
; /* connect for SLOT 2 */
231 /* log output level */
232 #define LOG_ERR 3 /* ERROR */
233 #define LOG_WAR 2 /* WARNING */
234 #define LOG_INF 1 /* INFORMATION */
236 //#define LOG_LEVEL LOG_INF
237 #define LOG_LEVEL LOG_ERR
239 //#define LOG(n,x) if( (n)>=LOG_LEVEL ) logerror x
242 /* --------------------- subroutines --------------------- */
244 static inline int Limit( int val
, int max
, int min
) {
247 else if ( val
< min
)
253 /* status set and IRQ handling */
254 static inline void OPL_STATUS_SET(FM_OPL
*OPL
,int flag
)
256 /* set status flag */
258 if(!(OPL
->status
& 0x80))
260 if(OPL
->status
& OPL
->statusmask
)
267 /* status reset and IRQ handling */
268 static inline void OPL_STATUS_RESET(FM_OPL
*OPL
,int flag
)
270 /* reset status flag */
272 if((OPL
->status
& 0x80))
274 if (!(OPL
->status
& OPL
->statusmask
) )
282 static inline void OPL_STATUSMASK_SET(FM_OPL
*OPL
,int flag
)
284 OPL
->statusmask
= flag
;
285 /* IRQ handling check */
286 OPL_STATUS_SET(OPL
,0);
287 OPL_STATUS_RESET(OPL
,0);
290 /* ----- key on ----- */
291 static inline void OPL_KEYON(OPL_SLOT
*SLOT
)
293 /* sin wave restart */
296 SLOT
->evm
= ENV_MOD_AR
;
297 SLOT
->evs
= SLOT
->evsa
;
301 /* ----- key off ----- */
302 static inline void OPL_KEYOFF(OPL_SLOT
*SLOT
)
304 if( SLOT
->evm
> ENV_MOD_RR
)
306 /* set envelope counter from envleope output */
307 SLOT
->evm
= ENV_MOD_RR
;
308 if( !(SLOT
->evc
&EG_DST
) )
309 //SLOT->evc = (ENV_CURVE[SLOT->evc>>ENV_BITS]<<ENV_BITS) + EG_DST;
312 SLOT
->evs
= SLOT
->evsr
;
316 /* ---------- calcrate Envelope Generator & Phase Generator ---------- */
317 /* return : envelope output */
318 static inline uint32_t OPL_CALC_SLOT( OPL_SLOT
*SLOT
)
320 /* calcrate envelope generator */
321 if( (SLOT
->evc
+=SLOT
->evs
) >= SLOT
->eve
)
324 case ENV_MOD_AR
: /* ATTACK -> DECAY1 */
326 SLOT
->evm
= ENV_MOD_DR
;
328 SLOT
->eve
= SLOT
->SL
;
329 SLOT
->evs
= SLOT
->evsd
;
331 case ENV_MOD_DR
: /* DECAY -> SL or RR */
332 SLOT
->evc
= SLOT
->SL
;
340 SLOT
->evm
= ENV_MOD_RR
;
341 SLOT
->evs
= SLOT
->evsr
;
344 case ENV_MOD_RR
: /* RR -> OFF */
346 SLOT
->eve
= EG_OFF
+1;
351 /* calcrate envelope */
352 return SLOT
->TLL
+ENV_CURVE
[SLOT
->evc
>>ENV_BITS
]+(SLOT
->ams
? ams
: 0);
355 /* set algorithm connection */
356 static void set_algorithm( OPL_CH
*CH
)
358 int32_t *carrier
= &outd
[0];
359 CH
->connect1
= CH
->CON
? carrier
: &feedback2
;
360 CH
->connect2
= carrier
;
363 /* ---------- frequency counter for operater update ---------- */
364 static inline void CALC_FCSLOT(OPL_CH
*CH
,OPL_SLOT
*SLOT
)
368 /* frequency step counter */
369 SLOT
->Incr
= CH
->fc
* SLOT
->mul
;
370 ksr
= CH
->kcode
>> SLOT
->KSR
;
372 if( SLOT
->ksr
!= ksr
)
375 /* attack , decay rate recalcration */
376 SLOT
->evsa
= SLOT
->AR
[ksr
];
377 SLOT
->evsd
= SLOT
->DR
[ksr
];
378 SLOT
->evsr
= SLOT
->RR
[ksr
];
380 SLOT
->TLL
= SLOT
->TL
+ (CH
->ksl_base
>>SLOT
->ksl
);
383 /* set multi,am,vib,EG-TYP,KSR,mul */
384 static inline void set_mul(FM_OPL
*OPL
,int slot
,int v
)
386 OPL_CH
*CH
= &OPL
->P_CH
[slot
/2];
387 OPL_SLOT
*SLOT
= &CH
->SLOT
[slot
&1];
389 SLOT
->mul
= MUL_TABLE
[v
&0x0f];
390 SLOT
->KSR
= (v
&0x10) ? 0 : 2;
391 SLOT
->eg_typ
= (v
&0x20)>>5;
392 SLOT
->vib
= (v
&0x40);
393 SLOT
->ams
= (v
&0x80);
394 CALC_FCSLOT(CH
,SLOT
);
398 static inline void set_ksl_tl(FM_OPL
*OPL
,int slot
,int v
)
400 OPL_CH
*CH
= &OPL
->P_CH
[slot
/2];
401 OPL_SLOT
*SLOT
= &CH
->SLOT
[slot
&1];
402 int ksl
= v
>>6; /* 0 / 1.5 / 3 / 6 db/OCT */
404 SLOT
->ksl
= ksl
? 3-ksl
: 31;
405 SLOT
->TL
= (v
&0x3f)*(0.75/EG_STEP
); /* 0.75db step */
407 if( !(OPL
->mode
&0x80) )
408 { /* not CSM latch total level */
409 SLOT
->TLL
= SLOT
->TL
+ (CH
->ksl_base
>>SLOT
->ksl
);
413 /* set attack rate & decay rate */
414 static inline void set_ar_dr(FM_OPL
*OPL
,int slot
,int v
)
416 OPL_CH
*CH
= &OPL
->P_CH
[slot
/2];
417 OPL_SLOT
*SLOT
= &CH
->SLOT
[slot
&1];
421 SLOT
->AR
= ar
? &OPL
->AR_TABLE
[ar
<<2] : RATE_0
;
422 SLOT
->evsa
= SLOT
->AR
[SLOT
->ksr
];
423 if( SLOT
->evm
== ENV_MOD_AR
) SLOT
->evs
= SLOT
->evsa
;
425 SLOT
->DR
= dr
? &OPL
->DR_TABLE
[dr
<<2] : RATE_0
;
426 SLOT
->evsd
= SLOT
->DR
[SLOT
->ksr
];
427 if( SLOT
->evm
== ENV_MOD_DR
) SLOT
->evs
= SLOT
->evsd
;
430 /* set sustain level & release rate */
431 static inline void set_sl_rr(FM_OPL
*OPL
,int slot
,int v
)
433 OPL_CH
*CH
= &OPL
->P_CH
[slot
/2];
434 OPL_SLOT
*SLOT
= &CH
->SLOT
[slot
&1];
438 SLOT
->SL
= SL_TABLE
[sl
];
439 if( SLOT
->evm
== ENV_MOD_DR
) SLOT
->eve
= SLOT
->SL
;
440 SLOT
->RR
= &OPL
->DR_TABLE
[rr
<<2];
441 SLOT
->evsr
= SLOT
->RR
[SLOT
->ksr
];
442 if( SLOT
->evm
== ENV_MOD_RR
) SLOT
->evs
= SLOT
->evsr
;
445 /* operator output calcrator */
446 #define OP_OUT(slot,env,con) slot->wavetable[((slot->Cnt+con)/(0x1000000/SIN_ENT))&(SIN_ENT-1)][env]
447 /* ---------- calcrate one of channel ---------- */
448 static inline void OPL_CALC_CH( OPL_CH
*CH
)
455 SLOT
= &CH
->SLOT
[SLOT1
];
456 env_out
=OPL_CALC_SLOT(SLOT
);
457 if( env_out
< EG_ENT
-1 )
460 if(SLOT
->vib
) SLOT
->Cnt
+= (SLOT
->Incr
*vib
/VIB_RATE
);
461 else SLOT
->Cnt
+= SLOT
->Incr
;
465 int feedback1
= (CH
->op1_out
[0]+CH
->op1_out
[1])>>CH
->FB
;
466 CH
->op1_out
[1] = CH
->op1_out
[0];
467 *CH
->connect1
+= CH
->op1_out
[0] = OP_OUT(SLOT
,env_out
,feedback1
);
471 *CH
->connect1
+= OP_OUT(SLOT
,env_out
,0);
475 CH
->op1_out
[1] = CH
->op1_out
[0];
479 SLOT
= &CH
->SLOT
[SLOT2
];
480 env_out
=OPL_CALC_SLOT(SLOT
);
481 if( env_out
< EG_ENT
-1 )
484 if(SLOT
->vib
) SLOT
->Cnt
+= (SLOT
->Incr
*vib
/VIB_RATE
);
485 else SLOT
->Cnt
+= SLOT
->Incr
;
487 outd
[0] += OP_OUT(SLOT
,env_out
, feedback2
);
491 /* ---------- calcrate rhythm block ---------- */
492 #define WHITE_NOISE_db 6.0
493 static inline void OPL_CALC_RH( OPL_CH
*CH
)
495 uint32_t env_tam
,env_sd
,env_top
,env_hh
;
496 int whitenoise
= (rand()&1)*(WHITE_NOISE_db
/EG_STEP
);
502 /* BD : same as FM serial mode and output level is large */
505 SLOT
= &CH
[6].SLOT
[SLOT1
];
506 env_out
=OPL_CALC_SLOT(SLOT
);
507 if( env_out
< EG_ENT
-1 )
510 if(SLOT
->vib
) SLOT
->Cnt
+= (SLOT
->Incr
*vib
/VIB_RATE
);
511 else SLOT
->Cnt
+= SLOT
->Incr
;
515 int feedback1
= (CH
[6].op1_out
[0]+CH
[6].op1_out
[1])>>CH
[6].FB
;
516 CH
[6].op1_out
[1] = CH
[6].op1_out
[0];
517 feedback2
= CH
[6].op1_out
[0] = OP_OUT(SLOT
,env_out
,feedback1
);
521 feedback2
= OP_OUT(SLOT
,env_out
,0);
526 CH
[6].op1_out
[1] = CH
[6].op1_out
[0];
527 CH
[6].op1_out
[0] = 0;
530 SLOT
= &CH
[6].SLOT
[SLOT2
];
531 env_out
=OPL_CALC_SLOT(SLOT
);
532 if( env_out
< EG_ENT
-1 )
535 if(SLOT
->vib
) SLOT
->Cnt
+= (SLOT
->Incr
*vib
/VIB_RATE
);
536 else SLOT
->Cnt
+= SLOT
->Incr
;
538 outd
[0] += OP_OUT(SLOT
,env_out
, feedback2
)*2;
541 // SD (17) = mul14[fnum7] + white noise
542 // TAM (15) = mul15[fnum8]
543 // TOP (18) = fnum6(mul18[fnum8]+whitenoise)
544 // HH (14) = fnum7(mul18[fnum8]+whitenoise) + white noise
545 env_sd
=OPL_CALC_SLOT(SLOT7_2
) + whitenoise
;
546 env_tam
=OPL_CALC_SLOT(SLOT8_1
);
547 env_top
=OPL_CALC_SLOT(SLOT8_2
);
548 env_hh
=OPL_CALC_SLOT(SLOT7_1
) + whitenoise
;
551 if(SLOT7_1
->vib
) SLOT7_1
->Cnt
+= (2*SLOT7_1
->Incr
*vib
/VIB_RATE
);
552 else SLOT7_1
->Cnt
+= 2*SLOT7_1
->Incr
;
553 if(SLOT7_2
->vib
) SLOT7_2
->Cnt
+= ((CH
[7].fc
*8)*vib
/VIB_RATE
);
554 else SLOT7_2
->Cnt
+= (CH
[7].fc
*8);
555 if(SLOT8_1
->vib
) SLOT8_1
->Cnt
+= (SLOT8_1
->Incr
*vib
/VIB_RATE
);
556 else SLOT8_1
->Cnt
+= SLOT8_1
->Incr
;
557 if(SLOT8_2
->vib
) SLOT8_2
->Cnt
+= ((CH
[8].fc
*48)*vib
/VIB_RATE
);
558 else SLOT8_2
->Cnt
+= (CH
[8].fc
*48);
560 tone8
= OP_OUT(SLOT8_2
,whitenoise
,0 );
563 if( env_sd
< EG_ENT
-1 )
564 outd
[0] += OP_OUT(SLOT7_1
,env_sd
, 0)*8;
566 if( env_tam
< EG_ENT
-1 )
567 outd
[0] += OP_OUT(SLOT8_1
,env_tam
, 0)*2;
569 if( env_top
< EG_ENT
-1 )
570 outd
[0] += OP_OUT(SLOT7_2
,env_top
,tone8
)*2;
572 if( env_hh
< EG_ENT
-1 )
573 outd
[0] += OP_OUT(SLOT7_2
,env_hh
,tone8
)*2;
576 /* ----------- initialize time tabls ----------- */
577 static void init_timetables( FM_OPL
*OPL
, int ARRATE
, int DRRATE
)
582 /* make attack rate & decay rate tables */
583 for (i
= 0;i
< 4;i
++) OPL
->AR_TABLE
[i
] = OPL
->DR_TABLE
[i
] = 0;
584 for (i
= 4;i
<= 60;i
++){
585 rate
= OPL
->freqbase
; /* frequency rate */
586 if( i
< 60 ) rate
*= 1.0+(i
&3)*0.25; /* b0-1 : x1 , x1.25 , x1.5 , x1.75 */
587 rate
*= 1<<((i
>>2)-1); /* b2-5 : shift bit */
588 rate
*= (double)(EG_ENT
<<ENV_BITS
);
589 OPL
->AR_TABLE
[i
] = rate
/ ARRATE
;
590 OPL
->DR_TABLE
[i
] = rate
/ DRRATE
;
592 for (i
= 60; i
< ARRAY_SIZE(OPL
->AR_TABLE
); i
++)
594 OPL
->AR_TABLE
[i
] = EG_AED
-1;
595 OPL
->DR_TABLE
[i
] = OPL
->DR_TABLE
[60];
598 for (i
= 0;i
< 64 ;i
++){ /* make for overflow area */
599 LOG(LOG_WAR
, ("rate %2d , ar %f ms , dr %f ms\n", i
,
600 ((double)(EG_ENT
<<ENV_BITS
) / OPL
->AR_TABLE
[i
]) * (1000.0 / OPL
->rate
),
601 ((double)(EG_ENT
<<ENV_BITS
) / OPL
->DR_TABLE
[i
]) * (1000.0 / OPL
->rate
) ));
606 /* ---------- generic table initialize ---------- */
607 static int OPLOpenTable( void )
614 /* allocate dynamic tables */
615 if( (TL_TABLE
= malloc(TL_MAX
*2*sizeof(int32_t))) == NULL
)
617 if( (SIN_TABLE
= malloc(SIN_ENT
*4 *sizeof(int32_t *))) == NULL
)
622 if( (AMS_TABLE
= malloc(AMS_ENT
*2 *sizeof(int32_t))) == NULL
)
628 if( (VIB_TABLE
= malloc(VIB_ENT
*2 *sizeof(int32_t))) == NULL
)
635 /* make total level table */
636 for (t
= 0;t
< EG_ENT
-1 ;t
++){
637 rate
= ((1<<TL_BITS
)-1)/pow(10,EG_STEP
*t
/20); /* dB -> voltage */
638 TL_TABLE
[ t
] = (int)rate
;
639 TL_TABLE
[TL_MAX
+t
] = -TL_TABLE
[t
];
640 /* LOG(LOG_INF,("TotalLevel(%3d) = %x\n",t,TL_TABLE[t]));*/
642 /* fill volume off area */
643 for ( t
= EG_ENT
-1; t
< TL_MAX
;t
++){
644 TL_TABLE
[t
] = TL_TABLE
[TL_MAX
+t
] = 0;
647 /* make sinwave table (total level offet) */
648 /* degree 0 = degree 180 = off */
649 SIN_TABLE
[0] = SIN_TABLE
[SIN_ENT
/2] = &TL_TABLE
[EG_ENT
-1];
650 for (s
= 1;s
<= SIN_ENT
/4;s
++){
651 pom
= sin(2*PI
*s
/SIN_ENT
); /* sin */
652 pom
= 20*log10(1/pom
); /* decibel */
653 j
= pom
/ EG_STEP
; /* TL_TABLE steps */
655 /* degree 0 - 90 , degree 180 - 90 : plus section */
656 SIN_TABLE
[ s
] = SIN_TABLE
[SIN_ENT
/2-s
] = &TL_TABLE
[j
];
657 /* degree 180 - 270 , degree 360 - 270 : minus section */
658 SIN_TABLE
[SIN_ENT
/2+s
] = SIN_TABLE
[SIN_ENT
-s
] = &TL_TABLE
[TL_MAX
+j
];
659 /* LOG(LOG_INF,("sin(%3d) = %f:%f db\n",s,pom,(double)j * EG_STEP));*/
661 for (s
= 0;s
< SIN_ENT
;s
++)
663 SIN_TABLE
[SIN_ENT
*1+s
] = s
<(SIN_ENT
/2) ? SIN_TABLE
[s
] : &TL_TABLE
[EG_ENT
];
664 SIN_TABLE
[SIN_ENT
*2+s
] = SIN_TABLE
[s
% (SIN_ENT
/2)];
665 SIN_TABLE
[SIN_ENT
*3+s
] = (s
/(SIN_ENT
/4))&1 ? &TL_TABLE
[EG_ENT
] : SIN_TABLE
[SIN_ENT
*2+s
];
668 /* envelope counter -> envelope output table */
669 for (i
=0; i
<EG_ENT
; i
++)
672 pom
= pow( ((double)(EG_ENT
-1-i
)/EG_ENT
) , 8 ) * EG_ENT
;
673 /* if( pom >= EG_ENT ) pom = EG_ENT-1; */
674 ENV_CURVE
[i
] = (int)pom
;
675 /* DECAY ,RELEASE curve */
676 ENV_CURVE
[(EG_DST
>>ENV_BITS
)+i
]= i
;
679 ENV_CURVE
[EG_OFF
>>ENV_BITS
]= EG_ENT
-1;
680 /* make LFO ams table */
681 for (i
=0; i
<AMS_ENT
; i
++)
683 pom
= (1.0+sin(2*PI
*i
/AMS_ENT
))/2; /* sin */
684 AMS_TABLE
[i
] = (1.0/EG_STEP
)*pom
; /* 1dB */
685 AMS_TABLE
[AMS_ENT
+i
] = (4.8/EG_STEP
)*pom
; /* 4.8dB */
687 /* make LFO vibrate table */
688 for (i
=0; i
<VIB_ENT
; i
++)
690 /* 100cent = 1seminote = 6% ?? */
691 pom
= (double)VIB_RATE
*0.06*sin(2*PI
*i
/VIB_ENT
); /* +-100sect step */
692 VIB_TABLE
[i
] = VIB_RATE
+ (pom
*0.07); /* +- 7cent */
693 VIB_TABLE
[VIB_ENT
+i
] = VIB_RATE
+ (pom
*0.14); /* +-14cent */
694 /* LOG(LOG_INF,("vib %d=%d\n",i,VIB_TABLE[VIB_ENT+i])); */
700 static void OPLCloseTable( void )
708 /* CSM Key Control */
709 static inline void CSMKeyControll(OPL_CH
*CH
)
711 OPL_SLOT
*slot1
= &CH
->SLOT
[SLOT1
];
712 OPL_SLOT
*slot2
= &CH
->SLOT
[SLOT2
];
716 /* total level latch */
717 slot1
->TLL
= slot1
->TL
+ (CH
->ksl_base
>>slot1
->ksl
);
718 slot1
->TLL
= slot1
->TL
+ (CH
->ksl_base
>>slot1
->ksl
);
720 CH
->op1_out
[0] = CH
->op1_out
[1] = 0;
725 /* ---------- opl initialize ---------- */
726 static void OPL_initialize(FM_OPL
*OPL
)
731 OPL
->freqbase
= (OPL
->rate
) ? ((double)OPL
->clock
/ OPL
->rate
) / 72 : 0;
732 /* Timer base time */
733 OPL
->TimerBase
= 1.0/((double)OPL
->clock
/ 72.0 );
734 /* make time tables */
735 init_timetables( OPL
, OPL_ARRATE
, OPL_DRRATE
);
736 /* make fnumber -> increment counter table */
737 for( fn
=0 ; fn
< 1024 ; fn
++ )
739 OPL
->FN_TABLE
[fn
] = OPL
->freqbase
* fn
* FREQ_RATE
* (1<<7) / 2;
742 OPL
->amsIncr
= OPL
->rate
? (double)AMS_ENT
*(1<<AMS_SHIFT
) / OPL
->rate
* 3.7 * ((double)OPL
->clock
/3600000) : 0;
743 OPL
->vibIncr
= OPL
->rate
? (double)VIB_ENT
*(1<<VIB_SHIFT
) / OPL
->rate
* 6.4 * ((double)OPL
->clock
/3600000) : 0;
746 /* ---------- write a OPL registers ---------- */
747 static void OPLWriteReg(FM_OPL
*OPL
, int r
, int v
)
755 case 0x00: /* 00-1f:control */
759 /* wave selector enable */
760 OPL
->wavesel
= v
&0x20;
763 /* preset compatible mode */
765 for(c
=0;c
<OPL
->max_ch
;c
++)
767 OPL
->P_CH
[c
].SLOT
[SLOT1
].wavetable
= &SIN_TABLE
[0];
768 OPL
->P_CH
[c
].SLOT
[SLOT2
].wavetable
= &SIN_TABLE
[0];
772 case 0x02: /* Timer 1 */
773 OPL
->T
[0] = (256-v
)*4;
775 case 0x03: /* Timer 2 */
776 OPL
->T
[1] = (256-v
)*16;
778 case 0x04: /* IRQ clear / mask and Timer enable */
780 { /* IRQ flag clear */
781 OPL_STATUS_RESET(OPL
,0x7f);
784 { /* set IRQ mask ,timer enable*/
786 uint8_t st2
= (v
>>1)&1;
787 /* IRQRST,T1MSK,t2MSK,EOSMSK,BRMSK,x,ST2,ST1 */
788 OPL_STATUS_RESET(OPL
,v
&0x78);
789 OPL_STATUSMASK_SET(OPL
,((~v
)&0x78)|0x01);
791 if(OPL
->st
[1] != st2
)
793 double interval
= st2
? (double)OPL
->T
[1]*OPL
->TimerBase
: 0.0;
795 if (OPL
->TimerHandler
) (OPL
->TimerHandler
)(OPL
->TimerParam
+1,interval
);
798 if(OPL
->st
[0] != st1
)
800 double interval
= st1
? (double)OPL
->T
[0]*OPL
->TimerBase
: 0.0;
802 if (OPL
->TimerHandler
) (OPL
->TimerHandler
)(OPL
->TimerParam
+0,interval
);
808 case 0x20: /* am,vib,ksr,eg type,mul */
809 slot
= slot_array
[r
&0x1f];
810 if(slot
== -1) return;
814 slot
= slot_array
[r
&0x1f];
815 if(slot
== -1) return;
816 set_ksl_tl(OPL
,slot
,v
);
819 slot
= slot_array
[r
&0x1f];
820 if(slot
== -1) return;
821 set_ar_dr(OPL
,slot
,v
);
824 slot
= slot_array
[r
&0x1f];
825 if(slot
== -1) return;
826 set_sl_rr(OPL
,slot
,v
);
832 /* amsep,vibdep,r,bd,sd,tom,tc,hh */
834 uint8_t rkey
= OPL
->rhythm
^v
;
835 OPL
->ams_table
= &AMS_TABLE
[v
&0x80 ? AMS_ENT
: 0];
836 OPL
->vib_table
= &VIB_TABLE
[v
&0x40 ? VIB_ENT
: 0];
837 OPL
->rhythm
= v
&0x3f;
841 usrintf_showmessage("OPL Rhythm mode select");
848 OPL
->P_CH
[6].op1_out
[0] = OPL
->P_CH
[6].op1_out
[1] = 0;
849 OPL_KEYON(&OPL
->P_CH
[6].SLOT
[SLOT1
]);
850 OPL_KEYON(&OPL
->P_CH
[6].SLOT
[SLOT2
]);
854 OPL_KEYOFF(&OPL
->P_CH
[6].SLOT
[SLOT1
]);
855 OPL_KEYOFF(&OPL
->P_CH
[6].SLOT
[SLOT2
]);
861 if(v
&0x08) OPL_KEYON(&OPL
->P_CH
[7].SLOT
[SLOT2
]);
862 else OPL_KEYOFF(&OPL
->P_CH
[7].SLOT
[SLOT2
]);
863 }/* TAM key on/off */
866 if(v
&0x04) OPL_KEYON(&OPL
->P_CH
[8].SLOT
[SLOT1
]);
867 else OPL_KEYOFF(&OPL
->P_CH
[8].SLOT
[SLOT1
]);
869 /* TOP-CY key on/off */
872 if(v
&0x02) OPL_KEYON(&OPL
->P_CH
[8].SLOT
[SLOT2
]);
873 else OPL_KEYOFF(&OPL
->P_CH
[8].SLOT
[SLOT2
]);
878 if(v
&0x01) OPL_KEYON(&OPL
->P_CH
[7].SLOT
[SLOT1
]);
879 else OPL_KEYOFF(&OPL
->P_CH
[7].SLOT
[SLOT1
]);
885 /* keyon,block,fnum */
886 if( (r
&0x0f) > 8) return;
887 CH
= &OPL
->P_CH
[r
&0x0f];
890 block_fnum
= (CH
->block_fnum
&0x1f00) | v
;
894 int keyon
= (v
>>5)&1;
895 block_fnum
= ((v
&0x1f)<<8) | (CH
->block_fnum
&0xff);
896 if(CH
->keyon
!= keyon
)
898 if( (CH
->keyon
=keyon
) )
900 CH
->op1_out
[0] = CH
->op1_out
[1] = 0;
901 OPL_KEYON(&CH
->SLOT
[SLOT1
]);
902 OPL_KEYON(&CH
->SLOT
[SLOT2
]);
906 OPL_KEYOFF(&CH
->SLOT
[SLOT1
]);
907 OPL_KEYOFF(&CH
->SLOT
[SLOT2
]);
912 if(CH
->block_fnum
!= block_fnum
)
914 int blockRv
= 7-(block_fnum
>>10);
915 int fnum
= block_fnum
&0x3ff;
916 CH
->block_fnum
= block_fnum
;
918 CH
->ksl_base
= KSL_TABLE
[block_fnum
>>6];
919 CH
->fc
= OPL
->FN_TABLE
[fnum
]>>blockRv
;
920 CH
->kcode
= CH
->block_fnum
>>9;
921 if( (OPL
->mode
&0x40) && CH
->block_fnum
&0x100) CH
->kcode
|=1;
922 CALC_FCSLOT(CH
,&CH
->SLOT
[SLOT1
]);
923 CALC_FCSLOT(CH
,&CH
->SLOT
[SLOT2
]);
928 if( (r
&0x0f) > 8) return;
929 CH
= &OPL
->P_CH
[r
&0x0f];
931 int feedback
= (v
>>1)&7;
932 CH
->FB
= feedback
? (8+1) - feedback
: 0;
937 case 0xe0: /* wave type */
938 slot
= slot_array
[r
&0x1f];
939 if(slot
== -1) return;
940 CH
= &OPL
->P_CH
[slot
/2];
943 /* LOG(LOG_INF,("OPL SLOT %d wave select %d\n",slot,v&3)); */
944 CH
->SLOT
[slot
&1].wavetable
= &SIN_TABLE
[(v
&0x03)*SIN_ENT
];
950 /* lock/unlock for common table */
951 static int OPL_LockTable(void)
954 if(num_lock
>1) return 0;
957 /* allocate total level table (128kb space) */
958 if( !OPLOpenTable() )
966 static void OPL_UnLockTable(void)
968 if(num_lock
) num_lock
--;
975 /*******************************************************************************/
976 /* YM3812 local section */
977 /*******************************************************************************/
979 /* ---------- update one of chip ----------- */
980 void YM3812UpdateOne(FM_OPL
*OPL
, int16_t *buffer
, int length
)
984 int16_t *buf
= buffer
;
985 uint32_t amsCnt
= OPL
->amsCnt
;
986 uint32_t vibCnt
= OPL
->vibCnt
;
987 uint8_t rhythm
= OPL
->rhythm
&0x20;
990 if( (void *)OPL
!= cur_chip
){
991 cur_chip
= (void *)OPL
;
992 /* channel pointers */
996 SLOT7_1
= &S_CH
[7].SLOT
[SLOT1
];
997 SLOT7_2
= &S_CH
[7].SLOT
[SLOT2
];
998 SLOT8_1
= &S_CH
[8].SLOT
[SLOT1
];
999 SLOT8_2
= &S_CH
[8].SLOT
[SLOT2
];
1001 amsIncr
= OPL
->amsIncr
;
1002 vibIncr
= OPL
->vibIncr
;
1003 ams_table
= OPL
->ams_table
;
1004 vib_table
= OPL
->vib_table
;
1006 R_CH
= rhythm
? &S_CH
[6] : E_CH
;
1007 for( i
=0; i
< length
; i
++ )
1009 /* channel A channel B channel C */
1011 ams
= ams_table
[(amsCnt
+=amsIncr
)>>AMS_SHIFT
];
1012 vib
= vib_table
[(vibCnt
+=vibIncr
)>>VIB_SHIFT
];
1015 for(CH
=S_CH
; CH
< R_CH
; CH
++)
1021 data
= Limit( outd
[0] , OPL_MAXOUT
, OPL_MINOUT
);
1022 /* store to sound buffer */
1023 buf
[i
] = data
>> OPL_OUTSB
;
1026 OPL
->amsCnt
= amsCnt
;
1027 OPL
->vibCnt
= vibCnt
;
1028 #ifdef OPL_OUTPUT_LOG
1031 for(opl_dbg_chip
=0;opl_dbg_chip
<opl_dbg_maxchip
;opl_dbg_chip
++)
1032 if( opl_dbg_opl
[opl_dbg_chip
] == OPL
) break;
1033 fprintf(opl_dbg_fp
,"%c%c%c",0x20+opl_dbg_chip
,length
&0xff,length
/256);
1038 /* ---------- reset one of chip ---------- */
1039 void OPLResetChip(FM_OPL
*OPL
)
1045 OPL
->mode
= 0; /* normal mode */
1046 OPL_STATUS_RESET(OPL
,0x7f);
1047 /* reset with register write */
1048 OPLWriteReg(OPL
,0x01,0); /* wabesel disable */
1049 OPLWriteReg(OPL
,0x02,0); /* Timer1 */
1050 OPLWriteReg(OPL
,0x03,0); /* Timer2 */
1051 OPLWriteReg(OPL
,0x04,0); /* IRQ mask clear */
1052 for(i
= 0xff ; i
>= 0x20 ; i
-- ) OPLWriteReg(OPL
,i
,0);
1053 /* reset operator parameter */
1054 for( c
= 0 ; c
< OPL
->max_ch
; c
++ )
1056 OPL_CH
*CH
= &OPL
->P_CH
[c
];
1057 /* OPL->P_CH[c].PAN = OPN_CENTER; */
1058 for(s
= 0 ; s
< 2 ; s
++ )
1061 CH
->SLOT
[s
].wavetable
= &SIN_TABLE
[0];
1062 /* CH->SLOT[s].evm = ENV_MOD_RR; */
1063 CH
->SLOT
[s
].evc
= EG_OFF
;
1064 CH
->SLOT
[s
].eve
= EG_OFF
+1;
1065 CH
->SLOT
[s
].evs
= 0;
1070 /* ---------- Create one of vietual YM3812 ---------- */
1071 /* 'rate' is sampling rate and 'bufsiz' is the size of the */
1072 FM_OPL
*OPLCreate(int clock
, int rate
)
1077 int max_ch
= 9; /* normaly 9 channels */
1079 if( OPL_LockTable() ==-1) return NULL
;
1080 /* allocate OPL state space */
1081 state_size
= sizeof(FM_OPL
);
1082 state_size
+= sizeof(OPL_CH
)*max_ch
;
1083 /* allocate memory block */
1084 ptr
= malloc(state_size
);
1085 if(ptr
==NULL
) return NULL
;
1087 memset(ptr
,0,state_size
);
1088 OPL
= (FM_OPL
*)ptr
; ptr
+=sizeof(FM_OPL
);
1089 OPL
->P_CH
= (OPL_CH
*)ptr
; ptr
+=sizeof(OPL_CH
)*max_ch
;
1090 /* set channel state pointer */
1093 OPL
->max_ch
= max_ch
;
1094 /* init grobal tables */
1095 OPL_initialize(OPL
);
1098 #ifdef OPL_OUTPUT_LOG
1101 opl_dbg_fp
= fopen("opllog.opl","wb");
1102 opl_dbg_maxchip
= 0;
1106 opl_dbg_opl
[opl_dbg_maxchip
] = OPL
;
1107 fprintf(opl_dbg_fp
,"%c%c%c%c%c%c",0x00+opl_dbg_maxchip
,
1111 (clock
/0x10000)&0xff,
1112 (clock
/0x1000000)&0xff);
1119 /* ---------- Destroy one of vietual YM3812 ---------- */
1120 void OPLDestroy(FM_OPL
*OPL
)
1122 #ifdef OPL_OUTPUT_LOG
1133 /* ---------- Option handlers ---------- */
1135 void OPLSetTimerHandler(FM_OPL
*OPL
,OPL_TIMERHANDLER TimerHandler
,int channelOffset
)
1137 OPL
->TimerHandler
= TimerHandler
;
1138 OPL
->TimerParam
= channelOffset
;
1141 /* ---------- YM3812 I/O interface ---------- */
1142 int OPLWrite(FM_OPL
*OPL
,int a
,int v
)
1145 { /* address port */
1146 OPL
->address
= v
& 0xff;
1150 #ifdef OPL_OUTPUT_LOG
1153 for(opl_dbg_chip
=0;opl_dbg_chip
<opl_dbg_maxchip
;opl_dbg_chip
++)
1154 if( opl_dbg_opl
[opl_dbg_chip
] == OPL
) break;
1155 fprintf(opl_dbg_fp
,"%c%c%c",0x10+opl_dbg_chip
,OPL
->address
,v
);
1158 OPLWriteReg(OPL
,OPL
->address
,v
);
1160 return OPL
->status
>>7;
1163 unsigned char OPLRead(FM_OPL
*OPL
,int a
)
1167 return OPL
->status
& (OPL
->statusmask
|0x80);
1170 switch(OPL
->address
)
1172 case 0x05: /* KeyBoard IN */
1175 case 0x0f: /* ADPCM-DATA */
1178 case 0x19: /* I/O DATA */
1180 case 0x1a: /* PCM-DATA */
1186 int OPLTimerOver(FM_OPL
*OPL
,int c
)
1190 OPL_STATUS_SET(OPL
,0x20);
1194 OPL_STATUS_SET(OPL
,0x40);
1195 /* CSM mode key,TL control */
1196 if( OPL
->mode
& 0x80 )
1197 { /* CSM mode total level latch and auto key on */
1200 CSMKeyControll( &OPL
->P_CH
[ch
] );
1204 if (OPL
->TimerHandler
) (OPL
->TimerHandler
)(OPL
->TimerParam
+c
,(double)OPL
->T
[c
]*OPL
->TimerBase
);
1205 return OPL
->status
>>7;