fmopl.c 39 KB

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