ym2612.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243
  1. /*
  2. ** This is a bunch of remains of original fm.c from MAME project. All stuff
  3. ** unrelated to ym2612 was removed, multiple chip support was removed,
  4. ** some parts of code were slightly rewritten and tied to the emulator.
  5. **
  6. ** SSG-EG was also removed, because it's rarely used, Sega2.doc even does not
  7. ** document it ("proprietary") and tells to write 0 to SSG-EG control register.
  8. **
  9. ** updated with fixes from mame 0.216 (file version 1.5.1) (kub)
  10. ** SSG-EG readded from GenPlus (kub)
  11. ** linear sample interpolation for chip to output rate adaption (kub)
  12. */
  13. /*
  14. **
  15. ** File: fm.c -- software implementation of Yamaha FM sound generator
  16. **
  17. ** Copyright (C) 2001, 2002, 2003 Jarek Burczynski (bujar at mame dot net)
  18. ** Copyright (C) 1998 Tatsuyuki Satoh , MultiArcadeMachineEmulator development
  19. **
  20. ** Version 1.4 (final beta)
  21. **
  22. */
  23. /*
  24. ** History:
  25. **
  26. ** 03-08-2003 Jarek Burczynski:
  27. ** - fixed YM2608 initial values (after the reset)
  28. ** - fixed flag and irqmask handling (YM2608)
  29. ** - fixed BUFRDY flag handling (YM2608)
  30. **
  31. ** 14-06-2003 Jarek Burczynski:
  32. ** - implemented all of the YM2608 status register flags
  33. ** - implemented support for external memory read/write via YM2608
  34. ** - implemented support for deltat memory limit register in YM2608 emulation
  35. **
  36. ** 22-05-2003 Jarek Burczynski:
  37. ** - fixed LFO PM calculations (copy&paste bugfix)
  38. **
  39. ** 08-05-2003 Jarek Burczynski:
  40. ** - fixed SSG support
  41. **
  42. ** 22-04-2003 Jarek Burczynski:
  43. ** - implemented 100% correct LFO generator (verified on real YM2610 and YM2608)
  44. **
  45. ** 15-04-2003 Jarek Burczynski:
  46. ** - added support for YM2608's register 0x110 - status mask
  47. **
  48. ** 01-12-2002 Jarek Burczynski:
  49. ** - fixed register addressing in YM2608, YM2610, YM2610B chips. (verified on real YM2608)
  50. ** The addressing patch used for early Neo-Geo games can be removed now.
  51. **
  52. ** 26-11-2002 Jarek Burczynski, Nicola Salmoria:
  53. ** - recreated YM2608 ADPCM ROM using data from real YM2608's output which leads to:
  54. ** - added emulation of YM2608 drums.
  55. ** - output of YM2608 is two times lower now - same as YM2610 (verified on real YM2608)
  56. **
  57. ** 16-08-2002 Jarek Burczynski:
  58. ** - binary exact Envelope Generator (verified on real YM2203);
  59. ** identical to YM2151
  60. ** - corrected 'off by one' error in feedback calculations (when feedback is off)
  61. ** - corrected connection (algorithm) calculation (verified on real YM2203 and YM2610)
  62. **
  63. ** 18-12-2001 Jarek Burczynski:
  64. ** - added SSG-EG support (verified on real YM2203)
  65. **
  66. ** 12-08-2001 Jarek Burczynski:
  67. ** - corrected ym_sin_tab and ym_tl_tab data (verified on real chip)
  68. ** - corrected feedback calculations (verified on real chip)
  69. ** - corrected phase generator calculations (verified on real chip)
  70. ** - corrected envelope generator calculations (verified on real chip)
  71. ** - corrected FM volume level (YM2610 and YM2610B).
  72. ** - changed YMxxxUpdateOne() functions (YM2203, YM2608, YM2610, YM2610B, YM2612) :
  73. ** this was needed to calculate YM2610 FM channels output correctly.
  74. ** (Each FM channel is calculated as in other chips, but the output of the channel
  75. ** gets shifted right by one *before* sending to accumulator. That was impossible to do
  76. ** with previous implementation).
  77. **
  78. ** 23-07-2001 Jarek Burczynski, Nicola Salmoria:
  79. ** - corrected YM2610 ADPCM type A algorithm and tables (verified on real chip)
  80. **
  81. ** 11-06-2001 Jarek Burczynski:
  82. ** - corrected end of sample bug in ADPCMA_calc_cha().
  83. ** Real YM2610 checks for equality between current and end addresses (only 20 LSB bits).
  84. **
  85. ** 08-12-98 hiro-shi:
  86. ** rename ADPCMA -> ADPCMB, ADPCMB -> ADPCMA
  87. ** move ROM limit check.(CALC_CH? -> 2610Write1/2)
  88. ** test program (ADPCMB_TEST)
  89. ** move ADPCM A/B end check.
  90. ** ADPCMB repeat flag(no check)
  91. ** change ADPCM volume rate (8->16) (32->48).
  92. **
  93. ** 09-12-98 hiro-shi:
  94. ** change ADPCM volume. (8->16, 48->64)
  95. ** replace ym2610 ch0/3 (YM-2610B)
  96. ** change ADPCM_SHIFT (10->8) missing bank change 0x4000-0xffff.
  97. ** add ADPCM_SHIFT_MASK
  98. ** change ADPCMA_DECODE_MIN/MAX.
  99. */
  100. /************************************************************************/
  101. /* comment of hiro-shi(Hiromitsu Shioya) */
  102. /* YM2610(B) = OPN-B */
  103. /* YM2610 : PSG:3ch FM:4ch ADPCM(18.5KHz):6ch DeltaT ADPCM:1ch */
  104. /* YM2610B : PSG:3ch FM:6ch ADPCM(18.5KHz):6ch DeltaT ADPCM:1ch */
  105. /************************************************************************/
  106. //#include <stdio.h>
  107. #include <string.h>
  108. #include <math.h>
  109. #include "ym2612.h"
  110. #ifndef EXTERNAL_YM2612
  111. #include <stdlib.h>
  112. // let it be 1 global to simplify things
  113. YM2612 ym2612;
  114. #else
  115. extern YM2612 *ym2612_940;
  116. #define ym2612 (*ym2612_940)
  117. #endif
  118. void memset32(void *dest, int c, int count);
  119. #ifndef __GNUC__
  120. #pragma warning (disable:4100) // unreferenced formal parameter
  121. #pragma warning (disable:4244)
  122. #pragma warning (disable:4245) // signed/unsigned in conversion
  123. #pragma warning (disable:4710)
  124. #pragma warning (disable:4018) // signed/unsigned
  125. #endif
  126. #ifndef INLINE
  127. #define INLINE static __inline
  128. #endif
  129. #ifndef M_PI
  130. #define M_PI 3.14159265358979323846
  131. #endif
  132. /* globals */
  133. #define FREQ_SH 16 /* 16.16 fixed point (frequency calculations) */
  134. #define EG_SH 16 /* 16.16 fixed point (envelope generator timing) */
  135. #define LFO_SH 24 /* 8.24 fixed point (LFO calculations) */
  136. #define TIMER_SH 16 /* 16.16 fixed point (timers calculations) */
  137. #define ENV_BITS 10
  138. #define ENV_LEN (1<<ENV_BITS)
  139. #define ENV_STEP (128.0/ENV_LEN)
  140. #define MAX_ATT_INDEX (ENV_LEN-1) /* 1023 */
  141. #define MIN_ATT_INDEX (0) /* 0 */
  142. #define EG_ATT 4
  143. #define EG_DEC 3
  144. #define EG_SUS 2
  145. #define EG_REL 1
  146. #define EG_OFF 0
  147. #define SIN_BITS 10
  148. #define SIN_LEN (1<<SIN_BITS)
  149. #define SIN_MASK (SIN_LEN-1)
  150. #define TL_RES_LEN (256) /* 8 bits addressing (real chip) */
  151. #define EG_TIMER_OVERFLOW (3*(1<<EG_SH)) /* envelope generator timer overflows every 3 samples (on real chip) */
  152. /* TL_TAB_LEN is calculated as:
  153. * 13 - sinus amplitude bits (Y axis)
  154. * 2 - sinus sign bit (Y axis)
  155. * TL_RES_LEN - sinus resolution (X axis)
  156. */
  157. //#define TL_TAB_LEN (13*2*TL_RES_LEN)
  158. #define TL_TAB_LEN (13*TL_RES_LEN*256/8) // 106496*2
  159. UINT16 ym_tl_tab[TL_TAB_LEN];
  160. /* ~3K wasted but oh well */
  161. UINT16 ym_tl_tab2[13*TL_RES_LEN];
  162. #define ENV_QUIET (2*13*TL_RES_LEN/8)
  163. /* sin waveform table in 'decibel' scale (use only period/4 values) */
  164. static UINT16 ym_sin_tab[256];
  165. /* sustain level table (3dB per step) */
  166. /* bit0, bit1, bit2, bit3, bit4, bit5, bit6 */
  167. /* 1, 2, 4, 8, 16, 32, 64 (value)*/
  168. /* 0.75, 1.5, 3, 6, 12, 24, 48 (dB)*/
  169. /* 0 - 15: 0, 3, 6, 9,12,15,18,21,24,27,30,33,36,39,42,93 (dB)*/
  170. #define SC(db) (UINT32) ( db * (4.0/ENV_STEP) )
  171. static const UINT32 sl_table[16]={
  172. SC( 0),SC( 1),SC( 2),SC(3 ),SC(4 ),SC(5 ),SC(6 ),SC( 7),
  173. SC( 8),SC( 9),SC(10),SC(11),SC(12),SC(13),SC(14),SC(31)
  174. };
  175. #undef SC
  176. #if 0
  177. #define RATE_STEPS (8)
  178. static const UINT8 eg_inc[19*RATE_STEPS]={
  179. /*cycle:0 1 2 3 4 5 6 7*/
  180. /* 0 */ 0,1, 0,1, 0,1, 0,1, /* rates 00..11 0 (increment by 0 or 1) */
  181. /* 1 */ 0,1, 0,1, 1,1, 0,1, /* rates 00..11 1 */
  182. /* 2 */ 0,1, 1,1, 0,1, 1,1, /* rates 00..11 2 */
  183. /* 3 */ 0,1, 1,1, 1,1, 1,1, /* rates 00..11 3 */
  184. /* 4 */ 1,1, 1,1, 1,1, 1,1, /* rate 12 0 (increment by 1) */
  185. /* 5 */ 1,1, 1,2, 1,1, 1,2, /* rate 12 1 */
  186. /* 6 */ 1,2, 1,2, 1,2, 1,2, /* rate 12 2 */
  187. /* 7 */ 1,2, 2,2, 1,2, 2,2, /* rate 12 3 */
  188. /* 8 */ 2,2, 2,2, 2,2, 2,2, /* rate 13 0 (increment by 2) */
  189. /* 9 */ 2,2, 2,4, 2,2, 2,4, /* rate 13 1 */
  190. /*10 */ 2,4, 2,4, 2,4, 2,4, /* rate 13 2 */
  191. /*11 */ 2,4, 4,4, 2,4, 4,4, /* rate 13 3 */
  192. /*12 */ 4,4, 4,4, 4,4, 4,4, /* rate 14 0 (increment by 4) */
  193. /*13 */ 4,4, 4,8, 4,4, 4,8, /* rate 14 1 */
  194. /*14 */ 4,8, 4,8, 4,8, 4,8, /* rate 14 2 */
  195. /*15 */ 4,8, 8,8, 4,8, 8,8, /* rate 14 3 */
  196. /*16 */ 8,8, 8,8, 8,8, 8,8, /* rates 15 0, 15 1, 15 2, 15 3 (increment by 8) */
  197. /*17 */ 16,16,16,16,16,16,16,16, /* rates 15 2, 15 3 for attack */
  198. /*18 */ 0,0, 0,0, 0,0, 0,0, /* infinity rates for attack and decay(s) */
  199. };
  200. #endif
  201. #define PACK(a0,a1,a2,a3,a4,a5,a6,a7) ((a7<<21)|(a6<<18)|(a5<<15)|(a4<<12)|(a3<<9)|(a2<<6)|(a1<<3)|(a0<<0))
  202. static const UINT32 eg_inc_pack[19] =
  203. {
  204. /* 0 */ PACK(0,1,0,1,0,1,0,1), /* rates 00..11 0 (increment by 0 or 1) */
  205. /* 1 */ PACK(0,1,0,1,1,1,0,1), /* rates 00..11 1 */
  206. /* 2 */ PACK(0,1,1,1,0,1,1,1), /* rates 00..11 2 */
  207. /* 3 */ PACK(0,1,1,1,1,1,1,1), /* rates 00..11 3 */
  208. /* 4 */ PACK(1,1,1,1,1,1,1,1), /* rate 12 0 (increment by 1) */
  209. /* 5 */ PACK(1,1,1,2,1,1,1,2), /* rate 12 1 */
  210. /* 6 */ PACK(1,2,1,2,1,2,1,2), /* rate 12 2 */
  211. /* 7 */ PACK(1,2,2,2,1,2,2,2), /* rate 12 3 */
  212. /* 8 */ PACK(2,2,2,2,2,2,2,2), /* rate 13 0 (increment by 2) */
  213. /* 9 */ PACK(2,2,2,3,2,2,2,3), /* rate 13 1 */
  214. /*10 */ PACK(2,3,2,3,2,3,2,3), /* rate 13 2 */
  215. /*11 */ PACK(2,3,3,3,2,3,3,3), /* rate 13 3 */
  216. /*12 */ PACK(3,3,3,3,3,3,3,3), /* rate 14 0 (increment by 4) */
  217. /*13 */ PACK(3,3,3,4,3,3,3,4), /* rate 14 1 */
  218. /*14 */ PACK(3,4,3,4,3,4,3,4), /* rate 14 2 */
  219. /*15 */ PACK(3,4,4,4,3,4,4,4), /* rate 14 3 */
  220. /*16 */ PACK(4,4,4,4,4,4,4,4), /* rates 15 0, 15 1, 15 2, 15 3 (increment by 8) */
  221. /*17 */ PACK(5,5,5,5,5,5,5,5), /* rates 15 2, 15 3 for attack */
  222. /*18 */ PACK(0,0,0,0,0,0,0,0), /* infinity rates for attack and decay(s) */
  223. };
  224. //#define O(a) (a*RATE_STEPS)
  225. #define O(a) a
  226. /*note that there is no O(17) in this table - it's directly in the code */
  227. static const UINT8 eg_rate_select[32+64+32]={ /* Envelope Generator rates (32 + 64 rates + 32 RKS) */
  228. /* 32 infinite time rates */
  229. O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),
  230. O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),
  231. O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),
  232. O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),
  233. /* rates 00-11 */
  234. O(18),O(18),O( 2),O( 3),
  235. O( 0),O( 1),O( 2),O( 3),
  236. O( 0),O( 1),O( 2),O( 3),
  237. O( 0),O( 1),O( 2),O( 3),
  238. O( 0),O( 1),O( 2),O( 3),
  239. O( 0),O( 1),O( 2),O( 3),
  240. O( 0),O( 1),O( 2),O( 3),
  241. O( 0),O( 1),O( 2),O( 3),
  242. O( 0),O( 1),O( 2),O( 3),
  243. O( 0),O( 1),O( 2),O( 3),
  244. O( 0),O( 1),O( 2),O( 3),
  245. O( 0),O( 1),O( 2),O( 3),
  246. /* rate 12 */
  247. O( 4),O( 5),O( 6),O( 7),
  248. /* rate 13 */
  249. O( 8),O( 9),O(10),O(11),
  250. /* rate 14 */
  251. O(12),O(13),O(14),O(15),
  252. /* rate 15 */
  253. O(16),O(16),O(16),O(16),
  254. /* 32 dummy rates (same as 15 3) */
  255. O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16),
  256. O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16),
  257. O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16),
  258. O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16)
  259. };
  260. #undef O
  261. /*rate 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15*/
  262. /*shift 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0 */
  263. /*mask 2047, 1023, 511, 255, 127, 63, 31, 15, 7, 3, 1, 0, 0, 0, 0, 0 */
  264. #define O(a) (a*1)
  265. static const UINT8 eg_rate_shift[32+64+32]={ /* Envelope Generator counter shifts (32 + 64 rates + 32 RKS) */
  266. /* 32 infinite time rates */
  267. O(11),O(11),O(11),O(11),O(11),O(11),O(11),O(11),
  268. O(11),O(11),O(11),O(11),O(11),O(11),O(11),O(11),
  269. O(11),O(11),O(11),O(11),O(11),O(11),O(11),O(11),
  270. O(11),O(11),O(11),O(11),O(11),O(11),O(11),O(11),
  271. /* rates 00-11 */
  272. O(11),O(11),O(11),O(11),
  273. O(10),O(10),O(10),O(10),
  274. O( 9),O( 9),O( 9),O( 9),
  275. O( 8),O( 8),O( 8),O( 8),
  276. O( 7),O( 7),O( 7),O( 7),
  277. O( 6),O( 6),O( 6),O( 6),
  278. O( 5),O( 5),O( 5),O( 5),
  279. O( 4),O( 4),O( 4),O( 4),
  280. O( 3),O( 3),O( 3),O( 3),
  281. O( 2),O( 2),O( 2),O( 2),
  282. O( 1),O( 1),O( 1),O( 1),
  283. O( 0),O( 0),O( 0),O( 0),
  284. /* rate 12 */
  285. O( 0),O( 0),O( 0),O( 0),
  286. /* rate 13 */
  287. O( 0),O( 0),O( 0),O( 0),
  288. /* rate 14 */
  289. O( 0),O( 0),O( 0),O( 0),
  290. /* rate 15 */
  291. O( 0),O( 0),O( 0),O( 0),
  292. /* 32 dummy rates (same as 15 3) */
  293. O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),
  294. O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),
  295. O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),
  296. O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0)
  297. };
  298. #undef O
  299. static const UINT8 dt_tab[4 * 32]={
  300. /* this is YM2151 and YM2612 phase increment data (in 10.10 fixed point format)*/
  301. /* FD=0 */
  302. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  303. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  304. /* FD=1 */
  305. 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2,
  306. 2, 3, 3, 3, 4, 4, 4, 5, 5, 6, 6, 7, 8, 8, 8, 8,
  307. /* FD=2 */
  308. 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5,
  309. 5, 6, 6, 7, 8, 8, 9,10,11,12,13,14,16,16,16,16,
  310. /* FD=3 */
  311. 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 6, 6, 7,
  312. 8 ,8, 9,10,11,12,13,14,16,17,19,20,22,22,22,22
  313. };
  314. /* OPN key frequency number -> key code follow table */
  315. /* fnum higher 4bit -> keycode lower 2bit */
  316. static const UINT8 opn_fktable[16] = {0,0,0,0,0,0,0,1,2,3,3,3,3,3,3,3};
  317. /* 8 LFO speed parameters */
  318. /* each value represents number of samples that one LFO level will last for */
  319. static const UINT32 lfo_samples_per_step[8] = {108, 77, 71, 67, 62, 44, 8, 5};
  320. /*There are 4 different LFO AM depths available, they are:
  321. 0 dB, 1.4 dB, 5.9 dB, 11.8 dB
  322. Here is how it is generated (in EG steps):
  323. 11.8 dB = 0, 2, 4, 6, 8, 10,12,14,16...126,126,124,122,120,118,....4,2,0
  324. 5.9 dB = 0, 1, 2, 3, 4, 5, 6, 7, 8....63, 63, 62, 61, 60, 59,.....2,1,0
  325. 1.4 dB = 0, 0, 0, 0, 1, 1, 1, 1, 2,...15, 15, 15, 15, 14, 14,.....0,0,0
  326. (1.4 dB is loosing precision as you can see)
  327. It's implemented as generator from 0..126 with step 2 then a shift
  328. right N times, where N is:
  329. 8 for 0 dB
  330. 3 for 1.4 dB
  331. 1 for 5.9 dB
  332. 0 for 11.8 dB
  333. */
  334. static const UINT8 lfo_ams_depth_shift[4] = {8, 3, 1, 0};
  335. /*There are 8 different LFO PM depths available, they are:
  336. 0, 3.4, 6.7, 10, 14, 20, 40, 80 (cents)
  337. Modulation level at each depth depends on F-NUMBER bits: 4,5,6,7,8,9,10
  338. (bits 8,9,10 = FNUM MSB from OCT/FNUM register)
  339. Here we store only first quarter (positive one) of full waveform.
  340. Full table (lfo_pm_table) containing all 128 waveforms is build
  341. at run (init) time.
  342. One value in table below represents 4 (four) basic LFO steps
  343. (1 PM step = 4 AM steps).
  344. For example:
  345. at LFO SPEED=0 (which is 108 samples per basic LFO step)
  346. one value from "lfo_pm_output" table lasts for 432 consecutive
  347. samples (4*108=432) and one full LFO waveform cycle lasts for 13824
  348. samples (32*432=13824; 32 because we store only a quarter of whole
  349. waveform in the table below)
  350. */
  351. static const UINT8 lfo_pm_output[7*8][8]={ /* 7 bits meaningful (of F-NUMBER), 8 LFO output levels per one depth (out of 32), 8 LFO depths */
  352. /* FNUM BIT 4: 000 0001xxxx */
  353. /* DEPTH 0 */ {0, 0, 0, 0, 0, 0, 0, 0},
  354. /* DEPTH 1 */ {0, 0, 0, 0, 0, 0, 0, 0},
  355. /* DEPTH 2 */ {0, 0, 0, 0, 0, 0, 0, 0},
  356. /* DEPTH 3 */ {0, 0, 0, 0, 0, 0, 0, 0},
  357. /* DEPTH 4 */ {0, 0, 0, 0, 0, 0, 0, 0},
  358. /* DEPTH 5 */ {0, 0, 0, 0, 0, 0, 0, 0},
  359. /* DEPTH 6 */ {0, 0, 0, 0, 0, 0, 0, 0},
  360. /* DEPTH 7 */ {0, 0, 0, 0, 1, 1, 1, 1},
  361. /* FNUM BIT 5: 000 0010xxxx */
  362. /* DEPTH 0 */ {0, 0, 0, 0, 0, 0, 0, 0},
  363. /* DEPTH 1 */ {0, 0, 0, 0, 0, 0, 0, 0},
  364. /* DEPTH 2 */ {0, 0, 0, 0, 0, 0, 0, 0},
  365. /* DEPTH 3 */ {0, 0, 0, 0, 0, 0, 0, 0},
  366. /* DEPTH 4 */ {0, 0, 0, 0, 0, 0, 0, 0},
  367. /* DEPTH 5 */ {0, 0, 0, 0, 0, 0, 0, 0},
  368. /* DEPTH 6 */ {0, 0, 0, 0, 1, 1, 1, 1},
  369. /* DEPTH 7 */ {0, 0, 1, 1, 2, 2, 2, 3},
  370. /* FNUM BIT 6: 000 0100xxxx */
  371. /* DEPTH 0 */ {0, 0, 0, 0, 0, 0, 0, 0},
  372. /* DEPTH 1 */ {0, 0, 0, 0, 0, 0, 0, 0},
  373. /* DEPTH 2 */ {0, 0, 0, 0, 0, 0, 0, 0},
  374. /* DEPTH 3 */ {0, 0, 0, 0, 0, 0, 0, 0},
  375. /* DEPTH 4 */ {0, 0, 0, 0, 0, 0, 0, 1},
  376. /* DEPTH 5 */ {0, 0, 0, 0, 1, 1, 1, 1},
  377. /* DEPTH 6 */ {0, 0, 1, 1, 2, 2, 2, 3},
  378. /* DEPTH 7 */ {0, 0, 2, 3, 4, 4, 5, 6},
  379. /* FNUM BIT 7: 000 1000xxxx */
  380. /* DEPTH 0 */ {0, 0, 0, 0, 0, 0, 0, 0},
  381. /* DEPTH 1 */ {0, 0, 0, 0, 0, 0, 0, 0},
  382. /* DEPTH 2 */ {0, 0, 0, 0, 0, 0, 1, 1},
  383. /* DEPTH 3 */ {0, 0, 0, 0, 1, 1, 1, 1},
  384. /* DEPTH 4 */ {0, 0, 0, 1, 1, 1, 1, 2},
  385. /* DEPTH 5 */ {0, 0, 1, 1, 2, 2, 2, 3},
  386. /* DEPTH 6 */ {0, 0, 2, 3, 4, 4, 5, 6},
  387. /* DEPTH 7 */ {0, 0, 4, 6, 8, 8, 0xa, 0xc},
  388. /* FNUM BIT 8: 001 0000xxxx */
  389. /* DEPTH 0 */ {0, 0, 0, 0, 0, 0, 0, 0},
  390. /* DEPTH 1 */ {0, 0, 0, 0, 1, 1, 1, 1},
  391. /* DEPTH 2 */ {0, 0, 0, 1, 1, 1, 2, 2},
  392. /* DEPTH 3 */ {0, 0, 1, 1, 2, 2, 3, 3},
  393. /* DEPTH 4 */ {0, 0, 1, 2, 2, 2, 3, 4},
  394. /* DEPTH 5 */ {0, 0, 2, 3, 4, 4, 5, 6},
  395. /* DEPTH 6 */ {0, 0, 4, 6, 8, 8, 0xa, 0xc},
  396. /* DEPTH 7 */ {0, 0, 8, 0xc,0x10,0x10,0x14,0x18},
  397. /* FNUM BIT 9: 010 0000xxxx */
  398. /* DEPTH 0 */ {0, 0, 0, 0, 0, 0, 0, 0},
  399. /* DEPTH 1 */ {0, 0, 0, 0, 2, 2, 2, 2},
  400. /* DEPTH 2 */ {0, 0, 0, 2, 2, 2, 4, 4},
  401. /* DEPTH 3 */ {0, 0, 2, 2, 4, 4, 6, 6},
  402. /* DEPTH 4 */ {0, 0, 2, 4, 4, 4, 6, 8},
  403. /* DEPTH 5 */ {0, 0, 4, 6, 8, 8, 0xa, 0xc},
  404. /* DEPTH 6 */ {0, 0, 8, 0xc,0x10,0x10,0x14,0x18},
  405. /* DEPTH 7 */ {0, 0,0x10,0x18,0x20,0x20,0x28,0x30},
  406. /* FNUM BIT10: 100 0000xxxx */
  407. /* DEPTH 0 */ {0, 0, 0, 0, 0, 0, 0, 0},
  408. /* DEPTH 1 */ {0, 0, 0, 0, 4, 4, 4, 4},
  409. /* DEPTH 2 */ {0, 0, 0, 4, 4, 4, 8, 8},
  410. /* DEPTH 3 */ {0, 0, 4, 4, 8, 8, 0xc, 0xc},
  411. /* DEPTH 4 */ {0, 0, 4, 8, 8, 8, 0xc,0x10},
  412. /* DEPTH 5 */ {0, 0, 8, 0xc,0x10,0x10,0x14,0x18},
  413. /* DEPTH 6 */ {0, 0,0x10,0x18,0x20,0x20,0x28,0x30},
  414. /* DEPTH 7 */ {0, 0,0x20,0x30,0x40,0x40,0x50,0x60},
  415. };
  416. /* all 128 LFO PM waveforms */
  417. static INT32 lfo_pm_table[128*8*32]; /* 128 combinations of 7 bits meaningful (of F-NUMBER), 8 LFO depths, 32 LFO output levels per one depth */
  418. /* there are 2048 FNUMs that can be generated using FNUM/BLK registers
  419. but LFO works with one more bit of a precision so we really need 4096 elements */
  420. static UINT32 fn_table[4096]; /* fnumber->increment counter */
  421. static int g_lfo_ampm;
  422. /* register number to channel number , slot offset */
  423. #define OPN_CHAN(N) (N&3)
  424. #define OPN_SLOT(N) ((N>>2)&3)
  425. /* slot number */
  426. #define SLOT1 0
  427. #define SLOT2 2
  428. #define SLOT3 1
  429. #define SLOT4 3
  430. /* OPN Mode Register Write */
  431. INLINE void set_timers( int v )
  432. {
  433. /* b7 = CSM MODE */
  434. /* b6 = 3 slot mode */
  435. /* b5 = reset b */
  436. /* b4 = reset a */
  437. /* b3 = timer enable b */
  438. /* b2 = timer enable a */
  439. /* b1 = load b */
  440. /* b0 = load a */
  441. ym2612.OPN.ST.mode = v;
  442. /* reset Timer b flag */
  443. if( v & 0x20 )
  444. ym2612.OPN.ST.status &= ~2;
  445. /* reset Timer a flag */
  446. if( v & 0x10 )
  447. ym2612.OPN.ST.status &= ~1;
  448. }
  449. INLINE void recalc_volout(FM_SLOT *SLOT)
  450. {
  451. INT16 vol_out = SLOT->volume;
  452. if ((SLOT->ssg&0x0c) == 0x0c)
  453. vol_out = (0x200 - SLOT->volume) & MAX_ATT_INDEX;
  454. SLOT->vol_out = vol_out + SLOT->tl;
  455. }
  456. INLINE void FM_KEYON(int c , int s )
  457. {
  458. FM_SLOT *SLOT = &ym2612.CH[c].SLOT[s];
  459. if( !SLOT->key )
  460. {
  461. SLOT->key = 1;
  462. SLOT->phase = 0; /* restart Phase Generator */
  463. SLOT->ssg ^= SLOT->ssgn;
  464. SLOT->ssgn = 0;
  465. SLOT->state = (SLOT->sl == MIN_ATT_INDEX) ? EG_SUS : EG_DEC;
  466. if (SLOT->ar_ksr < 32+62) {
  467. if (SLOT->volume > MIN_ATT_INDEX) SLOT->state = EG_ATT;
  468. } else {
  469. SLOT->volume = MIN_ATT_INDEX;
  470. }
  471. recalc_volout(SLOT);
  472. ym2612.slot_mask |= (1<<s) << (c*4);
  473. }
  474. }
  475. INLINE void FM_KEYOFF(int c , int s )
  476. {
  477. FM_SLOT *SLOT = &ym2612.CH[c].SLOT[s];
  478. if( SLOT->key )
  479. {
  480. SLOT->key = 0;
  481. if (SLOT->state>EG_REL) {
  482. SLOT->state = EG_REL;/* phase -> Release */
  483. if (SLOT->ssg&0x08) {
  484. if (SLOT->ssg&0x04)
  485. SLOT->volume = (0x200 - SLOT->volume);
  486. if (SLOT->volume >= 0x200) {
  487. SLOT->volume = MAX_ATT_INDEX;
  488. SLOT->state = EG_OFF;
  489. }
  490. }
  491. }
  492. SLOT->vol_out = SLOT->volume + SLOT->tl;
  493. }
  494. }
  495. /* set detune & multiple */
  496. INLINE void set_det_mul(FM_CH *CH, FM_SLOT *SLOT, int v)
  497. {
  498. SLOT->mul = (v&0x0f)? (v&0x0f)*2 : 1;
  499. SLOT->DT = ym2612.OPN.ST.dt_tab[(v>>4)&7];
  500. CH->SLOT[SLOT1].Incr=-1;
  501. }
  502. /* set total level */
  503. INLINE void set_tl(FM_SLOT *SLOT, int v)
  504. {
  505. SLOT->tl = (v&0x7f)<<(ENV_BITS-7); /* 7bit TL */
  506. if (SLOT->state > EG_REL)
  507. recalc_volout(SLOT);
  508. }
  509. /* set attack rate & key scale */
  510. INLINE void set_ar_ksr(FM_CH *CH, FM_SLOT *SLOT, int v)
  511. {
  512. UINT8 old_KSR = SLOT->KSR;
  513. int eg_sh_ar, eg_sel_ar;
  514. SLOT->ar = (v&0x1f) ? 32 + ((v&0x1f)<<1) : 0;
  515. SLOT->ar_ksr = SLOT->ar + SLOT->ksr;
  516. SLOT->KSR = 3-(v>>6);
  517. if (SLOT->KSR != old_KSR)
  518. {
  519. CH->SLOT[SLOT1].Incr=-1;
  520. }
  521. /* refresh Attack rate */
  522. if ((SLOT->ar_ksr) < 32+62)
  523. {
  524. eg_sh_ar = eg_rate_shift [SLOT->ar_ksr];
  525. eg_sel_ar = eg_rate_select[SLOT->ar_ksr];
  526. }
  527. else
  528. {
  529. eg_sh_ar = 0;
  530. eg_sel_ar = 18;
  531. }
  532. SLOT->eg_pack_ar = eg_inc_pack[eg_sel_ar] | (eg_sh_ar<<24);
  533. }
  534. /* set decay rate */
  535. INLINE void set_dr(FM_SLOT *SLOT, int v)
  536. {
  537. int eg_sh_d1r, eg_sel_d1r;
  538. SLOT->d1r = (v&0x1f) ? 32 + ((v&0x1f)<<1) : 0;
  539. eg_sh_d1r = eg_rate_shift [SLOT->d1r + SLOT->ksr];
  540. eg_sel_d1r= eg_rate_select[SLOT->d1r + SLOT->ksr];
  541. SLOT->eg_pack_d1r = eg_inc_pack[eg_sel_d1r] | (eg_sh_d1r<<24);
  542. }
  543. /* set sustain rate */
  544. INLINE void set_sr(FM_SLOT *SLOT, int v)
  545. {
  546. int eg_sh_d2r, eg_sel_d2r;
  547. SLOT->d2r = (v&0x1f) ? 32 + ((v&0x1f)<<1) : 0;
  548. eg_sh_d2r = eg_rate_shift [SLOT->d2r + SLOT->ksr];
  549. eg_sel_d2r= eg_rate_select[SLOT->d2r + SLOT->ksr];
  550. SLOT->eg_pack_d2r = eg_inc_pack[eg_sel_d2r] | (eg_sh_d2r<<24);
  551. }
  552. /* set release rate */
  553. INLINE void set_sl_rr(FM_SLOT *SLOT, int v)
  554. {
  555. int eg_sh_rr, eg_sel_rr;
  556. SLOT->sl = sl_table[ v>>4 ];
  557. if (SLOT->state == EG_DEC && (SLOT->volume >= (INT32)(SLOT->sl)))
  558. SLOT->state = EG_SUS;
  559. SLOT->rr = 34 + ((v&0x0f)<<2);
  560. eg_sh_rr = eg_rate_shift [SLOT->rr + SLOT->ksr];
  561. eg_sel_rr = eg_rate_select[SLOT->rr + SLOT->ksr];
  562. SLOT->eg_pack_rr = eg_inc_pack[eg_sel_rr] | (eg_sh_rr<<24);
  563. }
  564. INLINE signed int op_calc(UINT32 phase, unsigned int env, signed int pm)
  565. {
  566. int ret, sin = (phase>>16) + (pm>>1);
  567. int neg = sin & 0x200;
  568. if (sin & 0x100) sin ^= 0xff;
  569. sin&=0xff;
  570. env&=~1;
  571. // this was already checked
  572. // if (env >= ENV_QUIET) // 384
  573. // return 0;
  574. ret = ym_tl_tab[sin | (env<<7)];
  575. return neg ? -ret : ret;
  576. }
  577. INLINE signed int op_calc1(UINT32 phase, unsigned int env, signed int pm)
  578. {
  579. int ret, sin = (phase+pm)>>16;
  580. int neg = sin & 0x200;
  581. if (sin & 0x100) sin ^= 0xff;
  582. sin&=0xff;
  583. env&=~1;
  584. // if (env >= ENV_QUIET) // 384
  585. // return 0;
  586. ret = ym_tl_tab[sin | (env<<7)];
  587. return neg ? -ret : ret;
  588. }
  589. #if !defined(_ASM_YM2612_C) || defined(EXTERNAL_YM2612)
  590. /* advance LFO to next sample */
  591. INLINE int advance_lfo(int lfo_ampm, UINT32 lfo_cnt_old, UINT32 lfo_cnt)
  592. {
  593. UINT8 pos;
  594. UINT8 prev_pos;
  595. prev_pos = (lfo_cnt_old >> LFO_SH) & 127;
  596. pos = (lfo_cnt >> LFO_SH) & 127;
  597. /* update AM when LFO output changes */
  598. if (prev_pos != pos)
  599. {
  600. lfo_ampm &= 0xff;
  601. /* triangle (inverted) */
  602. /* AM: 0 to 126 step +2, 126 to 0 step -2 */
  603. if (pos<64)
  604. lfo_ampm |= ((pos^63) * 2) << 8; /* 0 - 126 */
  605. else
  606. lfo_ampm |= ((pos&63) * 2) << 8;
  607. }
  608. else
  609. {
  610. return lfo_ampm;
  611. }
  612. /* PM works with 4 times slower clock */
  613. prev_pos >>= 2;
  614. pos >>= 2;
  615. /* update PM when LFO output changes */
  616. if (prev_pos != pos)
  617. {
  618. lfo_ampm &= ~0xff;
  619. lfo_ampm |= pos; /* 0 - 32 */
  620. }
  621. return lfo_ampm;
  622. }
  623. INLINE void update_eg_phase(FM_SLOT *SLOT, UINT32 eg_cnt, UINT32 ssg_en)
  624. {
  625. INT32 volume = SLOT->volume;
  626. UINT32 pack = SLOT->eg_pack[SLOT->state - 1];
  627. UINT32 shift = pack >> 24;
  628. INT32 eg_inc_val;
  629. if (eg_cnt & ((1 << shift) - 1))
  630. return;
  631. eg_inc_val = pack >> ((eg_cnt >> shift) & 7) * 3;
  632. eg_inc_val = (1 << (eg_inc_val & 7)) >> 1;
  633. if ((SLOT->ssg&0x08) && ssg_en) {
  634. switch (SLOT->state)
  635. {
  636. case EG_ATT: /* attack phase */
  637. volume += ( ~volume * eg_inc_val ) >> 4;
  638. if ( volume <= MIN_ATT_INDEX )
  639. {
  640. volume = MIN_ATT_INDEX;
  641. SLOT->state = (SLOT->sl == MIN_ATT_INDEX) ? EG_SUS: EG_DEC;
  642. }
  643. break;
  644. case EG_DEC: /* decay phase */
  645. if (volume < 0x200)
  646. volume += 4*eg_inc_val;
  647. if ( volume >= (INT32) SLOT->sl )
  648. SLOT->state = EG_SUS;
  649. break;
  650. case EG_SUS: /* sustain phase */
  651. if (volume < 0x200)
  652. volume += 4*eg_inc_val;
  653. break;
  654. case EG_REL: /* release phase */
  655. if (volume < 0x200)
  656. volume += 4*eg_inc_val;
  657. if ( volume >= 0x200 )
  658. {
  659. volume = MAX_ATT_INDEX;
  660. SLOT->state = EG_OFF;
  661. }
  662. break;
  663. }
  664. SLOT->vol_out = volume + SLOT->tl;
  665. if ((SLOT->ssg&0x04) && (SLOT->state > EG_REL))
  666. SLOT->vol_out = ((0x200 - volume) & MAX_ATT_INDEX) + SLOT->tl;
  667. } else {
  668. switch (SLOT->state)
  669. {
  670. case EG_ATT: /* attack phase */
  671. volume += ( ~volume * eg_inc_val ) >> 4;
  672. if ( volume <= MIN_ATT_INDEX )
  673. {
  674. volume = MIN_ATT_INDEX;
  675. SLOT->state = (SLOT->sl == MIN_ATT_INDEX) ? EG_SUS: EG_DEC;
  676. }
  677. break;
  678. case EG_DEC: /* decay phase */
  679. volume += eg_inc_val;
  680. if ( volume >= (INT32) SLOT->sl )
  681. SLOT->state = EG_SUS;
  682. break;
  683. case EG_SUS: /* sustain phase */
  684. volume += eg_inc_val;
  685. if ( volume >= MAX_ATT_INDEX )
  686. {
  687. volume = MAX_ATT_INDEX;
  688. /* do not change SLOT->state (verified on real chip) */
  689. }
  690. break;
  691. case EG_REL: /* release phase */
  692. volume += eg_inc_val;
  693. if ( volume >= MAX_ATT_INDEX )
  694. {
  695. volume = MAX_ATT_INDEX;
  696. SLOT->state = EG_OFF;
  697. }
  698. break;
  699. }
  700. SLOT->vol_out = volume + SLOT->tl;
  701. }
  702. SLOT->volume = volume;
  703. }
  704. INLINE UINT32 update_ssg_eg_phase(FM_SLOT *SLOT, UINT32 phase)
  705. {
  706. if (SLOT->ssg&0x01) {
  707. if (SLOT->ssg&0x02) {
  708. SLOT->ssg ^= SLOT->ssgn ^ 4;
  709. SLOT->ssgn = 4;
  710. }
  711. if (SLOT->state != EG_ATT && !(SLOT->ssg&0x04))
  712. SLOT->volume = MAX_ATT_INDEX;
  713. } else {
  714. if (SLOT->ssg&0x02) {
  715. SLOT->ssg ^= 4;
  716. SLOT->ssgn ^= 4;
  717. } else
  718. phase = 0;
  719. if (SLOT->state != EG_ATT) {
  720. SLOT->state = (SLOT->sl == MIN_ATT_INDEX) ? EG_SUS : EG_DEC;
  721. if (SLOT->ar_ksr < 32+62) {
  722. if (SLOT->volume > MIN_ATT_INDEX) SLOT->state = EG_ATT;
  723. } else {
  724. SLOT->volume = MIN_ATT_INDEX;
  725. }
  726. }
  727. }
  728. // recalc_volout(SLOT);
  729. return phase;
  730. }
  731. #endif
  732. typedef struct
  733. {
  734. UINT16 vol_out1; /* 00: current output from EG circuit (without AM from LFO) */
  735. UINT16 vol_out2;
  736. UINT16 vol_out3;
  737. UINT16 vol_out4;
  738. UINT32 pad[2];
  739. UINT32 phase1; /* 10 */
  740. UINT32 phase2;
  741. UINT32 phase3;
  742. UINT32 phase4;
  743. UINT32 incr1; /* 20: phase step */
  744. UINT32 incr2;
  745. UINT32 incr3;
  746. UINT32 incr4;
  747. UINT32 lfo_cnt; /* 30 */
  748. UINT32 lfo_inc;
  749. INT32 mem; /* one sample delay memory */
  750. UINT32 eg_cnt; /* envelope generator counter */
  751. FM_CH *CH; /* 40: envelope generator counter */
  752. UINT32 eg_timer;
  753. UINT32 eg_timer_add;
  754. UINT32 pack; // 4c: stereo, lastchan, disabled, lfo_enabled | pan_r, pan_l, ams[2] | AMmasks[4] | FB[4] | lfo_ampm[16]
  755. UINT32 algo; /* 50: algo[3], was_update */
  756. INT32 op1_out;
  757. #ifdef _MIPS_ARCH_ALLEGREX
  758. UINT32 pad1[3+8];
  759. #endif
  760. } chan_rend_context;
  761. #if !defined(_ASM_YM2612_C) || defined(EXTERNAL_YM2612)
  762. #include <limits.h>
  763. static int clip(int n)
  764. {
  765. unsigned b = 14, s = n < 0;
  766. int m = s + INT_MAX;
  767. if (s + (n>>(b-1))) n = m >> (8*sizeof(int)-b);
  768. return n;
  769. }
  770. static void chan_render_loop(chan_rend_context *ct, int *buffer, int length)
  771. {
  772. int scounter; /* sample counter */
  773. /* sample generating loop */
  774. for (scounter = 0; scounter < length; scounter++)
  775. {
  776. int smp = 0; /* produced sample */
  777. unsigned int eg_out, eg_out2, eg_out4;
  778. FM_SLOT *SLOT;
  779. UINT32 cnt = ct->eg_timer_add+(ct->eg_timer & ((1<<EG_SH)-1));
  780. if (ct->pack & 2) while (cnt >= 1<<EG_SH) {
  781. cnt -= 1<<EG_SH;
  782. SLOT = &ct->CH->SLOT[SLOT1];
  783. if ((SLOT->ssg&0x08) && SLOT->state > EG_REL && SLOT->volume >= 0x200)
  784. ct->phase1 = update_ssg_eg_phase(SLOT, ct->phase1);
  785. SLOT = &ct->CH->SLOT[SLOT2];
  786. if ((SLOT->ssg&0x08) && SLOT->state > EG_REL && SLOT->volume >= 0x200)
  787. ct->phase2 = update_ssg_eg_phase(SLOT, ct->phase2);
  788. SLOT = &ct->CH->SLOT[SLOT3];
  789. if ((SLOT->ssg&0x08) && SLOT->state > EG_REL && SLOT->volume >= 0x200)
  790. ct->phase3 = update_ssg_eg_phase(SLOT, ct->phase3);
  791. SLOT = &ct->CH->SLOT[SLOT4];
  792. if ((SLOT->ssg&0x08) && SLOT->state > EG_REL && SLOT->volume >= 0x200)
  793. ct->phase4 = update_ssg_eg_phase(SLOT, ct->phase4);
  794. }
  795. if (ct->pack & 8) { /* LFO enabled ? (test Earthworm Jim in between demo 1 and 2) */
  796. ct->pack = (ct->pack&0xffff) | (advance_lfo(ct->pack >> 16, ct->lfo_cnt, ct->lfo_cnt + ct->lfo_inc) << 16);
  797. ct->lfo_cnt += ct->lfo_inc;
  798. }
  799. ct->eg_timer += ct->eg_timer_add;
  800. if (ct->eg_timer < EG_TIMER_OVERFLOW) {
  801. SLOT = &ct->CH->SLOT[SLOT1];
  802. SLOT->vol_ipol = SLOT->vol_out;
  803. if (SLOT->state > EG_REL) recalc_volout(SLOT);
  804. SLOT = &ct->CH->SLOT[SLOT2];
  805. SLOT->vol_ipol = SLOT->vol_out;
  806. if (SLOT->state > EG_REL) recalc_volout(SLOT);
  807. SLOT = &ct->CH->SLOT[SLOT3];
  808. SLOT->vol_ipol = SLOT->vol_out;
  809. if (SLOT->state > EG_REL) recalc_volout(SLOT);
  810. SLOT = &ct->CH->SLOT[SLOT4];
  811. SLOT->vol_ipol = SLOT->vol_out;
  812. if (SLOT->state > EG_REL) recalc_volout(SLOT);
  813. }
  814. else while (ct->eg_timer >= EG_TIMER_OVERFLOW)
  815. {
  816. ct->eg_timer -= EG_TIMER_OVERFLOW;
  817. ct->eg_cnt++;
  818. if (ct->eg_cnt >= 4096) ct->eg_cnt = 1;
  819. SLOT = &ct->CH->SLOT[SLOT1];
  820. SLOT->vol_ipol = SLOT->vol_out;
  821. if (SLOT->state != EG_OFF) update_eg_phase(SLOT, ct->eg_cnt, ct->pack & 2);
  822. SLOT = &ct->CH->SLOT[SLOT2];
  823. SLOT->vol_ipol = SLOT->vol_out;
  824. if (SLOT->state != EG_OFF) update_eg_phase(SLOT, ct->eg_cnt, ct->pack & 2);
  825. SLOT = &ct->CH->SLOT[SLOT3];
  826. SLOT->vol_ipol = SLOT->vol_out;
  827. if (SLOT->state != EG_OFF) update_eg_phase(SLOT, ct->eg_cnt, ct->pack & 2);
  828. SLOT = &ct->CH->SLOT[SLOT4];
  829. SLOT->vol_ipol = SLOT->vol_out;
  830. if (SLOT->state != EG_OFF) update_eg_phase(SLOT, ct->eg_cnt, ct->pack & 2);
  831. }
  832. #if 0
  833. UINT32 ifrac0 = ct->eg_timer / (EG_TIMER_OVERFLOW>>EG_SH);
  834. UINT32 ifrac1 = (1<<EG_SH) - ifrac0;
  835. SLOT = &ct->CH->SLOT[SLOT1];
  836. ct->vol_out1 = (SLOT->vol_ipol*ifrac1 + SLOT->vol_out*ifrac0) >> EG_SH;
  837. SLOT = &ct->CH->SLOT[SLOT2];
  838. ct->vol_out2 = (SLOT->vol_ipol*ifrac1 + SLOT->vol_out*ifrac0) >> EG_SH;
  839. SLOT = &ct->CH->SLOT[SLOT3];
  840. ct->vol_out3 = (SLOT->vol_ipol*ifrac1 + SLOT->vol_out*ifrac0) >> EG_SH;
  841. SLOT = &ct->CH->SLOT[SLOT4];
  842. ct->vol_out4 = (SLOT->vol_ipol*ifrac1 + SLOT->vol_out*ifrac0) >> EG_SH;
  843. #elif 1
  844. switch (ct->eg_timer >> EG_SH)
  845. {
  846. case 0:
  847. ct->vol_out1 = ct->CH->SLOT[SLOT1].vol_ipol;
  848. ct->vol_out2 = ct->CH->SLOT[SLOT2].vol_ipol;
  849. ct->vol_out3 = ct->CH->SLOT[SLOT3].vol_ipol;
  850. ct->vol_out4 = ct->CH->SLOT[SLOT4].vol_ipol;
  851. break;
  852. case (EG_TIMER_OVERFLOW>>EG_SH)-1:
  853. ct->vol_out1 = ct->CH->SLOT[SLOT1].vol_out;
  854. ct->vol_out2 = ct->CH->SLOT[SLOT2].vol_out;
  855. ct->vol_out3 = ct->CH->SLOT[SLOT3].vol_out;
  856. ct->vol_out4 = ct->CH->SLOT[SLOT4].vol_out;
  857. break;
  858. default:
  859. ct->vol_out1 = (ct->CH->SLOT[SLOT1].vol_ipol +
  860. ct->CH->SLOT[SLOT1].vol_out) >> 1;
  861. ct->vol_out2 = (ct->CH->SLOT[SLOT2].vol_ipol +
  862. ct->CH->SLOT[SLOT2].vol_out) >> 1;
  863. ct->vol_out3 = (ct->CH->SLOT[SLOT3].vol_ipol +
  864. ct->CH->SLOT[SLOT3].vol_out) >> 1;
  865. ct->vol_out4 = (ct->CH->SLOT[SLOT4].vol_ipol +
  866. ct->CH->SLOT[SLOT4].vol_out) >> 1;
  867. break;
  868. }
  869. #elif 0
  870. if (ct->eg_timer >> (EG_SH-1) < EG_TIMER_OVERFLOW >> EG_SH) {
  871. ct->vol_out1 = ct->CH->SLOT[SLOT1].vol_ipol;
  872. ct->vol_out2 = ct->CH->SLOT[SLOT2].vol_ipol;
  873. ct->vol_out3 = ct->CH->SLOT[SLOT3].vol_ipol;
  874. ct->vol_out4 = ct->CH->SLOT[SLOT4].vol_ipol;
  875. } else {
  876. ct->vol_out1 = ct->CH->SLOT[SLOT1].vol_out;
  877. ct->vol_out2 = ct->CH->SLOT[SLOT2].vol_out;
  878. ct->vol_out3 = ct->CH->SLOT[SLOT3].vol_out;
  879. ct->vol_out4 = ct->CH->SLOT[SLOT4].vol_out;
  880. }
  881. #else
  882. ct->vol_out1 = ct->CH->SLOT[SLOT1].vol_out;
  883. ct->vol_out2 = ct->CH->SLOT[SLOT2].vol_out;
  884. ct->vol_out3 = ct->CH->SLOT[SLOT3].vol_out;
  885. ct->vol_out4 = ct->CH->SLOT[SLOT4].vol_out;
  886. #endif
  887. if (ct->pack & 4) continue; /* output disabled */
  888. /* calculate channel sample */
  889. eg_out = ct->vol_out1;
  890. if ( (ct->pack & 8) && (ct->pack&(1<<(SLOT1+8))) ) eg_out += ct->pack >> (((ct->pack&0xc0)>>6)+24);
  891. if( eg_out < ENV_QUIET ) /* SLOT 1 */
  892. {
  893. int out = 0;
  894. if (ct->pack&0xf000) out = ((ct->op1_out>>16) + ((ct->op1_out<<16)>>16)) << ((ct->pack&0xf000)>>12); /* op1_out0 + op1_out1 */
  895. ct->op1_out <<= 16;
  896. ct->op1_out |= (unsigned short)op_calc1(ct->phase1, eg_out, out);
  897. } else {
  898. ct->op1_out <<= 16; /* op1_out0 = op1_out1; op1_out1 = 0; */
  899. }
  900. eg_out = ct->vol_out3; // volume_calc(&CH->SLOT[SLOT3]);
  901. eg_out2 = ct->vol_out2; // volume_calc(&CH->SLOT[SLOT2]);
  902. eg_out4 = ct->vol_out4; // volume_calc(&CH->SLOT[SLOT4]);
  903. if (ct->pack & 8) {
  904. unsigned int add = ct->pack >> (((ct->pack&0xc0)>>6)+24);
  905. if (ct->pack & (1<<(SLOT3+8))) eg_out += add;
  906. if (ct->pack & (1<<(SLOT2+8))) eg_out2 += add;
  907. if (ct->pack & (1<<(SLOT4+8))) eg_out4 += add;
  908. }
  909. switch( ct->algo&0x7 )
  910. {
  911. case 0:
  912. {
  913. /* M1---C1---MEM---M2---C2---OUT */
  914. int m2,c1,c2=0; /* Phase Modulation input for operators 2,3,4 */
  915. m2 = ct->mem;
  916. c1 = ct->op1_out>>16;
  917. if( eg_out < ENV_QUIET ) { /* SLOT 3 */
  918. c2 = op_calc(ct->phase3, eg_out, m2);
  919. }
  920. if( eg_out2 < ENV_QUIET ) { /* SLOT 2 */
  921. ct->mem = op_calc(ct->phase2, eg_out2, c1);
  922. }
  923. else ct->mem = 0;
  924. if( eg_out4 < ENV_QUIET ) { /* SLOT 4 */
  925. smp = op_calc(ct->phase4, eg_out4, c2);
  926. }
  927. break;
  928. }
  929. case 1:
  930. {
  931. /* M1------+-MEM---M2---C2---OUT */
  932. /* C1-+ */
  933. int m2,c2=0;
  934. m2 = ct->mem;
  935. ct->mem = ct->op1_out>>16;
  936. if( eg_out < ENV_QUIET ) { /* SLOT 3 */
  937. c2 = op_calc(ct->phase3, eg_out, m2);
  938. }
  939. if( eg_out2 < ENV_QUIET ) { /* SLOT 2 */
  940. ct->mem+= op_calc(ct->phase2, eg_out2, 0);
  941. }
  942. if( eg_out4 < ENV_QUIET ) { /* SLOT 4 */
  943. smp = op_calc(ct->phase4, eg_out4, c2);
  944. }
  945. break;
  946. }
  947. case 2:
  948. {
  949. /* M1-----------------+-C2---OUT */
  950. /* C1---MEM---M2-+ */
  951. int m2,c2;
  952. m2 = ct->mem;
  953. c2 = ct->op1_out>>16;
  954. if( eg_out < ENV_QUIET ) { /* SLOT 3 */
  955. c2 += op_calc(ct->phase3, eg_out, m2);
  956. }
  957. if( eg_out2 < ENV_QUIET ) { /* SLOT 2 */
  958. ct->mem = op_calc(ct->phase2, eg_out2, 0);
  959. }
  960. else ct->mem = 0;
  961. if( eg_out4 < ENV_QUIET ) { /* SLOT 4 */
  962. smp = op_calc(ct->phase4, eg_out4, c2);
  963. }
  964. break;
  965. }
  966. case 3:
  967. {
  968. /* M1---C1---MEM------+-C2---OUT */
  969. /* M2-+ */
  970. int c1,c2;
  971. c2 = ct->mem;
  972. c1 = ct->op1_out>>16;
  973. if( eg_out < ENV_QUIET ) { /* SLOT 3 */
  974. c2 += op_calc(ct->phase3, eg_out, 0);
  975. }
  976. if( eg_out2 < ENV_QUIET ) { /* SLOT 2 */
  977. ct->mem = op_calc(ct->phase2, eg_out2, c1);
  978. }
  979. else ct->mem = 0;
  980. if( eg_out4 < ENV_QUIET ) { /* SLOT 4 */
  981. smp = op_calc(ct->phase4, eg_out4, c2);
  982. }
  983. break;
  984. }
  985. case 4:
  986. {
  987. /* M1---C1-+-OUT */
  988. /* M2---C2-+ */
  989. /* MEM: not used */
  990. int c1,c2=0;
  991. c1 = ct->op1_out>>16;
  992. if( eg_out < ENV_QUIET ) { /* SLOT 3 */
  993. c2 = op_calc(ct->phase3, eg_out, 0);
  994. }
  995. if( eg_out2 < ENV_QUIET ) { /* SLOT 2 */
  996. smp = op_calc(ct->phase2, eg_out2, c1);
  997. }
  998. if( eg_out4 < ENV_QUIET ) { /* SLOT 4 */
  999. smp+= op_calc(ct->phase4, eg_out4, c2);
  1000. }
  1001. break;
  1002. }
  1003. case 5:
  1004. {
  1005. /* +----C1----+ */
  1006. /* M1-+-MEM---M2-+-OUT */
  1007. /* +----C2----+ */
  1008. int m2,c1,c2;
  1009. m2 = ct->mem;
  1010. ct->mem = c1 = c2 = ct->op1_out>>16;
  1011. if( eg_out < ENV_QUIET ) { /* SLOT 3 */
  1012. smp = op_calc(ct->phase3, eg_out, m2);
  1013. }
  1014. if( eg_out2 < ENV_QUIET ) { /* SLOT 2 */
  1015. smp+= op_calc(ct->phase2, eg_out2, c1);
  1016. }
  1017. if( eg_out4 < ENV_QUIET ) { /* SLOT 4 */
  1018. smp+= op_calc(ct->phase4, eg_out4, c2);
  1019. }
  1020. break;
  1021. }
  1022. case 6:
  1023. {
  1024. /* M1---C1-+ */
  1025. /* M2-+-OUT */
  1026. /* C2-+ */
  1027. /* MEM: not used */
  1028. int c1;
  1029. c1 = ct->op1_out>>16;
  1030. if( eg_out < ENV_QUIET ) { /* SLOT 3 */
  1031. smp = op_calc(ct->phase3, eg_out, 0);
  1032. }
  1033. if( eg_out2 < ENV_QUIET ) { /* SLOT 2 */
  1034. smp+= op_calc(ct->phase2, eg_out2, c1);
  1035. }
  1036. if( eg_out4 < ENV_QUIET ) { /* SLOT 4 */
  1037. smp+= op_calc(ct->phase4, eg_out4, 0);
  1038. }
  1039. break;
  1040. }
  1041. case 7:
  1042. {
  1043. /* M1-+ */
  1044. /* C1-+-OUT */
  1045. /* M2-+ */
  1046. /* C2-+ */
  1047. /* MEM: not used*/
  1048. smp = ct->op1_out>>16;
  1049. if( eg_out < ENV_QUIET ) { /* SLOT 3 */
  1050. smp += op_calc(ct->phase3, eg_out, 0);
  1051. }
  1052. if( eg_out2 < ENV_QUIET ) { /* SLOT 2 */
  1053. smp += op_calc(ct->phase2, eg_out2, 0);
  1054. }
  1055. if( eg_out4 < ENV_QUIET ) { /* SLOT 4 */
  1056. smp += op_calc(ct->phase4, eg_out4, 0);
  1057. }
  1058. break;
  1059. }
  1060. }
  1061. /* done calculating channel sample */
  1062. /* mix sample to output buffer */
  1063. if (smp) {
  1064. smp = clip(smp); /* saturate to 14 bit */
  1065. if (ct->algo & 0x80) {
  1066. smp &= ~0x1f; /* drop bits (DAC has 9 bits) */
  1067. smp -= (smp < 0 ? 7:0) << 5; /* discontinuity */
  1068. }
  1069. if (ct->pack & 1) { /* stereo */
  1070. if (ct->pack & 0x20) /* L */ /* TODO: check correctness */
  1071. buffer[scounter*2] += smp;
  1072. if (ct->pack & 0x10) /* R */
  1073. buffer[scounter*2+1] += smp;
  1074. } else {
  1075. buffer[scounter] += smp;
  1076. }
  1077. ct->algo |= 8;
  1078. }
  1079. /* update phase counters AFTER output calculations */
  1080. ct->phase1 += ct->incr1;
  1081. ct->phase2 += ct->incr2;
  1082. ct->phase3 += ct->incr3;
  1083. ct->phase4 += ct->incr4;
  1084. }
  1085. }
  1086. #else
  1087. void chan_render_loop(chan_rend_context *ct, int *buffer, unsigned short length);
  1088. #endif
  1089. static chan_rend_context crct;
  1090. static void chan_render_prep(void)
  1091. {
  1092. crct.eg_timer_add = ym2612.OPN.eg_timer_add;
  1093. crct.lfo_inc = ym2612.OPN.lfo_inc;
  1094. }
  1095. static void chan_render_finish(int *buffer, unsigned short length, int active_chans)
  1096. {
  1097. ym2612.OPN.eg_cnt = crct.eg_cnt;
  1098. ym2612.OPN.eg_timer = crct.eg_timer;
  1099. g_lfo_ampm = crct.pack >> 16; // need_save
  1100. ym2612.OPN.lfo_cnt = crct.lfo_cnt;
  1101. }
  1102. static UINT32 update_lfo_phase(FM_SLOT *SLOT, UINT32 block_fnum)
  1103. {
  1104. UINT32 fnum_lfo;
  1105. INT32 lfo_fn_table_index_offset;
  1106. UINT8 blk;
  1107. UINT32 fn;
  1108. int fc,fdt;
  1109. fnum_lfo = ((block_fnum & 0x7f0) >> 4) * 32 * 8;
  1110. lfo_fn_table_index_offset = lfo_pm_table[ fnum_lfo + crct.CH->pms + ((crct.pack>>16)&0xff) ];
  1111. if (lfo_fn_table_index_offset) /* LFO phase modulation active */
  1112. {
  1113. block_fnum = block_fnum*2 + lfo_fn_table_index_offset;
  1114. blk = (block_fnum&0x7000) >> 12;
  1115. fn = block_fnum & 0xfff;
  1116. /* phase increment counter */
  1117. fc = (fn_table[fn]>>(7-blk));
  1118. fdt = fc + SLOT->DT[crct.CH->kcode];
  1119. if (fdt < 0) fdt += fn_table[0x7ff*2] >> 2;
  1120. return (fdt * SLOT->mul) >> 1;
  1121. } else
  1122. return SLOT->Incr;
  1123. }
  1124. static int chan_render(int *buffer, int length, int c, UINT32 flags) // flags: stereo, ?, disabled, ?, pan_r, pan_l
  1125. {
  1126. crct.CH = &ym2612.CH[c];
  1127. crct.mem = crct.CH->mem_value; /* one sample delay memory */
  1128. crct.lfo_cnt = ym2612.OPN.lfo_cnt;
  1129. flags &= 0x37;
  1130. if (crct.lfo_inc) {
  1131. flags |= 8;
  1132. flags |= g_lfo_ampm << 16;
  1133. flags |= crct.CH->AMmasks << 8;
  1134. if (crct.CH->ams == 8) // no ams
  1135. flags &= ~0xf00;
  1136. else flags |= (crct.CH->ams&3)<<6;
  1137. }
  1138. flags |= (crct.CH->FB&0xf)<<12; /* feedback shift */
  1139. crct.pack = flags;
  1140. crct.eg_cnt = ym2612.OPN.eg_cnt; /* envelope generator counter */
  1141. crct.eg_timer = ym2612.OPN.eg_timer;
  1142. /* precalculate phase modulation incr */
  1143. crct.phase1 = crct.CH->SLOT[SLOT1].phase;
  1144. crct.phase2 = crct.CH->SLOT[SLOT2].phase;
  1145. crct.phase3 = crct.CH->SLOT[SLOT3].phase;
  1146. crct.phase4 = crct.CH->SLOT[SLOT4].phase;
  1147. crct.op1_out = crct.CH->op1_out;
  1148. crct.algo = crct.CH->ALGO & 7;
  1149. if (ym2612.OPN.ST.flags & ST_DAC)
  1150. crct.algo |= 0x80;
  1151. if(crct.CH->pms && (ym2612.OPN.ST.mode & 0xC0) && c == 2) {
  1152. /* 3 slot mode */
  1153. crct.incr1 = update_lfo_phase(&crct.CH->SLOT[SLOT1], ym2612.OPN.SL3.block_fnum[1]);
  1154. crct.incr2 = update_lfo_phase(&crct.CH->SLOT[SLOT2], ym2612.OPN.SL3.block_fnum[2]);
  1155. crct.incr3 = update_lfo_phase(&crct.CH->SLOT[SLOT3], ym2612.OPN.SL3.block_fnum[0]);
  1156. crct.incr4 = update_lfo_phase(&crct.CH->SLOT[SLOT4], crct.CH->block_fnum);
  1157. }
  1158. else if(crct.CH->pms)
  1159. {
  1160. crct.incr1 = update_lfo_phase(&crct.CH->SLOT[SLOT1], crct.CH->block_fnum);
  1161. crct.incr2 = update_lfo_phase(&crct.CH->SLOT[SLOT2], crct.CH->block_fnum);
  1162. crct.incr3 = update_lfo_phase(&crct.CH->SLOT[SLOT3], crct.CH->block_fnum);
  1163. crct.incr4 = update_lfo_phase(&crct.CH->SLOT[SLOT4], crct.CH->block_fnum);
  1164. }
  1165. else /* no LFO phase modulation */
  1166. {
  1167. crct.incr1 = crct.CH->SLOT[SLOT1].Incr;
  1168. crct.incr2 = crct.CH->SLOT[SLOT2].Incr;
  1169. crct.incr3 = crct.CH->SLOT[SLOT3].Incr;
  1170. crct.incr4 = crct.CH->SLOT[SLOT4].Incr;
  1171. }
  1172. chan_render_loop(&crct, buffer, length);
  1173. crct.CH->op1_out = crct.op1_out;
  1174. crct.CH->mem_value = crct.mem;
  1175. if (crct.CH->SLOT[SLOT1].state | crct.CH->SLOT[SLOT2].state | crct.CH->SLOT[SLOT3].state | crct.CH->SLOT[SLOT4].state)
  1176. {
  1177. crct.CH->SLOT[SLOT1].phase = crct.phase1;
  1178. crct.CH->SLOT[SLOT2].phase = crct.phase2;
  1179. crct.CH->SLOT[SLOT3].phase = crct.phase3;
  1180. crct.CH->SLOT[SLOT4].phase = crct.phase4;
  1181. }
  1182. else
  1183. ym2612.slot_mask &= ~(0xf << (c*4));
  1184. return (crct.algo & 8) >> 3; // had output
  1185. }
  1186. /* update phase increment and envelope generator */
  1187. INLINE void refresh_fc_eg_slot(FM_SLOT *SLOT, int fc, int kc)
  1188. {
  1189. int ksr, fdt;
  1190. /* (frequency) phase increment counter */
  1191. fdt = fc+SLOT->DT[kc];
  1192. /* detect overflow */
  1193. // if (fdt < 0) fdt += fn_table[0x7ff*2] >> (7-blk-1);
  1194. if (fdt < 0) fdt += fn_table[0x7ff*2] >> 2;
  1195. SLOT->Incr = fdt*SLOT->mul >> 1;
  1196. ksr = kc >> SLOT->KSR;
  1197. if( SLOT->ksr != ksr )
  1198. {
  1199. int eg_sh, eg_sel;
  1200. SLOT->ksr = ksr;
  1201. SLOT->ar_ksr = SLOT->ar + ksr;
  1202. /* calculate envelope generator rates */
  1203. if ((SLOT->ar_ksr) < 32+62)
  1204. {
  1205. eg_sh = eg_rate_shift [SLOT->ar_ksr];
  1206. eg_sel = eg_rate_select[SLOT->ar_ksr];
  1207. }
  1208. else
  1209. {
  1210. eg_sh = 0;
  1211. eg_sel = 18;
  1212. }
  1213. SLOT->eg_pack_ar = eg_inc_pack[eg_sel] | (eg_sh<<24);
  1214. eg_sh = eg_rate_shift [SLOT->d1r + ksr];
  1215. eg_sel = eg_rate_select[SLOT->d1r + ksr];
  1216. SLOT->eg_pack_d1r = eg_inc_pack[eg_sel] | (eg_sh<<24);
  1217. eg_sh = eg_rate_shift [SLOT->d2r + ksr];
  1218. eg_sel = eg_rate_select[SLOT->d2r + ksr];
  1219. SLOT->eg_pack_d2r = eg_inc_pack[eg_sel] | (eg_sh<<24);
  1220. eg_sh = eg_rate_shift [SLOT->rr + ksr];
  1221. eg_sel = eg_rate_select[SLOT->rr + ksr];
  1222. SLOT->eg_pack_rr = eg_inc_pack[eg_sel] | (eg_sh<<24);
  1223. }
  1224. }
  1225. /* update phase increment counters */
  1226. INLINE void refresh_fc_eg_chan(FM_CH *CH)
  1227. {
  1228. if( CH->SLOT[SLOT1].Incr==-1){
  1229. int fc = CH->fc;
  1230. int kc = CH->kcode;
  1231. refresh_fc_eg_slot(&CH->SLOT[SLOT1] , fc , kc );
  1232. refresh_fc_eg_slot(&CH->SLOT[SLOT2] , fc , kc );
  1233. refresh_fc_eg_slot(&CH->SLOT[SLOT3] , fc , kc );
  1234. refresh_fc_eg_slot(&CH->SLOT[SLOT4] , fc , kc );
  1235. }
  1236. }
  1237. INLINE void refresh_fc_eg_chan_sl3(void)
  1238. {
  1239. if( ym2612.CH[2].SLOT[SLOT1].Incr==-1)
  1240. {
  1241. refresh_fc_eg_slot(&ym2612.CH[2].SLOT[SLOT1], ym2612.OPN.SL3.fc[1], ym2612.OPN.SL3.kcode[1] );
  1242. refresh_fc_eg_slot(&ym2612.CH[2].SLOT[SLOT2], ym2612.OPN.SL3.fc[2], ym2612.OPN.SL3.kcode[2] );
  1243. refresh_fc_eg_slot(&ym2612.CH[2].SLOT[SLOT3], ym2612.OPN.SL3.fc[0], ym2612.OPN.SL3.kcode[0] );
  1244. refresh_fc_eg_slot(&ym2612.CH[2].SLOT[SLOT4], ym2612.CH[2].fc , ym2612.CH[2].kcode );
  1245. }
  1246. }
  1247. /* initialize time tables */
  1248. static void init_timetables(const UINT8 *dttable)
  1249. {
  1250. int i,d;
  1251. double rate;
  1252. /* DeTune table */
  1253. for (d = 0;d <= 3;d++){
  1254. for (i = 0;i <= 31;i++){
  1255. rate = ((double)dttable[d*32 + i]) * ym2612.OPN.ST.freqbase * (1<<(FREQ_SH-10));
  1256. ym2612.OPN.ST.dt_tab[d][i] = (INT32) rate;
  1257. ym2612.OPN.ST.dt_tab[d+4][i] = -ym2612.OPN.ST.dt_tab[d][i];
  1258. }
  1259. }
  1260. }
  1261. static void reset_channels(FM_CH *CH)
  1262. {
  1263. int c,s;
  1264. ym2612.OPN.ST.mode = 0; /* normal mode */
  1265. ym2612.OPN.ST.TA = 0;
  1266. ym2612.OPN.ST.TAC = 0;
  1267. ym2612.OPN.ST.TB = 0;
  1268. ym2612.OPN.ST.TBC = 0;
  1269. for( c = 0 ; c < 6 ; c++ )
  1270. {
  1271. CH[c].fc = 0;
  1272. for(s = 0 ; s < 4 ; s++ )
  1273. {
  1274. CH[c].SLOT[s].Incr = -1;
  1275. CH[c].SLOT[s].key = 0;
  1276. CH[c].SLOT[s].phase = 0;
  1277. CH[c].SLOT[s].ar = CH[c].SLOT[s].ksr = 0;
  1278. CH[c].SLOT[s].ar_ksr = 0;
  1279. CH[c].SLOT[s].ssg = CH[c].SLOT[s].ssgn = 0;
  1280. CH[c].SLOT[s].state= EG_OFF;
  1281. CH[c].SLOT[s].volume = MAX_ATT_INDEX;
  1282. CH[c].SLOT[s].vol_out = MAX_ATT_INDEX;
  1283. }
  1284. CH[c].mem_value = CH[c].op1_out = 0;
  1285. }
  1286. ym2612.slot_mask = 0;
  1287. ym2612.ssg_mask = 0;
  1288. }
  1289. /* initialize generic tables */
  1290. static void init_tables(void)
  1291. {
  1292. signed int i,x,y,p;
  1293. signed int n;
  1294. double o,m;
  1295. for (i=0; i < 256; i++)
  1296. {
  1297. /* non-standard sinus */
  1298. m = sin( ((i*2)+1) * M_PI / SIN_LEN ); /* checked against the real chip */
  1299. /* we never reach zero here due to ((i*2)+1) */
  1300. if (m>0.0)
  1301. o = 8*log(1.0/m)/log(2); /* convert to 'decibels' */
  1302. else
  1303. o = 8*log(-1.0/m)/log(2); /* convert to 'decibels' */
  1304. o = o / (ENV_STEP/4);
  1305. n = (int)(2.0*o);
  1306. if (n&1) /* round to nearest */
  1307. n = (n>>1)+1;
  1308. else
  1309. n = n>>1;
  1310. ym_sin_tab[ i ] = n;
  1311. //dprintf("FM.C: sin [%4i]= %4i", i, ym_sin_tab[i]);
  1312. }
  1313. //dprintf("FM.C: ENV_QUIET= %08x", ENV_QUIET );
  1314. for (x=0; x < TL_RES_LEN; x++)
  1315. {
  1316. m = (1<<16) / pow(2, (x+1) * (ENV_STEP/4.0) / 8.0);
  1317. m = floor(m);
  1318. /* we never reach (1<<16) here due to the (x+1) */
  1319. /* result fits within 16 bits at maximum */
  1320. n = (int)m; /* 16 bits here */
  1321. n >>= 4; /* 12 bits here */
  1322. if (n&1) /* round to nearest */
  1323. n = (n>>1)+1;
  1324. else
  1325. n = n>>1;
  1326. /* 11 bits here (rounded) */
  1327. n <<= 2; /* 13 bits here (as in real chip) */
  1328. ym_tl_tab2[ x ] = n;
  1329. for (i=1; i < 13; i++)
  1330. {
  1331. ym_tl_tab2[ x + i*TL_RES_LEN ] = n >> i;
  1332. }
  1333. }
  1334. for (x=0; x < 256; x++)
  1335. {
  1336. int sin = ym_sin_tab[ x ];
  1337. for (y=0; y < 2*13*TL_RES_LEN/8; y+=2)
  1338. {
  1339. p = (y<<2) + sin;
  1340. if (p >= 13*TL_RES_LEN)
  1341. ym_tl_tab[(y<<7) | x] = 0;
  1342. else ym_tl_tab[(y<<7) | x] = ym_tl_tab2[p];
  1343. }
  1344. }
  1345. /* build LFO PM modulation table */
  1346. for(i = 0; i < 8; i++) /* 8 PM depths */
  1347. {
  1348. UINT8 fnum;
  1349. for (fnum=0; fnum<128; fnum++) /* 7 bits meaningful of F-NUMBER */
  1350. {
  1351. UINT8 value;
  1352. UINT8 step;
  1353. UINT32 offset_depth = i;
  1354. UINT32 offset_fnum_bit;
  1355. UINT32 bit_tmp;
  1356. for (step=0; step<8; step++)
  1357. {
  1358. value = 0;
  1359. for (bit_tmp=0; bit_tmp<7; bit_tmp++) /* 7 bits */
  1360. {
  1361. if (fnum & (1<<bit_tmp)) /* only if bit "bit_tmp" is set */
  1362. {
  1363. offset_fnum_bit = bit_tmp * 8;
  1364. value += lfo_pm_output[offset_fnum_bit + offset_depth][step];
  1365. }
  1366. }
  1367. lfo_pm_table[(fnum*32*8) + (i*32) + step + 0] = value;
  1368. lfo_pm_table[(fnum*32*8) + (i*32) +(step^7)+ 8] = value;
  1369. lfo_pm_table[(fnum*32*8) + (i*32) + step +16] = -value;
  1370. lfo_pm_table[(fnum*32*8) + (i*32) +(step^7)+24] = -value;
  1371. }
  1372. }
  1373. }
  1374. }
  1375. /* CSM Key Controll */
  1376. #if 0
  1377. INLINE void CSMKeyControll(FM_CH *CH)
  1378. {
  1379. /* this is wrong, atm */
  1380. /* all key on */
  1381. FM_KEYON(CH,SLOT1);
  1382. FM_KEYON(CH,SLOT2);
  1383. FM_KEYON(CH,SLOT3);
  1384. FM_KEYON(CH,SLOT4);
  1385. }
  1386. #endif
  1387. /* prescaler set (and make time tables) */
  1388. static void OPNSetPres(int pres)
  1389. {
  1390. int i;
  1391. /* frequency base */
  1392. ym2612.OPN.ST.freqbase = (ym2612.OPN.ST.rate) ? ((double)ym2612.OPN.ST.clock / ym2612.OPN.ST.rate) / pres : 0;
  1393. ym2612.OPN.eg_timer_add = (1<<EG_SH) * ym2612.OPN.ST.freqbase;
  1394. /* make time tables */
  1395. init_timetables( dt_tab );
  1396. /* there are 2048 FNUMs that can be generated using FNUM/BLK registers
  1397. but LFO works with one more bit of a precision so we really need 4096 elements */
  1398. /* calculate fnumber -> increment counter table */
  1399. for(i = 0; i < 4096; i++)
  1400. {
  1401. /* freq table for octave 7 */
  1402. /* OPN phase increment counter = 20bit */
  1403. fn_table[i] = (UINT32)( (double)i * 32 * ym2612.OPN.ST.freqbase * (1<<(FREQ_SH-10)) ); /* -10 because chip works with 10.10 fixed point, while we use 16.16 */
  1404. }
  1405. /* LFO freq. table */
  1406. for(i = 0; i < 8; i++)
  1407. {
  1408. /* Amplitude modulation: 64 output levels (triangle waveform); 1 level lasts for one of "lfo_samples_per_step" samples */
  1409. /* Phase modulation: one entry from lfo_pm_output lasts for one of 4 * "lfo_samples_per_step" samples */
  1410. ym2612.OPN.lfo_freq[i] = (1.0 / lfo_samples_per_step[i]) * (1<<LFO_SH) * ym2612.OPN.ST.freqbase;
  1411. }
  1412. }
  1413. /* write a OPN register (0x30-0xff) */
  1414. static int OPNWriteReg(int r, int v)
  1415. {
  1416. int ret = 1;
  1417. FM_CH *CH;
  1418. FM_SLOT *SLOT;
  1419. UINT8 c = OPN_CHAN(r);
  1420. if (c == 3) return 0; /* 0xX3,0xX7,0xXB,0xXF */
  1421. if (r >= 0x100) c+=3;
  1422. CH = &ym2612.CH[c];
  1423. SLOT = &(CH->SLOT[OPN_SLOT(r)]);
  1424. switch( r & 0xf0 ) {
  1425. case 0x30: /* DET , MUL */
  1426. set_det_mul(CH,SLOT,v);
  1427. break;
  1428. case 0x40: /* TL */
  1429. set_tl(SLOT,v);
  1430. break;
  1431. case 0x50: /* KS, AR */
  1432. set_ar_ksr(CH,SLOT,v);
  1433. break;
  1434. case 0x60: /* bit7 = AM ENABLE, DR | depends on ksr */
  1435. set_dr(SLOT,v);
  1436. if(v&0x80) CH->AMmasks |= 1<<OPN_SLOT(r);
  1437. else CH->AMmasks &= ~(1<<OPN_SLOT(r));
  1438. break;
  1439. case 0x70: /* SR | depends on ksr */
  1440. set_sr(SLOT,v);
  1441. break;
  1442. case 0x80: /* SL, RR | depends on ksr */
  1443. set_sl_rr(SLOT,v);
  1444. break;
  1445. case 0x90: /* SSG-EG */
  1446. SLOT->ssg = v&0x0f;
  1447. SLOT->ssg ^= SLOT->ssgn;
  1448. if (v&0x08) ym2612.ssg_mask |= 1<<(OPN_SLOT(r) + c*4);
  1449. else ym2612.ssg_mask &= ~(1<<(OPN_SLOT(r) + c*4));
  1450. // if (SLOT->state > EG_REL)
  1451. // recalc_volout(SLOT);
  1452. break;
  1453. case 0xa0:
  1454. switch( OPN_SLOT(r) ){
  1455. case 0: /* 0xa0-0xa2 : FNUM1 | depends on fn_h (below) */
  1456. {
  1457. UINT32 fn = (((UINT32)( (CH->fn_h)&7))<<8) + v;
  1458. UINT8 blk = CH->fn_h>>3;
  1459. /* keyscale code */
  1460. CH->kcode = (blk<<2) | opn_fktable[fn >> 7];
  1461. /* phase increment counter */
  1462. CH->fc = fn_table[fn*2]>>(7-blk);
  1463. /* store fnum in clear form for LFO PM calculations */
  1464. CH->block_fnum = (blk<<11) | fn;
  1465. CH->SLOT[SLOT1].Incr=-1;
  1466. }
  1467. break;
  1468. case 1: /* 0xa4-0xa6 : FNUM2,BLK */
  1469. CH->fn_h = v&0x3f;
  1470. ret = 0;
  1471. break;
  1472. case 2: /* 0xa8-0xaa : 3CH FNUM1 */
  1473. if(r < 0x100)
  1474. {
  1475. UINT32 fn = (((UINT32)(ym2612.OPN.SL3.fn_h&7))<<8) + v;
  1476. UINT8 blk = ym2612.OPN.SL3.fn_h>>3;
  1477. /* keyscale code */
  1478. ym2612.OPN.SL3.kcode[c]= (blk<<2) | opn_fktable[fn >> 7];
  1479. /* phase increment counter */
  1480. ym2612.OPN.SL3.fc[c] = fn_table[fn*2]>>(7-blk);
  1481. ym2612.OPN.SL3.block_fnum[c] = (blk<<11) | fn;
  1482. ym2612.CH[2].SLOT[SLOT1].Incr=-1;
  1483. }
  1484. break;
  1485. case 3: /* 0xac-0xae : 3CH FNUM2,BLK */
  1486. if(r < 0x100)
  1487. ym2612.OPN.SL3.fn_h = v&0x3f;
  1488. ret = 0;
  1489. break;
  1490. default:
  1491. ret = 0;
  1492. break;
  1493. }
  1494. break;
  1495. case 0xb0:
  1496. switch( OPN_SLOT(r) ){
  1497. case 0: /* 0xb0-0xb2 : FB,ALGO */
  1498. {
  1499. int feedback = (v>>3)&7;
  1500. CH->ALGO = v&7;
  1501. CH->FB = feedback ? feedback+6 : 0;
  1502. }
  1503. break;
  1504. case 1: /* 0xb4-0xb6 : L , R , AMS , PMS (YM2612/YM2610B/YM2610/YM2608) */
  1505. {
  1506. int panshift = c<<1;
  1507. /* b0-2 PMS */
  1508. CH->pms = (v & 7) * 32; /* CH->pms = PM depth * 32 (index in lfo_pm_table) */
  1509. /* b4-5 AMS */
  1510. CH->ams = lfo_ams_depth_shift[(v>>4) & 3];
  1511. /* PAN : b7 = L, b6 = R */
  1512. ym2612.OPN.pan &= ~(3<<panshift);
  1513. ym2612.OPN.pan |= ((v & 0xc0) >> 6) << panshift; // ..LRLR
  1514. }
  1515. break;
  1516. default:
  1517. ret = 0;
  1518. break;
  1519. }
  1520. break;
  1521. default:
  1522. ret = 0;
  1523. break;
  1524. }
  1525. return ret;
  1526. }
  1527. /*******************************************************************************/
  1528. /* YM2612 local section */
  1529. /*******************************************************************************/
  1530. /* Generate samples for YM2612 */
  1531. int YM2612UpdateOne_(int *buffer, int length, int stereo, int is_buf_empty)
  1532. {
  1533. int pan;
  1534. int active_chs = 0;
  1535. int flags = stereo ? 1:0;
  1536. // if !is_buf_empty, it means it has valid samples to mix with, else it may contain trash
  1537. if (is_buf_empty) memset32(buffer, 0, length<<stereo);
  1538. /*
  1539. {
  1540. int c, s;
  1541. ppp();
  1542. for (c = 0; c < 6; c++) {
  1543. int slr = 0, slm;
  1544. printf("%i: ", c);
  1545. for (s = 0; s < 4; s++) {
  1546. if (ym2612.CH[c].SLOT[s].state != EG_OFF) slr = 1;
  1547. printf(" %i", ym2612.CH[c].SLOT[s].state != EG_OFF);
  1548. }
  1549. slm = (ym2612.slot_mask&(0xf<<(c*4))) ? 1 : 0;
  1550. printf(" | %i", slm);
  1551. printf(" | %i\n", ym2612.CH[c].SLOT[SLOT1].Incr==-1);
  1552. if (slr != slm) exit(1);
  1553. }
  1554. }
  1555. */
  1556. /* refresh PG and EG */
  1557. refresh_fc_eg_chan( &ym2612.CH[0] );
  1558. refresh_fc_eg_chan( &ym2612.CH[1] );
  1559. if( (ym2612.OPN.ST.mode & 0xc0) )
  1560. /* 3SLOT MODE */
  1561. refresh_fc_eg_chan_sl3();
  1562. else
  1563. refresh_fc_eg_chan( &ym2612.CH[2] );
  1564. refresh_fc_eg_chan( &ym2612.CH[3] );
  1565. refresh_fc_eg_chan( &ym2612.CH[4] );
  1566. refresh_fc_eg_chan( &ym2612.CH[5] );
  1567. pan = ym2612.OPN.pan;
  1568. /* mix to 32bit dest */
  1569. // flags: stereo, ssg_enabled, disabled, _, pan_r, pan_l
  1570. chan_render_prep();
  1571. #define BIT_IF(v,b,c) { v &= ~(1<<(b)); if (c) v |= 1<<(b); }
  1572. BIT_IF(flags, 1, (ym2612.ssg_mask & 0x00000f) && (ym2612.OPN.ST.flags & 1));
  1573. if (ym2612.slot_mask & 0x00000f) active_chs |= chan_render(buffer, length, 0, flags|((pan&0x003)<<4)) << 0;
  1574. BIT_IF(flags, 1, (ym2612.ssg_mask & 0x0000f0) && (ym2612.OPN.ST.flags & 1));
  1575. if (ym2612.slot_mask & 0x0000f0) active_chs |= chan_render(buffer, length, 1, flags|((pan&0x00c)<<2)) << 1;
  1576. BIT_IF(flags, 1, (ym2612.ssg_mask & 0x000f00) && (ym2612.OPN.ST.flags & 1));
  1577. if (ym2612.slot_mask & 0x000f00) active_chs |= chan_render(buffer, length, 2, flags|((pan&0x030) )) << 2;
  1578. BIT_IF(flags, 1, (ym2612.ssg_mask & 0x00f000) && (ym2612.OPN.ST.flags & 1));
  1579. if (ym2612.slot_mask & 0x00f000) active_chs |= chan_render(buffer, length, 3, flags|((pan&0x0c0)>>2)) << 3;
  1580. BIT_IF(flags, 1, (ym2612.ssg_mask & 0x0f0000) && (ym2612.OPN.ST.flags & 1));
  1581. if (ym2612.slot_mask & 0x0f0000) active_chs |= chan_render(buffer, length, 4, flags|((pan&0x300)>>4)) << 4;
  1582. BIT_IF(flags, 1, (ym2612.ssg_mask & 0xf00000) && (ym2612.OPN.ST.flags & 1));
  1583. if (ym2612.slot_mask & 0xf00000) active_chs |= chan_render(buffer, length, 5, flags|((pan&0xc00)>>6)|(!!ym2612.dacen<<2)) << 5;
  1584. #undef BIT_IF
  1585. chan_render_finish(buffer, length, active_chs);
  1586. return active_chs; // 1 if buffer updated
  1587. }
  1588. /* initialize YM2612 emulator */
  1589. void YM2612Init_(int clock, int rate, int flags)
  1590. {
  1591. memset(&ym2612, 0, sizeof(ym2612));
  1592. init_tables();
  1593. ym2612.OPN.ST.clock = clock;
  1594. ym2612.OPN.ST.rate = rate;
  1595. ym2612.OPN.ST.flags = flags;
  1596. OPNSetPres( 6*24 );
  1597. /* Extend handler */
  1598. YM2612ResetChip_();
  1599. }
  1600. /* reset */
  1601. void YM2612ResetChip_(void)
  1602. {
  1603. int i;
  1604. memset(ym2612.REGS, 0, sizeof(ym2612.REGS));
  1605. set_timers( 0x30 ); /* mode 0 , timer reset */
  1606. ym2612.REGS[0x27] = 0x30;
  1607. ym2612.OPN.eg_timer = 0;
  1608. ym2612.OPN.eg_cnt = 0;
  1609. ym2612.OPN.lfo_inc = 0;
  1610. ym2612.OPN.lfo_cnt = 0;
  1611. g_lfo_ampm = 126 << 8;
  1612. ym2612.OPN.ST.status = 0;
  1613. reset_channels( &ym2612.CH[0] );
  1614. for(i = 0xb6 ; i >= 0xb4 ; i-- )
  1615. {
  1616. OPNWriteReg(i ,0xc0);
  1617. OPNWriteReg(i|0x100,0xc0);
  1618. ym2612.REGS[i ] = 0xc0;
  1619. ym2612.REGS[i|0x100] = 0xc0;
  1620. }
  1621. for(i = 0xb2 ; i >= 0x30 ; i-- )
  1622. {
  1623. OPNWriteReg(i ,0);
  1624. OPNWriteReg(i|0x100,0);
  1625. }
  1626. for(i = 0x26 ; i >= 0x20 ; i-- ) OPNWriteReg(i,0);
  1627. /* DAC mode clear */
  1628. ym2612.dacen = 0;
  1629. ym2612.dacout = 0;
  1630. ym2612.addr_A1 = 0;
  1631. }
  1632. /* YM2612 write */
  1633. /* a = address */
  1634. /* v = value */
  1635. /* returns 1 if sample affecting state changed */
  1636. int YM2612Write_(unsigned int a, unsigned int v)
  1637. {
  1638. int addr, ret=1;
  1639. v &= 0xff; /* adjust to 8 bit bus */
  1640. switch( a & 3 ){
  1641. case 0: /* address port 0 */
  1642. case 2: /* address port 1 */
  1643. ym2612.OPN.ST.address = v;
  1644. ym2612.addr_A1 = (a & 2) >> 1;
  1645. ret = 0;
  1646. break;
  1647. case 1:
  1648. case 3: /* data port */
  1649. addr = ym2612.OPN.ST.address | ((int)ym2612.addr_A1 << 8);
  1650. switch( addr & 0x1f0 )
  1651. {
  1652. case 0x20: /* 0x20-0x2f Mode */
  1653. switch( addr )
  1654. {
  1655. case 0x22: /* LFO FREQ (YM2608/YM2610/YM2610B/YM2612) */
  1656. if (v&0x08) /* LFO enabled ? */
  1657. {
  1658. ym2612.OPN.lfo_inc = ym2612.OPN.lfo_freq[v&7];
  1659. }
  1660. else
  1661. {
  1662. ym2612.OPN.lfo_inc = 0;
  1663. ym2612.OPN.lfo_cnt = 0;
  1664. g_lfo_ampm = 126 << 8;
  1665. }
  1666. break;
  1667. #if 0 // handled elsewhere
  1668. case 0x24: { // timer A High 8
  1669. int TAnew = (ym2612.OPN.ST.TA & 0x03)|(((int)v)<<2);
  1670. if(ym2612.OPN.ST.TA != TAnew) {
  1671. // we should reset ticker only if new value is written. Outrun requires this.
  1672. ym2612.OPN.ST.TA = TAnew;
  1673. ym2612.OPN.ST.TAC = (1024-TAnew)*18;
  1674. ym2612.OPN.ST.TAT = 0;
  1675. }
  1676. }
  1677. ret=0;
  1678. break;
  1679. case 0x25: { // timer A Low 2
  1680. int TAnew = (ym2612.OPN.ST.TA & 0x3fc)|(v&3);
  1681. if(ym2612.OPN.ST.TA != TAnew) {
  1682. ym2612.OPN.ST.TA = TAnew;
  1683. ym2612.OPN.ST.TAC = (1024-TAnew)*18;
  1684. ym2612.OPN.ST.TAT = 0;
  1685. }
  1686. }
  1687. ret=0;
  1688. break;
  1689. case 0x26: // timer B
  1690. if(ym2612.OPN.ST.TB != v) {
  1691. ym2612.OPN.ST.TB = v;
  1692. ym2612.OPN.ST.TBC = (256-v)<<4;
  1693. ym2612.OPN.ST.TBC *= 18;
  1694. ym2612.OPN.ST.TBT = 0;
  1695. }
  1696. ret=0;
  1697. break;
  1698. #endif
  1699. case 0x27: /* mode, timer control */
  1700. set_timers( v );
  1701. ret=0;
  1702. break;
  1703. case 0x28: /* key on / off */
  1704. {
  1705. UINT8 c;
  1706. c = v & 0x03;
  1707. if( c == 3 ) { ret=0; break; }
  1708. if( v&0x04 ) c+=3;
  1709. if(v&0x10) FM_KEYON(c,SLOT1); else FM_KEYOFF(c,SLOT1);
  1710. if(v&0x20) FM_KEYON(c,SLOT2); else FM_KEYOFF(c,SLOT2);
  1711. if(v&0x40) FM_KEYON(c,SLOT3); else FM_KEYOFF(c,SLOT3);
  1712. if(v&0x80) FM_KEYON(c,SLOT4); else FM_KEYOFF(c,SLOT4);
  1713. break;
  1714. }
  1715. case 0x2a: /* DAC data (YM2612) */
  1716. ym2612.dacout = ((int)v - 0x80) << 6; /* level unknown (notaz: 8 seems to be too much) */
  1717. ret=0;
  1718. break;
  1719. case 0x2b: /* DAC Sel (YM2612) */
  1720. /* b7 = dac enable */
  1721. ym2612.dacen = v & 0x80;
  1722. ret=0;
  1723. break;
  1724. default:
  1725. break;
  1726. }
  1727. break;
  1728. default: /* 0x30-0xff OPN section */
  1729. /* write register */
  1730. ret = OPNWriteReg(addr,v);
  1731. }
  1732. break;
  1733. }
  1734. return ret;
  1735. }
  1736. #if 0
  1737. UINT8 YM2612Read_(void)
  1738. {
  1739. return ym2612.OPN.ST.status;
  1740. }
  1741. int YM2612PicoTick_(int n)
  1742. {
  1743. int ret = 0;
  1744. // timer A
  1745. if(ym2612.OPN.ST.mode & 0x01 && (ym2612.OPN.ST.TAT+=64*n) >= ym2612.OPN.ST.TAC) {
  1746. ym2612.OPN.ST.TAT -= ym2612.OPN.ST.TAC;
  1747. if(ym2612.OPN.ST.mode & 0x04) ym2612.OPN.ST.status |= 1;
  1748. // CSM mode total level latch and auto key on
  1749. if(ym2612.OPN.ST.mode & 0x80) {
  1750. CSMKeyControll( &(ym2612.CH[2]) ); // Vectorman2, etc.
  1751. ret = 1;
  1752. }
  1753. }
  1754. // timer B
  1755. if(ym2612.OPN.ST.mode & 0x02 && (ym2612.OPN.ST.TBT+=64*n) >= ym2612.OPN.ST.TBC) {
  1756. ym2612.OPN.ST.TBT -= ym2612.OPN.ST.TBC;
  1757. if(ym2612.OPN.ST.mode & 0x08) ym2612.OPN.ST.status |= 2;
  1758. }
  1759. return ret;
  1760. }
  1761. #endif
  1762. void YM2612PicoStateLoad_(void)
  1763. {
  1764. reset_channels( &ym2612.CH[0] );
  1765. ym2612.slot_mask = 0xffffff;
  1766. }
  1767. /* rather stupid design because I wanted to fit in unused register "space" */
  1768. typedef struct
  1769. {
  1770. UINT32 state_phase;
  1771. INT16 volume;
  1772. } ym_save_addon_slot;
  1773. typedef struct
  1774. {
  1775. UINT32 magic;
  1776. UINT8 address;
  1777. UINT8 status;
  1778. UINT8 addr_A1;
  1779. UINT8 unused;
  1780. int TAT;
  1781. int TBT;
  1782. UINT32 eg_cnt; // 10
  1783. UINT32 eg_timer;
  1784. UINT32 lfo_cnt;
  1785. UINT16 lfo_ampm;
  1786. UINT16 unused2;
  1787. UINT32 keyon_field; // 20
  1788. UINT32 kcode_fc_sl3_3;
  1789. UINT32 reserved[2];
  1790. } ym_save_addon;
  1791. typedef struct
  1792. {
  1793. UINT16 block_fnum[6];
  1794. UINT16 block_fnum_sl3[3];
  1795. UINT16 reserved[7];
  1796. } ym_save_addon2;
  1797. void YM2612PicoStateSave2(int tat, int tbt)
  1798. {
  1799. ym_save_addon_slot ss;
  1800. ym_save_addon2 sa2;
  1801. ym_save_addon sa;
  1802. unsigned char *ptr;
  1803. int c, s;
  1804. memset(&sa, 0, sizeof(sa));
  1805. memset(&sa2, 0, sizeof(sa2));
  1806. // chans 1,2,3
  1807. ptr = &ym2612.REGS[0x0b8];
  1808. for (c = 0; c < 3; c++)
  1809. {
  1810. for (s = 0; s < 4; s++) {
  1811. ss.state_phase = (ym2612.CH[c].SLOT[s].state << 29) | (ym2612.CH[c].SLOT[s].phase >> 3);
  1812. ss.volume = ym2612.CH[c].SLOT[s].volume;
  1813. if (ym2612.CH[c].SLOT[s].key)
  1814. sa.keyon_field |= 1 << (c*4 + s);
  1815. memcpy(ptr, &ss, 6);
  1816. ptr += 6;
  1817. }
  1818. sa2.block_fnum[c] = ym2612.CH[c].block_fnum;
  1819. }
  1820. // chans 4,5,6
  1821. ptr = &ym2612.REGS[0x1b8];
  1822. for (; c < 6; c++)
  1823. {
  1824. for (s = 0; s < 4; s++) {
  1825. ss.state_phase = (ym2612.CH[c].SLOT[s].state << 29) | (ym2612.CH[c].SLOT[s].phase >> 3);
  1826. ss.volume = ym2612.CH[c].SLOT[s].volume;
  1827. if (ym2612.CH[c].SLOT[s].key)
  1828. sa.keyon_field |= 1 << (c*4 + s);
  1829. memcpy(ptr, &ss, 6);
  1830. ptr += 6;
  1831. }
  1832. sa2.block_fnum[c] = ym2612.CH[c].block_fnum;
  1833. }
  1834. for (c = 0; c < 3; c++)
  1835. {
  1836. sa2.block_fnum_sl3[c] = ym2612.OPN.SL3.block_fnum[c];
  1837. }
  1838. memcpy(&ym2612.REGS[0], &sa2, sizeof(sa2)); // 0x20 max
  1839. // other things
  1840. ptr = &ym2612.REGS[0x100];
  1841. sa.magic = 0x41534d59; // 'YMSA'
  1842. sa.address = ym2612.OPN.ST.address;
  1843. sa.status = ym2612.OPN.ST.status;
  1844. sa.addr_A1 = ym2612.addr_A1;
  1845. sa.TAT = tat;
  1846. sa.TBT = tbt;
  1847. sa.eg_cnt = ym2612.OPN.eg_cnt;
  1848. sa.eg_timer = ym2612.OPN.eg_timer;
  1849. sa.lfo_cnt = ym2612.OPN.lfo_cnt;
  1850. sa.lfo_ampm = g_lfo_ampm;
  1851. memcpy(ptr, &sa, sizeof(sa)); // 0x30 max
  1852. }
  1853. int YM2612PicoStateLoad2(int *tat, int *tbt)
  1854. {
  1855. ym_save_addon_slot ss;
  1856. ym_save_addon2 sa2;
  1857. ym_save_addon sa;
  1858. unsigned char *ptr;
  1859. UINT32 fn;
  1860. UINT8 blk;
  1861. int c, s;
  1862. ptr = &ym2612.REGS[0x100];
  1863. memcpy(&sa, ptr, sizeof(sa)); // 0x30 max
  1864. if (sa.magic != 0x41534d59) return -1;
  1865. ptr = &ym2612.REGS[0];
  1866. memcpy(&sa2, ptr, sizeof(sa2));
  1867. ym2612.OPN.ST.address = sa.address;
  1868. ym2612.OPN.ST.status = sa.status;
  1869. ym2612.addr_A1 = sa.addr_A1;
  1870. ym2612.OPN.eg_cnt = sa.eg_cnt;
  1871. ym2612.OPN.eg_timer = sa.eg_timer;
  1872. ym2612.OPN.lfo_cnt = sa.lfo_cnt;
  1873. g_lfo_ampm = sa.lfo_ampm;
  1874. if (tat != NULL) *tat = sa.TAT;
  1875. if (tbt != NULL) *tbt = sa.TBT;
  1876. // chans 1,2,3
  1877. ptr = &ym2612.REGS[0x0b8];
  1878. for (c = 0; c < 3; c++)
  1879. {
  1880. for (s = 0; s < 4; s++) {
  1881. memcpy(&ss, ptr, 6);
  1882. ym2612.CH[c].SLOT[s].state = ss.state_phase >> 29;
  1883. ym2612.CH[c].SLOT[s].phase = ss.state_phase << 3;
  1884. ym2612.CH[c].SLOT[s].volume = ss.volume;
  1885. ym2612.CH[c].SLOT[s].key = (sa.keyon_field & (1 << (c*4 + s))) ? 1 : 0;
  1886. ym2612.CH[c].SLOT[s].ksr = (UINT8)-1;
  1887. ptr += 6;
  1888. }
  1889. ym2612.CH[c].SLOT[SLOT1].Incr=-1;
  1890. ym2612.CH[c].block_fnum = sa2.block_fnum[c];
  1891. fn = ym2612.CH[c].block_fnum & 0x7ff;
  1892. blk = ym2612.CH[c].block_fnum >> 11;
  1893. ym2612.CH[c].kcode= (blk<<2) | opn_fktable[fn >> 7];
  1894. ym2612.CH[c].fc = fn_table[fn*2]>>(7-blk);
  1895. refresh_fc_eg_chan( &ym2612.CH[c] );
  1896. }
  1897. // chans 4,5,6
  1898. ptr = &ym2612.REGS[0x1b8];
  1899. for (; c < 6; c++)
  1900. {
  1901. for (s = 0; s < 4; s++) {
  1902. memcpy(&ss, ptr, 6);
  1903. ym2612.CH[c].SLOT[s].state = ss.state_phase >> 29;
  1904. ym2612.CH[c].SLOT[s].phase = ss.state_phase << 3;
  1905. ym2612.CH[c].SLOT[s].volume = ss.volume;
  1906. ym2612.CH[c].SLOT[s].key = (sa.keyon_field & (1 << (c*4 + s))) ? 1 : 0;
  1907. ym2612.CH[c].SLOT[s].ksr = (UINT8)-1;
  1908. ptr += 6;
  1909. }
  1910. ym2612.CH[c].SLOT[SLOT1].Incr=-1;
  1911. ym2612.CH[c].block_fnum = sa2.block_fnum[c];
  1912. fn = ym2612.CH[c].block_fnum & 0x7ff;
  1913. blk = ym2612.CH[c].block_fnum >> 11;
  1914. ym2612.CH[c].kcode= (blk<<2) | opn_fktable[fn >> 7];
  1915. ym2612.CH[c].fc = fn_table[fn*2]>>(7-blk);
  1916. refresh_fc_eg_chan( &ym2612.CH[c] );
  1917. }
  1918. for (c = 0; c < 3; c++)
  1919. {
  1920. ym2612.OPN.SL3.block_fnum[c] = sa2.block_fnum_sl3[c];
  1921. fn = ym2612.OPN.SL3.block_fnum[c] & 0x7ff;
  1922. blk = ym2612.OPN.SL3.block_fnum[c] >> 11;
  1923. ym2612.OPN.SL3.kcode[c]= (blk<<2) | opn_fktable[fn >> 7];
  1924. ym2612.OPN.SL3.fc[c] = fn_table[fn*2]>>(7-blk);
  1925. }
  1926. return 0;
  1927. }
  1928. void *YM2612GetRegs(void)
  1929. {
  1930. return ym2612.REGS;
  1931. }