fmopl.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392
  1. /*
  2. **
  3. ** File: fmopl.c - software implementation of FM sound generator
  4. ** types OPL and OPL2
  5. **
  6. ** Copyright (C) 2002,2003 Jarek Burczynski (bujar at mame dot net)
  7. ** Copyright (C) 1999,2000 Tatsuyuki Satoh , MultiArcadeMachineEmulator development
  8. **
  9. ** Version 0.72
  10. **
  11. Revision History:
  12. 04-08-2003 Jarek Burczynski:
  13. - removed BFRDY hack. BFRDY is busy flag, and it should be 0 only when the chip
  14. handles memory read/write or during the adpcm synthesis when the chip
  15. requests another byte of ADPCM data.
  16. 24-07-2003 Jarek Burczynski:
  17. - added a small hack for Y8950 status BFRDY flag (bit 3 should be set after
  18. some (unknown) delay). Right now it's always set.
  19. 14-06-2003 Jarek Burczynski:
  20. - implemented all of the status register flags in Y8950 emulation
  21. - renamed Y8950SetDeltaTMemory() parameters from _rom_ to _mem_ since
  22. they can be either RAM or ROM
  23. 08-10-2002 Jarek Burczynski (thanks to Dox for the YM3526 chip)
  24. - corrected YM3526Read() to always set bit 2 and bit 1
  25. to HIGH state - identical to YM3812Read (verified on real YM3526)
  26. 04-28-2002 Jarek Burczynski:
  27. - binary exact Envelope Generator (verified on real YM3812);
  28. compared to YM2151: the EG clock is equal to internal_clock,
  29. rates are 2 times slower and volume resolution is one bit less
  30. - modified interface functions (they no longer return pointer -
  31. that's internal to the emulator now):
  32. - new wrapper functions for OPLCreate: YM3526Init(), YM3812Init() and Y8950Init()
  33. - corrected 'off by one' error in feedback calculations (when feedback is off)
  34. - enabled waveform usage (credit goes to Vlad Romascanu and zazzal22)
  35. - speeded up noise generator calculations (Nicola Salmoria)
  36. 03-24-2002 Jarek Burczynski (thanks to Dox for the YM3812 chip)
  37. Complete rewrite (all verified on real YM3812):
  38. - corrected sin_tab and tl_tab data
  39. - corrected operator output calculations
  40. - corrected waveform_select_enable register;
  41. simply: ignore all writes to waveform_select register when
  42. waveform_select_enable == 0 and do not change the waveform previously selected.
  43. - corrected KSR handling
  44. - corrected Envelope Generator: attack shape, Sustain mode and
  45. Percussive/Non-percussive modes handling
  46. - Envelope Generator rates are two times slower now
  47. - LFO amplitude (tremolo) and phase modulation (vibrato)
  48. - rhythm sounds phase generation
  49. - white noise generator (big thanks to Olivier Galibert for mentioning Berlekamp-Massey algorithm)
  50. - corrected key on/off handling (the 'key' signal is ORed from three sources: FM, rhythm and CSM)
  51. - funky details (like ignoring output of operator 1 in BD rhythm sound when connect == 1)
  52. 12-28-2001 Acho A. Tang
  53. - reflected Delta-T EOS status on Y8950 status port.
  54. - fixed subscription range of attack/decay tables
  55. To do:
  56. add delay before key off in CSM mode (see CSMKeyControll)
  57. verify volume of the FM part on the Y8950
  58. */
  59. #include <stdio.h>
  60. #include <stdlib.h>
  61. #include <string.h>
  62. #include <math.h>
  63. //#include "driver.h" /* use M.A.M.E. */
  64. //#include "ymdeltat.h"
  65. #include "fmopl.h"
  66. #ifndef PI
  67. #define PI 3.14159265358979323846
  68. #endif
  69. # define INLINE
  70. /* output final shift */
  71. #if (OPL_SAMPLE_BITS==16)
  72. #define FINAL_SH (0)
  73. #define MAXOUT (+32767)
  74. #define MINOUT (-32768)
  75. #else
  76. #define FINAL_SH (8)
  77. #define MAXOUT (+127)
  78. #define MINOUT (-128)
  79. #endif
  80. #define FREQ_SH 16 /* 16.16 fixed point (frequency calculations) */
  81. #define EG_SH 16 /* 16.16 fixed point (EG timing) */
  82. #define LFO_SH 24 /* 8.24 fixed point (LFO calculations) */
  83. #define TIMER_SH 16 /* 16.16 fixed point (timers calculations) */
  84. #define FREQ_MASK ((1<<FREQ_SH)-1)
  85. /* envelope output entries */
  86. #define ENV_BITS 10
  87. #define ENV_LEN (1<<ENV_BITS)
  88. #define ENV_STEP (128.0/ENV_LEN)
  89. #define MAX_ATT_INDEX ((1<<(ENV_BITS-1))-1) /*511*/
  90. #define MIN_ATT_INDEX (0)
  91. /* sinwave entries */
  92. #define SIN_BITS 10
  93. #define SIN_LEN (1<<SIN_BITS)
  94. #define SIN_MASK (SIN_LEN-1)
  95. #define TL_RES_LEN (256) /* 8 bits addressing (real chip) */
  96. /* register number to channel number , slot offset */
  97. #define SLOT1 0
  98. #define SLOT2 1
  99. /* Envelope Generator phases */
  100. #define EG_ATT 4
  101. #define EG_DEC 3
  102. #define EG_SUS 2
  103. #define EG_REL 1
  104. #define EG_OFF 0
  105. /* save output as raw 16-bit sample */
  106. /*#define SAVE_SAMPLE*/
  107. #ifdef SAVE_SAMPLE
  108. INLINE signed int acc_calc(signed int value)
  109. {
  110. if (value>=0)
  111. {
  112. if (value < 0x0200)
  113. return (value & ~0);
  114. if (value < 0x0400)
  115. return (value & ~1);
  116. if (value < 0x0800)
  117. return (value & ~3);
  118. if (value < 0x1000)
  119. return (value & ~7);
  120. if (value < 0x2000)
  121. return (value & ~15);
  122. if (value < 0x4000)
  123. return (value & ~31);
  124. return (value & ~63);
  125. }
  126. /*else value < 0*/
  127. if (value > -0x0200)
  128. return (~abs(value) & ~0);
  129. if (value > -0x0400)
  130. return (~abs(value) & ~1);
  131. if (value > -0x0800)
  132. return (~abs(value) & ~3);
  133. if (value > -0x1000)
  134. return (~abs(value) & ~7);
  135. if (value > -0x2000)
  136. return (~abs(value) & ~15);
  137. if (value > -0x4000)
  138. return (~abs(value) & ~31);
  139. return (~abs(value) & ~63);
  140. }
  141. static FILE *sample[1];
  142. #if 1 /*save to MONO file */
  143. #define SAVE_ALL_CHANNELS \
  144. { signed int pom = acc_calc(lt); \
  145. fputc((unsigned short)pom&0xff,sample[0]); \
  146. fputc(((unsigned short)pom>>8)&0xff,sample[0]); \
  147. }
  148. #else /*save to STEREO file */
  149. #define SAVE_ALL_CHANNELS \
  150. { signed int pom = lt; \
  151. fputc((unsigned short)pom&0xff,sample[0]); \
  152. fputc(((unsigned short)pom>>8)&0xff,sample[0]); \
  153. pom = rt; \
  154. fputc((unsigned short)pom&0xff,sample[0]); \
  155. fputc(((unsigned short)pom>>8)&0xff,sample[0]); \
  156. }
  157. #endif
  158. #endif
  159. /* #define LOG_CYM_FILE */
  160. #ifdef LOG_CYM_FILE
  161. FILE * cymfile = NULL;
  162. #endif
  163. /* mapping of register number (offset) to slot number used by the emulator */
  164. static const int slot_array[32]=
  165. {
  166. 0, 2, 4, 1, 3, 5,-1,-1,
  167. 6, 8,10, 7, 9,11,-1,-1,
  168. 12,14,16,13,15,17,-1,-1,
  169. -1,-1,-1,-1,-1,-1,-1,-1
  170. };
  171. /* key scale level */
  172. /* table is 3dB/octave , DV converts this into 6dB/octave */
  173. /* 0.1875 is bit 0 weight of the envelope counter (volume) expressed in the 'decibel' scale */
  174. #define DV (0.1875/2.0)
  175. static const UINT32 ksl_tab[8*16]=
  176. {
  177. /* OCT 0 */
  178. 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
  179. 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
  180. 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
  181. 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
  182. /* OCT 1 */
  183. 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
  184. 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
  185. 0.000/DV, 0.750/DV, 1.125/DV, 1.500/DV,
  186. 1.875/DV, 2.250/DV, 2.625/DV, 3.000/DV,
  187. /* OCT 2 */
  188. 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
  189. 0.000/DV, 1.125/DV, 1.875/DV, 2.625/DV,
  190. 3.000/DV, 3.750/DV, 4.125/DV, 4.500/DV,
  191. 4.875/DV, 5.250/DV, 5.625/DV, 6.000/DV,
  192. /* OCT 3 */
  193. 0.000/DV, 0.000/DV, 0.000/DV, 1.875/DV,
  194. 3.000/DV, 4.125/DV, 4.875/DV, 5.625/DV,
  195. 6.000/DV, 6.750/DV, 7.125/DV, 7.500/DV,
  196. 7.875/DV, 8.250/DV, 8.625/DV, 9.000/DV,
  197. /* OCT 4 */
  198. 0.000/DV, 0.000/DV, 3.000/DV, 4.875/DV,
  199. 6.000/DV, 7.125/DV, 7.875/DV, 8.625/DV,
  200. 9.000/DV, 9.750/DV,10.125/DV,10.500/DV,
  201. 10.875/DV,11.250/DV,11.625/DV,12.000/DV,
  202. /* OCT 5 */
  203. 0.000/DV, 3.000/DV, 6.000/DV, 7.875/DV,
  204. 9.000/DV,10.125/DV,10.875/DV,11.625/DV,
  205. 12.000/DV,12.750/DV,13.125/DV,13.500/DV,
  206. 13.875/DV,14.250/DV,14.625/DV,15.000/DV,
  207. /* OCT 6 */
  208. 0.000/DV, 6.000/DV, 9.000/DV,10.875/DV,
  209. 12.000/DV,13.125/DV,13.875/DV,14.625/DV,
  210. 15.000/DV,15.750/DV,16.125/DV,16.500/DV,
  211. 16.875/DV,17.250/DV,17.625/DV,18.000/DV,
  212. /* OCT 7 */
  213. 0.000/DV, 9.000/DV,12.000/DV,13.875/DV,
  214. 15.000/DV,16.125/DV,16.875/DV,17.625/DV,
  215. 18.000/DV,18.750/DV,19.125/DV,19.500/DV,
  216. 19.875/DV,20.250/DV,20.625/DV,21.000/DV
  217. };
  218. #undef DV
  219. /* sustain level table (3dB per step) */
  220. /* 0 - 15: 0, 3, 6, 9,12,15,18,21,24,27,30,33,36,39,42,93 (dB)*/
  221. #define SC(db) (UINT32) ( db * (2.0/ENV_STEP) )
  222. static const UINT32 sl_tab[16]={
  223. SC( 0),SC( 1),SC( 2),SC(3 ),SC(4 ),SC(5 ),SC(6 ),SC( 7),
  224. SC( 8),SC( 9),SC(10),SC(11),SC(12),SC(13),SC(14),SC(31)
  225. };
  226. #undef SC
  227. #define RATE_STEPS (8)
  228. static const unsigned char eg_inc[15*RATE_STEPS]={
  229. /*cycle:0 1 2 3 4 5 6 7*/
  230. /* 0 */ 0,1, 0,1, 0,1, 0,1, /* rates 00..12 0 (increment by 0 or 1) */
  231. /* 1 */ 0,1, 0,1, 1,1, 0,1, /* rates 00..12 1 */
  232. /* 2 */ 0,1, 1,1, 0,1, 1,1, /* rates 00..12 2 */
  233. /* 3 */ 0,1, 1,1, 1,1, 1,1, /* rates 00..12 3 */
  234. /* 4 */ 1,1, 1,1, 1,1, 1,1, /* rate 13 0 (increment by 1) */
  235. /* 5 */ 1,1, 1,2, 1,1, 1,2, /* rate 13 1 */
  236. /* 6 */ 1,2, 1,2, 1,2, 1,2, /* rate 13 2 */
  237. /* 7 */ 1,2, 2,2, 1,2, 2,2, /* rate 13 3 */
  238. /* 8 */ 2,2, 2,2, 2,2, 2,2, /* rate 14 0 (increment by 2) */
  239. /* 9 */ 2,2, 2,4, 2,2, 2,4, /* rate 14 1 */
  240. /*10 */ 2,4, 2,4, 2,4, 2,4, /* rate 14 2 */
  241. /*11 */ 2,4, 4,4, 2,4, 4,4, /* rate 14 3 */
  242. /*12 */ 4,4, 4,4, 4,4, 4,4, /* rates 15 0, 15 1, 15 2, 15 3 (increment by 4) */
  243. /*13 */ 8,8, 8,8, 8,8, 8,8, /* rates 15 2, 15 3 for attack */
  244. /*14 */ 0,0, 0,0, 0,0, 0,0, /* infinity rates for attack and decay(s) */
  245. };
  246. #define O(a) (a*RATE_STEPS)
  247. /*note that there is no O(13) in this table - it's directly in the code */
  248. static const unsigned char eg_rate_select[16+64+16]={ /* Envelope Generator rates (16 + 64 rates + 16 RKS) */
  249. /* 16 infinite time rates */
  250. O(14),O(14),O(14),O(14),O(14),O(14),O(14),O(14),
  251. O(14),O(14),O(14),O(14),O(14),O(14),O(14),O(14),
  252. /* rates 00-12 */
  253. O( 0),O( 1),O( 2),O( 3),
  254. O( 0),O( 1),O( 2),O( 3),
  255. O( 0),O( 1),O( 2),O( 3),
  256. O( 0),O( 1),O( 2),O( 3),
  257. O( 0),O( 1),O( 2),O( 3),
  258. O( 0),O( 1),O( 2),O( 3),
  259. O( 0),O( 1),O( 2),O( 3),
  260. O( 0),O( 1),O( 2),O( 3),
  261. O( 0),O( 1),O( 2),O( 3),
  262. O( 0),O( 1),O( 2),O( 3),
  263. O( 0),O( 1),O( 2),O( 3),
  264. O( 0),O( 1),O( 2),O( 3),
  265. O( 0),O( 1),O( 2),O( 3),
  266. /* rate 13 */
  267. O( 4),O( 5),O( 6),O( 7),
  268. /* rate 14 */
  269. O( 8),O( 9),O(10),O(11),
  270. /* rate 15 */
  271. O(12),O(12),O(12),O(12),
  272. /* 16 dummy rates (same as 15 3) */
  273. O(12),O(12),O(12),O(12),O(12),O(12),O(12),O(12),
  274. O(12),O(12),O(12),O(12),O(12),O(12),O(12),O(12),
  275. };
  276. #undef O
  277. /*rate 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 */
  278. /*shift 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0 */
  279. /*mask 4095, 2047, 1023, 511, 255, 127, 63, 31, 15, 7, 3, 1, 0, 0, 0, 0 */
  280. #define O(a) (a*1)
  281. static const unsigned char eg_rate_shift[16+64+16]={ /* Envelope Generator counter shifts (16 + 64 rates + 16 RKS) */
  282. /* 16 infinite time rates */
  283. O(0),O(0),O(0),O(0),O(0),O(0),O(0),O(0),
  284. O(0),O(0),O(0),O(0),O(0),O(0),O(0),O(0),
  285. /* rates 00-12 */
  286. O(12),O(12),O(12),O(12),
  287. O(11),O(11),O(11),O(11),
  288. O(10),O(10),O(10),O(10),
  289. O( 9),O( 9),O( 9),O( 9),
  290. O( 8),O( 8),O( 8),O( 8),
  291. O( 7),O( 7),O( 7),O( 7),
  292. O( 6),O( 6),O( 6),O( 6),
  293. O( 5),O( 5),O( 5),O( 5),
  294. O( 4),O( 4),O( 4),O( 4),
  295. O( 3),O( 3),O( 3),O( 3),
  296. O( 2),O( 2),O( 2),O( 2),
  297. O( 1),O( 1),O( 1),O( 1),
  298. O( 0),O( 0),O( 0),O( 0),
  299. /* rate 13 */
  300. O( 0),O( 0),O( 0),O( 0),
  301. /* rate 14 */
  302. O( 0),O( 0),O( 0),O( 0),
  303. /* rate 15 */
  304. O( 0),O( 0),O( 0),O( 0),
  305. /* 16 dummy rates (same as 15 3) */
  306. O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),
  307. O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),
  308. };
  309. #undef O
  310. /* multiple table */
  311. #define ML 2
  312. static const UINT8 mul_tab[16]= {
  313. /* 1/2, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,10,12,12,15,15 */
  314. 0.50*ML, 1.00*ML, 2.00*ML, 3.00*ML, 4.00*ML, 5.00*ML, 6.00*ML, 7.00*ML,
  315. 8.00*ML, 9.00*ML,10.00*ML,10.00*ML,12.00*ML,12.00*ML,15.00*ML,15.00*ML
  316. };
  317. #undef ML
  318. /* TL_TAB_LEN is calculated as:
  319. * 12 - sinus amplitude bits (Y axis)
  320. * 2 - sinus sign bit (Y axis)
  321. * TL_RES_LEN - sinus resolution (X axis)
  322. */
  323. #define TL_TAB_LEN (12*2*TL_RES_LEN)
  324. static signed int tl_tab[TL_TAB_LEN];
  325. #define ENV_QUIET (TL_TAB_LEN>>4)
  326. /* sin waveform table in 'decibel' scale */
  327. /* four waveforms on OPL2 type chips */
  328. static unsigned int sin_tab[SIN_LEN * 4];
  329. /* LFO Amplitude Modulation table (verified on real YM3812)
  330. 27 output levels (triangle waveform); 1 level takes one of: 192, 256 or 448 samples
  331. Length: 210 elements.
  332. Each of the elements has to be repeated
  333. exactly 64 times (on 64 consecutive samples).
  334. The whole table takes: 64 * 210 = 13440 samples.
  335. When AM = 1 data is used directly
  336. When AM = 0 data is divided by 4 before being used (loosing precision is important)
  337. */
  338. #define LFO_AM_TAB_ELEMENTS 210
  339. static const UINT8 lfo_am_table[LFO_AM_TAB_ELEMENTS] = {
  340. 0,0,0,0,0,0,0,
  341. 1,1,1,1,
  342. 2,2,2,2,
  343. 3,3,3,3,
  344. 4,4,4,4,
  345. 5,5,5,5,
  346. 6,6,6,6,
  347. 7,7,7,7,
  348. 8,8,8,8,
  349. 9,9,9,9,
  350. 10,10,10,10,
  351. 11,11,11,11,
  352. 12,12,12,12,
  353. 13,13,13,13,
  354. 14,14,14,14,
  355. 15,15,15,15,
  356. 16,16,16,16,
  357. 17,17,17,17,
  358. 18,18,18,18,
  359. 19,19,19,19,
  360. 20,20,20,20,
  361. 21,21,21,21,
  362. 22,22,22,22,
  363. 23,23,23,23,
  364. 24,24,24,24,
  365. 25,25,25,25,
  366. 26,26,26,
  367. 25,25,25,25,
  368. 24,24,24,24,
  369. 23,23,23,23,
  370. 22,22,22,22,
  371. 21,21,21,21,
  372. 20,20,20,20,
  373. 19,19,19,19,
  374. 18,18,18,18,
  375. 17,17,17,17,
  376. 16,16,16,16,
  377. 15,15,15,15,
  378. 14,14,14,14,
  379. 13,13,13,13,
  380. 12,12,12,12,
  381. 11,11,11,11,
  382. 10,10,10,10,
  383. 9,9,9,9,
  384. 8,8,8,8,
  385. 7,7,7,7,
  386. 6,6,6,6,
  387. 5,5,5,5,
  388. 4,4,4,4,
  389. 3,3,3,3,
  390. 2,2,2,2,
  391. 1,1,1,1
  392. };
  393. /* LFO Phase Modulation table (verified on real YM3812) */
  394. static const INT8 lfo_pm_table[8*8*2] = {
  395. /* FNUM2/FNUM = 00 0xxxxxxx (0x0000) */
  396. 0, 0, 0, 0, 0, 0, 0, 0, /*LFO PM depth = 0*/
  397. 0, 0, 0, 0, 0, 0, 0, 0, /*LFO PM depth = 1*/
  398. /* FNUM2/FNUM = 00 1xxxxxxx (0x0080) */
  399. 0, 0, 0, 0, 0, 0, 0, 0, /*LFO PM depth = 0*/
  400. 1, 0, 0, 0,-1, 0, 0, 0, /*LFO PM depth = 1*/
  401. /* FNUM2/FNUM = 01 0xxxxxxx (0x0100) */
  402. 1, 0, 0, 0,-1, 0, 0, 0, /*LFO PM depth = 0*/
  403. 2, 1, 0,-1,-2,-1, 0, 1, /*LFO PM depth = 1*/
  404. /* FNUM2/FNUM = 01 1xxxxxxx (0x0180) */
  405. 1, 0, 0, 0,-1, 0, 0, 0, /*LFO PM depth = 0*/
  406. 3, 1, 0,-1,-3,-1, 0, 1, /*LFO PM depth = 1*/
  407. /* FNUM2/FNUM = 10 0xxxxxxx (0x0200) */
  408. 2, 1, 0,-1,-2,-1, 0, 1, /*LFO PM depth = 0*/
  409. 4, 2, 0,-2,-4,-2, 0, 2, /*LFO PM depth = 1*/
  410. /* FNUM2/FNUM = 10 1xxxxxxx (0x0280) */
  411. 2, 1, 0,-1,-2,-1, 0, 1, /*LFO PM depth = 0*/
  412. 5, 2, 0,-2,-5,-2, 0, 2, /*LFO PM depth = 1*/
  413. /* FNUM2/FNUM = 11 0xxxxxxx (0x0300) */
  414. 3, 1, 0,-1,-3,-1, 0, 1, /*LFO PM depth = 0*/
  415. 6, 3, 0,-3,-6,-3, 0, 3, /*LFO PM depth = 1*/
  416. /* FNUM2/FNUM = 11 1xxxxxxx (0x0380) */
  417. 3, 1, 0,-1,-3,-1, 0, 1, /*LFO PM depth = 0*/
  418. 7, 3, 0,-3,-7,-3, 0, 3 /*LFO PM depth = 1*/
  419. };
  420. /* lock level of common table */
  421. static int num_lock = 0;
  422. static void *cur_chip = NULL; /* current chip pointer */
  423. static OPL_SLOT *SLOT7_1, *SLOT7_2, *SLOT8_1, *SLOT8_2;
  424. static signed int phase_modulation; /* phase modulation input (SLOT 2) */
  425. static signed int output[1];
  426. #if BUILD_Y8950
  427. static INT32 output_deltat[4]; /* for Y8950 DELTA-T, chip is mono, that 4 here is just for safety */
  428. #endif
  429. static UINT32 LFO_AM;
  430. static INT32 LFO_PM;
  431. INLINE int limit( int val, int max, int min ) {
  432. if ( val > max )
  433. val = max;
  434. else if ( val < min )
  435. val = min;
  436. return val;
  437. }
  438. /* status set and IRQ handling */
  439. INLINE void OPL_STATUS_SET(FM_OPL *OPL,int flag)
  440. {
  441. /* set status flag */
  442. OPL->status |= flag;
  443. if(!(OPL->status & 0x80))
  444. {
  445. if(OPL->status & OPL->statusmask)
  446. { /* IRQ on */
  447. OPL->status |= 0x80;
  448. /* callback user interrupt handler (IRQ is OFF to ON) */
  449. if(OPL->IRQHandler) (OPL->IRQHandler)(OPL->IRQParam,1);
  450. }
  451. }
  452. }
  453. /* status reset and IRQ handling */
  454. INLINE void OPL_STATUS_RESET(FM_OPL *OPL,int flag)
  455. {
  456. /* reset status flag */
  457. OPL->status &=~flag;
  458. if((OPL->status & 0x80))
  459. {
  460. if (!(OPL->status & OPL->statusmask) )
  461. {
  462. OPL->status &= 0x7f;
  463. /* callback user interrupt handler (IRQ is ON to OFF) */
  464. if(OPL->IRQHandler) (OPL->IRQHandler)(OPL->IRQParam,0);
  465. }
  466. }
  467. }
  468. /* IRQ mask set */
  469. INLINE void OPL_STATUSMASK_SET(FM_OPL *OPL,int flag)
  470. {
  471. OPL->statusmask = flag;
  472. /* IRQ handling check */
  473. OPL_STATUS_SET(OPL,0);
  474. OPL_STATUS_RESET(OPL,0);
  475. }
  476. /* advance LFO to next sample */
  477. INLINE void advance_lfo(FM_OPL *OPL)
  478. {
  479. UINT8 tmp;
  480. /* LFO */
  481. OPL->lfo_am_cnt += OPL->lfo_am_inc;
  482. if (OPL->lfo_am_cnt >= (LFO_AM_TAB_ELEMENTS<<LFO_SH) ) /* lfo_am_table is 210 elements long */
  483. OPL->lfo_am_cnt -= (LFO_AM_TAB_ELEMENTS<<LFO_SH);
  484. tmp = lfo_am_table[ OPL->lfo_am_cnt >> LFO_SH ];
  485. if (OPL->lfo_am_depth)
  486. LFO_AM = tmp;
  487. else
  488. LFO_AM = tmp>>2;
  489. OPL->lfo_pm_cnt += OPL->lfo_pm_inc;
  490. LFO_PM = ((OPL->lfo_pm_cnt>>LFO_SH) & 7) | OPL->lfo_pm_depth_range;
  491. }
  492. /* advance to next sample */
  493. INLINE void advance(FM_OPL *OPL)
  494. {
  495. OPL_CH *CH;
  496. OPL_SLOT *op;
  497. int i;
  498. OPL->eg_timer += OPL->eg_timer_add;
  499. while (OPL->eg_timer >= OPL->eg_timer_overflow)
  500. {
  501. OPL->eg_timer -= OPL->eg_timer_overflow;
  502. OPL->eg_cnt++;
  503. for (i=0; i<9*2; i++)
  504. {
  505. CH = &OPL->P_CH[i/2];
  506. op = &CH->SLOT[i&1];
  507. /* Envelope Generator */
  508. switch(op->state)
  509. {
  510. case EG_ATT: /* attack phase */
  511. if ( !(OPL->eg_cnt & ((1<<op->eg_sh_ar)-1) ) )
  512. {
  513. op->volume += (~op->volume *
  514. (eg_inc[op->eg_sel_ar + ((OPL->eg_cnt>>op->eg_sh_ar)&7)])
  515. ) >>3;
  516. if (op->volume <= MIN_ATT_INDEX)
  517. {
  518. op->volume = MIN_ATT_INDEX;
  519. op->state = EG_DEC;
  520. }
  521. }
  522. break;
  523. case EG_DEC: /* decay phase */
  524. if ( !(OPL->eg_cnt & ((1<<op->eg_sh_dr)-1) ) )
  525. {
  526. op->volume += eg_inc[op->eg_sel_dr + ((OPL->eg_cnt>>op->eg_sh_dr)&7)];
  527. if ( op->volume >= op->sl )
  528. op->state = EG_SUS;
  529. }
  530. break;
  531. case EG_SUS: /* sustain phase */
  532. /* this is important behaviour:
  533. one can change percusive/non-percussive modes on the fly and
  534. the chip will remain in sustain phase - verified on real YM3812 */
  535. if(op->eg_type) /* non-percussive mode */
  536. {
  537. /* do nothing */
  538. }
  539. else /* percussive mode */
  540. {
  541. /* during sustain phase chip adds Release Rate (in percussive mode) */
  542. if ( !(OPL->eg_cnt & ((1<<op->eg_sh_rr)-1) ) )
  543. {
  544. op->volume += eg_inc[op->eg_sel_rr + ((OPL->eg_cnt>>op->eg_sh_rr)&7)];
  545. if ( op->volume >= MAX_ATT_INDEX )
  546. op->volume = MAX_ATT_INDEX;
  547. }
  548. /* else do nothing in sustain phase */
  549. }
  550. break;
  551. case EG_REL: /* release phase */
  552. if ( !(OPL->eg_cnt & ((1<<op->eg_sh_rr)-1) ) )
  553. {
  554. op->volume += eg_inc[op->eg_sel_rr + ((OPL->eg_cnt>>op->eg_sh_rr)&7)];
  555. if ( op->volume >= MAX_ATT_INDEX )
  556. {
  557. op->volume = MAX_ATT_INDEX;
  558. op->state = EG_OFF;
  559. }
  560. }
  561. break;
  562. default:
  563. break;
  564. }
  565. }
  566. }
  567. for (i=0; i<9*2; i++)
  568. {
  569. CH = &OPL->P_CH[i/2];
  570. op = &CH->SLOT[i&1];
  571. /* Phase Generator */
  572. if(op->vib)
  573. {
  574. UINT8 block;
  575. unsigned int block_fnum = CH->block_fnum;
  576. unsigned int fnum_lfo = (block_fnum&0x0380) >> 7;
  577. signed int lfo_fn_table_index_offset = lfo_pm_table[LFO_PM + 16*fnum_lfo ];
  578. if (lfo_fn_table_index_offset) /* LFO phase modulation active */
  579. {
  580. block_fnum += lfo_fn_table_index_offset;
  581. block = (block_fnum&0x1c00) >> 10;
  582. op->Cnt += (OPL->fn_tab[block_fnum&0x03ff] >> (7-block)) * op->mul;
  583. }
  584. else /* LFO phase modulation = zero */
  585. {
  586. op->Cnt += op->Incr;
  587. }
  588. }
  589. else /* LFO phase modulation disabled for this operator */
  590. {
  591. op->Cnt += op->Incr;
  592. }
  593. }
  594. /* The Noise Generator of the YM3812 is 23-bit shift register.
  595. * Period is equal to 2^23-2 samples.
  596. * Register works at sampling frequency of the chip, so output
  597. * can change on every sample.
  598. *
  599. * Output of the register and input to the bit 22 is:
  600. * bit0 XOR bit14 XOR bit15 XOR bit22
  601. *
  602. * Simply use bit 22 as the noise output.
  603. */
  604. OPL->noise_p += OPL->noise_f;
  605. i = OPL->noise_p >> FREQ_SH; /* number of events (shifts of the shift register) */
  606. OPL->noise_p &= FREQ_MASK;
  607. while (i)
  608. {
  609. /*
  610. UINT32 j;
  611. j = ( (OPL->noise_rng) ^ (OPL->noise_rng>>14) ^ (OPL->noise_rng>>15) ^ (OPL->noise_rng>>22) ) & 1;
  612. OPL->noise_rng = (j<<22) | (OPL->noise_rng>>1);
  613. */
  614. /*
  615. Instead of doing all the logic operations above, we
  616. use a trick here (and use bit 0 as the noise output).
  617. The difference is only that the noise bit changes one
  618. step ahead. This doesn't matter since we don't know
  619. what is real state of the noise_rng after the reset.
  620. */
  621. if (OPL->noise_rng & 1) OPL->noise_rng ^= 0x800302;
  622. OPL->noise_rng >>= 1;
  623. i--;
  624. }
  625. }
  626. INLINE signed int op_calc(UINT32 phase, unsigned int env, signed int pm, unsigned int wave_tab)
  627. {
  628. UINT32 p;
  629. p = (env<<4) + sin_tab[wave_tab + ((((signed int)((phase & ~FREQ_MASK) + (pm<<16))) >> FREQ_SH ) & SIN_MASK) ];
  630. if (p >= TL_TAB_LEN)
  631. return 0;
  632. return tl_tab[p];
  633. }
  634. INLINE signed int op_calc1(UINT32 phase, unsigned int env, signed int pm, unsigned int wave_tab)
  635. {
  636. UINT32 p;
  637. p = (env<<4) + sin_tab[wave_tab + ((((signed int)((phase & ~FREQ_MASK) + pm )) >> FREQ_SH ) & SIN_MASK) ];
  638. if (p >= TL_TAB_LEN)
  639. return 0;
  640. return tl_tab[p];
  641. }
  642. #define volume_calc(OP) ((OP)->TLL + ((UINT32)(OP)->volume) + (LFO_AM & (OP)->AMmask))
  643. /* calculate output */
  644. INLINE void OPL_CALC_CH( OPL_CH *CH )
  645. {
  646. OPL_SLOT *SLOT;
  647. unsigned int env;
  648. signed int out;
  649. phase_modulation = 0;
  650. /* SLOT 1 */
  651. SLOT = &CH->SLOT[SLOT1];
  652. env = volume_calc(SLOT);
  653. out = SLOT->op1_out[0] + SLOT->op1_out[1];
  654. SLOT->op1_out[0] = SLOT->op1_out[1];
  655. *SLOT->connect1 += SLOT->op1_out[0];
  656. SLOT->op1_out[1] = 0;
  657. if( env < ENV_QUIET )
  658. {
  659. if (!SLOT->FB)
  660. out = 0;
  661. SLOT->op1_out[1] = op_calc1(SLOT->Cnt, env, (out<<SLOT->FB), SLOT->wavetable );
  662. }
  663. /* SLOT 2 */
  664. SLOT++;
  665. env = volume_calc(SLOT);
  666. if( env < ENV_QUIET )
  667. output[0] += op_calc(SLOT->Cnt, env, phase_modulation, SLOT->wavetable);
  668. }
  669. /*
  670. operators used in the rhythm sounds generation process:
  671. Envelope Generator:
  672. channel operator register number Bass High Snare Tom Top
  673. / slot number TL ARDR SLRR Wave Drum Hat Drum Tom Cymbal
  674. 6 / 0 12 50 70 90 f0 +
  675. 6 / 1 15 53 73 93 f3 +
  676. 7 / 0 13 51 71 91 f1 +
  677. 7 / 1 16 54 74 94 f4 +
  678. 8 / 0 14 52 72 92 f2 +
  679. 8 / 1 17 55 75 95 f5 +
  680. Phase Generator:
  681. channel operator register number Bass High Snare Tom Top
  682. / slot number MULTIPLE Drum Hat Drum Tom Cymbal
  683. 6 / 0 12 30 +
  684. 6 / 1 15 33 +
  685. 7 / 0 13 31 + + +
  686. 7 / 1 16 34 ----- n o t u s e d -----
  687. 8 / 0 14 32 +
  688. 8 / 1 17 35 + +
  689. channel operator register number Bass High Snare Tom Top
  690. number number BLK/FNUM2 FNUM Drum Hat Drum Tom Cymbal
  691. 6 12,15 B6 A6 +
  692. 7 13,16 B7 A7 + + +
  693. 8 14,17 B8 A8 + + +
  694. */
  695. /* calculate rhythm */
  696. INLINE void OPL_CALC_RH( OPL_CH *CH, unsigned int noise )
  697. {
  698. OPL_SLOT *SLOT;
  699. signed int out;
  700. unsigned int env;
  701. /* Bass Drum (verified on real YM3812):
  702. - depends on the channel 6 'connect' register:
  703. when connect = 0 it works the same as in normal (non-rhythm) mode (op1->op2->out)
  704. when connect = 1 _only_ operator 2 is present on output (op2->out), operator 1 is ignored
  705. - output sample always is multiplied by 2
  706. */
  707. phase_modulation = 0;
  708. /* SLOT 1 */
  709. SLOT = &CH[6].SLOT[SLOT1];
  710. env = volume_calc(SLOT);
  711. out = SLOT->op1_out[0] + SLOT->op1_out[1];
  712. SLOT->op1_out[0] = SLOT->op1_out[1];
  713. if (!SLOT->CON)
  714. phase_modulation = SLOT->op1_out[0];
  715. /* else ignore output of operator 1 */
  716. SLOT->op1_out[1] = 0;
  717. if( env < ENV_QUIET )
  718. {
  719. if (!SLOT->FB)
  720. out = 0;
  721. SLOT->op1_out[1] = op_calc1(SLOT->Cnt, env, (out<<SLOT->FB), SLOT->wavetable );
  722. }
  723. /* SLOT 2 */
  724. SLOT++;
  725. env = volume_calc(SLOT);
  726. if( env < ENV_QUIET )
  727. output[0] += op_calc(SLOT->Cnt, env, phase_modulation, SLOT->wavetable) * 2;
  728. /* Phase generation is based on: */
  729. /* HH (13) channel 7->slot 1 combined with channel 8->slot 2 (same combination as TOP CYMBAL but different output phases) */
  730. /* SD (16) channel 7->slot 1 */
  731. /* TOM (14) channel 8->slot 1 */
  732. /* TOP (17) channel 7->slot 1 combined with channel 8->slot 2 (same combination as HIGH HAT but different output phases) */
  733. /* Envelope generation based on: */
  734. /* HH channel 7->slot1 */
  735. /* SD channel 7->slot2 */
  736. /* TOM channel 8->slot1 */
  737. /* TOP channel 8->slot2 */
  738. /* The following formulas can be well optimized.
  739. I leave them in direct form for now (in case I've missed something).
  740. */
  741. /* High Hat (verified on real YM3812) */
  742. env = volume_calc(SLOT7_1);
  743. if( env < ENV_QUIET )
  744. {
  745. /* high hat phase generation:
  746. phase = d0 or 234 (based on frequency only)
  747. phase = 34 or 2d0 (based on noise)
  748. */
  749. /* base frequency derived from operator 1 in channel 7 */
  750. unsigned char bit7 = ((SLOT7_1->Cnt>>FREQ_SH)>>7)&1;
  751. unsigned char bit3 = ((SLOT7_1->Cnt>>FREQ_SH)>>3)&1;
  752. unsigned char bit2 = ((SLOT7_1->Cnt>>FREQ_SH)>>2)&1;
  753. unsigned char res1 = (bit2 ^ bit7) | bit3;
  754. /* when res1 = 0 phase = 0x000 | 0xd0; */
  755. /* when res1 = 1 phase = 0x200 | (0xd0>>2); */
  756. UINT32 phase = res1 ? (0x200|(0xd0>>2)) : 0xd0;
  757. /* enable gate based on frequency of operator 2 in channel 8 */
  758. unsigned char bit5e= ((SLOT8_2->Cnt>>FREQ_SH)>>5)&1;
  759. unsigned char bit3e= ((SLOT8_2->Cnt>>FREQ_SH)>>3)&1;
  760. unsigned char res2 = (bit3e ^ bit5e);
  761. /* when res2 = 0 pass the phase from calculation above (res1); */
  762. /* when res2 = 1 phase = 0x200 | (0xd0>>2); */
  763. if (res2)
  764. phase = (0x200|(0xd0>>2));
  765. /* when phase & 0x200 is set and noise=1 then phase = 0x200|0xd0 */
  766. /* when phase & 0x200 is set and noise=0 then phase = 0x200|(0xd0>>2), ie no change */
  767. if (phase&0x200)
  768. {
  769. if (noise)
  770. phase = 0x200|0xd0;
  771. }
  772. else
  773. /* when phase & 0x200 is clear and noise=1 then phase = 0xd0>>2 */
  774. /* when phase & 0x200 is clear and noise=0 then phase = 0xd0, ie no change */
  775. {
  776. if (noise)
  777. phase = 0xd0>>2;
  778. }
  779. output[0] += op_calc(phase<<FREQ_SH, env, 0, SLOT7_1->wavetable) * 2;
  780. }
  781. /* Snare Drum (verified on real YM3812) */
  782. env = volume_calc(SLOT7_2);
  783. if( env < ENV_QUIET )
  784. {
  785. /* base frequency derived from operator 1 in channel 7 */
  786. unsigned char bit8 = ((SLOT7_1->Cnt>>FREQ_SH)>>8)&1;
  787. /* when bit8 = 0 phase = 0x100; */
  788. /* when bit8 = 1 phase = 0x200; */
  789. UINT32 phase = bit8 ? 0x200 : 0x100;
  790. /* Noise bit XOR'es phase by 0x100 */
  791. /* when noisebit = 0 pass the phase from calculation above */
  792. /* when noisebit = 1 phase ^= 0x100; */
  793. /* in other words: phase ^= (noisebit<<8); */
  794. if (noise)
  795. phase ^= 0x100;
  796. output[0] += op_calc(phase<<FREQ_SH, env, 0, SLOT7_2->wavetable) * 2;
  797. }
  798. /* Tom Tom (verified on real YM3812) */
  799. env = volume_calc(SLOT8_1);
  800. if( env < ENV_QUIET )
  801. output[0] += op_calc(SLOT8_1->Cnt, env, 0, SLOT8_1->wavetable) * 2;
  802. /* Top Cymbal (verified on real YM3812) */
  803. env = volume_calc(SLOT8_2);
  804. if( env < ENV_QUIET )
  805. {
  806. /* base frequency derived from operator 1 in channel 7 */
  807. unsigned char bit7 = ((SLOT7_1->Cnt>>FREQ_SH)>>7)&1;
  808. unsigned char bit3 = ((SLOT7_1->Cnt>>FREQ_SH)>>3)&1;
  809. unsigned char bit2 = ((SLOT7_1->Cnt>>FREQ_SH)>>2)&1;
  810. unsigned char res1 = (bit2 ^ bit7) | bit3;
  811. /* when res1 = 0 phase = 0x000 | 0x100; */
  812. /* when res1 = 1 phase = 0x200 | 0x100; */
  813. UINT32 phase = res1 ? 0x300 : 0x100;
  814. /* enable gate based on frequency of operator 2 in channel 8 */
  815. unsigned char bit5e= ((SLOT8_2->Cnt>>FREQ_SH)>>5)&1;
  816. unsigned char bit3e= ((SLOT8_2->Cnt>>FREQ_SH)>>3)&1;
  817. unsigned char res2 = (bit3e ^ bit5e);
  818. /* when res2 = 0 pass the phase from calculation above (res1); */
  819. /* when res2 = 1 phase = 0x200 | 0x100; */
  820. if (res2)
  821. phase = 0x300;
  822. output[0] += op_calc(phase<<FREQ_SH, env, 0, SLOT8_2->wavetable) * 2;
  823. }
  824. }
  825. /* generic table initialize */
  826. static int init_tables(void)
  827. {
  828. signed int i,x;
  829. signed int n;
  830. double o,m;
  831. for (x=0; x<TL_RES_LEN; x++)
  832. {
  833. m = (1<<16) / pow(2, (x+1) * (ENV_STEP/4.0) / 8.0);
  834. m = floor(m);
  835. /* we never reach (1<<16) here due to the (x+1) */
  836. /* result fits within 16 bits at maximum */
  837. n = (int)m; /* 16 bits here */
  838. n >>= 4; /* 12 bits here */
  839. if (n&1) /* round to nearest */
  840. n = (n>>1)+1;
  841. else
  842. n = n>>1;
  843. /* 11 bits here (rounded) */
  844. n <<= 1; /* 12 bits here (as in real chip) */
  845. tl_tab[ x*2 + 0 ] = n;
  846. tl_tab[ x*2 + 1 ] = -tl_tab[ x*2 + 0 ];
  847. for (i=1; i<12; i++)
  848. {
  849. tl_tab[ x*2+0 + i*2*TL_RES_LEN ] = tl_tab[ x*2+0 ]>>i;
  850. tl_tab[ x*2+1 + i*2*TL_RES_LEN ] = -tl_tab[ x*2+0 + i*2*TL_RES_LEN ];
  851. }
  852. #if 0
  853. //logerror("tl %04i", x*2);
  854. for (i=0; i<12; i++)
  855. //logerror(", [%02i] %5i", i*2, tl_tab[ x*2 /*+1*/ + i*2*TL_RES_LEN ] );
  856. //logerror("\n");
  857. #endif
  858. }
  859. /*//logerror("FMOPL.C: TL_TAB_LEN = %i elements (%i bytes)\n",TL_TAB_LEN, (int)sizeof(tl_tab));*/
  860. for (i=0; i<SIN_LEN; i++)
  861. {
  862. /* non-standard sinus */
  863. m = sin( ((i*2)+1) * PI / SIN_LEN ); /* checked against the real chip */
  864. /* we never reach zero here due to ((i*2)+1) */
  865. if (m>0.0)
  866. o = 8*log(1.0/m)/log(2); /* convert to 'decibels' */
  867. else
  868. o = 8*log(-1.0/m)/log(2); /* convert to 'decibels' */
  869. o = o / (ENV_STEP/4);
  870. n = (int)(2.0*o);
  871. if (n&1) /* round to nearest */
  872. n = (n>>1)+1;
  873. else
  874. n = n>>1;
  875. sin_tab[ i ] = n*2 + (m>=0.0? 0: 1 );
  876. /*//logerror("FMOPL.C: sin [%4i (hex=%03x)]= %4i (tl_tab value=%5i)\n", i, i, sin_tab[i], tl_tab[sin_tab[i]] );*/
  877. }
  878. for (i=0; i<SIN_LEN; i++)
  879. {
  880. /* waveform 1: __ __ */
  881. /* / \____/ \____*/
  882. /* output only first half of the sinus waveform (positive one) */
  883. if (i & (1<<(SIN_BITS-1)) )
  884. sin_tab[1*SIN_LEN+i] = TL_TAB_LEN;
  885. else
  886. sin_tab[1*SIN_LEN+i] = sin_tab[i];
  887. /* waveform 2: __ __ __ __ */
  888. /* / \/ \/ \/ \*/
  889. /* abs(sin) */
  890. sin_tab[2*SIN_LEN+i] = sin_tab[i & (SIN_MASK>>1) ];
  891. /* waveform 3: _ _ _ _ */
  892. /* / |_/ |_/ |_/ |_*/
  893. /* abs(output only first quarter of the sinus waveform) */
  894. if (i & (1<<(SIN_BITS-2)) )
  895. sin_tab[3*SIN_LEN+i] = TL_TAB_LEN;
  896. else
  897. sin_tab[3*SIN_LEN+i] = sin_tab[i & (SIN_MASK>>2)];
  898. /*//logerror("FMOPL.C: sin1[%4i]= %4i (tl_tab value=%5i)\n", i, sin_tab[1*SIN_LEN+i], tl_tab[sin_tab[1*SIN_LEN+i]] );
  899. //logerror("FMOPL.C: sin2[%4i]= %4i (tl_tab value=%5i)\n", i, sin_tab[2*SIN_LEN+i], tl_tab[sin_tab[2*SIN_LEN+i]] );
  900. //logerror("FMOPL.C: sin3[%4i]= %4i (tl_tab value=%5i)\n", i, sin_tab[3*SIN_LEN+i], tl_tab[sin_tab[3*SIN_LEN+i]] );*/
  901. }
  902. /*//logerror("FMOPL.C: ENV_QUIET= %08x (dec*8=%i)\n", ENV_QUIET, ENV_QUIET*8 );*/
  903. #ifdef SAVE_SAMPLE
  904. sample[0]=fopen("sampsum.pcm","wb");
  905. #endif
  906. return 1;
  907. }
  908. static void OPLCloseTable( void )
  909. {
  910. #ifdef SAVE_SAMPLE
  911. fclose(sample[0]);
  912. #endif
  913. }
  914. static void OPL_initalize(FM_OPL *OPL)
  915. {
  916. int i;
  917. /* frequency base */
  918. OPL->freqbase = (OPL->rate) ? ((double)OPL->clock / 72.0) / OPL->rate : 0;
  919. #if 0
  920. OPL->rate = (double)OPL->clock / 72.0;
  921. OPL->freqbase = 1.0;
  922. #endif
  923. /*//logerror("freqbase=%f\n", OPL->freqbase);*/
  924. /* Timer base time */
  925. OPL->TimerBase = 1.0 / ((double)OPL->clock / 72.0 );
  926. /* make fnumber -> increment counter table */
  927. for( i=0 ; i < 1024 ; i++ )
  928. {
  929. /* opn phase increment counter = 20bit */
  930. OPL->fn_tab[i] = (UINT32)( (double)i * 64 * OPL->freqbase * (1<<(FREQ_SH-10)) ); /* -10 because chip works with 10.10 fixed point, while we use 16.16 */
  931. #if 0
  932. //logerror("FMOPL.C: fn_tab[%4i] = %08x (dec=%8i)\n",
  933. i, OPL->fn_tab[i]>>6, OPL->fn_tab[i]>>6 );
  934. #endif
  935. }
  936. #if 0
  937. for( i=0 ; i < 16 ; i++ )
  938. {
  939. //logerror("FMOPL.C: sl_tab[%i] = %08x\n",
  940. i, sl_tab[i] );
  941. }
  942. for( i=0 ; i < 8 ; i++ )
  943. {
  944. int j;
  945. //logerror("FMOPL.C: ksl_tab[oct=%2i] =",i);
  946. for (j=0; j<16; j++)
  947. {
  948. //logerror("%08x ", ksl_tab[i*16+j] );
  949. }
  950. //logerror("\n");
  951. }
  952. #endif
  953. /* Amplitude modulation: 27 output levels (triangle waveform); 1 level takes one of: 192, 256 or 448 samples */
  954. /* One entry from LFO_AM_TABLE lasts for 64 samples */
  955. OPL->lfo_am_inc = (1.0 / 64.0 ) * (1<<LFO_SH) * OPL->freqbase;
  956. /* Vibrato: 8 output levels (triangle waveform); 1 level takes 1024 samples */
  957. OPL->lfo_pm_inc = (1.0 / 1024.0) * (1<<LFO_SH) * OPL->freqbase;
  958. /*//logerror ("OPL->lfo_am_inc = %8x ; OPL->lfo_pm_inc = %8x\n", OPL->lfo_am_inc, OPL->lfo_pm_inc);*/
  959. /* Noise generator: a step takes 1 sample */
  960. OPL->noise_f = (1.0 / 1.0) * (1<<FREQ_SH) * OPL->freqbase;
  961. OPL->eg_timer_add = (1<<EG_SH) * OPL->freqbase;
  962. OPL->eg_timer_overflow = ( 1 ) * (1<<EG_SH);
  963. /*//logerror("OPLinit eg_timer_add=%8x eg_timer_overflow=%8x\n", OPL->eg_timer_add, OPL->eg_timer_overflow);*/
  964. }
  965. INLINE void FM_KEYON(OPL_SLOT *SLOT, UINT32 key_set)
  966. {
  967. if( !SLOT->key )
  968. {
  969. /* restart Phase Generator */
  970. SLOT->Cnt = 0;
  971. /* phase -> Attack */
  972. SLOT->state = EG_ATT;
  973. }
  974. SLOT->key |= key_set;
  975. }
  976. INLINE void FM_KEYOFF(OPL_SLOT *SLOT, UINT32 key_clr)
  977. {
  978. if( SLOT->key )
  979. {
  980. SLOT->key &= key_clr;
  981. if( !SLOT->key )
  982. {
  983. /* phase -> Release */
  984. if (SLOT->state>EG_REL)
  985. SLOT->state = EG_REL;
  986. }
  987. }
  988. }
  989. /* update phase increment counter of operator (also update the EG rates if necessary) */
  990. INLINE void CALC_FCSLOT(OPL_CH *CH,OPL_SLOT *SLOT)
  991. {
  992. int ksr;
  993. /* (frequency) phase increment counter */
  994. SLOT->Incr = CH->fc * SLOT->mul;
  995. ksr = CH->kcode >> SLOT->KSR;
  996. if( SLOT->ksr != ksr )
  997. {
  998. SLOT->ksr = ksr;
  999. /* calculate envelope generator rates */
  1000. if ((SLOT->ar + SLOT->ksr) < 16+62)
  1001. {
  1002. SLOT->eg_sh_ar = eg_rate_shift [SLOT->ar + SLOT->ksr ];
  1003. SLOT->eg_sel_ar = eg_rate_select[SLOT->ar + SLOT->ksr ];
  1004. }
  1005. else
  1006. {
  1007. SLOT->eg_sh_ar = 0;
  1008. SLOT->eg_sel_ar = 13*RATE_STEPS;
  1009. }
  1010. SLOT->eg_sh_dr = eg_rate_shift [SLOT->dr + SLOT->ksr ];
  1011. SLOT->eg_sel_dr = eg_rate_select[SLOT->dr + SLOT->ksr ];
  1012. SLOT->eg_sh_rr = eg_rate_shift [SLOT->rr + SLOT->ksr ];
  1013. SLOT->eg_sel_rr = eg_rate_select[SLOT->rr + SLOT->ksr ];
  1014. }
  1015. }
  1016. /* set multi,am,vib,EG-TYP,KSR,mul */
  1017. INLINE void set_mul(FM_OPL *OPL,int slot,int v)
  1018. {
  1019. OPL_CH *CH = &OPL->P_CH[slot/2];
  1020. OPL_SLOT *SLOT = &CH->SLOT[slot&1];
  1021. SLOT->mul = mul_tab[v&0x0f];
  1022. SLOT->KSR = (v&0x10) ? 0 : 2;
  1023. SLOT->eg_type = (v&0x20);
  1024. SLOT->vib = (v&0x40);
  1025. SLOT->AMmask = (v&0x80) ? ~0 : 0;
  1026. CALC_FCSLOT(CH,SLOT);
  1027. }
  1028. /* set ksl & tl */
  1029. INLINE void set_ksl_tl(FM_OPL *OPL,int slot,int v)
  1030. {
  1031. OPL_CH *CH = &OPL->P_CH[slot/2];
  1032. OPL_SLOT *SLOT = &CH->SLOT[slot&1];
  1033. int ksl = v>>6; /* 0 / 1.5 / 3.0 / 6.0 dB/OCT */
  1034. SLOT->ksl = ksl ? 3-ksl : 31;
  1035. SLOT->TL = (v&0x3f)<<(ENV_BITS-1-7); /* 7 bits TL (bit 6 = always 0) */
  1036. SLOT->TLL = SLOT->TL + (CH->ksl_base>>SLOT->ksl);
  1037. }
  1038. /* set attack rate & decay rate */
  1039. INLINE void set_ar_dr(FM_OPL *OPL,int slot,int v)
  1040. {
  1041. OPL_CH *CH = &OPL->P_CH[slot/2];
  1042. OPL_SLOT *SLOT = &CH->SLOT[slot&1];
  1043. SLOT->ar = (v>>4) ? 16 + ((v>>4) <<2) : 0;
  1044. if ((SLOT->ar + SLOT->ksr) < 16+62)
  1045. {
  1046. SLOT->eg_sh_ar = eg_rate_shift [SLOT->ar + SLOT->ksr ];
  1047. SLOT->eg_sel_ar = eg_rate_select[SLOT->ar + SLOT->ksr ];
  1048. }
  1049. else
  1050. {
  1051. SLOT->eg_sh_ar = 0;
  1052. SLOT->eg_sel_ar = 13*RATE_STEPS;
  1053. }
  1054. SLOT->dr = (v&0x0f)? 16 + ((v&0x0f)<<2) : 0;
  1055. SLOT->eg_sh_dr = eg_rate_shift [SLOT->dr + SLOT->ksr ];
  1056. SLOT->eg_sel_dr = eg_rate_select[SLOT->dr + SLOT->ksr ];
  1057. }
  1058. /* set sustain level & release rate */
  1059. INLINE void set_sl_rr(FM_OPL *OPL,int slot,int v)
  1060. {
  1061. OPL_CH *CH = &OPL->P_CH[slot/2];
  1062. OPL_SLOT *SLOT = &CH->SLOT[slot&1];
  1063. SLOT->sl = sl_tab[ v>>4 ];
  1064. SLOT->rr = (v&0x0f)? 16 + ((v&0x0f)<<2) : 0;
  1065. SLOT->eg_sh_rr = eg_rate_shift [SLOT->rr + SLOT->ksr ];
  1066. SLOT->eg_sel_rr = eg_rate_select[SLOT->rr + SLOT->ksr ];
  1067. }
  1068. /* write a value v to register r on OPL chip */
  1069. static void OPLWriteReg(FM_OPL *OPL, int r, int v)
  1070. {
  1071. OPL_CH *CH;
  1072. int slot;
  1073. int block_fnum;
  1074. /* adjust bus to 8 bits */
  1075. r &= 0xff;
  1076. v &= 0xff;
  1077. #ifdef LOG_CYM_FILE
  1078. if ((cymfile) && (r!=0) )
  1079. {
  1080. fputc( (unsigned char)r, cymfile );
  1081. fputc( (unsigned char)v, cymfile );
  1082. }
  1083. #endif
  1084. switch(r&0xe0)
  1085. {
  1086. case 0x00: /* 00-1f:control */
  1087. switch(r&0x1f)
  1088. {
  1089. case 0x01: /* waveform select enable */
  1090. if(OPL->type&OPL_TYPE_WAVESEL)
  1091. {
  1092. OPL->wavesel = v&0x20;
  1093. /* do not change the waveform previously selected */
  1094. }
  1095. break;
  1096. case 0x02: /* Timer 1 */
  1097. OPL->T[0] = (256-v)*4;
  1098. break;
  1099. case 0x03: /* Timer 2 */
  1100. OPL->T[1] = (256-v)*16;
  1101. break;
  1102. case 0x04: /* IRQ clear / mask and Timer enable */
  1103. if(v&0x80)
  1104. { /* IRQ flag clear */
  1105. OPL_STATUS_RESET(OPL,0x7f-0x08); /* don't reset BFRDY flag or we will have to call deltat module to set the flag */
  1106. }
  1107. else
  1108. { /* set IRQ mask ,timer enable*/
  1109. UINT8 st1 = v&1;
  1110. UINT8 st2 = (v>>1)&1;
  1111. /* IRQRST,T1MSK,t2MSK,EOSMSK,BRMSK,x,ST2,ST1 */
  1112. OPL_STATUS_RESET(OPL, v & (0x78-0x08) );
  1113. OPL_STATUSMASK_SET(OPL, (~v) & 0x78 );
  1114. /* timer 2 */
  1115. if(OPL->st[1] != st2)
  1116. {
  1117. double interval = st2 ? (double)OPL->T[1]*OPL->TimerBase : 0.0;
  1118. OPL->st[1] = st2;
  1119. if (OPL->TimerHandler) (OPL->TimerHandler)(OPL->TimerParam+1,interval);
  1120. }
  1121. /* timer 1 */
  1122. if(OPL->st[0] != st1)
  1123. {
  1124. double interval = st1 ? (double)OPL->T[0]*OPL->TimerBase : 0.0;
  1125. OPL->st[0] = st1;
  1126. if (OPL->TimerHandler) (OPL->TimerHandler)(OPL->TimerParam+0,interval);
  1127. }
  1128. }
  1129. break;
  1130. #if BUILD_Y8950
  1131. case 0x06: /* Key Board OUT */
  1132. if(OPL->type&OPL_TYPE_KEYBOARD)
  1133. {
  1134. if(OPL->keyboardhandler_w)
  1135. OPL->keyboardhandler_w(OPL->keyboard_param,v);
  1136. else
  1137. //logerror("Y8950: write unmapped KEYBOARD port\n");
  1138. }
  1139. break;
  1140. case 0x07: /* DELTA-T control 1 : START,REC,MEMDATA,REPT,SPOFF,x,x,RST */
  1141. if(OPL->type&OPL_TYPE_ADPCM)
  1142. YM_DELTAT_ADPCM_Write(OPL->deltat,r-0x07,v);
  1143. break;
  1144. #endif
  1145. case 0x08: /* MODE,DELTA-T control 2 : CSM,NOTESEL,x,x,smpl,da/ad,64k,rom */
  1146. OPL->mode = v;
  1147. #if BUILD_Y8950
  1148. if(OPL->type&OPL_TYPE_ADPCM)
  1149. YM_DELTAT_ADPCM_Write(OPL->deltat,r-0x07,v&0x0f); /* mask 4 LSBs in register 08 for DELTA-T unit */
  1150. #endif
  1151. break;
  1152. #if BUILD_Y8950
  1153. case 0x09: /* START ADD */
  1154. case 0x0a:
  1155. case 0x0b: /* STOP ADD */
  1156. case 0x0c:
  1157. case 0x0d: /* PRESCALE */
  1158. case 0x0e:
  1159. case 0x0f: /* ADPCM data write */
  1160. case 0x10: /* DELTA-N */
  1161. case 0x11: /* DELTA-N */
  1162. case 0x12: /* ADPCM volume */
  1163. if(OPL->type&OPL_TYPE_ADPCM)
  1164. YM_DELTAT_ADPCM_Write(OPL->deltat,r-0x07,v);
  1165. break;
  1166. case 0x15: /* DAC data high 8 bits (F7,F6...F2) */
  1167. case 0x16: /* DAC data low 2 bits (F1, F0 in bits 7,6) */
  1168. case 0x17: /* DAC data shift (S2,S1,S0 in bits 2,1,0) */
  1169. //logerror("FMOPL.C: DAC data register written, but not implemented reg=%02x val=%02x\n",r,v);
  1170. break;
  1171. case 0x18: /* I/O CTRL (Direction) */
  1172. if(OPL->type&OPL_TYPE_IO)
  1173. OPL->portDirection = v&0x0f;
  1174. break;
  1175. case 0x19: /* I/O DATA */
  1176. if(OPL->type&OPL_TYPE_IO)
  1177. {
  1178. OPL->portLatch = v;
  1179. if(OPL->porthandler_w)
  1180. OPL->porthandler_w(OPL->port_param,v&OPL->portDirection);
  1181. }
  1182. break;
  1183. #endif
  1184. default:
  1185. //logerror("FMOPL.C: write to unknown register: %02x\n",r);
  1186. break;
  1187. }
  1188. break;
  1189. case 0x20: /* am ON, vib ON, ksr, eg_type, mul */
  1190. slot = slot_array[r&0x1f];
  1191. if(slot < 0) return;
  1192. set_mul(OPL,slot,v);
  1193. break;
  1194. case 0x40:
  1195. slot = slot_array[r&0x1f];
  1196. if(slot < 0) return;
  1197. set_ksl_tl(OPL,slot,v);
  1198. break;
  1199. case 0x60:
  1200. slot = slot_array[r&0x1f];
  1201. if(slot < 0) return;
  1202. set_ar_dr(OPL,slot,v);
  1203. break;
  1204. case 0x80:
  1205. slot = slot_array[r&0x1f];
  1206. if(slot < 0) return;
  1207. set_sl_rr(OPL,slot,v);
  1208. break;
  1209. case 0xa0:
  1210. if (r == 0xbd) /* am depth, vibrato depth, r,bd,sd,tom,tc,hh */
  1211. {
  1212. OPL->lfo_am_depth = v & 0x80;
  1213. OPL->lfo_pm_depth_range = (v&0x40) ? 8 : 0;
  1214. OPL->rhythm = v&0x3f;
  1215. if(OPL->rhythm&0x20)
  1216. {
  1217. /* BD key on/off */
  1218. if(v&0x10)
  1219. {
  1220. FM_KEYON (&OPL->P_CH[6].SLOT[SLOT1], 2);
  1221. FM_KEYON (&OPL->P_CH[6].SLOT[SLOT2], 2);
  1222. }
  1223. else
  1224. {
  1225. FM_KEYOFF(&OPL->P_CH[6].SLOT[SLOT1],~2);
  1226. FM_KEYOFF(&OPL->P_CH[6].SLOT[SLOT2],~2);
  1227. }
  1228. /* HH key on/off */
  1229. if(v&0x01) FM_KEYON (&OPL->P_CH[7].SLOT[SLOT1], 2);
  1230. else FM_KEYOFF(&OPL->P_CH[7].SLOT[SLOT1],~2);
  1231. /* SD key on/off */
  1232. if(v&0x08) FM_KEYON (&OPL->P_CH[7].SLOT[SLOT2], 2);
  1233. else FM_KEYOFF(&OPL->P_CH[7].SLOT[SLOT2],~2);
  1234. /* TOM key on/off */
  1235. if(v&0x04) FM_KEYON (&OPL->P_CH[8].SLOT[SLOT1], 2);
  1236. else FM_KEYOFF(&OPL->P_CH[8].SLOT[SLOT1],~2);
  1237. /* TOP-CY key on/off */
  1238. if(v&0x02) FM_KEYON (&OPL->P_CH[8].SLOT[SLOT2], 2);
  1239. else FM_KEYOFF(&OPL->P_CH[8].SLOT[SLOT2],~2);
  1240. }
  1241. else
  1242. {
  1243. /* BD key off */
  1244. FM_KEYOFF(&OPL->P_CH[6].SLOT[SLOT1],~2);
  1245. FM_KEYOFF(&OPL->P_CH[6].SLOT[SLOT2],~2);
  1246. /* HH key off */
  1247. FM_KEYOFF(&OPL->P_CH[7].SLOT[SLOT1],~2);
  1248. /* SD key off */
  1249. FM_KEYOFF(&OPL->P_CH[7].SLOT[SLOT2],~2);
  1250. /* TOM key off */
  1251. FM_KEYOFF(&OPL->P_CH[8].SLOT[SLOT1],~2);
  1252. /* TOP-CY off */
  1253. FM_KEYOFF(&OPL->P_CH[8].SLOT[SLOT2],~2);
  1254. }
  1255. return;
  1256. }
  1257. /* keyon,block,fnum */
  1258. if( (r&0x0f) > 8) return;
  1259. CH = &OPL->P_CH[r&0x0f];
  1260. if(!(r&0x10))
  1261. { /* a0-a8 */
  1262. block_fnum = (CH->block_fnum&0x1f00) | v;
  1263. }
  1264. else
  1265. { /* b0-b8 */
  1266. block_fnum = ((v&0x1f)<<8) | (CH->block_fnum&0xff);
  1267. if(v&0x20)
  1268. {
  1269. FM_KEYON (&CH->SLOT[SLOT1], 1);
  1270. FM_KEYON (&CH->SLOT[SLOT2], 1);
  1271. }
  1272. else
  1273. {
  1274. FM_KEYOFF(&CH->SLOT[SLOT1],~1);
  1275. FM_KEYOFF(&CH->SLOT[SLOT2],~1);
  1276. }
  1277. }
  1278. /* update */
  1279. if(CH->block_fnum != block_fnum)
  1280. {
  1281. UINT8 block = block_fnum >> 10;
  1282. CH->block_fnum = block_fnum;
  1283. CH->ksl_base = ksl_tab[block_fnum>>6];
  1284. CH->fc = OPL->fn_tab[block_fnum&0x03ff] >> (7-block);
  1285. /* BLK 2,1,0 bits -> bits 3,2,1 of kcode */
  1286. CH->kcode = (CH->block_fnum&0x1c00)>>9;
  1287. /* the info below is actually opposite to what is stated in the Manuals (verifed on real YM3812) */
  1288. /* if notesel == 0 -> lsb of kcode is bit 10 (MSB) of fnum */
  1289. /* if notesel == 1 -> lsb of kcode is bit 9 (MSB-1) of fnum */
  1290. if (OPL->mode&0x40)
  1291. CH->kcode |= (CH->block_fnum&0x100)>>8; /* notesel == 1 */
  1292. else
  1293. CH->kcode |= (CH->block_fnum&0x200)>>9; /* notesel == 0 */
  1294. /* refresh Total Level in both SLOTs of this channel */
  1295. CH->SLOT[SLOT1].TLL = CH->SLOT[SLOT1].TL + (CH->ksl_base>>CH->SLOT[SLOT1].ksl);
  1296. CH->SLOT[SLOT2].TLL = CH->SLOT[SLOT2].TL + (CH->ksl_base>>CH->SLOT[SLOT2].ksl);
  1297. /* refresh frequency counter in both SLOTs of this channel */
  1298. CALC_FCSLOT(CH,&CH->SLOT[SLOT1]);
  1299. CALC_FCSLOT(CH,&CH->SLOT[SLOT2]);
  1300. }
  1301. break;
  1302. case 0xc0:
  1303. /* FB,C */
  1304. if( (r&0x0f) > 8) return;
  1305. CH = &OPL->P_CH[r&0x0f];
  1306. CH->SLOT[SLOT1].FB = (v>>1)&7 ? ((v>>1)&7) + 7 : 0;
  1307. CH->SLOT[SLOT1].CON = v&1;
  1308. CH->SLOT[SLOT1].connect1 = CH->SLOT[SLOT1].CON ? &output[0] : &phase_modulation;
  1309. break;
  1310. case 0xe0: /* waveform select */
  1311. /* simply ignore write to the waveform select register if selecting not enabled in test register */
  1312. if(OPL->wavesel)
  1313. {
  1314. slot = slot_array[r&0x1f];
  1315. if(slot < 0) return;
  1316. CH = &OPL->P_CH[slot/2];
  1317. CH->SLOT[slot&1].wavetable = (v&0x03)*SIN_LEN;
  1318. }
  1319. break;
  1320. }
  1321. }
  1322. #ifdef LOG_CYM_FILE
  1323. static void cymfile_callback (int n)
  1324. {
  1325. if (cymfile)
  1326. {
  1327. fputc( (unsigned char)0, cymfile );
  1328. }
  1329. }
  1330. #endif
  1331. /* lock/unlock for common table */
  1332. static int OPL_LockTable(void)
  1333. {
  1334. num_lock++;
  1335. if(num_lock>1) return 0;
  1336. /* first time */
  1337. cur_chip = NULL;
  1338. /* allocate total level table (128kb space) */
  1339. if( !init_tables() )
  1340. {
  1341. num_lock--;
  1342. return -1;
  1343. }
  1344. #ifdef LOG_CYM_FILE
  1345. cymfile = fopen("3812_.cym","wb");
  1346. if (cymfile)
  1347. timer_pulse ( TIME_IN_HZ(110), 0, cymfile_callback); /*110 Hz pulse timer*/
  1348. else
  1349. //logerror("Could not create file 3812_.cym\n");
  1350. #endif
  1351. return 0;
  1352. }
  1353. static void OPL_UnLockTable(void)
  1354. {
  1355. if(num_lock) num_lock--;
  1356. if(num_lock) return;
  1357. /* last time */
  1358. cur_chip = NULL;
  1359. OPLCloseTable();
  1360. #ifdef LOG_CYM_FILE
  1361. fclose (cymfile);
  1362. cymfile = NULL;
  1363. #endif
  1364. }
  1365. void OPLResetChip(FM_OPL *OPL)
  1366. {
  1367. int c,s;
  1368. int i;
  1369. OPL->eg_timer = 0;
  1370. OPL->eg_cnt = 0;
  1371. OPL->noise_rng = 1; /* noise shift register */
  1372. OPL->mode = 0; /* normal mode */
  1373. OPL_STATUS_RESET(OPL,0x7f);
  1374. /* reset with register write */
  1375. OPLWriteReg(OPL,0x01,0); /* wavesel disable */
  1376. OPLWriteReg(OPL,0x02,0); /* Timer1 */
  1377. OPLWriteReg(OPL,0x03,0); /* Timer2 */
  1378. OPLWriteReg(OPL,0x04,0); /* IRQ mask clear */
  1379. for(i = 0xff ; i >= 0x20 ; i-- ) OPLWriteReg(OPL,i,0);
  1380. /* reset operator parameters */
  1381. for( c = 0 ; c < 9 ; c++ )
  1382. {
  1383. OPL_CH *CH = &OPL->P_CH[c];
  1384. for(s = 0 ; s < 2 ; s++ )
  1385. {
  1386. /* wave table */
  1387. CH->SLOT[s].wavetable = 0;
  1388. CH->SLOT[s].state = EG_OFF;
  1389. CH->SLOT[s].volume = MAX_ATT_INDEX;
  1390. }
  1391. }
  1392. #if BUILD_Y8950
  1393. if(OPL->type&OPL_TYPE_ADPCM)
  1394. {
  1395. YM_DELTAT *DELTAT = OPL->deltat;
  1396. DELTAT->freqbase = OPL->freqbase;
  1397. DELTAT->output_pointer = &output_deltat[0];
  1398. DELTAT->portshift = 5;
  1399. DELTAT->output_range = 1<<23;
  1400. YM_DELTAT_ADPCM_Reset(DELTAT,0,YM_DELTAT_EMULATION_MODE_NORMAL);
  1401. }
  1402. #endif
  1403. }
  1404. /* Create one of virtual YM3812/YM3526/Y8950 */
  1405. /* 'clock' is chip clock in Hz */
  1406. /* 'rate' is sampling rate */
  1407. FM_OPL *OPLCreate(int type, int clock, int rate)
  1408. {
  1409. char *ptr;
  1410. FM_OPL *OPL;
  1411. int state_size;
  1412. if (OPL_LockTable() ==-1) return NULL;
  1413. /* calculate OPL state size */
  1414. state_size = sizeof(FM_OPL);
  1415. #if BUILD_Y8950
  1416. if (type&OPL_TYPE_ADPCM) state_size+= sizeof(YM_DELTAT);
  1417. #endif
  1418. /* allocate memory block */
  1419. ptr = malloc(state_size);
  1420. if (ptr==NULL)
  1421. return NULL;
  1422. /* clear */
  1423. memset(ptr,0,state_size);
  1424. OPL = (FM_OPL *)ptr;
  1425. ptr += sizeof(FM_OPL);
  1426. #if BUILD_Y8950
  1427. if (type&OPL_TYPE_ADPCM)
  1428. {
  1429. OPL->deltat = (YM_DELTAT *)ptr;
  1430. }
  1431. ptr += sizeof(YM_DELTAT);
  1432. #endif
  1433. OPL->type = type;
  1434. OPL->clock = clock;
  1435. OPL->rate = rate;
  1436. /* init global tables */
  1437. OPL_initalize(OPL);
  1438. return OPL;
  1439. }
  1440. /* Destroy one of virtual YM3812 */
  1441. void OPLDestroy(FM_OPL *OPL)
  1442. {
  1443. OPL_UnLockTable();
  1444. free(OPL);
  1445. }
  1446. /* Optional handlers */
  1447. static void OPLSetTimerHandler(FM_OPL *OPL,OPL_TIMERHANDLER TimerHandler,int channelOffset)
  1448. {
  1449. OPL->TimerHandler = TimerHandler;
  1450. OPL->TimerParam = channelOffset;
  1451. }
  1452. static void OPLSetIRQHandler(FM_OPL *OPL,OPL_IRQHANDLER IRQHandler,int param)
  1453. {
  1454. OPL->IRQHandler = IRQHandler;
  1455. OPL->IRQParam = param;
  1456. }
  1457. static void OPLSetUpdateHandler(FM_OPL *OPL,OPL_UPDATEHANDLER UpdateHandler,int param)
  1458. {
  1459. OPL->UpdateHandler = UpdateHandler;
  1460. OPL->UpdateParam = param;
  1461. }
  1462. int OPLWrite(FM_OPL *OPL,int a,int v)
  1463. {
  1464. if( !(a&1) )
  1465. { /* address port */
  1466. OPL->address = v & 0xff;
  1467. }
  1468. else
  1469. { /* data port */
  1470. if(OPL->UpdateHandler) OPL->UpdateHandler(OPL->UpdateParam,0);
  1471. OPLWriteReg(OPL,OPL->address,v);
  1472. }
  1473. return OPL->status>>7;
  1474. }
  1475. static unsigned char OPLRead(FM_OPL *OPL,int a)
  1476. {
  1477. if( !(a&1) )
  1478. {
  1479. /* status port */
  1480. #if BUILD_Y8950
  1481. if(OPL->type&OPL_TYPE_ADPCM) /* Y8950 */
  1482. {
  1483. return (OPL->status & (OPL->statusmask|0x80)) | (OPL->deltat->PCM_BSY&1);
  1484. }
  1485. #endif
  1486. /* OPL and OPL2 */
  1487. return OPL->status & (OPL->statusmask|0x80);
  1488. }
  1489. #if BUILD_Y8950
  1490. /* data port */
  1491. switch(OPL->address)
  1492. {
  1493. case 0x05: /* KeyBoard IN */
  1494. if(OPL->type&OPL_TYPE_KEYBOARD)
  1495. {
  1496. if(OPL->keyboardhandler_r)
  1497. return OPL->keyboardhandler_r(OPL->keyboard_param);
  1498. else
  1499. //logerror("Y8950: read unmapped KEYBOARD port\n");
  1500. }
  1501. return 0;
  1502. case 0x0f: /* ADPCM-DATA */
  1503. if(OPL->type&OPL_TYPE_ADPCM)
  1504. {
  1505. UINT8 val;
  1506. val = YM_DELTAT_ADPCM_Read(OPL->deltat);
  1507. /*//logerror("Y8950: read ADPCM value read=%02x\n",val);*/
  1508. return val;
  1509. }
  1510. return 0;
  1511. case 0x19: /* I/O DATA */
  1512. if(OPL->type&OPL_TYPE_IO)
  1513. {
  1514. if(OPL->porthandler_r)
  1515. return OPL->porthandler_r(OPL->port_param);
  1516. else
  1517. //logerror("Y8950:read unmapped I/O port\n");
  1518. }
  1519. return 0;
  1520. case 0x1a: /* PCM-DATA */
  1521. if(OPL->type&OPL_TYPE_ADPCM)
  1522. {
  1523. //logerror("Y8950 A/D convertion is accessed but not implemented !\n");
  1524. return 0x80; /* 2's complement PCM data - result from A/D convertion */
  1525. }
  1526. return 0;
  1527. }
  1528. #endif
  1529. return 0xff;
  1530. }
  1531. /* CSM Key Controll */
  1532. INLINE void CSMKeyControll(OPL_CH *CH)
  1533. {
  1534. FM_KEYON (&CH->SLOT[SLOT1], 4);
  1535. FM_KEYON (&CH->SLOT[SLOT2], 4);
  1536. /* The key off should happen exactly one sample later - not implemented correctly yet */
  1537. FM_KEYOFF(&CH->SLOT[SLOT1], ~4);
  1538. FM_KEYOFF(&CH->SLOT[SLOT2], ~4);
  1539. }
  1540. static int OPLTimerOver(FM_OPL *OPL,int c)
  1541. {
  1542. if( c )
  1543. { /* Timer B */
  1544. OPL_STATUS_SET(OPL,0x20);
  1545. }
  1546. else
  1547. { /* Timer A */
  1548. OPL_STATUS_SET(OPL,0x40);
  1549. /* CSM mode key,TL controll */
  1550. if( OPL->mode & 0x80 )
  1551. { /* CSM mode total level latch and auto key on */
  1552. int ch;
  1553. if(OPL->UpdateHandler) OPL->UpdateHandler(OPL->UpdateParam,0);
  1554. for(ch=0; ch<9; ch++)
  1555. CSMKeyControll( &OPL->P_CH[ch] );
  1556. }
  1557. }
  1558. /* reload timer */
  1559. if (OPL->TimerHandler) (OPL->TimerHandler)(OPL->TimerParam+c,(double)OPL->T[c]*OPL->TimerBase);
  1560. return OPL->status>>7;
  1561. }
  1562. #define MAX_OPL_CHIPS 2
  1563. #if (BUILD_YM3812)
  1564. static FM_OPL *OPL_YM3812[MAX_OPL_CHIPS]; /* array of pointers to the YM3812's */
  1565. static int YM3812NumChips = 0; /* number of chips */
  1566. int YM3812Init(int num, int clock, int rate)
  1567. {
  1568. int i;
  1569. if (YM3812NumChips)
  1570. return -1; /* duplicate init. */
  1571. YM3812NumChips = num;
  1572. for (i = 0;i < YM3812NumChips; i++)
  1573. {
  1574. /* emulator create */
  1575. OPL_YM3812[i] = OPLCreate(OPL_TYPE_YM3812,clock,rate);
  1576. if(OPL_YM3812[i] == NULL)
  1577. {
  1578. /* it's really bad - we run out of memeory */
  1579. YM3812NumChips = 0;
  1580. return -1;
  1581. }
  1582. /* reset */
  1583. YM3812ResetChip(i);
  1584. }
  1585. return 0;
  1586. }
  1587. void YM3812Shutdown(void)
  1588. {
  1589. int i;
  1590. for (i = 0;i < YM3812NumChips; i++)
  1591. {
  1592. /* emulator shutdown */
  1593. OPLDestroy(OPL_YM3812[i]);
  1594. OPL_YM3812[i] = NULL;
  1595. }
  1596. YM3812NumChips = 0;
  1597. }
  1598. void YM3812ResetChip(int which)
  1599. {
  1600. OPLResetChip(OPL_YM3812[which]);
  1601. }
  1602. int YM3812Write(int which, int a, int v)
  1603. {
  1604. return OPLWrite(OPL_YM3812[which], a, v);
  1605. }
  1606. unsigned char YM3812Read(int which, int a)
  1607. {
  1608. /* YM3812 always returns bit2 and bit1 in HIGH state */
  1609. return OPLRead(OPL_YM3812[which], a) | 0x06 ;
  1610. }
  1611. int YM3812TimerOver(int which, int c)
  1612. {
  1613. return OPLTimerOver(OPL_YM3812[which], c);
  1614. }
  1615. void YM3812SetTimerHandler(int which, OPL_TIMERHANDLER TimerHandler, int channelOffset)
  1616. {
  1617. OPLSetTimerHandler(OPL_YM3812[which], TimerHandler, channelOffset);
  1618. }
  1619. void YM3812SetIRQHandler(int which,OPL_IRQHANDLER IRQHandler,int param)
  1620. {
  1621. OPLSetIRQHandler(OPL_YM3812[which], IRQHandler, param);
  1622. }
  1623. void YM3812SetUpdateHandler(int which,OPL_UPDATEHANDLER UpdateHandler,int param)
  1624. {
  1625. OPLSetUpdateHandler(OPL_YM3812[which], UpdateHandler, param);
  1626. }
  1627. /*
  1628. ** Generate samples for one of the YM3812's
  1629. **
  1630. ** 'which' is the virtual YM3812 number
  1631. ** '*buffer' is the output buffer pointer
  1632. ** 'length' is the number of samples that should be generated
  1633. */
  1634. void YM3812UpdateOne(FM_OPL *OPL, INT16 *buffer, int length)
  1635. {
  1636. UINT8 rhythm = OPL->rhythm&0x20;
  1637. OPLSAMPLE *buf = buffer;
  1638. int i;
  1639. if( (void *)OPL != cur_chip ){
  1640. cur_chip = (void *)OPL;
  1641. /* rhythm slots */
  1642. SLOT7_1 = &OPL->P_CH[7].SLOT[SLOT1];
  1643. SLOT7_2 = &OPL->P_CH[7].SLOT[SLOT2];
  1644. SLOT8_1 = &OPL->P_CH[8].SLOT[SLOT1];
  1645. SLOT8_2 = &OPL->P_CH[8].SLOT[SLOT2];
  1646. }
  1647. for( i=0; i < length ; i++ )
  1648. {
  1649. int lt;
  1650. output[0] = 0;
  1651. advance_lfo(OPL);
  1652. /* FM part */
  1653. OPL_CALC_CH(&OPL->P_CH[0]);
  1654. OPL_CALC_CH(&OPL->P_CH[1]);
  1655. OPL_CALC_CH(&OPL->P_CH[2]);
  1656. OPL_CALC_CH(&OPL->P_CH[3]);
  1657. OPL_CALC_CH(&OPL->P_CH[4]);
  1658. OPL_CALC_CH(&OPL->P_CH[5]);
  1659. if(!rhythm)
  1660. {
  1661. OPL_CALC_CH(&OPL->P_CH[6]);
  1662. OPL_CALC_CH(&OPL->P_CH[7]);
  1663. OPL_CALC_CH(&OPL->P_CH[8]);
  1664. }
  1665. else /* Rhythm part */
  1666. {
  1667. OPL_CALC_RH(&OPL->P_CH[0], (OPL->noise_rng>>0)&1 );
  1668. }
  1669. lt = output[0];
  1670. lt >>= FINAL_SH;
  1671. /* limit check */
  1672. lt = limit( lt , MAXOUT, MINOUT );
  1673. #ifdef SAVE_SAMPLE
  1674. if (which==0)
  1675. {
  1676. SAVE_ALL_CHANNELS
  1677. }
  1678. #endif
  1679. /* store to sound buffer */
  1680. buf[i] = lt;
  1681. advance(OPL);
  1682. }
  1683. }
  1684. #endif /* BUILD_YM3812 */
  1685. #if (BUILD_YM3526)
  1686. static FM_OPL *OPL_YM3526[MAX_OPL_CHIPS]; /* array of pointers to the YM3526's */
  1687. static int YM3526NumChips = 0; /* number of chips */
  1688. int YM3526Init(int num, int clock, int rate)
  1689. {
  1690. int i;
  1691. if (YM3526NumChips)
  1692. return -1; /* duplicate init. */
  1693. YM3526NumChips = num;
  1694. for (i = 0;i < YM3526NumChips; i++)
  1695. {
  1696. /* emulator create */
  1697. OPL_YM3526[i] = OPLCreate(OPL_TYPE_YM3526,clock,rate);
  1698. if(OPL_YM3526[i] == NULL)
  1699. {
  1700. /* it's really bad - we run out of memeory */
  1701. YM3526NumChips = 0;
  1702. return -1;
  1703. }
  1704. /* reset */
  1705. YM3526ResetChip(i);
  1706. }
  1707. return 0;
  1708. }
  1709. void YM3526Shutdown(void)
  1710. {
  1711. int i;
  1712. for (i = 0;i < YM3526NumChips; i++)
  1713. {
  1714. /* emulator shutdown */
  1715. OPLDestroy(OPL_YM3526[i]);
  1716. OPL_YM3526[i] = NULL;
  1717. }
  1718. YM3526NumChips = 0;
  1719. }
  1720. void YM3526ResetChip(int which)
  1721. {
  1722. OPLResetChip(OPL_YM3526[which]);
  1723. }
  1724. int YM3526Write(int which, int a, int v)
  1725. {
  1726. return OPLWrite(OPL_YM3526[which], a, v);
  1727. }
  1728. unsigned char YM3526Read(int which, int a)
  1729. {
  1730. /* YM3526 always returns bit2 and bit1 in HIGH state */
  1731. return OPLRead(OPL_YM3526[which], a) | 0x06 ;
  1732. }
  1733. int YM3526TimerOver(int which, int c)
  1734. {
  1735. return OPLTimerOver(OPL_YM3526[which], c);
  1736. }
  1737. void YM3526SetTimerHandler(int which, OPL_TIMERHANDLER TimerHandler, int channelOffset)
  1738. {
  1739. OPLSetTimerHandler(OPL_YM3526[which], TimerHandler, channelOffset);
  1740. }
  1741. void YM3526SetIRQHandler(int which,OPL_IRQHANDLER IRQHandler,int param)
  1742. {
  1743. OPLSetIRQHandler(OPL_YM3526[which], IRQHandler, param);
  1744. }
  1745. void YM3526SetUpdateHandler(int which,OPL_UPDATEHANDLER UpdateHandler,int param)
  1746. {
  1747. OPLSetUpdateHandler(OPL_YM3526[which], UpdateHandler, param);
  1748. }
  1749. /*
  1750. ** Generate samples for one of the YM3526's
  1751. **
  1752. ** 'which' is the virtual YM3526 number
  1753. ** '*buffer' is the output buffer pointer
  1754. ** 'length' is the number of samples that should be generated
  1755. */
  1756. void YM3526UpdateOne(int which, INT16 *buffer, int length)
  1757. {
  1758. FM_OPL *OPL = OPL_YM3526[which];
  1759. UINT8 rhythm = OPL->rhythm&0x20;
  1760. OPLSAMPLE *buf = buffer;
  1761. int i;
  1762. if( (void *)OPL != cur_chip ){
  1763. cur_chip = (void *)OPL;
  1764. /* rhythm slots */
  1765. SLOT7_1 = &OPL->P_CH[7].SLOT[SLOT1];
  1766. SLOT7_2 = &OPL->P_CH[7].SLOT[SLOT2];
  1767. SLOT8_1 = &OPL->P_CH[8].SLOT[SLOT1];
  1768. SLOT8_2 = &OPL->P_CH[8].SLOT[SLOT2];
  1769. }
  1770. for( i=0; i < length ; i++ )
  1771. {
  1772. int lt;
  1773. output[0] = 0;
  1774. advance_lfo(OPL);
  1775. /* FM part */
  1776. OPL_CALC_CH(&OPL->P_CH[0]);
  1777. OPL_CALC_CH(&OPL->P_CH[1]);
  1778. OPL_CALC_CH(&OPL->P_CH[2]);
  1779. OPL_CALC_CH(&OPL->P_CH[3]);
  1780. OPL_CALC_CH(&OPL->P_CH[4]);
  1781. OPL_CALC_CH(&OPL->P_CH[5]);
  1782. if(!rhythm)
  1783. {
  1784. OPL_CALC_CH(&OPL->P_CH[6]);
  1785. OPL_CALC_CH(&OPL->P_CH[7]);
  1786. OPL_CALC_CH(&OPL->P_CH[8]);
  1787. }
  1788. else /* Rhythm part */
  1789. {
  1790. OPL_CALC_RH(&OPL->P_CH[0], (OPL->noise_rng>>0)&1 );
  1791. }
  1792. lt = output[0];
  1793. lt >>= FINAL_SH;
  1794. /* limit check */
  1795. lt = limit( lt , MAXOUT, MINOUT );
  1796. #ifdef SAVE_SAMPLE
  1797. if (which==0)
  1798. {
  1799. SAVE_ALL_CHANNELS
  1800. }
  1801. #endif
  1802. /* store to sound buffer */
  1803. buf[i] = lt;
  1804. advance(OPL);
  1805. }
  1806. }
  1807. #endif /* BUILD_YM3526 */
  1808. #if BUILD_Y8950
  1809. static FM_OPL *OPL_Y8950[MAX_OPL_CHIPS]; /* array of pointers to the Y8950's */
  1810. static int Y8950NumChips = 0; /* number of chips */
  1811. static void Y8950_deltat_status_set(UINT8 which, UINT8 changebits)
  1812. {
  1813. OPL_STATUS_SET(OPL_Y8950[which], changebits);
  1814. }
  1815. static void Y8950_deltat_status_reset(UINT8 which, UINT8 changebits)
  1816. {
  1817. OPL_STATUS_RESET(OPL_Y8950[which], changebits);
  1818. }
  1819. int Y8950Init(int num, int clock, int rate)
  1820. {
  1821. int i;
  1822. if (Y8950NumChips)
  1823. return -1; /* duplicate init. */
  1824. Y8950NumChips = num;
  1825. for (i = 0;i < Y8950NumChips; i++)
  1826. {
  1827. /* emulator create */
  1828. OPL_Y8950[i] = OPLCreate(OPL_TYPE_Y8950,clock,rate);
  1829. if(OPL_Y8950[i] == NULL)
  1830. {
  1831. /* it's really bad - we run out of memeory */
  1832. Y8950NumChips = 0;
  1833. return -1;
  1834. }
  1835. OPL_Y8950[i]->deltat->status_set_handler = Y8950_deltat_status_set;
  1836. OPL_Y8950[i]->deltat->status_reset_handler = Y8950_deltat_status_reset;
  1837. OPL_Y8950[i]->deltat->status_change_which_chip = i;
  1838. OPL_Y8950[i]->deltat->status_change_EOS_bit = 0x10; /* status flag: set bit4 on End Of Sample */
  1839. OPL_Y8950[i]->deltat->status_change_BRDY_bit = 0x08; /* status flag: set bit3 on BRDY (End Of: ADPCM analysis/synthesis, memory reading/writing) */
  1840. /*OPL_Y8950[i]->deltat->write_time = 10.0 / clock;*/ /* a single byte write takes 10 cycles of main clock */
  1841. /*OPL_Y8950[i]->deltat->read_time = 8.0 / clock;*/ /* a single byte read takes 8 cycles of main clock */
  1842. /* reset */
  1843. Y8950ResetChip(i);
  1844. }
  1845. return 0;
  1846. }
  1847. void Y8950Shutdown(void)
  1848. {
  1849. int i;
  1850. for (i = 0;i < Y8950NumChips; i++)
  1851. {
  1852. /* emulator shutdown */
  1853. OPLDestroy(OPL_Y8950[i]);
  1854. OPL_Y8950[i] = NULL;
  1855. }
  1856. Y8950NumChips = 0;
  1857. }
  1858. void Y8950ResetChip(int which)
  1859. {
  1860. OPLResetChip(OPL_Y8950[which]);
  1861. }
  1862. int Y8950Write(int which, int a, int v)
  1863. {
  1864. return OPLWrite(OPL_Y8950[which], a, v);
  1865. }
  1866. unsigned char Y8950Read(int which, int a)
  1867. {
  1868. return OPLRead(OPL_Y8950[which], a);
  1869. }
  1870. int Y8950TimerOver(int which, int c)
  1871. {
  1872. return OPLTimerOver(OPL_Y8950[which], c);
  1873. }
  1874. void Y8950SetTimerHandler(int which, OPL_TIMERHANDLER TimerHandler, int channelOffset)
  1875. {
  1876. OPLSetTimerHandler(OPL_Y8950[which], TimerHandler, channelOffset);
  1877. }
  1878. void Y8950SetIRQHandler(int which,OPL_IRQHANDLER IRQHandler,int param)
  1879. {
  1880. OPLSetIRQHandler(OPL_Y8950[which], IRQHandler, param);
  1881. }
  1882. void Y8950SetUpdateHandler(int which,OPL_UPDATEHANDLER UpdateHandler,int param)
  1883. {
  1884. OPLSetUpdateHandler(OPL_Y8950[which], UpdateHandler, param);
  1885. }
  1886. void Y8950SetDeltaTMemory(int which, void * deltat_mem_ptr, int deltat_mem_size )
  1887. {
  1888. FM_OPL *OPL = OPL_Y8950[which];
  1889. OPL->deltat->memory = (UINT8 *)(deltat_mem_ptr);
  1890. OPL->deltat->memory_size = deltat_mem_size;
  1891. }
  1892. /*
  1893. ** Generate samples for one of the Y8950's
  1894. **
  1895. ** 'which' is the virtual Y8950 number
  1896. ** '*buffer' is the output buffer pointer
  1897. ** 'length' is the number of samples that should be generated
  1898. */
  1899. void Y8950UpdateOne(int which, INT16 *buffer, int length)
  1900. {
  1901. int i;
  1902. FM_OPL *OPL = OPL_Y8950[which];
  1903. UINT8 rhythm = OPL->rhythm&0x20;
  1904. YM_DELTAT *DELTAT = OPL->deltat;
  1905. OPLSAMPLE *buf = buffer;
  1906. if( (void *)OPL != cur_chip ){
  1907. cur_chip = (void *)OPL;
  1908. /* rhythm slots */
  1909. SLOT7_1 = &OPL->P_CH[7].SLOT[SLOT1];
  1910. SLOT7_2 = &OPL->P_CH[7].SLOT[SLOT2];
  1911. SLOT8_1 = &OPL->P_CH[8].SLOT[SLOT1];
  1912. SLOT8_2 = &OPL->P_CH[8].SLOT[SLOT2];
  1913. }
  1914. for( i=0; i < length ; i++ )
  1915. {
  1916. int lt;
  1917. output[0] = 0;
  1918. output_deltat[0] = 0;
  1919. advance_lfo(OPL);
  1920. /* deltaT ADPCM */
  1921. if( DELTAT->portstate&0x80 )
  1922. YM_DELTAT_ADPCM_CALC(DELTAT);
  1923. /* FM part */
  1924. OPL_CALC_CH(&OPL->P_CH[0]);
  1925. OPL_CALC_CH(&OPL->P_CH[1]);
  1926. OPL_CALC_CH(&OPL->P_CH[2]);
  1927. OPL_CALC_CH(&OPL->P_CH[3]);
  1928. OPL_CALC_CH(&OPL->P_CH[4]);
  1929. OPL_CALC_CH(&OPL->P_CH[5]);
  1930. if(!rhythm)
  1931. {
  1932. OPL_CALC_CH(&OPL->P_CH[6]);
  1933. OPL_CALC_CH(&OPL->P_CH[7]);
  1934. OPL_CALC_CH(&OPL->P_CH[8]);
  1935. }
  1936. else /* Rhythm part */
  1937. {
  1938. OPL_CALC_RH(&OPL->P_CH[0], (OPL->noise_rng>>0)&1 );
  1939. }
  1940. lt = output[0] + (output_deltat[0]>>11);
  1941. lt >>= FINAL_SH;
  1942. /* limit check */
  1943. lt = limit( lt , MAXOUT, MINOUT );
  1944. #ifdef SAVE_SAMPLE
  1945. if (which==0)
  1946. {
  1947. SAVE_ALL_CHANNELS
  1948. }
  1949. #endif
  1950. /* store to sound buffer */
  1951. buf[i] = lt;
  1952. advance(OPL);
  1953. }
  1954. }
  1955. void Y8950SetPortHandler(int which,OPL_PORTHANDLER_W PortHandler_w,OPL_PORTHANDLER_R PortHandler_r,int param)
  1956. {
  1957. FM_OPL *OPL = OPL_Y8950[which];
  1958. OPL->porthandler_w = PortHandler_w;
  1959. OPL->porthandler_r = PortHandler_r;
  1960. OPL->port_param = param;
  1961. }
  1962. void Y8950SetKeyboardHandler(int which,OPL_PORTHANDLER_W KeyboardHandler_w,OPL_PORTHANDLER_R KeyboardHandler_r,int param)
  1963. {
  1964. FM_OPL *OPL = OPL_Y8950[which];
  1965. OPL->keyboardhandler_w = KeyboardHandler_w;
  1966. OPL->keyboardhandler_r = KeyboardHandler_r;
  1967. OPL->keyboard_param = param;
  1968. }
  1969. #endif