fmopl.c 35 KB

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