Previous 15994 Revisions Next

r5436 Saturday 15th August, 2009 at 02:51:10 UTC by R. Belmont
Bringing latest MAME YM2612/3834 update over to MESS so it can be tested on
the Genesis driver.
[src/emu/sound]fm.c fm2612.c* sound.mak

src/emu/sound/fm2612.c
r0r5436
1/*
2**
3** File: fm2612.c -- software implementation of Yamaha YM2612 FM sound generator
4** Split from fm.c to keep 2612 fixes from infecting other OPN chips
5**
6** Copyright Jarek Burczynski (bujar at mame dot net)
7** Copyright Tatsuyuki Satoh , MultiArcadeMachineEmulator development
8**
9** Version 1.5 (Genesis Plus GX ym2612.c rev. 325)
10**
11*/
12
13/*
14** History:
15**
16** 2006~2009  Eke-Eke (Genesis Plus GX):
17** Credits to Nemesis (@spritesmind.net), most of those fixes came from his tests on a Model 1 Sega Mega Drive
18** More informations at http://gendev.spritesmind.net/forum/viewtopic.php?t=386
19**
20**  - fixed LFO implementation (Spider-Man & Venom : Separation Anxiety intro,Warlock birds, Aladdin bug sound):
21**      .added support for CH3 special mode
22**      .fixed LFO update: it is done after output calculation, like EG/PG updates
23**      .fixed LFO on/off behavior: LFO is reset when switched ON and holded at its current level when switched OFF (AM & PM can still be applied)
24**  - improved internal timers emulation
25**  - fixed Attack Rate update in some specific case (Batman & Robin intro)
26**  - fixed EG behavior when Attack Rate is maximal
27**  - fixed EG behavior when SL=0 (Mega Turrican tracks 03,09...) or/and Key ON occurs at minimal attenuation
28**  - added EG output immediate update on register writes
29**  - fixed YM2612 initial values (after the reset)
30**  - implemented Detune overflow (Ariel, Comix Zone, Shaq Fu, Spiderman & many others)
31**  - implemented correct CSM mode emulation
32**  - implemented correct SSG-EG emulation (Asterix, Beavis&Butthead, Bubba'n Six & many others)
33**  - adjusted some EG rates
34**
35**  TODO: fix SSG-EG documentation, BUSY flag support
36**
37** 06-23-2007 Zsolt Vasvari:
38**  - changed the timing not to require the use of floating point calculations
39**
40** 03-08-2003 Jarek Burczynski:
41**  - fixed YM2608 initial values (after the reset)
42**  - fixed flag and irqmask handling (YM2608)
43**  - fixed BUFRDY flag handling (YM2608)
44**
45** 14-06-2003 Jarek Burczynski:
46**  - implemented all of the YM2608 status register flags
47**  - implemented support for external memory read/write via YM2608
48**  - implemented support for deltat memory limit register in YM2608 emulation
49**
50** 22-05-2003 Jarek Burczynski:
51**  - fixed LFO PM calculations (copy&paste bugfix)
52**
53** 08-05-2003 Jarek Burczynski:
54**  - fixed SSG support
55**
56** 22-04-2003 Jarek Burczynski:
57**  - implemented 100% correct LFO generator (verified on real YM2610 and YM2608)
58**
59** 15-04-2003 Jarek Burczynski:
60**  - added support for YM2608's register 0x110 - status mask
61**
62** 01-12-2002 Jarek Burczynski:
63**  - fixed register addressing in YM2608, YM2610, YM2610B chips. (verified on real YM2608)
64**    The addressing patch used for early Neo-Geo games can be removed now.
65**
66** 26-11-2002 Jarek Burczynski, Nicola Salmoria:
67**  - recreated YM2608 ADPCM ROM using data from real YM2608's output which leads to:
68**  - added emulation of YM2608 drums.
69**  - output of YM2608 is two times lower now - same as YM2610 (verified on real YM2608)
70**
71** 16-08-2002 Jarek Burczynski:
72**  - binary exact Envelope Generator (verified on real YM2203);
73**    identical to YM2151
74**  - corrected 'off by one' error in feedback calculations (when feedback is off)
75**  - corrected connection (algorithm) calculation (verified on real YM2203 and YM2610)
76**
77** 18-12-2001 Jarek Burczynski:
78**  - added SSG-EG support (verified on real YM2203)
79**
80** 12-08-2001 Jarek Burczynski:
81**  - corrected sin_tab and tl_tab data (verified on real chip)
82**  - corrected feedback calculations (verified on real chip)
83**  - corrected phase generator calculations (verified on real chip)
84**  - corrected envelope generator calculations (verified on real chip)
85**  - corrected FM volume level (YM2610 and YM2610B).
86**  - changed YMxxxUpdateOne() functions (YM2203, YM2608, YM2610, YM2610B, YM2612) :
87**    this was needed to calculate YM2610 FM channels output correctly.
88**    (Each FM channel is calculated as in other chips, but the output of the channel
89**    gets shifted right by one *before* sending to accumulator. That was impossible to do
90**    with previous implementation).
91**
92** 23-07-2001 Jarek Burczynski, Nicola Salmoria:
93**  - corrected YM2610 ADPCM type A algorithm and tables (verified on real chip)
94**
95** 11-06-2001 Jarek Burczynski:
96**  - corrected end of sample bug in ADPCMA_calc_cha().
97**    Real YM2610 checks for equality between current and end addresses (only 20 LSB bits).
98**
99** 08-12-98 hiro-shi:
100** rename ADPCMA -> ADPCMB, ADPCMB -> ADPCMA
101** move ROM limit check.(CALC_CH? -> 2610Write1/2)
102** test program (ADPCMB_TEST)
103** move ADPCM A/B end check.
104** ADPCMB repeat flag(no check)
105** change ADPCM volume rate (8->16) (32->48).
106**
107** 09-12-98 hiro-shi:
108** change ADPCM volume. (8->16, 48->64)
109** replace ym2610 ch0/3 (YM-2610B)
110** change ADPCM_SHIFT (10->8) missing bank change 0x4000-0xffff.
111** add ADPCM_SHIFT_MASK
112** change ADPCMA_DECODE_MIN/MAX.
113*/
114
115
116
117
118/************************************************************************/
119/*    comment of hiro-shi(Hiromitsu Shioya)                             */
120/*    YM2610(B) = OPN-B                                                 */
121/*    YM2610  : PSG:3ch FM:4ch ADPCM(18.5KHz):6ch DeltaT ADPCM:1ch      */
122/*    YM2610B : PSG:3ch FM:6ch ADPCM(18.5KHz):6ch DeltaT ADPCM:1ch      */
123/************************************************************************/
124
125#include <stdio.h>
126#include <stdlib.h>
127#include <string.h>
128#include <stdarg.h>
129#include <math.h>
130
131#ifndef __RAINE__
132#include "sndintrf.h"      /* use M.A.M.E. */
133#else
134#include "deftypes.h"      /* use RAINE */
135#include "support.h"      /* use RAINE */
136#endif
137#include "fm.h"
138
139/* shared function building option */
140#define BUILD_OPN (BUILD_YM2203||BUILD_YM2608||BUILD_YM2610||BUILD_YM2610B||BUILD_YM2612||BUILD_YM3438)
141#define BUILD_OPN_PRESCALER (BUILD_YM2203||BUILD_YM2608)
142
143
144/* globals */
145#define TYPE_SSG    0x01    /* SSG support          */
146#define TYPE_LFOPAN 0x02    /* OPN type LFO and PAN */
147#define TYPE_6CH    0x04    /* FM 6CH / 3CH         */
148#define TYPE_DAC    0x08    /* YM2612's DAC device  */
149#define TYPE_ADPCM  0x10    /* two ADPCM units      */
150#define TYPE_2610   0x20    /* bogus flag to differentiate 2608 from 2610 */
151
152
153#define TYPE_YM2203 (TYPE_SSG)
154#define TYPE_YM2608 (TYPE_SSG |TYPE_LFOPAN |TYPE_6CH |TYPE_ADPCM)
155#define TYPE_YM2610 (TYPE_SSG |TYPE_LFOPAN |TYPE_6CH |TYPE_ADPCM |TYPE_2610)
156#define TYPE_YM2612 (TYPE_DAC |TYPE_LFOPAN |TYPE_6CH)
157
158
159/* globals */
160#define FREQ_SH         16  /* 16.16 fixed point (frequency calculations) */
161#define EG_SH         16  /* 16.16 fixed point (envelope generator timing) */
162#define LFO_SH         24  /*  8.24 fixed point (LFO calculations)       */
163#define TIMER_SH      16  /* 16.16 fixed point (timers calculations)    */
164
165#define FREQ_MASK      ((1<<FREQ_SH)-1)
166
167#define MAXOUT    (+32767)
168#define MINOUT    (-32768)
169
170/* envelope generator */
171#define ENV_BITS      10
172#define ENV_LEN         (1<<ENV_BITS)
173#define ENV_STEP      (128.0/ENV_LEN)
174
175#define MAX_ATT_INDEX   (ENV_LEN-1) /* 1023 */
176#define MIN_ATT_INDEX   (0)         /* 0 */
177
178#define EG_ATT         4
179#define EG_DEC         3
180#define EG_SUS         2
181#define EG_REL         1
182#define EG_OFF         0
183
184/* operator unit */
185#define SIN_BITS      10
186#define SIN_LEN         (1<<SIN_BITS)
187#define SIN_MASK      (SIN_LEN-1)
188
189#define TL_RES_LEN      (256) /* 8 bits addressing (real chip) */
190
191/*  TL_TAB_LEN is calculated as:
192*   13 - sinus amplitude bits     (Y axis)
193*   2  - sinus sign bit           (Y axis)
194*   TL_RES_LEN - sinus resolution (X axis)
195*/
196#define TL_TAB_LEN (13*2*TL_RES_LEN)
197static signed int tl_tab[TL_TAB_LEN];
198
199#define ENV_QUIET      (TL_TAB_LEN>>3)
200
201/* sin waveform table in 'decibel' scale */
202static unsigned int sin_tab[SIN_LEN];
203
204/* sustain level table (3dB per step) */
205/* bit0, bit1, bit2, bit3, bit4, bit5, bit6 */
206/* 1,    2,    4,    8,    16,   32,   64   (value)*/
207/* 0.75, 1.5,  3,    6,    12,   24,   48   (dB)*/
208
209/* 0 - 15: 0, 3, 6, 9,12,15,18,21,24,27,30,33,36,39,42,93 (dB)*/
210/* attenuation value (10 bits) = (SL << 2) << 3 */
211#define SC(db) (UINT32) ( db * (4.0/ENV_STEP) )
212static const UINT32 sl_table[16]={
213 SC( 0),SC( 1),SC( 2),SC(3 ),SC(4 ),SC(5 ),SC(6 ),SC( 7),
214 SC( 8),SC( 9),SC(10),SC(11),SC(12),SC(13),SC(14),SC(31)
215};
216#undef SC
217
218
219#define RATE_STEPS (8)
220static const UINT8 eg_inc[19*RATE_STEPS]={
221
222/*cycle:0 1  2 3  4 5  6 7*/
223
224/* 0 */ 0,1, 0,1, 0,1, 0,1, /* rates 00..11 0 (increment by 0 or 1) */
225/* 1 */ 0,1, 0,1, 1,1, 0,1, /* rates 00..11 1 */
226/* 2 */ 0,1, 1,1, 0,1, 1,1, /* rates 00..11 2 */
227/* 3 */ 0,1, 1,1, 1,1, 1,1, /* rates 00..11 3 */
228
229/* 4 */ 1,1, 1,1, 1,1, 1,1, /* rate 12 0 (increment by 1) */
230/* 5 */ 1,1, 1,2, 1,1, 1,2, /* rate 12 1 */
231/* 6 */ 1,2, 1,2, 1,2, 1,2, /* rate 12 2 */
232/* 7 */ 1,2, 2,2, 1,2, 2,2, /* rate 12 3 */
233
234/* 8 */ 2,2, 2,2, 2,2, 2,2, /* rate 13 0 (increment by 2) */
235/* 9 */ 2,2, 2,4, 2,2, 2,4, /* rate 13 1 */
236/*10 */ 2,4, 2,4, 2,4, 2,4, /* rate 13 2 */
237/*11 */ 2,4, 4,4, 2,4, 4,4, /* rate 13 3 */
238
239/*12 */ 4,4, 4,4, 4,4, 4,4, /* rate 14 0 (increment by 4) */
240/*13 */ 4,4, 4,8, 4,4, 4,8, /* rate 14 1 */
241/*14 */ 4,8, 4,8, 4,8, 4,8, /* rate 14 2 */
242/*15 */ 4,8, 8,8, 4,8, 8,8, /* rate 14 3 */
243
244/*16 */ 8,8, 8,8, 8,8, 8,8, /* rates 15 0, 15 1, 15 2, 15 3 (increment by 8) */
245/*17 */ 16,16,16,16,16,16,16,16, /* rates 15 2, 15 3 for attack */
246/*18 */ 0,0, 0,0, 0,0, 0,0, /* infinity rates for attack and decay(s) */
247};
248
249
250#define O(a) (a*RATE_STEPS)
251
252/*note that there is no O(17) in this table - it's directly in the code */
253static const UINT8 eg_rate_select[32+64+32]={   /* Envelope Generator rates (32 + 64 rates + 32 RKS) */
254/* 32 infinite time rates */
255O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),
256O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),
257O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),
258O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),
259
260/* rates 00-11 */
261O( 0),O( 1),O( 2),O( 3),
262O( 0),O( 1),O( 2),O( 3),
263O( 0),O( 1),O( 2),O( 3),
264O( 0),O( 1),O( 2),O( 3),
265O( 0),O( 1),O( 2),O( 3),
266O( 0),O( 1),O( 2),O( 3),
267O( 0),O( 1),O( 2),O( 3),
268O( 0),O( 1),O( 2),O( 3),
269O( 0),O( 1),O( 2),O( 3),
270O( 0),O( 1),O( 2),O( 3),
271O( 0),O( 1),O( 2),O( 3),
272O( 0),O( 1),O( 2),O( 3),
273
274/* rate 12 */
275O( 4),O( 5),O( 6),O( 7),
276
277/* rate 13 */
278O( 8),O( 9),O(10),O(11),
279
280/* rate 14 */
281O(12),O(13),O(14),O(15),
282
283/* rate 15 */
284O(16),O(16),O(16),O(16),
285
286/* 32 dummy rates (same as 15 3) */
287O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16),
288O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16),
289O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16),
290O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16)
291
292};
293
294static const UINT8 eg_rate_select2612[32+64+32]={    /* Envelope Generator rates (32 + 64 rates + 32 RKS) from tests on YM2612 */
295/* 32 infinite time rates */
296O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),
297O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),
298O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),
299O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),
300
301/* rates 00-11 */
302/*
303O( 0),O( 1),O( 2),O( 3),
304O( 0),O( 1),O( 2),O( 3),
305*/
306O(18),O(18),O( 0),O( 0),
307O( 0),O( 0),O( 2),O( 2),   // Nemesis's tests
308
309O( 0),O( 1),O( 2),O( 3),
310O( 0),O( 1),O( 2),O( 3),
311O( 0),O( 1),O( 2),O( 3),
312O( 0),O( 1),O( 2),O( 3),
313O( 0),O( 1),O( 2),O( 3),
314O( 0),O( 1),O( 2),O( 3),
315O( 0),O( 1),O( 2),O( 3),
316O( 0),O( 1),O( 2),O( 3),
317O( 0),O( 1),O( 2),O( 3),
318O( 0),O( 1),O( 2),O( 3),
319
320/* rate 12 */
321O( 4),O( 5),O( 6),O( 7),
322
323/* rate 13 */
324O( 8),O( 9),O(10),O(11),
325
326/* rate 14 */
327O(12),O(13),O(14),O(15),
328
329/* rate 15 */
330O(16),O(16),O(16),O(16),
331
332/* 32 dummy rates (same as 15 3) */
333O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16),
334O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16),
335O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16),
336O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16)
337
338};
339#undef O
340
341/*rate  0,    1,    2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15*/
342/*shift 11,  10,  9,  8,  7,  6,  5,  4,  3,  2, 1,  0,  0,  0,  0,  0 */
343/*mask  2047, 1023, 511, 255, 127, 63, 31, 15, 7,  3, 1,  0,  0,  0,  0,  0 */
344
345#define O(a) (a*1)
346static const UINT8 eg_rate_shift[32+64+32]={   /* Envelope Generator counter shifts (32 + 64 rates + 32 RKS) */
347/* 32 infinite time rates */
348O(0),O(0),O(0),O(0),O(0),O(0),O(0),O(0),
349O(0),O(0),O(0),O(0),O(0),O(0),O(0),O(0),
350O(0),O(0),O(0),O(0),O(0),O(0),O(0),O(0),
351O(0),O(0),O(0),O(0),O(0),O(0),O(0),O(0),
352
353/* rates 00-11 */
354O(11),O(11),O(11),O(11),
355O(10),O(10),O(10),O(10),
356O( 9),O( 9),O( 9),O( 9),
357O( 8),O( 8),O( 8),O( 8),
358O( 7),O( 7),O( 7),O( 7),
359O( 6),O( 6),O( 6),O( 6),
360O( 5),O( 5),O( 5),O( 5),
361O( 4),O( 4),O( 4),O( 4),
362O( 3),O( 3),O( 3),O( 3),
363O( 2),O( 2),O( 2),O( 2),
364O( 1),O( 1),O( 1),O( 1),
365O( 0),O( 0),O( 0),O( 0),
366
367/* rate 12 */
368O( 0),O( 0),O( 0),O( 0),
369
370/* rate 13 */
371O( 0),O( 0),O( 0),O( 0),
372
373/* rate 14 */
374O( 0),O( 0),O( 0),O( 0),
375
376/* rate 15 */
377O( 0),O( 0),O( 0),O( 0),
378
379/* 32 dummy rates (same as 15 3) */
380O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),
381O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),
382O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),
383O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0)
384
385};
386#undef O
387
388static const UINT8 dt_tab[4 * 32]={
389/* this is YM2151 and YM2612 phase increment data (in 10.10 fixed point format)*/
390/* FD=0 */
391   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
392   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
393/* FD=1 */
394   0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2,
395   2, 3, 3, 3, 4, 4, 4, 5, 5, 6, 6, 7, 8, 8, 8, 8,
396/* FD=2 */
397   1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5,
398   5, 6, 6, 7, 8, 8, 9,10,11,12,13,14,16,16,16,16,
399/* FD=3 */
400   2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 6, 6, 7,
401   8 , 8, 9,10,11,12,13,14,16,17,19,20,22,22,22,22
402};
403
404
405/* OPN key frequency number -> key code follow table */
406/* fnum higher 4bit -> keycode lower 2bit */
407static const UINT8 opn_fktable[16] = {0,0,0,0,0,0,0,1,2,3,3,3,3,3,3,3};
408
409
410/* 8 LFO speed parameters */
411/* each value represents number of samples that one LFO level will last for */
412static const UINT32 lfo_samples_per_step[8] = {108, 77, 71, 67, 62, 44, 8, 5};
413
414
415
416/*There are 4 different LFO AM depths available, they are:
417  0 dB, 1.4 dB, 5.9 dB, 11.8 dB
418  Here is how it is generated (in EG steps):
419
420  11.8 dB = 0, 2, 4, 6, 8, 10,12,14,16...126,126,124,122,120,118,....4,2,0
421   5.9 dB = 0, 1, 2, 3, 4, 5, 6, 7, 8....63, 63, 62, 61, 60, 59,.....2,1,0
422   1.4 dB = 0, 0, 0, 0, 1, 1, 1, 1, 2,...15, 15, 15, 15, 14, 14,.....0,0,0
423
424  (1.4 dB is loosing precision as you can see)
425
426  It's implemented as generator from 0..126 with step 2 then a shift
427  right N times, where N is:
428    8 for 0 dB
429    3 for 1.4 dB
430    1 for 5.9 dB
431    0 for 11.8 dB
432*/
433static const UINT8 lfo_ams_depth_shift[4] = {8, 3, 1, 0};
434
435
436
437/*There are 8 different LFO PM depths available, they are:
438  0, 3.4, 6.7, 10, 14, 20, 40, 80 (cents)
439
440  Modulation level at each depth depends on F-NUMBER bits: 4,5,6,7,8,9,10
441  (bits 8,9,10 = FNUM MSB from OCT/FNUM register)
442
443  Here we store only first quarter (positive one) of full waveform.
444  Full table (lfo_pm_table) containing all 128 waveforms is build
445  at run (init) time.
446
447  One value in table below represents 4 (four) basic LFO steps
448  (1 PM step = 4 AM steps).
449
450  For example:
451   at LFO SPEED=0 (which is 108 samples per basic LFO step)
452   one value from "lfo_pm_output" table lasts for 432 consecutive
453   samples (4*108=432) and one full LFO waveform cycle lasts for 13824
454   samples (32*432=13824; 32 because we store only a quarter of whole
455            waveform in the table below)
456*/
457static const UINT8 lfo_pm_output[7*8][8]={ /* 7 bits meaningful (of F-NUMBER), 8 LFO output levels per one depth (out of 32), 8 LFO depths */
458/* FNUM BIT 4: 000 0001xxxx */
459/* DEPTH 0 */ {0,   0,   0,   0,   0,   0,   0,   0},
460/* DEPTH 1 */ {0,   0,   0,   0,   0,   0,   0,   0},
461/* DEPTH 2 */ {0,   0,   0,   0,   0,   0,   0,   0},
462/* DEPTH 3 */ {0,   0,   0,   0,   0,   0,   0,   0},
463/* DEPTH 4 */ {0,   0,   0,   0,   0,   0,   0,   0},
464/* DEPTH 5 */ {0,   0,   0,   0,   0,   0,   0,   0},
465/* DEPTH 6 */ {0,   0,   0,   0,   0,   0,   0,   0},
466/* DEPTH 7 */ {0,   0,   0,   0,   1,   1,   1,   1},
467
468/* FNUM BIT 5: 000 0010xxxx */
469/* DEPTH 0 */ {0,   0,   0,   0,   0,   0,   0,   0},
470/* DEPTH 1 */ {0,   0,   0,   0,   0,   0,   0,   0},
471/* DEPTH 2 */ {0,   0,   0,   0,   0,   0,   0,   0},
472/* DEPTH 3 */ {0,   0,   0,   0,   0,   0,   0,   0},
473/* DEPTH 4 */ {0,   0,   0,   0,   0,   0,   0,   0},
474/* DEPTH 5 */ {0,   0,   0,   0,   0,   0,   0,   0},
475/* DEPTH 6 */ {0,   0,   0,   0,   1,   1,   1,   1},
476/* DEPTH 7 */ {0,   0,   1,   1,   2,   2,   2,   3},
477
478/* FNUM BIT 6: 000 0100xxxx */
479/* DEPTH 0 */ {0,   0,   0,   0,   0,   0,   0,   0},
480/* DEPTH 1 */ {0,   0,   0,   0,   0,   0,   0,   0},
481/* DEPTH 2 */ {0,   0,   0,   0,   0,   0,   0,   0},
482/* DEPTH 3 */ {0,   0,   0,   0,   0,   0,   0,   0},
483/* DEPTH 4 */ {0,   0,   0,   0,   0,   0,   0,   1},
484/* DEPTH 5 */ {0,   0,   0,   0,   1,   1,   1,   1},
485/* DEPTH 6 */ {0,   0,   1,   1,   2,   2,   2,   3},
486/* DEPTH 7 */ {0,   0,   2,   3,   4,   4,   5,   6},
487
488/* FNUM BIT 7: 000 1000xxxx */
489/* DEPTH 0 */ {0,   0,   0,   0,   0,   0,   0,   0},
490/* DEPTH 1 */ {0,   0,   0,   0,   0,   0,   0,   0},
491/* DEPTH 2 */ {0,   0,   0,   0,   0,   0,   1,   1},
492/* DEPTH 3 */ {0,   0,   0,   0,   1,   1,   1,   1},
493/* DEPTH 4 */ {0,   0,   0,   1,   1,   1,   1,   2},
494/* DEPTH 5 */ {0,   0,   1,   1,   2,   2,   2,   3},
495/* DEPTH 6 */ {0,   0,   2,   3,   4,   4,   5,   6},
496/* DEPTH 7 */ {0,   0,   4,   6,   8,   8, 0xa, 0xc},
497
498/* FNUM BIT 8: 001 0000xxxx */
499/* DEPTH 0 */ {0,   0,   0,   0,   0,   0,   0,   0},
500/* DEPTH 1 */ {0,   0,   0,   0,   1,   1,   1,   1},
501/* DEPTH 2 */ {0,   0,   0,   1,   1,   1,   2,   2},
502/* DEPTH 3 */ {0,   0,   1,   1,   2,   2,   3,   3},
503/* DEPTH 4 */ {0,   0,   1,   2,   2,   2,   3,   4},
504/* DEPTH 5 */ {0,   0,   2,   3,   4,   4,   5,   6},
505/* DEPTH 6 */ {0,   0,   4,   6,   8,   8, 0xa, 0xc},
506/* DEPTH 7 */ {0,   0,   8, 0xc,0x10,0x10,0x14,0x18},
507
508/* FNUM BIT 9: 010 0000xxxx */
509/* DEPTH 0 */ {0,   0,   0,   0,   0,   0,   0,   0},
510/* DEPTH 1 */ {0,   0,   0,   0,   2,   2,   2,   2},
511/* DEPTH 2 */ {0,   0,   0,   2,   2,   2,   4,   4},
512/* DEPTH 3 */ {0,   0,   2,   2,   4,   4,   6,   6},
513/* DEPTH 4 */ {0,   0,   2,   4,   4,   4,   6,   8},
514/* DEPTH 5 */ {0,   0,   4,   6,   8,   8, 0xa, 0xc},
515/* DEPTH 6 */ {0,   0,   8, 0xc,0x10,0x10,0x14,0x18},
516/* DEPTH 7 */ {0,   0,0x10,0x18,0x20,0x20,0x28,0x30},
517
518/* FNUM BIT10: 100 0000xxxx */
519/* DEPTH 0 */ {0,   0,   0,   0,   0,   0,   0,   0},
520/* DEPTH 1 */ {0,   0,   0,   0,   4,   4,   4,   4},
521/* DEPTH 2 */ {0,   0,   0,   4,   4,   4,   8,   8},
522/* DEPTH 3 */ {0,   0,   4,   4,   8,   8, 0xc, 0xc},
523/* DEPTH 4 */ {0,   0,   4,   8,   8,   8, 0xc,0x10},
524/* DEPTH 5 */ {0,   0,   8, 0xc,0x10,0x10,0x14,0x18},
525/* DEPTH 6 */ {0,   0,0x10,0x18,0x20,0x20,0x28,0x30},
526/* DEPTH 7 */ {0,   0,0x20,0x30,0x40,0x40,0x50,0x60},
527
528};
529
530/* all 128 LFO PM waveforms */
531static INT32 lfo_pm_table[128*8*32]; /* 128 combinations of 7 bits meaningful (of F-NUMBER), 8 LFO depths, 32 LFO output levels per one depth */
532
533/* register number to channel number , slot offset */
534#define OPN_CHAN(N) (N&3)
535#define OPN_SLOT(N) ((N>>2)&3)
536
537/* slot number */
538#define SLOT1 0
539#define SLOT2 2
540#define SLOT3 1
541#define SLOT4 3
542
543/* bit0 = Right enable , bit1 = Left enable */
544#define OUTD_RIGHT  1
545#define OUTD_LEFT   2
546#define OUTD_CENTER 3
547
548
549/* save output as raw 16-bit sample */
550/* #define SAVE_SAMPLE */
551
552#ifdef SAVE_SAMPLE
553static FILE *sample[1];
554   #if 1   /*save to MONO file */
555      #define SAVE_ALL_CHANNELS \
556      {   signed int pom = lt; \
557         fputc((unsigned short)pom&0xff,sample[0]); \
558         fputc(((unsigned short)pom>>8)&0xff,sample[0]); \
559      }
560   #else   /*save to STEREO file */
561      #define SAVE_ALL_CHANNELS \
562      {   signed int pom = lt; \
563         fputc((unsigned short)pom&0xff,sample[0]); \
564         fputc(((unsigned short)pom>>8)&0xff,sample[0]); \
565         pom = rt; \
566         fputc((unsigned short)pom&0xff,sample[0]); \
567         fputc(((unsigned short)pom>>8)&0xff,sample[0]); \
568      }
569   #endif
570#endif
571
572
573/* struct describing a single operator (SLOT) */
574typedef struct
575{
576   INT32   *DT;      /* detune          :dt_tab[DT] */
577   UINT8   KSR;      /* key scale rate  :3-KSR */
578   UINT32   ar;         /* attack rate  */
579   UINT32   d1r;      /* decay rate   */
580   UINT32   d2r;      /* sustain rate */
581   UINT32   rr;         /* release rate */
582   UINT8   ksr;      /* key scale rate  :kcode>>(3-KSR) */
583   UINT32   mul;      /* multiple        :ML_TABLE[ML] */
584
585   /* Phase Generator */
586   UINT32   phase;      /* phase counter */
587   INT32   Incr;      /* phase step */
588
589   /* Envelope Generator */
590   UINT8   state;      /* phase type */
591   UINT32   tl;         /* total level: TL << 3 */
592   INT32   volume;      /* envelope counter */
593   UINT32   sl;         /* sustain level:sl_table[SL] */
594   UINT32   vol_out;   /* current output from EG circuit (without AM from LFO) */
595
596   UINT8   eg_sh_ar;   /*  (attack state) */
597   UINT8   eg_sel_ar;   /*  (attack state) */
598   UINT8   eg_sh_d1r;   /*  (decay state) */
599   UINT8   eg_sel_d1r;   /*  (decay state) */
600   UINT8   eg_sh_d2r;   /*  (sustain state) */
601   UINT8   eg_sel_d2r;   /*  (sustain state) */
602   UINT8   eg_sh_rr;   /*  (release state) */
603   UINT8   eg_sel_rr;   /*  (release state) */
604
605   UINT8   ssg;      /* SSG-EG waveform */
606   UINT8   ssgn;      /* SSG-EG negated output */
607
608   UINT8   key;      /* 0=last key was KEY OFF, 1=KEY ON */
609
610   /* LFO */
611   UINT32   AMmask;      /* AM enable flag */
612
613} FM_SLOT;
614
615typedef struct
616{
617   FM_SLOT   SLOT[4];   /* four SLOTs (operators) */
618
619   UINT8   ALGO;      /* algorithm */
620   UINT8   FB;         /* feedback shift */
621   INT32   op1_out[2];   /* op1 output for feedback */
622
623   INT32   *connect1;   /* SLOT1 output pointer */
624   INT32   *connect3;   /* SLOT3 output pointer */
625   INT32   *connect2;   /* SLOT2 output pointer */
626   INT32   *connect4;   /* SLOT4 output pointer */
627
628   INT32   *mem_connect;/* where to put the delayed sample (MEM) */
629   INT32   mem_value;   /* delayed sample (MEM) value */
630
631   INT32   pms;      /* channel PMS */
632   UINT8   ams;      /* channel AMS */
633
634   UINT32   fc;         /* fnum,blk:adjusted to sample rate */
635   UINT8   kcode;      /* key code:                        */
636   UINT32   block_fnum;   /* current blk/fnum value for this slot (can be different betweeen slots of one channel in 3slot mode) */
637} FM_CH;
638
639
640typedef struct
641{
642   const device_config *device;
643   void *      param;            /* this chip parameter  */
644   double      freqbase;         /* frequency base       */
645   int         timer_prescaler;   /* timer prescaler      */
646   UINT8      irq;            /* interrupt level      */
647   UINT8      irqmask;         /* irq mask             */
648#if FM_BUSY_FLAG_SUPPORT
649   TIME_TYPE   busy_expiry_time;   /* expiry time of the busy status */
650#endif
651   UINT32       clock;            /* master clock  (Hz)   */
652   UINT32       rate;            /* sampling rate (Hz)   */
653   UINT16      address;         /* address register     */
654   UINT8      status;            /* status flag          */
655   UINT32      mode;            /* mode  CSM / 3SLOT    */
656   UINT8      fn_h;            /* freq latch           */
657   UINT8      prescaler_sel;      /* prescaler selector   */
658   INT32      TA;               /* timer a              */
659   INT32      TAC;            /* timer a counter      */
660   UINT8      TB;               /* timer b              */
661   INT32      TBC;            /* timer b counter      */
662   /* local time tables */
663   INT32      dt_tab[8][32];      /* DeTune table         */
664   /* Extention Timer and IRQ handler */
665   FM_TIMERHANDLER   timer_handler;
666   FM_IRQHANDLER   IRQ_Handler;
667   const ssg_callbacks *SSG;
668} FM_ST;
669
670
671
672/***********************************************************/
673/* OPN unit                                                */
674/***********************************************************/
675
676/* OPN 3slot struct */
677typedef struct
678{
679   UINT32  fc[3];         /* fnum3,blk3: calculated */
680   UINT8   fn_h;         /* freq3 latch */
681   UINT8   kcode[3];      /* key code */
682   UINT32   block_fnum[3];   /* current fnum value for this slot (can be different betweeen slots of one channel in 3slot mode) */
683   UINT8   key_csm;        /* CSM mode Key-ON flag */
684} FM_3SLOT;
685
686/* OPN/A/B common state */
687typedef struct
688{
689   UINT8   type;         /* chip type */
690   FM_ST   ST;            /* general state */
691   FM_3SLOT SL3;         /* 3 slot mode state */
692   FM_CH   *P_CH;         /* pointer of CH */
693   unsigned int pan[6*2];   /* fm channels output masks (0xffffffff = enable) */
694
695   UINT32   eg_cnt;         /* global envelope generator counter */
696   UINT32   eg_timer;      /* global envelope generator counter works at frequency = chipclock/64/3 */
697   UINT32   eg_timer_add;   /* step of eg_timer */
698   UINT32   eg_timer_overflow;/* envelope generator timer overlfows every 3 samples (on real chip) */
699
700
701   /* there are 2048 FNUMs that can be generated using FNUM/BLK registers
702      but LFO works with one more bit of a precision so we really need 4096 elements */
703   UINT32  fn_table[4096]; /* fnumber->increment counter */
704   UINT32 fn_max;    /* maximal phase increment (used for phase overflow) */
705
706   /* LFO */
707   UINT32   lfo_cnt;
708   UINT32   lfo_inc;
709   UINT32   lfo_freq[8];   /* LFO FREQ table */
710} FM_OPN;
711
712
713
714/* current chip state */
715static INT32   m2,c1,c2;      /* Phase Modulation input for operators 2,3,4 */
716static INT32   mem;         /* one sample delay memory */
717
718static INT32   out_fm[8];      /* outputs of working channels */
719
720static UINT32   LFO_AM;         /* runtime LFO calculations helper */
721static INT32   LFO_PM;         /* runtime LFO calculations helper */
722
723/* log output level */
724#define LOG_ERR  3      /* ERROR       */
725#define LOG_WAR  2      /* WARNING     */
726#define LOG_INF  1      /* INFORMATION */
727#define LOG_LEVEL LOG_INF
728
729#ifndef __RAINE__
730#define LOG(n,x) do { if( (n)>=LOG_LEVEL ) logerror x; } while (0)
731#endif
732
733/* limitter */
734#define Limit(val, max,min) { \
735   if ( val > max )      val = max; \
736   else if ( val < min ) val = min; \
737}
738
739
740/* status set and IRQ handling */
741INLINE void FM_STATUS_SET(FM_ST *ST,int flag)
742{
743   /* set status flag */
744   ST->status |= flag;
745   if ( !(ST->irq) && (ST->status & ST->irqmask) )
746   {
747      ST->irq = 1;
748      /* callback user interrupt handler (IRQ is OFF to ON) */
749      if(ST->IRQ_Handler) (ST->IRQ_Handler)(ST->param,1);
750   }
751}
752
753/* status reset and IRQ handling */
754INLINE void FM_STATUS_RESET(FM_ST *ST,int flag)
755{
756   /* reset status flag */
757   ST->status &=~flag;
758   if ( (ST->irq) && !(ST->status & ST->irqmask) )
759   {
760      ST->irq = 0;
761      /* callback user interrupt handler (IRQ is ON to OFF) */
762      if(ST->IRQ_Handler) (ST->IRQ_Handler)(ST->param,0);
763   }
764}
765
766/* IRQ mask set */
767INLINE void FM_IRQMASK_SET(FM_ST *ST,int flag)
768{
769   ST->irqmask = flag;
770   /* IRQ handling check */
771   FM_STATUS_SET(ST,0);
772   FM_STATUS_RESET(ST,0);
773}
774
775INLINE void FM_KEYON(FM_OPN *OPN, FM_CH *CH , int s )
776{
777   FM_SLOT *SLOT = &CH->SLOT[s];
778
779   if( !SLOT->key && !OPN->SL3.key_csm)
780   {
781      /* restart Phase Generator */
782      SLOT->phase = 0;
783
784      /* reset SSG-EG inversion flag */
785      SLOT->ssgn = 0;
786
787      if ((SLOT->ar + SLOT->ksr) < 94 /*32+62*/)
788      {
789         SLOT->state = (SLOT->volume <= MIN_ATT_INDEX) ? ((SLOT->sl == MIN_ATT_INDEX) ? EG_SUS : EG_DEC) : EG_ATT;
790      }
791      else
792      {
793         /* force attenuation level to 0 */
794         SLOT->volume = MIN_ATT_INDEX;
795
796         /* directly switch to Decay (or Sustain) */
797         SLOT->state = (SLOT->sl == MIN_ATT_INDEX) ? EG_SUS : EG_DEC;
798      }
799
800      /* recalculate EG output */
801      if ((SLOT->ssg&0x08) && (SLOT->ssgn ^ (SLOT->ssg&0x04)))
802         SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;
803      else
804         SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
805   }
806
807   SLOT->key = 1;
808}
809
810INLINE void FM_KEYOFF(FM_OPN *OPN, FM_CH *CH , int s )
811{
812   FM_SLOT *SLOT = &CH->SLOT[s];
813
814   if (SLOT->key && !OPN->SL3.key_csm)
815   {
816      if (SLOT->state>EG_REL)
817      {
818         SLOT->state = EG_REL; /* phase -> Release */
819
820         /* SSG-EG specific update */
821         if (SLOT->ssg&0x08)
822         {
823            /* convert EG attenuation level */
824            if (SLOT->ssgn ^ (SLOT->ssg&0x04))
825                    SLOT->volume = (0x200 - SLOT->volume);
826
827            /* force EG attenuation level */
828            if (SLOT->volume >= 0x200)
829            {
830               SLOT->volume = MAX_ATT_INDEX;
831               SLOT->state  = EG_OFF;
832            }
833
834            /* recalculate EG output */
835            SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
836         }
837      }
838   }
839
840   SLOT->key = 0;
841}
842
843INLINE void FM_KEYON_CSM(FM_OPN *OPN, FM_CH *CH , int s )
844{
845   FM_SLOT *SLOT = &CH->SLOT[s];
846
847   if( !SLOT->key && !OPN->SL3.key_csm)
848   {
849      /* restart Phase Generator */
850      SLOT->phase = 0;
851
852      /* reset SSG-EG inversion flag */
853      SLOT->ssgn = 0;
854
855      if ((SLOT->ar + SLOT->ksr) < 94 /*32+62*/)
856      {
857         SLOT->state = (SLOT->volume <= MIN_ATT_INDEX) ? ((SLOT->sl == MIN_ATT_INDEX) ? EG_SUS : EG_DEC) : EG_ATT;
858      }
859      else
860      {
861         /* force attenuation level to 0 */
862         SLOT->volume = MIN_ATT_INDEX;
863
864         /* directly switch to Decay (or Sustain) */
865         SLOT->state = (SLOT->sl == MIN_ATT_INDEX) ? EG_SUS : EG_DEC;
866      }
867
868      /* recalculate EG output */
869      if ((SLOT->ssg&0x08) && (SLOT->ssgn ^ (SLOT->ssg&0x04)))
870         SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;
871      else
872         SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
873   }
874}
875
876INLINE void FM_KEYOFF_CSM(FM_CH *CH , int s )
877{
878   FM_SLOT *SLOT = &CH->SLOT[s];
879   if (!SLOT->key)
880   {
881      if (SLOT->state>EG_REL)
882      {
883         SLOT->state = EG_REL; /* phase -> Release */
884
885         /* SSG-EG specific update */
886         if (SLOT->ssg&0x08)
887         {
888            /* convert EG attenuation level */
889            if (SLOT->ssgn ^ (SLOT->ssg&0x04))
890               SLOT->volume = (0x200 - SLOT->volume);
891
892            /* force EG attenuation level */
893            if (SLOT->volume >= 0x200)
894            {
895               SLOT->volume = MAX_ATT_INDEX;
896               SLOT->state  = EG_OFF;
897            }
898
899            /* recalculate EG output */
900            SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
901         }
902      }
903   }
904}
905
906/* OPN Mode Register Write */
907INLINE void set_timers( FM_OPN *OPN, FM_ST *ST, void *n, int v )
908{
909   /* b7 = CSM MODE */
910   /* b6 = 3 slot mode */
911   /* b5 = reset b */
912   /* b4 = reset a */
913   /* b3 = timer enable b */
914   /* b2 = timer enable a */
915   /* b1 = load b */
916   /* b0 = load a */
917   ST->mode = v;
918
919   if ((OPN->ST.mode ^ v) & 0xC0)
920   {
921      OPN->P_CH[2].SLOT[SLOT1].Incr=-1; /* recalculate phase (from Gens) */
922
923      /* CSM mode disabled and CSM key ON active*/
924      if (((v & 0xC0) != 0x80) && OPN->SL3.key_csm)
925      {
926         /* CSM Mode Key OFF (verified by Nemesis on real hardware) */
927         FM_KEYOFF_CSM(&OPN->P_CH[2],SLOT1);
928         FM_KEYOFF_CSM(&OPN->P_CH[2],SLOT2);
929         FM_KEYOFF_CSM(&OPN->P_CH[2],SLOT3);
930         FM_KEYOFF_CSM(&OPN->P_CH[2],SLOT4);
931         OPN->SL3.key_csm = 0;
932      }
933   }
934
935   /* reset Timer b flag */
936   if( v & 0x20 )
937      FM_STATUS_RESET(ST,0x02);
938   /* reset Timer a flag */
939   if( v & 0x10 )
940      FM_STATUS_RESET(ST,0x01);
941   /* load b */
942   if( v & 0x02 )
943   {
944      if( ST->TBC == 0 )
945      {
946         ST->TBC = ( 256-ST->TB)<<4;
947         /* External timer handler */
948         if (ST->timer_handler) (ST->timer_handler)(n,1,ST->TBC * ST->timer_prescaler,ST->clock);
949      }
950   }
951   else
952   {   /* stop timer b */
953      if( ST->TBC != 0 )
954      {
955         ST->TBC = 0;
956         if (ST->timer_handler) (ST->timer_handler)(n,1,0,ST->clock);
957      }
958   }
959   /* load a */
960   if( v & 0x01 )
961   {
962      if( ST->TAC == 0 )
963      {
964         ST->TAC = (1024-ST->TA);
965         /* External timer handler */
966         if (ST->timer_handler) (ST->timer_handler)(n,0,ST->TAC * ST->timer_prescaler,ST->clock);
967      }
968   }
969   else
970   {   /* stop timer a */
971      if( ST->TAC != 0 )
972      {
973         ST->TAC = 0;
974         if (ST->timer_handler) (ST->timer_handler)(n,0,0,ST->clock);
975      }
976   }
977}
978
979
980/* Timer A Overflow */
981INLINE void TimerAOver(FM_ST *ST)
982{
983   /* set status (if enabled) */
984   if(ST->mode & 0x04) FM_STATUS_SET(ST,0x01);
985   /* clear or reload the counter */
986   ST->TAC = (1024-ST->TA);
987   if (ST->timer_handler) (ST->timer_handler)(ST->param,0,ST->TAC * ST->timer_prescaler,ST->clock);
988}
989/* Timer B Overflow */
990INLINE void TimerBOver(FM_ST *ST)
991{
992   /* set status (if enabled) */
993   if(ST->mode & 0x08) FM_STATUS_SET(ST,0x02);
994   /* clear or reload the counter */
995   ST->TBC = ( 256-ST->TB)<<4;
996   if (ST->timer_handler) (ST->timer_handler)(ST->param,1,ST->TBC * ST->timer_prescaler,ST->clock);
997}
998
999
1000#if FM_INTERNAL_TIMER
1001/* ----- internal timer mode , update timer */
1002
1003/* ---------- calculate timer A ---------- */
1004   #define INTERNAL_TIMER_A(ST,CSM_CH)               \
1005   {                                       \
1006      if( ST->TAC &&  (ST->timer_handler==0) )      \
1007         if( (ST->TAC -= (int)(ST->freqbase*4096)) <= 0 )   \
1008         {                                 \
1009            TimerAOver( ST );                  \
1010            /* CSM mode total level latch and auto key on */   \
1011            if( ST->mode & 0x80 )               \
1012               CSMKeyControll( CSM_CH );         \
1013         }                                 \
1014   }
1015/* ---------- calculate timer B ---------- */
1016   #define INTERNAL_TIMER_B(ST,step)                  \
1017   {                                          \
1018      if( ST->TBC && (ST->timer_handler==0) )            \
1019         if( (ST->TBC -= (int)(ST->freqbase*4096*step)) <= 0 )   \
1020            TimerBOver( ST );                     \
1021   }
1022#else /* FM_INTERNAL_TIMER */
1023/* external timer mode */
1024#define INTERNAL_TIMER_A(ST,CSM_CH)
1025#define INTERNAL_TIMER_B(ST,step)
1026#endif /* FM_INTERNAL_TIMER */
1027
1028
1029
1030#if FM_BUSY_FLAG_SUPPORT
1031#define FM_BUSY_CLEAR(ST) ((ST)->busy_expiry_time = UNDEFINED_TIME)
1032INLINE UINT8 FM_STATUS_FLAG(FM_ST *ST)
1033{
1034   if( COMPARE_TIMES(ST->busy_expiry_time, UNDEFINED_TIME) != 0 )
1035   {
1036      if (COMPARE_TIMES(ST->busy_expiry_time, FM_GET_TIME_NOW(ST->device->machine)) > 0)
1037         return ST->status | 0x80;   /* with busy */
1038      /* expire */
1039      FM_BUSY_CLEAR(ST);
1040   }
1041   return ST->status;
1042}
1043INLINE void FM_BUSY_SET(FM_ST *ST,int busyclock )
1044{
1045   TIME_TYPE expiry_period = MULTIPLY_TIME_BY_INT(ATTOTIME_IN_HZ(ST->clock), busyclock * ST->timer_prescaler);
1046   ST->busy_expiry_time = ADD_TIMES(FM_GET_TIME_NOW(ST->device->machine), expiry_period);
1047}
1048#else
1049#define FM_STATUS_FLAG(ST) ((ST)->status)
1050#define FM_BUSY_SET(ST,bclock) {}
1051#define FM_BUSY_CLEAR(ST) {}
1052#endif
1053
1054
1055/* set algorithm connection */
1056static void setup_connection( FM_CH *CH, int ch )
1057{
1058   INT32 *carrier = &out_fm[ch];
1059
1060   INT32 **om1 = &CH->connect1;
1061   INT32 **om2 = &CH->connect3;
1062   INT32 **oc1 = &CH->connect2;
1063
1064   INT32 **memc = &CH->mem_connect;
1065
1066   switch( CH->ALGO ){
1067   case 0:
1068      /* M1---C1---MEM---M2---C2---OUT */
1069      *om1 = &c1;
1070      *oc1 = &mem;
1071      *om2 = &c2;
1072      *memc= &m2;
1073      break;
1074   case 1:
1075      /* M1------+-MEM---M2---C2---OUT */
1076      /*      C1-+                     */
1077      *om1 = &mem;
1078      *oc1 = &mem;
1079      *om2 = &c2;
1080      *memc= &m2;
1081      break;
1082   case 2:
1083      /* M1-----------------+-C2---OUT */
1084      /*      C1---MEM---M2-+          */
1085      *om1 = &c2;
1086      *oc1 = &mem;
1087      *om2 = &c2;
1088      *memc= &m2;
1089      break;
1090   case 3:
1091      /* M1---C1---MEM------+-C2---OUT */
1092      /*                 M2-+          */
1093      *om1 = &c1;
1094      *oc1 = &mem;
1095      *om2 = &c2;
1096      *memc= &c2;
1097      break;
1098   case 4:
1099      /* M1---C1-+-OUT */
1100      /* M2---C2-+     */
1101      /* MEM: not used */
1102      *om1 = &c1;
1103      *oc1 = carrier;
1104      *om2 = &c2;
1105      *memc= &mem;   /* store it anywhere where it will not be used */
1106      break;
1107   case 5:
1108      /*    +----C1----+     */
1109      /* M1-+-MEM---M2-+-OUT */
1110      /*    +----C2----+     */
1111      *om1 = 0;   /* special mark */
1112      *oc1 = carrier;
1113      *om2 = carrier;
1114      *memc= &m2;
1115      break;
1116   case 6:
1117      /* M1---C1-+     */
1118      /*      M2-+-OUT */
1119      /*      C2-+     */
1120      /* MEM: not used */
1121      *om1 = &c1;
1122      *oc1 = carrier;
1123      *om2 = carrier;
1124      *memc= &mem;   /* store it anywhere where it will not be used */
1125      break;
1126   case 7:
1127      /* M1-+     */
1128      /* C1-+-OUT */
1129      /* M2-+     */
1130      /* C2-+     */
1131      /* MEM: not used*/
1132      *om1 = carrier;
1133      *oc1 = carrier;
1134      *om2 = carrier;
1135      *memc= &mem;   /* store it anywhere where it will not be used */
1136      break;
1137   }
1138
1139   CH->connect4 = carrier;
1140}
1141
1142/* set detune & multiple */
1143INLINE void set_det_mul(FM_ST *ST,FM_CH *CH,FM_SLOT *SLOT,int v)
1144{
1145   SLOT->mul = (v&0x0f)? (v&0x0f)*2 : 1;
1146   SLOT->DT  = ST->dt_tab[(v>>4)&7];
1147   CH->SLOT[SLOT1].Incr=-1;
1148}
1149
1150/* set total level */
1151INLINE void set_tl(FM_CH *CH,FM_SLOT *SLOT , int v)
1152{
1153   SLOT->tl = (v&0x7f)<<(ENV_BITS-7); /* 7bit TL */
1154
1155   /* recalculate EG output */
1156   if ((SLOT->ssg&0x08) && (SLOT->ssgn ^ (SLOT->ssg&0x04)) && (SLOT->state > EG_REL))
1157      SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;
1158   else
1159      SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
1160}
1161
1162/* set attack rate & key scale  */
1163INLINE void set_ar_ksr(UINT8 type, FM_CH *CH,FM_SLOT *SLOT,int v)
1164{
1165   UINT8 old_KSR = SLOT->KSR;
1166
1167   SLOT->ar = (v&0x1f) ? 32 + ((v&0x1f)<<1) : 0;
1168
1169   SLOT->KSR = 3-(v>>6);
1170   if (SLOT->KSR != old_KSR)
1171   {
1172      CH->SLOT[SLOT1].Incr=-1;
1173   }
1174
1175   /* Even if it seems unnecessary, in some odd case, KSR and KC are both modified   */
1176   /* and could result in SLOT->kc remaining unchanged.                              */
1177   /* In such case, AR values would not be recalculated despite SLOT->ar has changed */
1178   /* This fixes the introduction music of Batman & Robin    (Eke-Eke)               */
1179   if ((SLOT->ar + SLOT->ksr) < 94 /*32+62*/)
1180   {
1181      SLOT->eg_sh_ar  = eg_rate_shift [SLOT->ar  + SLOT->ksr ];
1182      SLOT->eg_sel_ar = eg_rate_select2612[SLOT->ar  + SLOT->ksr ];
1183   }
1184   else
1185   {
1186      SLOT->eg_sh_ar  = 0;
1187      SLOT->eg_sel_ar = 18*RATE_STEPS;   /* verified by Nemesis on real hardware */
1188   }
1189}
1190
1191/* set decay rate */
1192INLINE void set_dr(UINT8 type, FM_SLOT *SLOT,int v)
1193{
1194   SLOT->d1r = (v&0x1f) ? 32 + ((v&0x1f)<<1) : 0;
1195
1196   SLOT->eg_sh_d1r = eg_rate_shift [SLOT->d1r + SLOT->ksr];
1197   SLOT->eg_sel_d1r= eg_rate_select2612[SLOT->d1r + SLOT->ksr];
1198}
1199
1200/* set sustain rate */
1201INLINE void set_sr(UINT8 type, FM_SLOT *SLOT,int v)
1202{
1203   SLOT->d2r = (v&0x1f) ? 32 + ((v&0x1f)<<1) : 0;
1204
1205   SLOT->eg_sh_d2r = eg_rate_shift [SLOT->d2r + SLOT->ksr];
1206   SLOT->eg_sel_d2r= eg_rate_select2612[SLOT->d2r + SLOT->ksr];
1207}
1208
1209/* set release rate */
1210INLINE void set_sl_rr(UINT8 type, FM_SLOT *SLOT,int v)
1211{
1212   SLOT->sl = sl_table[ v>>4 ];
1213
1214   /* check EG state changes */
1215   if ((SLOT->state == EG_DEC) && (SLOT->volume >= (INT32)(SLOT->sl)))
1216      SLOT->state = EG_SUS;
1217
1218   SLOT->rr  = 34 + ((v&0x0f)<<2);
1219
1220   SLOT->eg_sh_rr  = eg_rate_shift [SLOT->rr  + SLOT->ksr];
1221   SLOT->eg_sel_rr = eg_rate_select2612[SLOT->rr  + SLOT->ksr];
1222}
1223
1224
1225
1226INLINE signed int op_calc(UINT32 phase, unsigned int env, signed int pm)
1227{
1228   UINT32 p;
1229
1230   p = (env<<3) + sin_tab[ ( ((signed int)((phase & ~FREQ_MASK) + (pm<<15))) >> FREQ_SH ) & SIN_MASK ];
1231
1232   if (p >= TL_TAB_LEN)
1233      return 0;
1234   return tl_tab[p];
1235}
1236
1237INLINE signed int op_calc1(UINT32 phase, unsigned int env, signed int pm)
1238{
1239   UINT32 p;
1240
1241   p = (env<<3) + sin_tab[ ( ((signed int)((phase & ~FREQ_MASK) + pm      )) >> FREQ_SH ) & SIN_MASK ];
1242
1243   if (p >= TL_TAB_LEN)
1244      return 0;
1245   return tl_tab[p];
1246}
1247
1248/* advance LFO to next sample */
1249INLINE void advance_lfo(FM_OPN *OPN)
1250{
1251   int pos;
1252
1253   if (OPN->lfo_inc)  /* LFO enabled ? */
1254   {
1255      /* increment LFO counter */
1256      /* when LFO is enabled, one level will last for 108, 77, 71, 67, 62, 44, 8 or 5 samples */
1257      OPN->lfo_cnt +=  OPN->lfo_inc;
1258
1259      /* LFO current position */
1260      pos = ( OPN->lfo_cnt >> LFO_SH) & 127;
1261
1262      /* triangle */
1263      /* AM: 0 to 126 step +2, 126 to 0 step -2 */
1264      if (pos<64)
1265         LFO_AM = pos * 2;
1266      else
1267         LFO_AM = 126 - ((pos&63) * 2);
1268
1269      /* PM works with 4 times slower clock */
1270      LFO_PM = pos >> 2;
1271   }
1272   /* when LFO is disabled, current level is held (fix Spider-Man & Venom : Separation Anxiety) */
1273}
1274
1275/* changed from INLINE to static here to work around gcc 4.2.1 codegen bug */
1276static void advance_eg_channel(FM_OPN *OPN, FM_SLOT *SLOT)
1277{
1278   unsigned int out;
1279   unsigned int i = 4; /* four operators per channel */
1280
1281   do
1282   {
1283      switch(SLOT->state)
1284      {
1285         case EG_ATT:    /* attack phase */
1286         if (!(OPN->eg_cnt & ((1<<SLOT->eg_sh_ar)-1)))
1287         {
1288                 /* update attenuation level */
1289                 SLOT->volume += (~SLOT->volume * (eg_inc[SLOT->eg_sel_ar + ((OPN->eg_cnt>>SLOT->eg_sh_ar)&7)]))>>4;
1290
1291                 /* check phase transition*/
1292                 if (SLOT->volume <= MIN_ATT_INDEX)
1293                 {
1294                    SLOT->volume = MIN_ATT_INDEX;
1295                    SLOT->state = (SLOT->sl == MIN_ATT_INDEX) ? EG_SUS : EG_DEC; /* special case where SL=0 */
1296                 }
1297
1298                 /* recalculate EG output */
1299                 if ((SLOT->ssg&0x08) && (SLOT->ssgn ^ (SLOT->ssg&0x04)))  /* SSG-EG Output Inversion */
1300               SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;
1301                 else
1302                    SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
1303         }
1304         break;
1305
1306         case EG_DEC:  /* decay phase */
1307         if (!(OPN->eg_cnt & ((1<<SLOT->eg_sh_d1r)-1)))
1308         {
1309                 /* SSG EG type */
1310                 if (SLOT->ssg&0x08)
1311                 {
1312                    /* update attenuation level */
1313                    if (SLOT->volume < 0x200)
1314               {
1315                  SLOT->volume += 4 * eg_inc[SLOT->eg_sel_d1r + ((OPN->eg_cnt>>SLOT->eg_sh_d1r)&7)];
1316
1317                  /* recalculate EG output */
1318                  if (SLOT->ssgn ^ (SLOT->ssg&0x04))   /* SSG-EG Output Inversion */
1319                     SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;
1320                  else
1321                     SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
1322               }
1323
1324                 }
1325                 else
1326                 {
1327               /* update attenuation level */
1328               SLOT->volume += eg_inc[SLOT->eg_sel_d1r + ((OPN->eg_cnt>>SLOT->eg_sh_d1r)&7)];
1329
1330               /* recalculate EG output */
1331               SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
1332                 }
1333
1334                 /* check phase transition*/
1335                 if (SLOT->volume >= (INT32)(SLOT->sl))
1336                    SLOT->state = EG_SUS;
1337         }
1338         break;
1339
1340         case EG_SUS:  /* sustain phase */
1341         if (!(OPN->eg_cnt & ((1<<SLOT->eg_sh_d2r)-1)))
1342         {
1343                 /* SSG EG type */
1344                 if (SLOT->ssg&0x08)
1345                 {
1346               /* update attenuation level */
1347               if (SLOT->volume < 0x200)
1348               {
1349                  SLOT->volume += 4 * eg_inc[SLOT->eg_sel_d2r + ((OPN->eg_cnt>>SLOT->eg_sh_d2r)&7)];
1350
1351                  /* recalculate EG output */
1352                  if (SLOT->ssgn ^ (SLOT->ssg&0x04))   /* SSG-EG Output Inversion */
1353                     SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;
1354                  else
1355                     SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
1356               }
1357                 }
1358                 else
1359                 {
1360                    /* update attenuation level */
1361                    SLOT->volume += eg_inc[SLOT->eg_sel_d2r + ((OPN->eg_cnt>>SLOT->eg_sh_d2r)&7)];
1362
1363                    /* check phase transition*/
1364                    if ( SLOT->volume >= MAX_ATT_INDEX )
1365                       SLOT->volume = MAX_ATT_INDEX;
1366                    /* do not change SLOT->state (verified on real chip) */
1367
1368                    /* recalculate EG output */
1369                    SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
1370                 }
1371         }
1372         break;
1373
1374         case EG_REL:  /* release phase */
1375         if (!(OPN->eg_cnt & ((1<<SLOT->eg_sh_rr)-1)))
1376         {
1377                 /* SSG EG type */
1378                 if (SLOT->ssg&0x08)
1379                 {
1380                    /* update attenuation level */
1381                    if (SLOT->volume < 0x200)
1382                       SLOT->volume += 4 * eg_inc[SLOT->eg_sel_rr + ((OPN->eg_cnt>>SLOT->eg_sh_rr)&7)];
1383               /* check phase transition */
1384               if (SLOT->volume >= 0x200)
1385               {
1386                  SLOT->volume = MAX_ATT_INDEX;
1387                  SLOT->state = EG_OFF;
1388               }
1389                 }
1390                 else
1391                 {
1392                    /* update attenuation level */
1393                    SLOT->volume += eg_inc[SLOT->eg_sel_rr + ((OPN->eg_cnt>>SLOT->eg_sh_rr)&7)];
1394
1395                    /* check phase transition*/
1396                    if (SLOT->volume >= MAX_ATT_INDEX)
1397                    {
1398                       SLOT->volume = MAX_ATT_INDEX;
1399                       SLOT->state = EG_OFF;
1400                    }
1401                 }
1402
1403                 /* recalculate EG output */
1404                 SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
1405
1406         }
1407         break;
1408      }
1409
1410      out = ((UINT32)SLOT->volume);
1411
1412                /* negate output (changes come from alternate bit, init comes from attack bit) */
1413      if ((SLOT->ssg&0x08) && (SLOT->ssgn&2) && (SLOT->state > EG_REL))
1414         out ^= MAX_ATT_INDEX;
1415
1416      /* we need to store the result here because we are going to change ssgn
1417            in next instruction */
1418      SLOT->vol_out = out + SLOT->tl;
1419
1420      SLOT++;
1421      i--;
1422   }while (i);
1423
1424}
1425
1426/* SSG-EG update process */
1427/* The behavior is based upon Nemesis tests on real hardware */
1428/* This is actually executed before each samples */
1429static void update_ssg_eg_channel(FM_SLOT *SLOT)
1430{
1431   unsigned int i = 4; /* four operators per channel */
1432
1433   do
1434   {
1435      /* detect SSG-EG transition */
1436      /* this is not required during release phase as the attenuation has been forced to MAX and output invert flag is not used */
1437      /* if an Attack Phase is programmed, inversion can occur on each sample */
1438      if ((SLOT->ssg & 0x08) && (SLOT->volume >= 0x200) && (SLOT->state > EG_REL))
1439      {
1440         if (SLOT->ssg & 0x01)  /* bit 0 = hold SSG-EG */
1441         {
1442            /* set inversion flag */
1443                 if (SLOT->ssg & 0x02)
1444                    SLOT->ssgn = 4;
1445
1446            /* force attenuation level during decay phases */
1447            if ((SLOT->state != EG_ATT) && !(SLOT->ssgn ^ (SLOT->ssg & 0x04)))
1448               SLOT->volume  = MAX_ATT_INDEX;
1449         }
1450         else  /* loop SSG-EG */
1451         {
1452            /* toggle output inversion flag or reset Phase Generator */
1453                 if (SLOT->ssg & 0x02)
1454                    SLOT->ssgn ^= 4;
1455                 else
1456                    SLOT->phase = 0;
1457
1458            /* same as Key ON */
1459            if (SLOT->state != EG_ATT)
1460            {
1461               if ((SLOT->ar + SLOT->ksr) < 94 /*32+62*/)
1462               {
1463                  SLOT->state = (SLOT->volume <= MIN_ATT_INDEX) ? ((SLOT->sl == MIN_ATT_INDEX) ? EG_SUS : EG_DEC) : EG_ATT;
1464               }
1465               else
1466               {
1467                  /* Attack Rate is maximal: directly switch to Decay or Substain */
1468                  SLOT->volume = MIN_ATT_INDEX;
1469                  SLOT->state = (SLOT->sl == MIN_ATT_INDEX) ? EG_SUS : EG_DEC;
1470               }
1471            }
1472         }
1473
1474         /* recalculate EG output */
1475         if (SLOT->ssgn ^ (SLOT->ssg&0x04))
1476            SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;
1477         else
1478            SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
1479      }
1480
1481      /* next slot */
1482      SLOT++;
1483      i--;
1484   } while (i);
1485}
1486
1487
1488#define volume_calc(OP) ((OP)->vol_out + (AM & (OP)->AMmask))
1489
1490INLINE void update_phase_lfo_slot(FM_OPN *OPN, FM_SLOT *SLOT, INT32 pms, UINT32 block_fnum)
1491{
1492   UINT32 fnum_lfo   = ((block_fnum & 0x7f0) >> 4) * 32 * 8;
1493   INT32  lfo_fn_table_index_offset = lfo_pm_table[ fnum_lfo + pms + LFO_PM ];
1494
1495   block_fnum = block_fnum*2 + lfo_fn_table_index_offset;
1496
1497   if (lfo_fn_table_index_offset)    /* LFO phase modulation active */
1498   {
1499      UINT8 blk = (block_fnum&0x7000) >> 12;
1500      UINT32 fn  = block_fnum & 0xfff;
1501
1502      /* recalculate keyscale code */
1503      int kc = (blk<<2) | opn_fktable[fn >> 7];
1504
1505      /* recalculate (frequency) phase increment counter */
1506      int fc = (OPN->fn_table[fn]>>(7-blk)) + SLOT->DT[kc];
1507
1508      /* (frequency) phase overflow (credits to Nemesis) */
1509      if (fc < 0) fc += OPN->fn_max;
1510
1511      /* update phase */
1512      SLOT->phase += (fc * SLOT->mul) >> 1;
1513   }
1514   else    /* LFO phase modulation  = zero */
1515   {
1516      SLOT->phase += SLOT->Incr;
1517   }
1518}
1519
1520INLINE void update_phase_lfo_channel(FM_OPN *OPN, FM_CH *CH)
1521{
1522   UINT32 block_fnum = CH->block_fnum;
1523
1524   UINT32 fnum_lfo  = ((block_fnum & 0x7f0) >> 4) * 32 * 8;
1525   INT32  lfo_fn_table_index_offset = lfo_pm_table[ fnum_lfo + CH->pms + LFO_PM ];
1526
1527   block_fnum = block_fnum*2 + lfo_fn_table_index_offset;
1528
1529   if (lfo_fn_table_index_offset)    /* LFO phase modulation active */
1530   {
1531      UINT8 blk = (block_fnum&0x7000) >> 12;
1532      UINT32 fn  = block_fnum & 0xfff;
1533
1534      /* recalculate keyscale code */
1535      int kc = (blk<<2) | opn_fktable[fn >> 7];
1536
1537      /* recalculate (frequency) phase increment counter */
1538      int fc = (OPN->fn_table[fn]>>(7-blk));
1539
1540      /* (frequency) phase overflow (credits to Nemesis) */
1541      int finc = fc + CH->SLOT[SLOT1].DT[kc];
1542      if (finc < 0) finc += OPN->fn_max;
1543      CH->SLOT[SLOT1].phase += (finc*CH->SLOT[SLOT1].mul) >> 1;
1544
1545      finc = fc + CH->SLOT[SLOT2].DT[kc];
1546      if (finc < 0) finc += OPN->fn_max;
1547      CH->SLOT[SLOT2].phase += (finc*CH->SLOT[SLOT2].mul) >> 1;
1548
1549      finc = fc + CH->SLOT[SLOT3].DT[kc];
1550      if (finc < 0) finc += OPN->fn_max;
1551      CH->SLOT[SLOT3].phase += (finc*CH->SLOT[SLOT3].mul) >> 1;
1552
1553      finc = fc + CH->SLOT[SLOT4].DT[kc];
1554      if (finc < 0) finc += OPN->fn_max;
1555      CH->SLOT[SLOT4].phase += (finc*CH->SLOT[SLOT4].mul) >> 1;
1556   }
1557   else    /* LFO phase modulation  = zero */
1558   {
1559           CH->SLOT[SLOT1].phase += CH->SLOT[SLOT1].Incr;
1560           CH->SLOT[SLOT2].phase += CH->SLOT[SLOT2].Incr;
1561           CH->SLOT[SLOT3].phase += CH->SLOT[SLOT3].Incr;
1562           CH->SLOT[SLOT4].phase += CH->SLOT[SLOT4].Incr;
1563   }
1564}
1565
1566INLINE void chan_calc(FM_OPN *OPN, FM_CH *CH, int chnum)
1567{
1568   unsigned int eg_out;
1569
1570   UINT32 AM = LFO_AM >> CH->ams;
1571
1572
1573   m2 = c1 = c2 = mem = 0;
1574
1575   *CH->mem_connect = CH->mem_value;   /* restore delayed sample (MEM) value to m2 or c2 */
1576
1577   eg_out = volume_calc(&CH->SLOT[SLOT1]);
1578   {
1579      INT32 out = CH->op1_out[0] + CH->op1_out[1];
1580      CH->op1_out[0] = CH->op1_out[1];
1581
1582      if( !CH->connect1 ){
1583         /* algorithm 5  */
1584         mem = c1 = c2 = CH->op1_out[0];
1585      }
1586      else
1587      {
1588         /* other algorithms */
1589         *CH->connect1 += CH->op1_out[0];
1590      }
1591
1592      CH->op1_out[1] = 0;
1593      if( eg_out < ENV_QUIET )   /* SLOT 1 */
1594      {
1595         if (!CH->FB)
1596            out=0;
1597
1598         CH->op1_out[1] = op_calc1(CH->SLOT[SLOT1].phase, eg_out, (out<<CH->FB) );
1599      }
1600   }
1601
1602   eg_out = volume_calc(&CH->SLOT[SLOT3]);
1603   if( eg_out < ENV_QUIET )      /* SLOT 3 */
1604      *CH->connect3 += op_calc(CH->SLOT[SLOT3].phase, eg_out, m2);
1605
1606   eg_out = volume_calc(&CH->SLOT[SLOT2]);
1607   if( eg_out < ENV_QUIET )      /* SLOT 2 */
1608      *CH->connect2 += op_calc(CH->SLOT[SLOT2].phase, eg_out, c1);
1609
1610   eg_out = volume_calc(&CH->SLOT[SLOT4]);
1611   if( eg_out < ENV_QUIET )      /* SLOT 4 */
1612      *CH->connect4 += op_calc(CH->SLOT[SLOT4].phase, eg_out, c2);
1613
1614
1615   /* store current MEM */
1616   CH->mem_value = mem;
1617
1618   /* update phase counters AFTER output calculations */
1619   if(CH->pms)
1620   {
1621      /* add support for 3 slot mode */
1622      if ((OPN->ST.mode & 0xC0) && (chnum == 2))
1623      {
1624              update_phase_lfo_slot(OPN, &CH->SLOT[SLOT1], CH->pms, OPN->SL3.block_fnum[1]);
1625              update_phase_lfo_slot(OPN, &CH->SLOT[SLOT2], CH->pms, OPN->SL3.block_fnum[2]);
1626              update_phase_lfo_slot(OPN, &CH->SLOT[SLOT3], CH->pms, OPN->SL3.block_fnum[0]);
1627              update_phase_lfo_slot(OPN, &CH->SLOT[SLOT4], CH->pms, CH->block_fnum);
1628      }
1629      else update_phase_lfo_channel(OPN, CH);
1630   }
1631   else   /* no LFO phase modulation */
1632   {
1633      CH->SLOT[SLOT1].phase += CH->SLOT[SLOT1].Incr;
1634      CH->SLOT[SLOT2].phase += CH->SLOT[SLOT2].Incr;
1635      CH->SLOT[SLOT3].phase += CH->SLOT[SLOT3].Incr;
1636      CH->SLOT[SLOT4].phase += CH->SLOT[SLOT4].Incr;
1637   }
1638}
1639
1640/* update phase increment and envelope generator */
1641INLINE void refresh_fc_eg_slot(FM_OPN *OPN, FM_SLOT *SLOT , int fc , int kc )
1642{
1643   int ksr = kc >> SLOT->KSR;
1644
1645   fc += SLOT->DT[kc];
1646
1647   /* detects frequency overflow (credits to Nemesis) */
1648   if (fc < 0) fc += OPN->fn_max;
1649
1650   /* (frequency) phase increment counter */
1651   SLOT->Incr = (fc * SLOT->mul) >> 1;
1652
1653   if( SLOT->ksr != ksr )
1654   {
1655      SLOT->ksr = ksr;
1656
1657      /* calculate envelope generator rates */
1658      if ((SLOT->ar + SLOT->ksr) < 32+62)
1659      {
1660         SLOT->eg_sh_ar  = eg_rate_shift [SLOT->ar  + SLOT->ksr ];
1661         SLOT->eg_sel_ar = eg_rate_select2612[SLOT->ar  + SLOT->ksr ];
1662      }
1663      else
1664      {
1665         SLOT->eg_sh_ar  = 0;
1666         SLOT->eg_sel_ar = 18*RATE_STEPS; /* verified by Nemesis on real hardware (Attack phase is blocked) */
1667      }
1668
1669      SLOT->eg_sh_d1r = eg_rate_shift [SLOT->d1r + SLOT->ksr];
1670      SLOT->eg_sh_d2r = eg_rate_shift [SLOT->d2r + SLOT->ksr];
1671      SLOT->eg_sh_rr  = eg_rate_shift [SLOT->rr  + SLOT->ksr];
1672
1673      SLOT->eg_sel_d1r= eg_rate_select2612[SLOT->d1r + SLOT->ksr];
1674      SLOT->eg_sel_d2r= eg_rate_select2612[SLOT->d2r + SLOT->ksr];
1675      SLOT->eg_sel_rr = eg_rate_select2612[SLOT->rr  + SLOT->ksr];
1676   }
1677}
1678
1679/* update phase increment counters */
1680/* Changed from INLINE to static to work around gcc 4.2.1 codegen bug */
1681static void refresh_fc_eg_chan(FM_OPN *OPN, FM_CH *CH )
1682{
1683   if( CH->SLOT[SLOT1].Incr==-1){
1684      int fc = CH->fc;
1685      int kc = CH->kcode;
1686      refresh_fc_eg_slot(OPN, &CH->SLOT[SLOT1] , fc , kc );
1687      refresh_fc_eg_slot(OPN, &CH->SLOT[SLOT2] , fc , kc );
1688      refresh_fc_eg_slot(OPN, &CH->SLOT[SLOT3] , fc , kc );
1689      refresh_fc_eg_slot(OPN, &CH->SLOT[SLOT4] , fc , kc );
1690   }
1691}
1692
1693/* initialize time tables */
1694static void init_timetables( FM_ST *ST , const UINT8 *dttable )
1695{
1696   int i,d;
1697   double rate;
1698
1699#if 0
1700   logerror("FM.C: samplerate=%8i chip clock=%8i  freqbase=%f  \n",
1701          ST->rate, ST->clock, ST->freqbase );
1702#endif
1703
1704   /* DeTune table */
1705   for (d = 0;d <= 3;d++){
1706      for (i = 0;i <= 31;i++){
1707         rate = ((double)dttable[d*32 + i]) * SIN_LEN  * ST->freqbase  * (1<<FREQ_SH) / ((double)(1<<20));
1708         ST->dt_tab[d][i]   = (INT32) rate;
1709         ST->dt_tab[d+4][i] = -ST->dt_tab[d][i];
1710#if 0
1711         logerror("FM.C: DT [%2i %2i] = %8x  \n", d, i, ST->dt_tab[d][i] );
1712#endif
1713      }
1714   }
1715
1716}
1717
1718
1719static void reset_channels( FM_ST *ST , FM_CH *CH , int num )
1720{
1721   int c,s;
1722
1723   ST->mode   = 0;   /* normal mode */
1724   ST->TA     = 0;
1725   ST->TAC    = 0;
1726   ST->TB     = 0;
1727   ST->TBC    = 0;
1728
1729   for( c = 0 ; c < num ; c++ )
1730   {
1731      CH[c].fc = 0;
1732      for(s = 0 ; s < 4 ; s++ )
1733      {
1734         CH[c].SLOT[s].ssg = 0;
1735         CH[c].SLOT[s].ssgn = 0;
1736         CH[c].SLOT[s].state= EG_OFF;
1737         CH[c].SLOT[s].volume = MAX_ATT_INDEX;
1738         CH[c].SLOT[s].vol_out= MAX_ATT_INDEX;
1739      }
1740   }
1741}
1742
1743/* initialize generic tables */
1744static int init_tables(void)
1745{
1746   signed int i,x;
1747   signed int n;
1748   double o,m;
1749
1750   for (x=0; x<TL_RES_LEN; x++)
1751   {
1752      m = (1<<16) / pow(2, (x+1) * (ENV_STEP/4.0) / 8.0);
1753      m = floor(m);
1754
1755      /* we never reach (1<<16) here due to the (x+1) */
1756      /* result fits within 16 bits at maximum */
1757
1758      n = (int)m;      /* 16 bits here */
1759      n >>= 4;      /* 12 bits here */
1760      if (n&1)      /* round to nearest */
1761         n = (n>>1)+1;
1762      else
1763         n = n>>1;
1764                  /* 11 bits here (rounded) */
1765      n <<= 2;      /* 13 bits here (as in real chip) */
1766      tl_tab[ x*2 + 0 ] = n;
1767      tl_tab[ x*2 + 1 ] = -tl_tab[ x*2 + 0 ];
1768
1769      for (i=1; i<13; i++)
1770      {
1771         tl_tab[ x*2+0 + i*2*TL_RES_LEN ] =  tl_tab[ x*2+0 ]>>i;
1772         tl_tab[ x*2+1 + i*2*TL_RES_LEN ] = -tl_tab[ x*2+0 + i*2*TL_RES_LEN ];
1773      }
1774   #if 0
1775         logerror("tl %04i", x);
1776         for (i=0; i<13; i++)
1777            logerror(", [%02i] %4x", i*2, tl_tab[ x*2 /*+1*/ + i*2*TL_RES_LEN ]);
1778         logerror("\n");
1779      }
1780   #endif
1781   }
1782   /*logerror("FM.C: TL_TAB_LEN = %i elements (%i bytes)\n",TL_TAB_LEN, (int)sizeof(tl_tab));*/
1783
1784
1785   for (i=0; i<SIN_LEN; i++)
1786   {
1787      /* non-standard sinus */
1788      m = sin( ((i*2)+1) * M_PI / SIN_LEN ); /* checked against the real chip */
1789
1790      /* we never reach zero here due to ((i*2)+1) */
1791
1792      if (m>0.0)
1793         o = 8*log(1.0/m)/log(2.0);   /* convert to 'decibels' */
1794      else
1795         o = 8*log(-1.0/m)/log(2.0);   /* convert to 'decibels' */
1796
1797      o = o / (ENV_STEP/4);
1798
1799      n = (int)(2.0*o);
1800      if (n&1)                  /* round to nearest */
1801         n = (n>>1)+1;
1802      else
1803         n = n>>1;
1804
1805      sin_tab[ i ] = n*2 + (m>=0.0? 0: 1 );
1806      /*logerror("FM.C: sin [%4i]= %4i (tl_tab value=%5i)\n", i, sin_tab[i],tl_tab[sin_tab[i]]);*/
1807   }
1808
1809   /*logerror("FM.C: ENV_QUIET= %08x\n",ENV_QUIET );*/
1810
1811
1812   /* build LFO PM modulation table */
1813   for(i = 0; i < 8; i++) /* 8 PM depths */
1814   {
1815      UINT8 fnum;
1816      for (fnum=0; fnum<128; fnum++) /* 7 bits meaningful of F-NUMBER */
1817      {
1818         UINT8 value;
1819         UINT8 step;
1820         UINT32 offset_depth = i;
1821         UINT32 offset_fnum_bit;
1822         UINT32 bit_tmp;
1823
1824         for (step=0; step<8; step++)
1825         {
1826            value = 0;
1827            for (bit_tmp=0; bit_tmp<7; bit_tmp++) /* 7 bits */
1828            {
1829               if (fnum & (1<<bit_tmp)) /* only if bit "bit_tmp" is set */
1830               {
1831                  offset_fnum_bit = bit_tmp * 8;
1832                  value += lfo_pm_output[offset_fnum_bit + offset_depth][step];
1833               }
1834            }
1835            lfo_pm_table[(fnum*32*8) + (i*32) + step   + 0] = value;
1836            lfo_pm_table[(fnum*32*8) + (i*32) +(step^7)+ 8] = value;
1837            lfo_pm_table[(fnum*32*8) + (i*32) + step   +16] = -value;
1838            lfo_pm_table[(fnum*32*8) + (i*32) +(step^7)+24] = -value;
1839         }
1840#if 0
1841         logerror("LFO depth=%1x FNUM=%04x (<<4=%4x): ", i, fnum, fnum<<4);
1842         for (step=0; step<16; step++) /* dump only positive part of waveforms */
1843            logerror("%02x ", lfo_pm_table[(fnum*32*8) + (i*32) + step] );
1844         logerror("\n");
1845#endif
1846
1847      }
1848   }
1849
1850
1851
1852#ifdef SAVE_SAMPLE
1853   sample[0]=fopen("sampsum.pcm","wb");
1854#endif
1855
1856   return 1;
1857
1858}
1859
1860
1861
1862static void FMCloseTable( void )
1863{
1864#ifdef SAVE_SAMPLE
1865   fclose(sample[0]);
1866#endif
1867   return;
1868}
1869
1870
1871/* CSM Key Controll */
1872INLINE void CSMKeyControll(FM_OPN *OPN, FM_CH *CH)
1873{
1874   /* all key ON (verified by Nemesis on real hardware) */
1875   FM_KEYON_CSM(OPN,CH,SLOT1);
1876   FM_KEYON_CSM(OPN,CH,SLOT2);
1877   FM_KEYON_CSM(OPN,CH,SLOT3);
1878   FM_KEYON_CSM(OPN,CH,SLOT4);
1879   OPN->SL3.key_csm = 1;
1880}
1881
1882#ifdef __STATE_H__
1883/* FM channel save , internal state only */
1884static void FMsave_state_channel(const device_config *device,FM_CH *CH,int num_ch)
1885{
1886   int slot , ch;
1887
1888   for(ch=0;ch<num_ch;ch++,CH++)
1889   {
1890      /* channel */
1891      state_save_register_device_item_array(device, ch, CH->op1_out);
1892      state_save_register_device_item(device, ch, CH->fc);
1893      /* slots */
1894      for(slot=0;slot<4;slot++)
1895      {
1896         FM_SLOT *SLOT = &CH->SLOT[slot];
1897         state_save_register_device_item(device, ch * 4 + slot, SLOT->phase);
1898         state_save_register_device_item(device, ch * 4 + slot, SLOT->state);
1899         state_save_register_device_item(device, ch * 4 + slot, SLOT->volume);
1900      }
1901   }
1902}
1903
1904static void FMsave_state_st(const device_config *device,FM_ST *ST)
1905{
1906#if FM_BUSY_FLAG_SUPPORT
1907   state_save_register_device_item(device, 0, ST->busy_expiry_time.seconds );
1908   state_save_register_device_item(device, 0, ST->busy_expiry_time.attoseconds );
1909#endif
1910   state_save_register_device_item(device, 0, ST->address );
1911   state_save_register_device_item(device, 0, ST->irq     );
1912   state_save_register_device_item(device, 0, ST->irqmask );
1913   state_save_register_device_item(device, 0, ST->status  );
1914   state_save_register_device_item(device, 0, ST->mode    );
1915   state_save_register_device_item(device, 0, ST->prescaler_sel );
1916   state_save_register_device_item(device, 0, ST->fn_h );
1917   state_save_register_device_item(device, 0, ST->TA   );
1918   state_save_register_device_item(device, 0, ST->TAC  );
1919   state_save_register_device_item(device, 0, ST->TB  );
1920   state_save_register_device_item(device, 0, ST->TBC  );
1921}
1922#endif /* _STATE_H */
1923
1924#if BUILD_OPN
1925
1926
1927
1928/* prescaler set (and make time tables) */
1929static void OPNSetPres(FM_OPN *OPN, int pres, int timer_prescaler, int SSGpres)
1930{
1931   int i;
1932
1933   /* frequency base */
1934   OPN->ST.freqbase = (OPN->ST.rate) ? ((double)OPN->ST.clock / OPN->ST.rate) / pres : 0;
1935
1936#if 0
1937   OPN->ST.rate = (double)OPN->ST.clock / pres;
1938   OPN->ST.freqbase = 1.0;
1939#endif
1940
1941   OPN->eg_timer_add  = (1<<EG_SH)  *  OPN->ST.freqbase;
1942   OPN->eg_timer_overflow = ( 3 ) * (1<<EG_SH);
1943
1944
1945   /* Timer base time */
1946   OPN->ST.timer_prescaler = timer_prescaler;
1947
1948   /* SSG part  prescaler set */
1949   if( SSGpres ) (*OPN->ST.SSG->set_clock)( OPN->ST.param, OPN->ST.clock * 2 / SSGpres );
1950
1951   /* make time tables */
1952   init_timetables( &OPN->ST, dt_tab );
1953
1954   /* there are 2048 FNUMs that can be generated using FNUM/BLK registers
1955      but LFO works with one more bit of a precision so we really need 4096 elements */
1956   for(i = 0; i < 4096; i++)
1957   {
1958      /* freq table for octave 7 */
1959      /* OPN phase increment counter = 20bit */
1960      /* the correct formula is : F-Number = (144 * fnote * 2^20 / M) / 2^(B-1) */
1961      /* where sample clock is  M/144 */
1962      /* this means the increment value for one clock sample is FNUM * 2^(B-1) = FNUM * 64 for octave 7 */
1963      /* we also need to handle the ratio between the chip frequency and the emulated frequency (can be 1.0)  */
1964      OPN->fn_table[i] = (UINT32)( (double)i * 32 * OPN->ST.freqbase * (1<<(FREQ_SH-10)) ); /* -10 because chip works with 10.10 fixed point, while we use 16.16 */
1965#if 0
1966      logerror("FM.C: fn_table[%4i] = %08x (dec=%8i)\n",
1967             i, OPN->fn_table[i]>>6,OPN->fn_table[i]>>6 );
1968#endif
1969   }
1970
1971   /* maximal frequency is required for Phase overflow calculation, register size is 17 bits (Nemesis) */
1972   OPN->fn_max = (UINT32)( (double)0x20000 * OPN->ST.freqbase * (1<<(FREQ_SH-10)) );
1973
1974   /* LFO freq. table */
1975   for(i = 0; i < 8; i++)
1976   {
1977      /* Amplitude modulation: 64 output levels (triangle waveform); 1 level lasts for one of "lfo_samples_per_step" samples */
1978      /* Phase modulation: one entry from lfo_pm_output lasts for one of 4 * "lfo_samples_per_step" samples  */
1979      OPN->lfo_freq[i] = (1.0 / lfo_samples_per_step[i]) * (1<<LFO_SH) * OPN->ST.freqbase;
1980#if 0
1981      logerror("FM.C: lfo_freq[%i] = %08x (dec=%8i)\n",
1982             i, OPN->lfo_freq[i],OPN->lfo_freq[i] );
1983#endif
1984   }
1985}
1986
1987
1988
1989/* write a OPN mode register 0x20-0x2f */
1990static void OPNWriteMode(FM_OPN *OPN, int r, int v)
1991{
1992   UINT8 c;
1993   FM_CH *CH;
1994
1995   switch(r){
1996   case 0x21:   /* Test */
1997      break;
1998   case 0x22:   /* LFO FREQ (YM2608/YM2610/YM2610B/YM2612) */
1999      if (v&0x08) /* LFO enabled ? */
2000      {
2001         if (!OPN->lfo_inc)
2002         {
2003                 /* restart LFO */
2004                 OPN->lfo_cnt  = 0;
2005                 LFO_AM  = 0;
2006                 LFO_PM  = 0;
2007         }
2008
2009         OPN->lfo_inc = OPN->lfo_freq[v&7];
2010      }
2011      else
2012      {
2013              OPN->lfo_inc = 0;
2014      }
2015      break;
2016   case 0x24:   /* timer A High 8*/
2017      OPN->ST.TA = (OPN->ST.TA & 0x03)|(((int)v)<<2);
2018      break;
2019   case 0x25:   /* timer A Low 2*/
2020      OPN->ST.TA = (OPN->ST.TA & 0x3fc)|(v&3);
2021      break;
2022   case 0x26:   /* timer B */
2023      OPN->ST.TB = v;
2024      break;
2025   case 0x27:   /* mode, timer control */
2026      set_timers( OPN, &(OPN->ST),OPN->ST.param,v );
2027      break;
2028   case 0x28:   /* key on / off */
2029      c = v & 0x03;
2030      if( c == 3 ) break;
2031      if( (v&0x04) && (OPN->type & TYPE_6CH) ) c+=3;
2032      CH = OPN->P_CH;
2033      CH = &CH[c];
2034      if(v&0x10) FM_KEYON(OPN,CH,SLOT1); else FM_KEYOFF(OPN,CH,SLOT1);
2035      if(v&0x20) FM_KEYON(OPN,CH,SLOT2); else FM_KEYOFF(OPN,CH,SLOT2);
2036      if(v&0x40) FM_KEYON(OPN,CH,SLOT3); else FM_KEYOFF(OPN,CH,SLOT3);
2037      if(v&0x80) FM_KEYON(OPN,CH,SLOT4); else FM_KEYOFF(OPN,CH,SLOT4);
2038      break;
2039   }
2040}
2041
2042/* write a OPN register (0x30-0xff) */
2043static void OPNWriteReg(FM_OPN *OPN, int r, int v)
2044{
2045   FM_CH *CH;
2046   FM_SLOT *SLOT;
2047
2048   UINT8 c = OPN_CHAN(r);
2049
2050   if (c == 3) return; /* 0xX3,0xX7,0xXB,0xXF */
2051
2052   if (r >= 0x100) c+=3;
2053
2054   CH = OPN->P_CH;
2055   CH = &CH[c];
2056
2057   SLOT = &(CH->SLOT[OPN_SLOT(r)]);
2058
2059   switch( r & 0xf0 ) {
2060   case 0x30:   /* DET , MUL */
2061      set_det_mul(&OPN->ST,CH,SLOT,v);
2062      break;
2063
2064   case 0x40:   /* TL */
2065      set_tl(CH,SLOT,v);
2066      break;
2067
2068   case 0x50:   /* KS, AR */
2069      set_ar_ksr(OPN->type,CH,SLOT,v);
2070      break;
2071
2072   case 0x60:   /* bit7 = AM ENABLE, DR */
2073      set_dr(OPN->type, SLOT,v);
2074
2075      if(OPN->type & TYPE_LFOPAN) /* YM2608/2610/2610B/2612 */
2076      {
2077         SLOT->AMmask = (v&0x80) ? ~0 : 0;
2078      }
2079      break;
2080
2081   case 0x70:   /*     SR */
2082      set_sr(OPN->type,SLOT,v);
2083      break;
2084
2085   case 0x80:   /* SL, RR */
2086      set_sl_rr(OPN->type,SLOT,v);
2087      break;
2088
2089   case 0x90:   /* SSG-EG */
2090      SLOT->ssg  =  v&0x0f;
2091     
2092         /* recalculate EG output */
2093      if ((SLOT->ssg&0x08) && (SLOT->ssgn ^ (SLOT->ssg&0x04)) && (SLOT->state > EG_REL))
2094         SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;
2095      else
2096         SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
2097
2098      /* SSG-EG envelope shapes :
2099
2100        E AtAlH
2101        1 0 0 0  \\\\
2102
2103        1 0 0 1  \___
2104
2105        1 0 1 0  \/\/
2106                  ___
2107        1 0 1 1  \
2108
2109        1 1 0 0  ////
2110                  ___
2111        1 1 0 1  /
2112
2113        1 1 1 0  /\/\
2114
2115        1 1 1 1  /___
2116
2117
2118        E = SSG-EG enable
2119
2120
2121        The shapes are generated using Attack, Decay and Sustain phases.
2122
2123        Each single character in the diagrams above represents this whole
2124        sequence:
2125
2126        - when KEY-ON = 1, normal Attack phase is generated (*without* any
2127          difference when compared to normal mode),
2128
2129        - later, when envelope level reaches minimum level (max volume),
2130          the EG switches to Decay phase (which works with bigger steps
2131          when compared to normal mode - see below),
2132
2133        - later when envelope level passes the SL level,
2134          the EG swithes to Sustain phase (which works with bigger steps
2135          when compared to normal mode - see below),
2136
2137        - finally when envelope level reaches maximum level (min volume),
2138          the EG switches to Attack phase again (depends on actual waveform).
2139
2140        Important is that when switch to Attack phase occurs, the phase counter
2141        of that operator will be zeroed-out (as in normal KEY-ON) but not always.
2142        (I havent found the rule for that - perhaps only when the output level is low)
2143
2144        The difference (when compared to normal Envelope Generator mode) is
2145        that the resolution in Decay and Sustain phases is 4 times lower;
2146        this results in only 256 steps instead of normal 1024.
2147        In other words:
2148        when SSG-EG is disabled, the step inside of the EG is one,
2149        when SSG-EG is enabled, the step is four (in Decay and Sustain phases).
2150
2151        Times between the level changes are the same in both modes.
2152
2153
2154        Important:
2155        Decay 1 Level (so called SL) is compared to actual SSG-EG output, so
2156        it is the same in both SSG and no-SSG modes, with this exception:
2157
2158        when the SSG-EG is enabled and is generating raising levels
2159        (when the EG output is inverted) the SL will be found at wrong level !!!
2160        For example, when SL=02:
2161            0 -6 = -6dB in non-inverted EG output
2162            96-6 = -90dB in inverted EG output
2163        Which means that EG compares its level to SL as usual, and that the
2164        output is simply inverted afterall.
2165
2166
2167        The Yamaha's manuals say that AR should be set to 0x1f (max speed).
2168        That is not necessary, but then EG will be generating Attack phase.
2169
2170        */
2171
2172
2173      break;
2174
2175   case 0xa0:
2176      switch( OPN_SLOT(r) ){
2177      case 0:      /* 0xa0-0xa2 : FNUM1 */
2178         {
2179            UINT32 fn = (((UINT32)( (OPN->ST.fn_h)&7))<<8) + v;
2180            UINT8 blk = OPN->ST.fn_h>>3;
2181            /* keyscale code */
2182            CH->kcode = (blk<<2) | opn_fktable[fn >> 7];
2183            /* phase increment counter */
2184            CH->fc = OPN->fn_table[fn*2]>>(7-blk);
2185
2186            /* store fnum in clear form for LFO PM calculations */
2187            CH->block_fnum = (blk<<11) | fn;
2188
2189            CH->SLOT[SLOT1].Incr=-1;
2190         }
2191         break;
2192      case 1:      /* 0xa4-0xa6 : FNUM2,BLK */
2193         OPN->ST.fn_h = v&0x3f;
2194         break;
2195      case 2:      /* 0xa8-0xaa : 3CH FNUM1 */
2196         if(r < 0x100)
2197         {
2198            UINT32 fn = (((UINT32)(OPN->SL3.fn_h&7))<<8) + v;
2199            UINT8 blk = OPN->SL3.fn_h>>3;
2200            /* keyscale code */
2201            OPN->SL3.kcode[c]= (blk<<2) | opn_fktable[fn >> 7];
2202            /* phase increment counter */
2203            OPN->SL3.fc[c] = OPN->fn_table[fn*2]>>(7-blk);
2204            OPN->SL3.block_fnum[c] = (blk<<11) | fn;
2205            (OPN->P_CH)[2].SLOT[SLOT1].Incr=-1;
2206         }
2207         break;
2208      case 3:      /* 0xac-0xae : 3CH FNUM2,BLK */
2209         if(r < 0x100)
2210            OPN->SL3.fn_h = v&0x3f;
2211         break;
2212      }
2213      break;
2214
2215   case 0xb0:
2216      switch( OPN_SLOT(r) ){
2217      case 0:      /* 0xb0-0xb2 : FB,ALGO */
2218         {
2219            int feedback = (v>>3)&7;
2220            CH->ALGO = v&7;
2221            CH->FB   = feedback ? feedback+6 : 0;
2222            setup_connection( CH, c );
2223         }
2224         break;
2225      case 1:      /* 0xb4-0xb6 : L , R , AMS , PMS (YM2612/YM2610B/YM2610/YM2608) */
2226         if( OPN->type & TYPE_LFOPAN)
2227         {
2228            /* b0-2 PMS */
2229            CH->pms = (v & 7) * 32; /* CH->pms = PM depth * 32 (index in lfo_pm_table) */
2230
2231            /* b4-5 AMS */
2232            CH->ams = lfo_ams_depth_shift[(v>>4) & 0x03];
2233
2234            /* PAN :  b7 = L, b6 = R */
2235            OPN->pan[ c*2   ] = (v & 0x80) ? ~0 : 0;
2236            OPN->pan[ c*2+1 ] = (v & 0x40) ? ~0 : 0;
2237
2238         }
2239         break;
2240      }
2241      break;
2242   }
2243}
2244
2245#endif /* BUILD_OPN */
2246
2247#if (BUILD_YM2612||BUILD_YM3438)
2248/*******************************************************************************/
2249/*      YM2612 local section                                                   */
2250/*******************************************************************************/
2251/* here's the virtual YM2612 */
2252typedef struct
2253{
2254   UINT8      REGS[512];         /* registers            */
2255   FM_OPN      OPN;            /* OPN state            */
2256   FM_CH      CH[6];            /* channel state        */
2257   UINT8      addr_A1;         /* address line A1      */
2258
2259   /* dac output (YM2612) */
2260   int         dacen;
2261   INT32      dacout;
2262} YM2612;
2263
2264static int dacen;
2265
2266/* Generate samples for one of the YM2612s */
2267void ym2612_update_one(void *chip, FMSAMPLE **buffer, int length)
2268{
2269   YM2612 *F2612 = (YM2612 *)chip;
2270   FM_OPN *OPN   = &F2612->OPN;
2271   int i;
2272   FMSAMPLE  *bufL,*bufR;
2273   INT32 dacout  = F2612->dacout;
2274   FM_CH   *cch[6];
2275
2276   /* set bufer */
2277   bufL = buffer[0];
2278   bufR = buffer[1];
2279
2280   cch[0]   = &F2612->CH[0];
2281   cch[1]   = &F2612->CH[1];
2282   cch[2]   = &F2612->CH[2];
2283   cch[3]   = &F2612->CH[3];
2284   cch[4]   = &F2612->CH[4];
2285   cch[5]   = &F2612->CH[5];
2286   /* DAC mode */
2287   dacen = F2612->dacen;
2288
2289   /* refresh PG and EG */
2290   refresh_fc_eg_chan( OPN, cch[0] );
2291   refresh_fc_eg_chan( OPN, cch[1] );
2292   if( (OPN->ST.mode & 0xc0) )
2293   {
2294      /* 3SLOT MODE */
2295      if( cch[2]->SLOT[SLOT1].Incr==-1)
2296      {
2297         refresh_fc_eg_slot(OPN, &cch[2]->SLOT[SLOT1] , OPN->SL3.fc[1] , OPN->SL3.kcode[1] );
2298         refresh_fc_eg_slot(OPN, &cch[2]->SLOT[SLOT2] , OPN->SL3.fc[2] , OPN->SL3.kcode[2] );
2299         refresh_fc_eg_slot(OPN, &cch[2]->SLOT[SLOT3] , OPN->SL3.fc[0] , OPN->SL3.kcode[0] );
2300         refresh_fc_eg_slot(OPN, &cch[2]->SLOT[SLOT4] , cch[2]->fc , cch[2]->kcode );
2301      }
2302   }else refresh_fc_eg_chan( OPN, cch[2] );
2303   refresh_fc_eg_chan( OPN, cch[3] );
2304   refresh_fc_eg_chan( OPN, cch[4] );
2305   refresh_fc_eg_chan( OPN, cch[5] );
2306
2307   /* buffering */
2308   for(i=0; i < length ; i++)
2309   {
2310      /* clear outputs */
2311      out_fm[0] = 0;
2312      out_fm[1] = 0;
2313      out_fm[2] = 0;
2314      out_fm[3] = 0;
2315      out_fm[4] = 0;
2316      out_fm[5] = 0;
2317
2318      /* calculate FM */
2319      chan_calc(OPN, cch[0], 0 );
2320      chan_calc(OPN, cch[1], 1 );
2321      chan_calc(OPN, cch[2], 2 );
2322      chan_calc(OPN, cch[3], 3 );
2323      chan_calc(OPN, cch[4], 4 );
2324      if( dacen )
2325         *cch[5]->connect4 += dacout;
2326      else
2327         chan_calc(OPN, cch[5], 5 );
2328
2329      /* update SSG-EG envelope type */
2330      update_ssg_eg_channel(&cch[0]->SLOT[SLOT1]);
2331      update_ssg_eg_channel(&cch[1]->SLOT[SLOT1]);
2332      update_ssg_eg_channel(&cch[2]->SLOT[SLOT1]);
2333      update_ssg_eg_channel(&cch[3]->SLOT[SLOT1]);
2334      update_ssg_eg_channel(&cch[4]->SLOT[SLOT1]);
2335      update_ssg_eg_channel(&cch[5]->SLOT[SLOT1]);
2336
2337      /* advance LFO */
2338      advance_lfo(OPN);
2339
2340      /* advance envelope generator */
2341      OPN->eg_timer += OPN->eg_timer_add;
2342      while (OPN->eg_timer >= OPN->eg_timer_overflow)
2343      {
2344         OPN->eg_timer -= OPN->eg_timer_overflow;
2345         OPN->eg_cnt++;
2346
2347         advance_eg_channel(OPN, &cch[0]->SLOT[SLOT1]);
2348         advance_eg_channel(OPN, &cch[1]->SLOT[SLOT1]);
2349         advance_eg_channel(OPN, &cch[2]->SLOT[SLOT1]);
2350         advance_eg_channel(OPN, &cch[3]->SLOT[SLOT1]);
2351         advance_eg_channel(OPN, &cch[4]->SLOT[SLOT1]);
2352         advance_eg_channel(OPN, &cch[5]->SLOT[SLOT1]);
2353      }
2354
2355      {
2356         int lt,rt;
2357
2358         lt  = ((out_fm[0]>>0) & OPN->pan[0]);
2359         rt  = ((out_fm[0]>>0) & OPN->pan[1]);
2360         lt += ((out_fm[1]>>0) & OPN->pan[2]);
2361         rt += ((out_fm[1]>>0) & OPN->pan[3]);
2362         lt += ((out_fm[2]>>0) & OPN->pan[4]);
2363         rt += ((out_fm[2]>>0) & OPN->pan[5]);
2364         lt += ((out_fm[3]>>0) & OPN->pan[6]);
2365         rt += ((out_fm[3]>>0) & OPN->pan[7]);
2366         lt += ((out_fm[4]>>0) & OPN->pan[8]);
2367         rt += ((out_fm[4]>>0) & OPN->pan[9]);
2368         lt += ((out_fm[5]>>0) & OPN->pan[10]);
2369         rt += ((out_fm[5]>>0) & OPN->pan[11]);
2370
2371         Limit( lt, MAXOUT, MINOUT );
2372         Limit( rt, MAXOUT, MINOUT );
2373
2374         #ifdef SAVE_SAMPLE
2375            SAVE_ALL_CHANNELS
2376         #endif
2377
2378         /* buffering */
2379         bufL[i] = lt;
2380         bufR[i] = rt;
2381      }
2382
2383      /* CSM mode: if CSM Key ON has occured, CSM Key OFF need to be sent       */
2384      /* only if Timer A does not overflow again (i.e CSM Key ON not set again) */
2385      OPN->SL3.key_csm <<= 1;
2386
2387      /* timer A control */
2388      INTERNAL_TIMER_A( &OPN->ST , cch[2] )
2389
2390      /* CSM Mode Key ON still disabled */
2391      /* CSM Mode Key OFF (verified by Nemesis on real hardware) */
2392      FM_KEYOFF_CSM(cch[2],SLOT1);
2393      FM_KEYOFF_CSM(cch[2],SLOT2);
2394      FM_KEYOFF_CSM(cch[2],SLOT3);
2395      FM_KEYOFF_CSM(cch[2],SLOT4);
2396      OPN->SL3.key_csm = 0;
2397   }
2398   INTERNAL_TIMER_B(&OPN->ST,length)
2399
2400}
2401
2402#ifdef __STATE_H__
2403void ym2612_postload(void *chip)
2404{
2405   if (chip)
2406   {
2407      YM2612 *F2612 = (YM2612 *)chip;
2408      int r;
2409
2410      /* DAC data & port */
2411      F2612->dacout = ((int)F2612->REGS[0x2a] - 0x80) << 6;   /* level unknown */
2412      F2612->dacen  = F2612->REGS[0x2d] & 0x80;
2413      /* OPN registers */
2414      /* DT / MULTI , TL , KS / AR , AMON / DR , SR , SL / RR , SSG-EG */
2415      for(r=0x30;r<0x9e;r++)
2416         if((r&3) != 3)
2417         {
2418            OPNWriteReg(&F2612->OPN,r,F2612->REGS[r]);
2419            OPNWriteReg(&F2612->OPN,r|0x100,F2612->REGS[r|0x100]);
2420         }
2421      /* FB / CONNECT , L / R / AMS / PMS */
2422      for(r=0xb0;r<0xb6;r++)
2423         if((r&3) != 3)
2424         {
2425            OPNWriteReg(&F2612->OPN,r,F2612->REGS[r]);
2426            OPNWriteReg(&F2612->OPN,r|0x100,F2612->REGS[r|0x100]);
2427         }
2428      /* channels */
2429      /*FM_channel_postload(F2612->CH,6);*/
2430   }
2431}
2432
2433static void YM2612_save_state(YM2612 *F2612, const device_config *device)
2434{
2435   state_save_register_device_item_array(device, 0, F2612->REGS);
2436   FMsave_state_st(device,&F2612->OPN.ST);
2437   FMsave_state_channel(device,F2612->CH,6);
2438   /* 3slots */
2439   state_save_register_device_item_array(device, 0, F2612->OPN.SL3.fc);
2440   state_save_register_device_item(device, 0, F2612->OPN.SL3.fn_h);
2441   state_save_register_device_item_array(device, 0, F2612->OPN.SL3.kcode);
2442   /* address register1 */
2443   state_save_register_device_item(device, 0, F2612->addr_A1);
2444}
2445#endif /* _STATE_H */
2446
2447/* initialize YM2612 emulator(s) */
2448void * ym2612_init(void *param, const device_config *device, int clock, int rate,
2449               FM_TIMERHANDLER timer_handler,FM_IRQHANDLER IRQHandler)
2450{
2451   YM2612 *F2612;
2452
2453   /* allocate extend state space */
2454   if( (F2612 = (YM2612 *)malloc(sizeof(YM2612)))==NULL)
2455      return NULL;
2456   /* clear */
2457   memset(F2612,0,sizeof(YM2612));
2458   /* allocate total level table (128kb space) */
2459   if( !init_tables() )
2460   {
2461      free( F2612 );
2462      return NULL;
2463   }
2464
2465   F2612->OPN.ST.param = param;
2466   F2612->OPN.type = TYPE_YM2612;
2467   F2612->OPN.P_CH = F2612->CH;
2468   F2612->OPN.ST.device = device;
2469   F2612->OPN.ST.clock = clock;
2470   F2612->OPN.ST.rate = rate;
2471   /* F2612->OPN.ST.irq = 0; */
2472   /* F2612->OPN.ST.status = 0; */
2473   /* Extend handler */
2474   F2612->OPN.ST.timer_handler = timer_handler;
2475   F2612->OPN.ST.IRQ_Handler   = IRQHandler;
2476
2477#ifdef __STATE_H__
2478   YM2612_save_state(F2612, device);
2479#endif
2480   return F2612;
2481}
2482
2483/* shut down emulator */
2484void ym2612_shutdown(void *chip)
2485{
2486   YM2612 *F2612 = (YM2612 *)chip;
2487
2488   FMCloseTable();
2489   free(F2612);
2490}
2491
2492/* reset one of chip */
2493void ym2612_reset_chip(void *chip)
2494{
2495   int i;
2496   YM2612 *F2612 = (YM2612 *)chip;
2497   FM_OPN *OPN   = &F2612->OPN;
2498
2499   OPNSetPres( OPN, 6*24, 6*24, 0);
2500   /* status clear */
2501   FM_IRQMASK_SET(&OPN->ST,0x03);
2502   FM_BUSY_CLEAR(&OPN->ST);
2503   OPNWriteMode(OPN,0x27,0x30); /* mode 0 , timer reset */
2504
2505   OPN->eg_timer = 0;
2506   OPN->eg_cnt   = 0;
2507   OPN->ST.status = 0;
2508   OPN->ST.mode = 0;
2509   LFO_AM  = 0;
2510   LFO_PM  = 0;
2511   OPN->lfo_cnt  = 0;
2512
2513   OPNWriteMode(OPN,0x27,0x30);
2514   OPNWriteMode(OPN,0x26,0x00);
2515   OPNWriteMode(OPN,0x25,0x00);
2516   OPNWriteMode(OPN,0x24,0x00);
2517
2518   FM_STATUS_RESET(&OPN->ST, 0xff);
2519
2520   reset_channels( &OPN->ST , &F2612->CH[0] , 6 );
2521   for(i = 0xb6 ; i >= 0xb4 ; i-- )
2522   {
2523      OPNWriteReg(OPN,i      ,0xc0);
2524      OPNWriteReg(OPN,i|0x100,0xc0);
2525   }
2526   for(i = 0xb2 ; i >= 0x30 ; i-- )
2527   {
2528      OPNWriteReg(OPN,i      ,0);
2529      OPNWriteReg(OPN,i|0x100,0);
2530   }
2531   for(i = 0x26 ; i >= 0x20 ; i-- ) OPNWriteReg(OPN,i,0);
2532   /* DAC mode clear */
2533   F2612->dacen = 0;
2534}
2535
2536/* YM2612 write */
2537/* n = number  */
2538/* a = address */
2539/* v = value   */
2540int ym2612_write(void *chip, int a, UINT8 v)
2541{
2542   YM2612 *F2612 = (YM2612 *)chip;
2543   int addr;
2544
2545   v &= 0xff;   /* adjust to 8 bit bus */
2546
2547   switch( a&3){
2548   case 0:   /* address port 0 */
2549      F2612->OPN.ST.address = v;
2550      F2612->addr_A1 = 0;
2551      break;
2552
2553   case 1:   /* data port 0    */
2554      if (F2612->addr_A1 != 0)
2555         break;   /* verified on real YM2608 */
2556
2557      addr = F2612->OPN.ST.address;
2558      F2612->REGS[addr] = v;
2559      switch( addr & 0xf0 )
2560      {
2561      case 0x20:   /* 0x20-0x2f Mode */
2562         switch( addr )
2563         {
2564         case 0x2a:   /* DAC data (YM2612) */
2565            ym2612_update_req(F2612->OPN.ST.param);
2566            F2612->dacout = ((int)v - 0x80) << 6;   /* level unknown */
2567            break;
2568         case 0x2b:   /* DAC Sel  (YM2612) */
2569            /* b7 = dac enable */
2570            F2612->dacen = v & 0x80;
2571            break;
2572         default:   /* OPN section */
2573            ym2612_update_req(F2612->OPN.ST.param);
2574            /* write register */
2575            OPNWriteMode(&(F2612->OPN),addr,v);
2576         }
2577         break;
2578      default:   /* 0x30-0xff OPN section */
2579         ym2612_update_req(F2612->OPN.ST.param);
2580         /* write register */
2581         OPNWriteReg(&(F2612->OPN),addr,v);
2582      }
2583      break;
2584
2585   case 2:   /* address port 1 */
2586      F2612->OPN.ST.address = v;
2587      F2612->addr_A1 = 1;
2588      break;
2589
2590   case 3:   /* data port 1    */
2591      if (F2612->addr_A1 != 1)
2592         break;   /* verified on real YM2608 */
2593
2594      addr = F2612->OPN.ST.address;
2595      F2612->REGS[addr | 0x100] = v;
2596      ym2612_update_req(F2612->OPN.ST.param);
2597      OPNWriteReg(&(F2612->OPN),addr | 0x100,v);
2598      break;
2599   }
2600   return F2612->OPN.ST.irq;
2601}
2602
2603UINT8 ym2612_read(void *chip,int a)
2604{
2605   YM2612 *F2612 = (YM2612 *)chip;
2606
2607   switch( a&3){
2608   case 0:   /* status 0 */
2609      return FM_STATUS_FLAG(&F2612->OPN.ST);
2610   case 1:
2611   case 2:
2612   case 3:
2613      LOG(LOG_WAR,("YM2612 #%p:A=%d read unmapped area\n",F2612->OPN.ST.param,a));
2614      return FM_STATUS_FLAG(&F2612->OPN.ST);
2615   }
2616   return 0;
2617}
2618
2619int ym2612_timer_over(void *chip,int c)
2620{
2621   YM2612 *F2612 = (YM2612 *)chip;
2622
2623   if( c )
2624   {   /* Timer B */
2625      TimerBOver( &(F2612->OPN.ST) );
2626   }
2627   else
2628   {   /* Timer A */
2629      ym2612_update_req(F2612->OPN.ST.param);
2630      /* timer update */
2631      TimerAOver( &(F2612->OPN.ST) );
2632      /* CSM mode key,TL controll */
2633      if ((F2612->OPN.ST.mode & 0xc0) == 0x80)
2634      {   /* CSM mode total level latch and auto key on */
2635         CSMKeyControll( &F2612->OPN, &(F2612->CH[2]) );
2636      }
2637   }
2638   return F2612->OPN.ST.irq;
2639}
2640
2641#endif /* (BUILD_YM2612||BUILD_YM3238) */
src/emu/sound/fm.c
r5435r5436
132132#endif
133133
134134/* shared function building option */
135#define BUILD_OPN (BUILD_YM2203||BUILD_YM2608||BUILD_YM2610||BUILD_YM2610B||BUILD_YM2612||BUILD_YM3438)
135#define BUILD_OPN (BUILD_YM2203||BUILD_YM2608||BUILD_YM2610||BUILD_YM2610B)
136136#define BUILD_OPN_PRESCALER (BUILD_YM2203||BUILD_YM2608)
137137
138138
r5435r5436
148148#define TYPE_YM2203 (TYPE_SSG)
149149#define TYPE_YM2608 (TYPE_SSG |TYPE_LFOPAN |TYPE_6CH |TYPE_ADPCM)
150150#define TYPE_YM2610 (TYPE_SSG |TYPE_LFOPAN |TYPE_6CH |TYPE_ADPCM |TYPE_2610)
151#define TYPE_YM2612 (TYPE_DAC |TYPE_LFOPAN |TYPE_6CH)
152151
153152
154153
r5435r5436
292291
293292};
294293
295static const UINT8 eg_rate_select2612[32+64+32]={    /* Envelope Generator rates (32 + 64 rates + 32 RKS) from tests on YM2612 */
296/* 32 infinite time rates */
297O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),
298O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),
299O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),
300O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),
301
302/* rates 00-11 */
303O( 18),O( 18),O( 0),O( 0),
304O( 0),O( 0),O( 2),O( 2),  // Nemesis's tests
305
306O( 0),O( 1),O( 2),O( 3),
307O( 0),O( 1),O( 2),O( 3),
308O( 0),O( 1),O( 2),O( 3),
309O( 0),O( 1),O( 2),O( 3),
310O( 0),O( 1),O( 2),O( 3),
311O( 0),O( 1),O( 2),O( 3),
312O( 0),O( 1),O( 2),O( 3),
313O( 0),O( 1),O( 2),O( 3),
314O( 0),O( 1),O( 2),O( 3),
315O( 0),O( 1),O( 2),O( 3),
316
317/* rate 12 */
318O( 4),O( 5),O( 6),O( 7),
319
320/* rate 13 */
321O( 8),O( 9),O(10),O(11),
322
323/* rate 14 */
324O(12),O(13),O(14),O(15),
325
326/* rate 15 */
327O(16),O(16),O(16),O(16),
328
329/* 32 dummy rates (same as 15 3) */
330O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16),
331O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16),
332O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16),
333O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16)
334
335};
336294#undef O
337295
338296/*rate  0,    1,    2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15*/
r5435r5436
922880      SLOT->key = 1;
923881      SLOT->phase = 0;      /* restart Phase Generator */
924882      SLOT->ssgn = (SLOT->ssg & 0x04) >> 1;
925
926      if ((type == TYPE_YM2612) || (type == TYPE_YM2608))
927      {
928              if( (SLOT->ar + SLOT->ksr) < 32+62 )
929              {
930                 SLOT->state = EG_ATT;    /* phase -> Attack */
931         }
932         else
933         {
934                 /* directly switch to Decay */
935                 SLOT->volume = MIN_ATT_INDEX;
936                 SLOT->state = EG_DEC;
937         }
938      }
939      else
940      {
941         SLOT->state = EG_ATT;
942      }
883      SLOT->state = EG_ATT;
943884   }
944885}
945886
r5435r5436
10721013   if ((SLOT->ar + SLOT->ksr) < 32+62)
10731014   {
10741015      SLOT->eg_sh_ar  = eg_rate_shift [SLOT->ar  + SLOT->ksr ];
1075      if ((type == TYPE_YM2612) || (type == TYPE_YM2608))
1076      {
1077                   SLOT->eg_sh_ar  = eg_rate_shift [SLOT->ar  + SLOT->ksr ];
1078         SLOT->eg_sel_ar = eg_rate_select2612[SLOT->ar  + SLOT->ksr ];
1079      }
1080      else
1081      {
1082         SLOT->eg_sel_ar = eg_rate_select[SLOT->ar  + SLOT->ksr ];
1083      }
1016      SLOT->eg_sel_ar = eg_rate_select[SLOT->ar  + SLOT->ksr ];
10841017   }
10851018   else
10861019   {
r5435r5436
10951028   SLOT->d1r = (v&0x1f) ? 32 + ((v&0x1f)<<1) : 0;
10961029
10971030   SLOT->eg_sh_d1r = eg_rate_shift [SLOT->d1r + SLOT->ksr];
1098   if ((type == TYPE_YM2612) || (type == TYPE_YM2608))
1099   {
1100      SLOT->eg_sel_d1r= eg_rate_select2612[SLOT->d1r + SLOT->ksr];
1101   }
1102   else
1103   {
1104      SLOT->eg_sel_d1r= eg_rate_select[SLOT->d1r + SLOT->ksr];
1105   }
1106
1031   SLOT->eg_sel_d1r= eg_rate_select[SLOT->d1r + SLOT->ksr];
11071032}
11081033
11091034/* set sustain rate */
r5435r5436
11121037   SLOT->d2r = (v&0x1f) ? 32 + ((v&0x1f)<<1) : 0;
11131038
11141039   SLOT->eg_sh_d2r = eg_rate_shift [SLOT->d2r + SLOT->ksr];
1115   if ((type == TYPE_YM2612) || (type == TYPE_YM2608))
1116   {
1117      SLOT->eg_sel_d2r= eg_rate_select2612[SLOT->d2r + SLOT->ksr];
1118   }
1119   else
1120   {
1121      SLOT->eg_sel_d2r= eg_rate_select[SLOT->d2r + SLOT->ksr];
1122   }
1040   SLOT->eg_sel_d2r= eg_rate_select[SLOT->d2r + SLOT->ksr];
11231041}
11241042
11251043/* set release rate */
r5435r5436
11301048   SLOT->rr  = 34 + ((v&0x0f)<<2);
11311049
11321050   SLOT->eg_sh_rr  = eg_rate_shift [SLOT->rr  + SLOT->ksr];
1133   if ((type == TYPE_YM2612) || (type == TYPE_YM2608))
1134   {
1135      SLOT->eg_sel_rr = eg_rate_select2612[SLOT->rr  + SLOT->ksr];
1136   }
1137   else
1138   {
1139      SLOT->eg_sel_rr = eg_rate_select[SLOT->rr  + SLOT->ksr];
1140   }
1051   SLOT->eg_sel_rr = eg_rate_select[SLOT->rr  + SLOT->ksr];
11411052}
11421053
11431054
r5435r5436
12381149      break;
12391150
12401151      case EG_DEC:   /* decay phase */
1241         if ((OPN->type == TYPE_YM2612) || (OPN->type == TYPE_YM2608))
12421152         {
1243                                if ( !(OPN->eg_cnt & ((1<<SLOT->eg_sh_d1r)-1) ) )
1244                                {
1245               if (SLOT->ssg&0x08)   /* SSG EG type envelope selected */
1246               {
1247                         SLOT->volume += 6 * eg_inc[SLOT->eg_sel_d1r + ((OPN->eg_cnt>>SLOT->eg_sh_d1r)&7)];
1248               }
1249               else
1250               {
1251                                                SLOT->volume += eg_inc[SLOT->eg_sel_d1r + ((OPN->eg_cnt>>SLOT->eg_sh_d1r)&7)];
1252               }
1253                 }
1254
1255            /* check transition even if no volume update: this fixes the case when SL = MIN_ATT_INDEX */
1256            if ( SLOT->volume >= (INT32)(SLOT->sl) )
1257            {
1258               SLOT->volume = (INT32)(SLOT->sl);
1259               SLOT->state = EG_SUS;
1260            }
1261         }
1262         else
1263         {
12641153            if (SLOT->ssg&0x08)   /* SSG EG type envelope selected */
12651154            {
12661155               if ( !(OPN->eg_cnt & ((1<<SLOT->eg_sh_d1r)-1) ) )
r5435r5436
12901179            if ( !(OPN->eg_cnt & ((1<<SLOT->eg_sh_d2r)-1) ) )
12911180            {
12921181
1293               if ((OPN->type == TYPE_YM2612) || (OPN->type == TYPE_YM2608))
1294               {
1295                  SLOT->volume += 6 * eg_inc[SLOT->eg_sel_d2r + ((OPN->eg_cnt>>SLOT->eg_sh_d2r)&7)];
1296               }
1297               else
1298               {
1299                  SLOT->volume += 4 * eg_inc[SLOT->eg_sel_d2r + ((OPN->eg_cnt>>SLOT->eg_sh_d2r)&7)];
1300               }
1182               SLOT->volume += 4 * eg_inc[SLOT->eg_sel_d2r + ((OPN->eg_cnt>>SLOT->eg_sh_d2r)&7)];
13011183
13021184               if ( SLOT->volume >= ENV_QUIET )
13031185               {
1304                  if ((OPN->type != TYPE_YM2612) && (OPN->type != TYPE_YM2608))
1305                  {
1306                     SLOT->volume = MAX_ATT_INDEX;
1307                  }
1186                  SLOT->volume = MAX_ATT_INDEX;
13081187
13091188                  if (SLOT->ssg&0x01)   /* bit 0 = hold */
13101189                  {
r5435r5436
13231202                     /* restart of the Phase Generator should be here */
13241203                     SLOT->phase = 0;
13251204
1326                     if ((OPN->type == TYPE_YM2612) || (OPN->type == TYPE_YM2608))
13271205                     {
1328                        if ((SLOT->ar + SLOT->ksr) < 94 /*32+62*/)
1329                        {
1330                           SLOT->state = EG_ATT; /* phase -> Attack */
1331                        }
1332                        else
1333                        {
1334                           /* Attack Rate is maximal: directly switch to Decay (or Substain) */
1335                           SLOT->volume = MIN_ATT_INDEX;
1336                           SLOT->state = (SLOT->sl == MIN_ATT_INDEX) ? EG_SUS : EG_DEC;
1337                        }
1338                     }
1339                     else
1340                     {
13411206                        /* phase -> Attack */
13421207                        SLOT->volume = 511;
13431208                        SLOT->state = EG_ATT;
r5435r5436
13681233            if ( !(OPN->eg_cnt & ((1<<SLOT->eg_sh_rr)-1) ) )
13691234            {
13701235               /* SSG-EG affects Release phase also (Nemesis) */
1371               if ((SLOT->ssg&0x08) && ((OPN->type = TYPE_YM2612) || (OPN->type = TYPE_YM2608)))
1372               {
1373                  SLOT->volume += 6 * eg_inc[SLOT->eg_sel_rr + ((OPN->eg_cnt>>SLOT->eg_sh_rr)&7)];
1374               }
1375               else
1376               {
1377                  SLOT->volume += eg_inc[SLOT->eg_sel_rr + ((OPN->eg_cnt>>SLOT->eg_sh_rr)&7)];
1378               }
1236               SLOT->volume += eg_inc[SLOT->eg_sel_rr + ((OPN->eg_cnt>>SLOT->eg_sh_rr)&7)];
13791237
13801238               if ( SLOT->volume >= MAX_ATT_INDEX )
13811239               {
r5435r5436
15911449      if ((SLOT->ar + SLOT->ksr) < 32+62)
15921450      {
15931451         SLOT->eg_sh_ar  = eg_rate_shift [SLOT->ar  + SLOT->ksr ];
1594         if ((OPN->type == TYPE_YM2612) || (OPN->type == TYPE_YM2608))
1595         {
1596            SLOT->eg_sel_ar = eg_rate_select2612[SLOT->ar  + SLOT->ksr ];
1597         }
1598         else
1599         {
1600            SLOT->eg_sel_ar = eg_rate_select[SLOT->ar  + SLOT->ksr ];
1601         }
1452         SLOT->eg_sel_ar = eg_rate_select[SLOT->ar  + SLOT->ksr ];
16021453      }
16031454      else
16041455      {
r5435r5436
16101461      SLOT->eg_sh_d2r = eg_rate_shift [SLOT->d2r + SLOT->ksr];
16111462      SLOT->eg_sh_rr  = eg_rate_shift [SLOT->rr  + SLOT->ksr];
16121463
1613      if ((OPN->type == TYPE_YM2612) || (OPN->type == TYPE_YM2608))
1614      {
1615         SLOT->eg_sel_d1r= eg_rate_select2612[SLOT->d1r + SLOT->ksr];
1616         SLOT->eg_sel_d2r= eg_rate_select2612[SLOT->d2r + SLOT->ksr];
1617         SLOT->eg_sel_rr = eg_rate_select2612[SLOT->rr  + SLOT->ksr];
1618      }
1619      else
1620      {
1621         SLOT->eg_sel_d1r= eg_rate_select[SLOT->d1r + SLOT->ksr];
1622         SLOT->eg_sel_d2r= eg_rate_select[SLOT->d2r + SLOT->ksr];
1623         SLOT->eg_sel_rr = eg_rate_select[SLOT->rr  + SLOT->ksr];
1624      }
1464      SLOT->eg_sel_d1r= eg_rate_select[SLOT->d1r + SLOT->ksr];
1465      SLOT->eg_sel_d2r= eg_rate_select[SLOT->d2r + SLOT->ksr];
1466      SLOT->eg_sel_rr = eg_rate_select[SLOT->rr  + SLOT->ksr];
16251467   }
16261468}
16271469
r5435r5436
46014443}
46024444
46034445#endif /* (BUILD_YM2610||BUILD_YM2610B) */
4604
4605
4606
4607#if (BUILD_YM2612||BUILD_YM3438)
4608/*******************************************************************************/
4609/*      YM2612 local section                                                   */
4610/*******************************************************************************/
4611/* here's the virtual YM2612 */
4612typedef struct
4613{
4614   UINT8      REGS[512];         /* registers            */
4615   FM_OPN      OPN;            /* OPN state            */
4616   FM_CH      CH[6];            /* channel state        */
4617   UINT8      addr_A1;         /* address line A1      */
4618
4619   /* dac output (YM2612) */
4620   int         dacen;
4621   INT32      dacout;
4622} YM2612;
4623
4624static int dacen;
4625
4626/* Generate samples for one of the YM2612s */
4627void ym2612_update_one(void *chip, FMSAMPLE **buffer, int length)
4628{
4629   YM2612 *F2612 = (YM2612 *)chip;
4630   FM_OPN *OPN   = &F2612->OPN;
4631   int i;
4632   FMSAMPLE  *bufL,*bufR;
4633   INT32 dacout  = F2612->dacout;
4634   FM_CH   *cch[6];
4635
4636   /* set bufer */
4637   bufL = buffer[0];
4638   bufR = buffer[1];
4639
4640   cch[0]   = &F2612->CH[0];
4641   cch[1]   = &F2612->CH[1];
4642   cch[2]   = &F2612->CH[2];
4643   cch[3]   = &F2612->CH[3];
4644   cch[4]   = &F2612->CH[4];
4645   cch[5]   = &F2612->CH[5];
4646   /* DAC mode */
4647   dacen = F2612->dacen;
4648
4649   /* refresh PG and EG */
4650   refresh_fc_eg_chan( OPN, cch[0] );
4651   refresh_fc_eg_chan( OPN, cch[1] );
4652   if( (OPN->ST.mode & 0xc0) )
4653   {
4654      /* 3SLOT MODE */
4655      if( cch[2]->SLOT[SLOT1].Incr==-1)
4656      {
4657         refresh_fc_eg_slot(OPN, &cch[2]->SLOT[SLOT1] , OPN->SL3.fc[1] , OPN->SL3.kcode[1] );
4658         refresh_fc_eg_slot(OPN, &cch[2]->SLOT[SLOT2] , OPN->SL3.fc[2] , OPN->SL3.kcode[2] );
4659         refresh_fc_eg_slot(OPN, &cch[2]->SLOT[SLOT3] , OPN->SL3.fc[0] , OPN->SL3.kcode[0] );
4660         refresh_fc_eg_slot(OPN, &cch[2]->SLOT[SLOT4] , cch[2]->fc , cch[2]->kcode );
4661      }
4662   }else refresh_fc_eg_chan( OPN, cch[2] );
4663   refresh_fc_eg_chan( OPN, cch[3] );
4664   refresh_fc_eg_chan( OPN, cch[4] );
4665   refresh_fc_eg_chan( OPN, cch[5] );
4666
4667   /* buffering */
4668   for(i=0; i < length ; i++)
4669   {
4670
4671      advance_lfo(OPN);
4672
4673      /* clear outputs */
4674      out_fm[0] = 0;
4675      out_fm[1] = 0;
4676      out_fm[2] = 0;
4677      out_fm[3] = 0;
4678      out_fm[4] = 0;
4679      out_fm[5] = 0;
4680
4681      /* calculate FM */
4682      chan_calc(OPN, cch[0], 0 );
4683      chan_calc(OPN, cch[1], 1 );
4684      chan_calc(OPN, cch[2], 2 );
4685      chan_calc(OPN, cch[3], 3 );
4686      chan_calc(OPN, cch[4], 4 );
4687      if( dacen )
4688         *cch[5]->connect4 += dacout;
4689      else
4690         chan_calc(OPN, cch[5], 5 );
4691
4692      /* advance envelope generator */
4693      OPN->eg_timer += OPN->eg_timer_add;
4694      while (OPN->eg_timer >= OPN->eg_timer_overflow)
4695      {
4696         OPN->eg_timer -= OPN->eg_timer_overflow;
4697         OPN->eg_cnt++;
4698
4699         advance_eg_channel(OPN, &cch[0]->SLOT[SLOT1]);
4700         advance_eg_channel(OPN, &cch[1]->SLOT[SLOT1]);
4701         advance_eg_channel(OPN, &cch[2]->SLOT[SLOT1]);
4702         advance_eg_channel(OPN, &cch[3]->SLOT[SLOT1]);
4703         advance_eg_channel(OPN, &cch[4]->SLOT[SLOT1]);
4704         advance_eg_channel(OPN, &cch[5]->SLOT[SLOT1]);
4705      }
4706
4707      {
4708         int lt,rt;
4709
4710         lt  = ((out_fm[0]>>0) & OPN->pan[0]);
4711         rt  = ((out_fm[0]>>0) & OPN->pan[1]);
4712         lt += ((out_fm[1]>>0) & OPN->pan[2]);
4713         rt += ((out_fm[1]>>0) & OPN->pan[3]);
4714         lt += ((out_fm[2]>>0) & OPN->pan[4]);
4715         rt += ((out_fm[2]>>0) & OPN->pan[5]);
4716         lt += ((out_fm[3]>>0) & OPN->pan[6]);
4717         rt += ((out_fm[3]>>0) & OPN->pan[7]);
4718         lt += ((out_fm[4]>>0) & OPN->pan[8]);
4719         rt += ((out_fm[4]>>0) & OPN->pan[9]);
4720         lt += ((out_fm[5]>>0) & OPN->pan[10]);
4721         rt += ((out_fm[5]>>0) & OPN->pan[11]);
4722
4723
4724         lt >>= FINAL_SH;
4725         rt >>= FINAL_SH;
4726
4727         Limit( lt, MAXOUT, MINOUT );
4728         Limit( rt, MAXOUT, MINOUT );
4729
4730         #ifdef SAVE_SAMPLE
4731            SAVE_ALL_CHANNELS
4732         #endif
4733
4734         /* buffering */
4735         bufL[i] = lt;
4736         bufR[i] = rt;
4737      }
4738
4739      /* timer A control */
4740      INTERNAL_TIMER_A( &OPN->ST , cch[2] )
4741   }
4742   INTERNAL_TIMER_B(&OPN->ST,length)
4743
4744}
4745
4746#ifdef __STATE_H__
4747void ym2612_postload(void *chip)
4748{
4749   if (chip)
4750   {
4751      YM2612 *F2612 = (YM2612 *)chip;
4752      int r;
4753
4754      /* DAC data & port */
4755      F2612->dacout = ((int)F2612->REGS[0x2a] - 0x80) << 6;   /* level unknown */
4756      F2612->dacen  = F2612->REGS[0x2d] & 0x80;
4757      /* OPN registers */
4758      /* DT / MULTI , TL , KS / AR , AMON / DR , SR , SL / RR , SSG-EG */
4759      for(r=0x30;r<0x9e;r++)
4760         if((r&3) != 3)
4761         {
4762            OPNWriteReg(&F2612->OPN,r,F2612->REGS[r]);
4763            OPNWriteReg(&F2612->OPN,r|0x100,F2612->REGS[r|0x100]);
4764         }
4765      /* FB / CONNECT , L / R / AMS / PMS */
4766      for(r=0xb0;r<0xb6;r++)
4767         if((r&3) != 3)
4768         {
4769            OPNWriteReg(&F2612->OPN,r,F2612->REGS[r]);
4770            OPNWriteReg(&F2612->OPN,r|0x100,F2612->REGS[r|0x100]);
4771         }
4772      /* channels */
4773      /*FM_channel_postload(F2612->CH,6);*/
4774   }
4775}
4776
4777static void YM2612_save_state(YM2612 *F2612, const device_config *device)
4778{
4779   state_save_register_device_item_array(device, 0, F2612->REGS);
4780   FMsave_state_st(device,&F2612->OPN.ST);
4781   FMsave_state_channel(device,F2612->CH,6);
4782   /* 3slots */
4783   state_save_register_device_item_array(device, 0, F2612->OPN.SL3.fc);
4784   state_save_register_device_item(device, 0, F2612->OPN.SL3.fn_h);
4785   state_save_register_device_item_array(device, 0, F2612->OPN.SL3.kcode);
4786   /* address register1 */
4787   state_save_register_device_item(device, 0, F2612->addr_A1);
4788}
4789#endif /* _STATE_H */
4790
4791/* initialize YM2612 emulator(s) */
4792void * ym2612_init(void *param, const device_config *device, int clock, int rate,
4793               FM_TIMERHANDLER timer_handler,FM_IRQHANDLER IRQHandler)
4794{
4795   YM2612 *F2612;
4796
4797   /* allocate extend state space */
4798   if( (F2612 = (YM2612 *)malloc(sizeof(YM2612)))==NULL)
4799      return NULL;
4800   /* clear */
4801   memset(F2612,0,sizeof(YM2612));
4802   /* allocate total level table (128kb space) */
4803   if( !init_tables() )
4804   {
4805      free( F2612 );
4806      return NULL;
4807   }
4808
4809   F2612->OPN.ST.param = param;
4810   F2612->OPN.type = TYPE_YM2612;
4811   F2612->OPN.P_CH = F2612->CH;
4812   F2612->OPN.ST.device = device;
4813   F2612->OPN.ST.clock = clock;
4814   F2612->OPN.ST.rate = rate;
4815   /* F2612->OPN.ST.irq = 0; */
4816   /* F2612->OPN.ST.status = 0; */
4817   /* Extend handler */
4818   F2612->OPN.ST.timer_handler = timer_handler;
4819   F2612->OPN.ST.IRQ_Handler   = IRQHandler;
4820
4821#ifdef __STATE_H__
4822   YM2612_save_state(F2612, device);
4823#endif
4824   return F2612;
4825}
4826
4827/* shut down emulator */
4828void ym2612_shutdown(void *chip)
4829{
4830   YM2612 *F2612 = (YM2612 *)chip;
4831
4832   FMCloseTable();
4833   free(F2612);
4834}
4835
4836/* reset one of chip */
4837void ym2612_reset_chip(void *chip)
4838{
4839   int i;
4840   YM2612 *F2612 = (YM2612 *)chip;
4841   FM_OPN *OPN   = &F2612->OPN;
4842
4843   OPNSetPres( OPN, 6*24, 6*24, 0);
4844   /* status clear */
4845   FM_IRQMASK_SET(&OPN->ST,0x03);
4846   FM_BUSY_CLEAR(&OPN->ST);
4847   OPNWriteMode(OPN,0x27,0x30); /* mode 0 , timer reset */
4848
4849   OPN->eg_timer = 0;
4850   OPN->eg_cnt   = 0;
4851
4852   FM_STATUS_RESET(&OPN->ST, 0xff);
4853
4854   reset_channels( &OPN->ST , &F2612->CH[0] , 6 );
4855   for(i = 0xb6 ; i >= 0xb4 ; i-- )
4856   {
4857      OPNWriteReg(OPN,i      ,0xc0);
4858      OPNWriteReg(OPN,i|0x100,0xc0);
4859   }
4860   for(i = 0xb2 ; i >= 0x30 ; i-- )
4861   {
4862      OPNWriteReg(OPN,i      ,0);
4863      OPNWriteReg(OPN,i|0x100,0);
4864   }
4865   for(i = 0x26 ; i >= 0x20 ; i-- ) OPNWriteReg(OPN,i,0);
4866   /* DAC mode clear */
4867   F2612->dacen = 0;
4868}
4869
4870/* YM2612 write */
4871/* n = number  */
4872/* a = address */
4873/* v = value   */
4874int ym2612_write(void *chip, int a, UINT8 v)
4875{
4876   YM2612 *F2612 = (YM2612 *)chip;
4877   int addr;
4878
4879   v &= 0xff;   /* adjust to 8 bit bus */
4880
4881   switch( a&3){
4882   case 0:   /* address port 0 */
4883      F2612->OPN.ST.address = v;
4884      F2612->addr_A1 = 0;
4885      break;
4886
4887   case 1:   /* data port 0    */
4888      if (F2612->addr_A1 != 0)
4889         break;   /* verified on real YM2608 */
4890
4891      addr = F2612->OPN.ST.address;
4892      F2612->REGS[addr] = v;
4893      switch( addr & 0xf0 )
4894      {
4895      case 0x20:   /* 0x20-0x2f Mode */
4896         switch( addr )
4897         {
4898         case 0x2a:   /* DAC data (YM2612) */
4899            ym2612_update_req(F2612->OPN.ST.param);
4900            F2612->dacout = ((int)v - 0x80) << 6;   /* level unknown */
4901            break;
4902         case 0x2b:   /* DAC Sel  (YM2612) */
4903            /* b7 = dac enable */
4904            F2612->dacen = v & 0x80;
4905            break;
4906         default:   /* OPN section */
4907            ym2612_update_req(F2612->OPN.ST.param);
4908            /* write register */
4909            OPNWriteMode(&(F2612->OPN),addr,v);
4910         }
4911         break;
4912      default:   /* 0x30-0xff OPN section */
4913         ym2612_update_req(F2612->OPN.ST.param);
4914         /* write register */
4915         OPNWriteReg(&(F2612->OPN),addr,v);
4916      }
4917      break;
4918
4919   case 2:   /* address port 1 */
4920      F2612->OPN.ST.address = v;
4921      F2612->addr_A1 = 1;
4922      break;
4923
4924   case 3:   /* data port 1    */
4925      if (F2612->addr_A1 != 1)
4926         break;   /* verified on real YM2608 */
4927
4928      addr = F2612->OPN.ST.address;
4929      F2612->REGS[addr | 0x100] = v;
4930      ym2612_update_req(F2612->OPN.ST.param);
4931      OPNWriteReg(&(F2612->OPN),addr | 0x100,v);
4932      break;
4933   }
4934   return F2612->OPN.ST.irq;
4935}
4936
4937UINT8 ym2612_read(void *chip,int a)
4938{
4939   YM2612 *F2612 = (YM2612 *)chip;
4940
4941   switch( a&3){
4942   case 0:   /* status 0 */
4943      return FM_STATUS_FLAG(&F2612->OPN.ST);
4944   case 1:
4945   case 2:
4946   case 3:
4947      LOG(LOG_WAR,("YM2612 #%p:A=%d read unmapped area\n",F2612->OPN.ST.param,a));
4948      return FM_STATUS_FLAG(&F2612->OPN.ST);
4949   }
4950   return 0;
4951}
4952
4953int ym2612_timer_over(void *chip,int c)
4954{
4955   YM2612 *F2612 = (YM2612 *)chip;
4956
4957   if( c )
4958   {   /* Timer B */
4959      TimerBOver( &(F2612->OPN.ST) );
4960   }
4961   else
4962   {   /* Timer A */
4963      ym2612_update_req(F2612->OPN.ST.param);
4964      /* timer update */
4965      TimerAOver( &(F2612->OPN.ST) );
4966      /* CSM mode key,TL controll */
4967      if( F2612->OPN.ST.mode & 0x80 )
4968      {   /* CSM mode total level latch and auto key on */
4969         CSMKeyControll( F2612->OPN.type, &(F2612->CH[2]) );
4970      }
4971   }
4972   return F2612->OPN.ST.irq;
4973}
4974
4975#endif /* (BUILD_YM2612||BUILD_YM3238) */
src/emu/sound/sound.mak
r5435r5436
729729endif
730730
731731ifneq ($(filter YM2612 YM3438,$(SOUNDS)),)
732SOUNDOBJS += $(SOUNDOBJ)/2612intf.o $(SOUNDOBJ)/ay8910.o $(SOUNDOBJ)/fm.o
732SOUNDOBJS += $(SOUNDOBJ)/2612intf.o $(SOUNDOBJ)/ay8910.o $(SOUNDOBJ)/fm2612.o
733733endif
734734
735735ifneq ($(filter YM3812,$(SOUNDS)),)

Previous 15994 Revisions Next


© 1998-2018 The MESS Team