fmopl.c 31 KB

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