1 /*
2 **
3 ** File: ym2413.c - software implementation of YM2413
4 **                  FM sound generator type OPLL
5 **
6 ** Copyright (C) 2002 Jarek Burczynski
7 **
8 ** Version 1.0
9 **
10 **
11 
12 to do:
13 
14 - make sure of the sinus amplitude bits
15 
16 - make sure of the EG resolution bits (looks like the biggest
17   modulation index generated by the modulator is 123, 124 = no modulation)
18 - find proper algorithm for attack phase of EG
19 
20 - tune up instruments ROM
21 
22 - support sample replay in test mode (it is NOT as simple as setting bit 0
23   in register 0x0f and using register 0x10 for sample data).
24   Which games use this feature ?
25 
26 */
27 
28 /** EkeEke (2011): removed multiple chips support, cleaned code & added FM board interface for Genesis Plus GX **/
29 
30 #include "shared.h"
31 
32 #define FREQ_SH 16  /* 16.16 fixed point (frequency calculations) */
33 #define EG_SH   16  /* 16.16 fixed point (EG timing)              */
34 #define LFO_SH  24  /*  8.24 fixed point (LFO calculations)       */
35 
36 #define FREQ_MASK    ((1<<FREQ_SH)-1)
37 
38 /* envelope output entries */
39 #define ENV_BITS    10
40 #define ENV_LEN      (1<<ENV_BITS)
41 #define ENV_STEP    (128.0/ENV_LEN)
42 
43 #define MAX_ATT_INDEX  ((1<<(ENV_BITS-2))-1) /*255*/
44 #define MIN_ATT_INDEX  (0)
45 
46 /* sinwave entries */
47 #define SIN_BITS    10
48 #define SIN_LEN      (1<<SIN_BITS)
49 #define SIN_MASK    (SIN_LEN-1)
50 
51 #define TL_RES_LEN    (256)  /* 8 bits addressing (real chip) */
52 
53 /* register number to channel number , slot offset */
54 #define SLOT1 0
55 #define SLOT2 1
56 
57 /* Envelope Generator phases */
58 #define EG_DMP      5
59 #define EG_ATT      4
60 #define EG_DEC      3
61 #define EG_SUS      2
62 #define EG_REL      1
63 #define EG_OFF      0
64 
65 typedef struct
66 {
67   UINT32  ar;       /* attack rate: AR<<2           */
68   UINT32  dr;       /* decay rate:  DR<<2           */
69   UINT32  rr;       /* release rate:RR<<2           */
70   UINT8  KSR;       /* key scale rate               */
71   UINT8  ksl;       /* keyscale level               */
72   UINT8  ksr;       /* key scale rate: kcode>>KSR   */
73   UINT8  mul;       /* multiple: mul_tab[ML]        */
74 
75   /* Phase Generator */
76   UINT32 phase;     /* frequency counter            */
77   UINT32 freq;      /* frequency counter step       */
78   UINT8 fb_shift;   /* feedback shift value         */
79   INT32 op1_out[2]; /* slot1 output for feedback    */
80 
81   /* Envelope Generator */
82   UINT8  eg_type;   /* percussive/nonpercussive mode  */
83   UINT8  state;     /* phase type                     */
84   UINT32  TL;       /* total level: TL << 2           */
85   INT32  TLL;       /* adjusted now TL                */
86   INT32  volume;    /* envelope counter               */
87   UINT32  sl;       /* sustain level: sl_tab[SL]      */
88 
89   UINT8  eg_sh_dp;  /* (dump state)                   */
90   UINT8  eg_sel_dp; /* (dump state)                   */
91   UINT8  eg_sh_ar;  /* (attack state)                 */
92   UINT8  eg_sel_ar; /* (attack state)                 */
93   UINT8  eg_sh_dr;  /* (decay state)                  */
94   UINT8  eg_sel_dr; /* (decay state)                  */
95   UINT8  eg_sh_rr;  /* (release state for non-perc.)  */
96   UINT8  eg_sel_rr; /* (release state for non-perc.)  */
97   UINT8  eg_sh_rs;  /* (release state for perc.mode)  */
98   UINT8  eg_sel_rs; /* (release state for perc.mode)  */
99 
100   UINT32  key;      /* 0 = KEY OFF, >0 = KEY ON */
101 
102   /* LFO */
103   UINT32  AMmask;   /* LFO Amplitude Modulation enable mask */
104   UINT8  vib;       /* LFO Phase Modulation enable flag (active high)*/
105 
106   /* waveform select */
107   unsigned int wavetable;
108 } YM2413_OPLL_SLOT;
109 
110 typedef struct
111 {
112   YM2413_OPLL_SLOT SLOT[2];
113 
114   /* phase generator state */
115   UINT32  block_fnum;   /* block+fnum */
116   UINT32  fc;           /* Freq. freqement base */
117   UINT32  ksl_base;     /* KeyScaleLevel Base step  */
118   UINT8   kcode;        /* key code (for key scaling) */
119   UINT8   sus;          /* sus on/off (release speed in percussive mode)  */
120 } YM2413_OPLL_CH;
121 
122 /* chip state */
123 typedef struct {
124     YM2413_OPLL_CH P_CH[9];   /* OPLL chips have 9 channels */
125   UINT8  instvol_r[9];        /* instrument/volume (or volume/volume in percussive mode)  */
126 
127   UINT32  eg_cnt;             /* global envelope generator counter  */
128   UINT32  eg_timer;           /* global envelope generator counter works at frequency = chipclock/72 */
129   UINT32  eg_timer_add;       /* step of eg_timer */
130   UINT32  eg_timer_overflow;  /* envelope generator timer overlfows every 1 sample (on real chip) */
131 
132   UINT8  rhythm;              /* Rhythm mode  */
133 
134   /* LFO */
135   UINT32  lfo_am_cnt;
136   UINT32  lfo_am_inc;
137   UINT32  lfo_pm_cnt;
138   UINT32  lfo_pm_inc;
139 
140   UINT32  noise_rng;      /* 23 bit noise shift register  */
141   UINT32  noise_p;        /* current noise 'phase'  */
142   UINT32  noise_f;        /* current noise period */
143 
144 
145 /* instrument settings */
146 /*
147   0-user instrument
148   1-15 - fixed instruments
149   16 -bass drum settings
150   17,18 - other percussion instruments
151 */
152   UINT8 inst_tab[19][8];
153 
154   UINT32  fn_tab[1024];     /* fnumber->increment counter  */
155 
156   UINT8 address;          /* address register */
157   UINT8 status;          /* status flag       */
158 
159   double clock;         /* master clock  (Hz) */
160   int rate;            /* sampling rate (Hz)  */
161 } YM2413;
162 
163 /* key scale level */
164 /* table is 3dB/octave, DV converts this into 6dB/octave */
165 /* 0.1875 is bit 0 weight of the envelope counter (volume) expressed in the 'decibel' scale */
166 #define DV (0.1875/1.0)
167 static const UINT32 ksl_tab[8*16]=
168 {
169   /* OCT 0 */
170    0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
171    0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
172    0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
173    0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
174   /* OCT 1 */
175    0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
176    0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
177    0.000/DV, 0.750/DV, 1.125/DV, 1.500/DV,
178    1.875/DV, 2.250/DV, 2.625/DV, 3.000/DV,
179   /* OCT 2 */
180    0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
181    0.000/DV, 1.125/DV, 1.875/DV, 2.625/DV,
182    3.000/DV, 3.750/DV, 4.125/DV, 4.500/DV,
183    4.875/DV, 5.250/DV, 5.625/DV, 6.000/DV,
184   /* OCT 3 */
185    0.000/DV, 0.000/DV, 0.000/DV, 1.875/DV,
186    3.000/DV, 4.125/DV, 4.875/DV, 5.625/DV,
187    6.000/DV, 6.750/DV, 7.125/DV, 7.500/DV,
188    7.875/DV, 8.250/DV, 8.625/DV, 9.000/DV,
189   /* OCT 4 */
190    0.000/DV, 0.000/DV, 3.000/DV, 4.875/DV,
191    6.000/DV, 7.125/DV, 7.875/DV, 8.625/DV,
192    9.000/DV, 9.750/DV,10.125/DV,10.500/DV,
193   10.875/DV,11.250/DV,11.625/DV,12.000/DV,
194   /* OCT 5 */
195    0.000/DV, 3.000/DV, 6.000/DV, 7.875/DV,
196    9.000/DV,10.125/DV,10.875/DV,11.625/DV,
197   12.000/DV,12.750/DV,13.125/DV,13.500/DV,
198   13.875/DV,14.250/DV,14.625/DV,15.000/DV,
199   /* OCT 6 */
200    0.000/DV, 6.000/DV, 9.000/DV,10.875/DV,
201   12.000/DV,13.125/DV,13.875/DV,14.625/DV,
202   15.000/DV,15.750/DV,16.125/DV,16.500/DV,
203   16.875/DV,17.250/DV,17.625/DV,18.000/DV,
204   /* OCT 7 */
205    0.000/DV, 9.000/DV,12.000/DV,13.875/DV,
206   15.000/DV,16.125/DV,16.875/DV,17.625/DV,
207   18.000/DV,18.750/DV,19.125/DV,19.500/DV,
208   19.875/DV,20.250/DV,20.625/DV,21.000/DV
209 };
210 #undef DV
211 
212 /* sustain level table (3dB per step) */
213 /* 0 - 15: 0, 3, 6, 9,12,15,18,21,24,27,30,33,36,39,42,45 (dB)*/
214 #define SC(db) (UINT32) ( db * (1.0/ENV_STEP) )
215 static const UINT32 sl_tab[16]={
216  SC( 0),SC( 1),SC( 2),SC(3 ),SC(4 ),SC(5 ),SC(6 ),SC( 7),
217  SC( 8),SC( 9),SC(10),SC(11),SC(12),SC(13),SC(14),SC(15)
218 };
219 #undef SC
220 
221 
222 #define RATE_STEPS (8)
223 static const unsigned char eg_inc[15*RATE_STEPS]={
224 
225 /*cycle:0 1  2 3  4 5  6 7*/
226 
227 /* 0 */ 0,1, 0,1, 0,1, 0,1, /* rates 00..12 0 (increment by 0 or 1) */
228 /* 1 */ 0,1, 0,1, 1,1, 0,1, /* rates 00..12 1 */
229 /* 2 */ 0,1, 1,1, 0,1, 1,1, /* rates 00..12 2 */
230 /* 3 */ 0,1, 1,1, 1,1, 1,1, /* rates 00..12 3 */
231 
232 /* 4 */ 1,1, 1,1, 1,1, 1,1, /* rate 13 0 (increment by 1) */
233 /* 5 */ 1,1, 1,2, 1,1, 1,2, /* rate 13 1 */
234 /* 6 */ 1,2, 1,2, 1,2, 1,2, /* rate 13 2 */
235 /* 7 */ 1,2, 2,2, 1,2, 2,2, /* rate 13 3 */
236 
237 /* 8 */ 2,2, 2,2, 2,2, 2,2, /* rate 14 0 (increment by 2) */
238 /* 9 */ 2,2, 2,4, 2,2, 2,4, /* rate 14 1 */
239 /*10 */ 2,4, 2,4, 2,4, 2,4, /* rate 14 2 */
240 /*11 */ 2,4, 4,4, 2,4, 4,4, /* rate 14 3 */
241 
242 /*12 */ 4,4, 4,4, 4,4, 4,4, /* rates 15 0, 15 1, 15 2, 15 3 (increment by 4) */
243 /*13 */ 8,8, 8,8, 8,8, 8,8, /* rates 15 2, 15 3 for attack */
244 /*14 */ 0,0, 0,0, 0,0, 0,0, /* infinity rates for attack and decay(s) */
245 };
246 
247 
248 #define O(a) (a*RATE_STEPS)
249 
250 /*note that there is no O(13) in this table - it's directly in the code */
251 static const unsigned char eg_rate_select[16+64+16]={  /* Envelope Generator rates (16 + 64 rates + 16 RKS) */
252 /* 16 infinite time rates */
253 O(14),O(14),O(14),O(14),O(14),O(14),O(14),O(14),
254 O(14),O(14),O(14),O(14),O(14),O(14),O(14),O(14),
255 
256 /* rates 00-12 */
257 O( 0),O( 1),O( 2),O( 3),
258 O( 0),O( 1),O( 2),O( 3),
259 O( 0),O( 1),O( 2),O( 3),
260 O( 0),O( 1),O( 2),O( 3),
261 O( 0),O( 1),O( 2),O( 3),
262 O( 0),O( 1),O( 2),O( 3),
263 O( 0),O( 1),O( 2),O( 3),
264 O( 0),O( 1),O( 2),O( 3),
265 O( 0),O( 1),O( 2),O( 3),
266 O( 0),O( 1),O( 2),O( 3),
267 O( 0),O( 1),O( 2),O( 3),
268 O( 0),O( 1),O( 2),O( 3),
269 O( 0),O( 1),O( 2),O( 3),
270 
271 /* rate 13 */
272 O( 4),O( 5),O( 6),O( 7),
273 
274 /* rate 14 */
275 O( 8),O( 9),O(10),O(11),
276 
277 /* rate 15 */
278 O(12),O(12),O(12),O(12),
279 
280 /* 16 dummy rates (same as 15 3) */
281 O(12),O(12),O(12),O(12),O(12),O(12),O(12),O(12),
282 O(12),O(12),O(12),O(12),O(12),O(12),O(12),O(12),
283 
284 };
285 #undef O
286 
287 /*rate  0,    1,    2,    3,    4,   5,   6,   7,  8,  9, 10, 11, 12, 13, 14, 15 */
288 /*shift 13,   12,   11,   10,   9,   8,   7,   6,  5,  4,  3,  2,  1,  0,  0,  0 */
289 /*mask  8191, 4095, 2047, 1023, 511, 255, 127, 63, 31, 15, 7,  3,  1,  0,  0,  0 */
290 
291 #define O(a) (a*1)
292 static const unsigned char eg_rate_shift[16+64+16]={  /* Envelope Generator counter shifts (16 + 64 rates + 16 RKS) */
293 /* 16 infinite time rates */
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 
297 /* rates 00-12 */
298 O(13),O(13),O(13),O(13),
299 O(12),O(12),O(12),O(12),
300 O(11),O(11),O(11),O(11),
301 O(10),O(10),O(10),O(10),
302 O( 9),O( 9),O( 9),O( 9),
303 O( 8),O( 8),O( 8),O( 8),
304 O( 7),O( 7),O( 7),O( 7),
305 O( 6),O( 6),O( 6),O( 6),
306 O( 5),O( 5),O( 5),O( 5),
307 O( 4),O( 4),O( 4),O( 4),
308 O( 3),O( 3),O( 3),O( 3),
309 O( 2),O( 2),O( 2),O( 2),
310 O( 1),O( 1),O( 1),O( 1),
311 
312 /* rate 13 */
313 O( 0),O( 0),O( 0),O( 0),
314 
315 /* rate 14 */
316 O( 0),O( 0),O( 0),O( 0),
317 
318 /* rate 15 */
319 O( 0),O( 0),O( 0),O( 0),
320 
321 /* 16 dummy rates (same as 15 3) */
322 O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),
323 O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),
324 
325 };
326 #undef O
327 
328 
329 /* multiple table */
330 #define ML 2
331 static const UINT8 mul_tab[16]= {
332 /* 1/2, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,10,12,12,15,15 */
333    0.50*ML, 1.00*ML, 2.00*ML, 3.00*ML, 4.00*ML, 5.00*ML, 6.00*ML, 7.00*ML,
334    8.00*ML, 9.00*ML,10.00*ML,10.00*ML,12.00*ML,12.00*ML,15.00*ML,15.00*ML
335 };
336 #undef ML
337 
338 /*  TL_TAB_LEN is calculated as:
339 *  11 - sinus amplitude bits     (Y axis)
340 *  2  - sinus sign bit           (Y axis)
341 *  TL_RES_LEN - sinus resolution (X axis)
342 */
343 #define TL_TAB_LEN (11*2*TL_RES_LEN)
344 static signed int tl_tab[TL_TAB_LEN];
345 
346 #define ENV_QUIET    (TL_TAB_LEN>>5)
347 
348 /* sin waveform table in 'decibel' scale */
349 /* two waveforms on OPLL type chips */
350 static unsigned int sin_tab[SIN_LEN * 2];
351 
352 
353 /* LFO Amplitude Modulation table (verified on real YM3812)
354    27 output levels (triangle waveform); 1 level takes one of: 192, 256 or 448 samples
355 
356    Length: 210 elements.
357 
358   Each of the elements has to be repeated
359   exactly 64 times (on 64 consecutive samples).
360   The whole table takes: 64 * 210 = 13440 samples.
361 
362 We use data>>1, until we find what it really is on real chip...
363 
364 */
365 
366 #define LFO_AM_TAB_ELEMENTS 210
367 
368 static const UINT8 lfo_am_table[LFO_AM_TAB_ELEMENTS] = {
369 0,0,0,0,0,0,0,
370 1,1,1,1,
371 2,2,2,2,
372 3,3,3,3,
373 4,4,4,4,
374 5,5,5,5,
375 6,6,6,6,
376 7,7,7,7,
377 8,8,8,8,
378 9,9,9,9,
379 10,10,10,10,
380 11,11,11,11,
381 12,12,12,12,
382 13,13,13,13,
383 14,14,14,14,
384 15,15,15,15,
385 16,16,16,16,
386 17,17,17,17,
387 18,18,18,18,
388 19,19,19,19,
389 20,20,20,20,
390 21,21,21,21,
391 22,22,22,22,
392 23,23,23,23,
393 24,24,24,24,
394 25,25,25,25,
395 26,26,26,
396 25,25,25,25,
397 24,24,24,24,
398 23,23,23,23,
399 22,22,22,22,
400 21,21,21,21,
401 20,20,20,20,
402 19,19,19,19,
403 18,18,18,18,
404 17,17,17,17,
405 16,16,16,16,
406 15,15,15,15,
407 14,14,14,14,
408 13,13,13,13,
409 12,12,12,12,
410 11,11,11,11,
411 10,10,10,10,
412 9,9,9,9,
413 8,8,8,8,
414 7,7,7,7,
415 6,6,6,6,
416 5,5,5,5,
417 4,4,4,4,
418 3,3,3,3,
419 2,2,2,2,
420 1,1,1,1
421 };
422 
423 /* LFO Phase Modulation table (verified on real YM2413) */
424 static const INT8 lfo_pm_table[8*8] = {
425 
426 /* FNUM2/FNUM = 0 00xxxxxx (0x0000) */
427 0, 0, 0, 0, 0, 0, 0, 0,
428 
429 /* FNUM2/FNUM = 0 01xxxxxx (0x0040) */
430 1, 0, 0, 0,-1, 0, 0, 0,
431 
432 /* FNUM2/FNUM = 0 10xxxxxx (0x0080) */
433 2, 1, 0,-1,-2,-1, 0, 1,
434 
435 /* FNUM2/FNUM = 0 11xxxxxx (0x00C0) */
436 3, 1, 0,-1,-3,-1, 0, 1,
437 
438 /* FNUM2/FNUM = 1 00xxxxxx (0x0100) */
439 4, 2, 0,-2,-4,-2, 0, 2,
440 
441 /* FNUM2/FNUM = 1 01xxxxxx (0x0140) */
442 5, 2, 0,-2,-5,-2, 0, 2,
443 
444 /* FNUM2/FNUM = 1 10xxxxxx (0x0180) */
445 6, 3, 0,-3,-6,-3, 0, 3,
446 
447 /* FNUM2/FNUM = 1 11xxxxxx (0x01C0) */
448 7, 3, 0,-3,-7,-3, 0, 3,
449 };
450 
451 
452 /* This is not 100% perfect yet but very close */
453 /*
454  - multi parameters are 100% correct (instruments and drums)
455  - LFO PM and AM enable are 100% correct
456  - waveform DC and DM select are 100% correct
457 */
458 
459 static unsigned char table[19][8] = {
460 /* MULT  MULT modTL DcDmFb AR/DR AR/DR SL/RR SL/RR */
461 /*   0     1     2     3     4     5     6    7    */
462   {0x49, 0x4c, 0x4c, 0x12, 0x00, 0x00, 0x00, 0x00 },  /* 0 */
463 
464   {0x61, 0x61, 0x1e, 0x17, 0xf0, 0x78, 0x00, 0x17 },  /* 1 */
465   {0x13, 0x41, 0x1e, 0x0d, 0xd7, 0xf7, 0x13, 0x13 },  /* 2 */
466   {0x13, 0x01, 0x99, 0x04, 0xf2, 0xf4, 0x11, 0x23 },  /* 3 */
467   {0x21, 0x61, 0x1b, 0x07, 0xaf, 0x64, 0x40, 0x27 },  /* 4 */
468 
469 /*{0x22, 0x21, 0x1e, 0x09, 0xf0, 0x76, 0x08, 0x28 },  */ /* 5 */
470   {0x22, 0x21, 0x1e, 0x06, 0xf0, 0x75, 0x08, 0x18 },  /* 5 */
471 
472 /*{0x31, 0x22, 0x16, 0x09, 0x90, 0x7f, 0x00, 0x08 },  */ /* 6 */
473   {0x31, 0x22, 0x16, 0x05, 0x90, 0x71, 0x00, 0x13 },  /* 6 */
474 
475   {0x21, 0x61, 0x1d, 0x07, 0x82, 0x80, 0x10, 0x17 },  /* 7 */
476   {0x23, 0x21, 0x2d, 0x16, 0xc0, 0x70, 0x07, 0x07 },  /* 8 */
477   {0x61, 0x61, 0x1b, 0x06, 0x64, 0x65, 0x10, 0x17 },  /* 9 */
478 
479 /* {0x61, 0x61, 0x0c, 0x08, 0x85, 0xa0, 0x79, 0x07 },  */ /* A */
480   {0x61, 0x61, 0x0c, 0x18, 0x85, 0xf0, 0x70, 0x07 },  /* A */
481 
482   {0x23, 0x01, 0x07, 0x11, 0xf0, 0xa4, 0x00, 0x22 },  /* B */
483   {0x97, 0xc1, 0x24, 0x07, 0xff, 0xf8, 0x22, 0x12 },  /* C */
484 
485 /* {0x61, 0x10, 0x0c, 0x08, 0xf2, 0xc4, 0x40, 0xc8 },  */ /* D */
486   {0x61, 0x10, 0x0c, 0x05, 0xf2, 0xf4, 0x40, 0x44 },  /* D */
487 
488   {0x01, 0x01, 0x55, 0x03, 0xf3, 0x92, 0xf3, 0xf3 },  /* E */
489   {0x61, 0x41, 0x89, 0x03, 0xf1, 0xf4, 0xf0, 0x13 },  /* F */
490 
491 /* drum instruments definitions */
492 /* MULTI MULTI modTL  xxx  AR/DR AR/DR SL/RR SL/RR */
493 /*   0     1     2     3     4     5     6    7    */
494   {0x01, 0x01, 0x16, 0x00, 0xfd, 0xf8, 0x2f, 0x6d },/* BD(multi verified, modTL verified, mod env - verified(close), carr. env verifed) */
495   {0x01, 0x01, 0x00, 0x00, 0xd8, 0xd8, 0xf9, 0xf8 },/* HH(multi verified), SD(multi not used) */
496   {0x05, 0x01, 0x00, 0x00, 0xf8, 0xba, 0x49, 0x55 },/* TOM(multi,env verified), TOP CYM(multi verified, env verified) */
497 };
498 
499 static signed int output[2];
500 
501 static UINT32  LFO_AM;
502 static INT32  LFO_PM;
503 
504 /* emulated chip */
505 static YM2413 ym2413;
506 
507 /* advance LFO to next sample */
advance_lfo(void)508 INLINE void advance_lfo(void)
509 {
510   /* LFO */
511   ym2413.lfo_am_cnt += ym2413.lfo_am_inc;
512   if (ym2413.lfo_am_cnt >= (LFO_AM_TAB_ELEMENTS<<LFO_SH) )  /* lfo_am_table is 210 elements long */
513     ym2413.lfo_am_cnt -= (LFO_AM_TAB_ELEMENTS<<LFO_SH);
514 
515   LFO_AM = lfo_am_table[ ym2413.lfo_am_cnt >> LFO_SH ] >> 1;
516 
517   ym2413.lfo_pm_cnt += ym2413.lfo_pm_inc;
518   LFO_PM = (ym2413.lfo_pm_cnt>>LFO_SH) & 7;
519 }
520 
521 /* advance to next sample */
advance(void)522 INLINE void advance(void)
523 {
524   YM2413_OPLL_CH *CH;
525   YM2413_OPLL_SLOT *op;
526   unsigned int i;
527 
528   /* Envelope Generator */
529   ym2413.eg_timer += ym2413.eg_timer_add;
530 
531   while (ym2413.eg_timer >= ym2413.eg_timer_overflow)
532   {
533     ym2413.eg_timer -= ym2413.eg_timer_overflow;
534 
535     ym2413.eg_cnt++;
536 
537     for (i=0; i<9*2; i++)
538     {
539       CH  = &ym2413.P_CH[i>>1];
540 
541       op  = &CH->SLOT[i&1];
542 
543       switch(op->state)
544       {
545         case EG_DMP:    /* dump phase */
546         /*dump phase is performed by both operators in each channel*/
547         /*when CARRIER envelope gets down to zero level,
548         **  phases in BOTH opearators are reset (at the same time ?)
549         */
550           if ( !(ym2413.eg_cnt & ((1<<op->eg_sh_dp)-1) ) )
551           {
552             op->volume += eg_inc[op->eg_sel_dp + ((ym2413.eg_cnt>>op->eg_sh_dp)&7)];
553 
554             if ( op->volume >= MAX_ATT_INDEX )
555             {
556               op->volume = MAX_ATT_INDEX;
557               op->state = EG_ATT;
558               /* restart Phase Generator  */
559               op->phase = 0;
560             }
561           }
562           break;
563 
564         case EG_ATT:    /* attack phase */
565           if ( !(ym2413.eg_cnt & ((1<<op->eg_sh_ar)-1) ) )
566           {
567             op->volume += (~op->volume *
568                                            (eg_inc[op->eg_sel_ar + ((ym2413.eg_cnt>>op->eg_sh_ar)&7)])
569                                           ) >>2;
570 
571             if (op->volume <= MIN_ATT_INDEX)
572             {
573               op->volume = MIN_ATT_INDEX;
574               op->state = EG_DEC;
575             }
576           }
577           break;
578 
579         case EG_DEC:  /* decay phase */
580           if ( !(ym2413.eg_cnt & ((1<<op->eg_sh_dr)-1) ) )
581           {
582             op->volume += eg_inc[op->eg_sel_dr + ((ym2413.eg_cnt>>op->eg_sh_dr)&7)];
583 
584             if ( op->volume >= op->sl )
585               op->state = EG_SUS;
586           }
587           break;
588 
589         case EG_SUS:  /* sustain phase */
590           /* this is important behaviour:
591           one can change percusive/non-percussive modes on the fly and
592           the chip will remain in sustain phase - verified on real YM3812 */
593 
594           if(op->eg_type)    /* non-percussive mode (sustained tone) */
595           {
596                     /* do nothing */
597           }
598           else        /* percussive mode */
599           {
600             /* during sustain phase chip adds Release Rate (in percussive mode) */
601             if ( !(ym2413.eg_cnt & ((1<<op->eg_sh_rr)-1) ) )
602             {
603               op->volume += eg_inc[op->eg_sel_rr + ((ym2413.eg_cnt>>op->eg_sh_rr)&7)];
604 
605               if ( op->volume >= MAX_ATT_INDEX )
606                 op->volume = MAX_ATT_INDEX;
607             }
608             /* else do nothing in sustain phase */
609           }
610           break;
611 
612         case EG_REL:  /* release phase */
613         /* exclude modulators in melody channels from performing anything in this mode*/
614         /* allowed are only carriers in melody mode and rhythm slots in rhythm mode */
615 
616         /*This table shows which operators and on what conditions are allowed to perform EG_REL:
617         (a) - always perform EG_REL
618         (n) - never perform EG_REL
619         (r) - perform EG_REL in Rhythm mode ONLY
620           0: 0 (n),  1 (a)
621           1: 2 (n),  3 (a)
622           2: 4 (n),  5 (a)
623           3: 6 (n),  7 (a)
624           4: 8 (n),  9 (a)
625           5: 10(n),  11(a)
626           6: 12(r),  13(a)
627           7: 14(r),  15(a)
628           8: 16(r),  17(a)
629         */
630           if ( (i&1) || ((ym2413.rhythm&0x20) && (i>=12)) )/* exclude modulators */
631           {
632             if(op->eg_type)    /* non-percussive mode (sustained tone) */
633             /*this is correct: use RR when SUS = OFF*/
634             /*and use RS when SUS = ON*/
635             {
636               if (CH->sus)
637               {
638                 if ( !(ym2413.eg_cnt & ((1<<op->eg_sh_rs)-1) ) )
639                 {
640                   op->volume += eg_inc[op->eg_sel_rs + ((ym2413.eg_cnt>>op->eg_sh_rs)&7)];
641                   if ( op->volume >= MAX_ATT_INDEX )
642                   {
643                     op->volume = MAX_ATT_INDEX;
644                     op->state = EG_OFF;
645                   }
646                 }
647               }
648               else
649               {
650                 if ( !(ym2413.eg_cnt & ((1<<op->eg_sh_rr)-1) ) )
651                 {
652                   op->volume += eg_inc[op->eg_sel_rr + ((ym2413.eg_cnt>>op->eg_sh_rr)&7)];
653                   if ( op->volume >= MAX_ATT_INDEX )
654                   {
655                     op->volume = MAX_ATT_INDEX;
656                     op->state = EG_OFF;
657                   }
658                 }
659               }
660             }
661             else        /* percussive mode */
662             {
663               if ( !(ym2413.eg_cnt & ((1<<op->eg_sh_rs)-1) ) )
664               {
665                 op->volume += eg_inc[op->eg_sel_rs + ((ym2413.eg_cnt>>op->eg_sh_rs)&7)];
666                 if ( op->volume >= MAX_ATT_INDEX )
667                 {
668                   op->volume = MAX_ATT_INDEX;
669                   op->state = EG_OFF;
670                 }
671               }
672             }
673           }
674           break;
675 
676       default:
677       break;
678       }
679     }
680   }
681 
682   for (i=0; i<9*2; i++)
683   {
684     CH  = &ym2413.P_CH[i/2];
685     op  = &CH->SLOT[i&1];
686 
687     /* Phase Generator */
688     if(op->vib)
689     {
690       UINT8 block;
691 
692       unsigned int fnum_lfo   = 8*((CH->block_fnum&0x01c0) >> 6);
693       unsigned int block_fnum = CH->block_fnum * 2;
694       signed int lfo_fn_table_index_offset = lfo_pm_table[LFO_PM + fnum_lfo ];
695 
696       if (lfo_fn_table_index_offset)  /* LFO phase modulation active */
697       {
698         block_fnum += lfo_fn_table_index_offset;
699         block = (block_fnum&0x1c00) >> 10;
700         op->phase += (ym2413.fn_tab[block_fnum&0x03ff] >> (7-block)) * op->mul;
701       }
702       else  /* LFO phase modulation  = zero */
703       {
704         op->phase += op->freq;
705       }
706     }
707     else  /* LFO phase modulation disabled for this operator */
708     {
709       op->phase += op->freq;
710     }
711   }
712 
713   /*  The Noise Generator of the YM3812 is 23-bit shift register.
714   *  Period is equal to 2^23-2 samples.
715   *  Register works at sampling frequency of the chip, so output
716   *  can change on every sample.
717   *
718   *  Output of the register and input to the bit 22 is:
719   *  bit0 XOR bit14 XOR bit15 XOR bit22
720   *
721   *  Simply use bit 22 as the noise output.
722   */
723 
724   ym2413.noise_p += ym2413.noise_f;
725   i = ym2413.noise_p >> FREQ_SH;    /* number of events (shifts of the shift register) */
726   ym2413.noise_p &= FREQ_MASK;
727   while (i)
728   {
729     /*
730     UINT32 j;
731     j = ( (chip->noise_rng) ^ (chip->noise_rng>>14) ^ (chip->noise_rng>>15) ^ (chip->noise_rng>>22) ) & 1;
732     chip->noise_rng = (j<<22) | (chip->noise_rng>>1);
733     */
734 
735     /*
736       Instead of doing all the logic operations above, we
737       use a trick here (and use bit 0 as the noise output).
738       The difference is only that the noise bit changes one
739       step ahead. This doesn't matter since we don't know
740       what is real state of the noise_rng after the reset.
741     */
742 
743     if (ym2413.noise_rng & 1) ym2413.noise_rng ^= 0x800302;
744     ym2413.noise_rng >>= 1;
745 
746     i--;
747   }
748 }
749 
750 
op_calc(UINT32 phase,unsigned int env,signed int pm,unsigned int wave_tab)751 INLINE signed int op_calc(UINT32 phase, unsigned int env, signed int pm, unsigned int wave_tab)
752 {
753   UINT32 p = (env<<5) + sin_tab[wave_tab + ((((signed int)((phase & ~FREQ_MASK) + (pm<<17))) >> FREQ_SH ) & SIN_MASK) ];
754 
755   if (p >= TL_TAB_LEN)
756     return 0;
757   return tl_tab[p];
758 }
759 
op_calc1(UINT32 phase,unsigned int env,signed int pm,unsigned int wave_tab)760 INLINE signed int op_calc1(UINT32 phase, unsigned int env, signed int pm, unsigned int wave_tab)
761 {
762   UINT32 p = (env<<5) + sin_tab[wave_tab + ((((signed int)((phase & ~FREQ_MASK) + pm)) >> FREQ_SH ) & SIN_MASK) ];
763 
764   if (p >= TL_TAB_LEN)
765     return 0;
766   return tl_tab[p];
767 }
768 
769 #define volume_calc(OP) ((OP)->TLL + ((UINT32)(OP)->volume) + (LFO_AM & (OP)->AMmask))
770 
771 /* calculate output */
chan_calc(YM2413_OPLL_CH * CH,unsigned int chan)772 INLINE void chan_calc( YM2413_OPLL_CH *CH, unsigned int chan )
773 {
774   YM2413_OPLL_SLOT *SLOT;
775   unsigned int env;
776   signed int out;
777   signed int phase_modulation;  /* phase modulation input (SLOT 2) */
778 
779   /* SLOT 1 */
780   SLOT = &CH->SLOT[SLOT1];
781   env  = volume_calc(SLOT);
782   out  = SLOT->op1_out[0] + SLOT->op1_out[1];
783 
784   SLOT->op1_out[0] = SLOT->op1_out[1];
785   phase_modulation = SLOT->op1_out[0];
786 
787   SLOT->op1_out[1] = 0;
788 
789   if( env < ENV_QUIET )
790   {
791     if (!SLOT->fb_shift)
792       out = 0;
793     SLOT->op1_out[1] = op_calc1(SLOT->phase, env, (out<<SLOT->fb_shift), SLOT->wavetable );
794   }
795 
796   /* SLOT 2 */
797 
798   SLOT++;
799   env = volume_calc(SLOT);
800   if( env < ENV_QUIET )
801   {
802       output[0] += op_calc(SLOT->phase, env, phase_modulation, SLOT->wavetable)
803 #ifndef USE_PER_SOUND_CHANNELS_CONFIG
804     ;
805 #else
806     /* apply user-set volume scaling */
807     * config.sms_fm_ch_volumes[chan]/100;
808 #endif
809   }
810 }
811 
812 /*
813   operators used in the rhythm sounds generation process:
814 
815   Envelope Generator:
816 
817 channel  operator  register number   Bass  High  Snare Tom  Top
818 / slot   number    TL ARDR SLRR Wave Drum  Hat   Drum  Tom  Cymbal
819  6 / 0   12        50  70   90   f0  +
820  6 / 1   15        53  73   93   f3  +
821  7 / 0   13        51  71   91   f1        +
822  7 / 1   16        54  74   94   f4              +
823  8 / 0   14        52  72   92   f2                    +
824  8 / 1   17        55  75   95   f5                          +
825 
826   Phase Generator:
827 
828 channel  operator  register number   Bass  High  Snare Tom  Top
829 / slot   number    MULTIPLE          Drum  Hat   Drum  Tom  Cymbal
830  6 / 0   12        30                +
831  6 / 1   15        33                +
832  7 / 0   13        31                      +     +           +
833  7 / 1   16        34                -----  n o t  u s e d -----
834  8 / 0   14        32                                  +
835  8 / 1   17        35                      +                 +
836 
837 channel  operator  register number   Bass  High  Snare Tom  Top
838 number   number    BLK/FNUM2 FNUM    Drum  Hat   Drum  Tom  Cymbal
839    6     12,15     B6        A6      +
840 
841    7     13,16     B7        A7            +     +           +
842 
843    8     14,17     B8        A8            +           +     +
844 
845 */
846 
847 /* calculate rhythm */
848 
rhythm_calc(YM2413_OPLL_CH * CH,unsigned int noise)849 INLINE void rhythm_calc( YM2413_OPLL_CH *CH, unsigned int noise )
850 {
851   YM2413_OPLL_SLOT *SLOT;
852   signed int out;
853   unsigned int env;
854   signed int phase_modulation;  /* phase modulation input (SLOT 2) */
855 
856 
857   /* Bass Drum (verified on real YM3812):
858     - depends on the channel 6 'connect' register:
859         when connect = 0 it works the same as in normal (non-rhythm) mode (op1->op2->out)
860         when connect = 1 _only_ operator 2 is present on output (op2->out), operator 1 is ignored
861     - output sample always is multiplied by 2
862   */
863 
864 
865   /* SLOT 1 */
866   SLOT = &CH[6].SLOT[SLOT1];
867   env = volume_calc(SLOT);
868 
869   out = SLOT->op1_out[0] + SLOT->op1_out[1];
870   SLOT->op1_out[0] = SLOT->op1_out[1];
871 
872   phase_modulation = SLOT->op1_out[0];
873 
874   SLOT->op1_out[1] = 0;
875   if( env < ENV_QUIET )
876   {
877     if (!SLOT->fb_shift)
878       out = 0;
879     SLOT->op1_out[1] = op_calc1(SLOT->phase, env, (out<<SLOT->fb_shift), SLOT->wavetable );
880   }
881 
882   /* SLOT 2 */
883   SLOT++;
884   env = volume_calc(SLOT);
885   if( env < ENV_QUIET )
886     output[1] += op_calc(SLOT->phase, env, phase_modulation, SLOT->wavetable)
887 #ifndef USE_PER_SOUND_CHANNELS_CONFIG
888     ;
889 #else
890     /* apply user-set volume scaling */
891     * config.sms_fm_ch_volumes[6]/100;
892 #endif
893 
894 
895   /* Phase generation is based on: */
896   /* HH  (13) channel 7->slot 1 combined with channel 8->slot 2 (same combination as TOP CYMBAL but different output phases) */
897   /* SD  (16) channel 7->slot 1 */
898   /* TOM (14) channel 8->slot 1 */
899   /* TOP (17) channel 7->slot 1 combined with channel 8->slot 2 (same combination as HIGH HAT but different output phases) */
900 
901   /* Envelope generation based on: */
902   /* HH  channel 7->slot1 */
903   /* SD  channel 7->slot2 */
904   /* TOM channel 8->slot1 */
905   /* TOP channel 8->slot2 */
906 
907 
908   /* The following formulas can be well optimized.
909      I leave them in direct form for now (in case I've missed something).
910   */
911 
912   /* High Hat (verified on real YM3812) */
913   env = volume_calc(&CH[7].SLOT[SLOT1]);
914   if( env < ENV_QUIET )
915   {
916 
917     /* high hat phase generation:
918       phase = d0 or 234 (based on frequency only)
919       phase = 34 or 2d0 (based on noise)
920     */
921 
922     /* base frequency derived from operator 1 in channel 7 */
923     unsigned char bit7 = ((CH[7].SLOT[SLOT1].phase>>FREQ_SH)>>7)&1;
924     unsigned char bit3 = ((CH[7].SLOT[SLOT1].phase>>FREQ_SH)>>3)&1;
925     unsigned char bit2 = ((CH[7].SLOT[SLOT1].phase>>FREQ_SH)>>2)&1;
926 
927     unsigned char res1 = (bit2 ^ bit7) | bit3;
928 
929     /* when res1 = 0 phase = 0x000 | 0xd0; */
930     /* when res1 = 1 phase = 0x200 | (0xd0>>2); */
931     UINT32 phase = res1 ? (0x200|(0xd0>>2)) : 0xd0;
932 
933     /* enable gate based on frequency of operator 2 in channel 8 */
934     unsigned char bit5e= ((CH[8].SLOT[SLOT2].phase>>FREQ_SH)>>5)&1;
935     unsigned char bit3e= ((CH[8].SLOT[SLOT2].phase>>FREQ_SH)>>3)&1;
936 
937     unsigned char res2 = (bit3e | bit5e);
938 
939     /* when res2 = 0 pass the phase from calculation above (res1); */
940     /* when res2 = 1 phase = 0x200 | (0xd0>>2); */
941     if (res2)
942       phase = (0x200|(0xd0>>2));
943 
944 
945     /* when phase & 0x200 is set and noise=1 then phase = 0x200|0xd0 */
946     /* when phase & 0x200 is set and noise=0 then phase = 0x200|(0xd0>>2), ie no change */
947     if (phase&0x200)
948     {
949       if (noise)
950         phase = 0x200|0xd0;
951     }
952     else
953     /* when phase & 0x200 is clear and noise=1 then phase = 0xd0>>2 */
954     /* when phase & 0x200 is clear and noise=0 then phase = 0xd0, ie no change */
955     {
956       if (noise)
957         phase = 0xd0>>2;
958     }
959 
960     output[1] += op_calc(phase<<FREQ_SH, env, 0, CH[7].SLOT[SLOT1].wavetable)
961 #ifndef USE_PER_SOUND_CHANNELS_CONFIG
962     ;
963 #else
964     /* apply user-set volume scaling */
965     * config.sms_fm_ch_volumes[7]/100;
966 #endif
967   }
968 
969   /* Snare Drum (verified on real YM3812) */
970   env = volume_calc(&CH[7].SLOT[SLOT2]);
971   if( env < ENV_QUIET )
972   {
973     /* base frequency derived from operator 1 in channel 7 */
974     unsigned char bit8 = ((CH[7].SLOT[SLOT1].phase>>FREQ_SH)>>8)&1;
975 
976     /* when bit8 = 0 phase = 0x100; */
977     /* when bit8 = 1 phase = 0x200; */
978     UINT32 phase = bit8 ? 0x200 : 0x100;
979 
980     /* Noise bit XOR'es phase by 0x100 */
981     /* when noisebit = 0 pass the phase from calculation above */
982     /* when noisebit = 1 phase ^= 0x100; */
983     /* in other words: phase ^= (noisebit<<8); */
984     if (noise)
985       phase ^= 0x100;
986 
987     output[1] += op_calc(phase<<FREQ_SH, env, 0, CH[7].SLOT[SLOT2].wavetable)
988 #ifndef USE_PER_SOUND_CHANNELS_CONFIG
989     ;
990 #else
991     /* apply user-set volume scaling */
992     * config.sms_fm_ch_volumes[7]/100;
993 #endif
994   }
995 
996   /* Tom Tom (verified on real YM3812) */
997   env = volume_calc(&CH[8].SLOT[SLOT1]);
998   if( env < ENV_QUIET )
999     output[1] += op_calc(CH[8].SLOT[SLOT1].phase, env, 0, CH[8].SLOT[SLOT1].wavetable)
1000 #ifndef USE_PER_SOUND_CHANNELS_CONFIG
1001     ;
1002 #else
1003     /* apply user-set volume scaling */
1004     * config.sms_fm_ch_volumes[8]/100;
1005 #endif
1006 
1007   /* Top Cymbal (verified on real YM2413) */
1008   env = volume_calc(&CH[8].SLOT[SLOT2]);
1009   if( env < ENV_QUIET )
1010   {
1011     /* base frequency derived from operator 1 in channel 7 */
1012     unsigned char bit7 = ((CH[7].SLOT[SLOT1].phase>>FREQ_SH)>>7)&1;
1013     unsigned char bit3 = ((CH[7].SLOT[SLOT1].phase>>FREQ_SH)>>3)&1;
1014     unsigned char bit2 = ((CH[7].SLOT[SLOT1].phase>>FREQ_SH)>>2)&1;
1015 
1016     unsigned char res1 = (bit2 ^ bit7) | bit3;
1017 
1018     /* when res1 = 0 phase = 0x000 | 0x100; */
1019     /* when res1 = 1 phase = 0x200 | 0x100; */
1020     UINT32 phase = res1 ? 0x300 : 0x100;
1021 
1022     /* enable gate based on frequency of operator 2 in channel 8 */
1023     unsigned char bit5e= ((CH[8].SLOT[SLOT2].phase>>FREQ_SH)>>5)&1;
1024     unsigned char bit3e= ((CH[8].SLOT[SLOT2].phase>>FREQ_SH)>>3)&1;
1025 
1026     unsigned char res2 = (bit3e | bit5e);
1027     /* when res2 = 0 pass the phase from calculation above (res1); */
1028     /* when res2 = 1 phase = 0x200 | 0x100; */
1029     if (res2)
1030       phase = 0x300;
1031 
1032     output[1] += op_calc(phase<<FREQ_SH, env, 0, CH[8].SLOT[SLOT2].wavetable)
1033 #ifndef USE_PER_SOUND_CHANNELS_CONFIG
1034     ;
1035 #else
1036     /* apply user-set volume scaling */
1037     * config.sms_fm_ch_volumes[8]/100;
1038 #endif
1039   }
1040 }
1041 
1042 
1043 /* generic table initialize */
init_tables(void)1044 static int init_tables(void)
1045 {
1046   signed int i,x;
1047   signed int n;
1048   double o,m;
1049 
1050   for (x=0; x<TL_RES_LEN; x++)
1051   {
1052     m = (1<<16) / pow(2, (x+1) * (ENV_STEP/4.0) / 8.0);
1053     m = floor(m);
1054 
1055     /* we never reach (1<<16) here due to the (x+1) */
1056     /* result fits within 16 bits at maximum */
1057 
1058     n = (int)m;    /* 16 bits here */
1059     n >>= 4;    /* 12 bits here */
1060     if (n&1)    /* round to nearest */
1061       n = (n>>1)+1;
1062     else
1063       n = n>>1;
1064             /* 11 bits here (rounded) */
1065     tl_tab[ x*2 + 0 ] = n;
1066     tl_tab[ x*2 + 1 ] = -tl_tab[ x*2 + 0 ];
1067 
1068     for (i=1; i<11; i++)
1069     {
1070       tl_tab[ x*2+0 + i*2*TL_RES_LEN ] =  tl_tab[ x*2+0 ]>>i;
1071       tl_tab[ x*2+1 + i*2*TL_RES_LEN ] = -tl_tab[ x*2+0 + i*2*TL_RES_LEN ];
1072     }
1073   }
1074 
1075   for (i=0; i<SIN_LEN; i++)
1076   {
1077     /* non-standard sinus */
1078     m = sin( ((i*2)+1) * M_PI / SIN_LEN ); /* checked against the real chip */
1079 
1080     /* we never reach zero here due to ((i*2)+1) */
1081 
1082     if (m>0.0)
1083       o = 8*log(1.0/m)/log(2);  /* convert to 'decibels' */
1084     else
1085       o = 8*log(-1.0/m)/log(2);  /* convert to 'decibels' */
1086 
1087     o = o / (ENV_STEP/4);
1088 
1089     n = (int)(2.0*o);
1090     if (n&1)            /* round to nearest */
1091       n = (n>>1)+1;
1092     else
1093       n = n>>1;
1094 
1095     /* waveform 0: standard sinus  */
1096     sin_tab[ i ] = n*2 + (m>=0.0? 0: 1 );
1097 
1098     /* waveform 1:  __      __     */
1099     /*             /  \____/  \____*/
1100     /* output only first half of the sinus waveform (positive one) */
1101     if (i & (1<<(SIN_BITS-1)) )
1102       sin_tab[1*SIN_LEN+i] = TL_TAB_LEN;
1103     else
1104       sin_tab[1*SIN_LEN+i] = sin_tab[i];
1105   }
1106 
1107   return 1;
1108 }
1109 
1110 
OPLL_initalize(void)1111 static void OPLL_initalize(void)
1112 {
1113   int i;
1114 
1115   /* YM2413 always running at original frequency */
1116   double freqbase = 1.0;
1117 
1118   /* make fnumber -> increment counter table */
1119   for( i = 0 ; i < 1024; i++ )
1120   {
1121     /* OPLL (YM2413) phase increment counter = 18bit */
1122     ym2413.fn_tab[i] = (UINT32)( (double)i * 64 * freqbase * (1<<(FREQ_SH-10)) ); /* -10 because chip works with 10.10 fixed point, while we use 16.16 */
1123   }
1124 
1125   /* Amplitude modulation: 27 output levels (triangle waveform); 1 level takes one of: 192, 256 or 448 samples */
1126   /* One entry from LFO_AM_TABLE lasts for 64 samples */
1127   ym2413.lfo_am_inc = (1.0 / 64.0 ) * (1<<LFO_SH) * freqbase;
1128 
1129   /* Vibrato: 8 output levels (triangle waveform); 1 level takes 1024 samples */
1130   ym2413.lfo_pm_inc = (1.0 / 1024.0) * (1<<LFO_SH) * freqbase;
1131 
1132   /* Noise generator: a step takes 1 sample */
1133   ym2413.noise_f = (1.0 / 1.0) * (1<<FREQ_SH) * freqbase;
1134 
1135   ym2413.eg_timer_add  = (1<<EG_SH) * freqbase;
1136   ym2413.eg_timer_overflow = ( 1 ) * (1<<EG_SH);
1137 }
1138 
KEY_ON(YM2413_OPLL_SLOT * SLOT,UINT32 key_set)1139 INLINE void KEY_ON(YM2413_OPLL_SLOT *SLOT, UINT32 key_set)
1140 {
1141   if( !SLOT->key )
1142   {
1143     /* do NOT restart Phase Generator (verified on real YM2413)*/
1144     /* phase -> Dump */
1145     SLOT->state = EG_DMP;
1146   }
1147   SLOT->key |= key_set;
1148 }
1149 
KEY_OFF(YM2413_OPLL_SLOT * SLOT,UINT32 key_clr)1150 INLINE void KEY_OFF(YM2413_OPLL_SLOT *SLOT, UINT32 key_clr)
1151 {
1152   if( SLOT->key )
1153   {
1154     SLOT->key &= key_clr;
1155 
1156     if( !SLOT->key )
1157     {
1158       /* phase -> Release */
1159       if (SLOT->state>EG_REL)
1160         SLOT->state = EG_REL;
1161     }
1162   }
1163 }
1164 
1165 /* update phase increment counter of operator (also update the EG rates if necessary) */
CALC_FCSLOT(YM2413_OPLL_CH * CH,YM2413_OPLL_SLOT * SLOT)1166 INLINE void CALC_FCSLOT(YM2413_OPLL_CH *CH,YM2413_OPLL_SLOT *SLOT)
1167 {
1168   int ksr;
1169   UINT32 SLOT_rs;
1170   UINT32 SLOT_dp;
1171 
1172   /* (frequency) phase increment counter */
1173   SLOT->freq = CH->fc * SLOT->mul;
1174   ksr = CH->kcode >> SLOT->KSR;
1175 
1176   if( SLOT->ksr != ksr )
1177   {
1178     SLOT->ksr = ksr;
1179 
1180     /* calculate envelope generator rates */
1181     if ((SLOT->ar + SLOT->ksr) < 16+62)
1182     {
1183       SLOT->eg_sh_ar  = eg_rate_shift [SLOT->ar + SLOT->ksr ];
1184       SLOT->eg_sel_ar = eg_rate_select[SLOT->ar + SLOT->ksr ];
1185     }
1186     else
1187     {
1188       SLOT->eg_sh_ar  = 0;
1189       SLOT->eg_sel_ar = 13*RATE_STEPS;
1190     }
1191     SLOT->eg_sh_dr  = eg_rate_shift [SLOT->dr + SLOT->ksr ];
1192     SLOT->eg_sel_dr = eg_rate_select[SLOT->dr + SLOT->ksr ];
1193     SLOT->eg_sh_rr  = eg_rate_shift [SLOT->rr + SLOT->ksr ];
1194     SLOT->eg_sel_rr = eg_rate_select[SLOT->rr + SLOT->ksr ];
1195 
1196   }
1197 
1198   if (CH->sus)
1199     SLOT_rs  = 16 + (5<<2);
1200   else
1201     SLOT_rs  = 16 + (7<<2);
1202 
1203   SLOT->eg_sh_rs  = eg_rate_shift [SLOT_rs + SLOT->ksr ];
1204   SLOT->eg_sel_rs = eg_rate_select[SLOT_rs + SLOT->ksr ];
1205 
1206   SLOT_dp  = 16 + (13<<2);
1207   SLOT->eg_sh_dp  = eg_rate_shift [SLOT_dp + SLOT->ksr ];
1208   SLOT->eg_sel_dp = eg_rate_select[SLOT_dp + SLOT->ksr ];
1209 }
1210 
1211 /* set multi,am,vib,EG-TYP,KSR,mul */
set_mul(int slot,int v)1212 INLINE void set_mul(int slot,int v)
1213 {
1214   YM2413_OPLL_CH   *CH   = &ym2413.P_CH[slot/2];
1215   YM2413_OPLL_SLOT *SLOT = &CH->SLOT[slot&1];
1216 
1217   SLOT->mul     = mul_tab[v&0x0f];
1218   SLOT->KSR     = (v&0x10) ? 0 : 2;
1219   SLOT->eg_type = (v&0x20);
1220   SLOT->vib     = (v&0x40);
1221   SLOT->AMmask  = (v&0x80) ? ~0 : 0;
1222   CALC_FCSLOT(CH,SLOT);
1223 }
1224 
1225 /* set ksl, tl */
set_ksl_tl(int chan,int v)1226 INLINE void set_ksl_tl(int chan,int v)
1227 {
1228   YM2413_OPLL_CH   *CH   = &ym2413.P_CH[chan];
1229   /* modulator */
1230   YM2413_OPLL_SLOT *SLOT = &CH->SLOT[SLOT1];
1231 
1232   int ksl = v>>6; /* 0 / 1.5 / 3.0 / 6.0 dB/OCT */
1233 
1234   SLOT->ksl = ksl ? 3-ksl : 31;
1235   SLOT->TL  = (v&0x3f)<<(ENV_BITS-2-7); /* 7 bits TL (bit 6 = always 0) */
1236   SLOT->TLL = SLOT->TL + (CH->ksl_base>>SLOT->ksl);
1237 }
1238 
1239 /* set ksl , waveforms, feedback */
set_ksl_wave_fb(int chan,int v)1240 INLINE void set_ksl_wave_fb(int chan,int v)
1241 {
1242   YM2413_OPLL_CH   *CH   = &ym2413.P_CH[chan];
1243   /* modulator */
1244   YM2413_OPLL_SLOT *SLOT = &CH->SLOT[SLOT1];
1245   SLOT->wavetable = ((v&0x08)>>3)*SIN_LEN;
1246   SLOT->fb_shift  = (v&7) ? (v&7) + 8 : 0;
1247 
1248   /*carrier*/
1249   SLOT = &CH->SLOT[SLOT2];
1250   SLOT->wavetable = ((v&0x10)>>4)*SIN_LEN;
1251   v >>= 6; /* 0 / 1.5 / 3.0 / 6.0 dB/OCT */
1252   SLOT->ksl = v ? 3-v : 31;
1253   SLOT->TLL = SLOT->TL + (CH->ksl_base>>SLOT->ksl);
1254 }
1255 
1256 /* set attack rate & decay rate  */
set_ar_dr(int slot,int v)1257 INLINE void set_ar_dr(int slot,int v)
1258 {
1259   YM2413_OPLL_CH   *CH   = &ym2413.P_CH[slot/2];
1260   YM2413_OPLL_SLOT *SLOT = &CH->SLOT[slot&1];
1261 
1262   SLOT->ar = (v>>4)  ? 16 + ((v>>4)  <<2) : 0;
1263 
1264   if ((SLOT->ar + SLOT->ksr) < 16+62)
1265   {
1266     SLOT->eg_sh_ar  = eg_rate_shift [SLOT->ar + SLOT->ksr ];
1267     SLOT->eg_sel_ar = eg_rate_select[SLOT->ar + SLOT->ksr ];
1268   }
1269   else
1270   {
1271     SLOT->eg_sh_ar  = 0;
1272     SLOT->eg_sel_ar = 13*RATE_STEPS;
1273   }
1274 
1275   SLOT->dr    = (v&0x0f)? 16 + ((v&0x0f)<<2) : 0;
1276   SLOT->eg_sh_dr  = eg_rate_shift [SLOT->dr + SLOT->ksr ];
1277   SLOT->eg_sel_dr = eg_rate_select[SLOT->dr + SLOT->ksr ];
1278 }
1279 
1280 /* set sustain level & release rate */
set_sl_rr(int slot,int v)1281 INLINE void set_sl_rr(int slot,int v)
1282 {
1283   YM2413_OPLL_CH   *CH   = &ym2413.P_CH[slot/2];
1284   YM2413_OPLL_SLOT *SLOT = &CH->SLOT[slot&1];
1285 
1286   SLOT->sl  = sl_tab[ v>>4 ];
1287 
1288   SLOT->rr  = (v&0x0f)? 16 + ((v&0x0f)<<2) : 0;
1289   SLOT->eg_sh_rr  = eg_rate_shift [SLOT->rr + SLOT->ksr ];
1290   SLOT->eg_sel_rr = eg_rate_select[SLOT->rr + SLOT->ksr ];
1291 }
1292 
load_instrument(UINT32 chan,UINT32 slot,UINT8 * inst)1293 static void load_instrument(UINT32 chan, UINT32 slot, UINT8* inst )
1294 {
1295   set_mul(slot, inst[0]);
1296   set_mul(slot+1, inst[1]);
1297   set_ksl_tl(chan, inst[2]);
1298   set_ksl_wave_fb(chan, inst[3]);
1299   set_ar_dr(slot,   inst[4]);
1300   set_ar_dr(slot+1, inst[5]);
1301   set_sl_rr(slot,   inst[6]);
1302   set_sl_rr(slot+1, inst[7]);
1303 }
1304 
update_instrument_zero(UINT8 r)1305 static void update_instrument_zero(UINT8 r)
1306 {
1307   UINT8* inst = &ym2413.inst_tab[0][0]; /* point to user instrument */
1308   UINT32 chan;
1309 
1310   UINT32 chan_max = 9;
1311   if (ym2413.rhythm & 0x20)
1312     chan_max=6;
1313 
1314   switch(r&7)
1315   {
1316     case 0:
1317       for (chan=0; chan<chan_max; chan++)
1318       {
1319         if ((ym2413.instvol_r[chan]&0xf0)==0)
1320         {
1321           set_mul(chan*2, inst[0]);
1322         }
1323       }
1324       break;
1325 
1326     case 1:
1327       for (chan=0; chan<chan_max; chan++)
1328       {
1329         if ((ym2413.instvol_r[chan]&0xf0)==0)
1330         {
1331           set_mul(chan*2+1, inst[1]);
1332         }
1333       }
1334       break;
1335 
1336     case 2:
1337       for (chan=0; chan<chan_max; chan++)
1338       {
1339         if ((ym2413.instvol_r[chan]&0xf0)==0)
1340         {
1341           set_ksl_tl(chan, inst[2]);
1342         }
1343       }
1344       break;
1345 
1346     case 3:
1347       for (chan=0; chan<chan_max; chan++)
1348       {
1349         if ((ym2413.instvol_r[chan]&0xf0)==0)
1350         {
1351           set_ksl_wave_fb(chan, inst[3]);
1352         }
1353       }
1354       break;
1355 
1356     case 4:
1357       for (chan=0; chan<chan_max; chan++)
1358       {
1359         if ((ym2413.instvol_r[chan]&0xf0)==0)
1360         {
1361           set_ar_dr(chan*2, inst[4]);
1362         }
1363       }
1364       break;
1365 
1366     case 5:
1367       for (chan=0; chan<chan_max; chan++)
1368       {
1369         if ((ym2413.instvol_r[chan]&0xf0)==0)
1370         {
1371           set_ar_dr(chan*2+1, inst[5]);
1372         }
1373       }
1374       break;
1375 
1376     case 6:
1377       for (chan=0; chan<chan_max; chan++)
1378       {
1379         if ((ym2413.instvol_r[chan]&0xf0)==0)
1380         {
1381           set_sl_rr(chan*2, inst[6]);
1382         }
1383       }
1384       break;
1385 
1386     case 7:
1387       for (chan=0; chan<chan_max; chan++)
1388       {
1389         if ((ym2413.instvol_r[chan]&0xf0)==0)
1390         {
1391           set_sl_rr(chan*2+1, inst[7]);
1392         }
1393       }
1394       break;
1395   }
1396 }
1397 
1398 /* write a value v to register r on chip chip */
OPLLWriteReg(int r,int v)1399 static void OPLLWriteReg(int r, int v)
1400 {
1401   YM2413_OPLL_CH *CH;
1402   YM2413_OPLL_SLOT *SLOT;
1403 
1404   /* adjust bus to 8 bits */
1405   r &= 0xff;
1406   v &= 0xff;
1407 
1408   switch(r&0xf0)
1409   {
1410     case 0x00:  /* 00-0f:control */
1411     {
1412       switch(r&0x0f)
1413       {
1414         case 0x00:  /* AM/VIB/EGTYP/KSR/MULTI (modulator) */
1415         case 0x01:  /* AM/VIB/EGTYP/KSR/MULTI (carrier) */
1416         case 0x02:  /* Key Scale Level, Total Level (modulator) */
1417         case 0x03:  /* Key Scale Level, carrier waveform, modulator waveform, Feedback */
1418         case 0x04:  /* Attack, Decay (modulator) */
1419         case 0x05:  /* Attack, Decay (carrier) */
1420         case 0x06:  /* Sustain, Release (modulator) */
1421         case 0x07:  /* Sustain, Release (carrier) */
1422         {
1423           ym2413.inst_tab[0][r] = v;
1424           update_instrument_zero(r);
1425           break;
1426         }
1427 
1428         case 0x0e:  /* x, x, r,bd,sd,tom,tc,hh */
1429         {
1430           if(v&0x20)
1431           {
1432             /* rhythm OFF to ON */
1433             if ((ym2413.rhythm&0x20)==0)
1434             {
1435               /* Load instrument settings for channel seven(chan=6 since we're zero based). (Bass drum) */
1436               load_instrument(6, 12, &ym2413.inst_tab[16][0]);
1437 
1438               /* Load instrument settings for channel eight. (High hat and snare drum) */
1439               load_instrument(7, 14, &ym2413.inst_tab[17][0]);
1440 
1441               CH   = &ym2413.P_CH[7];
1442               SLOT = &CH->SLOT[SLOT1]; /* modulator envelope is HH */
1443               SLOT->TL  = ((ym2413.instvol_r[7]>>4)<<2)<<(ENV_BITS-2-7); /* 7 bits TL (bit 6 = always 0) */
1444               SLOT->TLL = SLOT->TL + (CH->ksl_base>>SLOT->ksl);
1445 
1446               /* Load instrument settings for channel nine. (Tom-tom and top cymbal) */
1447               load_instrument(8, 16, &ym2413.inst_tab[18][0]);
1448 
1449               CH   = &ym2413.P_CH[8];
1450               SLOT = &CH->SLOT[SLOT1]; /* modulator envelope is TOM */
1451               SLOT->TL  = ((ym2413.instvol_r[8]>>4)<<2)<<(ENV_BITS-2-7); /* 7 bits TL (bit 6 = always 0) */
1452               SLOT->TLL = SLOT->TL + (CH->ksl_base>>SLOT->ksl);
1453             }
1454 
1455             /* BD key on/off */
1456             if(v&0x10)
1457             {
1458               KEY_ON (&ym2413.P_CH[6].SLOT[SLOT1], 2);
1459               KEY_ON (&ym2413.P_CH[6].SLOT[SLOT2], 2);
1460             }
1461             else
1462             {
1463               KEY_OFF(&ym2413.P_CH[6].SLOT[SLOT1],~2);
1464               KEY_OFF(&ym2413.P_CH[6].SLOT[SLOT2],~2);
1465             }
1466 
1467             /* HH key on/off */
1468             if(v&0x01) KEY_ON (&ym2413.P_CH[7].SLOT[SLOT1], 2);
1469             else       KEY_OFF(&ym2413.P_CH[7].SLOT[SLOT1],~2);
1470 
1471             /* SD key on/off */
1472             if(v&0x08) KEY_ON (&ym2413.P_CH[7].SLOT[SLOT2], 2);
1473             else       KEY_OFF(&ym2413.P_CH[7].SLOT[SLOT2],~2);
1474 
1475             /* TOM key on/off */
1476             if(v&0x04) KEY_ON (&ym2413.P_CH[8].SLOT[SLOT1], 2);
1477             else       KEY_OFF(&ym2413.P_CH[8].SLOT[SLOT1],~2);
1478 
1479             /* TOP-CY key on/off */
1480             if(v&0x02) KEY_ON (&ym2413.P_CH[8].SLOT[SLOT2], 2);
1481             else       KEY_OFF(&ym2413.P_CH[8].SLOT[SLOT2],~2);
1482           }
1483           else
1484           {
1485             /* rhythm ON to OFF */
1486             if (ym2413.rhythm&0x20)
1487             {
1488               /* Load instrument settings for channel seven(chan=6 since we're zero based).*/
1489               load_instrument(6, 12, &ym2413.inst_tab[ym2413.instvol_r[6]>>4][0]);
1490 
1491               /* Load instrument settings for channel eight.*/
1492               load_instrument(7, 14, &ym2413.inst_tab[ym2413.instvol_r[7]>>4][0]);
1493 
1494               /* Load instrument settings for channel nine.*/
1495               load_instrument(8, 16, &ym2413.inst_tab[ym2413.instvol_r[8]>>4][0]);
1496             }
1497 
1498             /* BD key off */
1499             KEY_OFF(&ym2413.P_CH[6].SLOT[SLOT1],~2);
1500             KEY_OFF(&ym2413.P_CH[6].SLOT[SLOT2],~2);
1501 
1502             /* HH key off */
1503             KEY_OFF(&ym2413.P_CH[7].SLOT[SLOT1],~2);
1504 
1505             /* SD key off */
1506             KEY_OFF(&ym2413.P_CH[7].SLOT[SLOT2],~2);
1507 
1508             /* TOM key off */
1509             KEY_OFF(&ym2413.P_CH[8].SLOT[SLOT1],~2);
1510 
1511             /* TOP-CY off */
1512             KEY_OFF(&ym2413.P_CH[8].SLOT[SLOT2],~2);
1513           }
1514 
1515           ym2413.rhythm = v&0x3f;
1516           break;
1517         }
1518       }
1519 
1520       break;
1521     }
1522 
1523     case 0x10:
1524     case 0x20:
1525     {
1526       int block_fnum;
1527 
1528       int chan = r&0x0f;
1529 
1530       if (chan >= 9)
1531         chan -= 9;  /* verified on real YM2413 */
1532 
1533       CH = &ym2413.P_CH[chan];
1534 
1535       if(r&0x10)
1536       {
1537         /* 10-18: FNUM 0-7 */
1538         block_fnum = (CH->block_fnum&0x0f00) | v;
1539       }
1540       else
1541       {
1542         /* 20-28: suson, keyon, block, FNUM 8 */
1543         block_fnum = ((v&0x0f)<<8) | (CH->block_fnum&0xff);
1544 
1545         if(v&0x10)
1546         {
1547           KEY_ON (&CH->SLOT[SLOT1], 1);
1548           KEY_ON (&CH->SLOT[SLOT2], 1);
1549         }
1550         else
1551         {
1552           KEY_OFF(&CH->SLOT[SLOT1],~1);
1553           KEY_OFF(&CH->SLOT[SLOT2],~1);
1554         }
1555 
1556         CH->sus = v & 0x20;
1557       }
1558 
1559       /* update */
1560       if(CH->block_fnum != block_fnum)
1561       {
1562         UINT8 block;
1563         CH->block_fnum = block_fnum;
1564 
1565         /* BLK 2,1,0 bits -> bits 3,2,1 of kcode, FNUM MSB -> kcode LSB */
1566         CH->kcode    = (block_fnum&0x0f00)>>8;
1567 
1568         CH->ksl_base = ksl_tab[block_fnum>>5];
1569 
1570         block_fnum   = block_fnum * 2;
1571         block        = (block_fnum&0x1c00) >> 10;
1572         CH->fc       = ym2413.fn_tab[block_fnum&0x03ff] >> (7-block);
1573 
1574         /* refresh Total Level in both SLOTs of this channel */
1575         CH->SLOT[SLOT1].TLL = CH->SLOT[SLOT1].TL + (CH->ksl_base>>CH->SLOT[SLOT1].ksl);
1576         CH->SLOT[SLOT2].TLL = CH->SLOT[SLOT2].TL + (CH->ksl_base>>CH->SLOT[SLOT2].ksl);
1577 
1578         /* refresh frequency counter in both SLOTs of this channel */
1579         CALC_FCSLOT(CH,&CH->SLOT[SLOT1]);
1580         CALC_FCSLOT(CH,&CH->SLOT[SLOT2]);
1581       }
1582 
1583       break;
1584     }
1585 
1586     case 0x30:  /* inst 4 MSBs, VOL 4 LSBs */
1587     {
1588       int chan = r&0x0f;
1589 
1590       if (chan >= 9)
1591         chan -= 9;  /* verified on real YM2413 */
1592 
1593       CH   = &ym2413.P_CH[chan];
1594       SLOT = &CH->SLOT[SLOT2]; /* carrier */
1595       SLOT->TL  = ((v&0x0f)<<2)<<(ENV_BITS-2-7); /* 7 bits TL (bit 6 = always 0) */
1596       SLOT->TLL = SLOT->TL + (CH->ksl_base>>SLOT->ksl);
1597 
1598       /*check wether we are in rhythm mode and handle instrument/volume register accordingly*/
1599       if ((chan>=6) && (ym2413.rhythm&0x20))
1600       {
1601         /* we're in rhythm mode*/
1602 
1603         if (chan>=7) /* only for channel 7 and 8 (channel 6 is handled in usual way)*/
1604         {
1605           SLOT = &CH->SLOT[SLOT1]; /* modulator envelope is HH(chan=7) or TOM(chan=8) */
1606           SLOT->TL  = ((v>>4)<<2)<<(ENV_BITS-2-7); /* 7 bits TL (bit 6 = always 0) */
1607           SLOT->TLL = SLOT->TL + (CH->ksl_base>>SLOT->ksl);
1608         }
1609       }
1610       else
1611       {
1612         if ((ym2413.instvol_r[chan]&0xf0) != (v&0xf0))
1613         {
1614           ym2413.instvol_r[chan] = v;  /* store for later use */
1615           load_instrument(chan, chan * 2, &ym2413.inst_tab[v>>4][0]);
1616         }
1617       }
1618 
1619       break;
1620     }
1621 
1622     default:
1623       break;
1624   }
1625 }
1626 
1627 
YM2413Init(void)1628 void YM2413Init(void)
1629 {
1630   init_tables();
1631 
1632   /* clear */
1633   memset(&ym2413,0,sizeof(YM2413));
1634 
1635   /* init global tables */
1636   OPLL_initalize();
1637 }
1638 
YM2413ResetChip(void)1639 void YM2413ResetChip(void)
1640 {
1641   int c,s;
1642   int i;
1643 
1644   ym2413.eg_timer = 0;
1645   ym2413.eg_cnt   = 0;
1646 
1647   ym2413.noise_rng = 1;  /* noise shift register */
1648 
1649 
1650   /* setup instruments table */
1651   for (i=0; i<19; i++)
1652   {
1653     for (c=0; c<8; c++)
1654     {
1655       ym2413.inst_tab[i][c] = table[i][c];
1656     }
1657   }
1658 
1659 
1660   /* reset with register write */
1661   OPLLWriteReg(0x0f,0); /*test reg*/
1662   for(i = 0x3f ; i >= 0x10 ; i-- ) OPLLWriteReg(i,0x00);
1663 
1664   /* reset operator parameters */
1665   for( c = 0 ; c < 9 ; c++ )
1666   {
1667     YM2413_OPLL_CH *CH = &ym2413.P_CH[c];
1668     for(s = 0 ; s < 2 ; s++ )
1669     {
1670       /* wave table */
1671       CH->SLOT[s].wavetable = 0;
1672       CH->SLOT[s].state     = EG_OFF;
1673       CH->SLOT[s].volume    = MAX_ATT_INDEX;
1674     }
1675   }
1676 }
1677 
1678 /* YM2413 I/O interface */
1679 
YM2413Write(unsigned int a,unsigned int v)1680 void YM2413Write(unsigned int a, unsigned int v)
1681 {
1682   if( !(a&2) )
1683   {
1684     if( !(a&1) )
1685     {
1686       /* address port */
1687       ym2413.address = v & 0xff;
1688     }
1689     else
1690     {
1691       /* data port */
1692       OPLLWriteReg(ym2413.address,v);
1693     }
1694   }
1695   else
1696   {
1697     /* bit 0 enable/disable FM output (Master System / Mark-III FM adapter specific) */
1698     ym2413.status = v & 0x01;
1699   }
1700 }
1701 
YM2413Read(void)1702 unsigned int YM2413Read(void)
1703 {
1704   /* bit 0 returns latched FM enable status, bits 1-2 return zero (Master System / Mark-III FM adapter specific) */
1705   return 0xF8 | ym2413.status;
1706 }
1707 
YM2413Update(int * buffer,int length)1708 void YM2413Update(int *buffer, int length)
1709 {
1710   int i, out;
1711 
1712   for( i=0; i < length ; i++ )
1713   {
1714     output[0] = 0;
1715     output[1] = 0;
1716 
1717     advance_lfo();
1718 
1719     /* FM part */
1720     chan_calc(&ym2413.P_CH[0], 0);
1721     chan_calc(&ym2413.P_CH[1], 1);
1722     chan_calc(&ym2413.P_CH[2], 2);
1723     chan_calc(&ym2413.P_CH[3], 3);
1724     chan_calc(&ym2413.P_CH[4], 4);
1725     chan_calc(&ym2413.P_CH[5], 5);
1726 
1727     if(!(ym2413.rhythm&0x20))
1728     {
1729       chan_calc(&ym2413.P_CH[6], 6);
1730       chan_calc(&ym2413.P_CH[7], 7);
1731       chan_calc(&ym2413.P_CH[8], 8);
1732     }
1733     else    /* Rhythm part */
1734     {
1735       rhythm_calc(&ym2413.P_CH[0], (ym2413.noise_rng>>0)&1 );
1736     }
1737 
1738     /* Melody (MO) & Rythm (RO) outputs mixing & amplification (latched bit controls FM output) */
1739     out = (output[0] + (output[1] * 2)) * 2 * ym2413.status;
1740 
1741     /* Store to stereo sound buffer */
1742     *buffer++ = out;
1743     *buffer++ = out;
1744 
1745     advance();
1746   }
1747 }
1748 
YM2413GetContextPtr(void)1749 unsigned char *YM2413GetContextPtr(void)
1750 {
1751   return (unsigned char *)&ym2413;
1752 }
1753 
YM2413GetContextSize(void)1754 unsigned int YM2413GetContextSize(void)
1755 {
1756   return sizeof(YM2413);
1757 }
1758