fmopl.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  1. /*
  2. **
  3. ** File: fmopl.c -- software implementation of FM sound generator
  4. **
  5. ** Copyright (C) 1999,2000 Tatsuyuki Satoh , MultiArcadeMachineEmurator development
  6. **
  7. ** Version 0.37a
  8. **
  9. */
  10. /*
  11. preliminary :
  12. Problem :
  13. note:
  14. */
  15. /* This version of fmopl.c is a fork of the MAME one, relicensed under the LGPL.
  16. *
  17. * This library is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU Lesser General Public
  19. * License as published by the Free Software Foundation; either
  20. * version 2.1 of the License, or (at your option) any later version.
  21. *
  22. * This library is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  25. * Lesser General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Lesser General Public
  28. * License along with this library; if not, write to the Free Software
  29. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
  30. */
  31. #define INLINE static inline
  32. #define HAS_YM3812 1
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <stdarg.h>
  37. #include <math.h>
  38. //#include "driver.h" /* use M.A.M.E. */
  39. #include "fmopl.h"
  40. #ifndef PI
  41. #define PI 3.14159265358979323846
  42. #endif
  43. /* -------------------- for debug --------------------- */
  44. /* #define OPL_OUTPUT_LOG */
  45. #ifdef OPL_OUTPUT_LOG
  46. static FILE *opl_dbg_fp = NULL;
  47. static FM_OPL *opl_dbg_opl[16];
  48. static int opl_dbg_maxchip,opl_dbg_chip;
  49. #endif
  50. /* -------------------- preliminary define section --------------------- */
  51. /* attack/decay rate time rate */
  52. #define OPL_ARRATE 141280 /* RATE 4 = 2826.24ms @ 3.6MHz */
  53. #define OPL_DRRATE 1956000 /* RATE 4 = 39280.64ms @ 3.6MHz */
  54. #define DELTAT_MIXING_LEVEL (1) /* DELTA-T ADPCM MIXING LEVEL */
  55. #define FREQ_BITS 24 /* frequency turn */
  56. /* counter bits = 20 , octerve 7 */
  57. #define FREQ_RATE (1<<(FREQ_BITS-20))
  58. #define TL_BITS (FREQ_BITS+2)
  59. /* final output shift , limit minimum and maximum */
  60. #define OPL_OUTSB (TL_BITS+3-16) /* OPL output final shift 16bit */
  61. #define OPL_MAXOUT (0x7fff<<OPL_OUTSB)
  62. #define OPL_MINOUT (-0x8000<<OPL_OUTSB)
  63. /* -------------------- quality selection --------------------- */
  64. /* sinwave entries */
  65. /* used static memory = SIN_ENT * 4 (byte) */
  66. #define SIN_ENT 2048
  67. /* output level entries (envelope,sinwave) */
  68. /* envelope counter lower bits */
  69. #define ENV_BITS 16
  70. /* envelope output entries */
  71. #define EG_ENT 4096
  72. /* used dynamic memory = EG_ENT*4*4(byte)or EG_ENT*6*4(byte) */
  73. /* used static memory = EG_ENT*4 (byte) */
  74. #define EG_OFF ((2*EG_ENT)<<ENV_BITS) /* OFF */
  75. #define EG_DED EG_OFF
  76. #define EG_DST (EG_ENT<<ENV_BITS) /* DECAY START */
  77. #define EG_AED EG_DST
  78. #define EG_AST 0 /* ATTACK START */
  79. #define EG_STEP (96.0/EG_ENT) /* OPL is 0.1875 dB step */
  80. /* LFO table entries */
  81. #define VIB_ENT 512
  82. #define VIB_SHIFT (32-9)
  83. #define AMS_ENT 512
  84. #define AMS_SHIFT (32-9)
  85. #define VIB_RATE 256
  86. /* -------------------- local defines , macros --------------------- */
  87. /* register number to channel number , slot offset */
  88. #define SLOT1 0
  89. #define SLOT2 1
  90. /* envelope phase */
  91. #define ENV_MOD_RR 0x00
  92. #define ENV_MOD_DR 0x01
  93. #define ENV_MOD_AR 0x02
  94. /* -------------------- tables --------------------- */
  95. static const int slot_array[32]=
  96. {
  97. 0, 2, 4, 1, 3, 5,-1,-1,
  98. 6, 8,10, 7, 9,11,-1,-1,
  99. 12,14,16,13,15,17,-1,-1,
  100. -1,-1,-1,-1,-1,-1,-1,-1
  101. };
  102. /* key scale level */
  103. /* table is 3dB/OCT , DV converts this in TL step at 6dB/OCT */
  104. #define DV (EG_STEP/2)
  105. static const UINT32 KSL_TABLE[8*16]=
  106. {
  107. /* OCT 0 */
  108. 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
  109. 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
  110. 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
  111. 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
  112. /* OCT 1 */
  113. 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
  114. 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
  115. 0.000/DV, 0.750/DV, 1.125/DV, 1.500/DV,
  116. 1.875/DV, 2.250/DV, 2.625/DV, 3.000/DV,
  117. /* OCT 2 */
  118. 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
  119. 0.000/DV, 1.125/DV, 1.875/DV, 2.625/DV,
  120. 3.000/DV, 3.750/DV, 4.125/DV, 4.500/DV,
  121. 4.875/DV, 5.250/DV, 5.625/DV, 6.000/DV,
  122. /* OCT 3 */
  123. 0.000/DV, 0.000/DV, 0.000/DV, 1.875/DV,
  124. 3.000/DV, 4.125/DV, 4.875/DV, 5.625/DV,
  125. 6.000/DV, 6.750/DV, 7.125/DV, 7.500/DV,
  126. 7.875/DV, 8.250/DV, 8.625/DV, 9.000/DV,
  127. /* OCT 4 */
  128. 0.000/DV, 0.000/DV, 3.000/DV, 4.875/DV,
  129. 6.000/DV, 7.125/DV, 7.875/DV, 8.625/DV,
  130. 9.000/DV, 9.750/DV,10.125/DV,10.500/DV,
  131. 10.875/DV,11.250/DV,11.625/DV,12.000/DV,
  132. /* OCT 5 */
  133. 0.000/DV, 3.000/DV, 6.000/DV, 7.875/DV,
  134. 9.000/DV,10.125/DV,10.875/DV,11.625/DV,
  135. 12.000/DV,12.750/DV,13.125/DV,13.500/DV,
  136. 13.875/DV,14.250/DV,14.625/DV,15.000/DV,
  137. /* OCT 6 */
  138. 0.000/DV, 6.000/DV, 9.000/DV,10.875/DV,
  139. 12.000/DV,13.125/DV,13.875/DV,14.625/DV,
  140. 15.000/DV,15.750/DV,16.125/DV,16.500/DV,
  141. 16.875/DV,17.250/DV,17.625/DV,18.000/DV,
  142. /* OCT 7 */
  143. 0.000/DV, 9.000/DV,12.000/DV,13.875/DV,
  144. 15.000/DV,16.125/DV,16.875/DV,17.625/DV,
  145. 18.000/DV,18.750/DV,19.125/DV,19.500/DV,
  146. 19.875/DV,20.250/DV,20.625/DV,21.000/DV
  147. };
  148. #undef DV
  149. /* sustain lebel table (3db per step) */
  150. /* 0 - 15: 0, 3, 6, 9,12,15,18,21,24,27,30,33,36,39,42,93 (dB)*/
  151. #define SC(db) (db*((3/EG_STEP)*(1<<ENV_BITS)))+EG_DST
  152. static const INT32 SL_TABLE[16]={
  153. SC( 0),SC( 1),SC( 2),SC(3 ),SC(4 ),SC(5 ),SC(6 ),SC( 7),
  154. SC( 8),SC( 9),SC(10),SC(11),SC(12),SC(13),SC(14),SC(31)
  155. };
  156. #undef SC
  157. #define TL_MAX (EG_ENT*2) /* limit(tl + ksr + envelope) + sinwave */
  158. /* TotalLevel : 48 24 12 6 3 1.5 0.75 (dB) */
  159. /* TL_TABLE[ 0 to TL_MAX ] : plus section */
  160. /* TL_TABLE[ TL_MAX to TL_MAX+TL_MAX-1 ] : minus section */
  161. static INT32 *TL_TABLE;
  162. /* pointers to TL_TABLE with sinwave output offset */
  163. static INT32 **SIN_TABLE;
  164. /* LFO table */
  165. static INT32 *AMS_TABLE;
  166. static INT32 *VIB_TABLE;
  167. /* envelope output curve table */
  168. /* attack + decay + OFF */
  169. static INT32 ENV_CURVE[2*EG_ENT+1];
  170. /* multiple table */
  171. #define ML 2
  172. static const UINT32 MUL_TABLE[16]= {
  173. /* 1/2, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15 */
  174. 0.50*ML, 1.00*ML, 2.00*ML, 3.00*ML, 4.00*ML, 5.00*ML, 6.00*ML, 7.00*ML,
  175. 8.00*ML, 9.00*ML,10.00*ML,10.00*ML,12.00*ML,12.00*ML,15.00*ML,15.00*ML
  176. };
  177. #undef ML
  178. /* dummy attack / decay rate ( when rate == 0 ) */
  179. static INT32 RATE_0[16]=
  180. {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  181. /* -------------------- static state --------------------- */
  182. /* lock level of common table */
  183. static int num_lock = 0;
  184. /* work table */
  185. static void *cur_chip = NULL; /* current chip point */
  186. /* currenct chip state */
  187. /* static OPLSAMPLE *bufL,*bufR; */
  188. static OPL_CH *S_CH;
  189. static OPL_CH *E_CH;
  190. OPL_SLOT *SLOT7_1,*SLOT7_2,*SLOT8_1,*SLOT8_2;
  191. static INT32 outd[1];
  192. static INT32 ams;
  193. static INT32 vib;
  194. INT32 *ams_table;
  195. INT32 *vib_table;
  196. static INT32 amsIncr;
  197. static INT32 vibIncr;
  198. static INT32 feedback2; /* connect for SLOT 2 */
  199. /* log output level */
  200. #define LOG_ERR 3 /* ERROR */
  201. #define LOG_WAR 2 /* WARNING */
  202. #define LOG_INF 1 /* INFORMATION */
  203. //#define LOG_LEVEL LOG_INF
  204. #define LOG_LEVEL LOG_ERR
  205. //#define LOG(n,x) if( (n)>=LOG_LEVEL ) logerror x
  206. #define LOG(n,x)
  207. /* --------------------- subroutines --------------------- */
  208. INLINE int Limit( int val, int max, int min ) {
  209. if ( val > max )
  210. val = max;
  211. else if ( val < min )
  212. val = min;
  213. return val;
  214. }
  215. /* status set and IRQ handling */
  216. INLINE void OPL_STATUS_SET(FM_OPL *OPL,int flag)
  217. {
  218. /* set status flag */
  219. OPL->status |= flag;
  220. if(!(OPL->status & 0x80))
  221. {
  222. if(OPL->status & OPL->statusmask)
  223. { /* IRQ on */
  224. OPL->status |= 0x80;
  225. /* callback user interrupt handler (IRQ is OFF to ON) */
  226. if(OPL->IRQHandler) (OPL->IRQHandler)(OPL->IRQParam,1);
  227. }
  228. }
  229. }
  230. /* status reset and IRQ handling */
  231. INLINE void OPL_STATUS_RESET(FM_OPL *OPL,int flag)
  232. {
  233. /* reset status flag */
  234. OPL->status &=~flag;
  235. if((OPL->status & 0x80))
  236. {
  237. if (!(OPL->status & OPL->statusmask) )
  238. {
  239. OPL->status &= 0x7f;
  240. /* callback user interrupt handler (IRQ is ON to OFF) */
  241. if(OPL->IRQHandler) (OPL->IRQHandler)(OPL->IRQParam,0);
  242. }
  243. }
  244. }
  245. /* IRQ mask set */
  246. INLINE void OPL_STATUSMASK_SET(FM_OPL *OPL,int flag)
  247. {
  248. OPL->statusmask = flag;
  249. /* IRQ handling check */
  250. OPL_STATUS_SET(OPL,0);
  251. OPL_STATUS_RESET(OPL,0);
  252. }
  253. /* ----- key on ----- */
  254. INLINE void OPL_KEYON(OPL_SLOT *SLOT)
  255. {
  256. /* sin wave restart */
  257. SLOT->Cnt = 0;
  258. /* set attack */
  259. SLOT->evm = ENV_MOD_AR;
  260. SLOT->evs = SLOT->evsa;
  261. SLOT->evc = EG_AST;
  262. SLOT->eve = EG_AED;
  263. }
  264. /* ----- key off ----- */
  265. INLINE void OPL_KEYOFF(OPL_SLOT *SLOT)
  266. {
  267. if( SLOT->evm > ENV_MOD_RR)
  268. {
  269. /* set envelope counter from envleope output */
  270. SLOT->evm = ENV_MOD_RR;
  271. if( !(SLOT->evc&EG_DST) )
  272. //SLOT->evc = (ENV_CURVE[SLOT->evc>>ENV_BITS]<<ENV_BITS) + EG_DST;
  273. SLOT->evc = EG_DST;
  274. SLOT->eve = EG_DED;
  275. SLOT->evs = SLOT->evsr;
  276. }
  277. }
  278. /* ---------- calcrate Envelope Generator & Phase Generator ---------- */
  279. /* return : envelope output */
  280. INLINE UINT32 OPL_CALC_SLOT( OPL_SLOT *SLOT )
  281. {
  282. /* calcrate envelope generator */
  283. if( (SLOT->evc+=SLOT->evs) >= SLOT->eve )
  284. {
  285. switch( SLOT->evm ){
  286. case ENV_MOD_AR: /* ATTACK -> DECAY1 */
  287. /* next DR */
  288. SLOT->evm = ENV_MOD_DR;
  289. SLOT->evc = EG_DST;
  290. SLOT->eve = SLOT->SL;
  291. SLOT->evs = SLOT->evsd;
  292. break;
  293. case ENV_MOD_DR: /* DECAY -> SL or RR */
  294. SLOT->evc = SLOT->SL;
  295. SLOT->eve = EG_DED;
  296. if(SLOT->eg_typ)
  297. {
  298. SLOT->evs = 0;
  299. }
  300. else
  301. {
  302. SLOT->evm = ENV_MOD_RR;
  303. SLOT->evs = SLOT->evsr;
  304. }
  305. break;
  306. case ENV_MOD_RR: /* RR -> OFF */
  307. SLOT->evc = EG_OFF;
  308. SLOT->eve = EG_OFF+1;
  309. SLOT->evs = 0;
  310. break;
  311. }
  312. }
  313. /* calcrate envelope */
  314. return SLOT->TLL+ENV_CURVE[SLOT->evc>>ENV_BITS]+(SLOT->ams ? ams : 0);
  315. }
  316. /* set algorythm connection */
  317. static void set_algorythm( OPL_CH *CH)
  318. {
  319. INT32 *carrier = &outd[0];
  320. CH->connect1 = CH->CON ? carrier : &feedback2;
  321. CH->connect2 = carrier;
  322. }
  323. /* ---------- frequency counter for operater update ---------- */
  324. INLINE void CALC_FCSLOT(OPL_CH *CH,OPL_SLOT *SLOT)
  325. {
  326. int ksr;
  327. /* frequency step counter */
  328. SLOT->Incr = CH->fc * SLOT->mul;
  329. ksr = CH->kcode >> SLOT->KSR;
  330. if( SLOT->ksr != ksr )
  331. {
  332. SLOT->ksr = ksr;
  333. /* attack , decay rate recalcration */
  334. SLOT->evsa = SLOT->AR[ksr];
  335. SLOT->evsd = SLOT->DR[ksr];
  336. SLOT->evsr = SLOT->RR[ksr];
  337. }
  338. SLOT->TLL = SLOT->TL + (CH->ksl_base>>SLOT->ksl);
  339. }
  340. /* set multi,am,vib,EG-TYP,KSR,mul */
  341. INLINE void set_mul(FM_OPL *OPL,int slot,int v)
  342. {
  343. OPL_CH *CH = &OPL->P_CH[slot/2];
  344. OPL_SLOT *SLOT = &CH->SLOT[slot&1];
  345. SLOT->mul = MUL_TABLE[v&0x0f];
  346. SLOT->KSR = (v&0x10) ? 0 : 2;
  347. SLOT->eg_typ = (v&0x20)>>5;
  348. SLOT->vib = (v&0x40);
  349. SLOT->ams = (v&0x80);
  350. CALC_FCSLOT(CH,SLOT);
  351. }
  352. /* set ksl & tl */
  353. INLINE void set_ksl_tl(FM_OPL *OPL,int slot,int v)
  354. {
  355. OPL_CH *CH = &OPL->P_CH[slot/2];
  356. OPL_SLOT *SLOT = &CH->SLOT[slot&1];
  357. int ksl = v>>6; /* 0 / 1.5 / 3 / 6 db/OCT */
  358. SLOT->ksl = ksl ? 3-ksl : 31;
  359. SLOT->TL = (v&0x3f)*(0.75/EG_STEP); /* 0.75db step */
  360. if( !(OPL->mode&0x80) )
  361. { /* not CSM latch total level */
  362. SLOT->TLL = SLOT->TL + (CH->ksl_base>>SLOT->ksl);
  363. }
  364. }
  365. /* set attack rate & decay rate */
  366. INLINE void set_ar_dr(FM_OPL *OPL,int slot,int v)
  367. {
  368. OPL_CH *CH = &OPL->P_CH[slot/2];
  369. OPL_SLOT *SLOT = &CH->SLOT[slot&1];
  370. int ar = v>>4;
  371. int dr = v&0x0f;
  372. SLOT->AR = ar ? &OPL->AR_TABLE[ar<<2] : RATE_0;
  373. SLOT->evsa = SLOT->AR[SLOT->ksr];
  374. if( SLOT->evm == ENV_MOD_AR ) SLOT->evs = SLOT->evsa;
  375. SLOT->DR = dr ? &OPL->DR_TABLE[dr<<2] : RATE_0;
  376. SLOT->evsd = SLOT->DR[SLOT->ksr];
  377. if( SLOT->evm == ENV_MOD_DR ) SLOT->evs = SLOT->evsd;
  378. }
  379. /* set sustain level & release rate */
  380. INLINE void set_sl_rr(FM_OPL *OPL,int slot,int v)
  381. {
  382. OPL_CH *CH = &OPL->P_CH[slot/2];
  383. OPL_SLOT *SLOT = &CH->SLOT[slot&1];
  384. int sl = v>>4;
  385. int rr = v & 0x0f;
  386. SLOT->SL = SL_TABLE[sl];
  387. if( SLOT->evm == ENV_MOD_DR ) SLOT->eve = SLOT->SL;
  388. SLOT->RR = &OPL->DR_TABLE[rr<<2];
  389. SLOT->evsr = SLOT->RR[SLOT->ksr];
  390. if( SLOT->evm == ENV_MOD_RR ) SLOT->evs = SLOT->evsr;
  391. }
  392. /* operator output calcrator */
  393. #define OP_OUT(slot,env,con) slot->wavetable[((slot->Cnt+con)/(0x1000000/SIN_ENT))&(SIN_ENT-1)][env]
  394. /* ---------- calcrate one of channel ---------- */
  395. INLINE void OPL_CALC_CH( OPL_CH *CH )
  396. {
  397. UINT32 env_out;
  398. OPL_SLOT *SLOT;
  399. feedback2 = 0;
  400. /* SLOT 1 */
  401. SLOT = &CH->SLOT[SLOT1];
  402. env_out=OPL_CALC_SLOT(SLOT);
  403. if( env_out < EG_ENT-1 )
  404. {
  405. /* PG */
  406. if(SLOT->vib) SLOT->Cnt += (SLOT->Incr*vib/VIB_RATE);
  407. else SLOT->Cnt += SLOT->Incr;
  408. /* connectoion */
  409. if(CH->FB)
  410. {
  411. int feedback1 = (CH->op1_out[0]+CH->op1_out[1])>>CH->FB;
  412. CH->op1_out[1] = CH->op1_out[0];
  413. *CH->connect1 += CH->op1_out[0] = OP_OUT(SLOT,env_out,feedback1);
  414. }
  415. else
  416. {
  417. *CH->connect1 += OP_OUT(SLOT,env_out,0);
  418. }
  419. }else
  420. {
  421. CH->op1_out[1] = CH->op1_out[0];
  422. CH->op1_out[0] = 0;
  423. }
  424. /* SLOT 2 */
  425. SLOT = &CH->SLOT[SLOT2];
  426. env_out=OPL_CALC_SLOT(SLOT);
  427. if( env_out < EG_ENT-1 )
  428. {
  429. /* PG */
  430. if(SLOT->vib) SLOT->Cnt += (SLOT->Incr*vib/VIB_RATE);
  431. else SLOT->Cnt += SLOT->Incr;
  432. /* connectoion */
  433. outd[0] += OP_OUT(SLOT,env_out, feedback2);
  434. }
  435. }
  436. /* ---------- calcrate rythm block ---------- */
  437. #define WHITE_NOISE_db 6.0
  438. INLINE void OPL_CALC_RH( OPL_CH *CH )
  439. {
  440. UINT32 env_tam,env_sd,env_top,env_hh;
  441. int whitenoise = (rand()&1)*(WHITE_NOISE_db/EG_STEP);
  442. INT32 tone8;
  443. OPL_SLOT *SLOT;
  444. int env_out;
  445. /* BD : same as FM serial mode and output level is large */
  446. feedback2 = 0;
  447. /* SLOT 1 */
  448. SLOT = &CH[6].SLOT[SLOT1];
  449. env_out=OPL_CALC_SLOT(SLOT);
  450. if( env_out < EG_ENT-1 )
  451. {
  452. /* PG */
  453. if(SLOT->vib) SLOT->Cnt += (SLOT->Incr*vib/VIB_RATE);
  454. else SLOT->Cnt += SLOT->Incr;
  455. /* connectoion */
  456. if(CH[6].FB)
  457. {
  458. int feedback1 = (CH[6].op1_out[0]+CH[6].op1_out[1])>>CH[6].FB;
  459. CH[6].op1_out[1] = CH[6].op1_out[0];
  460. feedback2 = CH[6].op1_out[0] = OP_OUT(SLOT,env_out,feedback1);
  461. }
  462. else
  463. {
  464. feedback2 = OP_OUT(SLOT,env_out,0);
  465. }
  466. }else
  467. {
  468. feedback2 = 0;
  469. CH[6].op1_out[1] = CH[6].op1_out[0];
  470. CH[6].op1_out[0] = 0;
  471. }
  472. /* SLOT 2 */
  473. SLOT = &CH[6].SLOT[SLOT2];
  474. env_out=OPL_CALC_SLOT(SLOT);
  475. if( env_out < EG_ENT-1 )
  476. {
  477. /* PG */
  478. if(SLOT->vib) SLOT->Cnt += (SLOT->Incr*vib/VIB_RATE);
  479. else SLOT->Cnt += SLOT->Incr;
  480. /* connectoion */
  481. outd[0] += OP_OUT(SLOT,env_out, feedback2)*2;
  482. }
  483. // SD (17) = mul14[fnum7] + white noise
  484. // TAM (15) = mul15[fnum8]
  485. // TOP (18) = fnum6(mul18[fnum8]+whitenoise)
  486. // HH (14) = fnum7(mul18[fnum8]+whitenoise) + white noise
  487. env_sd =OPL_CALC_SLOT(SLOT7_2) + whitenoise;
  488. env_tam=OPL_CALC_SLOT(SLOT8_1);
  489. env_top=OPL_CALC_SLOT(SLOT8_2);
  490. env_hh =OPL_CALC_SLOT(SLOT7_1) + whitenoise;
  491. /* PG */
  492. if(SLOT7_1->vib) SLOT7_1->Cnt += (2*SLOT7_1->Incr*vib/VIB_RATE);
  493. else SLOT7_1->Cnt += 2*SLOT7_1->Incr;
  494. if(SLOT7_2->vib) SLOT7_2->Cnt += ((CH[7].fc*8)*vib/VIB_RATE);
  495. else SLOT7_2->Cnt += (CH[7].fc*8);
  496. if(SLOT8_1->vib) SLOT8_1->Cnt += (SLOT8_1->Incr*vib/VIB_RATE);
  497. else SLOT8_1->Cnt += SLOT8_1->Incr;
  498. if(SLOT8_2->vib) SLOT8_2->Cnt += ((CH[8].fc*48)*vib/VIB_RATE);
  499. else SLOT8_2->Cnt += (CH[8].fc*48);
  500. tone8 = OP_OUT(SLOT8_2,whitenoise,0 );
  501. /* SD */
  502. if( env_sd < EG_ENT-1 )
  503. outd[0] += OP_OUT(SLOT7_1,env_sd, 0)*8;
  504. /* TAM */
  505. if( env_tam < EG_ENT-1 )
  506. outd[0] += OP_OUT(SLOT8_1,env_tam, 0)*2;
  507. /* TOP-CY */
  508. if( env_top < EG_ENT-1 )
  509. outd[0] += OP_OUT(SLOT7_2,env_top,tone8)*2;
  510. /* HH */
  511. if( env_hh < EG_ENT-1 )
  512. outd[0] += OP_OUT(SLOT7_2,env_hh,tone8)*2;
  513. }
  514. /* ----------- initialize time tabls ----------- */
  515. static void init_timetables( FM_OPL *OPL , int ARRATE , int DRRATE )
  516. {
  517. int i;
  518. double rate;
  519. /* make attack rate & decay rate tables */
  520. for (i = 0;i < 4;i++) OPL->AR_TABLE[i] = OPL->DR_TABLE[i] = 0;
  521. for (i = 4;i <= 60;i++){
  522. rate = OPL->freqbase; /* frequency rate */
  523. if( i < 60 ) rate *= 1.0+(i&3)*0.25; /* b0-1 : x1 , x1.25 , x1.5 , x1.75 */
  524. rate *= 1<<((i>>2)-1); /* b2-5 : shift bit */
  525. rate *= (double)(EG_ENT<<ENV_BITS);
  526. OPL->AR_TABLE[i] = rate / ARRATE;
  527. OPL->DR_TABLE[i] = rate / DRRATE;
  528. }
  529. for (i = 60;i < 76;i++)
  530. {
  531. OPL->AR_TABLE[i] = EG_AED-1;
  532. OPL->DR_TABLE[i] = OPL->DR_TABLE[60];
  533. }
  534. #if 0
  535. for (i = 0;i < 64 ;i++){ /* make for overflow area */
  536. LOG(LOG_WAR,("rate %2d , ar %f ms , dr %f ms \n",i,
  537. ((double)(EG_ENT<<ENV_BITS) / OPL->AR_TABLE[i]) * (1000.0 / OPL->rate),
  538. ((double)(EG_ENT<<ENV_BITS) / OPL->DR_TABLE[i]) * (1000.0 / OPL->rate) ));
  539. }
  540. #endif
  541. }
  542. /* ---------- generic table initialize ---------- */
  543. static int OPLOpenTable( void )
  544. {
  545. int s,t;
  546. double rate;
  547. int i,j;
  548. double pom;
  549. /* allocate dynamic tables */
  550. if( (TL_TABLE = malloc(TL_MAX*2*sizeof(INT32))) == NULL)
  551. return 0;
  552. if( (SIN_TABLE = malloc(SIN_ENT*4 *sizeof(INT32 *))) == NULL)
  553. {
  554. free(TL_TABLE);
  555. return 0;
  556. }
  557. if( (AMS_TABLE = malloc(AMS_ENT*2 *sizeof(INT32))) == NULL)
  558. {
  559. free(TL_TABLE);
  560. free(SIN_TABLE);
  561. return 0;
  562. }
  563. if( (VIB_TABLE = malloc(VIB_ENT*2 *sizeof(INT32))) == NULL)
  564. {
  565. free(TL_TABLE);
  566. free(SIN_TABLE);
  567. free(AMS_TABLE);
  568. return 0;
  569. }
  570. /* make total level table */
  571. for (t = 0;t < EG_ENT-1 ;t++){
  572. rate = ((1<<TL_BITS)-1)/pow(10,EG_STEP*t/20); /* dB -> voltage */
  573. TL_TABLE[ t] = (int)rate;
  574. TL_TABLE[TL_MAX+t] = -TL_TABLE[t];
  575. /* LOG(LOG_INF,("TotalLevel(%3d) = %x\n",t,TL_TABLE[t]));*/
  576. }
  577. /* fill volume off area */
  578. for ( t = EG_ENT-1; t < TL_MAX ;t++){
  579. TL_TABLE[t] = TL_TABLE[TL_MAX+t] = 0;
  580. }
  581. /* make sinwave table (total level offet) */
  582. /* degree 0 = degree 180 = off */
  583. SIN_TABLE[0] = SIN_TABLE[SIN_ENT/2] = &TL_TABLE[EG_ENT-1];
  584. for (s = 1;s <= SIN_ENT/4;s++){
  585. pom = sin(2*PI*s/SIN_ENT); /* sin */
  586. pom = 20*log10(1/pom); /* decibel */
  587. j = pom / EG_STEP; /* TL_TABLE steps */
  588. /* degree 0 - 90 , degree 180 - 90 : plus section */
  589. SIN_TABLE[ s] = SIN_TABLE[SIN_ENT/2-s] = &TL_TABLE[j];
  590. /* degree 180 - 270 , degree 360 - 270 : minus section */
  591. SIN_TABLE[SIN_ENT/2+s] = SIN_TABLE[SIN_ENT -s] = &TL_TABLE[TL_MAX+j];
  592. /* LOG(LOG_INF,("sin(%3d) = %f:%f db\n",s,pom,(double)j * EG_STEP));*/
  593. }
  594. for (s = 0;s < SIN_ENT;s++)
  595. {
  596. SIN_TABLE[SIN_ENT*1+s] = s<(SIN_ENT/2) ? SIN_TABLE[s] : &TL_TABLE[EG_ENT];
  597. SIN_TABLE[SIN_ENT*2+s] = SIN_TABLE[s % (SIN_ENT/2)];
  598. SIN_TABLE[SIN_ENT*3+s] = (s/(SIN_ENT/4))&1 ? &TL_TABLE[EG_ENT] : SIN_TABLE[SIN_ENT*2+s];
  599. }
  600. /* envelope counter -> envelope output table */
  601. for (i=0; i<EG_ENT; i++)
  602. {
  603. /* ATTACK curve */
  604. pom = pow( ((double)(EG_ENT-1-i)/EG_ENT) , 8 ) * EG_ENT;
  605. /* if( pom >= EG_ENT ) pom = EG_ENT-1; */
  606. ENV_CURVE[i] = (int)pom;
  607. /* DECAY ,RELEASE curve */
  608. ENV_CURVE[(EG_DST>>ENV_BITS)+i]= i;
  609. }
  610. /* off */
  611. ENV_CURVE[EG_OFF>>ENV_BITS]= EG_ENT-1;
  612. /* make LFO ams table */
  613. for (i=0; i<AMS_ENT; i++)
  614. {
  615. pom = (1.0+sin(2*PI*i/AMS_ENT))/2; /* sin */
  616. AMS_TABLE[i] = (1.0/EG_STEP)*pom; /* 1dB */
  617. AMS_TABLE[AMS_ENT+i] = (4.8/EG_STEP)*pom; /* 4.8dB */
  618. }
  619. /* make LFO vibrate table */
  620. for (i=0; i<VIB_ENT; i++)
  621. {
  622. /* 100cent = 1seminote = 6% ?? */
  623. pom = (double)VIB_RATE*0.06*sin(2*PI*i/VIB_ENT); /* +-100sect step */
  624. VIB_TABLE[i] = VIB_RATE + (pom*0.07); /* +- 7cent */
  625. VIB_TABLE[VIB_ENT+i] = VIB_RATE + (pom*0.14); /* +-14cent */
  626. /* LOG(LOG_INF,("vib %d=%d\n",i,VIB_TABLE[VIB_ENT+i])); */
  627. }
  628. return 1;
  629. }
  630. static void OPLCloseTable( void )
  631. {
  632. free(TL_TABLE);
  633. free(SIN_TABLE);
  634. free(AMS_TABLE);
  635. free(VIB_TABLE);
  636. }
  637. /* CSM Key Controll */
  638. INLINE void CSMKeyControll(OPL_CH *CH)
  639. {
  640. OPL_SLOT *slot1 = &CH->SLOT[SLOT1];
  641. OPL_SLOT *slot2 = &CH->SLOT[SLOT2];
  642. /* all key off */
  643. OPL_KEYOFF(slot1);
  644. OPL_KEYOFF(slot2);
  645. /* total level latch */
  646. slot1->TLL = slot1->TL + (CH->ksl_base>>slot1->ksl);
  647. slot1->TLL = slot1->TL + (CH->ksl_base>>slot1->ksl);
  648. /* key on */
  649. CH->op1_out[0] = CH->op1_out[1] = 0;
  650. OPL_KEYON(slot1);
  651. OPL_KEYON(slot2);
  652. }
  653. /* ---------- opl initialize ---------- */
  654. static void OPL_initalize(FM_OPL *OPL)
  655. {
  656. int fn;
  657. /* frequency base */
  658. OPL->freqbase = (OPL->rate) ? ((double)OPL->clock / OPL->rate) / 72 : 0;
  659. /* Timer base time */
  660. OPL->TimerBase = 1.0/((double)OPL->clock / 72.0 );
  661. /* make time tables */
  662. init_timetables( OPL , OPL_ARRATE , OPL_DRRATE );
  663. /* make fnumber -> increment counter table */
  664. for( fn=0 ; fn < 1024 ; fn++ )
  665. {
  666. OPL->FN_TABLE[fn] = OPL->freqbase * fn * FREQ_RATE * (1<<7) / 2;
  667. }
  668. /* LFO freq.table */
  669. OPL->amsIncr = OPL->rate ? (double)AMS_ENT*(1<<AMS_SHIFT) / OPL->rate * 3.7 * ((double)OPL->clock/3600000) : 0;
  670. OPL->vibIncr = OPL->rate ? (double)VIB_ENT*(1<<VIB_SHIFT) / OPL->rate * 6.4 * ((double)OPL->clock/3600000) : 0;
  671. }
  672. /* ---------- write a OPL registers ---------- */
  673. static void OPLWriteReg(FM_OPL *OPL, int r, int v)
  674. {
  675. OPL_CH *CH;
  676. int slot;
  677. int block_fnum;
  678. switch(r&0xe0)
  679. {
  680. case 0x00: /* 00-1f:controll */
  681. switch(r&0x1f)
  682. {
  683. case 0x01:
  684. /* wave selector enable */
  685. if(OPL->type&OPL_TYPE_WAVESEL)
  686. {
  687. OPL->wavesel = v&0x20;
  688. if(!OPL->wavesel)
  689. {
  690. /* preset compatible mode */
  691. int c;
  692. for(c=0;c<OPL->max_ch;c++)
  693. {
  694. OPL->P_CH[c].SLOT[SLOT1].wavetable = &SIN_TABLE[0];
  695. OPL->P_CH[c].SLOT[SLOT2].wavetable = &SIN_TABLE[0];
  696. }
  697. }
  698. }
  699. return;
  700. case 0x02: /* Timer 1 */
  701. OPL->T[0] = (256-v)*4;
  702. break;
  703. case 0x03: /* Timer 2 */
  704. OPL->T[1] = (256-v)*16;
  705. return;
  706. case 0x04: /* IRQ clear / mask and Timer enable */
  707. if(v&0x80)
  708. { /* IRQ flag clear */
  709. OPL_STATUS_RESET(OPL,0x7f);
  710. }
  711. else
  712. { /* set IRQ mask ,timer enable*/
  713. UINT8 st1 = v&1;
  714. UINT8 st2 = (v>>1)&1;
  715. /* IRQRST,T1MSK,t2MSK,EOSMSK,BRMSK,x,ST2,ST1 */
  716. OPL_STATUS_RESET(OPL,v&0x78);
  717. OPL_STATUSMASK_SET(OPL,((~v)&0x78)|0x01);
  718. /* timer 2 */
  719. if(OPL->st[1] != st2)
  720. {
  721. double interval = st2 ? (double)OPL->T[1]*OPL->TimerBase : 0.0;
  722. OPL->st[1] = st2;
  723. if (OPL->TimerHandler) (OPL->TimerHandler)(OPL->TimerParam+1,interval);
  724. }
  725. /* timer 1 */
  726. if(OPL->st[0] != st1)
  727. {
  728. double interval = st1 ? (double)OPL->T[0]*OPL->TimerBase : 0.0;
  729. OPL->st[0] = st1;
  730. if (OPL->TimerHandler) (OPL->TimerHandler)(OPL->TimerParam+0,interval);
  731. }
  732. }
  733. return;
  734. #if BUILD_Y8950
  735. case 0x06: /* Key Board OUT */
  736. if(OPL->type&OPL_TYPE_KEYBOARD)
  737. {
  738. if(OPL->keyboardhandler_w)
  739. OPL->keyboardhandler_w(OPL->keyboard_param,v);
  740. else
  741. LOG(LOG_WAR,("OPL:write unmapped KEYBOARD port\n"));
  742. }
  743. return;
  744. case 0x07: /* DELTA-T controll : START,REC,MEMDATA,REPT,SPOFF,x,x,RST */
  745. if(OPL->type&OPL_TYPE_ADPCM)
  746. YM_DELTAT_ADPCM_Write(OPL->deltat,r-0x07,v);
  747. return;
  748. case 0x08: /* MODE,DELTA-T : CSM,NOTESEL,x,x,smpl,da/ad,64k,rom */
  749. OPL->mode = v;
  750. v&=0x1f; /* for DELTA-T unit */
  751. case 0x09: /* START ADD */
  752. case 0x0a:
  753. case 0x0b: /* STOP ADD */
  754. case 0x0c:
  755. case 0x0d: /* PRESCALE */
  756. case 0x0e:
  757. case 0x0f: /* ADPCM data */
  758. case 0x10: /* DELTA-N */
  759. case 0x11: /* DELTA-N */
  760. case 0x12: /* EG-CTRL */
  761. if(OPL->type&OPL_TYPE_ADPCM)
  762. YM_DELTAT_ADPCM_Write(OPL->deltat,r-0x07,v);
  763. return;
  764. #if 0
  765. case 0x15: /* DAC data */
  766. case 0x16:
  767. case 0x17: /* SHIFT */
  768. return;
  769. case 0x18: /* I/O CTRL (Direction) */
  770. if(OPL->type&OPL_TYPE_IO)
  771. OPL->portDirection = v&0x0f;
  772. return;
  773. case 0x19: /* I/O DATA */
  774. if(OPL->type&OPL_TYPE_IO)
  775. {
  776. OPL->portLatch = v;
  777. if(OPL->porthandler_w)
  778. OPL->porthandler_w(OPL->port_param,v&OPL->portDirection);
  779. }
  780. return;
  781. case 0x1a: /* PCM data */
  782. return;
  783. #endif
  784. #endif
  785. }
  786. break;
  787. case 0x20: /* am,vib,ksr,eg type,mul */
  788. slot = slot_array[r&0x1f];
  789. if(slot == -1) return;
  790. set_mul(OPL,slot,v);
  791. return;
  792. case 0x40:
  793. slot = slot_array[r&0x1f];
  794. if(slot == -1) return;
  795. set_ksl_tl(OPL,slot,v);
  796. return;
  797. case 0x60:
  798. slot = slot_array[r&0x1f];
  799. if(slot == -1) return;
  800. set_ar_dr(OPL,slot,v);
  801. return;
  802. case 0x80:
  803. slot = slot_array[r&0x1f];
  804. if(slot == -1) return;
  805. set_sl_rr(OPL,slot,v);
  806. return;
  807. case 0xa0:
  808. switch(r)
  809. {
  810. case 0xbd:
  811. /* amsep,vibdep,r,bd,sd,tom,tc,hh */
  812. {
  813. UINT8 rkey = OPL->rythm^v;
  814. OPL->ams_table = &AMS_TABLE[v&0x80 ? AMS_ENT : 0];
  815. OPL->vib_table = &VIB_TABLE[v&0x40 ? VIB_ENT : 0];
  816. OPL->rythm = v&0x3f;
  817. if(OPL->rythm&0x20)
  818. {
  819. #if 0
  820. usrintf_showmessage("OPL Rythm mode select");
  821. #endif
  822. /* BD key on/off */
  823. if(rkey&0x10)
  824. {
  825. if(v&0x10)
  826. {
  827. OPL->P_CH[6].op1_out[0] = OPL->P_CH[6].op1_out[1] = 0;
  828. OPL_KEYON(&OPL->P_CH[6].SLOT[SLOT1]);
  829. OPL_KEYON(&OPL->P_CH[6].SLOT[SLOT2]);
  830. }
  831. else
  832. {
  833. OPL_KEYOFF(&OPL->P_CH[6].SLOT[SLOT1]);
  834. OPL_KEYOFF(&OPL->P_CH[6].SLOT[SLOT2]);
  835. }
  836. }
  837. /* SD key on/off */
  838. if(rkey&0x08)
  839. {
  840. if(v&0x08) OPL_KEYON(&OPL->P_CH[7].SLOT[SLOT2]);
  841. else OPL_KEYOFF(&OPL->P_CH[7].SLOT[SLOT2]);
  842. }/* TAM key on/off */
  843. if(rkey&0x04)
  844. {
  845. if(v&0x04) OPL_KEYON(&OPL->P_CH[8].SLOT[SLOT1]);
  846. else OPL_KEYOFF(&OPL->P_CH[8].SLOT[SLOT1]);
  847. }
  848. /* TOP-CY key on/off */
  849. if(rkey&0x02)
  850. {
  851. if(v&0x02) OPL_KEYON(&OPL->P_CH[8].SLOT[SLOT2]);
  852. else OPL_KEYOFF(&OPL->P_CH[8].SLOT[SLOT2]);
  853. }
  854. /* HH key on/off */
  855. if(rkey&0x01)
  856. {
  857. if(v&0x01) OPL_KEYON(&OPL->P_CH[7].SLOT[SLOT1]);
  858. else OPL_KEYOFF(&OPL->P_CH[7].SLOT[SLOT1]);
  859. }
  860. }
  861. }
  862. return;
  863. }
  864. /* keyon,block,fnum */
  865. if( (r&0x0f) > 8) return;
  866. CH = &OPL->P_CH[r&0x0f];
  867. if(!(r&0x10))
  868. { /* a0-a8 */
  869. block_fnum = (CH->block_fnum&0x1f00) | v;
  870. }
  871. else
  872. { /* b0-b8 */
  873. int keyon = (v>>5)&1;
  874. block_fnum = ((v&0x1f)<<8) | (CH->block_fnum&0xff);
  875. if(CH->keyon != keyon)
  876. {
  877. if( (CH->keyon=keyon) )
  878. {
  879. CH->op1_out[0] = CH->op1_out[1] = 0;
  880. OPL_KEYON(&CH->SLOT[SLOT1]);
  881. OPL_KEYON(&CH->SLOT[SLOT2]);
  882. }
  883. else
  884. {
  885. OPL_KEYOFF(&CH->SLOT[SLOT1]);
  886. OPL_KEYOFF(&CH->SLOT[SLOT2]);
  887. }
  888. }
  889. }
  890. /* update */
  891. if(CH->block_fnum != block_fnum)
  892. {
  893. int blockRv = 7-(block_fnum>>10);
  894. int fnum = block_fnum&0x3ff;
  895. CH->block_fnum = block_fnum;
  896. CH->ksl_base = KSL_TABLE[block_fnum>>6];
  897. CH->fc = OPL->FN_TABLE[fnum]>>blockRv;
  898. CH->kcode = CH->block_fnum>>9;
  899. if( (OPL->mode&0x40) && CH->block_fnum&0x100) CH->kcode |=1;
  900. CALC_FCSLOT(CH,&CH->SLOT[SLOT1]);
  901. CALC_FCSLOT(CH,&CH->SLOT[SLOT2]);
  902. }
  903. return;
  904. case 0xc0:
  905. /* FB,C */
  906. if( (r&0x0f) > 8) return;
  907. CH = &OPL->P_CH[r&0x0f];
  908. {
  909. int feedback = (v>>1)&7;
  910. CH->FB = feedback ? (8+1) - feedback : 0;
  911. CH->CON = v&1;
  912. set_algorythm(CH);
  913. }
  914. return;
  915. case 0xe0: /* wave type */
  916. slot = slot_array[r&0x1f];
  917. if(slot == -1) return;
  918. CH = &OPL->P_CH[slot/2];
  919. if(OPL->wavesel)
  920. {
  921. /* LOG(LOG_INF,("OPL SLOT %d wave select %d\n",slot,v&3)); */
  922. CH->SLOT[slot&1].wavetable = &SIN_TABLE[(v&0x03)*SIN_ENT];
  923. }
  924. return;
  925. }
  926. }
  927. /* lock/unlock for common table */
  928. static int OPL_LockTable(void)
  929. {
  930. num_lock++;
  931. if(num_lock>1) return 0;
  932. /* first time */
  933. cur_chip = NULL;
  934. /* allocate total level table (128kb space) */
  935. if( !OPLOpenTable() )
  936. {
  937. num_lock--;
  938. return -1;
  939. }
  940. return 0;
  941. }
  942. static void OPL_UnLockTable(void)
  943. {
  944. if(num_lock) num_lock--;
  945. if(num_lock) return;
  946. /* last time */
  947. cur_chip = NULL;
  948. OPLCloseTable();
  949. }
  950. #if (BUILD_YM3812 || BUILD_YM3526)
  951. /*******************************************************************************/
  952. /* YM3812 local section */
  953. /*******************************************************************************/
  954. /* ---------- update one of chip ----------- */
  955. void YM3812UpdateOne(FM_OPL *OPL, INT16 *buffer, int length)
  956. {
  957. int i;
  958. int data;
  959. OPLSAMPLE *buf = buffer;
  960. UINT32 amsCnt = OPL->amsCnt;
  961. UINT32 vibCnt = OPL->vibCnt;
  962. UINT8 rythm = OPL->rythm&0x20;
  963. OPL_CH *CH,*R_CH;
  964. if( (void *)OPL != cur_chip ){
  965. cur_chip = (void *)OPL;
  966. /* channel pointers */
  967. S_CH = OPL->P_CH;
  968. E_CH = &S_CH[9];
  969. /* rythm slot */
  970. SLOT7_1 = &S_CH[7].SLOT[SLOT1];
  971. SLOT7_2 = &S_CH[7].SLOT[SLOT2];
  972. SLOT8_1 = &S_CH[8].SLOT[SLOT1];
  973. SLOT8_2 = &S_CH[8].SLOT[SLOT2];
  974. /* LFO state */
  975. amsIncr = OPL->amsIncr;
  976. vibIncr = OPL->vibIncr;
  977. ams_table = OPL->ams_table;
  978. vib_table = OPL->vib_table;
  979. }
  980. R_CH = rythm ? &S_CH[6] : E_CH;
  981. for( i=0; i < length ; i++ )
  982. {
  983. /* channel A channel B channel C */
  984. /* LFO */
  985. ams = ams_table[(amsCnt+=amsIncr)>>AMS_SHIFT];
  986. vib = vib_table[(vibCnt+=vibIncr)>>VIB_SHIFT];
  987. outd[0] = 0;
  988. /* FM part */
  989. for(CH=S_CH ; CH < R_CH ; CH++)
  990. OPL_CALC_CH(CH);
  991. /* Rythn part */
  992. if(rythm)
  993. OPL_CALC_RH(S_CH);
  994. /* limit check */
  995. data = Limit( outd[0] , OPL_MAXOUT, OPL_MINOUT );
  996. /* store to sound buffer */
  997. buf[i] = data >> OPL_OUTSB;
  998. }
  999. OPL->amsCnt = amsCnt;
  1000. OPL->vibCnt = vibCnt;
  1001. #ifdef OPL_OUTPUT_LOG
  1002. if(opl_dbg_fp)
  1003. {
  1004. for(opl_dbg_chip=0;opl_dbg_chip<opl_dbg_maxchip;opl_dbg_chip++)
  1005. if( opl_dbg_opl[opl_dbg_chip] == OPL) break;
  1006. fprintf(opl_dbg_fp,"%c%c%c",0x20+opl_dbg_chip,length&0xff,length/256);
  1007. }
  1008. #endif
  1009. }
  1010. #endif /* (BUILD_YM3812 || BUILD_YM3526) */
  1011. #if BUILD_Y8950
  1012. void Y8950UpdateOne(FM_OPL *OPL, INT16 *buffer, int length)
  1013. {
  1014. int i;
  1015. int data;
  1016. OPLSAMPLE *buf = buffer;
  1017. UINT32 amsCnt = OPL->amsCnt;
  1018. UINT32 vibCnt = OPL->vibCnt;
  1019. UINT8 rythm = OPL->rythm&0x20;
  1020. OPL_CH *CH,*R_CH;
  1021. YM_DELTAT *DELTAT = OPL->deltat;
  1022. /* setup DELTA-T unit */
  1023. YM_DELTAT_DECODE_PRESET(DELTAT);
  1024. if( (void *)OPL != cur_chip ){
  1025. cur_chip = (void *)OPL;
  1026. /* channel pointers */
  1027. S_CH = OPL->P_CH;
  1028. E_CH = &S_CH[9];
  1029. /* rythm slot */
  1030. SLOT7_1 = &S_CH[7].SLOT[SLOT1];
  1031. SLOT7_2 = &S_CH[7].SLOT[SLOT2];
  1032. SLOT8_1 = &S_CH[8].SLOT[SLOT1];
  1033. SLOT8_2 = &S_CH[8].SLOT[SLOT2];
  1034. /* LFO state */
  1035. amsIncr = OPL->amsIncr;
  1036. vibIncr = OPL->vibIncr;
  1037. ams_table = OPL->ams_table;
  1038. vib_table = OPL->vib_table;
  1039. }
  1040. R_CH = rythm ? &S_CH[6] : E_CH;
  1041. for( i=0; i < length ; i++ )
  1042. {
  1043. /* channel A channel B channel C */
  1044. /* LFO */
  1045. ams = ams_table[(amsCnt+=amsIncr)>>AMS_SHIFT];
  1046. vib = vib_table[(vibCnt+=vibIncr)>>VIB_SHIFT];
  1047. outd[0] = 0;
  1048. /* deltaT ADPCM */
  1049. if( DELTAT->portstate )
  1050. YM_DELTAT_ADPCM_CALC(DELTAT);
  1051. /* FM part */
  1052. for(CH=S_CH ; CH < R_CH ; CH++)
  1053. OPL_CALC_CH(CH);
  1054. /* Rythn part */
  1055. if(rythm)
  1056. OPL_CALC_RH(S_CH);
  1057. /* limit check */
  1058. data = Limit( outd[0] , OPL_MAXOUT, OPL_MINOUT );
  1059. /* store to sound buffer */
  1060. buf[i] = data >> OPL_OUTSB;
  1061. }
  1062. OPL->amsCnt = amsCnt;
  1063. OPL->vibCnt = vibCnt;
  1064. /* deltaT START flag */
  1065. if( !DELTAT->portstate )
  1066. OPL->status &= 0xfe;
  1067. }
  1068. #endif
  1069. /* ---------- reset one of chip ---------- */
  1070. void OPLResetChip(FM_OPL *OPL)
  1071. {
  1072. int c,s;
  1073. int i;
  1074. /* reset chip */
  1075. OPL->mode = 0; /* normal mode */
  1076. OPL_STATUS_RESET(OPL,0x7f);
  1077. /* reset with register write */
  1078. OPLWriteReg(OPL,0x01,0); /* wabesel disable */
  1079. OPLWriteReg(OPL,0x02,0); /* Timer1 */
  1080. OPLWriteReg(OPL,0x03,0); /* Timer2 */
  1081. OPLWriteReg(OPL,0x04,0); /* IRQ mask clear */
  1082. for(i = 0xff ; i >= 0x20 ; i-- ) OPLWriteReg(OPL,i,0);
  1083. /* reset OPerator paramater */
  1084. for( c = 0 ; c < OPL->max_ch ; c++ )
  1085. {
  1086. OPL_CH *CH = &OPL->P_CH[c];
  1087. /* OPL->P_CH[c].PAN = OPN_CENTER; */
  1088. for(s = 0 ; s < 2 ; s++ )
  1089. {
  1090. /* wave table */
  1091. CH->SLOT[s].wavetable = &SIN_TABLE[0];
  1092. /* CH->SLOT[s].evm = ENV_MOD_RR; */
  1093. CH->SLOT[s].evc = EG_OFF;
  1094. CH->SLOT[s].eve = EG_OFF+1;
  1095. CH->SLOT[s].evs = 0;
  1096. }
  1097. }
  1098. #if BUILD_Y8950
  1099. if(OPL->type&OPL_TYPE_ADPCM)
  1100. {
  1101. YM_DELTAT *DELTAT = OPL->deltat;
  1102. DELTAT->freqbase = OPL->freqbase;
  1103. DELTAT->output_pointer = outd;
  1104. DELTAT->portshift = 5;
  1105. DELTAT->output_range = DELTAT_MIXING_LEVEL<<TL_BITS;
  1106. YM_DELTAT_ADPCM_Reset(DELTAT,0);
  1107. }
  1108. #endif
  1109. }
  1110. /* ---------- Create one of vietual YM3812 ---------- */
  1111. /* 'rate' is sampling rate and 'bufsiz' is the size of the */
  1112. FM_OPL *OPLCreate(int type, int clock, int rate)
  1113. {
  1114. char *ptr;
  1115. FM_OPL *OPL;
  1116. int state_size;
  1117. int max_ch = 9; /* normaly 9 channels */
  1118. if( OPL_LockTable() ==-1) return NULL;
  1119. /* allocate OPL state space */
  1120. state_size = sizeof(FM_OPL);
  1121. state_size += sizeof(OPL_CH)*max_ch;
  1122. #if BUILD_Y8950
  1123. if(type&OPL_TYPE_ADPCM) state_size+= sizeof(YM_DELTAT);
  1124. #endif
  1125. /* allocate memory block */
  1126. ptr = malloc(state_size);
  1127. if(ptr==NULL) return NULL;
  1128. /* clear */
  1129. memset(ptr,0,state_size);
  1130. OPL = (FM_OPL *)ptr; ptr+=sizeof(FM_OPL);
  1131. OPL->P_CH = (OPL_CH *)ptr; ptr+=sizeof(OPL_CH)*max_ch;
  1132. #if BUILD_Y8950
  1133. if(type&OPL_TYPE_ADPCM) OPL->deltat = (YM_DELTAT *)ptr; ptr+=sizeof(YM_DELTAT);
  1134. #endif
  1135. /* set channel state pointer */
  1136. OPL->type = type;
  1137. OPL->clock = clock;
  1138. OPL->rate = rate;
  1139. OPL->max_ch = max_ch;
  1140. /* init grobal tables */
  1141. OPL_initalize(OPL);
  1142. /* reset chip */
  1143. OPLResetChip(OPL);
  1144. #ifdef OPL_OUTPUT_LOG
  1145. if(!opl_dbg_fp)
  1146. {
  1147. opl_dbg_fp = fopen("opllog.opl","wb");
  1148. opl_dbg_maxchip = 0;
  1149. }
  1150. if(opl_dbg_fp)
  1151. {
  1152. opl_dbg_opl[opl_dbg_maxchip] = OPL;
  1153. fprintf(opl_dbg_fp,"%c%c%c%c%c%c",0x00+opl_dbg_maxchip,
  1154. type,
  1155. clock&0xff,
  1156. (clock/0x100)&0xff,
  1157. (clock/0x10000)&0xff,
  1158. (clock/0x1000000)&0xff);
  1159. opl_dbg_maxchip++;
  1160. }
  1161. #endif
  1162. return OPL;
  1163. }
  1164. /* ---------- Destroy one of vietual YM3812 ---------- */
  1165. void OPLDestroy(FM_OPL *OPL)
  1166. {
  1167. #ifdef OPL_OUTPUT_LOG
  1168. if(opl_dbg_fp)
  1169. {
  1170. fclose(opl_dbg_fp);
  1171. opl_dbg_fp = NULL;
  1172. }
  1173. #endif
  1174. OPL_UnLockTable();
  1175. free(OPL);
  1176. }
  1177. /* ---------- Option handlers ---------- */
  1178. void OPLSetTimerHandler(FM_OPL *OPL,OPL_TIMERHANDLER TimerHandler,int channelOffset)
  1179. {
  1180. OPL->TimerHandler = TimerHandler;
  1181. OPL->TimerParam = channelOffset;
  1182. }
  1183. void OPLSetIRQHandler(FM_OPL *OPL,OPL_IRQHANDLER IRQHandler,int param)
  1184. {
  1185. OPL->IRQHandler = IRQHandler;
  1186. OPL->IRQParam = param;
  1187. }
  1188. void OPLSetUpdateHandler(FM_OPL *OPL,OPL_UPDATEHANDLER UpdateHandler,int param)
  1189. {
  1190. OPL->UpdateHandler = UpdateHandler;
  1191. OPL->UpdateParam = param;
  1192. }
  1193. #if BUILD_Y8950
  1194. void OPLSetPortHandler(FM_OPL *OPL,OPL_PORTHANDLER_W PortHandler_w,OPL_PORTHANDLER_R PortHandler_r,int param)
  1195. {
  1196. OPL->porthandler_w = PortHandler_w;
  1197. OPL->porthandler_r = PortHandler_r;
  1198. OPL->port_param = param;
  1199. }
  1200. void OPLSetKeyboardHandler(FM_OPL *OPL,OPL_PORTHANDLER_W KeyboardHandler_w,OPL_PORTHANDLER_R KeyboardHandler_r,int param)
  1201. {
  1202. OPL->keyboardhandler_w = KeyboardHandler_w;
  1203. OPL->keyboardhandler_r = KeyboardHandler_r;
  1204. OPL->keyboard_param = param;
  1205. }
  1206. #endif
  1207. /* ---------- YM3812 I/O interface ---------- */
  1208. int OPLWrite(FM_OPL *OPL,int a,int v)
  1209. {
  1210. if( !(a&1) )
  1211. { /* address port */
  1212. OPL->address = v & 0xff;
  1213. }
  1214. else
  1215. { /* data port */
  1216. if(OPL->UpdateHandler) OPL->UpdateHandler(OPL->UpdateParam,0);
  1217. #ifdef OPL_OUTPUT_LOG
  1218. if(opl_dbg_fp)
  1219. {
  1220. for(opl_dbg_chip=0;opl_dbg_chip<opl_dbg_maxchip;opl_dbg_chip++)
  1221. if( opl_dbg_opl[opl_dbg_chip] == OPL) break;
  1222. fprintf(opl_dbg_fp,"%c%c%c",0x10+opl_dbg_chip,OPL->address,v);
  1223. }
  1224. #endif
  1225. OPLWriteReg(OPL,OPL->address,v);
  1226. }
  1227. return OPL->status>>7;
  1228. }
  1229. unsigned char OPLRead(FM_OPL *OPL,int a)
  1230. {
  1231. if( !(a&1) )
  1232. { /* status port */
  1233. return OPL->status & (OPL->statusmask|0x80);
  1234. }
  1235. /* data port */
  1236. switch(OPL->address)
  1237. {
  1238. case 0x05: /* KeyBoard IN */
  1239. if(OPL->type&OPL_TYPE_KEYBOARD)
  1240. {
  1241. if(OPL->keyboardhandler_r)
  1242. return OPL->keyboardhandler_r(OPL->keyboard_param);
  1243. else
  1244. LOG(LOG_WAR,("OPL:read unmapped KEYBOARD port\n"));
  1245. }
  1246. return 0;
  1247. #if 0
  1248. case 0x0f: /* ADPCM-DATA */
  1249. return 0;
  1250. #endif
  1251. case 0x19: /* I/O DATA */
  1252. if(OPL->type&OPL_TYPE_IO)
  1253. {
  1254. if(OPL->porthandler_r)
  1255. return OPL->porthandler_r(OPL->port_param);
  1256. else
  1257. LOG(LOG_WAR,("OPL:read unmapped I/O port\n"));
  1258. }
  1259. return 0;
  1260. case 0x1a: /* PCM-DATA */
  1261. return 0;
  1262. }
  1263. return 0;
  1264. }
  1265. int OPLTimerOver(FM_OPL *OPL,int c)
  1266. {
  1267. if( c )
  1268. { /* Timer B */
  1269. OPL_STATUS_SET(OPL,0x20);
  1270. }
  1271. else
  1272. { /* Timer A */
  1273. OPL_STATUS_SET(OPL,0x40);
  1274. /* CSM mode key,TL controll */
  1275. if( OPL->mode & 0x80 )
  1276. { /* CSM mode total level latch and auto key on */
  1277. int ch;
  1278. if(OPL->UpdateHandler) OPL->UpdateHandler(OPL->UpdateParam,0);
  1279. for(ch=0;ch<9;ch++)
  1280. CSMKeyControll( &OPL->P_CH[ch] );
  1281. }
  1282. }
  1283. /* reload timer */
  1284. if (OPL->TimerHandler) (OPL->TimerHandler)(OPL->TimerParam+c,(double)OPL->T[c]*OPL->TimerBase);
  1285. return OPL->status>>7;
  1286. }