fmopl.c 39 KB

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