2
0

fmopl.c 30 KB

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