PageRenderTime 67ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/src/pico/ym2612.cpp

https://bitbucket.org/elemental/emumaster
C++ | 1795 lines | 1206 code | 273 blank | 316 comment | 160 complexity | 278e69c300a3d921330b9280a08eac93 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  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. /*
  10. **
  11. ** File: fm.c -- software implementation of Yamaha FM sound generator
  12. **
  13. ** Copyright (C) 2001, 2002, 2003 Jarek Burczynski (bujar at mame dot net)
  14. ** Copyright (C) 1998 Tatsuyuki Satoh , MultiArcadeMachineEmulator development
  15. **
  16. ** Version 1.4 (final beta)
  17. **
  18. */
  19. /*
  20. ** History:
  21. **
  22. ** 03-08-2003 Jarek Burczynski:
  23. ** - fixed YM2608 initial values (after the reset)
  24. ** - fixed flag and irqmask handling (YM2608)
  25. ** - fixed BUFRDY flag handling (YM2608)
  26. **
  27. ** 14-06-2003 Jarek Burczynski:
  28. ** - implemented all of the YM2608 status register flags
  29. ** - implemented support for external memory read/write via YM2608
  30. ** - implemented support for deltat memory limit register in YM2608 emulation
  31. **
  32. ** 22-05-2003 Jarek Burczynski:
  33. ** - fixed LFO PM calculations (copy&paste bugfix)
  34. **
  35. ** 08-05-2003 Jarek Burczynski:
  36. ** - fixed SSG support
  37. **
  38. ** 22-04-2003 Jarek Burczynski:
  39. ** - implemented 100% correct LFO generator (verified on real YM2610 and YM2608)
  40. **
  41. ** 15-04-2003 Jarek Burczynski:
  42. ** - added support for YM2608's register 0x110 - status mask
  43. **
  44. ** 01-12-2002 Jarek Burczynski:
  45. ** - fixed register addressing in YM2608, YM2610, YM2610B chips. (verified on real YM2608)
  46. ** The addressing patch used for early Neo-Geo games can be removed now.
  47. **
  48. ** 26-11-2002 Jarek Burczynski, Nicola Salmoria:
  49. ** - recreated YM2608 ADPCM ROM using data from real YM2608's output which leads to:
  50. ** - added emulation of YM2608 drums.
  51. ** - output of YM2608 is two times lower now - same as YM2610 (verified on real YM2608)
  52. **
  53. ** 16-08-2002 Jarek Burczynski:
  54. ** - binary exact Envelope Generator (verified on real YM2203);
  55. ** identical to YM2151
  56. ** - corrected 'off by one' error in feedback calculations (when feedback is off)
  57. ** - corrected connection (algorithm) calculation (verified on real YM2203 and YM2610)
  58. **
  59. ** 18-12-2001 Jarek Burczynski:
  60. ** - added SSG-EG support (verified on real YM2203)
  61. **
  62. ** 12-08-2001 Jarek Burczynski:
  63. ** - corrected ym_sin_tab and ym_tl_tab data (verified on real chip)
  64. ** - corrected feedback calculations (verified on real chip)
  65. ** - corrected phase generator calculations (verified on real chip)
  66. ** - corrected envelope generator calculations (verified on real chip)
  67. ** - corrected FM volume level (YM2610 and YM2610B).
  68. ** - changed YMxxxUpdateOne() functions (YM2203, YM2608, YM2610, YM2610B, YM2612) :
  69. ** this was needed to calculate YM2610 FM channels output correctly.
  70. ** (Each FM channel is calculated as in other chips, but the output of the channel
  71. ** gets shifted right by one *before* sending to accumulator. That was impossible to do
  72. ** with previous implementation).
  73. **
  74. ** 23-07-2001 Jarek Burczynski, Nicola Salmoria:
  75. ** - corrected YM2610 ADPCM type A algorithm and tables (verified on real chip)
  76. **
  77. ** 11-06-2001 Jarek Burczynski:
  78. ** - corrected end of sample bug in ADPCMA_calc_cha().
  79. ** Real YM2610 checks for equality between current and end addresses (only 20 LSB bits).
  80. **
  81. ** 08-12-98 hiro-shi:
  82. ** rename ADPCMA -> ADPCMB, ADPCMB -> ADPCMA
  83. ** move ROM limit check.(CALC_CH? -> 2610Write1/2)
  84. ** test program (ADPCMB_TEST)
  85. ** move ADPCM A/B end check.
  86. ** ADPCMB repeat flag(no check)
  87. ** change ADPCM volume rate (8->16) (32->48).
  88. **
  89. ** 09-12-98 hiro-shi:
  90. ** change ADPCM volume. (8->16, 48->64)
  91. ** replace ym2610 ch0/3 (YM-2610B)
  92. ** change ADPCM_SHIFT (10->8) missing bank change 0x4000-0xffff.
  93. ** add ADPCM_SHIFT_MASK
  94. ** change ADPCMA_DECODE_MIN/MAX.
  95. */
  96. /************************************************************************/
  97. /* comment of hiro-shi(Hiromitsu Shioya) */
  98. /* YM2610(B) = OPN-B */
  99. /* YM2610 : PSG:3ch FM:4ch ADPCM(18.5KHz):6ch DeltaT ADPCM:1ch */
  100. /* YM2610B : PSG:3ch FM:6ch ADPCM(18.5KHz):6ch DeltaT ADPCM:1ch */
  101. /************************************************************************/
  102. //#include <stdio.h>
  103. #include <string.h>
  104. #include <math.h>
  105. #include "ym2612.h"
  106. #include <stdlib.h>
  107. // let it be 1 global to simplify things
  108. YM2612 ym2612;
  109. extern "C" void memset32(int *dest, int c, int count);
  110. #ifndef INLINE
  111. #define INLINE static __inline
  112. #endif
  113. #ifndef M_PI
  114. #define M_PI 3.14159265358979323846
  115. #endif
  116. /* globals */
  117. #define FREQ_SH 16 /* 16.16 fixed point (frequency calculations) */
  118. #define EG_SH 16 /* 16.16 fixed point (envelope generator timing) */
  119. #define LFO_SH 25 /* 7.25 fixed point (LFO calculations) */
  120. #define TIMER_SH 16 /* 16.16 fixed point (timers calculations) */
  121. #define ENV_BITS 10
  122. #define ENV_LEN (1<<ENV_BITS)
  123. #define ENV_STEP (128.0/ENV_LEN)
  124. #define MAX_ATT_INDEX (ENV_LEN-1) /* 1023 */
  125. #define MIN_ATT_INDEX (0) /* 0 */
  126. #define EG_ATT 4
  127. #define EG_DEC 3
  128. #define EG_SUS 2
  129. #define EG_REL 1
  130. #define EG_OFF 0
  131. #define SIN_BITS 10
  132. #define SIN_LEN (1<<SIN_BITS)
  133. #define SIN_MASK (SIN_LEN-1)
  134. #define TL_RES_LEN (256) /* 8 bits addressing (real chip) */
  135. #define EG_TIMER_OVERFLOW (3*(1<<EG_SH)) /* envelope generator timer overflows every 3 samples (on real chip) */
  136. #define MAXOUT (+32767)
  137. #define MINOUT (-32768)
  138. /* limitter */
  139. #define Limit(val, max,min) { \
  140. if ( val > max ) val = max; \
  141. else if ( val < min ) val = min; \
  142. }
  143. /* TL_TAB_LEN is calculated as:
  144. * 13 - sinus amplitude bits (Y axis)
  145. * 2 - sinus sign bit (Y axis)
  146. * TL_RES_LEN - sinus resolution (X axis)
  147. */
  148. //#define TL_TAB_LEN (13*2*TL_RES_LEN)
  149. #define TL_TAB_LEN (13*TL_RES_LEN*256/8) // 106496*2
  150. u16 ym_tl_tab[TL_TAB_LEN];
  151. /* ~3K wasted but oh well */
  152. u16 ym_tl_tab2[13*TL_RES_LEN];
  153. #define ENV_QUIET (2*13*TL_RES_LEN/8)
  154. /* sin waveform table in 'decibel' scale (use only period/4 values) */
  155. static u16 ym_sin_tab[256];
  156. /* sustain level table (3dB per step) */
  157. /* bit0, bit1, bit2, bit3, bit4, bit5, bit6 */
  158. /* 1, 2, 4, 8, 16, 32, 64 (value)*/
  159. /* 0.75, 1.5, 3, 6, 12, 24, 48 (dB)*/
  160. /* 0 - 15: 0, 3, 6, 9,12,15,18,21,24,27,30,33,36,39,42,93 (dB)*/
  161. #define SC(db) (u32) ( db * (4.0/ENV_STEP) )
  162. static const u32 sl_table[16]={
  163. SC( 0),SC( 1),SC( 2),SC(3 ),SC(4 ),SC(5 ),SC(6 ),SC( 7),
  164. SC( 8),SC( 9),SC(10),SC(11),SC(12),SC(13),SC(14),SC(31)
  165. };
  166. #undef SC
  167. #if 0
  168. #define RATE_STEPS (8)
  169. static const u8 eg_inc[19*RATE_STEPS]={
  170. /*cycle:0 1 2 3 4 5 6 7*/
  171. /* 0 */ 0,1, 0,1, 0,1, 0,1, /* rates 00..11 0 (increment by 0 or 1) */
  172. /* 1 */ 0,1, 0,1, 1,1, 0,1, /* rates 00..11 1 */
  173. /* 2 */ 0,1, 1,1, 0,1, 1,1, /* rates 00..11 2 */
  174. /* 3 */ 0,1, 1,1, 1,1, 1,1, /* rates 00..11 3 */
  175. /* 4 */ 1,1, 1,1, 1,1, 1,1, /* rate 12 0 (increment by 1) */
  176. /* 5 */ 1,1, 1,2, 1,1, 1,2, /* rate 12 1 */
  177. /* 6 */ 1,2, 1,2, 1,2, 1,2, /* rate 12 2 */
  178. /* 7 */ 1,2, 2,2, 1,2, 2,2, /* rate 12 3 */
  179. /* 8 */ 2,2, 2,2, 2,2, 2,2, /* rate 13 0 (increment by 2) */
  180. /* 9 */ 2,2, 2,4, 2,2, 2,4, /* rate 13 1 */
  181. /*10 */ 2,4, 2,4, 2,4, 2,4, /* rate 13 2 */
  182. /*11 */ 2,4, 4,4, 2,4, 4,4, /* rate 13 3 */
  183. /*12 */ 4,4, 4,4, 4,4, 4,4, /* rate 14 0 (increment by 4) */
  184. /*13 */ 4,4, 4,8, 4,4, 4,8, /* rate 14 1 */
  185. /*14 */ 4,8, 4,8, 4,8, 4,8, /* rate 14 2 */
  186. /*15 */ 4,8, 8,8, 4,8, 8,8, /* rate 14 3 */
  187. /*16 */ 8,8, 8,8, 8,8, 8,8, /* rates 15 0, 15 1, 15 2, 15 3 (increment by 8) */
  188. /*17 */ 16,16,16,16,16,16,16,16, /* rates 15 2, 15 3 for attack */
  189. /*18 */ 0,0, 0,0, 0,0, 0,0, /* infinity rates for attack and decay(s) */
  190. };
  191. #endif
  192. #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))
  193. static const u32 eg_inc_pack[19] =
  194. {
  195. /* 0 */ PACK(0,1,0,1,0,1,0,1), /* rates 00..11 0 (increment by 0 or 1) */
  196. /* 1 */ PACK(0,1,0,1,1,1,0,1), /* rates 00..11 1 */
  197. /* 2 */ PACK(0,1,1,1,0,1,1,1), /* rates 00..11 2 */
  198. /* 3 */ PACK(0,1,1,1,1,1,1,1), /* rates 00..11 3 */
  199. /* 4 */ PACK(1,1,1,1,1,1,1,1), /* rate 12 0 (increment by 1) */
  200. /* 5 */ PACK(1,1,1,2,1,1,1,2), /* rate 12 1 */
  201. /* 6 */ PACK(1,2,1,2,1,2,1,2), /* rate 12 2 */
  202. /* 7 */ PACK(1,2,2,2,1,2,2,2), /* rate 12 3 */
  203. /* 8 */ PACK(2,2,2,2,2,2,2,2), /* rate 13 0 (increment by 2) */
  204. /* 9 */ PACK(2,2,2,3,2,2,2,3), /* rate 13 1 */
  205. /*10 */ PACK(2,3,2,3,2,3,2,3), /* rate 13 2 */
  206. /*11 */ PACK(2,3,3,3,2,3,3,3), /* rate 13 3 */
  207. /*12 */ PACK(3,3,3,3,3,3,3,3), /* rate 14 0 (increment by 4) */
  208. /*13 */ PACK(3,3,3,4,3,3,3,4), /* rate 14 1 */
  209. /*14 */ PACK(3,4,3,4,3,4,3,4), /* rate 14 2 */
  210. /*15 */ PACK(3,4,4,4,3,4,4,4), /* rate 14 3 */
  211. /*16 */ PACK(4,4,4,4,4,4,4,4), /* rates 15 0, 15 1, 15 2, 15 3 (increment by 8) */
  212. /*17 */ PACK(5,5,5,5,5,5,5,5), /* rates 15 2, 15 3 for attack */
  213. /*18 */ PACK(0,0,0,0,0,0,0,0), /* infinity rates for attack and decay(s) */
  214. };
  215. //#define O(a) (a*RATE_STEPS)
  216. #define O(a) a
  217. /*note that there is no O(17) in this table - it's directly in the code */
  218. static const u8 eg_rate_select[32+64+32]={ /* Envelope Generator rates (32 + 64 rates + 32 RKS) */
  219. /* 32 infinite time rates */
  220. O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),
  221. O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),
  222. O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),
  223. O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),
  224. /* rates 00-11 */
  225. O( 0),O( 1),O( 2),O( 3),
  226. O( 0),O( 1),O( 2),O( 3),
  227. O( 0),O( 1),O( 2),O( 3),
  228. O( 0),O( 1),O( 2),O( 3),
  229. O( 0),O( 1),O( 2),O( 3),
  230. O( 0),O( 1),O( 2),O( 3),
  231. O( 0),O( 1),O( 2),O( 3),
  232. O( 0),O( 1),O( 2),O( 3),
  233. O( 0),O( 1),O( 2),O( 3),
  234. O( 0),O( 1),O( 2),O( 3),
  235. O( 0),O( 1),O( 2),O( 3),
  236. O( 0),O( 1),O( 2),O( 3),
  237. /* rate 12 */
  238. O( 4),O( 5),O( 6),O( 7),
  239. /* rate 13 */
  240. O( 8),O( 9),O(10),O(11),
  241. /* rate 14 */
  242. O(12),O(13),O(14),O(15),
  243. /* rate 15 */
  244. O(16),O(16),O(16),O(16),
  245. /* 32 dummy rates (same as 15 3) */
  246. O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16),
  247. O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16),
  248. O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16),
  249. O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16)
  250. };
  251. #undef O
  252. /*rate 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15*/
  253. /*shift 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0 */
  254. /*mask 2047, 1023, 511, 255, 127, 63, 31, 15, 7, 3, 1, 0, 0, 0, 0, 0 */
  255. #define O(a) (a*1)
  256. static const u8 eg_rate_shift[32+64+32]={ /* Envelope Generator counter shifts (32 + 64 rates + 32 RKS) */
  257. /* 32 infinite time rates */
  258. O(0),O(0),O(0),O(0),O(0),O(0),O(0),O(0),
  259. O(0),O(0),O(0),O(0),O(0),O(0),O(0),O(0),
  260. O(0),O(0),O(0),O(0),O(0),O(0),O(0),O(0),
  261. O(0),O(0),O(0),O(0),O(0),O(0),O(0),O(0),
  262. /* rates 00-11 */
  263. O(11),O(11),O(11),O(11),
  264. O(10),O(10),O(10),O(10),
  265. O( 9),O( 9),O( 9),O( 9),
  266. O( 8),O( 8),O( 8),O( 8),
  267. O( 7),O( 7),O( 7),O( 7),
  268. O( 6),O( 6),O( 6),O( 6),
  269. O( 5),O( 5),O( 5),O( 5),
  270. O( 4),O( 4),O( 4),O( 4),
  271. O( 3),O( 3),O( 3),O( 3),
  272. O( 2),O( 2),O( 2),O( 2),
  273. O( 1),O( 1),O( 1),O( 1),
  274. O( 0),O( 0),O( 0),O( 0),
  275. /* rate 12 */
  276. O( 0),O( 0),O( 0),O( 0),
  277. /* rate 13 */
  278. O( 0),O( 0),O( 0),O( 0),
  279. /* rate 14 */
  280. O( 0),O( 0),O( 0),O( 0),
  281. /* rate 15 */
  282. O( 0),O( 0),O( 0),O( 0),
  283. /* 32 dummy rates (same as 15 3) */
  284. O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),
  285. O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),
  286. O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),
  287. O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0)
  288. };
  289. #undef O
  290. static const u8 dt_tab[4 * 32]={
  291. /* this is YM2151 and YM2612 phase increment data (in 10.10 fixed point format)*/
  292. /* FD=0 */
  293. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  294. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  295. /* FD=1 */
  296. 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2,
  297. 2, 3, 3, 3, 4, 4, 4, 5, 5, 6, 6, 7, 8, 8, 8, 8,
  298. /* FD=2 */
  299. 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5,
  300. 5, 6, 6, 7, 8, 8, 9,10,11,12,13,14,16,16,16,16,
  301. /* FD=3 */
  302. 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 6, 6, 7,
  303. 8 , 8, 9,10,11,12,13,14,16,17,19,20,22,22,22,22
  304. };
  305. /* OPN key frequency number -> key code follow table */
  306. /* fnum higher 4bit -> keycode lower 2bit */
  307. static const u8 opn_fktable[16] = {0,0,0,0,0,0,0,1,2,3,3,3,3,3,3,3};
  308. /* 8 LFO speed parameters */
  309. /* each value represents number of samples that one LFO level will last for */
  310. static const u32 lfo_samples_per_step[8] = {108, 77, 71, 67, 62, 44, 8, 5};
  311. /*There are 4 different LFO AM depths available, they are:
  312. 0 dB, 1.4 dB, 5.9 dB, 11.8 dB
  313. Here is how it is generated (in EG steps):
  314. 11.8 dB = 0, 2, 4, 6, 8, 10,12,14,16...126,126,124,122,120,118,....4,2,0
  315. 5.9 dB = 0, 1, 2, 3, 4, 5, 6, 7, 8....63, 63, 62, 61, 60, 59,.....2,1,0
  316. 1.4 dB = 0, 0, 0, 0, 1, 1, 1, 1, 2,...15, 15, 15, 15, 14, 14,.....0,0,0
  317. (1.4 dB is loosing precision as you can see)
  318. It's implemented as generator from 0..126 with step 2 then a shift
  319. right N times, where N is:
  320. 8 for 0 dB
  321. 3 for 1.4 dB
  322. 1 for 5.9 dB
  323. 0 for 11.8 dB
  324. */
  325. static const u8 lfo_ams_depth_shift[4] = {8, 3, 1, 0};
  326. /*There are 8 different LFO PM depths available, they are:
  327. 0, 3.4, 6.7, 10, 14, 20, 40, 80 (cents)
  328. Modulation level at each depth depends on F-NUMBER bits: 4,5,6,7,8,9,10
  329. (bits 8,9,10 = FNUM MSB from OCT/FNUM register)
  330. Here we store only first quarter (positive one) of full waveform.
  331. Full table (lfo_pm_table) containing all 128 waveforms is build
  332. at run (init) time.
  333. One value in table below represents 4 (four) basic LFO steps
  334. (1 PM step = 4 AM steps).
  335. For example:
  336. at LFO SPEED=0 (which is 108 samples per basic LFO step)
  337. one value from "lfo_pm_output" table lasts for 432 consecutive
  338. samples (4*108=432) and one full LFO waveform cycle lasts for 13824
  339. samples (32*432=13824; 32 because we store only a quarter of whole
  340. waveform in the table below)
  341. */
  342. static const u8 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 */
  343. /* FNUM BIT 4: 000 0001xxxx */
  344. /* DEPTH 0 */ {0, 0, 0, 0, 0, 0, 0, 0},
  345. /* DEPTH 1 */ {0, 0, 0, 0, 0, 0, 0, 0},
  346. /* DEPTH 2 */ {0, 0, 0, 0, 0, 0, 0, 0},
  347. /* DEPTH 3 */ {0, 0, 0, 0, 0, 0, 0, 0},
  348. /* DEPTH 4 */ {0, 0, 0, 0, 0, 0, 0, 0},
  349. /* DEPTH 5 */ {0, 0, 0, 0, 0, 0, 0, 0},
  350. /* DEPTH 6 */ {0, 0, 0, 0, 0, 0, 0, 0},
  351. /* DEPTH 7 */ {0, 0, 0, 0, 1, 1, 1, 1},
  352. /* FNUM BIT 5: 000 0010xxxx */
  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, 1, 1, 1, 1},
  360. /* DEPTH 7 */ {0, 0, 1, 1, 2, 2, 2, 3},
  361. /* FNUM BIT 6: 000 0100xxxx */
  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, 1},
  367. /* DEPTH 5 */ {0, 0, 0, 0, 1, 1, 1, 1},
  368. /* DEPTH 6 */ {0, 0, 1, 1, 2, 2, 2, 3},
  369. /* DEPTH 7 */ {0, 0, 2, 3, 4, 4, 5, 6},
  370. /* FNUM BIT 7: 000 1000xxxx */
  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, 1, 1},
  374. /* DEPTH 3 */ {0, 0, 0, 0, 1, 1, 1, 1},
  375. /* DEPTH 4 */ {0, 0, 0, 1, 1, 1, 1, 2},
  376. /* DEPTH 5 */ {0, 0, 1, 1, 2, 2, 2, 3},
  377. /* DEPTH 6 */ {0, 0, 2, 3, 4, 4, 5, 6},
  378. /* DEPTH 7 */ {0, 0, 4, 6, 8, 8, 0xa, 0xc},
  379. /* FNUM BIT 8: 001 0000xxxx */
  380. /* DEPTH 0 */ {0, 0, 0, 0, 0, 0, 0, 0},
  381. /* DEPTH 1 */ {0, 0, 0, 0, 1, 1, 1, 1},
  382. /* DEPTH 2 */ {0, 0, 0, 1, 1, 1, 2, 2},
  383. /* DEPTH 3 */ {0, 0, 1, 1, 2, 2, 3, 3},
  384. /* DEPTH 4 */ {0, 0, 1, 2, 2, 2, 3, 4},
  385. /* DEPTH 5 */ {0, 0, 2, 3, 4, 4, 5, 6},
  386. /* DEPTH 6 */ {0, 0, 4, 6, 8, 8, 0xa, 0xc},
  387. /* DEPTH 7 */ {0, 0, 8, 0xc,0x10,0x10,0x14,0x18},
  388. /* FNUM BIT 9: 010 0000xxxx */
  389. /* DEPTH 0 */ {0, 0, 0, 0, 0, 0, 0, 0},
  390. /* DEPTH 1 */ {0, 0, 0, 0, 2, 2, 2, 2},
  391. /* DEPTH 2 */ {0, 0, 0, 2, 2, 2, 4, 4},
  392. /* DEPTH 3 */ {0, 0, 2, 2, 4, 4, 6, 6},
  393. /* DEPTH 4 */ {0, 0, 2, 4, 4, 4, 6, 8},
  394. /* DEPTH 5 */ {0, 0, 4, 6, 8, 8, 0xa, 0xc},
  395. /* DEPTH 6 */ {0, 0, 8, 0xc,0x10,0x10,0x14,0x18},
  396. /* DEPTH 7 */ {0, 0,0x10,0x18,0x20,0x20,0x28,0x30},
  397. /* FNUM BIT10: 100 0000xxxx */
  398. /* DEPTH 0 */ {0, 0, 0, 0, 0, 0, 0, 0},
  399. /* DEPTH 1 */ {0, 0, 0, 0, 4, 4, 4, 4},
  400. /* DEPTH 2 */ {0, 0, 0, 4, 4, 4, 8, 8},
  401. /* DEPTH 3 */ {0, 0, 4, 4, 8, 8, 0xc, 0xc},
  402. /* DEPTH 4 */ {0, 0, 4, 8, 8, 8, 0xc,0x10},
  403. /* DEPTH 5 */ {0, 0, 8, 0xc,0x10,0x10,0x14,0x18},
  404. /* DEPTH 6 */ {0, 0,0x10,0x18,0x20,0x20,0x28,0x30},
  405. /* DEPTH 7 */ {0, 0,0x20,0x30,0x40,0x40,0x50,0x60},
  406. };
  407. /* all 128 LFO PM waveforms */
  408. static s32 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 */
  409. /* there are 2048 FNUMs that can be generated using FNUM/BLK registers
  410. but LFO works with one more bit of a precision so we really need 4096 elements */
  411. static u32 fn_table[4096]; /* fnumber->increment counter */
  412. static int g_lfo_ampm = 0;
  413. /* register number to channel number , slot offset */
  414. #define OPN_CHAN(N) (N&3)
  415. #define OPN_SLOT(N) ((N>>2)&3)
  416. /* slot number */
  417. #define SLOT1 0
  418. #define SLOT2 2
  419. #define SLOT3 1
  420. #define SLOT4 3
  421. /* OPN Mode Register Write */
  422. INLINE void set_timers( int v )
  423. {
  424. /* b7 = CSM MODE */
  425. /* b6 = 3 slot mode */
  426. /* b5 = reset b */
  427. /* b4 = reset a */
  428. /* b3 = timer enable b */
  429. /* b2 = timer enable a */
  430. /* b1 = load b */
  431. /* b0 = load a */
  432. ym2612.OPN.ST.mode = v;
  433. /* reset Timer b flag */
  434. if( v & 0x20 )
  435. ym2612.OPN.ST.status &= ~2;
  436. /* reset Timer a flag */
  437. if( v & 0x10 )
  438. ym2612.OPN.ST.status &= ~1;
  439. }
  440. INLINE void FM_KEYON(int c , int s )
  441. {
  442. FM_SLOT *fmSlot = &ym2612.CH[c].fmSlot[s];
  443. if( !fmSlot->key )
  444. {
  445. fmSlot->key = 1;
  446. fmSlot->phase = 0; /* restart Phase Generator */
  447. fmSlot->state = EG_ATT; /* phase -> Attack */
  448. ym2612.slot_mask |= (1<<s) << (c*4);
  449. }
  450. }
  451. INLINE void FM_KEYOFF(int c , int s )
  452. {
  453. FM_SLOT *fmSlot = &ym2612.CH[c].fmSlot[s];
  454. if( fmSlot->key )
  455. {
  456. fmSlot->key = 0;
  457. if (fmSlot->state>EG_REL)
  458. fmSlot->state = EG_REL;/* phase -> Release */
  459. }
  460. }
  461. /* set detune & multiple */
  462. INLINE void set_det_mul(FM_CH *CH, FM_SLOT *fmSlot, int v)
  463. {
  464. fmSlot->mul = (v&0x0f)? (v&0x0f)*2 : 1;
  465. fmSlot->DT = ym2612.OPN.ST.dt_tab[(v>>4)&7];
  466. CH->fmSlot[SLOT1].Incr=-1;
  467. }
  468. /* set total level */
  469. INLINE void set_tl(FM_SLOT *fmSlot, int v)
  470. {
  471. fmSlot->tl = (v&0x7f)<<(ENV_BITS-7); /* 7bit TL */
  472. }
  473. /* set attack rate & key scale */
  474. INLINE void set_ar_ksr(FM_CH *CH, FM_SLOT *fmSlot, int v)
  475. {
  476. u8 old_KSR = fmSlot->KSR;
  477. fmSlot->ar = (v&0x1f) ? 32 + ((v&0x1f)<<1) : 0;
  478. fmSlot->KSR = 3-(v>>6);
  479. if (fmSlot->KSR != old_KSR)
  480. {
  481. CH->fmSlot[SLOT1].Incr=-1;
  482. }
  483. else
  484. {
  485. int eg_sh_ar, eg_sel_ar;
  486. /* refresh Attack rate */
  487. if ((fmSlot->ar + fmSlot->ksr) < 32+62)
  488. {
  489. eg_sh_ar = eg_rate_shift [fmSlot->ar + fmSlot->ksr ];
  490. eg_sel_ar = eg_rate_select[fmSlot->ar + fmSlot->ksr ];
  491. }
  492. else
  493. {
  494. eg_sh_ar = 0;
  495. eg_sel_ar = 17;
  496. }
  497. fmSlot->eg_pack_ar = eg_inc_pack[eg_sel_ar] | (eg_sh_ar<<24);
  498. }
  499. }
  500. /* set decay rate */
  501. INLINE void set_dr(FM_SLOT *fmSlot, int v)
  502. {
  503. int eg_sh_d1r, eg_sel_d1r;
  504. fmSlot->d1r = (v&0x1f) ? 32 + ((v&0x1f)<<1) : 0;
  505. eg_sh_d1r = eg_rate_shift [fmSlot->d1r + fmSlot->ksr];
  506. eg_sel_d1r= eg_rate_select[fmSlot->d1r + fmSlot->ksr];
  507. fmSlot->eg_pack_d1r = eg_inc_pack[eg_sel_d1r] | (eg_sh_d1r<<24);
  508. }
  509. /* set sustain rate */
  510. INLINE void set_sr(FM_SLOT *fmSlot, int v)
  511. {
  512. int eg_sh_d2r, eg_sel_d2r;
  513. fmSlot->d2r = (v&0x1f) ? 32 + ((v&0x1f)<<1) : 0;
  514. eg_sh_d2r = eg_rate_shift [fmSlot->d2r + fmSlot->ksr];
  515. eg_sel_d2r= eg_rate_select[fmSlot->d2r + fmSlot->ksr];
  516. fmSlot->eg_pack_d2r = eg_inc_pack[eg_sel_d2r] | (eg_sh_d2r<<24);
  517. }
  518. /* set release rate */
  519. INLINE void set_sl_rr(FM_SLOT *fmSlot, int v)
  520. {
  521. int eg_sh_rr, eg_sel_rr;
  522. fmSlot->sl = sl_table[ v>>4 ];
  523. fmSlot->rr = 34 + ((v&0x0f)<<2);
  524. eg_sh_rr = eg_rate_shift [fmSlot->rr + fmSlot->ksr];
  525. eg_sel_rr = eg_rate_select[fmSlot->rr + fmSlot->ksr];
  526. fmSlot->eg_pack_rr = eg_inc_pack[eg_sel_rr] | (eg_sh_rr<<24);
  527. }
  528. INLINE signed int op_calc(u32 phase, unsigned int env, signed int pm)
  529. {
  530. int ret, sin = (phase>>16) + (pm>>1);
  531. int neg = sin & 0x200;
  532. if (sin & 0x100) sin ^= 0xff;
  533. sin&=0xff;
  534. env&=~1;
  535. // this was already checked
  536. // if (env >= ENV_QUIET) // 384
  537. // return 0;
  538. ret = ym_tl_tab[sin | (env<<7)];
  539. return neg ? -ret : ret;
  540. }
  541. INLINE signed int op_calc1(u32 phase, unsigned int env, signed int pm)
  542. {
  543. int ret, sin = (phase+pm)>>16;
  544. int neg = sin & 0x200;
  545. if (sin & 0x100) sin ^= 0xff;
  546. sin&=0xff;
  547. env&=~1;
  548. // if (env >= ENV_QUIET) // 384
  549. // return 0;
  550. ret = ym_tl_tab[sin | (env<<7)];
  551. return neg ? -ret : ret;
  552. }
  553. #if !defined(_ASM_YM2612_C)
  554. /* advance LFO to next sample */
  555. INLINE int advance_lfo(int lfo_ampm, u32 lfo_cnt_old, u32 lfo_cnt)
  556. {
  557. u8 pos;
  558. u8 prev_pos;
  559. prev_pos = (lfo_cnt_old >> LFO_SH) & 127;
  560. pos = (lfo_cnt >> LFO_SH) & 127;
  561. /* update AM when LFO output changes */
  562. if (prev_pos != pos)
  563. {
  564. lfo_ampm &= 0xff;
  565. /* triangle */
  566. /* AM: 0 to 126 step +2, 126 to 0 step -2 */
  567. if (pos<64)
  568. lfo_ampm |= ((pos&63) * 2) << 8; /* 0 - 126 */
  569. else
  570. lfo_ampm |= (126 - (pos&63)*2) << 8;
  571. }
  572. else
  573. {
  574. return lfo_ampm;
  575. }
  576. /* PM works with 4 times slower clock */
  577. prev_pos >>= 2;
  578. pos >>= 2;
  579. /* update PM when LFO output changes */
  580. if (prev_pos != pos)
  581. {
  582. lfo_ampm &= ~0xff;
  583. lfo_ampm |= pos; /* 0 - 32 */
  584. }
  585. return lfo_ampm;
  586. }
  587. #define EG_INC_VAL() \
  588. ((1 << ((pack >> ((eg_cnt>>shift)&7)*3)&7)) >> 1)
  589. INLINE u32 update_eg_phase(FM_SLOT *fmSlot, u32 eg_cnt)
  590. {
  591. s32 volume = fmSlot->volume;
  592. switch(fmSlot->state)
  593. {
  594. case EG_ATT: /* attack phase */
  595. {
  596. u32 pack = fmSlot->eg_pack_ar;
  597. u32 shift = pack>>24;
  598. if ( !(eg_cnt & ((1<<shift)-1) ) )
  599. {
  600. volume += ( ~volume * EG_INC_VAL() ) >>4;
  601. if (volume <= MIN_ATT_INDEX)
  602. {
  603. volume = MIN_ATT_INDEX;
  604. fmSlot->state = EG_DEC;
  605. }
  606. }
  607. break;
  608. }
  609. case EG_DEC: /* decay phase */
  610. {
  611. u32 pack = fmSlot->eg_pack_d1r;
  612. u32 shift = pack>>24;
  613. if ( !(eg_cnt & ((1<<shift)-1) ) )
  614. {
  615. volume += EG_INC_VAL();
  616. if ( volume >= (s32) fmSlot->sl )
  617. fmSlot->state = EG_SUS;
  618. }
  619. break;
  620. }
  621. case EG_SUS: /* sustain phase */
  622. {
  623. u32 pack = fmSlot->eg_pack_d2r;
  624. u32 shift = pack>>24;
  625. if ( !(eg_cnt & ((1<<shift)-1) ) )
  626. {
  627. volume += EG_INC_VAL();
  628. if ( volume >= MAX_ATT_INDEX )
  629. {
  630. volume = MAX_ATT_INDEX;
  631. /* do not change SLOT->state (verified on real chip) */
  632. }
  633. }
  634. break;
  635. }
  636. case EG_REL: /* release phase */
  637. {
  638. u32 pack = fmSlot->eg_pack_rr;
  639. u32 shift = pack>>24;
  640. if ( !(eg_cnt & ((1<<shift)-1) ) )
  641. {
  642. volume += EG_INC_VAL();
  643. if ( volume >= MAX_ATT_INDEX )
  644. {
  645. volume = MAX_ATT_INDEX;
  646. fmSlot->state = EG_OFF;
  647. }
  648. }
  649. break;
  650. }
  651. }
  652. fmSlot->volume = volume;
  653. return fmSlot->tl + ((u32)volume); /* tl is 7bit<<3, volume 0-1023 (0-2039 total) */
  654. }
  655. #endif
  656. typedef struct
  657. {
  658. u16 vol_out1; /* 00: current output from EG circuit (without AM from LFO) */
  659. u16 vol_out2;
  660. u16 vol_out3;
  661. u16 vol_out4;
  662. u32 pad[2];
  663. u32 phase1; /* 10 */
  664. u32 phase2;
  665. u32 phase3;
  666. u32 phase4;
  667. u32 incr1; /* 20: phase step */
  668. u32 incr2;
  669. u32 incr3;
  670. u32 incr4;
  671. u32 lfo_cnt; /* 30 */
  672. u32 lfo_inc;
  673. s32 mem; /* one sample delay memory */
  674. u32 eg_cnt; /* envelope generator counter */
  675. FM_CH *CH; /* 40: envelope generator counter */
  676. u32 eg_timer;
  677. u32 eg_timer_add;
  678. u32 pack; // 4c: stereo, lastchan, disabled, lfo_enabled | pan_r, pan_l, ams[2] | AMmasks[4] | FB[4] | lfo_ampm[16]
  679. u32 algo; /* 50: algo[3], was_update */
  680. s32 op1_out;
  681. #ifdef _MIPS_ARCH_ALLEGREX
  682. u32 pad1[3+8];
  683. #endif
  684. } chan_rend_context;
  685. #if !defined(_ASM_YM2612_C)
  686. static void chan_render_loop(chan_rend_context *ct, int *buffer, int length)
  687. {
  688. int scounter; /* sample counter */
  689. /* sample generating loop */
  690. for (scounter = 0; scounter < length; scounter++)
  691. {
  692. int smp = 0; /* produced sample */
  693. unsigned int eg_out, eg_out2, eg_out4;
  694. if (ct->pack & 8) { /* LFO enabled ? (test Earthworm Jim in between demo 1 and 2) */
  695. ct->pack = (ct->pack&0xffff) | (advance_lfo(ct->pack >> 16, ct->lfo_cnt, ct->lfo_cnt + ct->lfo_inc) << 16);
  696. ct->lfo_cnt += ct->lfo_inc;
  697. }
  698. ct->eg_timer += ct->eg_timer_add;
  699. while (ct->eg_timer >= EG_TIMER_OVERFLOW)
  700. {
  701. ct->eg_timer -= EG_TIMER_OVERFLOW;
  702. ct->eg_cnt++;
  703. if (ct->CH->fmSlot[SLOT1].state != EG_OFF) ct->vol_out1 = update_eg_phase(&ct->CH->fmSlot[SLOT1], ct->eg_cnt);
  704. if (ct->CH->fmSlot[SLOT2].state != EG_OFF) ct->vol_out2 = update_eg_phase(&ct->CH->fmSlot[SLOT2], ct->eg_cnt);
  705. if (ct->CH->fmSlot[SLOT3].state != EG_OFF) ct->vol_out3 = update_eg_phase(&ct->CH->fmSlot[SLOT3], ct->eg_cnt);
  706. if (ct->CH->fmSlot[SLOT4].state != EG_OFF) ct->vol_out4 = update_eg_phase(&ct->CH->fmSlot[SLOT4], ct->eg_cnt);
  707. }
  708. if (ct->pack & 4) continue; /* output disabled */
  709. /* calculate channel sample */
  710. eg_out = ct->vol_out1;
  711. if ( (ct->pack & 8) && (ct->pack&(1<<(SLOT1+8))) ) eg_out += ct->pack >> (((ct->pack&0xc0)>>6)+24);
  712. if( eg_out < ENV_QUIET ) /* SLOT 1 */
  713. {
  714. int out = 0;
  715. if (ct->pack&0xf000) out = ((ct->op1_out>>16) + (ct->op1_out<<16>>16)) << ((ct->pack&0xf000)>>12); /* op1_out0 + op1_out1 */
  716. ct->op1_out <<= 16;
  717. ct->op1_out |= (unsigned short)op_calc1(ct->phase1, eg_out, out);
  718. } else {
  719. ct->op1_out <<= 16; /* op1_out0 = op1_out1; op1_out1 = 0; */
  720. }
  721. eg_out = ct->vol_out3; // volume_calc(&CH->SLOT[SLOT3]);
  722. eg_out2 = ct->vol_out2; // volume_calc(&CH->SLOT[SLOT2]);
  723. eg_out4 = ct->vol_out4; // volume_calc(&CH->SLOT[SLOT4]);
  724. if (ct->pack & 8) {
  725. unsigned int add = ct->pack >> (((ct->pack&0xc0)>>6)+24);
  726. if (ct->pack & (1<<(SLOT3+8))) eg_out += add;
  727. if (ct->pack & (1<<(SLOT2+8))) eg_out2 += add;
  728. if (ct->pack & (1<<(SLOT4+8))) eg_out4 += add;
  729. }
  730. switch( ct->CH->ALGO )
  731. {
  732. case 0:
  733. {
  734. /* M1---C1---MEM---M2---C2---OUT */
  735. int m2,c1,c2=0; /* Phase Modulation input for operators 2,3,4 */
  736. m2 = ct->mem;
  737. c1 = ct->op1_out>>16;
  738. if( eg_out < ENV_QUIET ) { /* SLOT 3 */
  739. c2 = op_calc(ct->phase3, eg_out, m2);
  740. }
  741. if( eg_out2 < ENV_QUIET ) { /* SLOT 2 */
  742. ct->mem = op_calc(ct->phase2, eg_out2, c1);
  743. }
  744. else ct->mem = 0;
  745. if( eg_out4 < ENV_QUIET ) { /* SLOT 4 */
  746. smp = op_calc(ct->phase4, eg_out4, c2);
  747. }
  748. break;
  749. }
  750. case 1:
  751. {
  752. /* M1------+-MEM---M2---C2---OUT */
  753. /* C1-+ */
  754. int m2,c2=0;
  755. m2 = ct->mem;
  756. ct->mem = ct->op1_out>>16;
  757. if( eg_out < ENV_QUIET ) { /* SLOT 3 */
  758. c2 = op_calc(ct->phase3, eg_out, m2);
  759. }
  760. if( eg_out2 < ENV_QUIET ) { /* SLOT 2 */
  761. ct->mem+= op_calc(ct->phase2, eg_out2, 0);
  762. }
  763. if( eg_out4 < ENV_QUIET ) { /* SLOT 4 */
  764. smp = op_calc(ct->phase4, eg_out4, c2);
  765. }
  766. break;
  767. }
  768. case 2:
  769. {
  770. /* M1-----------------+-C2---OUT */
  771. /* C1---MEM---M2-+ */
  772. int m2,c2;
  773. m2 = ct->mem;
  774. c2 = ct->op1_out>>16;
  775. if( eg_out < ENV_QUIET ) { /* SLOT 3 */
  776. c2 += op_calc(ct->phase3, eg_out, m2);
  777. }
  778. if( eg_out2 < ENV_QUIET ) { /* SLOT 2 */
  779. ct->mem = op_calc(ct->phase2, eg_out2, 0);
  780. }
  781. else ct->mem = 0;
  782. if( eg_out4 < ENV_QUIET ) { /* SLOT 4 */
  783. smp = op_calc(ct->phase4, eg_out4, c2);
  784. }
  785. break;
  786. }
  787. case 3:
  788. {
  789. /* M1---C1---MEM------+-C2---OUT */
  790. /* M2-+ */
  791. int c1,c2;
  792. c2 = ct->mem;
  793. c1 = ct->op1_out>>16;
  794. if( eg_out < ENV_QUIET ) { /* SLOT 3 */
  795. c2 += op_calc(ct->phase3, eg_out, 0);
  796. }
  797. if( eg_out2 < ENV_QUIET ) { /* SLOT 2 */
  798. ct->mem = op_calc(ct->phase2, eg_out2, c1);
  799. }
  800. else ct->mem = 0;
  801. if( eg_out4 < ENV_QUIET ) { /* SLOT 4 */
  802. smp = op_calc(ct->phase4, eg_out4, c2);
  803. }
  804. break;
  805. }
  806. case 4:
  807. {
  808. /* M1---C1-+-OUT */
  809. /* M2---C2-+ */
  810. /* MEM: not used */
  811. int c1,c2=0;
  812. c1 = ct->op1_out>>16;
  813. if( eg_out < ENV_QUIET ) { /* SLOT 3 */
  814. c2 = op_calc(ct->phase3, eg_out, 0);
  815. }
  816. if( eg_out2 < ENV_QUIET ) { /* SLOT 2 */
  817. smp = op_calc(ct->phase2, eg_out2, c1);
  818. }
  819. if( eg_out4 < ENV_QUIET ) { /* SLOT 4 */
  820. smp+= op_calc(ct->phase4, eg_out4, c2);
  821. }
  822. break;
  823. }
  824. case 5:
  825. {
  826. /* +----C1----+ */
  827. /* M1-+-MEM---M2-+-OUT */
  828. /* +----C2----+ */
  829. int m2,c1,c2;
  830. m2 = ct->mem;
  831. ct->mem = c1 = c2 = ct->op1_out>>16;
  832. if( eg_out < ENV_QUIET ) { /* SLOT 3 */
  833. smp = op_calc(ct->phase3, eg_out, m2);
  834. }
  835. if( eg_out2 < ENV_QUIET ) { /* SLOT 2 */
  836. smp+= op_calc(ct->phase2, eg_out2, c1);
  837. }
  838. if( eg_out4 < ENV_QUIET ) { /* SLOT 4 */
  839. smp+= op_calc(ct->phase4, eg_out4, c2);
  840. }
  841. break;
  842. }
  843. case 6:
  844. {
  845. /* M1---C1-+ */
  846. /* M2-+-OUT */
  847. /* C2-+ */
  848. /* MEM: not used */
  849. int c1;
  850. c1 = ct->op1_out>>16;
  851. if( eg_out < ENV_QUIET ) { /* SLOT 3 */
  852. smp = op_calc(ct->phase3, eg_out, 0);
  853. }
  854. if( eg_out2 < ENV_QUIET ) { /* SLOT 2 */
  855. smp+= op_calc(ct->phase2, eg_out2, c1);
  856. }
  857. if( eg_out4 < ENV_QUIET ) { /* SLOT 4 */
  858. smp+= op_calc(ct->phase4, eg_out4, 0);
  859. }
  860. break;
  861. }
  862. case 7:
  863. {
  864. /* M1-+ */
  865. /* C1-+-OUT */
  866. /* M2-+ */
  867. /* C2-+ */
  868. /* MEM: not used*/
  869. smp = ct->op1_out>>16;
  870. if( eg_out < ENV_QUIET ) { /* SLOT 3 */
  871. smp += op_calc(ct->phase3, eg_out, 0);
  872. }
  873. if( eg_out2 < ENV_QUIET ) { /* SLOT 2 */
  874. smp += op_calc(ct->phase2, eg_out2, 0);
  875. }
  876. if( eg_out4 < ENV_QUIET ) { /* SLOT 4 */
  877. smp += op_calc(ct->phase4, eg_out4, 0);
  878. }
  879. break;
  880. }
  881. }
  882. /* done calculating channel sample */
  883. /* mix sample to output buffer */
  884. if (smp) {
  885. if (ct->pack & 1) { /* stereo */
  886. if (ct->pack & 0x20) /* L */ /* TODO: check correctness */
  887. buffer[scounter*2] += smp;
  888. if (ct->pack & 0x10) /* R */
  889. buffer[scounter*2+1] += smp;
  890. } else {
  891. buffer[scounter] += smp;
  892. }
  893. ct->algo = 8; // algo is only used in asm, here only bit3 is used
  894. }
  895. /* update phase counters AFTER output calculations */
  896. ct->phase1 += ct->incr1;
  897. ct->phase2 += ct->incr2;
  898. ct->phase3 += ct->incr3;
  899. ct->phase4 += ct->incr4;
  900. }
  901. }
  902. #else
  903. extern "C" void chan_render_loop(chan_rend_context *ct, int *buffer, unsigned short length);
  904. #endif
  905. static chan_rend_context __attribute__((aligned(64))) crct;
  906. static int chan_render(int *buffer, int length, int c, u32 flags) // flags: stereo, ?, disabled, ?, pan_r, pan_l
  907. {
  908. crct.CH = &ym2612.CH[c];
  909. crct.mem = crct.CH->mem_value; /* one sample delay memory */
  910. crct.lfo_cnt = ym2612.OPN.lfo_cnt;
  911. crct.lfo_inc = ym2612.OPN.lfo_inc;
  912. flags &= 0x35;
  913. if (crct.lfo_inc) {
  914. flags |= 8;
  915. flags |= g_lfo_ampm << 16;
  916. flags |= crct.CH->AMmasks << 8;
  917. if (crct.CH->ams == 8) // no ams
  918. flags &= ~0xf00;
  919. else flags |= (crct.CH->ams&3)<<6;
  920. }
  921. flags |= (crct.CH->FB&0xf)<<12; /* feedback shift */
  922. crct.pack = flags;
  923. crct.eg_cnt = ym2612.OPN.eg_cnt; /* envelope generator counter */
  924. crct.eg_timer = ym2612.OPN.eg_timer;
  925. crct.eg_timer_add = ym2612.OPN.eg_timer_add;
  926. /* precalculate phase modulation incr */
  927. crct.phase1 = crct.CH->fmSlot[SLOT1].phase;
  928. crct.phase2 = crct.CH->fmSlot[SLOT2].phase;
  929. crct.phase3 = crct.CH->fmSlot[SLOT3].phase;
  930. crct.phase4 = crct.CH->fmSlot[SLOT4].phase;
  931. /* current output from EG circuit (without AM from LFO) */
  932. crct.vol_out1 = crct.CH->fmSlot[SLOT1].tl + ((u32)crct.CH->fmSlot[SLOT1].volume);
  933. crct.vol_out2 = crct.CH->fmSlot[SLOT2].tl + ((u32)crct.CH->fmSlot[SLOT2].volume);
  934. crct.vol_out3 = crct.CH->fmSlot[SLOT3].tl + ((u32)crct.CH->fmSlot[SLOT3].volume);
  935. crct.vol_out4 = crct.CH->fmSlot[SLOT4].tl + ((u32)crct.CH->fmSlot[SLOT4].volume);
  936. crct.op1_out = crct.CH->op1_out;
  937. crct.algo = crct.CH->ALGO & 7;
  938. if(crct.CH->pms)
  939. {
  940. /* add support for 3 slot mode */
  941. u32 block_fnum = crct.CH->block_fnum;
  942. u32 fnum_lfo = ((block_fnum & 0x7f0) >> 4) * 32 * 8;
  943. s32 lfo_fn_table_index_offset = lfo_pm_table[ fnum_lfo + crct.CH->pms + ((crct.pack>>16)&0xff) ];
  944. if (lfo_fn_table_index_offset) /* LFO phase modulation active */
  945. {
  946. u8 blk;
  947. u32 fn;
  948. int kc,fc;
  949. block_fnum = block_fnum*2 + lfo_fn_table_index_offset;
  950. blk = (block_fnum&0x7000) >> 12;
  951. fn = block_fnum & 0xfff;
  952. /* keyscale code */
  953. kc = (blk<<2) | opn_fktable[fn >> 8];
  954. /* phase increment counter */
  955. fc = fn_table[fn]>>(7-blk);
  956. crct.incr1 = ((fc+crct.CH->fmSlot[SLOT1].DT[kc])*crct.CH->fmSlot[SLOT1].mul) >> 1;
  957. crct.incr2 = ((fc+crct.CH->fmSlot[SLOT2].DT[kc])*crct.CH->fmSlot[SLOT2].mul) >> 1;
  958. crct.incr3 = ((fc+crct.CH->fmSlot[SLOT3].DT[kc])*crct.CH->fmSlot[SLOT3].mul) >> 1;
  959. crct.incr4 = ((fc+crct.CH->fmSlot[SLOT4].DT[kc])*crct.CH->fmSlot[SLOT4].mul) >> 1;
  960. }
  961. else /* LFO phase modulation = zero */
  962. {
  963. crct.incr1 = crct.CH->fmSlot[SLOT1].Incr;
  964. crct.incr2 = crct.CH->fmSlot[SLOT2].Incr;
  965. crct.incr3 = crct.CH->fmSlot[SLOT3].Incr;
  966. crct.incr4 = crct.CH->fmSlot[SLOT4].Incr;
  967. }
  968. }
  969. else /* no LFO phase modulation */
  970. {
  971. crct.incr1 = crct.CH->fmSlot[SLOT1].Incr;
  972. crct.incr2 = crct.CH->fmSlot[SLOT2].Incr;
  973. crct.incr3 = crct.CH->fmSlot[SLOT3].Incr;
  974. crct.incr4 = crct.CH->fmSlot[SLOT4].Incr;
  975. }
  976. chan_render_loop(&crct, buffer, length);
  977. crct.CH->op1_out = crct.op1_out;
  978. crct.CH->mem_value = crct.mem;
  979. if (crct.CH->fmSlot[SLOT1].state | crct.CH->fmSlot[SLOT2].state | crct.CH->fmSlot[SLOT3].state | crct.CH->fmSlot[SLOT4].state)
  980. {
  981. crct.CH->fmSlot[SLOT1].phase = crct.phase1;
  982. crct.CH->fmSlot[SLOT2].phase = crct.phase2;
  983. crct.CH->fmSlot[SLOT3].phase = crct.phase3;
  984. crct.CH->fmSlot[SLOT4].phase = crct.phase4;
  985. }
  986. else
  987. ym2612.slot_mask &= ~(0xf << (c*4));
  988. // if this the last call, write back persistent stuff:
  989. if ((ym2612.slot_mask >> ((c+1)*4)) == 0)
  990. {
  991. ym2612.OPN.eg_cnt = crct.eg_cnt;
  992. ym2612.OPN.eg_timer = crct.eg_timer;
  993. g_lfo_ampm = crct.pack >> 16;
  994. ym2612.OPN.lfo_cnt = crct.lfo_cnt;
  995. }
  996. return (crct.algo & 8) >> 3; // had output
  997. }
  998. /* update phase increment and envelope generator */
  999. INLINE void refresh_fc_eg_slot(FM_SLOT *fmSlot, int fc, int kc)
  1000. {
  1001. int ksr;
  1002. /* (frequency) phase increment counter */
  1003. fmSlot->Incr = ((fc+fmSlot->DT[kc])*fmSlot->mul) >> 1;
  1004. ksr = kc >> fmSlot->KSR;
  1005. if( fmSlot->ksr != ksr )
  1006. {
  1007. int eg_sh, eg_sel;
  1008. fmSlot->ksr = ksr;
  1009. /* calculate envelope generator rates */
  1010. if ((fmSlot->ar + fmSlot->ksr) < 32+62)
  1011. {
  1012. eg_sh = eg_rate_shift [fmSlot->ar + fmSlot->ksr ];
  1013. eg_sel = eg_rate_select[fmSlot->ar + fmSlot->ksr ];
  1014. }
  1015. else
  1016. {
  1017. eg_sh = 0;
  1018. eg_sel = 17;
  1019. }
  1020. fmSlot->eg_pack_ar = eg_inc_pack[eg_sel] | (eg_sh<<24);
  1021. eg_sh = eg_rate_shift [fmSlot->d1r + fmSlot->ksr];
  1022. eg_sel = eg_rate_select[fmSlot->d1r + fmSlot->ksr];
  1023. fmSlot->eg_pack_d1r = eg_inc_pack[eg_sel] | (eg_sh<<24);
  1024. eg_sh = eg_rate_shift [fmSlot->d2r + fmSlot->ksr];
  1025. eg_sel = eg_rate_select[fmSlot->d2r + fmSlot->ksr];
  1026. fmSlot->eg_pack_d2r = eg_inc_pack[eg_sel] | (eg_sh<<24);
  1027. eg_sh = eg_rate_shift [fmSlot->rr + fmSlot->ksr];
  1028. eg_sel = eg_rate_select[fmSlot->rr + fmSlot->ksr];
  1029. fmSlot->eg_pack_rr = eg_inc_pack[eg_sel] | (eg_sh<<24);
  1030. }
  1031. }
  1032. /* update phase increment counters */
  1033. INLINE void refresh_fc_eg_chan(FM_CH *CH)
  1034. {
  1035. if( CH->fmSlot[SLOT1].Incr == (u32)-1){
  1036. int fc = CH->fc;
  1037. int kc = CH->kcode;
  1038. refresh_fc_eg_slot(&CH->fmSlot[SLOT1] , fc , kc );
  1039. refresh_fc_eg_slot(&CH->fmSlot[SLOT2] , fc , kc );
  1040. refresh_fc_eg_slot(&CH->fmSlot[SLOT3] , fc , kc );
  1041. refresh_fc_eg_slot(&CH->fmSlot[SLOT4] , fc , kc );
  1042. }
  1043. }
  1044. /* initialize time tables */
  1045. static void init_timetables(const u8 *dttable)
  1046. {
  1047. int i,d;
  1048. qreal rate;
  1049. /* DeTune table */
  1050. for (d = 0;d <= 3;d++){
  1051. for (i = 0;i <= 31;i++){
  1052. rate = ((qreal)dttable[d*32 + i]) * SIN_LEN * ym2612.OPN.ST.freqbase * (1<<FREQ_SH) / ((qreal)(1<<20));
  1053. ym2612.OPN.ST.dt_tab[d][i] = (s32) rate;
  1054. ym2612.OPN.ST.dt_tab[d+4][i] = -ym2612.OPN.ST.dt_tab[d][i];
  1055. }
  1056. }
  1057. }
  1058. static void reset_channels(FM_CH *CH)
  1059. {
  1060. int c,s;
  1061. ym2612.OPN.ST.mode = 0; /* normal mode */
  1062. ym2612.OPN.ST.TA = 0;
  1063. ym2612.OPN.ST.TAC = 0;
  1064. ym2612.OPN.ST.TB = 0;
  1065. ym2612.OPN.ST.TBC = 0;
  1066. for( c = 0 ; c < 6 ; c++ )
  1067. {
  1068. CH[c].fc = 0;
  1069. for(s = 0 ; s < 4 ; s++ )
  1070. {
  1071. CH[c].fmSlot[s].state= EG_OFF;
  1072. CH[c].fmSlot[s].volume = MAX_ATT_INDEX;
  1073. }
  1074. }
  1075. ym2612.slot_mask = 0;
  1076. }
  1077. /* initialize generic tables */
  1078. static void init_tables(void)
  1079. {
  1080. signed int i,x,y,p;
  1081. signed int n;
  1082. qreal o,m;
  1083. for (i=0; i < 256; i++)
  1084. {
  1085. /* non-standard sinus */
  1086. m = sin( ((i*2)+1) * M_PI / SIN_LEN ); /* checked against the real chip */
  1087. /* we never reach zero here due to ((i*2)+1) */
  1088. if (m>0.0)
  1089. o = 8*log(1.0/m)/log(2); /* convert to 'decibels' */
  1090. else
  1091. o = 8*log(-1.0/m)/log(2); /* convert to 'decibels' */
  1092. o = o / (ENV_STEP/4);
  1093. n = (int)(2.0*o);
  1094. if (n&1) /* round to nearest */
  1095. n = (n>>1)+1;
  1096. else
  1097. n = n>>1;
  1098. ym_sin_tab[ i ] = n;
  1099. //dprintf("FM.C: sin [%4i]= %4i", i, ym_sin_tab[i]);
  1100. }
  1101. //dprintf("FM.C: ENV_QUIET= %08x", ENV_QUIET );
  1102. for (x=0; x < TL_RES_LEN; x++)
  1103. {
  1104. m = (1<<16) / pow(2, (x+1) * (ENV_STEP/4.0) / 8.0);
  1105. m = floor(m);
  1106. /* we never reach (1<<16) here due to the (x+1) */
  1107. /* result fits within 16 bits at maximum */
  1108. n = (int)m; /* 16 bits here */
  1109. n >>= 4; /* 12 bits here */
  1110. if (n&1) /* round to nearest */
  1111. n = (n>>1)+1;
  1112. else
  1113. n = n>>1;
  1114. /* 11 bits here (rounded) */
  1115. n <<= 2; /* 13 bits here (as in real chip) */
  1116. ym_tl_tab2[ x ] = n;
  1117. for (i=1; i < 13; i++)
  1118. {
  1119. ym_tl_tab2[ x + i*TL_RES_LEN ] = n >> i;
  1120. }
  1121. }
  1122. for (x=0; x < 256; x++)
  1123. {
  1124. int sin = ym_sin_tab[ x ];
  1125. for (y=0; y < 2*13*TL_RES_LEN/8; y+=2)
  1126. {
  1127. p = (y<<2) + sin;
  1128. if (p >= 13*TL_RES_LEN)
  1129. ym_tl_tab[(y<<7) | x] = 0;
  1130. else ym_tl_tab[(y<<7) | x] = ym_tl_tab2[p];
  1131. }
  1132. }
  1133. /* build LFO PM modulation table */
  1134. for(i = 0; i < 8; i++) /* 8 PM depths */
  1135. {
  1136. u8 fnum;
  1137. for (fnum=0; fnum<128; fnum++) /* 7 bits meaningful of F-NUMBER */
  1138. {
  1139. u8 value;
  1140. u8 step;
  1141. u32 offset_depth = i;
  1142. u32 offset_fnum_bit;
  1143. u32 bit_tmp;
  1144. for (step=0; step<8; step++)
  1145. {
  1146. value = 0;
  1147. for (bit_tmp=0; bit_tmp<7; bit_tmp++) /* 7 bits */
  1148. {
  1149. if (fnum & (1<<bit_tmp)) /* only if bit "bit_tmp" is set */
  1150. {
  1151. offset_fnum_bit = bit_tmp * 8;
  1152. value += lfo_pm_output[offset_fnum_bit + offset_depth][step];
  1153. }
  1154. }
  1155. lfo_pm_table[(fnum*32*8) + (i*32) + step + 0] = value;
  1156. lfo_pm_table[(fnum*32*8) + (i*32) +(step^7)+ 8] = value;
  1157. lfo_pm_table[(fnum*32*8) + (i*32) + step +16] = -value;
  1158. lfo_pm_table[(fnum*32*8) + (i*32) +(step^7)+24] = -value;
  1159. }
  1160. }
  1161. }
  1162. }
  1163. /* prescaler set (and make time tables) */
  1164. static void OPNSetPres(int pres)
  1165. {
  1166. int i;
  1167. /* frequency base */
  1168. ym2612.OPN.ST.freqbase = (ym2612.OPN.ST.rate) ? ((qreal)ym2612.OPN.ST.clock / ym2612.OPN.ST.rate) / pres : 0;
  1169. ym2612.OPN.eg_timer_add = (1<<EG_SH) * ym2612.OPN.ST.freqbase;
  1170. /* make time tables */
  1171. init_timetables( dt_tab );
  1172. /* there are 2048 FNUMs that can be generated using FNUM/BLK registers
  1173. but LFO works with one more bit of a precision so we really need 4096 elements */
  1174. /* calculate fnumber -> increment counter table */
  1175. for(i = 0; i < 4096; i++)
  1176. {
  1177. /* freq table for octave 7 */
  1178. /* OPN phase increment counter = 20bit */
  1179. fn_table[i] = (u32)( (qreal)i * 32 * ym2612.OPN.ST.freqbase * (1<<(FREQ_SH-10)) ); /* -10 because chip works with 10.10 fixed point, while we use 16.16 */
  1180. }
  1181. /* LFO freq. table */
  1182. for(i = 0; i < 8; i++)
  1183. {
  1184. /* Amplitude modulation: 64 output levels (triangle waveform); 1 level lasts for one of "lfo_samples_per_step" samples */
  1185. /* Phase modulation: one entry from lfo_pm_output lasts for one of 4 * "lfo_samples_per_step" samples */
  1186. ym2612.OPN.lfo_freq[i] = (1.0 / lfo_samples_per_step[i]) * (1<<LFO_SH) * ym2612.OPN.ST.freqbase;
  1187. }
  1188. }
  1189. /* write a OPN register (0x30-0xff) */
  1190. static int OPNWriteReg(int r, int v)
  1191. {
  1192. int ret = 1;
  1193. FM_CH *CH;
  1194. FM_SLOT *fmSlot;
  1195. u8 c = OPN_CHAN(r);
  1196. if (c == 3) return 0; /* 0xX3,0xX7,0xXB,0xXF */
  1197. if (r >= 0x100) c+=3;
  1198. CH = &ym2612.CH[c];
  1199. fmSlot = &(CH->fmSlot[OPN_SLOT(r)]);
  1200. switch( r & 0xf0 ) {
  1201. case 0x30: /* DET , MUL */
  1202. set_det_mul(CH,fmSlot,v);
  1203. break;
  1204. case 0x40: /* TL */
  1205. set_tl(fmSlot,v);
  1206. break;
  1207. case 0x50: /* KS, AR */
  1208. set_ar_ksr(CH,fmSlot,v);
  1209. break;
  1210. case 0x60: /* bit7 = AM ENABLE, DR */
  1211. set_dr(fmSlot,v);
  1212. if(v&0x80) CH->AMmasks |= 1<<OPN_SLOT(r);
  1213. else CH->AMmasks &= ~(1<<OPN_SLOT(r));
  1214. break;
  1215. case 0x70: /* SR */
  1216. set_sr(fmSlot,v);
  1217. break;
  1218. case 0x80: /* SL, RR */
  1219. set_sl_rr(fmSlot,v);
  1220. break;
  1221. case 0x90: /* SSG-EG */
  1222. // removed.
  1223. ret = 0;
  1224. break;
  1225. case 0xa0:
  1226. switch( OPN_SLOT(r) ){
  1227. case 0: /* 0xa0-0xa2 : FNUM1 */
  1228. {
  1229. u32 fn = (((u32)( (ym2612.OPN.ST.fn_h)&7))<<8) + v;
  1230. u8 blk = ym2612.OPN.ST.fn_h>>3;
  1231. /* keyscale code */
  1232. CH->kcode = (blk<<2) | opn_fktable[fn >> 7];
  1233. /* phase increment counter */
  1234. CH->fc = fn_table[fn*2]>>(7-blk);
  1235. /* store fnum in clear form for LFO PM calculations */
  1236. CH->block_fnum = (blk<<11) | fn;
  1237. CH->fmSlot[SLOT1].Incr=-1;
  1238. }
  1239. break;
  1240. case 1: /* 0xa4-0xa6 : FNUM2,BLK */
  1241. ym2612.OPN.ST.fn_h = v&0x3f;
  1242. ret = 0;
  1243. break;
  1244. case 2: /* 0xa8-0xaa : 3CH FNUM1 */
  1245. if(r < 0x100)
  1246. {
  1247. u32 fn = (((u32)(ym2612.OPN.SL3.fn_h&7))<<8) + v;
  1248. u8 blk = ym2612.OPN.SL3.fn_h>>3;
  1249. /* keyscale code */
  1250. ym2612.OPN.SL3.kcode[c]= (blk<<2) | opn_fktable[fn >> 7];
  1251. /* phase increment counter */
  1252. ym2612.OPN.SL3.fc[c] = fn_table[fn*2]>>(7-blk);
  1253. ym2612.OPN.SL3.block_fnum[c] = fn;
  1254. ym2612.CH[2].fmSlot[SLOT1].Incr=-1;
  1255. }
  1256. break;
  1257. case 3: /* 0xac-0xae : 3CH FNUM2,BLK */
  1258. if(r < 0x100)
  1259. ym2612.OPN.SL3.fn_h = v&0x3f;
  1260. ret = 0;
  1261. break;
  1262. default:
  1263. ret = 0;
  1264. break;
  1265. }
  1266. break;
  1267. case 0xb0:
  1268. switch( OPN_SLOT(r) ){
  1269. case 0: /* 0xb0-0xb2 : FB,ALGO */
  1270. {
  1271. int feedback = (v>>3)&7;
  1272. CH->ALGO = v&7;
  1273. CH->FB = feedback ? feedback+6 : 0;
  1274. }
  1275. break;
  1276. case 1: /* 0xb4-0xb6 : L , R , AMS , PMS (YM2612/YM2610B/YM2610/YM2608) */
  1277. {
  1278. int panshift = c<<1;
  1279. /* b0-2 PMS */
  1280. CH->pms = (v & 7) * 32; /* CH->pms = PM depth * 32 (index in lfo_pm_table) */
  1281. /* b4-5 AMS */
  1282. CH->ams = lfo_ams_depth_shift[(v>>4) & 3];
  1283. /* PAN : b7 = L, b6 = R */
  1284. ym2612.OPN.pan &= ~(3<<panshift);
  1285. ym2612.OPN.pan |= ((v & 0xc0) >> 6) << panshift; // ..LRLR
  1286. }
  1287. break;
  1288. default:
  1289. ret = 0;
  1290. break;
  1291. }
  1292. break;
  1293. default:
  1294. ret = 0;
  1295. break;
  1296. }
  1297. return ret;
  1298. }
  1299. /*******************************************************************************/
  1300. /* YM2612 local section */
  1301. /*******************************************************************************/
  1302. int *ym2612_dacen;
  1303. s32 *ym2612_dacout;
  1304. FM_ST *ym2612_st;
  1305. /* Generate samples for YM2612 */
  1306. void YM2612Update(int *buffer, int length)
  1307. {
  1308. /* refresh PG and EG */
  1309. refresh_fc_eg_chan( &ym2612.CH[0] );
  1310. refresh_fc_eg_chan( &ym2612.CH[1] );
  1311. if( (ym2612.OPN.ST.mode & 0xc0) )
  1312. {
  1313. /* 3SLOT MODE */
  1314. if( ym2612.CH[2].fmSlot[SLOT1].Incr == (u32)-1)
  1315. {
  1316. refresh_fc_eg_slot(&ym2612.CH[2].fmSlot[SLOT1], ym2612.OPN.SL3.fc[1], ym2612.OPN.SL3.kcode[1] );
  1317. refresh_fc_eg_slot(&ym2612.CH[2].fmSlot[SLOT2], ym2612.OPN.SL3.fc[2], ym2612.OPN.SL3.kcode[2] );
  1318. refresh_fc_eg_slot(&ym2612.CH[2].fmSlot[SLOT3], ym2612.OPN.SL3.fc[0], ym2612.OPN.SL3.kcode[0] );
  1319. refresh_fc_eg_slot(&ym2612.CH[2].fmSlot[SLOT4], ym2612.CH[2].fc , ym2612.CH[2].kcode );
  1320. }
  1321. } else refresh_fc_eg_chan( &ym2612.CH[2] );
  1322. refresh_fc_eg_chan( &ym2612.CH[3] );
  1323. refresh_fc_eg_chan( &ym2612.CH[4] );
  1324. refresh_fc_eg_chan( &ym2612.CH[5] );
  1325. int pan = ym2612.OPN.pan;
  1326. int stereo = 1; // TODO remove, first in assembly
  1327. /* mix to 32bit dest */
  1328. // flags: stereo, ?, disabled, ?, pan_r, pan_l
  1329. if (ym2612.slot_mask & 0x00000f) chan_render(buffer, length, 0, stereo|((pan&0x003)<<4));
  1330. if (ym2612.slot_mask & 0x0000f0) chan_render(buffer, length, 1, stereo|((pan&0x00c)<<2));
  1331. if (ym2612.slot_mask & 0x000f00) chan_render(buffer, length, 2, stereo|((pan&0x030) ));
  1332. if (ym2612.slot_mask & 0x00f000) chan_render(buffer, length, 3, stereo|((pan&0x0c0)>>2));
  1333. if (ym2612.slot_mask & 0x0f0000) chan_render(buffer, length, 4, stereo|((pan&0x300)>>4));
  1334. if (ym2612.slot_mask & 0xf00000) chan_render(buffer, length, 5, stereo|((pan&0xc00)>>6)|(ym2612.dacen<<2));
  1335. }
  1336. /* initialize YM2612 emulator */
  1337. void YM2612Init(int clock, int rate)
  1338. {
  1339. // notaz
  1340. ym2612_dacen = &ym2612.dacen;
  1341. ym2612_dacout = &ym2612.dacout;
  1342. ym2612_st = &ym2612.OPN.ST;
  1343. memset(&ym2612, 0, sizeof(ym2612));
  1344. init_tables();
  1345. ym2612.OPN.ST.clock = clock;
  1346. ym2612.OPN.ST.rate = rate;
  1347. /* Extend handler */
  1348. YM2612ResetChip();
  1349. }
  1350. /* reset */
  1351. void YM2612ResetChip()
  1352. {
  1353. int i;
  1354. memset(ym2612.REGS, 0, sizeof(ym2612.REGS));
  1355. OPNSetPres( 6*24 );
  1356. set_timers( 0x30 ); /* mode 0 , timer reset */
  1357. ym2612.REGS[0x27] = 0x30;
  1358. ym2612.OPN.eg_timer = 0;
  1359. ym2612.OPN.eg_cnt = 0;
  1360. ym2612.OPN.ST.status = 0;
  1361. reset_channels( &ym2612.CH[0] );
  1362. for(i = 0xb6 ; i >= 0xb4 ; i-- )
  1363. {
  1364. OPNWriteReg(i ,0xc0);
  1365. OPNWriteReg(i|0x100,0xc0);
  1366. ym2612.REGS[i ] = 0xc0;
  1367. ym2612.REGS[i|0x100] = 0xc0;
  1368. }
  1369. for(i = 0xb2 ; i >= 0x30 ; i-- )
  1370. {
  1371. OPNWriteReg(i ,0);
  1372. OPNWriteReg(i|0x100,0);
  1373. }
  1374. for(i = 0x26 ; i >= 0x20 ; i-- ) OPNWriteReg(i,0);
  1375. /* DAC mode clear */
  1376. ym2612.dacen = 0;
  1377. ym2612.addr_A1 = 0;
  1378. }
  1379. /* YM2612 write */
  1380. /* a = address */
  1381. /* v = value */
  1382. /* returns 1 if sample affecting state changed */
  1383. int YM2612Write(unsigned int a, unsigned int v)
  1384. {
  1385. int addr, ret=1;
  1386. v &= 0xff; /* adjust to 8 bit bus */
  1387. switch( a&3){
  1388. case 0: /* address port 0 */
  1389. ym2612.OPN.ST.address = v;
  1390. ym2612.addr_A1 = 0;
  1391. ret=0;
  1392. break;
  1393. case 1: /* data port 0 */
  1394. if (ym2612.addr_A1 != 0) {
  1395. ret=0;
  1396. break; /* verified on real YM2608 */
  1397. }
  1398. addr = ym2612.OPN.ST.address;
  1399. ym2612.REGS[addr] = v;
  1400. switch( addr & 0xf0 )
  1401. {
  1402. case 0x20: /* 0x20-0x2f Mode */
  1403. switch( addr )
  1404. {
  1405. case 0x22: /* LFO FREQ (YM2608/YM2610/YM2610B/YM2612) */
  1406. if (v&0x08) /* LFO enabled ? */
  1407. {
  1408. ym2612.OPN.lfo_inc = ym2612.OPN.lfo_freq[v&7];
  1409. }
  1410. else
  1411. {
  1412. ym2612.OPN.lfo_inc = 0;
  1413. }
  1414. break;
  1415. case 0x24: { // timer A High 8
  1416. int TAnew = (ym2612.OPN.ST.TA & 0x03)|(((int)v)<<2);
  1417. if(ym2612.OPN.ST.TA != TAnew) {
  1418. // we should reset ticker only if new value is written. Outrun requires this.
  1419. ym2612.OPN.ST.TA = TAnew;
  1420. ym2612.OPN.ST.TAC = (1024-TAnew)*18;
  1421. ym2612.OPN.ST.TAT = 0;
  1422. }
  1423. }
  1424. ret=0;
  1425. break;
  1426. case 0x25: { // timer A Low 2
  1427. int TAnew = (ym2612.OPN.ST.TA & 0x3fc)|(v&3);
  1428. if(ym2612.OPN.ST.TA != TAnew) {
  1429. ym2612.OPN.ST.TA = TAnew;
  1430. ym2612.OPN.ST.TAC = (1024-TAnew)*18;
  1431. ym2612.OPN.ST.TAT = 0;
  1432. }
  1433. }
  1434. ret=0;
  1435. break;
  1436. case 0x26: // timer B
  1437. if(ym2612.OPN.ST.TB != v) {
  1438. ym2612.OPN.ST.TB = v;
  1439. ym2612.OPN.ST.TBC = (256-v)<<4;
  1440. ym2612.OPN.ST.TBC *= 18;
  1441. ym2612.OPN.ST.TBT = 0;
  1442. }
  1443. ret=0;
  1444. break;
  1445. case 0x27: /* mode, timer control */
  1446. set_timers( v );
  1447. ret=0;
  1448. break;
  1449. case 0x28: /* key on / off */
  1450. {
  1451. u8 c;
  1452. c = v & 0x03;
  1453. if( c == 3 ) { ret=0; break; }
  1454. if( v&0x04 ) c+=3;
  1455. if(v&0x10) FM_KEYON(c,SLOT1); else FM_KEYOFF(c,SLOT1);
  1456. if(v&0x20) FM_KEYON(c,SLOT2); else FM_KEYOFF(c,SLOT2);
  1457. if(v&0x40) FM_KEYON(c,SLOT3); else FM_KEYOFF(c,SLOT3);
  1458. if(v&0x80) FM_KEYON(c,SLOT4); else FM_KEYOFF(c,SLOT4);
  1459. break;
  1460. }
  1461. case 0x2a: /* DAC data (YM2612) */
  1462. ym2612.dacout = ((int)v - 0x80) << 6; /* level unknown (notaz: 8 seems to be too much) */
  1463. ret=0;
  1464. break;
  1465. case 0x2b: /* DAC Sel (YM2612) */
  1466. /* b7 = dac enable */
  1467. ym2612.dacen = v & 0x80;
  1468. ret=0;
  1469. break;
  1470. defaul

Large files files are truncated, but you can click here to view the full file