fmopl.c 37 KB

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