Debian Codegraph @

BambooTracker/chip/mame/fmopn.c

raw
1 // license:GPL-2.0+
2 // copyright-holders:Jarek Burczynski,Tatsuyuki Satoh
3 #define YM2610B_WARNING
4
5 /*
6 **
7 ** File: fm.c -- software implementation of Yamaha FM sound generator
8 **
9 ** Copyright Jarek Burczynski (bujar at mame dot net)
10 ** Copyright Tatsuyuki Satoh , MultiArcadeMachineEmulator development
11 **
12 ** Version 1.5.1 (Genesis Plus GX ym2612.c rev. 368)
13 **
14 */
15
16 /*
17 ** History:
18 **
19 ** 2006~2012 Eke-Eke (Genesis Plus GX):
20 ** Huge thanks to Nemesis, lot of those fixes came from his tests on Sega Genesis hardware
21 ** More informations at http://gendev.spritesmind.net/forum/viewtopic.php?t=386
22 **
23 ** TODO:
24 **
25 ** - core documentation
26 ** - BUSY flag support
27 **
28 ** CHANGELOG:
29 **
30 ** 07-30-2014 dink (FB Alpha project):
31 ** - fixed missing dac channel on savestate load
32 **
33 ** xx-xx-xxxx
34 ** - fixed LFO implementation:
35 ** .added support for CH3 special mode: fixes various sound effects (birds in Warlock, bug sound in Aladdin...)
36 ** .inverted LFO AM waveform: fixes Spider-Man & Venom : Separation Anxiety (intro), California Games (surfing event)
37 ** .improved LFO timing accuracy: now updated AFTER sample output, like EG/PG updates, and without any precision loss anymore.
38 ** - improved internal timers emulation
39 ** - adjusted lowest EG rates increment values
40 ** - fixed Attack Rate not being updated in some specific cases (Batman & Robin intro)
41 ** - fixed EG behavior when Attack Rate is maximal
42 ** - fixed EG behavior when SL=0 (Mega Turrican tracks 03,09...) or/and Key ON occurs at minimal attenuation
43 ** - implemented EG output immediate changes on register writes
44 ** - fixed YM2612 initial values (after the reset): fixes missing intro in B.O.B
45 ** - implemented Detune overflow (Ariel, Comix Zone, Shaq Fu, Spiderman & many other games using GEMS sound engine)
46 ** - implemented accurate CSM mode emulation
47 ** - implemented accurate SSG-EG emulation (Asterix, Beavis&Butthead, Bubba'n Stix & many other games)
48 ** - implemented accurate address/data ports behavior
49 **
50 ** 06-23-2007 Zsolt Vasvari:
51 ** - changed the timing not to require the use of floating point calculations
52 **
53 ** 03-08-2003 Jarek Burczynski:
54 ** - fixed YM2608 initial values (after the reset)
55 ** - fixed flag and irqmask handling (YM2608)
56 ** - fixed BUFRDY flag handling (YM2608)
57 **
58 ** 14-06-2003 Jarek Burczynski:
59 ** - implemented all of the YM2608 status register flags
60 ** - implemented support for external memory read/write via YM2608
61 ** - implemented support for deltat memory limit register in YM2608 emulation
62 **
63 ** 22-05-2003 Jarek Burczynski:
64 ** - fixed LFO PM calculations (copy&paste bugfix)
65 **
66 ** 08-05-2003 Jarek Burczynski:
67 ** - fixed SSG support
68 **
69 ** 22-04-2003 Jarek Burczynski:
70 ** - implemented 100% correct LFO generator (verified on real YM2610 and YM2608)
71 **
72 ** 15-04-2003 Jarek Burczynski:
73 ** - added support for YM2608's register 0x110 - status mask
74 **
75 ** 01-12-2002 Jarek Burczynski:
76 ** - fixed register addressing in YM2608, YM2610, YM2610B chips. (verified on real YM2608)
77 ** The addressing patch used for early Neo-Geo games can be removed now.
78 **
79 ** 26-11-2002 Jarek Burczynski, Nicola Salmoria:
80 ** - recreated YM2608 ADPCM ROM using data from real YM2608's output which leads to:
81 ** - added emulation of YM2608 drums.
82 ** - output of YM2608 is two times lower now - same as YM2610 (verified on real YM2608)
83 **
84 ** 16-08-2002 Jarek Burczynski:
85 ** - binary exact Envelope Generator (verified on real YM2203);
86 ** identical to YM2151
87 ** - corrected 'off by one' error in feedback calculations (when feedback is off)
88 ** - corrected connection (algorithm) calculation (verified on real YM2203 and YM2610)
89 **
90 ** 18-12-2001 Jarek Burczynski:
91 ** - added SSG-EG support (verified on real YM2203)
92 **
93 ** 12-08-2001 Jarek Burczynski:
94 ** - corrected sin_tab and tl_tab data (verified on real chip)
95 ** - corrected feedback calculations (verified on real chip)
96 ** - corrected phase generator calculations (verified on real chip)
97 ** - corrected envelope generator calculations (verified on real chip)
98 ** - corrected FM volume level (YM2610 and YM2610B).
99 ** - changed YMxxxUpdateOne() functions (YM2203, YM2608, YM2610, YM2610B, YM2612) :
100 ** this was needed to calculate YM2610 FM channels output correctly.
101 ** (Each FM channel is calculated as in other chips, but the output of the channel
102 ** gets shifted right by one *before* sending to accumulator. That was impossible to do
103 ** with previous implementation).
104 **
105 ** 23-07-2001 Jarek Burczynski, Nicola Salmoria:
106 ** - corrected YM2610 ADPCM type A algorithm and tables (verified on real chip)
107 **
108 ** 11-06-2001 Jarek Burczynski:
109 ** - corrected end of sample bug in ADPCMA_calc_cha().
110 ** Real YM2610 checks for equality between current and end addresses (only 20 LSB bits).
111 **
112 ** 08-12-98 hiro-shi:
113 ** rename ADPCMA -> ADPCMB, ADPCMB -> ADPCMA
114 ** move ROM limit check.(CALC_CH? -> 2610Write1/2)
115 ** test program (ADPCMB_TEST)
116 ** move ADPCM A/B end check.
117 ** ADPCMB repeat flag(no check)
118 ** change ADPCM volume rate (8->16) (32->48).
119 **
120 ** 09-12-98 hiro-shi:
121 ** change ADPCM volume. (8->16, 48->64)
122 ** replace ym2610 ch0/3 (YM-2610B)
123 ** change ADPCM_SHIFT (10->8) missing bank change 0x4000-0xffff.
124 ** add ADPCM_SHIFT_MASK
125 ** change ADPCMA_DECODE_MIN/MAX.
126 */
127
128
129
130
131 /************************************************************************/
132 /* comment of hiro-shi(Hiromitsu Shioya) */
133 /* YM2610(B) = OPN-B */
134 /* YM2610 : PSG:3ch FM:4ch ADPCM(18.5KHz):6ch DeltaT ADPCM:1ch */
135 /* YM2610B : PSG:3ch FM:6ch ADPCM(18.5KHz):6ch DeltaT ADPCM:1ch */
136 /************************************************************************/
137
138 #include <stdlib.h>
139 #include <string.h> // for memset
140 #include <stddef.h> // for NULL
141 #define _USE_MATH_DEFINES
142 #include <math.h>
143
144 #include "fmopn.h"
145
146
147 /* include external DELTA-T unit (when needed) */
148 #if (BUILD_YM2608||BUILD_YM2610||BUILD_YM2610B)
149 #include "ymdeltat.h"
150 #endif
151
152 /* shared function building option */
153 #define BUILD_OPN (BUILD_YM2203||BUILD_YM2608||BUILD_YM2610||BUILD_YM2610B||BUILD_YM2612)
154 #define BUILD_OPN_PRESCALER (BUILD_YM2203||BUILD_YM2608)
155
156
157 /* globals */
158 #define TYPE_SSG 0x01 /* SSG support */
159 #define TYPE_LFOPAN 0x02 /* OPN type LFO and PAN */
160 #define TYPE_6CH 0x04 /* FM 6CH / 3CH */
161 #define TYPE_DAC 0x08 /* YM2612's DAC device */
162 #define TYPE_ADPCM 0x10 /* two ADPCM units */
163 #define TYPE_2610 0x20 /* bogus flag to differentiate 2608 from 2610 */
164
165
166 #define TYPE_YM2203 (TYPE_SSG)
167 #define TYPE_YM2608 (TYPE_SSG |TYPE_LFOPAN |TYPE_6CH |TYPE_ADPCM)
168 #define TYPE_YM2610 (TYPE_SSG |TYPE_LFOPAN |TYPE_6CH |TYPE_ADPCM |TYPE_2610)
169 #define TYPE_YM2612 (TYPE_DAC |TYPE_LFOPAN |TYPE_6CH)
170
171
172 /* globals */
173 #define FREQ_SH 16 /* 16.16 fixed point (frequency calculations) */
174 #define EG_SH 16 /* 16.16 fixed point (envelope generator timing) */
175 #define LFO_SH 24 /* 8.24 fixed point (LFO calculations) */
176 #define TIMER_SH 16 /* 16.16 fixed point (timers calculations) */
177
178 #define FREQ_MASK ((1<<FREQ_SH)-1)
179
180 /* envelope generator */
181 #define ENV_BITS 10
182 #define ENV_LEN (1<<ENV_BITS)
183 #define ENV_STEP (128.0/ENV_LEN)
184
185 #define MAX_ATT_INDEX (ENV_LEN-1) /* 1023 */
186 #define MIN_ATT_INDEX (0) /* 0 */
187
188 #define EG_ATT 4
189 #define EG_DEC 3
190 #define EG_SUS 2
191 #define EG_REL 1
192 #define EG_OFF 0
193
194 /* operator unit */
195 #define SIN_BITS 10
196 #define SIN_LEN (1<<SIN_BITS)
197 #define SIN_MASK (SIN_LEN-1)
198
199 #define TL_RES_LEN (256) /* 8 bits addressing (real chip) */
200
201 /* TL_TAB_LEN is calculated as:
202 * 13 - sinus amplitude bits (Y axis)
203 * 2 - sinus sign bit (Y axis)
204 * TL_RES_LEN - sinus resolution (X axis)
205 */
206 #define TL_TAB_LEN (13*2*TL_RES_LEN)
207 static signed int tl_tab[TL_TAB_LEN];
208
209 #define ENV_QUIET (TL_TAB_LEN>>3)
210
211 /* sin waveform table in 'decibel' scale */
212 static unsigned int sin_tab[SIN_LEN];
213
214 /* sustain level table (3dB per step) */
215 /* bit0, bit1, bit2, bit3, bit4, bit5, bit6 */
216 /* 1, 2, 4, 8, 16, 32, 64 (value)*/
217 /* 0.75, 1.5, 3, 6, 12, 24, 48 (dB)*/
218
219 /* 0 - 15: 0, 3, 6, 9,12,15,18,21,24,27,30,33,36,39,42,93 (dB)*/
220 /* attenuation value (10 bits) = (SL << 2) << 3 */
221 #define SC(db) (UINT32) ( db * (4.0/ENV_STEP) )
222 static const UINT32 sl_table[16]={
223 SC( 0),SC( 1),SC( 2),SC(3 ),SC(4 ),SC(5 ),SC(6 ),SC( 7),
224 SC( 8),SC( 9),SC(10),SC(11),SC(12),SC(13),SC(14),SC(31)
225 };
226 #undef SC
227
228
229 #define RATE_STEPS (8)
230 static const UINT8 eg_inc[19*RATE_STEPS]={
231 /*cycle:0 1 2 3 4 5 6 7*/
232
233 /* 0 */ 0,1, 0,1, 0,1, 0,1, /* rates 00..11 0 (increment by 0 or 1) */
234 /* 1 */ 0,1, 0,1, 1,1, 0,1, /* rates 00..11 1 */
235 /* 2 */ 0,1, 1,1, 0,1, 1,1, /* rates 00..11 2 */
236 /* 3 */ 0,1, 1,1, 1,1, 1,1, /* rates 00..11 3 */
237
238 /* 4 */ 1,1, 1,1, 1,1, 1,1, /* rate 12 0 (increment by 1) */
239 /* 5 */ 1,1, 1,2, 1,1, 1,2, /* rate 12 1 */
240 /* 6 */ 1,2, 1,2, 1,2, 1,2, /* rate 12 2 */
241 /* 7 */ 1,2, 2,2, 1,2, 2,2, /* rate 12 3 */
242
243 /* 8 */ 2,2, 2,2, 2,2, 2,2, /* rate 13 0 (increment by 2) */
244 /* 9 */ 2,2, 2,4, 2,2, 2,4, /* rate 13 1 */
245 /*10 */ 2,4, 2,4, 2,4, 2,4, /* rate 13 2 */
246 /*11 */ 2,4, 4,4, 2,4, 4,4, /* rate 13 3 */
247
248 /*12 */ 4,4, 4,4, 4,4, 4,4, /* rate 14 0 (increment by 4) */
249 /*13 */ 4,4, 4,8, 4,4, 4,8, /* rate 14 1 */
250 /*14 */ 4,8, 4,8, 4,8, 4,8, /* rate 14 2 */
251 /*15 */ 4,8, 8,8, 4,8, 8,8, /* rate 14 3 */
252
253 /*16 */ 8,8, 8,8, 8,8, 8,8, /* rates 15 0, 15 1, 15 2, 15 3 (increment by 8) */
254 /*17 */ 16,16,16,16,16,16,16,16, /* rates 15 2, 15 3 for attack */
255 /*18 */ 0,0, 0,0, 0,0, 0,0, /* infinity rates for attack and decay(s) */
256 };
257
258
259 #define O(a) (a*RATE_STEPS)
260
261 /*note that there is no O(17) in this table - it's directly in the code */
262 static const UINT8 eg_rate_select[32+64+32]={ /* Envelope Generator rates (32 + 64 rates + 32 RKS) */
263 /* 32 infinite time rates (same as Rate 0) */
264 O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),
265 O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),
266 O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),
267 O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),
268
269 /* rates 00-11 */
270 /*
271 O( 0),O( 1),O( 2),O( 3),
272 O( 0),O( 1),O( 2),O( 3),
273 */
274 O(18),O(18),O( 0),O( 0),
275 O( 0),O( 0),O( 2),O( 2), // Nemesis's tests
276
277 O( 0),O( 1),O( 2),O( 3),
278 O( 0),O( 1),O( 2),O( 3),
279 O( 0),O( 1),O( 2),O( 3),
280 O( 0),O( 1),O( 2),O( 3),
281 O( 0),O( 1),O( 2),O( 3),
282 O( 0),O( 1),O( 2),O( 3),
283 O( 0),O( 1),O( 2),O( 3),
284 O( 0),O( 1),O( 2),O( 3),
285 O( 0),O( 1),O( 2),O( 3),
286 O( 0),O( 1),O( 2),O( 3),
287
288 /* rate 12 */
289 O( 4),O( 5),O( 6),O( 7),
290
291 /* rate 13 */
292 O( 8),O( 9),O(10),O(11),
293
294 /* rate 14 */
295 O(12),O(13),O(14),O(15),
296
297 /* rate 15 */
298 O(16),O(16),O(16),O(16),
299
300 /* 32 dummy rates (same as 15 3) */
301 O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16),
302 O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16),
303 O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16),
304 O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16)
305
306 };
307 #undef O
308
309 /*rate 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15*/
310 /*shift 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0 */
311 /*mask 2047, 1023, 511, 255, 127, 63, 31, 15, 7, 3, 1, 0, 0, 0, 0, 0 */
312
313 #define O(a) (a*1)
314 static const UINT8 eg_rate_shift[32+64+32]={ /* Envelope Generator counter shifts (32 + 64 rates + 32 RKS) */
315 /* 32 infinite time rates */
316 /* O(0),O(0),O(0),O(0),O(0),O(0),O(0),O(0),
317 O(0),O(0),O(0),O(0),O(0),O(0),O(0),O(0),
318 O(0),O(0),O(0),O(0),O(0),O(0),O(0),O(0),
319 O(0),O(0),O(0),O(0),O(0),O(0),O(0),O(0), */
320
321 /* fixed (should be the same as rate 0, even if it makes no difference since increment value is 0 for these rates) */
322 O(11),O(11),O(11),O(11),O(11),O(11),O(11),O(11),
323 O(11),O(11),O(11),O(11),O(11),O(11),O(11),O(11),
324 O(11),O(11),O(11),O(11),O(11),O(11),O(11),O(11),
325 O(11),O(11),O(11),O(11),O(11),O(11),O(11),O(11),
326
327 /* rates 00-11 */
328 O(11),O(11),O(11),O(11),
329 O(10),O(10),O(10),O(10),
330 O( 9),O( 9),O( 9),O( 9),
331 O( 8),O( 8),O( 8),O( 8),
332 O( 7),O( 7),O( 7),O( 7),
333 O( 6),O( 6),O( 6),O( 6),
334 O( 5),O( 5),O( 5),O( 5),
335 O( 4),O( 4),O( 4),O( 4),
336 O( 3),O( 3),O( 3),O( 3),
337 O( 2),O( 2),O( 2),O( 2),
338 O( 1),O( 1),O( 1),O( 1),
339 O( 0),O( 0),O( 0),O( 0),
340
341 /* rate 12 */
342 O( 0),O( 0),O( 0),O( 0),
343
344 /* rate 13 */
345 O( 0),O( 0),O( 0),O( 0),
346
347 /* rate 14 */
348 O( 0),O( 0),O( 0),O( 0),
349
350 /* rate 15 */
351 O( 0),O( 0),O( 0),O( 0),
352
353 /* 32 dummy rates (same as 15 3) */
354 O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),
355 O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),
356 O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),
357 O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0)
358
359 };
360 #undef O
361
362 static const UINT8 dt_tab[4 * 32]={
363 /* this is YM2151 and YM2612 phase increment data (in 10.10 fixed point format)*/
364 /* FD=0 */
365 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
366 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
367 /* FD=1 */
368 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2,
369 2, 3, 3, 3, 4, 4, 4, 5, 5, 6, 6, 7, 8, 8, 8, 8,
370 /* FD=2 */
371 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5,
372 5, 6, 6, 7, 8, 8, 9,10,11,12,13,14,16,16,16,16,
373 /* FD=3 */
374 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 6, 6, 7,
375 8 , 8, 9,10,11,12,13,14,16,17,19,20,22,22,22,22
376 };
377
378
379 /* OPN key frequency number -> key code follow table */
380 /* fnum higher 4bit -> keycode lower 2bit */
381 static const UINT8 opn_fktable[16] = {0,0,0,0,0,0,0,1,2,3,3,3,3,3,3,3};
382
383
384 /* 8 LFO speed parameters */
385 /* each value represents number of samples that one LFO level will last for */
386 static const UINT32 lfo_samples_per_step[8] = {108, 77, 71, 67, 62, 44, 8, 5};
387
388
389
390 /*There are 4 different LFO AM depths available, they are:
391 0 dB, 1.4 dB, 5.9 dB, 11.8 dB
392 Here is how it is generated (in EG steps):
393
394 11.8 dB = 0, 2, 4, 6, 8, 10,12,14,16...126,126,124,122,120,118,....4,2,0
395 5.9 dB = 0, 1, 2, 3, 4, 5, 6, 7, 8....63, 63, 62, 61, 60, 59,.....2,1,0
396 1.4 dB = 0, 0, 0, 0, 1, 1, 1, 1, 2,...15, 15, 15, 15, 14, 14,.....0,0,0
397
398 (1.4 dB is losing precision as you can see)
399
400 It's implemented as generator from 0..126 with step 2 then a shift
401 right N times, where N is:
402 8 for 0 dB
403 3 for 1.4 dB
404 1 for 5.9 dB
405 0 for 11.8 dB
406 */
407 static const UINT8 lfo_ams_depth_shift[4] = {8, 3, 1, 0};
408
409
410
411 /*There are 8 different LFO PM depths available, they are:
412 0, 3.4, 6.7, 10, 14, 20, 40, 80 (cents)
413
414 Modulation level at each depth depends on F-NUMBER bits: 4,5,6,7,8,9,10
415 (bits 8,9,10 = FNUM MSB from OCT/FNUM register)
416
417 Here we store only first quarter (positive one) of full waveform.
418 Full table (lfo_pm_table) containing all 128 waveforms is build
419 at run (init) time.
420
421 One value in table below represents 4 (four) basic LFO steps
422 (1 PM step = 4 AM steps).
423
424 For example:
425 at LFO SPEED=0 (which is 108 samples per basic LFO step)
426 one value from "lfo_pm_output" table lasts for 432 consecutive
427 samples (4*108=432) and one full LFO waveform cycle lasts for 13824
428 samples (32*432=13824; 32 because we store only a quarter of whole
429 waveform in the table below)
430 */
431 static const UINT8 lfo_pm_output[7*8][8]={
432 /* 7 bits meaningful (of F-NUMBER), 8 LFO output levels per one depth (out of 32), 8 LFO depths */
433 /* FNUM BIT 4: 000 0001xxxx */
434 /* DEPTH 0 */ {0, 0, 0, 0, 0, 0, 0, 0},
435 /* DEPTH 1 */ {0, 0, 0, 0, 0, 0, 0, 0},
436 /* DEPTH 2 */ {0, 0, 0, 0, 0, 0, 0, 0},
437 /* DEPTH 3 */ {0, 0, 0, 0, 0, 0, 0, 0},
438 /* DEPTH 4 */ {0, 0, 0, 0, 0, 0, 0, 0},
439 /* DEPTH 5 */ {0, 0, 0, 0, 0, 0, 0, 0},
440 /* DEPTH 6 */ {0, 0, 0, 0, 0, 0, 0, 0},
441 /* DEPTH 7 */ {0, 0, 0, 0, 1, 1, 1, 1},
442
443 /* FNUM BIT 5: 000 0010xxxx */
444 /* DEPTH 0 */ {0, 0, 0, 0, 0, 0, 0, 0},
445 /* DEPTH 1 */ {0, 0, 0, 0, 0, 0, 0, 0},
446 /* DEPTH 2 */ {0, 0, 0, 0, 0, 0, 0, 0},
447 /* DEPTH 3 */ {0, 0, 0, 0, 0, 0, 0, 0},
448 /* DEPTH 4 */ {0, 0, 0, 0, 0, 0, 0, 0},
449 /* DEPTH 5 */ {0, 0, 0, 0, 0, 0, 0, 0},
450 /* DEPTH 6 */ {0, 0, 0, 0, 1, 1, 1, 1},
451 /* DEPTH 7 */ {0, 0, 1, 1, 2, 2, 2, 3},
452
453 /* FNUM BIT 6: 000 0100xxxx */
454 /* DEPTH 0 */ {0, 0, 0, 0, 0, 0, 0, 0},
455 /* DEPTH 1 */ {0, 0, 0, 0, 0, 0, 0, 0},
456 /* DEPTH 2 */ {0, 0, 0, 0, 0, 0, 0, 0},
457 /* DEPTH 3 */ {0, 0, 0, 0, 0, 0, 0, 0},
458 /* DEPTH 4 */ {0, 0, 0, 0, 0, 0, 0, 1},
459 /* DEPTH 5 */ {0, 0, 0, 0, 1, 1, 1, 1},
460 /* DEPTH 6 */ {0, 0, 1, 1, 2, 2, 2, 3},
461 /* DEPTH 7 */ {0, 0, 2, 3, 4, 4, 5, 6},
462
463 /* FNUM BIT 7: 000 1000xxxx */
464 /* DEPTH 0 */ {0, 0, 0, 0, 0, 0, 0, 0},
465 /* DEPTH 1 */ {0, 0, 0, 0, 0, 0, 0, 0},
466 /* DEPTH 2 */ {0, 0, 0, 0, 0, 0, 1, 1},
467 /* DEPTH 3 */ {0, 0, 0, 0, 1, 1, 1, 1},
468 /* DEPTH 4 */ {0, 0, 0, 1, 1, 1, 1, 2},
469 /* DEPTH 5 */ {0, 0, 1, 1, 2, 2, 2, 3},
470 /* DEPTH 6 */ {0, 0, 2, 3, 4, 4, 5, 6},
471 /* DEPTH 7 */ {0, 0, 4, 6, 8, 8, 0xa, 0xc},
472
473 /* FNUM BIT 8: 001 0000xxxx */
474 /* DEPTH 0 */ {0, 0, 0, 0, 0, 0, 0, 0},
475 /* DEPTH 1 */ {0, 0, 0, 0, 1, 1, 1, 1},
476 /* DEPTH 2 */ {0, 0, 0, 1, 1, 1, 2, 2},
477 /* DEPTH 3 */ {0, 0, 1, 1, 2, 2, 3, 3},
478 /* DEPTH 4 */ {0, 0, 1, 2, 2, 2, 3, 4},
479 /* DEPTH 5 */ {0, 0, 2, 3, 4, 4, 5, 6},
480 /* DEPTH 6 */ {0, 0, 4, 6, 8, 8, 0xa, 0xc},
481 /* DEPTH 7 */ {0, 0, 8, 0xc,0x10,0x10,0x14,0x18},
482
483 /* FNUM BIT 9: 010 0000xxxx */
484 /* DEPTH 0 */ {0, 0, 0, 0, 0, 0, 0, 0},
485 /* DEPTH 1 */ {0, 0, 0, 0, 2, 2, 2, 2},
486 /* DEPTH 2 */ {0, 0, 0, 2, 2, 2, 4, 4},
487 /* DEPTH 3 */ {0, 0, 2, 2, 4, 4, 6, 6},
488 /* DEPTH 4 */ {0, 0, 2, 4, 4, 4, 6, 8},
489 /* DEPTH 5 */ {0, 0, 4, 6, 8, 8, 0xa, 0xc},
490 /* DEPTH 6 */ {0, 0, 8, 0xc,0x10,0x10,0x14,0x18},
491 /* DEPTH 7 */ {0, 0,0x10,0x18,0x20,0x20,0x28,0x30},
492
493 /* FNUM BIT10: 100 0000xxxx */
494 /* DEPTH 0 */ {0, 0, 0, 0, 0, 0, 0, 0},
495 /* DEPTH 1 */ {0, 0, 0, 0, 4, 4, 4, 4},
496 /* DEPTH 2 */ {0, 0, 0, 4, 4, 4, 8, 8},
497 /* DEPTH 3 */ {0, 0, 4, 4, 8, 8, 0xc, 0xc},
498 /* DEPTH 4 */ {0, 0, 4, 8, 8, 8, 0xc,0x10},
499 /* DEPTH 5 */ {0, 0, 8, 0xc,0x10,0x10,0x14,0x18},
500 /* DEPTH 6 */ {0, 0,0x10,0x18,0x20,0x20,0x28,0x30},
501 /* DEPTH 7 */ {0, 0,0x20,0x30,0x40,0x40,0x50,0x60},
502
503 };
504
505 /* all 128 LFO PM waveforms */
506 static INT32 lfo_pm_table[128*8*32]; /* 128 combinations of 7 bits meaningful (of F-NUMBER), 8 LFO depths, 32 LFO output levels per one depth */
507
508 /* register number to channel number , slot offset */
509 #define OPN_CHAN(N) (N&3)
510 #define OPN_SLOT(N) ((N>>2)&3)
511
512 /* slot number */
513 #define SLOT1 0
514 #define SLOT2 2
515 #define SLOT3 1
516 #define SLOT4 3
517
518 /* bit0 = Right enable , bit1 = Left enable */
519 #define OUTD_RIGHT 1
520 #define OUTD_LEFT 2
521 #define OUTD_CENTER 3
522
523
524 /* struct describing a single operator (SLOT) */
525 typedef struct
526 {
527 INT32 *DT; /* detune :dt_tab[DT] */
528 UINT8 KSR; /* key scale rate :3-KSR */
529 UINT32 ar; /* attack rate */
530 UINT32 d1r; /* decay rate */
531 UINT32 d2r; /* sustain rate */
532 UINT32 rr; /* release rate */
533 UINT8 ksr; /* key scale rate :kcode>>(3-KSR) */
534 UINT32 mul; /* multiple :ML_TABLE[ML] */
535
536 /* Phase Generator */
537 UINT32 phase; /* phase counter */
538 INT32 Incr; /* phase step */
539
540 /* Envelope Generator */
541 UINT8 state; /* phase type */
542 UINT32 tl; /* total level: TL << 3 */
543 INT32 volume; /* envelope counter */
544 UINT32 sl; /* sustain level:sl_table[SL] */
545 UINT32 vol_out; /* current output from EG circuit (without AM from LFO) */
546
547 UINT8 eg_sh_ar; /* (attack state) */
548 UINT8 eg_sel_ar; /* (attack state) */
549 UINT8 eg_sh_d1r; /* (decay state) */
550 UINT8 eg_sel_d1r; /* (decay state) */
551 UINT8 eg_sh_d2r; /* (sustain state) */
552 UINT8 eg_sel_d2r; /* (sustain state) */
553 UINT8 eg_sh_rr; /* (release state) */
554 UINT8 eg_sel_rr; /* (release state) */
555
556 UINT8 ssg; /* SSG-EG waveform */
557 UINT8 ssgn; /* SSG-EG negated output */
558
559 UINT8 key; /* 0=last key was KEY OFF, 1=KEY ON */
560
561 /* LFO */
562 UINT32 AMmask; /* AM enable flag */
563
564 } FM_SLOT;
565
566 typedef struct
567 {
568 FM_SLOT SLOT[4]; /* four SLOTs (operators) */
569
570 UINT8 ALGO; /* algorithm */
571 UINT8 FB; /* feedback shift */
572 INT32 op1_out[2]; /* op1 output for feedback */
573
574 INT32 *connect1; /* SLOT1 output pointer */
575 INT32 *connect3; /* SLOT3 output pointer */
576 INT32 *connect2; /* SLOT2 output pointer */
577 INT32 *connect4; /* SLOT4 output pointer */
578
579 INT32 *mem_connect;/* where to put the delayed sample (MEM) */
580 INT32 mem_value; /* delayed sample (MEM) value */
581
582 INT32 pms; /* channel PMS */
583 UINT8 ams; /* channel AMS */
584
585 UINT32 fc; /* fnum,blk:adjusted to sample rate */
586 UINT8 kcode; /* key code: */
587 UINT32 block_fnum; /* current blk/fnum value for this slot (can be different betweeen slots of one channel in 3slot mode) */
588 UINT8 Muted;
589 } FM_CH;
590
591
592 typedef struct
593 {
594 void * param; /* this chip parameter */
595 UINT32 clock; /* master clock (Hz) */
596 UINT32 rate; /* sampling rate (Hz) */
597 double freqbase; /* frequency base */
598 int timer_prescaler; /* timer prescaler */
599 #if FM_BUSY_FLAG_SUPPORT
600 TIME_TYPE busy_expiry_time; /* expiry time of the busy status */
601 #endif
602 UINT8 address; /* address register */
603 UINT8 irq; /* interrupt level */
604 UINT8 irqmask; /* irq mask */
605 UINT8 status; /* status flag */
606 UINT32 mode; /* mode CSM / 3SLOT */
607 UINT8 fn_h; /* freq latch */
608 UINT8 prescaler_sel; /* prescaler selector */
609 INT32 TA; /* timer a */
610 INT32 TAC; /* timer a counter */
611 UINT8 TB; /* timer b */
612 INT32 TBC; /* timer b counter */
613 /* local time tables */
614 INT32 dt_tab[8][32]; /* DeTune table */
615 /* Extention Timer and IRQ handler */
616 FM_TIMERHANDLER timer_handler;
617 FM_IRQHANDLER IRQ_Handler;
618 ssg_callbacks SSG_funcs;
619 void * SSG_param;
620 } FM_ST;
621
622
623
624 /***********************************************************/
625 /* OPN unit */
626 /***********************************************************/
627
628 /* OPN 3slot struct */
629 typedef struct
630 {
631 UINT32 fc[3]; /* fnum3,blk3: calculated */
632 UINT8 fn_h; /* freq3 latch */
633 UINT8 kcode[3]; /* key code */
634 UINT32 block_fnum[3]; /* current fnum value for this slot (can be different betweeen slots of one channel in 3slot mode) */
635 UINT8 key_csm; /* CSM mode Key-ON flag */
636 } FM_3SLOT;
637
638 /* OPN/A/B common state */
639 typedef struct
640 {
641 UINT8 type; /* chip type */
642 UINT8 smpRateNative; /* emulating at native sample rate (enable sample rate change callback) */
643 UINT8 LegacyMode; /* behave like old emulation regarding FNum writes and Key Off */
644 FM_ST ST; /* general state */
645 FM_3SLOT SL3; /* 3 slot mode state */
646 FM_CH *P_CH; /* pointer of CH */
647 UINT32 pan[6*2]; /* fm channels output masks (0xffffffff = enable) */
648
649 UINT32 eg_cnt; /* global envelope generator counter */
650 UINT32 eg_timer; /* global envelope generator counter works at frequency = chipclock/144/3 */
651 UINT32 eg_timer_add; /* step of eg_timer */
652 UINT32 eg_timer_overflow;/* envelope generator timer overflows every 3 samples (on real chip) */
653
654
655 /* there are 2048 FNUMs that can be generated using FNUM/BLK registers
656 but LFO works with one more bit of a precision so we really need 4096 elements */
657 UINT32 fn_table[4096]; /* fnumber->increment counter */
658 UINT32 fn_max; /* maximal phase increment (used for phase overflow) */
659
660 /* LFO */
661 UINT8 lfo_cnt; /* current LFO phase (out of 128) */
662 UINT32 lfo_timer; /* current LFO phase runs at LFO frequency */
663 UINT32 lfo_timer_add; /* step of lfo_timer */
664 UINT32 lfo_timer_overflow; /* LFO timer overflows every N samples (depends on LFO frequency) */
665 UINT32 LFO_AM; /* current LFO AM step */
666 UINT32 LFO_PM; /* current LFO PM step */
667
668 INT32 m2,c1,c2; /* Phase Modulation input for operators 2,3,4 */
669 INT32 mem; /* one sample delay memory */
670
671 INT32 out_fm[6]; /* outputs of working channels */
672
673 #if (BUILD_YM2608||BUILD_YM2610||BUILD_YM2610B)
674 INT32 out_adpcm[4]; /* channel output NONE,LEFT,RIGHT or CENTER for YM2608/YM2610 ADPCM */
675 INT32 out_delta[4]; /* channel output NONE,LEFT,RIGHT or CENTER for YM2608/YM2610 DELTAT*/
676 #endif
677
678 DEVCB_SRATE_CHG smpRateFunc;
679 void* smpRateData;
680 } FM_OPN;
681
682
683
684 /* log output level */
685 #define LOG_ERR 3 /* ERROR */
686 #define LOG_WAR 2 /* WARNING */
687 #define LOG_INF 1 /* INFORMATION */
688 #define LOG_LEVEL LOG_INF
689
690 #ifndef __RAINE__
691 #define LOG(n,x) do { if( (n)>=LOG_LEVEL ) logerror x; } while (0)
692 #endif
693
694 /* limitter */
695 #define Limit(val, max,min) { \
696 if ( val > max ) val = max; \
697 else if ( val < min ) val = min; \
698 }
699
700
701 static UINT8 tablesInit = 0;
702
703 /* status set and IRQ handling */
704 INLINE void FM_STATUS_SET(FM_ST *ST,int flag)
705 {
706 /* set status flag */
707 ST->status |= flag;
708 if ( !(ST->irq) && (ST->status & ST->irqmask) )
709 {
710 ST->irq = 1;
711 /* callback user interrupt handler (IRQ is OFF to ON) */
712 if(ST->IRQ_Handler) (ST->IRQ_Handler)(ST->param,1);
713 }
714 }
715
716 /* status reset and IRQ handling */
717 INLINE void FM_STATUS_RESET(FM_ST *ST,int flag)
718 {
719 /* reset status flag */
720 ST->status &=~flag;
721 if ( (ST->irq) && !(ST->status & ST->irqmask) )
722 {
723 ST->irq = 0;
724 /* callback user interrupt handler (IRQ is ON to OFF) */
725 if(ST->IRQ_Handler) (ST->IRQ_Handler)(ST->param,0);
726 }
727 }
728
729 /* IRQ mask set */
730 INLINE void FM_IRQMASK_SET(FM_ST *ST,int flag)
731 {
732 ST->irqmask = (UINT8)flag;
733 /* IRQ handling check */
734 FM_STATUS_SET(ST,0);
735 FM_STATUS_RESET(ST,0);
736 }
737
738 INLINE void FM_KEYON(FM_OPN *OPN, FM_CH *CH , int s )
739 {
740 FM_SLOT *SLOT = &CH->SLOT[s];
741
742 // Note by Valley Bell:
743 // I assume that the CSM mode shouldn't affect channels
744 // other than FM3, so I added a check for it here.
745 if( !SLOT->key && (!OPN->SL3.key_csm || CH == &OPN->P_CH[2]))
746 {
747 /* restart Phase Generator */
748 SLOT->phase = 0;
749
750 /* reset SSG-EG inversion flag */
751 SLOT->ssgn = 0;
752
753 if ((SLOT->ar + SLOT->ksr) < 94 /*32+62*/)
754 {
755 SLOT->state = (SLOT->volume <= MIN_ATT_INDEX) ? ((SLOT->sl == MIN_ATT_INDEX) ? EG_SUS : EG_DEC) : EG_ATT;
756 }
757 else
758 {
759 /* force attenuation level to 0 */
760 SLOT->volume = MIN_ATT_INDEX;
761
762 /* directly switch to Decay (or Sustain) */
763 SLOT->state = (SLOT->sl == MIN_ATT_INDEX) ? EG_SUS : EG_DEC;
764 }
765
766 /* recalculate EG output */
767 if ((SLOT->ssg&0x08) && (SLOT->ssgn ^ (SLOT->ssg&0x04)))
768 SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;
769 else
770 SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
771 }
772
773 SLOT->key = 1;
774 }
775
776 INLINE void FM_KEYOFF(FM_OPN *OPN, FM_CH *CH , int s )
777 {
778 FM_SLOT *SLOT = &CH->SLOT[s];
779
780 if (SLOT->key && (!OPN->SL3.key_csm || CH == &OPN->P_CH[2]))
781 {
782 if (SLOT->state>EG_REL)
783 {
784 SLOT->state = EG_REL; /* phase -> Release */
785
786 /* SSG-EG specific update */
787 if (SLOT->ssg&0x08)
788 {
789 /* convert EG attenuation level */
790 if (SLOT->ssgn ^ (SLOT->ssg&0x04))
791 SLOT->volume = (0x200 - SLOT->volume);
792
793 /* force EG attenuation level */
794 if (SLOT->volume >= 0x200)
795 {
796 SLOT->volume = MAX_ATT_INDEX;
797 SLOT->state = EG_OFF;
798 }
799
800 /* recalculate EG output */
801 SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
802 }
803 }
804 if (OPN->LegacyMode) // workaround for VGMs trimmed with VGMTool
805 {
806 // When at maximum release rate AND there was a Key On just this sample, enforce an instant Key Off.
807 if (SLOT->rr >= 94 && SLOT->phase == 0)
808 {
809 SLOT->volume = MAX_ATT_INDEX;
810 SLOT->state = EG_OFF;
811 }
812 }
813 }
814
815 SLOT->key = 0;
816 }
817
818 INLINE void FM_KEYON_CSM(FM_OPN *OPN, FM_CH *CH , int s )
819 {
820 FM_SLOT *SLOT = &CH->SLOT[s];
821
822 if (!SLOT->key && !OPN->SL3.key_csm)
823 {
824 /* restart Phase Generator */
825 SLOT->phase = 0;
826
827 /* reset SSG-EG inversion flag */
828 SLOT->ssgn = 0;
829
830 if ((SLOT->ar + SLOT->ksr) < 94 /*32+62*/)
831 {
832 SLOT->state = (SLOT->volume <= MIN_ATT_INDEX) ? ((SLOT->sl == MIN_ATT_INDEX) ? EG_SUS : EG_DEC) : EG_ATT;
833 }
834 else
835 {
836 /* force attenuation level to 0 */
837 SLOT->volume = MIN_ATT_INDEX;
838
839 /* directly switch to Decay (or Sustain) */
840 SLOT->state = (SLOT->sl == MIN_ATT_INDEX) ? EG_SUS : EG_DEC;
841 }
842
843 /* recalculate EG output */
844 if ((SLOT->ssg&0x08) && (SLOT->ssgn ^ (SLOT->ssg&0x04)))
845 SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;
846 else
847 SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
848 }
849 }
850
851 INLINE void FM_KEYOFF_CSM(FM_CH *CH , int s )
852 {
853 FM_SLOT *SLOT = &CH->SLOT[s];
854 if (!SLOT->key)
855 {
856 if (SLOT->state>EG_REL)
857 {
858 SLOT->state = EG_REL; /* phase -> Release */
859
860 /* SSG-EG specific update */
861 if (SLOT->ssg&0x08)
862 {
863 /* convert EG attenuation level */
864 if (SLOT->ssgn ^ (SLOT->ssg&0x04))
865 SLOT->volume = (0x200 - SLOT->volume);
866
867 /* force EG attenuation level */
868 if (SLOT->volume >= 0x200)
869 {
870 SLOT->volume = MAX_ATT_INDEX;
871 SLOT->state = EG_OFF;
872 }
873
874 /* recalculate EG output */
875 SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
876 }
877 }
878 }
879 }
880
881 /* OPN Mode Register Write */
882 INLINE void set_timers( FM_OPN *OPN, FM_ST *ST, void *n, int v )
883 {
884 /* b7 = CSM MODE */
885 /* b6 = 3 slot mode */
886 /* b5 = reset b */
887 /* b4 = reset a */
888 /* b3 = timer enable b */
889 /* b2 = timer enable a */
890 /* b1 = load b */
891 /* b0 = load a */
892
893 if ((OPN->ST.mode ^ v) & 0xC0)
894 {
895 /* phase increment need to be recalculated */
896 OPN->P_CH[2].SLOT[SLOT1].Incr=-1;
897
898 /* CSM mode disabled and CSM key ON active*/
899 if (((v & 0xC0) != 0x80) && OPN->SL3.key_csm)
900 {
901 /* CSM Mode Key OFF (verified by Nemesis on real hardware) */
902 FM_KEYOFF_CSM(&OPN->P_CH[2],SLOT1);
903 FM_KEYOFF_CSM(&OPN->P_CH[2],SLOT2);
904 FM_KEYOFF_CSM(&OPN->P_CH[2],SLOT3);
905 FM_KEYOFF_CSM(&OPN->P_CH[2],SLOT4);
906 OPN->SL3.key_csm = 0;
907 }
908 }
909
910 /* reload Timers */
911 if ((v&1) && !(ST->mode&1))
912 {
913 ST->TAC = (1024-ST->TA);
914 /* External timer handler */
915 if (ST->timer_handler) (ST->timer_handler)(n,0,ST->TAC * ST->timer_prescaler,ST->clock);
916 ST->TAC *= 4096;
917 }
918 else if (!(v & 1))
919 {
920 if( ST->TAC != 0 )
921 {
922 ST->TAC = 0;
923 if (ST->timer_handler) (ST->timer_handler)(n,0,0,ST->clock);
924 }
925 }
926
927 if ((v&2) && !(ST->mode&2))
928 {
929 ST->TBC = ( 256-ST->TB)<<4;
930 /* External timer handler */
931 if (ST->timer_handler) (ST->timer_handler)(n,1,ST->TBC * ST->timer_prescaler,ST->clock);
932 ST->TBC *= 4096;
933 }
934 else if (!(v & 2))
935 {
936 if( ST->TBC != 0 )
937 {
938 ST->TBC = 0;
939 if (ST->timer_handler) (ST->timer_handler)(n,1,0,ST->clock);
940 }
941 }
942
943 /* reset Timers flags */
944 ST->status &= (~v >> 4);
945
946 /* if IRQ should be lowered now, do so */
947 if ( (ST->irq) && !(ST->status & ST->irqmask) )
948 {
949 ST->irq = 0;
950 /* callback user interrupt handler (IRQ is ON to OFF) */
951 if(ST->IRQ_Handler) (ST->IRQ_Handler)(ST->param, 0);
952 }
953 ST->mode = v;
954 }
955
956
957 /* Timer A Overflow */
958 INLINE void TimerAOver(FM_ST *ST)
959 {
960 /* set status (if enabled) */
961 if(ST->mode & 0x04) FM_STATUS_SET(ST,0x01);
962 /* clear or reload the counter */
963 ST->TAC = (1024-ST->TA);
964 if (ST->timer_handler) (ST->timer_handler)(ST->param,0,ST->TAC * ST->timer_prescaler,ST->clock);
965 ST->TAC *= 4096;
966 }
967 /* Timer B Overflow */
968 INLINE void TimerBOver(FM_ST *ST)
969 {
970 /* set status (if enabled) */
971 if(ST->mode & 0x08) FM_STATUS_SET(ST,0x02);
972 /* clear or reload the counter */
973 ST->TBC = ( 256-ST->TB)<<4;
974 if (ST->timer_handler) (ST->timer_handler)(ST->param,1,ST->TBC * ST->timer_prescaler,ST->clock);
975 ST->TBC *= 4096;
976 }
977
978
979 #if FM_INTERNAL_TIMER
980 /* ----- internal timer mode , update timer */
981
982 /* ---------- calculate timer A ---------- */
983 #define INTERNAL_TIMER_A(ST,CSM_CH) \
984 { \
985 if( (ST)->TAC && ((ST)->timer_handler==0) ) \
986 if( ((ST)->TAC -= (int)((ST)->freqbase*4096)) <= 0 ) \
987 { \
988 TimerAOver( ST ); \
989 /* CSM mode total level latch and auto key on */ \
990 if( (ST)->mode & 0x80 ) \
991 CSMKeyControll( OPN, CSM_CH ); \
992 } \
993 }
994 /* ---------- calculate timer B ---------- */
995 #define INTERNAL_TIMER_B(ST,step) \
996 { \
997 if( (ST)->TBC && ((ST)->timer_handler==0) ) \
998 if( ((ST)->TBC -= (int)((ST)->freqbase*4096*step)) <= 0 ) \
999 TimerBOver( ST ); \
1000 }
1001 #else /* FM_INTERNAL_TIMER */
1002 /* external timer mode */
1003 #define INTERNAL_TIMER_A(ST,CSM_CH)
1004 #define INTERNAL_TIMER_B(ST,step)
1005 #endif /* FM_INTERNAL_TIMER */
1006
1007
1008
1009 #if FM_BUSY_FLAG_SUPPORT
1010 #define FM_BUSY_CLEAR(ST) ((ST)->busy_expiry_time = UNDEFINED_TIME)
1011 INLINE UINT8 FM_STATUS_FLAG(FM_ST *ST)
1012 {
1013 if( COMPARE_TIMES(ST->busy_expiry_time, UNDEFINED_TIME) != 0 )
1014 {
1015 if (COMPARE_TIMES(ST->busy_expiry_time, FM_GET_TIME_NOW(&ST->device->machine())) > 0)
1016 return ST->status | 0x80; /* with busy */
1017 /* expire */
1018 FM_BUSY_CLEAR(ST);
1019 }
1020 return ST->status;
1021 }
1022 INLINE void FM_BUSY_SET(FM_ST *ST,int busyclock )
1023 {
1024 TIME_TYPE expiry_period = MULTIPLY_TIME_BY_INT(attotime::from_hz(ST->clock), busyclock * ST->timer_prescaler);
1025 ST->busy_expiry_time = ADD_TIMES(FM_GET_TIME_NOW(&ST->device->machine()), expiry_period);
1026 }
1027 #else
1028 #define FM_STATUS_FLAG(ST) ((ST)->status)
1029 #define FM_BUSY_SET(ST,bclock) {}
1030 #define FM_BUSY_CLEAR(ST) {}
1031 #endif
1032
1033
1034 /* set algorithm connection */
1035 INLINE void setup_connection( FM_OPN *OPN, FM_CH *CH, int ch )
1036 {
1037 INT32 *carrier = &OPN->out_fm[ch];
1038
1039 INT32 **om1 = &CH->connect1;
1040 INT32 **om2 = &CH->connect3;
1041 INT32 **oc1 = &CH->connect2;
1042
1043 INT32 **memc = &CH->mem_connect;
1044
1045 switch( CH->ALGO )
1046 {
1047 case 0:
1048 /* M1---C1---MEM---M2---C2---OUT */
1049 *om1 = &OPN->c1;
1050 *oc1 = &OPN->mem;
1051 *om2 = &OPN->c2;
1052 *memc= &OPN->m2;
1053 break;
1054 case 1:
1055 /* M1------+-MEM---M2---C2---OUT */
1056 /* C1-+ */
1057 *om1 = &OPN->mem;
1058 *oc1 = &OPN->mem;
1059 *om2 = &OPN->c2;
1060 *memc= &OPN->m2;
1061 break;
1062 case 2:
1063 /* M1-----------------+-C2---OUT */
1064 /* C1---MEM---M2-+ */
1065 *om1 = &OPN->c2;
1066 *oc1 = &OPN->mem;
1067 *om2 = &OPN->c2;
1068 *memc= &OPN->m2;
1069 break;
1070 case 3:
1071 /* M1---C1---MEM------+-C2---OUT */
1072 /* M2-+ */
1073 *om1 = &OPN->c1;
1074 *oc1 = &OPN->mem;
1075 *om2 = &OPN->c2;
1076 *memc= &OPN->c2;
1077 break;
1078 case 4:
1079 /* M1---C1-+-OUT */
1080 /* M2---C2-+ */
1081 /* MEM: not used */
1082 *om1 = &OPN->c1;
1083 *oc1 = carrier;
1084 *om2 = &OPN->c2;
1085 *memc= &OPN->mem; /* store it anywhere where it will not be used */
1086 break;
1087 case 5:
1088 /* +----C1----+ */
1089 /* M1-+-MEM---M2-+-OUT */
1090 /* +----C2----+ */
1091 *om1 = NULL; /* special mark */
1092 *oc1 = carrier;
1093 *om2 = carrier;
1094 *memc= &OPN->m2;
1095 break;
1096 case 6:
1097 /* M1---C1-+ */
1098 /* M2-+-OUT */
1099 /* C2-+ */
1100 /* MEM: not used */
1101 *om1 = &OPN->c1;
1102 *oc1 = carrier;
1103 *om2 = carrier;
1104 *memc= &OPN->mem; /* store it anywhere where it will not be used */
1105 break;
1106 case 7:
1107 /* M1-+ */
1108 /* C1-+-OUT */
1109 /* M2-+ */
1110 /* C2-+ */
1111 /* MEM: not used*/
1112 *om1 = carrier;
1113 *oc1 = carrier;
1114 *om2 = carrier;
1115 *memc= &OPN->mem; /* store it anywhere where it will not be used */
1116 break;
1117 }
1118
1119 CH->connect4 = carrier;
1120 }
1121
1122 /* set detune & multiple */
1123 INLINE void set_det_mul(FM_ST *ST,FM_CH *CH,FM_SLOT *SLOT,int v)
1124 {
1125 SLOT->mul = (v&0x0f)? (v&0x0f)*2 : 1;
1126 SLOT->DT = ST->dt_tab[(v>>4)&7];
1127 CH->SLOT[SLOT1].Incr=-1;
1128 }
1129
1130 /* set total level */
1131 INLINE void set_tl(FM_CH *CH,FM_SLOT *SLOT , int v)
1132 {
1133 (void)CH;
1134 SLOT->tl = (v&0x7f)<<(ENV_BITS-7); /* 7bit TL */
1135
1136 /* recalculate EG output */
1137 if ((SLOT->ssg&0x08) && (SLOT->ssgn ^ (SLOT->ssg&0x04)) && (SLOT->state > EG_REL))
1138 SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;
1139 else
1140 SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
1141 }
1142
1143 /* set attack rate & key scale */
1144 INLINE void set_ar_ksr(UINT8 type, FM_CH *CH,FM_SLOT *SLOT,int v)
1145 {
1146 (void)type;
1147 UINT8 old_KSR = SLOT->KSR;
1148
1149 SLOT->ar = (v&0x1f) ? 32 + ((v&0x1f)<<1) : 0;
1150
1151 SLOT->KSR = (UINT8)(3-(v>>6));
1152 if (SLOT->KSR != old_KSR)
1153 {
1154 CH->SLOT[SLOT1].Incr=-1;
1155 }
1156
1157 /* Even if it seems unnecessary to do it here, it could happen that KSR and KC */
1158 /* are modified but the resulted SLOT->ksr value (kc >> SLOT->KSR) remains unchanged. */
1159 /* In such case, Attack Rate would not be recalculated by "refresh_fc_eg_slot". */
1160 /* This actually fixes the intro of "The Adventures of Batman & Robin" (Eke-Eke) */
1161 if ((SLOT->ar + SLOT->ksr) < (32+62))
1162 {
1163 SLOT->eg_sh_ar = eg_rate_shift [SLOT->ar + SLOT->ksr ];
1164 SLOT->eg_sel_ar = eg_rate_select[SLOT->ar + SLOT->ksr ];
1165 }
1166 else
1167 {
1168 /* verified by Nemesis on real hardware (Attack phase is blocked) */
1169 SLOT->eg_sh_ar = 0;
1170 SLOT->eg_sel_ar = 18*RATE_STEPS;
1171 }
1172 }
1173
1174 /* set decay rate */
1175 INLINE void set_dr(UINT8 type, FM_SLOT *SLOT,int v)
1176 {
1177 (void)type;
1178 SLOT->d1r = (v&0x1f) ? 32 + ((v&0x1f)<<1) : 0;
1179
1180 SLOT->eg_sh_d1r = eg_rate_shift [SLOT->d1r + SLOT->ksr];
1181 SLOT->eg_sel_d1r= eg_rate_select[SLOT->d1r + SLOT->ksr];
1182 }
1183
1184 /* set sustain rate */
1185 INLINE void set_sr(UINT8 type, FM_SLOT *SLOT,int v)
1186 {
1187 (void)type;
1188 SLOT->d2r = (v&0x1f) ? 32 + ((v&0x1f)<<1) : 0;
1189
1190 SLOT->eg_sh_d2r = eg_rate_shift [SLOT->d2r + SLOT->ksr];
1191 SLOT->eg_sel_d2r= eg_rate_select[SLOT->d2r + SLOT->ksr];
1192 }
1193
1194 /* set release rate */
1195 INLINE void set_sl_rr(UINT8 type, FM_SLOT *SLOT,int v)
1196 {
1197 (void)type;
1198 SLOT->sl = sl_table[ v>>4 ];
1199
1200 /* check EG state changes */
1201 if ((SLOT->state == EG_DEC) && (SLOT->volume >= (INT32)(SLOT->sl)))
1202 SLOT->state = EG_SUS;
1203
1204 SLOT->rr = 34 + ((v&0x0f)<<2);
1205
1206 SLOT->eg_sh_rr = eg_rate_shift [SLOT->rr + SLOT->ksr];
1207 SLOT->eg_sel_rr = eg_rate_select[SLOT->rr + SLOT->ksr];
1208 }
1209
1210 /* advance LFO to next sample */
1211 INLINE void advance_lfo(FM_OPN *OPN)
1212 {
1213 if (OPN->lfo_timer_overflow) /* LFO enabled ? */
1214 {
1215 /* increment LFO timer */
1216 OPN->lfo_timer += OPN->lfo_timer_add;
1217
1218 /* when LFO is enabled, one level will last for 108, 77, 71, 67, 62, 44, 8 or 5 samples */
1219 while (OPN->lfo_timer >= OPN->lfo_timer_overflow)
1220 {
1221 OPN->lfo_timer -= OPN->lfo_timer_overflow;
1222
1223 /* There are 128 LFO steps */
1224 OPN->lfo_cnt = ( OPN->lfo_cnt + 1 ) & 127;
1225
1226 /* triangle (inverted) */
1227 /* AM: from 126 to 0 step -2, 0 to 126 step +2 */
1228 if (OPN->lfo_cnt<64)
1229 OPN->LFO_AM = (OPN->lfo_cnt ^ 63) << 1;
1230 else
1231 OPN->LFO_AM = (OPN->lfo_cnt & 63) << 1;
1232
1233 /* PM works with 4 times slower clock */
1234 OPN->LFO_PM = OPN->lfo_cnt >> 2;
1235 }
1236 }
1237 }
1238
1239 INLINE void advance_eg_channel(FM_OPN *OPN, FM_SLOT *SLOT)
1240 {
1241 //unsigned int out;
1242 unsigned int i = 4; /* four operators per channel */
1243
1244 do
1245 {
1246 switch(SLOT->state)
1247 {
1248 case EG_ATT: /* attack phase */
1249 if (!(OPN->eg_cnt & ((1<<SLOT->eg_sh_ar)-1)))
1250 {
1251 /* update attenuation level */
1252 SLOT->volume += (~SLOT->volume * (eg_inc[SLOT->eg_sel_ar + ((OPN->eg_cnt>>SLOT->eg_sh_ar)&7)]))>>4;
1253
1254 /* check phase transition*/
1255 if (SLOT->volume <= MIN_ATT_INDEX)
1256 {
1257 SLOT->volume = MIN_ATT_INDEX;
1258 SLOT->state = (SLOT->sl == MIN_ATT_INDEX) ? EG_SUS : EG_DEC; /* special case where SL=0 */
1259 }
1260
1261 /* recalculate EG output */
1262 if ((SLOT->ssg&0x08) && (SLOT->ssgn ^ (SLOT->ssg&0x04))) /* SSG-EG Output Inversion */
1263 SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;
1264 else
1265 SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
1266 }
1267 break;
1268
1269 case EG_DEC: /* decay phase */
1270 if (!(OPN->eg_cnt & ((1<<SLOT->eg_sh_d1r)-1)))
1271 {
1272 /* SSG EG type */
1273 if (SLOT->ssg&0x08)
1274 {
1275 /* update attenuation level */
1276 if (SLOT->volume < 0x200)
1277 {
1278 SLOT->volume += 4 * eg_inc[SLOT->eg_sel_d1r + ((OPN->eg_cnt>>SLOT->eg_sh_d1r)&7)];
1279
1280 /* recalculate EG output */
1281 if (SLOT->ssgn ^ (SLOT->ssg&0x04)) /* SSG-EG Output Inversion */
1282 SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;
1283 else
1284 SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
1285 }
1286
1287 }
1288 else
1289 {
1290 /* update attenuation level */
1291 SLOT->volume += eg_inc[SLOT->eg_sel_d1r + ((OPN->eg_cnt>>SLOT->eg_sh_d1r)&7)];
1292
1293 /* recalculate EG output */
1294 SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
1295 }
1296
1297 /* check phase transition*/
1298 if (SLOT->volume >= (INT32)(SLOT->sl))
1299 SLOT->state = EG_SUS;
1300 }
1301 break;
1302
1303 case EG_SUS: /* sustain phase */
1304 if (!(OPN->eg_cnt & ((1<<SLOT->eg_sh_d2r)-1)))
1305 {
1306 /* SSG EG type */
1307 if (SLOT->ssg&0x08)
1308 {
1309 /* update attenuation level */
1310 if (SLOT->volume < 0x200)
1311 {
1312 SLOT->volume += 4 * eg_inc[SLOT->eg_sel_d2r + ((OPN->eg_cnt>>SLOT->eg_sh_d2r)&7)];
1313
1314 /* recalculate EG output */
1315 if (SLOT->ssgn ^ (SLOT->ssg&0x04)) /* SSG-EG Output Inversion */
1316 SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;
1317 else
1318 SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
1319 }
1320 }
1321 else
1322 {
1323 /* update attenuation level */
1324 SLOT->volume += eg_inc[SLOT->eg_sel_d2r + ((OPN->eg_cnt>>SLOT->eg_sh_d2r)&7)];
1325
1326 /* check phase transition*/
1327 if ( SLOT->volume >= MAX_ATT_INDEX )
1328 SLOT->volume = MAX_ATT_INDEX;
1329 /* do not change SLOT->state (verified on real chip) */
1330
1331 /* recalculate EG output */
1332 SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
1333 }
1334 }
1335 break;
1336
1337 case EG_REL: /* release phase */
1338 if (!(OPN->eg_cnt & ((1<<SLOT->eg_sh_rr)-1)))
1339 {
1340 /* SSG EG type */
1341 if (SLOT->ssg&0x08)
1342 {
1343 /* update attenuation level */
1344 if (SLOT->volume < 0x200)
1345 SLOT->volume += 4 * eg_inc[SLOT->eg_sel_rr + ((OPN->eg_cnt>>SLOT->eg_sh_rr)&7)];
1346 /* check phase transition */
1347 if (SLOT->volume >= 0x200)
1348 {
1349 SLOT->volume = MAX_ATT_INDEX;
1350 SLOT->state = EG_OFF;
1351 }
1352 }
1353 else
1354 {
1355 /* update attenuation level */
1356 SLOT->volume += eg_inc[SLOT->eg_sel_rr + ((OPN->eg_cnt>>SLOT->eg_sh_rr)&7)];
1357
1358 /* check phase transition*/
1359 if (SLOT->volume >= MAX_ATT_INDEX)
1360 {
1361 SLOT->volume = MAX_ATT_INDEX;
1362 SLOT->state = EG_OFF;
1363 }
1364 }
1365
1366 /* recalculate EG output */
1367 SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
1368
1369 }
1370 break;
1371 }
1372
1373
1374 SLOT++;
1375 i--;
1376 }while (i);
1377
1378 }
1379
1380 /* SSG-EG update process */
1381 /* The behavior is based upon Nemesis tests on real hardware */
1382 /* This is actually executed before each samples */
1383 INLINE void update_ssg_eg_channel(FM_SLOT *SLOT)
1384 {
1385 unsigned int i = 4; /* four operators per channel */
1386
1387 do
1388 {
1389 /* detect SSG-EG transition */
1390 /* this is not required during release phase as the attenuation has been forced to MAX and output invert flag is not used */
1391 /* if an Attack Phase is programmed, inversion can occur on each sample */
1392 if ((SLOT->ssg & 0x08) && (SLOT->volume >= 0x200) && (SLOT->state > EG_REL))
1393 {
1394 if (SLOT->ssg & 0x01) /* bit 0 = hold SSG-EG */
1395 {
1396 /* set inversion flag */
1397 if (SLOT->ssg & 0x02)
1398 SLOT->ssgn = 4;
1399
1400 /* force attenuation level during decay phases */
1401 if ((SLOT->state != EG_ATT) && !(SLOT->ssgn ^ (SLOT->ssg & 0x04)))
1402 SLOT->volume = MAX_ATT_INDEX;
1403 }
1404 else /* loop SSG-EG */
1405 {
1406 /* toggle output inversion flag or reset Phase Generator */
1407 if (SLOT->ssg & 0x02)
1408 SLOT->ssgn ^= 4;
1409 else
1410 SLOT->phase = 0;
1411
1412 /* same as Key ON */
1413 if (SLOT->state != EG_ATT)
1414 {
1415 if ((SLOT->ar + SLOT->ksr) < 94 /*32+62*/)
1416 {
1417 SLOT->state = (SLOT->volume <= MIN_ATT_INDEX) ? ((SLOT->sl == MIN_ATT_INDEX) ? EG_SUS : EG_DEC) : EG_ATT;
1418 }
1419 else
1420 {
1421 /* Attack Rate is maximal: directly switch to Decay or Substain */
1422 SLOT->volume = MIN_ATT_INDEX;
1423 SLOT->state = (SLOT->sl == MIN_ATT_INDEX) ? EG_SUS : EG_DEC;
1424 }
1425 }
1426 }
1427
1428 /* recalculate EG output */
1429 if (SLOT->ssgn ^ (SLOT->ssg&0x04))
1430 SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;
1431 else
1432 SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
1433 }
1434
1435 /* next slot */
1436 SLOT++;
1437 i--;
1438 } while (i);
1439 }
1440
1441
1442 INLINE void update_phase_lfo_slot(FM_OPN *OPN, FM_SLOT *SLOT, INT32 pms, UINT32 block_fnum)
1443 {
1444 UINT32 fnum_lfo = ((block_fnum & 0x7f0) >> 4) * 32 * 8;
1445 INT32 lfo_fn_table_index_offset = lfo_pm_table[ fnum_lfo + pms + OPN->LFO_PM ];
1446
1447 block_fnum = block_fnum*2 + lfo_fn_table_index_offset;
1448
1449 if (lfo_fn_table_index_offset) /* LFO phase modulation active */
1450 {
1451 UINT8 blk = (block_fnum&0x7000) >> 12;
1452 UINT32 fn = block_fnum & 0xfff;
1453
1454 /* recalculate keyscale code */
1455 /*int kc = (blk<<2) | opn_fktable[fn >> 7];*/
1456 /* This really stupid bug caused a read outside of the
1457 array [size 0x10] and returned invalid values.
1458 This caused an annoying vibrato for some notes.
1459 (Note: seems to be a copy-and-paste from OPNWriteReg -> case 0xA0)
1460 Why are MAME cores always SOO buggy ?! */
1461 /* Oh, and before I forget: it's correct in fm.c */
1462 int kc = (blk<<2) | opn_fktable[fn >> 8];
1463 /* Thanks to Blargg - his patch that helped me to find this bug */
1464
1465 /* recalculate (frequency) phase increment counter */
1466 int fc = (OPN->fn_table[fn]>>(7-blk)) + SLOT->DT[kc];
1467
1468 /* (frequency) phase overflow (credits to Nemesis) */
1469 if (fc < 0) fc += OPN->fn_max;
1470
1471 /* update phase */
1472 SLOT->phase += (fc * SLOT->mul) >> 1;
1473 }
1474 else /* LFO phase modulation = zero */
1475 {
1476 SLOT->phase += SLOT->Incr;
1477 }
1478 }
1479
1480 INLINE void update_phase_lfo_channel(FM_OPN *OPN, FM_CH *CH)
1481 {
1482 UINT32 block_fnum = CH->block_fnum;
1483
1484 UINT32 fnum_lfo = ((block_fnum & 0x7f0) >> 4) * 32 * 8;
1485 INT32 lfo_fn_table_index_offset = lfo_pm_table[ fnum_lfo + CH->pms + OPN->LFO_PM ];
1486
1487 block_fnum = block_fnum*2 + lfo_fn_table_index_offset;
1488
1489 if (lfo_fn_table_index_offset) /* LFO phase modulation active */
1490 {
1491 UINT8 blk = (block_fnum&0x7000) >> 12;
1492 UINT32 fn = block_fnum & 0xfff;
1493
1494 /* recalculate keyscale code */
1495 /*int kc = (blk<<2) | opn_fktable[fn >> 7];*/
1496 /* the same stupid bug as above */
1497 int kc = (blk<<2) | opn_fktable[fn >> 8];
1498
1499 /* recalculate (frequency) phase increment counter */
1500 int fc = (OPN->fn_table[fn]>>(7-blk));
1501
1502 /* (frequency) phase overflow (credits to Nemesis) */
1503 int finc = fc + CH->SLOT[SLOT1].DT[kc];
1504 if (finc < 0) finc += OPN->fn_max;
1505 CH->SLOT[SLOT1].phase += (finc*CH->SLOT[SLOT1].mul) >> 1;
1506
1507 finc = fc + CH->SLOT[SLOT2].DT[kc];
1508 if (finc < 0) finc += OPN->fn_max;
1509 CH->SLOT[SLOT2].phase += (finc*CH->SLOT[SLOT2].mul) >> 1;
1510
1511 finc = fc + CH->SLOT[SLOT3].DT[kc];
1512 if (finc < 0) finc += OPN->fn_max;
1513 CH->SLOT[SLOT3].phase += (finc*CH->SLOT[SLOT3].mul) >> 1;
1514
1515 finc = fc + CH->SLOT[SLOT4].DT[kc];
1516 if (finc < 0) finc += OPN->fn_max;
1517 CH->SLOT[SLOT4].phase += (finc*CH->SLOT[SLOT4].mul) >> 1;
1518 }
1519 else /* LFO phase modulation = zero */
1520 {
1521 CH->SLOT[SLOT1].phase += CH->SLOT[SLOT1].Incr;
1522 CH->SLOT[SLOT2].phase += CH->SLOT[SLOT2].Incr;
1523 CH->SLOT[SLOT3].phase += CH->SLOT[SLOT3].Incr;
1524 CH->SLOT[SLOT4].phase += CH->SLOT[SLOT4].Incr;
1525 }
1526 }
1527
1528 /* update phase increment and envelope generator */
1529 INLINE void refresh_fc_eg_slot(FM_OPN *OPN, FM_SLOT *SLOT , int fc , int kc )
1530 {
1531 int ksr = kc >> SLOT->KSR;
1532
1533 fc += SLOT->DT[kc];
1534
1535 /* detects frequency overflow (credits to Nemesis) */
1536 if (fc < 0) fc += OPN->fn_max;
1537
1538 /* (frequency) phase increment counter */
1539 SLOT->Incr = (fc * SLOT->mul) >> 1;
1540
1541 if( SLOT->ksr != ksr )
1542 {
1543 SLOT->ksr = (UINT8)ksr;
1544
1545 /* calculate envelope generator rates */
1546 if ((SLOT->ar + SLOT->ksr) < 32+62)
1547 {
1548 SLOT->eg_sh_ar = eg_rate_shift [SLOT->ar + SLOT->ksr ];
1549 SLOT->eg_sel_ar = eg_rate_select[SLOT->ar + SLOT->ksr ];
1550 }
1551 else
1552 {
1553 SLOT->eg_sh_ar = 0;
1554 SLOT->eg_sel_ar = 18*RATE_STEPS; /* verified by Nemesis on real hardware (Attack phase is blocked) */
1555 }
1556
1557 SLOT->eg_sh_d1r = eg_rate_shift [SLOT->d1r + SLOT->ksr];
1558 SLOT->eg_sh_d2r = eg_rate_shift [SLOT->d2r + SLOT->ksr];
1559 SLOT->eg_sh_rr = eg_rate_shift [SLOT->rr + SLOT->ksr];
1560
1561 SLOT->eg_sel_d1r= eg_rate_select[SLOT->d1r + SLOT->ksr];
1562 SLOT->eg_sel_d2r= eg_rate_select[SLOT->d2r + SLOT->ksr];
1563 SLOT->eg_sel_rr = eg_rate_select[SLOT->rr + SLOT->ksr];
1564 }
1565 }
1566
1567 /* update phase increment counters */
1568 INLINE void refresh_fc_eg_chan(FM_OPN *OPN, FM_CH *CH )
1569 {
1570 if( CH->SLOT[SLOT1].Incr==-1)
1571 {
1572 int fc = CH->fc;
1573 int kc = CH->kcode;
1574 refresh_fc_eg_slot(OPN, &CH->SLOT[SLOT1] , fc , kc );
1575 refresh_fc_eg_slot(OPN, &CH->SLOT[SLOT2] , fc , kc );
1576 refresh_fc_eg_slot(OPN, &CH->SLOT[SLOT3] , fc , kc );
1577 refresh_fc_eg_slot(OPN, &CH->SLOT[SLOT4] , fc , kc );
1578 }
1579 }
1580
1581 #define volume_calc(OP) ((OP)->vol_out + (AM & (OP)->AMmask))
1582
1583 INLINE signed int op_calc(UINT32 phase, unsigned int env, signed int pm)
1584 {
1585 UINT32 p;
1586
1587 p = (env<<3) + sin_tab[ ( ((signed int)((phase & ~FREQ_MASK) + (pm<<15))) >> FREQ_SH ) & SIN_MASK ];
1588
1589 if (p >= TL_TAB_LEN)
1590 return 0;
1591 return tl_tab[p];
1592 }
1593
1594 INLINE signed int op_calc1(UINT32 phase, unsigned int env, signed int pm)
1595 {
1596 UINT32 p;
1597
1598 p = (env<<3) + sin_tab[ ( ((signed int)((phase & ~FREQ_MASK) + pm )) >> FREQ_SH ) & SIN_MASK ];
1599
1600 if (p >= TL_TAB_LEN)
1601 return 0;
1602 return tl_tab[p];
1603 }
1604
1605 INLINE void chan_calc(FM_OPN *OPN, FM_CH *CH, int chnum)
1606 {
1607 UINT32 AM = OPN->LFO_AM >> CH->ams;
1608 unsigned int eg_out;
1609
1610 if (CH->Muted)
1611 return;
1612
1613 OPN->m2 = OPN->c1 = OPN->c2 = OPN->mem = 0;
1614
1615 *CH->mem_connect = CH->mem_value; /* restore delayed sample (MEM) value to m2 or c2 */
1616
1617 eg_out = volume_calc(&CH->SLOT[SLOT1]);
1618 {
1619 INT32 out = CH->op1_out[0] + CH->op1_out[1];
1620 CH->op1_out[0] = CH->op1_out[1];
1621
1622 if( !CH->connect1 )
1623 {
1624 /* algorithm 5 */
1625 OPN->mem = OPN->c1 = OPN->c2 = CH->op1_out[0];
1626 }
1627 else
1628 {
1629 /* other algorithms */
1630 *CH->connect1 += CH->op1_out[0];
1631 }
1632
1633
1634 CH->op1_out[1] = 0;
1635 if( eg_out < ENV_QUIET ) /* SLOT 1 */
1636 {
1637 if (!CH->FB)
1638 out=0;
1639
1640 CH->op1_out[1] = op_calc1(CH->SLOT[SLOT1].phase, eg_out, (out<<CH->FB) );
1641 }
1642 }
1643
1644 eg_out = volume_calc(&CH->SLOT[SLOT3]);
1645 if( eg_out < ENV_QUIET ) /* SLOT 3 */
1646 *CH->connect3 += op_calc(CH->SLOT[SLOT3].phase, eg_out, OPN->m2);
1647
1648 eg_out = volume_calc(&CH->SLOT[SLOT2]);
1649 if( eg_out < ENV_QUIET ) /* SLOT 2 */
1650 *CH->connect2 += op_calc(CH->SLOT[SLOT2].phase, eg_out, OPN->c1);
1651
1652 eg_out = volume_calc(&CH->SLOT[SLOT4]);
1653 if( eg_out < ENV_QUIET ) /* SLOT 4 */
1654 *CH->connect4 += op_calc(CH->SLOT[SLOT4].phase, eg_out, OPN->c2);
1655
1656
1657 /* store current MEM */
1658 CH->mem_value = OPN->mem;
1659
1660 /* update phase counters AFTER output calculations */
1661 if(CH->pms)
1662 {
1663 /* add support for 3 slot mode */
1664 if ((OPN->ST.mode & 0xC0) && (chnum == 2))
1665 {
1666 update_phase_lfo_slot(OPN, &CH->SLOT[SLOT1], CH->pms, OPN->SL3.block_fnum[1]);
1667 update_phase_lfo_slot(OPN, &CH->SLOT[SLOT2], CH->pms, OPN->SL3.block_fnum[2]);
1668 update_phase_lfo_slot(OPN, &CH->SLOT[SLOT3], CH->pms, OPN->SL3.block_fnum[0]);
1669 update_phase_lfo_slot(OPN, &CH->SLOT[SLOT4], CH->pms, CH->block_fnum);
1670 }
1671 else update_phase_lfo_channel(OPN, CH);
1672 }
1673 else /* no LFO phase modulation */
1674 {
1675 CH->SLOT[SLOT1].phase += CH->SLOT[SLOT1].Incr;
1676 CH->SLOT[SLOT2].phase += CH->SLOT[SLOT2].Incr;
1677 CH->SLOT[SLOT3].phase += CH->SLOT[SLOT3].Incr;
1678 CH->SLOT[SLOT4].phase += CH->SLOT[SLOT4].Incr;
1679 }
1680 }
1681
1682
1683 /* CSM Key Controll */
1684 INLINE void CSMKeyControll(FM_OPN *OPN, FM_CH *CH)
1685 {
1686 /* all key ON (verified by Nemesis on real hardware) */
1687 FM_KEYON_CSM(OPN,CH,SLOT1);
1688 FM_KEYON_CSM(OPN,CH,SLOT2);
1689 FM_KEYON_CSM(OPN,CH,SLOT3);
1690 FM_KEYON_CSM(OPN,CH,SLOT4);
1691 OPN->SL3.key_csm = 1;
1692 }
1693
1694 #if BUILD_OPN
1695
1696 /* write a OPN mode register 0x20-0x2f */
1697 static void OPNWriteMode(FM_OPN *OPN, int r, int v)
1698 {
1699 UINT8 c;
1700 FM_CH *CH;
1701
1702 switch(r)
1703 {
1704 case 0x21: /* Test */
1705 break;
1706 case 0x22: /* LFO FREQ (YM2608/YM2610/YM2610B/YM2612) */
1707 if( !(OPN->type & TYPE_LFOPAN) )
1708 break;
1709 if (v&8) /* LFO enabled ? */
1710 {
1711 OPN->lfo_timer_overflow = lfo_samples_per_step[v&7] << LFO_SH;
1712 }
1713 else
1714 {
1715 /* hold LFO waveform in reset state */
1716 OPN->lfo_timer_overflow = 0;
1717 OPN->lfo_timer = 0;
1718 OPN->lfo_cnt = 0;
1719 OPN->LFO_PM = 0;
1720 OPN->LFO_AM = 126;
1721 }
1722 break;
1723 case 0x24: /* timer A High 8*/
1724 OPN->ST.TA = (OPN->ST.TA & 0x03)|(((int)v)<<2);
1725 break;
1726 case 0x25: /* timer A Low 2*/
1727 OPN->ST.TA = (OPN->ST.TA & 0x3fc)|(v&3);
1728 break;
1729 case 0x26: /* timer B */
1730 OPN->ST.TB = (UINT8)v;
1731 break;
1732 case 0x27: /* mode, timer control */
1733 set_timers( OPN, &(OPN->ST),OPN->ST.param,v );
1734 break;
1735 case 0x28: /* key on / off */
1736 c = v & 0x03;
1737 if( c == 3 ) break;
1738 if( (v&0x04) && (OPN->type & TYPE_6CH) ) c+=3;
1739 CH = OPN->P_CH;
1740 CH = &CH[c];
1741 if(v&0x10) FM_KEYON(OPN,CH,SLOT1); else FM_KEYOFF(OPN,CH,SLOT1);
1742 if(v&0x20) FM_KEYON(OPN,CH,SLOT2); else FM_KEYOFF(OPN,CH,SLOT2);
1743 if(v&0x40) FM_KEYON(OPN,CH,SLOT3); else FM_KEYOFF(OPN,CH,SLOT3);
1744 if(v&0x80) FM_KEYON(OPN,CH,SLOT4); else FM_KEYOFF(OPN,CH,SLOT4);
1745 break;
1746 }
1747 }
1748
1749 /* write a OPN register (0x30-0xff) */
1750 static void OPNWriteReg(FM_OPN *OPN, int r, int v)
1751 {
1752 FM_CH *CH;
1753 FM_SLOT *SLOT;
1754
1755 UINT8 c = OPN_CHAN(r);
1756
1757 if (c == 3) return; /* 0xX3,0xX7,0xXB,0xXF */
1758
1759 if (r >= 0x100) c+=3;
1760
1761 CH = OPN->P_CH;
1762 CH = &CH[c];
1763
1764 SLOT = &(CH->SLOT[OPN_SLOT(r)]);
1765
1766 switch( r & 0xf0 )
1767 {
1768 case 0x30: /* DET , MUL */
1769 set_det_mul(&OPN->ST,CH,SLOT,v);
1770 break;
1771
1772 case 0x40: /* TL */
1773 set_tl(CH,SLOT,v);
1774 break;
1775
1776 case 0x50: /* KS, AR */
1777 set_ar_ksr(OPN->type,CH,SLOT,v);
1778 break;
1779
1780 case 0x60: /* bit7 = AM ENABLE, DR */
1781 set_dr(OPN->type, SLOT,v);
1782
1783 if(OPN->type & TYPE_LFOPAN) /* YM2608/2610/2610B/2612 */
1784 {
1785 SLOT->AMmask = (v&0x80) ? ~0 : 0;
1786 }
1787 break;
1788
1789 case 0x70: /* SR */
1790 set_sr(OPN->type,SLOT,v);
1791 break;
1792
1793 case 0x80: /* SL, RR */
1794 set_sl_rr(OPN->type,SLOT,v);
1795 break;
1796
1797 case 0x90: /* SSG-EG */
1798 SLOT->ssg = v&0x0f;
1799
1800 /* recalculate EG output */
1801 if (SLOT->state > EG_REL)
1802 {
1803 if ((SLOT->ssg&0x08) && (SLOT->ssgn ^ (SLOT->ssg&0x04)))
1804 SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;
1805 else
1806 SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
1807 }
1808
1809 /* SSG-EG envelope shapes :
1810
1811 E AtAlH
1812 1 0 0 0 \\\\
1813
1814 1 0 0 1 \___
1815
1816 1 0 1 0 \/\/
1817 ___
1818 1 0 1 1 \
1819
1820 1 1 0 0 ////
1821 ___
1822 1 1 0 1 /
1823
1824 1 1 1 0 /\/\
1825
1826 1 1 1 1 /___
1827
1828
1829 E = SSG-EG enable
1830
1831
1832 The shapes are generated using Attack, Decay and Sustain phases.
1833
1834 Each single character in the diagrams above represents this whole
1835 sequence:
1836
1837 - when KEY-ON = 1, normal Attack phase is generated (*without* any
1838 difference when compared to normal mode),
1839
1840 - later, when envelope level reaches minimum level (max volume),
1841 the EG switches to Decay phase (which works with bigger steps
1842 when compared to normal mode - see below),
1843
1844 - later when envelope level passes the SL level,
1845 the EG swithes to Sustain phase (which works with bigger steps
1846 when compared to normal mode - see below),
1847
1848 - finally when envelope level reaches maximum level (min volume),
1849 the EG switches to Attack phase again (depends on actual waveform).
1850
1851 Important is that when switch to Attack phase occurs, the phase counter
1852 of that operator will be zeroed-out (as in normal KEY-ON) but not always.
1853 (I havent found the rule for that - perhaps only when the output level is low)
1854
1855 The difference (when compared to normal Envelope Generator mode) is
1856 that the resolution in Decay and Sustain phases is 4 times lower;
1857 this results in only 256 steps instead of normal 1024.
1858 In other words:
1859 when SSG-EG is disabled, the step inside of the EG is one,
1860 when SSG-EG is enabled, the step is four (in Decay and Sustain phases).
1861
1862 Times between the level changes are the same in both modes.
1863
1864
1865 Important:
1866 Decay 1 Level (so called SL) is compared to actual SSG-EG output, so
1867 it is the same in both SSG and no-SSG modes, with this exception:
1868
1869 when the SSG-EG is enabled and is generating raising levels
1870 (when the EG output is inverted) the SL will be found at wrong level !!!
1871 For example, when SL=02:
1872 0 -6 = -6dB in non-inverted EG output
1873 96-6 = -90dB in inverted EG output
1874 Which means that EG compares its level to SL as usual, and that the
1875 output is simply inverted afterall.
1876
1877
1878 The Yamaha's manuals say that AR should be set to 0x1f (max speed).
1879 That is not necessary, but then EG will be generating Attack phase.
1880
1881 */
1882
1883
1884 break;
1885
1886 case 0xa0:
1887 switch( OPN_SLOT(r) )
1888 {
1889 case 0: /* 0xa0-0xa2 : FNUM1 */
1890 if (OPN->LegacyMode)
1891 OPN->ST.fn_h = (UINT8)(CH->block_fnum >> 8);
1892 {
1893 UINT32 fn = (((UINT32)( (OPN->ST.fn_h)&7))<<8) + v;
1894 UINT8 blk = OPN->ST.fn_h>>3;
1895 /* keyscale code */
1896 CH->kcode = (blk<<2) | opn_fktable[fn >> 7];
1897 /* phase increment counter */
1898 CH->fc = OPN->fn_table[fn*2]>>(7-blk);
1899
1900 /* store fnum in clear form for LFO PM calculations */
1901 CH->block_fnum = (blk<<11) | fn;
1902
1903 CH->SLOT[SLOT1].Incr=-1;
1904 }
1905 break;
1906 case 1: /* 0xa4-0xa6 : FNUM2,BLK */
1907 OPN->ST.fn_h = v&0x3f;
1908 if (OPN->LegacyMode) // behave like Gens (workaround for stupid Kega Fusion init block)
1909 CH->block_fnum = (OPN->ST.fn_h << 8) | (CH->block_fnum & 0xFF);
1910 break;
1911 case 2: /* 0xa8-0xaa : 3CH FNUM1 */
1912 if (OPN->LegacyMode)
1913 OPN->SL3.fn_h = (UINT8)(OPN->SL3.block_fnum[c] >> 8);
1914 if(r < 0x100)
1915 {
1916 UINT32 fn = (((UINT32)(OPN->SL3.fn_h&7))<<8) + v;
1917 UINT8 blk = OPN->SL3.fn_h>>3;
1918 /* keyscale code */
1919 OPN->SL3.kcode[c]= (blk<<2) | opn_fktable[fn >> 7];
1920 /* phase increment counter */
1921 OPN->SL3.fc[c] = OPN->fn_table[fn*2]>>(7-blk);
1922 OPN->SL3.block_fnum[c] = (blk<<11) | fn;
1923 (OPN->P_CH)[2].SLOT[SLOT1].Incr=-1;
1924 }
1925 break;
1926 case 3: /* 0xac-0xae : 3CH FNUM2,BLK */
1927 if(r < 0x100)
1928 {
1929 OPN->SL3.fn_h = v&0x3f;
1930 if (OPN->LegacyMode)
1931 OPN->SL3.block_fnum[c] = (OPN->SL3.fn_h << 8) | (OPN->SL3.block_fnum[c] & 0xFF);
1932 }
1933 break;
1934 }
1935 break;
1936
1937 case 0xb0:
1938 switch( OPN_SLOT(r) )
1939 {
1940 case 0: /* 0xb0-0xb2 : FB,ALGO */
1941 {
1942 int feedback = (v>>3)&7;
1943 CH->ALGO = v&7;
1944 CH->FB = (UINT8)(feedback ? feedback+6 : 0);
1945 setup_connection( OPN, CH, c );
1946 }
1947 break;
1948 case 1: /* 0xb4-0xb6 : L , R , AMS , PMS (YM2612/YM2610B/YM2610/YM2608) */
1949 if( OPN->type & TYPE_LFOPAN)
1950 {
1951 /* b0-2 PMS */
1952 CH->pms = (v & 7) * 32; /* CH->pms = PM depth * 32 (index in lfo_pm_table) */
1953
1954 /* b4-5 AMS */
1955 CH->ams = lfo_ams_depth_shift[(v>>4) & 0x03];
1956
1957 /* PAN : b7 = L, b6 = R */
1958 OPN->pan[ c*2 ] = (v & 0x80) ? ~0 : 0;
1959 OPN->pan[ c*2+1 ] = (v & 0x40) ? ~0 : 0;
1960
1961 }
1962 break;
1963 }
1964 break;
1965 }
1966 }
1967
1968 /* initialize time tables */
1969 static void init_timetables(FM_OPN *OPN)
1970 {
1971 int i,d;
1972 double rate;
1973
1974 #if 0
1975 logerror("FM.C: samplerate=%8i chip clock=%8i freqbase=%f \n",
1976 ST->rate, ST->clock, ST->freqbase );
1977 #endif
1978
1979 /* DeTune table */
1980 for (d = 0;d <= 3;d++)
1981 {
1982 for (i = 0;i <= 31;i++)
1983 {
1984 rate = ((double)dt_tab[d*32 + i]) * OPN->ST.freqbase * (1<<(FREQ_SH-10)); /* -10 because chip works with 10.10 fixed point, while we use 16.16 */
1985 OPN->ST.dt_tab[d][i] = (INT32) rate;
1986 OPN->ST.dt_tab[d+4][i] = -OPN->ST.dt_tab[d][i];
1987 #if 0
1988 logerror("FM.C: DT [%2i %2i] = %8x \n", d, i, ST->dt_tab[d][i] );
1989 #endif
1990 }
1991 }
1992
1993 /* there are 2048 FNUMs that can be generated using FNUM/BLK registers
1994 but LFO works with one more bit of a precision so we really need 4096 elements */
1995 /* calculate fnumber -> increment counter table */
1996 for(i = 0; i < 4096; i++)
1997 {
1998 /* freq table for octave 7 */
1999 /* OPN phase increment counter = 20bit */
2000 /* the correct formula is : F-Number = (144 * fnote * 2^20 / M) / 2^(B-1) */
2001 /* where sample clock is M/144 */
2002 /* this means the increment value for one clock sample is FNUM * 2^(B-1) = FNUM * 64 for octave 7 */
2003 /* we also need to handle the ratio between the chip frequency and the emulated frequency (can be 1.0) */
2004 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 */
2005 }
2006
2007 /* maximal frequency is required for Phase overflow calculation, register size is 17 bits (Nemesis) */
2008 OPN->fn_max = (UINT32)( (double)0x20000 * OPN->ST.freqbase * (1<<(FREQ_SH-10)) );
2009 }
2010
2011 /* prescaler set (and make time tables) */
2012 static void OPNSetPres(FM_OPN *OPN, int pres, int timer_prescaler, int SSGpres)
2013 {
2014 if (OPN->smpRateNative && OPN->smpRateFunc != NULL)
2015 {
2016 OPN->ST.rate = OPN->ST.clock / pres;
2017 if (OPN->smpRateFunc != NULL)
2018 OPN->smpRateFunc(OPN->smpRateData, OPN->ST.rate);
2019 }
2020
2021 /* frequency base */
2022 OPN->ST.freqbase = (OPN->ST.rate) ? ((double)OPN->ST.clock / OPN->ST.rate) / pres : 0;
2023 if (fabs(OPN->ST.freqbase - 1.0) < 0.00005)
2024 OPN->ST.freqbase = 1.0;
2025
2026 #if 0
2027 OPN->ST.rate = (double)OPN->ST.clock / pres;
2028 OPN->ST.freqbase = 1.0;
2029 #endif
2030
2031 /* EG is updated every 3 samples */
2032 OPN->eg_timer_add = (UINT32)((1<<EG_SH) * OPN->ST.freqbase);
2033 OPN->eg_timer_overflow = ( 3 ) * (1<<EG_SH);
2034
2035 /* LFO timer increment (every samples) */
2036 OPN->lfo_timer_add = (UINT32)((1<<LFO_SH) * OPN->ST.freqbase);
2037
2038 /* Timer base time */
2039 OPN->ST.timer_prescaler = timer_prescaler;
2040
2041 /* SSG part prescaler set */
2042 if( SSGpres ) OPN->ST.SSG_funcs.set_clock( OPN->ST.SSG_param, OPN->ST.clock * 2 / SSGpres );
2043
2044 /* make time tables */
2045 init_timetables( OPN );
2046 }
2047
2048
2049 static void reset_channels( FM_ST *ST , FM_CH *CH , int num )
2050 {
2051 int c,s;
2052
2053 ST->mode = 0; /* normal mode */
2054
2055 for( c = 0 ; c < num ; c++ )
2056 {
2057 //memset(&CH[c], 0x00, sizeof(FM_CH));
2058 CH[c].mem_value = 0;
2059 CH[c].op1_out[0] = 0;
2060 CH[c].op1_out[1] = 0;
2061 CH[c].fc = 0;
2062 for(s = 0 ; s < 4 ; s++ )
2063 {
2064 //memset(&CH[c].SLOT[s], 0x00, sizeof(FM_SLOT));
2065 CH[c].SLOT[s].DT = ST->dt_tab[0];
2066 CH[c].SLOT[s].Incr = -1;
2067 CH[c].SLOT[s].key = 0;
2068 CH[c].SLOT[s].phase = 0;
2069 CH[c].SLOT[s].ssg = 0;
2070 CH[c].SLOT[s].ssgn = 0;
2071 CH[c].SLOT[s].state= EG_OFF;
2072 CH[c].SLOT[s].volume = MAX_ATT_INDEX;
2073 CH[c].SLOT[s].vol_out= MAX_ATT_INDEX;
2074 }
2075 }
2076 }
2077
2078 /* initialize generic tables */
2079 static void init_tables(void)
2080 {
2081 signed int i,x;
2082 signed int n;
2083 double o,m;
2084
2085 if (tablesInit)
2086 return;
2087 tablesInit = 1;
2088
2089 /* build Linear Power Table */
2090 for (x=0; x<TL_RES_LEN; x++)
2091 {
2092 m = (1<<16) / pow(2, (x+1) * (ENV_STEP/4.0) / 8.0);
2093 m = floor(m);
2094
2095 /* we never reach (1<<16) here due to the (x+1) */
2096 /* result fits within 16 bits at maximum */
2097
2098 n = (int)m; /* 16 bits here */
2099 n >>= 4; /* 12 bits here */
2100 if (n&1) /* round to nearest */
2101 n = (n>>1)+1;
2102 else
2103 n = n>>1;
2104 /* 11 bits here (rounded) */
2105 n <<= 2; /* 13 bits here (as in real chip) */
2106
2107
2108 /* 14 bits (with sign bit) */
2109 tl_tab[ x*2 + 0 ] = n;
2110 tl_tab[ x*2 + 1 ] = -tl_tab[ x*2 + 0 ];
2111
2112 /* one entry in the 'Power' table use the following format, xxxxxyyyyyyyys with: */
2113 /* s = sign bit */
2114 /* yyyyyyyy = 8-bits decimal part (0-TL_RES_LEN) */
2115 /* xxxxx = 5-bits integer 'shift' value (0-31) but, since Power table output is 13 bits, */
2116 /* any value above 13 (included) would be discarded. */
2117 for (i=1; i<13; i++)
2118 {
2119 tl_tab[ x*2+0 + i*2*TL_RES_LEN ] = tl_tab[ x*2+0 ]>>i;
2120 tl_tab[ x*2+1 + i*2*TL_RES_LEN ] = -tl_tab[ x*2+0 + i*2*TL_RES_LEN ];
2121 }
2122 #if 0
2123 logerror("tl %04i", x);
2124 for (i=0; i<13; i++)
2125 logerror(", [%02i] %4x", i*2, tl_tab[ x*2 /*+1*/ + i*2*TL_RES_LEN ]);
2126 logerror("\n");
2127 #endif
2128 }
2129 /*logerror("FM.C: TL_TAB_LEN = %i elements (%i bytes)\n",TL_TAB_LEN, (int)sizeof(tl_tab));*/
2130
2131
2132 /* build Logarithmic Sinus table */
2133 for (i=0; i<SIN_LEN; i++)
2134 {
2135 /* non-standard sinus */
2136 m = sin( ((i*2)+1) * M_PI / SIN_LEN ); /* checked against the real chip */
2137 /* we never reach zero here due to ((i*2)+1) */
2138
2139 if (m>0.0)
2140 o = 8*log(1.0/m)/log(2.0); /* convert to 'decibels' */
2141 else
2142 o = 8*log(-1.0/m)/log(2.0); /* convert to 'decibels' */
2143
2144 o = o / (ENV_STEP/4);
2145
2146 n = (int)(2.0*o);
2147 if (n&1) /* round to nearest */
2148 n = (n>>1)+1;
2149 else
2150 n = n>>1;
2151
2152 /* 13-bits (8.5) value is formatted for above 'Power' table */
2153 sin_tab[ i ] = n*2 + (m>=0.0? 0: 1 );
2154 /*logerror("FM.C: sin [%4i]= %4i (tl_tab value=%5i)\n", i, sin_tab[i],tl_tab[sin_tab[i]]);*/
2155 }
2156
2157 /*logerror("FM.C: ENV_QUIET= %08x\n",ENV_QUIET );*/
2158
2159
2160 /* build LFO PM modulation table */
2161 for(i = 0; i < 8; i++) /* 8 PM depths */
2162 {
2163 UINT8 fnum;
2164 for (fnum=0; fnum<128; fnum++) /* 7 bits meaningful of F-NUMBER */
2165 {
2166 UINT8 value;
2167 UINT8 step;
2168 UINT32 offset_depth = i;
2169 UINT32 offset_fnum_bit;
2170 UINT32 bit_tmp;
2171
2172 for (step=0; step<8; step++)
2173 {
2174 value = 0;
2175 for (bit_tmp=0; bit_tmp<7; bit_tmp++) /* 7 bits */
2176 {
2177 if (fnum & (1<<bit_tmp)) /* only if bit "bit_tmp" is set */
2178 {
2179 offset_fnum_bit = bit_tmp * 8;
2180 value += lfo_pm_output[offset_fnum_bit + offset_depth][step];
2181 }
2182 }
2183 /* 32 steps for LFO PM (sinus) */
2184 lfo_pm_table[(fnum*32*8) + (i*32) + step + 0] = value;
2185 lfo_pm_table[(fnum*32*8) + (i*32) +(step^7)+ 8] = value;
2186 lfo_pm_table[(fnum*32*8) + (i*32) + step +16] = -value;
2187 lfo_pm_table[(fnum*32*8) + (i*32) +(step^7)+24] = -value;
2188 }
2189 #if 0
2190 logerror("LFO depth=%1x FNUM=%04x (<<4=%4x): ", i, fnum, fnum<<4);
2191 for (step=0; step<16; step++) /* dump only positive part of waveforms */
2192 logerror("%02x ", lfo_pm_table[(fnum*32*8) + (i*32) + step] );
2193 logerror("\n");
2194 #endif
2195
2196 }
2197 }
2198 }
2199
2200 #endif /* BUILD_OPN */
2201
2202
2203
2204 #if BUILD_OPN_PRESCALER
2205 /*
2206 prescaler circuit (best guess to verified chip behaviour)
2207
2208 +--------------+ +-sel2-+
2209 | +--|in20 |
2210 +---+ | +-sel1-+ | |
2211 M-CLK -+-|1/2|-+--|in10 | +---+ | out|--INT_CLOCK
2212 | +---+ | out|-|1/3|-|in21 |
2213 +----------|in11 | +---+ +------+
2214 +------+
2215
2216 reg.2d : sel2 = in21 (select sel2)
2217 reg.2e : sel1 = in11 (select sel1)
2218 reg.2f : sel1 = in10 , sel2 = in20 (clear selector)
2219 reset : sel1 = in11 , sel2 = in21 (clear both)
2220
2221 */
2222 static void OPNPrescaler_w(FM_OPN *OPN , int addr, int pre_divider)
2223 {
2224 static const int opn_pres[4] = { 2*12 , 2*12 , 6*12 , 3*12 };
2225 static const int ssg_pres[4] = { 1 , 1 , 4 , 2 };
2226 int sel;
2227
2228 switch(addr)
2229 {
2230 case 0: /* when reset */
2231 OPN->ST.prescaler_sel = 2;
2232 break;
2233 case 1: /* when postload */
2234 break;
2235 case 0x2d: /* divider sel : select 1/1 for 1/3line */
2236 OPN->ST.prescaler_sel |= 0x02;
2237 break;
2238 case 0x2e: /* divider sel , select 1/3line for output */
2239 OPN->ST.prescaler_sel |= 0x01;
2240 break;
2241 case 0x2f: /* divider sel , clear both selector to 1/2,1/2 */
2242 OPN->ST.prescaler_sel = 0;
2243 break;
2244 }
2245 sel = OPN->ST.prescaler_sel & 3;
2246 /* update prescaler */
2247 OPNSetPres( OPN, opn_pres[sel]*pre_divider,
2248 opn_pres[sel]*pre_divider,
2249 ssg_pres[sel]*pre_divider );
2250 }
2251 #endif /* BUILD_OPN_PRESCALER */
2252
2253 static void ssgdummy_set_clock(void* param, UINT32 clock)
2254 {
2255 (void)param;
2256 (void)clock;
2257 return;
2258 }
2259
2260 static void ssgdummy_write(void* param, UINT8 address, UINT8 data)
2261 {
2262 (void)param;
2263 (void)address;
2264 (void)data;
2265 return;
2266 }
2267
2268 static UINT8 ssgdummy_read(void* param)
2269 {
2270 (void)param;
2271 return 0x00;
2272 }
2273
2274 static void ssgdummy_reset(void* param)
2275 {
2276 (void)param;
2277 return;
2278 }
2279
2280 static const ssg_callbacks ssg_dummy_funcs =
2281 {
2282 ssgdummy_set_clock,
2283 ssgdummy_write,
2284 ssgdummy_read,
2285 ssgdummy_reset
2286 };
2287
2288 static void OPNLinkSSG(FM_OPN *OPN, const ssg_callbacks *ssg_cb, void *ssg_param)
2289 {
2290 if (ssg_cb == NULL)
2291 {
2292 OPN->ST.SSG_funcs = ssg_dummy_funcs;
2293 OPN->ST.SSG_param = NULL;
2294 }
2295 else
2296 {
2297 OPN->ST.SSG_funcs = *ssg_cb;
2298 OPN->ST.SSG_param = ssg_param;
2299 }
2300 return;
2301 }
2302
2303 static void OPNCheckNativeSampleRate(FM_OPN *OPN)
2304 {
2305 UINT32 pres;
2306 UINT32 nativeSRate;
2307 int sRateDiff;
2308
2309 pres = 6*12; // default prescaler
2310 if (OPN->type & TYPE_6CH)
2311 pres *= 2;
2312 nativeSRate = OPN->ST.clock / pres;
2313 sRateDiff = (int)OPN->ST.rate - (int)nativeSRate;
2314 OPN->smpRateNative = (abs(sRateDiff) <= 2);
2315
2316 return;
2317 }
2318
2319 static void OPNSetSmplRateChgCallback(FM_OPN *OPN, DEVCB_SRATE_CHG cbFunc, void* dataPtr)
2320 {
2321 // set Sample Rate Change Callback routine
2322 OPN->smpRateFunc = cbFunc;
2323 OPN->smpRateData = dataPtr;
2324 return;
2325 }
2326
2327
2328 #if BUILD_YM2203
2329 /*****************************************************************************/
2330 /* YM2203 local section */
2331 /*****************************************************************************/
2332
2333 /* here's the virtual YM2203(OPN) */
2334 typedef struct
2335 {
2336 DEV_DATA _devData;
2337
2338 UINT8 REGS[256]; /* registers */
2339 FM_OPN OPN; /* OPN state */
2340 FM_CH CH[3]; /* channel state */
2341 } YM2203;
2342
2343 /* Generate samples for one of the YM2203s */
2344 void ym2203_update_one(void *chip, UINT32 length, DEV_SMPL **buffer)
2345 {
2346 YM2203 *F2203 = (YM2203 *)chip;
2347 FM_OPN *OPN = &F2203->OPN;
2348 UINT32 i;
2349 DEV_SMPL *bufL,*bufR;
2350 FM_CH *cch[3];
2351
2352 if (buffer != NULL)
2353 {
2354 bufL = buffer[0];
2355 bufR = buffer[1];
2356 }
2357 else
2358 {
2359 bufL = bufR = NULL;
2360 }
2361 cch[0] = &F2203->CH[0];
2362 cch[1] = &F2203->CH[1];
2363 cch[2] = &F2203->CH[2];
2364
2365
2366 /* refresh PG and EG */
2367 refresh_fc_eg_chan( OPN, cch[0] );
2368 refresh_fc_eg_chan( OPN, cch[1] );
2369 if( (OPN->ST.mode & 0xc0) )
2370 {
2371 /* 3SLOT MODE */
2372 if( cch[2]->SLOT[SLOT1].Incr==-1)
2373 {
2374 refresh_fc_eg_slot(OPN, &cch[2]->SLOT[SLOT1] , OPN->SL3.fc[1] , OPN->SL3.kcode[1] );
2375 refresh_fc_eg_slot(OPN, &cch[2]->SLOT[SLOT2] , OPN->SL3.fc[2] , OPN->SL3.kcode[2] );
2376 refresh_fc_eg_slot(OPN, &cch[2]->SLOT[SLOT3] , OPN->SL3.fc[0] , OPN->SL3.kcode[0] );
2377 refresh_fc_eg_slot(OPN, &cch[2]->SLOT[SLOT4] , cch[2]->fc , cch[2]->kcode );
2378 }
2379 }
2380 else
2381 refresh_fc_eg_chan( OPN, cch[2] );
2382 if (! length)
2383 {
2384 update_ssg_eg_channel(&cch[0]->SLOT[SLOT1]);
2385 update_ssg_eg_channel(&cch[1]->SLOT[SLOT1]);
2386 update_ssg_eg_channel(&cch[2]->SLOT[SLOT1]);
2387 }
2388
2389
2390 /* YM2203 doesn't have LFO so we must keep these globals at 0 level */
2391 OPN->LFO_AM = 0;
2392 OPN->LFO_PM = 0;
2393
2394 /* buffering */
2395 for (i=0; i < length ; i++)
2396 {
2397 /* clear outputs */
2398 OPN->out_fm[0] = 0;
2399 OPN->out_fm[1] = 0;
2400 OPN->out_fm[2] = 0;
2401
2402 /* update SSG-EG output */
2403 update_ssg_eg_channel(&cch[0]->SLOT[SLOT1]);
2404 update_ssg_eg_channel(&cch[1]->SLOT[SLOT1]);
2405 update_ssg_eg_channel(&cch[2]->SLOT[SLOT1]);
2406
2407 /* calculate FM */
2408 chan_calc(OPN, cch[0], 0 );
2409 chan_calc(OPN, cch[1], 1 );
2410 chan_calc(OPN, cch[2], 2 );
2411
2412 /* advance envelope generator */
2413 OPN->eg_timer += OPN->eg_timer_add;
2414 while (OPN->eg_timer >= OPN->eg_timer_overflow)
2415 {
2416 OPN->eg_timer -= OPN->eg_timer_overflow;
2417 OPN->eg_cnt++;
2418
2419 advance_eg_channel(OPN, &cch[0]->SLOT[SLOT1]);
2420 advance_eg_channel(OPN, &cch[1]->SLOT[SLOT1]);
2421 advance_eg_channel(OPN, &cch[2]->SLOT[SLOT1]);
2422 }
2423
2424 /* buffering */
2425 {
2426 DEV_SMPL lt;
2427
2428 lt = OPN->out_fm[0] + OPN->out_fm[1] + OPN->out_fm[2];
2429
2430 /* buffering */
2431 bufL[i] = lt;
2432 bufR[i] = lt;
2433 }
2434
2435 /* CSM mode: if CSM Key ON has occured, CSM Key OFF need to be sent */
2436 /* only if Timer A does not overflow again (i.e CSM Key ON not set again) */
2437 OPN->SL3.key_csm <<= 1;
2438
2439 /* timer A control */
2440 INTERNAL_TIMER_A( &OPN->ST , cch[2] )
2441
2442 /* CSM Mode Key ON still disabled */
2443 if (OPN->SL3.key_csm & 2)
2444 {
2445 /* CSM Mode Key OFF (verified by Nemesis on real hardware) */
2446 FM_KEYOFF_CSM(cch[2],SLOT1);
2447 FM_KEYOFF_CSM(cch[2],SLOT2);
2448 FM_KEYOFF_CSM(cch[2],SLOT3);
2449 FM_KEYOFF_CSM(cch[2],SLOT4);
2450 OPN->SL3.key_csm = 0;
2451 }
2452 }
2453
2454 /* timer B control */
2455 INTERNAL_TIMER_B(&OPN->ST,length)
2456 }
2457
2458 static void ym2203_update_req(void *param)
2459 {
2460 ym2203_update_one(param, 0, NULL);
2461 }
2462
2463 /* ---------- reset one of chip ---------- */
2464 void ym2203_reset_chip(void *chip)
2465 {
2466 int i;
2467 YM2203 *F2203 = (YM2203 *)chip;
2468 FM_OPN *OPN = &F2203->OPN;
2469
2470 /* Reset Prescaler */
2471 OPNPrescaler_w(OPN, 0 , 1 );
2472 /* reset SSG section */
2473 OPN->ST.SSG_funcs.reset(OPN->ST.SSG_param);
2474 /* status clear */
2475 FM_IRQMASK_SET(&OPN->ST,0x03);
2476 FM_BUSY_CLEAR(&OPN->ST);
2477
2478 OPN->eg_timer = 0;
2479 OPN->eg_cnt = 0;
2480
2481 OPN->ST.TAC = 0;
2482 OPN->ST.TBC = 0;
2483
2484 OPN->SL3.key_csm = 0;
2485
2486 OPN->ST.status = 0;
2487 OPN->ST.mode = 0;
2488 OPN->ST.irq = 0;
2489
2490 memset(F2203->REGS, 0x00, sizeof(UINT8) * 256);
2491
2492 reset_channels( &OPN->ST , F2203->CH , 3 );
2493 /* reset Operator paramater */
2494 for(i = 0xb2 ; i >= 0x30 ; i-- ) OPNWriteReg(OPN,i,0);
2495 OPNWriteMode(OPN,0x27,0x30); /* mode 0 , timer reset */
2496 for(i = 0x26 ; i >= 0x20 ; i-- ) OPNWriteReg(OPN,i,0);
2497 }
2498
2499 /* ---------- Initialize YM2203 emulator(s) ----------
2500 'num' is the number of virtual YM2203s to allocate
2501 'clock' is the chip clock in Hz
2502 'rate' is sampling rate
2503 */
2504 void * ym2203_init(void *param, UINT32 clock, UINT32 rate,
2505 FM_TIMERHANDLER timer_handler,FM_IRQHANDLER IRQHandler)
2506 {
2507 YM2203 *F2203;
2508
2509 /* allocate ym2203 state space */
2510 F2203 = (YM2203 *)calloc(1,sizeof(YM2203));
2511 if( F2203==NULL)
2512 return NULL;
2513
2514 /* allocate total level table (128kb space) */
2515 init_tables();
2516
2517 F2203->OPN.ST.param = param;
2518 F2203->OPN.type = TYPE_YM2203;
2519 F2203->OPN.P_CH = F2203->CH;
2520 F2203->OPN.ST.clock = clock;
2521 F2203->OPN.ST.rate = rate;
2522 OPNCheckNativeSampleRate(&F2203->OPN);
2523
2524 F2203->OPN.ST.timer_handler = timer_handler;
2525 F2203->OPN.ST.IRQ_Handler = IRQHandler;
2526 OPNLinkSSG(&F2203->OPN, NULL, NULL);
2527 OPNSetSmplRateChgCallback(&F2203->OPN, NULL, NULL);
2528
2529 ym2203_set_mutemask(F2203, 0x00);
2530
2531 return F2203;
2532 }
2533
2534 /* link SSG emulator */
2535 void ym2203_link_ssg(void *chip, const ssg_callbacks *ssg, void *ssg_param)
2536 {
2537 YM2203 *F2203 = (YM2203 *)chip;
2538
2539 OPNLinkSSG(&F2203->OPN, ssg, ssg_param);
2540 OPNPrescaler_w(&F2203->OPN, 1, 1);
2541 return;
2542 }
2543
2544 /* set sample rate change callback */
2545 void ym2203_set_srchg_cb(void *chip, DEVCB_SRATE_CHG cbFunc, void* dataPtr)
2546 {
2547 YM2203 *F2203 = (YM2203 *)chip;
2548 OPNSetSmplRateChgCallback(&F2203->OPN, cbFunc, dataPtr);
2549 return;
2550 }
2551
2552 /* shut down emulator */
2553 void ym2203_shutdown(void *chip)
2554 {
2555 YM2203 *F2203 = (YM2203 *)chip;
2556
2557 free(F2203);
2558 }
2559
2560 /* YM2203 I/O interface */
2561 void ym2203_write(void *chip,UINT8 a,UINT8 v)
2562 {
2563 YM2203 *F2203 = (YM2203 *)chip;
2564 FM_OPN *OPN = &F2203->OPN;
2565
2566 if (a>=2)return;
2567 if( !(a&1) )
2568 { /* address port */
2569 OPN->ST.address = v;
2570
2571 /* Write register to SSG emulator */
2572 if( v < 16 ) OPN->ST.SSG_funcs.write(OPN->ST.SSG_param,a,v);
2573
2574 /* prescaler select : 2d,2e,2f */
2575 if( v >= 0x2d && v <= 0x2f )
2576 OPNPrescaler_w(OPN , v , 1);
2577 }
2578 else
2579 { /* data port */
2580 UINT8 addr = OPN->ST.address;
2581 F2203->REGS[addr] = v;
2582 switch( addr & 0xf0 )
2583 {
2584 case 0x00: /* 0x00-0x0f : SSG section */
2585 /* Write data to SSG emulator */
2586 OPN->ST.SSG_funcs.write(OPN->ST.SSG_param,a,v);
2587 break;
2588 case 0x20: /* 0x20-0x2f : Mode section */
2589 ym2203_update_req(F2203);
2590 /* write register */
2591 OPNWriteMode(OPN,addr,v);
2592 break;
2593 default: /* 0x30-0xff : OPN section */
2594 ym2203_update_req(F2203);
2595 /* write register */
2596 OPNWriteReg(OPN,addr,v);
2597 }
2598 FM_BUSY_SET(&OPN->ST,1);
2599 }
2600 //return OPN->ST.irq;
2601 return;
2602 }
2603
2604 UINT8 ym2203_read(void *chip,UINT8 a)
2605 {
2606 YM2203 *F2203 = (YM2203 *)chip;
2607 UINT8 addr = F2203->OPN.ST.address;
2608 UINT8 ret = 0;
2609
2610 if( !(a&1) )
2611 { /* status port */
2612 ret = FM_STATUS_FLAG(&F2203->OPN.ST);
2613 }
2614 else
2615 { /* data port (only SSG) */
2616 if( addr < 16 ) ret = F2203->OPN.ST.SSG_funcs.read(F2203->OPN.ST.SSG_param, 0);
2617 }
2618 return ret;
2619 }
2620
2621 UINT8 ym2203_timer_over(void *chip,UINT8 c)
2622 {
2623 YM2203 *F2203 = (YM2203 *)chip;
2624
2625 if( c )
2626 { /* Timer B */
2627 TimerBOver( &(F2203->OPN.ST) );
2628 }
2629 else
2630 { /* Timer A */
2631 ym2203_update_req(F2203);
2632 /* timer update */
2633 TimerAOver( &(F2203->OPN.ST) );
2634 /* CSM mode key,TL controll */
2635 if ((F2203->OPN.ST.mode & 0xc0) == 0x80)
2636 { /* CSM mode total level latch and auto key on */
2637 CSMKeyControll( &F2203->OPN, &(F2203->CH[2]) );
2638 }
2639 }
2640 return F2203->OPN.ST.irq;
2641 }
2642
2643 void ym2203_set_mutemask(void *chip, UINT32 MuteMask)
2644 {
2645 YM2203 *F2203 = (YM2203 *)chip;
2646 UINT8 CurChn;
2647
2648 for (CurChn = 0; CurChn < 3; CurChn ++)
2649 F2203->CH[CurChn].Muted = (MuteMask >> CurChn) & 0x01;
2650
2651 return;
2652 }
2653 #endif /* BUILD_YM2203 */
2654
2655
2656
2657 #if (BUILD_YM2608||BUILD_YM2610||BUILD_YM2610B)
2658
2659 /* ADPCM type A channel struct */
2660 typedef struct
2661 {
2662 UINT8 flag; /* port state */
2663 UINT8 flagMask; /* arrived flag mask */
2664 UINT8 now_data; /* current ROM data */
2665 UINT32 now_addr; /* current ROM address */
2666 UINT32 now_step;
2667 UINT32 step;
2668 UINT32 start; /* sample data start address*/
2669 UINT32 end; /* sample data end address */
2670 UINT8 IL; /* Instrument Level */
2671 INT32 adpcm_acc; /* accumulator */
2672 INT32 adpcm_step; /* step */
2673 INT32 adpcm_out; /* (speedup) hiro-shi!! */
2674 INT8 vol_mul; /* volume in "0.75dB" steps */
2675 UINT8 vol_shift; /* volume in "-6dB" steps */
2676 INT32 *pan; /* &out_adpcm[OPN_xxxx] */
2677 UINT8 Muted;
2678 } ADPCM_CH;
2679
2680 /* here's the virtual YM2610 */
2681 typedef struct
2682 {
2683 // DEV_DATA _devData;
2684
2685 UINT8 REGS[512]; /* registers */
2686 FM_OPN OPN; /* OPN state */
2687 FM_CH CH[6]; /* channel state */
2688 UINT8 addr_A1; /* address line A1 */
2689
2690 /* ADPCM-A unit */
2691 UINT8 *pcmbuf; /* pcm rom buffer */
2692 UINT32 pcm_size; /* size of pcm rom */
2693 UINT8 adpcmTL; /* adpcmA total level */
2694 ADPCM_CH adpcm[6]; /* adpcm channels */
2695 UINT32 adpcmreg[0x30]; /* registers */
2696 UINT8 adpcm_arrivedEndAddress;
2697 YM_DELTAT deltaT; /* Delta-T ADPCM unit */
2698 UINT8 MuteDeltaT;
2699
2700 UINT8 flagmask; /* YM2608 only */
2701 UINT8 irqmask; /* YM2608 only */
2702 } YM2610;
2703
2704 /* here is the virtual YM2608 */
2705 typedef YM2610 YM2608;
2706
2707
2708 /**** YM2610 ADPCM defines ****/
2709 #define ADPCM_SHIFT (16) /* frequency step rate */
2710 #define ADPCMA_ADDRESS_SHIFT 8 /* adpcm A address shift */
2711
2712 /* Algorithm and tables verified on real YM2608 and YM2610 */
2713
2714 /* usual ADPCM table (16 * 1.1^N) */
2715 static const int steps[49] =
2716 {
2717 16, 17, 19, 21, 23, 25, 28,
2718 31, 34, 37, 41, 45, 50, 55,
2719 60, 66, 73, 80, 88, 97, 107,
2720 118, 130, 143, 157, 173, 190, 209,
2721 230, 253, 279, 307, 337, 371, 408,
2722 449, 494, 544, 598, 658, 724, 796,
2723 876, 963, 1060, 1166, 1282, 1411, 1552
2724 };
2725
2726 /* different from the usual ADPCM table */
2727 static const int step_inc[8] = { -1*16, -1*16, -1*16, -1*16, 2*16, 5*16, 7*16, 9*16 };
2728
2729 /* speedup purposes only */
2730 static int jedi_table[ 49*16 ];
2731
2732
2733 static void Init_ADPCMATable(void)
2734 {
2735 int step, nib;
2736
2737 for (step = 0; step < 49; step++)
2738 {
2739 /* loop over all nibbles and compute the difference */
2740 for (nib = 0; nib < 16; nib++)
2741 {
2742 int value = (2*(nib & 0x07) + 1) * steps[step] / 8;
2743 jedi_table[step*16 + nib] = (nib&0x08) ? -value : value;
2744 }
2745 }
2746 }
2747
2748 /* ADPCM A (Non control type) : calculate one channel output */
2749 INLINE void ADPCMA_calc_chan( YM2610 *F2610, ADPCM_CH *ch )
2750 {
2751 UINT32 step;
2752 UINT8 data;
2753
2754 if (ch->Muted)
2755 return;
2756
2757
2758 ch->now_step += ch->step;
2759 if ( ch->now_step >= (1<<ADPCM_SHIFT) )
2760 {
2761 step = ch->now_step >> ADPCM_SHIFT;
2762 ch->now_step &= (1<<ADPCM_SHIFT)-1;
2763 do{
2764 /* end check */
2765 /* 11-06-2001 JB: corrected comparison. Was > instead of == */
2766 /* YM2610 checks lower 20 bits only, the 4 MSB bits are sample bank */
2767 /* Here we use 1<<21 to compensate for nibble calculations */
2768
2769 if ( (ch->now_addr & ((1<<21)-1)) == ((ch->end<<1) & ((1<<21)-1)) )
2770 {
2771 ch->flag = 0;
2772 F2610->adpcm_arrivedEndAddress |= ch->flagMask;
2773 return;
2774 }
2775 #if 0
2776 if ( ch->now_addr > (F2610->pcmsizeA<<1) )
2777 {
2778 LOG(LOG_WAR,("YM2610: Attempting to play past adpcm rom size!\n" ));
2779 return;
2780 }
2781 #endif
2782 if ( ch->now_addr&1 )
2783 data = ch->now_data & 0x0f;
2784 else
2785 {
2786 ch->now_data = *(F2610->pcmbuf+(ch->now_addr>>1));
2787 data = (ch->now_data >> 4) & 0x0f;
2788 }
2789
2790 ch->now_addr++;
2791
2792 ch->adpcm_acc += jedi_table[ch->adpcm_step + data];
2793
2794 /* the 12-bit accumulator wraps on the ym2610 and ym2608 (like the msm5205), it does not saturate (like the msm5218) */
2795 ch->adpcm_acc &= 0xfff;
2796
2797 /* extend 12-bit signed int */
2798 if (ch->adpcm_acc & 0x800)
2799 ch->adpcm_acc |= ~0xfff;
2800
2801 ch->adpcm_step += step_inc[data & 7];
2802 Limit( ch->adpcm_step, 48*16, 0*16 );
2803
2804 }while(--step);
2805
2806 /* calc pcm * volume data */
2807 ch->adpcm_out = ((ch->adpcm_acc * ch->vol_mul) >> ch->vol_shift) & ~3; /* multiply, shift and mask out 2 LSB bits */
2808 }
2809
2810 /* output for work of output channels (out_adpcm[OPNxxxx])*/
2811 *(ch->pan) += ch->adpcm_out;
2812 }
2813
2814 /* ADPCM type A Write */
2815 static void FM_ADPCMAWrite(YM2610 *F2610,int r,int v)
2816 {
2817 ADPCM_CH *adpcm = F2610->adpcm;
2818 UINT8 c = r&0x07;
2819
2820 F2610->adpcmreg[r] = v&0xff; /* stock data */
2821 switch( r )
2822 {
2823 case 0x00: /* DM,--,C5,C4,C3,C2,C1,C0 */
2824 if( !(v&0x80) )
2825 {
2826 /* KEY ON */
2827 for( c = 0; c < 6; c++ )
2828 {
2829 if( (v>>c)&1 )
2830 {
2831 /**** start adpcm ****/
2832 // The .step variable is already set and for the YM2608 it is different on channels 4 and 5.
2833 //adpcm[c].step = (UINT32)((float)(1<<ADPCM_SHIFT)*((float)F2610->OPN.ST.freqbase)/3.0);
2834 adpcm[c].now_addr = adpcm[c].start<<1;
2835 adpcm[c].now_step = 0;
2836 adpcm[c].adpcm_acc = 0;
2837 adpcm[c].adpcm_step= 0;
2838 adpcm[c].adpcm_out = 0;
2839 adpcm[c].flag = 1;
2840
2841 if(F2610->pcmbuf==NULL)
2842 { /* Check ROM Mapped */
2843 //logerror("YM2608-YM2610: ADPCM-A rom not mapped\n");
2844 adpcm[c].flag = 0;
2845 }
2846 else
2847 {
2848 if(adpcm[c].end >= F2610->pcm_size)
2849 { /* Check End in Range */
2850 //logerror("YM2610: ADPCM-A end out of range: $%08x\n",adpcm[c].end);
2851 /*adpcm[c].end = F2610->pcm_size-1;*/ /* JB: DO NOT uncomment this, otherwise you will break the comparison in the ADPCM_CALC_CHA() */
2852 }
2853 if(adpcm[c].start >= F2610->pcm_size) /* Check Start in Range */
2854 {
2855 //logerror("YM2608-YM2610: ADPCM-A start out of range: $%08x\n",adpcm[c].start);
2856 adpcm[c].flag = 0;
2857 }
2858 }
2859 }
2860 }
2861 }
2862 else
2863 {
2864 /* KEY OFF */
2865 for( c = 0; c < 6; c++ )
2866 if( (v>>c)&1 )
2867 adpcm[c].flag = 0;
2868 }
2869 break;
2870 case 0x01: /* B0-5 = TL */
2871 F2610->adpcmTL = (v & 0x3f) ^ 0x3f;
2872 for( c = 0; c < 6; c++ )
2873 {
2874 int volume = F2610->adpcmTL + adpcm[c].IL;
2875
2876 if ( volume >= 63 ) /* This is correct, 63 = quiet */
2877 {
2878 adpcm[c].vol_mul = 0;
2879 adpcm[c].vol_shift = 0;
2880 }
2881 else
2882 {
2883 adpcm[c].vol_mul = 15 - (volume & 7); /* so called 0.75 dB */
2884 adpcm[c].vol_shift = (UINT8)(1 + (volume >> 3)); /* Yamaha engineers used the approximation: each -6 dB is close to divide by two (shift right) */
2885 }
2886
2887 /* calc pcm * volume data */
2888 adpcm[c].adpcm_out = ((adpcm[c].adpcm_acc * adpcm[c].vol_mul) >> adpcm[c].vol_shift) & ~3; /* multiply, shift and mask out low 2 bits */
2889 }
2890 break;
2891 default:
2892 c = r&0x07;
2893 if( c >= 0x06 ) return;
2894 switch( r&0x38 )
2895 {
2896 case 0x08: /* B7=L,B6=R, B4-0=IL */
2897 {
2898 int volume;
2899
2900 adpcm[c].IL = (v & 0x1f) ^ 0x1f;
2901
2902 volume = F2610->adpcmTL + adpcm[c].IL;
2903
2904 if ( volume >= 63 ) /* This is correct, 63 = quiet */
2905 {
2906 adpcm[c].vol_mul = 0;
2907 adpcm[c].vol_shift = 0;
2908 }
2909 else
2910 {
2911 adpcm[c].vol_mul = 15 - (volume & 7); /* so called 0.75 dB */
2912 adpcm[c].vol_shift = (UINT8)(1 + (volume >> 3)); /* Yamaha engineers used the approximation: each -6 dB is close to divide by two (shift right) */
2913 }
2914
2915 adpcm[c].pan = &F2610->OPN.out_adpcm[(v>>6)&0x03];
2916
2917 /* calc pcm * volume data */
2918 adpcm[c].adpcm_out = ((adpcm[c].adpcm_acc * adpcm[c].vol_mul) >> adpcm[c].vol_shift) & ~3; /* multiply, shift and mask out low 2 bits */
2919 }
2920 break;
2921 case 0x10:
2922 case 0x18:
2923 adpcm[c].start = ( (F2610->adpcmreg[0x18 + c]*0x0100 | F2610->adpcmreg[0x10 + c]) << ADPCMA_ADDRESS_SHIFT);
2924 break;
2925 case 0x20:
2926 case 0x28:
2927 adpcm[c].end = ( (F2610->adpcmreg[0x28 + c]*0x0100 | F2610->adpcmreg[0x20 + c]) << ADPCMA_ADDRESS_SHIFT);
2928 adpcm[c].end += (1<<ADPCMA_ADDRESS_SHIFT) - 1;
2929 break;
2930 }
2931 }
2932 }
2933
2934 #endif /* (BUILD_YM2608||BUILD_YM2610||BUILD_YM2610B) */
2935
2936
2937 #if BUILD_YM2608
2938 /*****************************************************************************/
2939 /* YM2608 local section */
2940 /*****************************************************************************/
2941
2942
2943
2944 const unsigned int YM2608_ADPCM_ROM_addr[2*6] = {
2945 0x0000, 0x01bf, /* bass drum */
2946 0x01c0, 0x043f, /* snare drum */
2947 0x0440, 0x1b7f, /* top cymbal */
2948 0x1b80, 0x1cff, /* high hat */
2949 0x1d00, 0x1f7f, /* tom tom */
2950 0x1f80, 0x1fff /* rim shot */
2951 };
2952
2953
2954 #include "fmopn_2608rom.h"
2955
2956
2957
2958 /* flag enable control 0x110 */
2959 INLINE void YM2608IRQFlagWrite(FM_OPN *OPN, YM2608 *F2608, int v)
2960 {
2961 if( v & 0x80 )
2962 { /* Reset IRQ flag */
2963 FM_STATUS_RESET(&OPN->ST, 0xf7); /* don't touch BUFRDY flag otherwise we'd have to call ymdeltat module to set the flag back */
2964 }
2965 else
2966 { /* Set status flag mask */
2967 F2608->flagmask = (~(v&0x1f));
2968 FM_IRQMASK_SET(&OPN->ST, (F2608->irqmask & F2608->flagmask) );
2969 }
2970 }
2971
2972 /* compatible mode & IRQ enable control 0x29 */
2973 INLINE void YM2608IRQMaskWrite(FM_OPN *OPN, YM2608 *F2608, int v)
2974 {
2975 /* SCH,xx,xxx,EN_ZERO,EN_BRDY,EN_EOS,EN_TB,EN_TA */
2976
2977 /* extend 3ch. enable/disable */
2978 if(v&0x80)
2979 OPN->type |= TYPE_6CH; /* OPNA mode - 6 FM channels */
2980 else
2981 OPN->type &= ~TYPE_6CH; /* OPN mode - 3 FM channels */
2982
2983 /* IRQ MASK store and set */
2984 F2608->irqmask = v&0x1f;
2985 FM_IRQMASK_SET(&OPN->ST, (F2608->irqmask & F2608->flagmask) );
2986 }
2987
2988 /* Generate samples for one of the YM2608s */
2989 void ym2608_update_one(void *chip, UINT32 length, stream_sample_t **buffer)
2990 {
2991 YM2608 *F2608 = (YM2608 *)chip;
2992 FM_OPN *OPN = &F2608->OPN;
2993 YM_DELTAT *DELTAT = &F2608->deltaT;
2994 UINT32 i;
2995 UINT8 j;
2996 stream_sample_t *bufL,*bufR;
2997 FM_CH *cch[6];
2998 INT32 *out_fm = OPN->out_fm;
2999
3000 /* set bufer */
3001 if (buffer != NULL)
3002 {
3003 bufL = buffer[0];
3004 bufR = buffer[1];
3005 }
3006 else
3007 {
3008 bufL = bufR = NULL;
3009 }
3010
3011 cch[0] = &F2608->CH[0];
3012 cch[1] = &F2608->CH[1];
3013 cch[2] = &F2608->CH[2];
3014 cch[3] = &F2608->CH[3];
3015 cch[4] = &F2608->CH[4];
3016 cch[5] = &F2608->CH[5];
3017
3018 /* refresh PG and EG */
3019 refresh_fc_eg_chan( OPN, cch[0] );
3020 refresh_fc_eg_chan( OPN, cch[1] );
3021 if( (OPN->ST.mode & 0xc0) )
3022 {
3023 /* 3SLOT MODE */
3024 if( cch[2]->SLOT[SLOT1].Incr==-1)
3025 {
3026 refresh_fc_eg_slot(OPN, &cch[2]->SLOT[SLOT1] , OPN->SL3.fc[1] , OPN->SL3.kcode[1] );
3027 refresh_fc_eg_slot(OPN, &cch[2]->SLOT[SLOT2] , OPN->SL3.fc[2] , OPN->SL3.kcode[2] );
3028 refresh_fc_eg_slot(OPN, &cch[2]->SLOT[SLOT3] , OPN->SL3.fc[0] , OPN->SL3.kcode[0] );
3029 refresh_fc_eg_slot(OPN, &cch[2]->SLOT[SLOT4] , cch[2]->fc , cch[2]->kcode );
3030 }
3031 }
3032 else
3033 refresh_fc_eg_chan( OPN, cch[2] );
3034 refresh_fc_eg_chan( OPN, cch[3] );
3035 refresh_fc_eg_chan( OPN, cch[4] );
3036 refresh_fc_eg_chan( OPN, cch[5] );
3037 if (! length)
3038 {
3039 update_ssg_eg_channel(&cch[0]->SLOT[SLOT1]);
3040 update_ssg_eg_channel(&cch[1]->SLOT[SLOT1]);
3041 update_ssg_eg_channel(&cch[2]->SLOT[SLOT1]);
3042 update_ssg_eg_channel(&cch[3]->SLOT[SLOT1]);
3043 update_ssg_eg_channel(&cch[4]->SLOT[SLOT1]);
3044 update_ssg_eg_channel(&cch[5]->SLOT[SLOT1]);
3045 }
3046
3047
3048 /* buffering */
3049 for(i=0; i < length ; i++)
3050 {
3051 /* clear output acc. */
3052 OPN->out_adpcm[OUTD_LEFT] = OPN->out_adpcm[OUTD_RIGHT] = OPN->out_adpcm[OUTD_CENTER] = 0;
3053 OPN->out_delta[OUTD_LEFT] = OPN->out_delta[OUTD_RIGHT] = OPN->out_delta[OUTD_CENTER] = 0;
3054 /* clear outputs */
3055 out_fm[0] = 0;
3056 out_fm[1] = 0;
3057 out_fm[2] = 0;
3058 out_fm[3] = 0;
3059 out_fm[4] = 0;
3060 out_fm[5] = 0;
3061
3062 /* update SSG-EG output */
3063 update_ssg_eg_channel(&cch[0]->SLOT[SLOT1]);
3064 update_ssg_eg_channel(&cch[1]->SLOT[SLOT1]);
3065 update_ssg_eg_channel(&cch[2]->SLOT[SLOT1]);
3066 update_ssg_eg_channel(&cch[3]->SLOT[SLOT1]);
3067 update_ssg_eg_channel(&cch[4]->SLOT[SLOT1]);
3068 update_ssg_eg_channel(&cch[5]->SLOT[SLOT1]);
3069
3070 /* calculate FM */
3071 chan_calc(OPN, cch[0], 0 );
3072 chan_calc(OPN, cch[1], 1 );
3073 chan_calc(OPN, cch[2], 2 );
3074 chan_calc(OPN, cch[3], 3 );
3075 chan_calc(OPN, cch[4], 4 );
3076 chan_calc(OPN, cch[5], 5 );
3077
3078 /* deltaT ADPCM */
3079 if( DELTAT->portstate&0x80 && ! F2608->MuteDeltaT )
3080 YM_DELTAT_ADPCM_CALC(DELTAT);
3081
3082 /* ADPCMA */
3083 for( j = 0; j < 6; j++ )
3084 {
3085 if( F2608->adpcm[j].flag )
3086 ADPCMA_calc_chan( F2608, &F2608->adpcm[j]);
3087 }
3088
3089 /* advance LFO */
3090 advance_lfo(OPN);
3091
3092 /* advance envelope generator */
3093 OPN->eg_timer += OPN->eg_timer_add;
3094 while (OPN->eg_timer >= OPN->eg_timer_overflow)
3095 {
3096 OPN->eg_timer -= OPN->eg_timer_overflow;
3097 OPN->eg_cnt++;
3098
3099 advance_eg_channel(OPN, &cch[0]->SLOT[SLOT1]);
3100 advance_eg_channel(OPN, &cch[1]->SLOT[SLOT1]);
3101 advance_eg_channel(OPN, &cch[2]->SLOT[SLOT1]);
3102 advance_eg_channel(OPN, &cch[3]->SLOT[SLOT1]);
3103 advance_eg_channel(OPN, &cch[4]->SLOT[SLOT1]);
3104 advance_eg_channel(OPN, &cch[5]->SLOT[SLOT1]);
3105 }
3106
3107 /* buffering */
3108 {
3109 stream_sample_t lt,rt;
3110
3111 lt = (OPN->out_adpcm[OUTD_LEFT] + OPN->out_adpcm[OUTD_CENTER])<<1;
3112 rt = (OPN->out_adpcm[OUTD_RIGHT] + OPN->out_adpcm[OUTD_CENTER])<<1;
3113 lt += (OPN->out_delta[OUTD_LEFT] + OPN->out_delta[OUTD_CENTER])>>8;
3114 rt += (OPN->out_delta[OUTD_RIGHT] + OPN->out_delta[OUTD_CENTER])>>8;
3115 lt += (out_fm[0] & OPN->pan[0]);
3116 rt += (out_fm[0] & OPN->pan[1]);
3117 lt += (out_fm[1] & OPN->pan[2]);
3118 rt += (out_fm[1] & OPN->pan[3]);
3119 lt += (out_fm[2] & OPN->pan[4]);
3120 rt += (out_fm[2] & OPN->pan[5]);
3121 lt += (out_fm[3] & OPN->pan[6]);
3122 rt += (out_fm[3] & OPN->pan[7]);
3123 lt += (out_fm[4] & OPN->pan[8]);
3124 rt += (out_fm[4] & OPN->pan[9]);
3125 lt += (out_fm[5] & OPN->pan[10]);
3126 rt += (out_fm[5] & OPN->pan[11]);
3127
3128 // I like that emulation can have more precision than the real thing.
3129 //lt >>= 1; // shift right verified on real YM2608
3130 //rt >>= 1;
3131
3132 /* buffering */
3133 bufL[i] = lt;
3134 bufR[i] = rt;
3135 }
3136
3137 /* CSM mode: if CSM Key ON has occured, CSM Key OFF need to be sent */
3138 /* only if Timer A does not overflow again (i.e CSM Key ON not set again) */
3139 OPN->SL3.key_csm <<= 1;
3140
3141 /* timer A control */
3142 INTERNAL_TIMER_A( &OPN->ST , cch[2] )
3143
3144 /* CSM Mode Key ON still disabled */
3145 if (OPN->SL3.key_csm & 2)
3146 {
3147 /* CSM Mode Key OFF (verified by Nemesis on real hardware) */
3148 FM_KEYOFF_CSM(cch[2],SLOT1);
3149 FM_KEYOFF_CSM(cch[2],SLOT2);
3150 FM_KEYOFF_CSM(cch[2],SLOT3);
3151 FM_KEYOFF_CSM(cch[2],SLOT4);
3152 OPN->SL3.key_csm = 0;
3153 }
3154 }
3155
3156 /* timer B control */
3157 INTERNAL_TIMER_B(&OPN->ST,length)
3158
3159 /* check IRQ for DELTA-T EOS */
3160 FM_STATUS_SET(&OPN->ST, 0);
3161 }
3162
3163 static void ym2608_update_req(void *param)
3164 {
3165 ym2608_update_one(param, 0, NULL);
3166 }
3167
3168 static void YM2608_deltat_status_set(void *chip, UINT8 changebits)
3169 {
3170 YM2608 *F2608 = (YM2608 *)chip;
3171 FM_STATUS_SET(&(F2608->OPN.ST), changebits);
3172 }
3173 static void YM2608_deltat_status_reset(void *chip, UINT8 changebits)
3174 {
3175 YM2608 *F2608 = (YM2608 *)chip;
3176 FM_STATUS_RESET(&(F2608->OPN.ST), changebits);
3177 }
3178 /* YM2608(OPNA) */
3179 void * ym2608_init(void *param, UINT32 clock, UINT32 rate,
3180 FM_TIMERHANDLER timer_handler,FM_IRQHANDLER IRQHandler)
3181 {
3182 YM2608 *F2608;
3183
3184 /* allocate extend state space */
3185 F2608 = (YM2608 *)calloc(1,sizeof(YM2608));
3186 if( F2608==NULL)
3187 return NULL;
3188
3189 /* allocate total level table (128kb space) */
3190 init_tables();
3191
3192 F2608->OPN.ST.param = param;
3193 F2608->OPN.type = TYPE_YM2608;
3194 F2608->OPN.P_CH = F2608->CH;
3195 F2608->OPN.ST.clock = clock;
3196 F2608->OPN.ST.rate = rate;
3197 OPNCheckNativeSampleRate(&F2608->OPN);
3198
3199 /* External handlers */
3200 F2608->OPN.ST.timer_handler = timer_handler;
3201 F2608->OPN.ST.IRQ_Handler = IRQHandler;
3202 OPNLinkSSG(&F2608->OPN, NULL, NULL);
3203 OPNSetSmplRateChgCallback(&F2608->OPN, NULL, NULL);
3204
3205 /* DELTA-T */
3206 F2608->deltaT.memory = NULL;
3207 F2608->deltaT.memory_size = 0x00;
3208 F2608->deltaT.memory_mask = 0x00;
3209
3210 //F2608->deltaT.write_time = 20.0 / clock; /* a single byte write takes 20 cycles of main clock */
3211 //F2608->deltaT.read_time = 18.0 / clock; /* a single byte read takes 18 cycles of main clock */
3212
3213 F2608->deltaT.status_set_handler = YM2608_deltat_status_set;
3214 F2608->deltaT.status_reset_handler = YM2608_deltat_status_reset;
3215 F2608->deltaT.status_change_which_chip = F2608;
3216 F2608->deltaT.status_change_EOS_bit = 0x04; /* status flag: set bit2 on End Of Sample */
3217 F2608->deltaT.status_change_BRDY_bit = 0x08; /* status flag: set bit3 on BRDY */
3218 F2608->deltaT.status_change_ZERO_bit = 0x10; /* status flag: set bit4 if silence continues for more than 290 miliseconds while recording the ADPCM */
3219
3220 YM_DELTAT_ADPCM_Init(&F2608->deltaT,YM_DELTAT_EMULATION_MODE_NORMAL,5,F2608->OPN.out_delta,1<<23);
3221
3222 /* ADPCM Rhythm */
3223 F2608->pcmbuf = (UINT8*)YM2608_ADPCM_ROM;
3224 F2608->pcm_size = 0x2000;
3225
3226 Init_ADPCMATable();
3227
3228 ym2608_set_mutemask(F2608, 0x00);
3229
3230 return F2608;
3231 }
3232
3233 /* link SSG emulator */
3234 void ym2608_link_ssg(void *chip, const ssg_callbacks *ssg, void *ssg_param)
3235 {
3236 YM2608 *F2608 = (YM2608 *)chip;
3237
3238 OPNLinkSSG(&F2608->OPN, ssg, ssg_param);
3239 OPNPrescaler_w(&F2608->OPN, 1, 2);
3240 return;
3241 }
3242
3243 /* set sample rate change callback */
3244 /*void ym2608_set_srchg_cb(void *chip, DEVCB_SRATE_CHG cbFunc, void* dataPtr)
3245 {
3246 YM2608 *F2608 = (YM2608 *)chip;
3247 OPNSetSmplRateChgCallback(&F2608->OPN, cbFunc, dataPtr);
3248 return;
3249 }*/
3250
3251 /* shut down emulator */
3252 void ym2608_shutdown(void *chip)
3253 {
3254 YM2608 *F2608 = (YM2608 *)chip;
3255
3256 free(F2608->deltaT.memory); F2608->deltaT.memory = NULL;
3257
3258 free(F2608);
3259 }
3260
3261 /* reset one of chips */
3262 void ym2608_reset_chip(void *chip)
3263 {
3264 int i;
3265 YM2608 *F2608 = (YM2608 *)chip;
3266 FM_OPN *OPN = &F2608->OPN;
3267 YM_DELTAT *DELTAT = &F2608->deltaT;
3268
3269 /* Reset Prescaler */
3270 OPNPrescaler_w(OPN , 0 , 2);
3271 F2608->deltaT.freqbase = OPN->ST.freqbase;
3272 /* reset SSG section */
3273 OPN->ST.SSG_funcs.reset(OPN->ST.SSG_param);
3274
3275 /* status clear */
3276 FM_BUSY_CLEAR(&OPN->ST);
3277
3278 /* register 0x29 - default value after reset is:
3279 enable only 3 FM channels and enable all the status flags */
3280 YM2608IRQMaskWrite(OPN, F2608, 0x1f ); /* default value for D4-D0 is 1 */
3281
3282 /* register 0x10, A1=1 - default value is 1 for D4, D3, D2, 0 for the rest */
3283 YM2608IRQFlagWrite(OPN, F2608, 0x1c ); /* default: enable timer A and B, disable EOS, BRDY and ZERO */
3284
3285 OPN->eg_timer = 0;
3286 OPN->eg_cnt = 0;
3287
3288 OPN->ST.TAC = 0;
3289 OPN->ST.TBC = 0;
3290
3291 OPN->SL3.key_csm = 0;
3292
3293 OPN->ST.status = 0;
3294 OPN->ST.mode = 0;
3295 OPN->ST.irq = 0;
3296
3297 memset(F2608->REGS, 0x00, sizeof(UINT8) * 512);
3298
3299 reset_channels( &OPN->ST , F2608->CH , 6 );
3300 /* reset Operator paramater */
3301 for(i = 0xb6 ; i >= 0xb4 ; i-- )
3302 {
3303 OPNWriteReg(OPN,i ,0xc0);
3304 OPNWriteReg(OPN,i|0x100,0xc0);
3305 }
3306 for(i = 0xb2 ; i >= 0x30 ; i-- )
3307 {
3308 OPNWriteReg(OPN,i ,0);
3309 OPNWriteReg(OPN,i|0x100,0);
3310 }
3311 OPNWriteMode(OPN,0x27,0x30); /* mode 0 , timer reset */
3312 for(i = 0x26 ; i >= 0x20 ; i-- ) OPNWriteReg(OPN,i,0);
3313
3314 /* ADPCM - percussion sounds */
3315 for( i = 0; i < 6; i++ )
3316 {
3317 if (i<=3) /* channels 0,1,2,3 */
3318 F2608->adpcm[i].step = (UINT32)((float)(1<<ADPCM_SHIFT)*((float)F2608->OPN.ST.freqbase)/3.0);
3319 else /* channels 4 and 5 work with slower clock */
3320 F2608->adpcm[i].step = (UINT32)((float)(1<<ADPCM_SHIFT)*((float)F2608->OPN.ST.freqbase)/6.0);
3321
3322 F2608->adpcm[i].start = YM2608_ADPCM_ROM_addr[i*2];
3323 F2608->adpcm[i].end = YM2608_ADPCM_ROM_addr[i*2+1];
3324
3325 F2608->adpcm[i].now_addr = 0;
3326 F2608->adpcm[i].now_step = 0;
3327 // F2608->adpcm[i].delta = 21866;
3328 F2608->adpcm[i].vol_mul = 0;
3329 F2608->adpcm[i].pan = &OPN->out_adpcm[OUTD_CENTER]; /* default center */
3330 F2608->adpcm[i].flagMask = 0;
3331 F2608->adpcm[i].flag = 0;
3332 F2608->adpcm[i].adpcm_acc = 0;
3333 F2608->adpcm[i].adpcm_step= 0;
3334 F2608->adpcm[i].adpcm_out = 0;
3335 }
3336 F2608->adpcmTL = 0x3f;
3337
3338 F2608->adpcm_arrivedEndAddress = 0; /* not used */
3339
3340 /* DELTA-T unit */
3341 DELTAT->freqbase = OPN->ST.freqbase;
3342 YM_DELTAT_ADPCM_Reset(DELTAT,OUTD_CENTER);
3343 }
3344
3345 /* YM2608 write */
3346 /* n = number */
3347 /* a = address */
3348 /* v = value */
3349 void ym2608_write(void *chip, UINT8 a,UINT8 v)
3350 {
3351 YM2608 *F2608 = (YM2608 *)chip;
3352 FM_OPN *OPN = &F2608->OPN;
3353 int addr;
3354
3355 //v &= 0xff; /*adjust to 8 bit bus */
3356
3357
3358 switch(a&3)
3359 {
3360 case 0: /* address port 0 */
3361 OPN->ST.address = v;
3362 F2608->addr_A1 = 0;
3363
3364 /* Write register to SSG emulator */
3365 if( v < 16 ) OPN->ST.SSG_funcs.write(OPN->ST.SSG_param,a,v);
3366 /* prescaler selecter : 2d,2e,2f */
3367 if( v >= 0x2d && v <= 0x2f )
3368 {
3369 OPNPrescaler_w(OPN , v , 2);
3370 //TODO: set ADPCM[c].step
3371 F2608->deltaT.freqbase = OPN->ST.freqbase;
3372 }
3373 break;
3374
3375 case 1: /* data port 0 */
3376 if (F2608->addr_A1 != 0)
3377 break; /* verified on real YM2608 */
3378
3379 addr = OPN->ST.address;
3380 F2608->REGS[addr] = v;
3381 switch(addr & 0xf0)
3382 {
3383 case 0x00: /* SSG section */
3384 /* Write data to SSG emulator */
3385 OPN->ST.SSG_funcs.write(OPN->ST.SSG_param,a,v);
3386 break;
3387 case 0x10: /* 0x10-0x1f : Rhythm section */
3388 ym2608_update_req(F2608);
3389 FM_ADPCMAWrite(F2608,addr-0x10,v);
3390 break;
3391 case 0x20: /* Mode Register */
3392 switch(addr)
3393 {
3394 case 0x29: /* SCH,xx,xxx,EN_ZERO,EN_BRDY,EN_EOS,EN_TB,EN_TA */
3395 YM2608IRQMaskWrite(OPN, F2608, v);
3396 break;
3397 default:
3398 ym2608_update_req(F2608);
3399 OPNWriteMode(OPN,addr,v);
3400 }
3401 break;
3402 default: /* OPN section */
3403 ym2608_update_req(F2608);
3404 OPNWriteReg(OPN,addr,v);
3405 }
3406 break;
3407
3408 case 2: /* address port 1 */
3409 OPN->ST.address = v;
3410 F2608->addr_A1 = 1;
3411 break;
3412
3413 case 3: /* data port 1 */
3414 if (F2608->addr_A1 != 1)
3415 break; /* verified on real YM2608 */
3416
3417 addr = OPN->ST.address;
3418 F2608->REGS[addr | 0x100] = v;
3419 ym2608_update_req(F2608);
3420 switch( addr & 0xf0 )
3421 {
3422 case 0x00: /* DELTAT PORT */
3423 switch( addr )
3424 {
3425 case 0x0e: /* DAC data */
3426 //logerror("YM2608: write to DAC data (unimplemented) value=%02x\n",v);
3427 break;
3428 default:
3429 /* 0x00-0x0d */
3430 YM_DELTAT_ADPCM_Write(&F2608->deltaT,addr,v);
3431 }
3432 break;
3433 case 0x10: /* IRQ Flag control */
3434 if( addr == 0x10 )
3435 {
3436 YM2608IRQFlagWrite(OPN, F2608, v);
3437 }
3438 break;
3439 default:
3440 OPNWriteReg(OPN,addr | 0x100,v);
3441 }
3442 }
3443 //return OPN->ST.irq;
3444 return;
3445 }
3446
3447 UINT8 ym2608_read(void *chip,UINT8 a)
3448 {
3449 YM2608 *F2608 = (YM2608 *)chip;
3450 UINT8 addr = F2608->OPN.ST.address;
3451 UINT8 ret = 0;
3452
3453 switch( a&3 )
3454 {
3455 case 0: /* status 0 : YM2203 compatible */
3456 /* BUSY:x:x:x:x:x:FLAGB:FLAGA */
3457 ret = FM_STATUS_FLAG(&F2608->OPN.ST) & 0x83;
3458 break;
3459
3460 case 1: /* status 0, ID */
3461 if( addr < 16 ) ret = F2608->OPN.ST.SSG_funcs.read(F2608->OPN.ST.SSG_param);
3462 else if(addr == 0xff) ret = 0x01; /* ID code */
3463 break;
3464
3465 case 2: /* status 1 : status 0 + ADPCM status */
3466 /* BUSY : x : PCMBUSY : ZERO : BRDY : EOS : FLAGB : FLAGA */
3467 ret = (FM_STATUS_FLAG(&F2608->OPN.ST) & (F2608->flagmask|0x80)) | ((F2608->deltaT.PCM_BSY & 1)<<5) ;
3468 break;
3469
3470 case 3:
3471 if(addr == 0x08)
3472 {
3473 ret = YM_DELTAT_ADPCM_Read(&F2608->deltaT);
3474 }
3475 else if(addr == 0x0f)
3476 {
3477 //logerror("YM2608 A/D conversion is accessed but not implemented !\n");
3478 ret = 0x80; /* 2's complement PCM data - result from A/D conversion */
3479 }
3480 break;
3481 }
3482 return ret;
3483 }
3484
3485 UINT8 ym2608_timer_over(void *chip,UINT8 c)
3486 {
3487 YM2608 *F2608 = (YM2608 *)chip;
3488
3489 switch(c)
3490 {
3491 #if 0
3492 case 2:
3493 { /* BUFRDY flag */
3494 YM_DELTAT_BRDY_callback( &F2608->deltaT );
3495 }
3496 break;
3497 #endif
3498 case 1:
3499 { /* Timer B */
3500 TimerBOver( &(F2608->OPN.ST) );
3501 }
3502 break;
3503 case 0:
3504 { /* Timer A */
3505 ym2608_update_req(F2608);
3506 /* timer update */
3507 TimerAOver( &(F2608->OPN.ST) );
3508 /* CSM mode key,TL controll */
3509 if ((F2608->OPN.ST.mode & 0xc0) == 0x80)
3510 { /* CSM mode total level latch and auto key on */
3511 CSMKeyControll( &F2608->OPN, &(F2608->CH[2]) );
3512 }
3513 }
3514 break;
3515 default:
3516 break;
3517 }
3518
3519 return F2608->OPN.ST.irq;
3520 }
3521
3522 void ym2608_alloc_pcmromb(void* chip, UINT32 memsize)
3523 {
3524 YM2608* F2608 = (YM2608*)chip;
3525
3526 if (F2608->deltaT.memory_size == memsize)
3527 return;
3528 F2608->deltaT.memory = (UINT8*)realloc(F2608->deltaT.memory, memsize);
3529 F2608->deltaT.memory_size = memsize;
3530 memset(F2608->deltaT.memory, 0, memsize);
3531 YM_DELTAT_calc_mem_mask(&F2608->deltaT);
3532
3533 return;
3534 }
3535
3536 void ym2608_write_pcmromb(void* chip, UINT32 offset, UINT32 length, const UINT8* data)
3537 {
3538 YM2608* F2608 = (YM2608*)chip;
3539
3540 if (offset > F2608->deltaT.memory_size)
3541 return;
3542 if (offset + length > F2608->deltaT.memory_size)
3543 length = F2608->deltaT.memory_size - offset;
3544
3545 memcpy(F2608->deltaT.memory + offset, data, length);
3546
3547 return;
3548 }
3549
3550 void ym2608_set_mutemask(void *chip, UINT32 MuteMask)
3551 {
3552 YM2608 *F2608 = (YM2608 *)chip;
3553 UINT8 CurChn;
3554
3555 for (CurChn = 0; CurChn < 6; CurChn ++)
3556 F2608->CH[CurChn].Muted = (MuteMask >> CurChn) & 0x01;
3557 for (CurChn = 0; CurChn < 6; CurChn ++)
3558 F2608->adpcm[CurChn].Muted = (MuteMask >> (CurChn + 6)) & 0x01;
3559 F2608->MuteDeltaT = (MuteMask >> 12) & 0x01;
3560
3561 return;
3562 }
3563 #endif /* BUILD_YM2608 */
3564
3565
3566
3567 #if (BUILD_YM2610||BUILD_YM2610B)
3568 /* YM2610(OPNB) */
3569
3570 /* Generate samples for one of the YM2610s */
3571 void ym2610_update_one(void *chip, UINT32 length, DEV_SMPL **buffer)
3572 {
3573 YM2610 *F2610 = (YM2610 *)chip;
3574 FM_OPN *OPN = &F2610->OPN;
3575 YM_DELTAT *DELTAT = &F2610->deltaT;
3576 UINT32 i;
3577 UINT8 j;
3578 DEV_SMPL *bufL,*bufR;
3579 FM_CH *cch[4];
3580 INT32 *out_fm = OPN->out_fm;
3581
3582 /* buffer setup */
3583 if (buffer != NULL)
3584 {
3585 bufL = buffer[0];
3586 bufR = buffer[1];
3587 }
3588 else
3589 {
3590 bufL = bufR = NULL;
3591 }
3592
3593 cch[0] = &F2610->CH[1];
3594 cch[1] = &F2610->CH[2];
3595 cch[2] = &F2610->CH[4];
3596 cch[3] = &F2610->CH[5];
3597
3598 #ifdef YM2610B_WARNING
3599 #define FM_KEY_IS(SLOT) ((SLOT)->key)
3600 #define FM_MSG_YM2610B "YM2610-%p.CH%d is playing,Check whether the type of the chip is YM2610B\n"
3601 /* Check YM2610B warning message */
3602 if( FM_KEY_IS(&F2610->CH[0].SLOT[3]) )
3603 {
3604 LOG(LOG_WAR,(FM_MSG_YM2610B,F2610->OPN.ST.param,0));
3605 FM_KEY_IS(&F2610->CH[0].SLOT[3]) = 0;
3606 }
3607 if( FM_KEY_IS(&F2610->CH[3].SLOT[3]) )
3608 {
3609 LOG(LOG_WAR,(FM_MSG_YM2610B,F2610->OPN.ST.param,3));
3610 FM_KEY_IS(&F2610->CH[3].SLOT[3]) = 0;
3611 }
3612 #endif
3613
3614 /* refresh PG and EG */
3615 refresh_fc_eg_chan( OPN, cch[0] );
3616 if( (OPN->ST.mode & 0xc0) )
3617 {
3618 /* 3SLOT MODE */
3619 if( cch[1]->SLOT[SLOT1].Incr==-1)
3620 {
3621 refresh_fc_eg_slot(OPN, &cch[1]->SLOT[SLOT1] , OPN->SL3.fc[1] , OPN->SL3.kcode[1] );
3622 refresh_fc_eg_slot(OPN, &cch[1]->SLOT[SLOT2] , OPN->SL3.fc[2] , OPN->SL3.kcode[2] );
3623 refresh_fc_eg_slot(OPN, &cch[1]->SLOT[SLOT3] , OPN->SL3.fc[0] , OPN->SL3.kcode[0] );
3624 refresh_fc_eg_slot(OPN, &cch[1]->SLOT[SLOT4] , cch[1]->fc , cch[1]->kcode );
3625 }
3626 }
3627 else
3628 refresh_fc_eg_chan( OPN, cch[1] );
3629 refresh_fc_eg_chan( OPN, cch[2] );
3630 refresh_fc_eg_chan( OPN, cch[3] );
3631 if (! length)
3632 {
3633 update_ssg_eg_channel(&cch[0]->SLOT[SLOT1]);
3634 update_ssg_eg_channel(&cch[1]->SLOT[SLOT1]);
3635 update_ssg_eg_channel(&cch[2]->SLOT[SLOT1]);
3636 update_ssg_eg_channel(&cch[3]->SLOT[SLOT1]);
3637 }
3638
3639 /* buffering */
3640 for(i=0; i < length ; i++)
3641 {
3642 /* clear output acc. */
3643 OPN->out_adpcm[OUTD_LEFT] = OPN->out_adpcm[OUTD_RIGHT] = OPN->out_adpcm[OUTD_CENTER] = 0;
3644 OPN->out_delta[OUTD_LEFT] = OPN->out_delta[OUTD_RIGHT] = OPN->out_delta[OUTD_CENTER] = 0;
3645 /* clear outputs */
3646 out_fm[1] = 0;
3647 out_fm[2] = 0;
3648 out_fm[4] = 0;
3649 out_fm[5] = 0;
3650
3651 /* update SSG-EG output */
3652 update_ssg_eg_channel(&cch[0]->SLOT[SLOT1]);
3653 update_ssg_eg_channel(&cch[1]->SLOT[SLOT1]);
3654 update_ssg_eg_channel(&cch[2]->SLOT[SLOT1]);
3655 update_ssg_eg_channel(&cch[3]->SLOT[SLOT1]);
3656
3657 /* calculate FM */
3658 chan_calc(OPN, cch[0], 1 ); /*remapped to 1*/
3659 chan_calc(OPN, cch[1], 2 ); /*remapped to 2*/
3660 chan_calc(OPN, cch[2], 4 ); /*remapped to 4*/
3661 chan_calc(OPN, cch[3], 5 ); /*remapped to 5*/
3662
3663 /* deltaT ADPCM */
3664 if( DELTAT->portstate&0x80 && ! F2610->MuteDeltaT )
3665 YM_DELTAT_ADPCM_CALC(DELTAT);
3666
3667 /* ADPCMA */
3668 for( j = 0; j < 6; j++ )
3669 {
3670 if( F2610->adpcm[j].flag )
3671 ADPCMA_calc_chan( F2610, &F2610->adpcm[j]);
3672 }
3673
3674 /* advance LFO */
3675 advance_lfo(OPN);
3676
3677 /* advance envelope generator */
3678 OPN->eg_timer += OPN->eg_timer_add;
3679 while (OPN->eg_timer >= OPN->eg_timer_overflow)
3680 {
3681 OPN->eg_timer -= OPN->eg_timer_overflow;
3682 OPN->eg_cnt++;
3683
3684 advance_eg_channel(OPN, &cch[0]->SLOT[SLOT1]);
3685 advance_eg_channel(OPN, &cch[1]->SLOT[SLOT1]);
3686 advance_eg_channel(OPN, &cch[2]->SLOT[SLOT1]);
3687 advance_eg_channel(OPN, &cch[3]->SLOT[SLOT1]);
3688 }
3689
3690 /* buffering */
3691 {
3692 DEV_SMPL lt,rt;
3693
3694 lt = (OPN->out_adpcm[OUTD_LEFT] + OPN->out_adpcm[OUTD_CENTER])<<1;
3695 rt = (OPN->out_adpcm[OUTD_RIGHT] + OPN->out_adpcm[OUTD_CENTER])<<1;
3696 lt += (OPN->out_delta[OUTD_LEFT] + OPN->out_delta[OUTD_CENTER])>>8;
3697 rt += (OPN->out_delta[OUTD_RIGHT] + OPN->out_delta[OUTD_CENTER])>>8;
3698
3699
3700 lt += (out_fm[1] & OPN->pan[2]);
3701 rt += (out_fm[1] & OPN->pan[3]);
3702 lt += (out_fm[2] & OPN->pan[4]);
3703 rt += (out_fm[2] & OPN->pan[5]);
3704
3705 lt += (out_fm[4] & OPN->pan[8]);
3706 rt += (out_fm[4] & OPN->pan[9]);
3707 lt += (out_fm[5] & OPN->pan[10]);
3708 rt += (out_fm[5] & OPN->pan[11]);
3709
3710 //lt >>= 1; // the shift right was verified on real chip
3711 //rt >>= 1;
3712
3713 /* buffering */
3714 bufL[i] = lt;
3715 bufR[i] = rt;
3716 }
3717
3718 /* CSM mode: if CSM Key ON has occured, CSM Key OFF need to be sent */
3719 /* only if Timer A does not overflow again (i.e CSM Key ON not set again) */
3720 OPN->SL3.key_csm <<= 1;
3721
3722 /* timer A control */
3723 INTERNAL_TIMER_A( &OPN->ST , cch[1] )
3724
3725 /* CSM Mode Key ON still disabled */
3726 if (OPN->SL3.key_csm & 2)
3727 {
3728 /* CSM Mode Key OFF (verified by Nemesis on real hardware) */
3729 FM_KEYOFF_CSM(cch[1],SLOT1);
3730 FM_KEYOFF_CSM(cch[1],SLOT2);
3731 FM_KEYOFF_CSM(cch[1],SLOT3);
3732 FM_KEYOFF_CSM(cch[1],SLOT4);
3733 OPN->SL3.key_csm = 0;
3734 }
3735 }
3736
3737 /* timer B control */
3738 INTERNAL_TIMER_B(&OPN->ST,length)
3739 }
3740
3741 static void ym2610_update_req(void *param)
3742 {
3743 ym2610b_update_one(param, 0, NULL);
3744 }
3745
3746 #if BUILD_YM2610B
3747 /* Generate samples for one of the YM2610Bs */
3748 void ym2610b_update_one(void *chip, UINT32 length, DEV_SMPL **buffer)
3749 {
3750 YM2610 *F2610 = (YM2610 *)chip;
3751 FM_OPN *OPN = &F2610->OPN;
3752 YM_DELTAT *DELTAT = &F2610->deltaT;
3753 UINT32 i;
3754 UINT8 j;
3755 DEV_SMPL *bufL,*bufR;
3756 FM_CH *cch[6];
3757 INT32 *out_fm = OPN->out_fm;
3758
3759 /* buffer setup */
3760 if (buffer != NULL)
3761 {
3762 bufL = buffer[0];
3763 bufR = buffer[1];
3764 }
3765 else
3766 {
3767 bufL = bufR = NULL;
3768 }
3769
3770 cch[0] = &F2610->CH[0];
3771 cch[1] = &F2610->CH[1];
3772 cch[2] = &F2610->CH[2];
3773 cch[3] = &F2610->CH[3];
3774 cch[4] = &F2610->CH[4];
3775 cch[5] = &F2610->CH[5];
3776
3777 /* refresh PG and EG */
3778 refresh_fc_eg_chan( OPN, cch[0] );
3779 refresh_fc_eg_chan( OPN, cch[1] );
3780 if( (OPN->ST.mode & 0xc0) )
3781 {
3782 /* 3SLOT MODE */
3783 if( cch[2]->SLOT[SLOT1].Incr==-1)
3784 {
3785 refresh_fc_eg_slot(OPN, &cch[2]->SLOT[SLOT1] , OPN->SL3.fc[1] , OPN->SL3.kcode[1] );
3786 refresh_fc_eg_slot(OPN, &cch[2]->SLOT[SLOT2] , OPN->SL3.fc[2] , OPN->SL3.kcode[2] );
3787 refresh_fc_eg_slot(OPN, &cch[2]->SLOT[SLOT3] , OPN->SL3.fc[0] , OPN->SL3.kcode[0] );
3788 refresh_fc_eg_slot(OPN, &cch[2]->SLOT[SLOT4] , cch[2]->fc , cch[2]->kcode );
3789 }
3790 }
3791 else
3792 refresh_fc_eg_chan( OPN, cch[2] );
3793 refresh_fc_eg_chan( OPN, cch[3] );
3794 refresh_fc_eg_chan( OPN, cch[4] );
3795 refresh_fc_eg_chan( OPN, cch[5] );
3796 if (! length)
3797 {
3798 update_ssg_eg_channel(&cch[0]->SLOT[SLOT1]);
3799 update_ssg_eg_channel(&cch[1]->SLOT[SLOT1]);
3800 update_ssg_eg_channel(&cch[2]->SLOT[SLOT1]);
3801 update_ssg_eg_channel(&cch[3]->SLOT[SLOT1]);
3802 update_ssg_eg_channel(&cch[4]->SLOT[SLOT1]);
3803 update_ssg_eg_channel(&cch[5]->SLOT[SLOT1]);
3804 }
3805
3806 /* buffering */
3807 for(i=0; i < length ; i++)
3808 {
3809 /* clear output acc. */
3810 OPN->out_adpcm[OUTD_LEFT] = OPN->out_adpcm[OUTD_RIGHT] = OPN->out_adpcm[OUTD_CENTER] = 0;
3811 OPN->out_delta[OUTD_LEFT] = OPN->out_delta[OUTD_RIGHT] = OPN->out_delta[OUTD_CENTER] = 0;
3812 /* clear outputs */
3813 out_fm[0] = 0;
3814 out_fm[1] = 0;
3815 out_fm[2] = 0;
3816 out_fm[3] = 0;
3817 out_fm[4] = 0;
3818 out_fm[5] = 0;
3819
3820 /* update SSG-EG output */
3821 update_ssg_eg_channel(&cch[0]->SLOT[SLOT1]);
3822 update_ssg_eg_channel(&cch[1]->SLOT[SLOT1]);
3823 update_ssg_eg_channel(&cch[2]->SLOT[SLOT1]);
3824 update_ssg_eg_channel(&cch[3]->SLOT[SLOT1]);
3825 update_ssg_eg_channel(&cch[4]->SLOT[SLOT1]);
3826 update_ssg_eg_channel(&cch[5]->SLOT[SLOT1]);
3827
3828 /* calculate FM */
3829 chan_calc(OPN, cch[0], 0 );
3830 chan_calc(OPN, cch[1], 1 );
3831 chan_calc(OPN, cch[2], 2 );
3832 chan_calc(OPN, cch[3], 3 );
3833 chan_calc(OPN, cch[4], 4 );
3834 chan_calc(OPN, cch[5], 5 );
3835
3836 /* deltaT ADPCM */
3837 if( DELTAT->portstate&0x80 && ! F2610->MuteDeltaT )
3838 YM_DELTAT_ADPCM_CALC(DELTAT);
3839
3840 /* ADPCMA */
3841 for( j = 0; j < 6; j++ )
3842 {
3843 if( F2610->adpcm[j].flag )
3844 ADPCMA_calc_chan( F2610, &F2610->adpcm[j]);
3845 }
3846
3847 /* advance LFO */
3848 advance_lfo(OPN);
3849
3850 /* advance envelope generator */
3851 OPN->eg_timer += OPN->eg_timer_add;
3852 while (OPN->eg_timer >= OPN->eg_timer_overflow)
3853 {
3854 OPN->eg_timer -= OPN->eg_timer_overflow;
3855 OPN->eg_cnt++;
3856
3857 advance_eg_channel(OPN, &cch[0]->SLOT[SLOT1]);
3858 advance_eg_channel(OPN, &cch[1]->SLOT[SLOT1]);
3859 advance_eg_channel(OPN, &cch[2]->SLOT[SLOT1]);
3860 advance_eg_channel(OPN, &cch[3]->SLOT[SLOT1]);
3861 advance_eg_channel(OPN, &cch[4]->SLOT[SLOT1]);
3862 advance_eg_channel(OPN, &cch[5]->SLOT[SLOT1]);
3863 }
3864
3865
3866 /* buffering */
3867 {
3868 DEV_SMPL lt,rt;
3869
3870 lt = (OPN->out_adpcm[OUTD_LEFT] + OPN->out_adpcm[OUTD_CENTER])<<1;
3871 rt = (OPN->out_adpcm[OUTD_RIGHT] + OPN->out_adpcm[OUTD_CENTER])<<1;
3872 lt += (OPN->out_delta[OUTD_LEFT] + OPN->out_delta[OUTD_CENTER])>>8;
3873 rt += (OPN->out_delta[OUTD_RIGHT] + OPN->out_delta[OUTD_CENTER])>>8;
3874
3875 lt += (out_fm[0] & OPN->pan[0]);
3876 rt += (out_fm[0] & OPN->pan[1]);
3877 lt += (out_fm[1] & OPN->pan[2]);
3878 rt += (out_fm[1] & OPN->pan[3]);
3879 lt += (out_fm[2] & OPN->pan[4]);
3880 rt += (out_fm[2] & OPN->pan[5]);
3881 lt += (out_fm[3] & OPN->pan[6]);
3882 rt += (out_fm[3] & OPN->pan[7]);
3883 lt += (out_fm[4] & OPN->pan[8]);
3884 rt += (out_fm[4] & OPN->pan[9]);
3885 lt += (out_fm[5] & OPN->pan[10]);
3886 rt += (out_fm[5] & OPN->pan[11]);
3887
3888 //lt >>= 1; // the shift right is verified on YM2610
3889 //rt >>= 1;
3890
3891 /* buffering */
3892 bufL[i] = lt;
3893 bufR[i] = rt;
3894 }
3895
3896 /* CSM mode: if CSM Key ON has occured, CSM Key OFF need to be sent */
3897 /* only if Timer A does not overflow again (i.e CSM Key ON not set again) */
3898 OPN->SL3.key_csm <<= 1;
3899
3900 /* timer A control */
3901 INTERNAL_TIMER_A( &OPN->ST , cch[2] )
3902
3903 /* CSM Mode Key ON still disabled */
3904 if (OPN->SL3.key_csm & 2)
3905 {
3906 /* CSM Mode Key OFF (verified by Nemesis on real hardware) */
3907 FM_KEYOFF_CSM(cch[2],SLOT1);
3908 FM_KEYOFF_CSM(cch[2],SLOT2);
3909 FM_KEYOFF_CSM(cch[2],SLOT3);
3910 FM_KEYOFF_CSM(cch[2],SLOT4);
3911 OPN->SL3.key_csm = 0;
3912 }
3913 }
3914
3915 /* timer B control */
3916 INTERNAL_TIMER_B(&OPN->ST,length)
3917 }
3918 #endif /* BUILD_YM2610B */
3919
3920
3921 static void YM2610_deltat_status_set(void *chip, UINT8 changebits)
3922 {
3923 YM2610 *F2610 = (YM2610 *)chip;
3924 F2610->adpcm_arrivedEndAddress |= changebits;
3925 }
3926 static void YM2610_deltat_status_reset(void *chip, UINT8 changebits)
3927 {
3928 YM2610 *F2610 = (YM2610 *)chip;
3929 F2610->adpcm_arrivedEndAddress &= (~changebits);
3930 }
3931
3932 void *ym2610_init(void *param, UINT32 clock, UINT32 rate,
3933 FM_TIMERHANDLER timer_handler,FM_IRQHANDLER IRQHandler)
3934 {
3935 YM2610 *F2610;
3936
3937 /* allocate extend state space */
3938 F2610 = (YM2610 *)calloc(1,sizeof(YM2610));
3939 if( F2610==NULL)
3940 return NULL;
3941
3942 /* allocate total level table (128kb space) */
3943 init_tables();
3944
3945 /* FM */
3946 F2610->OPN.ST.param = param;
3947 F2610->OPN.type = TYPE_YM2610;
3948 F2610->OPN.P_CH = F2610->CH;
3949 F2610->OPN.ST.clock = clock;
3950 F2610->OPN.ST.rate = rate;
3951 OPNCheckNativeSampleRate(&F2610->OPN);
3952 /* Extend handler */
3953 F2610->OPN.ST.timer_handler = timer_handler;
3954 F2610->OPN.ST.IRQ_Handler = IRQHandler;
3955 OPNLinkSSG(&F2610->OPN, NULL, NULL);
3956 OPNSetSmplRateChgCallback(&F2610->OPN, NULL, NULL);
3957 /* ADPCM */
3958 F2610->pcmbuf = NULL;
3959 F2610->pcm_size = 0x00;
3960 /* DELTA-T */
3961 F2610->deltaT.memory = NULL;
3962 F2610->deltaT.memory_size = 0x00;
3963 F2610->deltaT.memory_mask = 0x00;
3964
3965 F2610->deltaT.status_set_handler = YM2610_deltat_status_set;
3966 F2610->deltaT.status_reset_handler = YM2610_deltat_status_reset;
3967 F2610->deltaT.status_change_which_chip = F2610;
3968 F2610->deltaT.status_change_EOS_bit = 0x80; /* status flag: set bit7 on End Of Sample */
3969
3970 YM_DELTAT_ADPCM_Init(&F2610->deltaT,YM_DELTAT_EMULATION_MODE_YM2610,8,F2610->OPN.out_delta,1<<23);
3971
3972 Init_ADPCMATable();
3973
3974 ym2610_set_mutemask(F2610, 0x00);
3975
3976 return F2610;
3977 }
3978
3979 /* link SSG emulator */
3980 void ym2610_link_ssg(void *chip, const ssg_callbacks *ssg, void *ssg_param)
3981 {
3982 YM2610 *F2610 = (YM2610 *)chip;
3983
3984 OPNLinkSSG(&F2610->OPN, ssg, ssg_param);
3985 OPNPrescaler_w(&F2610->OPN, 1, 2);
3986 return;
3987 }
3988
3989 /* shut down emulator */
3990 void ym2610_shutdown(void *chip)
3991 {
3992 YM2610 *F2610 = (YM2610 *)chip;
3993
3994 free(F2610->pcmbuf); F2610->pcmbuf = NULL;
3995 free(F2610->deltaT.memory); F2610->deltaT.memory = NULL;
3996
3997 free(F2610);
3998 }
3999
4000 /* reset one of chip */
4001 void ym2610_reset_chip(void *chip)
4002 {
4003 int i;
4004 YM2610 *F2610 = (YM2610 *)chip;
4005 FM_OPN *OPN = &F2610->OPN;
4006 YM_DELTAT *DELTAT = &F2610->deltaT;
4007
4008 /* Reset Prescaler */
4009 OPNPrescaler_w( OPN, 0, 2); /* OPN 1/6 , SSG 1/4 */
4010 /* reset SSG section */
4011 OPN->ST.SSG_funcs.reset(OPN->ST.SSG_param);
4012 /* status clear */
4013 FM_IRQMASK_SET(&OPN->ST,0x03);
4014 FM_BUSY_CLEAR(&OPN->ST);
4015
4016 OPN->eg_timer = 0;
4017 OPN->eg_cnt = 0;
4018
4019 OPN->ST.TAC = 0;
4020 OPN->ST.TBC = 0;
4021
4022 OPN->SL3.key_csm = 0;
4023
4024 OPN->ST.status = 0;
4025 OPN->ST.mode = 0;
4026 OPN->ST.irq = 0;
4027
4028 memset(F2610->REGS, 0x00, sizeof(UINT8) * 512);
4029
4030 reset_channels( &OPN->ST , F2610->CH , 6 );
4031 /* reset Operator paramater */
4032 for(i = 0xb6 ; i >= 0xb4 ; i-- )
4033 {
4034 OPNWriteReg(OPN,i ,0xc0);
4035 OPNWriteReg(OPN,i|0x100,0xc0);
4036 }
4037 for(i = 0xb2 ; i >= 0x30 ; i-- )
4038 {
4039 OPNWriteReg(OPN,i ,0);
4040 OPNWriteReg(OPN,i|0x100,0);
4041 }
4042 OPNWriteMode(OPN,0x27,0x30); /* mode 0 , timer reset */
4043 for(i = 0x26 ; i >= 0x20 ; i-- ) OPNWriteReg(OPN,i,0);
4044
4045 /**** ADPCM work initial ****/
4046 for( i = 0; i < 6 ; i++ )
4047 {
4048 F2610->adpcm[i].step = (UINT32)((float)(1<<ADPCM_SHIFT)*((float)F2610->OPN.ST.freqbase)/3.0);
4049 F2610->adpcm[i].now_addr = 0;
4050 F2610->adpcm[i].now_step = 0;
4051 F2610->adpcm[i].start = 0;
4052 F2610->adpcm[i].end = 0;
4053 // F2610->adpcm[i].delta = 21866;
4054 F2610->adpcm[i].vol_mul = 0;
4055 F2610->adpcm[i].pan = &OPN->out_adpcm[OUTD_CENTER]; /* default center */
4056 F2610->adpcm[i].flagMask = 1<<i;
4057 F2610->adpcm[i].flag = 0;
4058 F2610->adpcm[i].adpcm_acc = 0;
4059 F2610->adpcm[i].adpcm_step= 0;
4060 F2610->adpcm[i].adpcm_out = 0;
4061 }
4062 F2610->adpcmTL = 0x3f;
4063
4064 F2610->adpcm_arrivedEndAddress = 0;
4065
4066 /* DELTA-T unit */
4067 DELTAT->freqbase = OPN->ST.freqbase;
4068 YM_DELTAT_ADPCM_Reset(DELTAT,OUTD_CENTER);
4069 }
4070
4071 /* YM2610 write */
4072 /* n = number */
4073 /* a = address */
4074 /* v = value */
4075 void ym2610_write(void *chip, UINT8 a, UINT8 v)
4076 {
4077 YM2610 *F2610 = (YM2610 *)chip;
4078 FM_OPN *OPN = &F2610->OPN;
4079 int addr;
4080 int ch;
4081
4082 //v &= 0xff; /* adjust to 8 bit bus */
4083
4084 switch( a&3 )
4085 {
4086 case 0: /* address port 0 */
4087 OPN->ST.address = v;
4088 F2610->addr_A1 = 0;
4089
4090 /* Write register to SSG emulator */
4091 if( v < 16 ) OPN->ST.SSG_funcs.write(OPN->ST.SSG_param,a,v);
4092 break;
4093
4094 case 1: /* data port 0 */
4095 if (F2610->addr_A1 != 0)
4096 break; /* verified on real YM2608 */
4097
4098 addr = OPN->ST.address;
4099 F2610->REGS[addr] = v;
4100 switch(addr & 0xf0)
4101 {
4102 case 0x00: /* SSG section */
4103 /* Write data to SSG emulator */
4104 OPN->ST.SSG_funcs.write(OPN->ST.SSG_param,a,v);
4105 break;
4106 case 0x10: /* DeltaT ADPCM */
4107 ym2610_update_req(F2610);
4108
4109 switch(addr)
4110 {
4111 case 0x10: /* control 1 */
4112 case 0x11: /* control 2 */
4113 case 0x12: /* start address L */
4114 case 0x13: /* start address H */
4115 case 0x14: /* stop address L */
4116 case 0x15: /* stop address H */
4117
4118 case 0x19: /* delta-n L */
4119 case 0x1a: /* delta-n H */
4120 case 0x1b: /* volume */
4121 {
4122 YM_DELTAT_ADPCM_Write(&F2610->deltaT,addr-0x10,v);
4123 }
4124 break;
4125
4126 case 0x1c: /* FLAG CONTROL : Extend Status Clear/Mask */
4127 {
4128 UINT8 statusmask = ~v;
4129 /* set arrived flag mask */
4130 for(ch=0;ch<6;ch++)
4131 F2610->adpcm[ch].flagMask = statusmask&(1<<ch);
4132
4133 F2610->deltaT.status_change_EOS_bit = statusmask & 0x80; /* status flag: set bit7 on End Of Sample */
4134
4135 /* clear arrived flag */
4136 F2610->adpcm_arrivedEndAddress &= statusmask;
4137 }
4138 break;
4139
4140 default:
4141 logerror("YM2610: write to unknown deltat register %02x val=%02x\n",addr,v);
4142 break;
4143 }
4144
4145 break;
4146 case 0x20: /* Mode Register */
4147 ym2610_update_req(F2610);
4148 OPNWriteMode(OPN,addr,v);
4149 break;
4150 default: /* OPN section */
4151 ym2610_update_req(F2610);
4152 /* write register */
4153 OPNWriteReg(OPN,addr,v);
4154 }
4155 break;
4156
4157 case 2: /* address port 1 */
4158 OPN->ST.address = v;
4159 F2610->addr_A1 = 1;
4160 break;
4161
4162 case 3: /* data port 1 */
4163 if (F2610->addr_A1 != 1)
4164 break; /* verified on real YM2608 */
4165
4166 ym2610_update_req(F2610);
4167 addr = OPN->ST.address;
4168 F2610->REGS[addr | 0x100] = v;
4169 if( addr < 0x30 )
4170 /* 100-12f : ADPCM A section */
4171 FM_ADPCMAWrite(F2610,addr,v);
4172 else
4173 OPNWriteReg(OPN,addr | 0x100,v);
4174 }
4175 //return OPN->ST.irq;
4176 return;
4177 }
4178
4179 UINT8 ym2610_read(void *chip,UINT8 a)
4180 {
4181 YM2610 *F2610 = (YM2610 *)chip;
4182 UINT8 addr = F2610->OPN.ST.address;
4183 UINT8 ret = 0;
4184
4185 switch( a&3)
4186 {
4187 case 0: /* status 0 : YM2203 compatible */
4188 ret = FM_STATUS_FLAG(&F2610->OPN.ST) & 0x83;
4189 break;
4190 case 1: /* data 0 */
4191 if( addr < 16 ) ret = F2610->OPN.ST.SSG_funcs.read(F2610->OPN.ST.SSG_param, 0);
4192 else if( addr == 0xff ) ret = 0x01;
4193 break;
4194 case 2: /* status 1 : ADPCM status */
4195 /* ADPCM STATUS (arrived End Address) */
4196 /* B,--,A5,A4,A3,A2,A1,A0 */
4197 /* B = ADPCM-B(DELTA-T) arrived end address */
4198 /* A0-A5 = ADPCM-A arrived end address */
4199 ret = F2610->adpcm_arrivedEndAddress;
4200 break;
4201 case 3:
4202 ret = 0;
4203 break;
4204 }
4205 return ret;
4206 }
4207
4208 UINT8 ym2610_timer_over(void *chip,UINT8 c)
4209 {
4210 YM2610 *F2610 = (YM2610 *)chip;
4211
4212 if( c )
4213 { /* Timer B */
4214 TimerBOver( &(F2610->OPN.ST) );
4215 }
4216 else
4217 { /* Timer A */
4218 ym2610_update_req(F2610);
4219 /* timer update */
4220 TimerAOver( &(F2610->OPN.ST) );
4221 /* CSM mode key,TL controll */
4222 if ((F2610->OPN.ST.mode & 0xc0) == 0x80)
4223 { /* CSM mode total level latch and auto key on */
4224 CSMKeyControll( &F2610->OPN, &(F2610->CH[2]) );
4225 }
4226 }
4227 return F2610->OPN.ST.irq;
4228 }
4229
4230 void ym2610_alloc_pcmroma(void* chip, UINT32 memsize)
4231 {
4232 YM2610 *F2610 = (YM2610 *)chip;
4233
4234 if (F2610->pcm_size == memsize)
4235 return;
4236 F2610->pcmbuf = (UINT8*)realloc(F2610->pcmbuf, memsize);
4237 F2610->pcm_size = memsize;
4238 memset(F2610->pcmbuf, 0xFF, memsize);
4239
4240 return;
4241 }
4242
4243 void ym2610_write_pcmroma(void* chip, UINT32 offset, UINT32 length, const UINT8* data)
4244 {
4245 YM2610 *F2610 = (YM2610 *)chip;
4246
4247 if (offset > F2610->pcm_size)
4248 return;
4249 if (offset + length > F2610->pcm_size)
4250 length = F2610->pcm_size - offset;
4251
4252 memcpy(F2610->pcmbuf + offset, data, length);
4253
4254 return;
4255 }
4256
4257 void ym2610_alloc_pcmromb(void* chip, UINT32 memsize)
4258 {
4259 YM2610 *F2610 = (YM2610 *)chip;
4260
4261 if (F2610->deltaT.memory_size == memsize)
4262 return;
4263 F2610->deltaT.memory = (UINT8*)realloc(F2610->deltaT.memory, memsize);
4264 F2610->deltaT.memory_size = memsize;
4265 memset(F2610->deltaT.memory, 0xFF, memsize);
4266 YM_DELTAT_calc_mem_mask(&F2610->deltaT);
4267
4268 return;
4269 }
4270
4271 void ym2610_write_pcmromb(void* chip, UINT32 offset, UINT32 length, const UINT8* data)
4272 {
4273 YM2610 *F2610 = (YM2610 *)chip;
4274
4275 if (offset > F2610->deltaT.memory_size)
4276 return;
4277 if (offset + length > F2610->deltaT.memory_size)
4278 length = F2610->deltaT.memory_size - offset;
4279
4280 memcpy(F2610->deltaT.memory + offset, data, length);
4281
4282 return;
4283 }
4284
4285 void ym2610_set_mutemask(void *chip, UINT32 MuteMask)
4286 {
4287 YM2610 *F2610 = (YM2610 *)chip;
4288 UINT8 CurChn;
4289
4290 for (CurChn = 0; CurChn < 6; CurChn ++)
4291 F2610->CH[CurChn].Muted = (MuteMask >> CurChn) & 0x01;
4292 for (CurChn = 0; CurChn < 6; CurChn ++)
4293 F2610->adpcm[CurChn].Muted = (MuteMask >> (CurChn + 6)) & 0x01;
4294 F2610->MuteDeltaT = (MuteMask >> 12) & 0x01;
4295
4296 return;
4297 }
4298 #endif /* (BUILD_YM2610||BUILD_YM2610B) */
4299
4300
4301 #if (BUILD_YM2612)
4302 /*******************************************************************************/
4303 /* YM2612 local section */
4304 /*******************************************************************************/
4305
4306 /* here's the virtual YM2612 */
4307 typedef struct
4308 {
4309 DEV_DATA _devData;
4310
4311 UINT8 REGS[512]; /* registers */
4312 FM_OPN OPN; /* OPN state */
4313 FM_CH CH[6]; /* channel state */
4314 UINT8 addr_A1; /* address line A1 */
4315
4316 /* dac output (YM2612) */
4317 UINT8 dacen;
4318 UINT8 dac_test;
4319 INT32 dacout;
4320 UINT8 MuteDAC;
4321
4322 UINT8 WaveOutMode;
4323 INT32 WaveL;
4324 INT32 WaveR;
4325 } YM2612;
4326
4327 /* Generate samples for one of the YM2612s */
4328 void ym2612_update_one(void *chip, UINT32 length, DEV_SMPL **buffer)
4329 {
4330 YM2612 *F2612 = (YM2612 *)chip;
4331 FM_OPN *OPN = &F2612->OPN;
4332 INT32 *out_fm = OPN->out_fm;
4333 UINT32 i;
4334 DEV_SMPL *bufL,*bufR;
4335 INT32 dacout;
4336 FM_CH *cch[6];
4337 INT32 lt,rt;
4338
4339 /* set buffer */
4340 if (buffer != NULL)
4341 {
4342 bufL = buffer[0];
4343 bufR = buffer[1];
4344 }
4345 else
4346 {
4347 // for internal 0-sample update
4348 bufL = bufR = NULL;
4349 }
4350
4351 cch[0] = &F2612->CH[0];
4352 cch[1] = &F2612->CH[1];
4353 cch[2] = &F2612->CH[2];
4354 cch[3] = &F2612->CH[3];
4355 cch[4] = &F2612->CH[4];
4356 cch[5] = &F2612->CH[5];
4357
4358 if (! F2612->MuteDAC)
4359 dacout = F2612->dacout << 5; /* level unknown */
4360 else
4361 dacout = 0;
4362
4363 /* refresh PG and EG */
4364 refresh_fc_eg_chan( OPN, cch[0] );
4365 refresh_fc_eg_chan( OPN, cch[1] );
4366 if( (OPN->ST.mode & 0xc0) )
4367 {
4368 /* 3SLOT MODE */
4369 if( cch[2]->SLOT[SLOT1].Incr==-1)
4370 {
4371 refresh_fc_eg_slot(OPN, &cch[2]->SLOT[SLOT1] , OPN->SL3.fc[1] , OPN->SL3.kcode[1] );
4372 refresh_fc_eg_slot(OPN, &cch[2]->SLOT[SLOT2] , OPN->SL3.fc[2] , OPN->SL3.kcode[2] );
4373 refresh_fc_eg_slot(OPN, &cch[2]->SLOT[SLOT3] , OPN->SL3.fc[0] , OPN->SL3.kcode[0] );
4374 refresh_fc_eg_slot(OPN, &cch[2]->SLOT[SLOT4] , cch[2]->fc , cch[2]->kcode );
4375 }
4376 }
4377 else
4378 refresh_fc_eg_chan( OPN, cch[2] );
4379 refresh_fc_eg_chan( OPN, cch[3] );
4380 refresh_fc_eg_chan( OPN, cch[4] );
4381 refresh_fc_eg_chan( OPN, cch[5] );
4382 if (! length)
4383 {
4384 update_ssg_eg_channel(&cch[0]->SLOT[SLOT1]);
4385 update_ssg_eg_channel(&cch[1]->SLOT[SLOT1]);
4386 update_ssg_eg_channel(&cch[2]->SLOT[SLOT1]);
4387 update_ssg_eg_channel(&cch[3]->SLOT[SLOT1]);
4388 update_ssg_eg_channel(&cch[4]->SLOT[SLOT1]);
4389 update_ssg_eg_channel(&cch[5]->SLOT[SLOT1]);
4390 }
4391
4392
4393 /* buffering */
4394 for(i=0; i < length ; i++)
4395 {
4396 /* clear outputs */
4397 out_fm[0] = 0;
4398 out_fm[1] = 0;
4399 out_fm[2] = 0;
4400 out_fm[3] = 0;
4401 out_fm[4] = 0;
4402 out_fm[5] = 0;
4403
4404 /* update SSG-EG output */
4405 update_ssg_eg_channel(&cch[0]->SLOT[SLOT1]);
4406 update_ssg_eg_channel(&cch[1]->SLOT[SLOT1]);
4407 update_ssg_eg_channel(&cch[2]->SLOT[SLOT1]);
4408 update_ssg_eg_channel(&cch[3]->SLOT[SLOT1]);
4409 update_ssg_eg_channel(&cch[4]->SLOT[SLOT1]);
4410 update_ssg_eg_channel(&cch[5]->SLOT[SLOT1]);
4411
4412 /* calculate FM */
4413 if (! F2612->dac_test)
4414 {
4415 chan_calc(OPN, cch[0], 0 );
4416 chan_calc(OPN, cch[1], 1 );
4417 chan_calc(OPN, cch[2], 2 );
4418 chan_calc(OPN, cch[3], 3 );
4419 chan_calc(OPN, cch[4], 4 );
4420 if( F2612->dacen )
4421 *cch[5]->connect4 += dacout;
4422 else
4423 chan_calc(OPN, cch[5], 5 );
4424 }
4425 else
4426 {
4427 out_fm[0] = out_fm[1] = dacout;
4428 out_fm[2] = out_fm[3] = dacout;
4429 out_fm[5] = dacout;
4430 }
4431
4432 /* advance LFO */
4433 advance_lfo(OPN);
4434
4435 /* advance envelope generator */
4436 OPN->eg_timer += OPN->eg_timer_add;
4437 while (OPN->eg_timer >= OPN->eg_timer_overflow)
4438 {
4439 OPN->eg_timer -= OPN->eg_timer_overflow;
4440 OPN->eg_cnt++;
4441
4442 advance_eg_channel(OPN, &cch[0]->SLOT[SLOT1]);
4443 advance_eg_channel(OPN, &cch[1]->SLOT[SLOT1]);
4444 advance_eg_channel(OPN, &cch[2]->SLOT[SLOT1]);
4445 advance_eg_channel(OPN, &cch[3]->SLOT[SLOT1]);
4446 advance_eg_channel(OPN, &cch[4]->SLOT[SLOT1]);
4447 advance_eg_channel(OPN, &cch[5]->SLOT[SLOT1]);
4448 }
4449
4450 if (out_fm[0] > 8192) out_fm[0] = 8192;
4451 else if (out_fm[0] < -8192) out_fm[0] = -8192;
4452 if (out_fm[1] > 8192) out_fm[1] = 8192;
4453 else if (out_fm[1] < -8192) out_fm[1] = -8192;
4454 if (out_fm[2] > 8192) out_fm[2] = 8192;
4455 else if (out_fm[2] < -8192) out_fm[2] = -8192;
4456 if (out_fm[3] > 8192) out_fm[3] = 8192;
4457 else if (out_fm[3] < -8192) out_fm[3] = -8192;
4458 if (out_fm[4] > 8192) out_fm[4] = 8192;
4459 else if (out_fm[4] < -8192) out_fm[4] = -8192;
4460 if (out_fm[5] > 8192) out_fm[5] = 8192;
4461 else if (out_fm[5] < -8192) out_fm[5] = -8192;
4462
4463 /* 6-channels mixing */
4464 lt = ((out_fm[0]>>0) & OPN->pan[0]);
4465 rt = ((out_fm[0]>>0) & OPN->pan[1]);
4466 lt += ((out_fm[1]>>0) & OPN->pan[2]);
4467 rt += ((out_fm[1]>>0) & OPN->pan[3]);
4468 lt += ((out_fm[2]>>0) & OPN->pan[4]);
4469 rt += ((out_fm[2]>>0) & OPN->pan[5]);
4470 lt += ((out_fm[3]>>0) & OPN->pan[6]);
4471 rt += ((out_fm[3]>>0) & OPN->pan[7]);
4472 if (! F2612->dac_test)
4473 {
4474 lt += ((out_fm[4]>>0) & OPN->pan[8]);
4475 rt += ((out_fm[4]>>0) & OPN->pan[9]);
4476 }
4477 else
4478 {
4479 // DAC test mode ignores panning for channel 4
4480 lt += dacout;
4481 lt += dacout;
4482 }
4483 lt += ((out_fm[5]>>0) & OPN->pan[10]);
4484 rt += ((out_fm[5]>>0) & OPN->pan[11]);
4485
4486 /* buffering */
4487 if (F2612->WaveOutMode)
4488 {
4489 if (F2612->WaveOutMode & 0x01)
4490 F2612->WaveL = lt;
4491 if (F2612->WaveOutMode & 0x02)
4492 F2612->WaveR = rt;
4493 F2612->WaveOutMode ^= 0x03;
4494 }
4495 else
4496 {
4497 F2612->WaveL = lt;
4498 F2612->WaveR = rt;
4499 }
4500 bufL[i] = F2612->WaveL;
4501 bufR[i] = F2612->WaveR;
4502
4503 /* CSM mode: if CSM Key ON has occured, CSM Key OFF need to be sent */
4504 /* only if Timer A does not overflow again (i.e CSM Key ON not set again) */
4505 OPN->SL3.key_csm <<= 1;
4506
4507 /* timer A control */
4508 INTERNAL_TIMER_A( &OPN->ST , cch[2] )
4509
4510 /* CSM Mode Key ON still disabled */
4511 if (OPN->SL3.key_csm & 2)
4512 {
4513 /* CSM Mode Key OFF (verified by Nemesis on real hardware) */
4514 FM_KEYOFF_CSM(cch[2],SLOT1);
4515 FM_KEYOFF_CSM(cch[2],SLOT2);
4516 FM_KEYOFF_CSM(cch[2],SLOT3);
4517 FM_KEYOFF_CSM(cch[2],SLOT4);
4518 OPN->SL3.key_csm = 0;
4519 }
4520 }
4521
4522 /* timer B control */
4523 INTERNAL_TIMER_B(&OPN->ST,length)
4524 }
4525
4526 static void ym2612_update_req(void *param)
4527 {
4528 ym2612_update_one(param, 0, NULL);
4529 }
4530
4531 /* initialize YM2612 emulator(s) */
4532 void * ym2612_init(void *param, UINT32 clock, UINT32 rate,
4533 FM_TIMERHANDLER timer_handler,FM_IRQHANDLER IRQHandler)
4534 {
4535 YM2612 *F2612;
4536
4537 /* allocate extend state space */
4538 F2612 = (YM2612 *)calloc(1, sizeof(YM2612));
4539 if (F2612 == NULL)
4540 return NULL;
4541
4542 /* allocate total level table (128kb space) */
4543 init_tables();
4544
4545 /* FM */
4546 F2612->OPN.ST.param = param;
4547 F2612->OPN.type = TYPE_YM2612;
4548 F2612->OPN.P_CH = F2612->CH;
4549 F2612->OPN.ST.clock = clock;
4550 F2612->OPN.ST.rate = rate;
4551 OPNCheckNativeSampleRate(&F2612->OPN);
4552 /* Extend handler */
4553 F2612->OPN.ST.timer_handler = timer_handler;
4554 F2612->OPN.ST.IRQ_Handler = IRQHandler;
4555 F2612->OPN.LegacyMode = 0x00;
4556 F2612->WaveOutMode = 0x00;
4557 OPNLinkSSG(&F2612->OPN, NULL, NULL);
4558 OPNSetSmplRateChgCallback(&F2612->OPN, NULL, NULL);
4559
4560 ym2612_set_mutemask(F2612, 0x00);
4561
4562 return F2612;
4563 }
4564
4565 /* shut down emulator */
4566 void ym2612_shutdown(void *chip)
4567 {
4568 YM2612 *F2612 = (YM2612 *)chip;
4569
4570 free(F2612);
4571 }
4572
4573 /* reset one of chip */
4574 void ym2612_reset_chip(void *chip)
4575 {
4576 int i;
4577 YM2612 *F2612 = (YM2612 *)chip;
4578 FM_OPN *OPN = &F2612->OPN;
4579
4580 /* Reset Prescaler */
4581 OPNSetPres( OPN, 6*24, 6*24, 0);
4582 /* status clear */
4583 FM_IRQMASK_SET(&OPN->ST,0x03);
4584 FM_BUSY_CLEAR(&OPN->ST);
4585
4586 OPN->eg_timer = 0;
4587 OPN->eg_cnt = 0;
4588
4589 OPN->ST.TAC = 0;
4590 OPN->ST.TBC = 0;
4591
4592 OPN->SL3.key_csm = 0;
4593
4594 OPN->ST.status = 0;
4595 OPN->ST.mode = 0;
4596 OPN->ST.irq = 0;
4597
4598 memset(F2612->REGS, 0x00, sizeof(UINT8) * 512);
4599
4600 reset_channels( &OPN->ST , F2612->CH , 6 );
4601 /* reset Operator paramater */
4602 for(i = 0xb6 ; i >= 0xb4 ; i-- )
4603 {
4604 OPNWriteReg(OPN,i ,0xc0);
4605 OPNWriteReg(OPN,i|0x100,0xc0);
4606 }
4607 for(i = 0xb2 ; i >= 0x30 ; i-- )
4608 {
4609 OPNWriteReg(OPN,i ,0);
4610 OPNWriteReg(OPN,i|0x100,0);
4611 }
4612 OPNWriteMode(OPN,0x27,0x30); /* mode 0 , timer reset */
4613 for(i = 0x26 ; i >= 0x20 ; i-- ) OPNWriteReg(OPN,i,0);
4614
4615 /* DAC mode clear */
4616 F2612->dacen = 0;
4617 F2612->dac_test = 0;
4618 F2612->dacout = 0;
4619
4620 F2612->WaveOutMode >>= 1;
4621 F2612->WaveL = F2612->WaveR = 0;
4622 }
4623
4624 /* YM2612 write */
4625 /* n = number */
4626 /* a = address */
4627 /* v = value */
4628 void ym2612_write(void *chip, UINT8 a, UINT8 v)
4629 {
4630 YM2612 *F2612 = (YM2612 *)chip;
4631 FM_OPN *OPN = &F2612->OPN;
4632 int addr;
4633
4634 //v &= 0xff; /* adjust to 8 bit bus */
4635
4636 switch( a&3 )
4637 {
4638 case 0: /* address port 0 */
4639 OPN->ST.address = v;
4640 F2612->addr_A1 = 0;
4641
4642 /* Write register to SSG emulator */
4643 if( v < 16 ) OPN->ST.SSG_funcs.write(OPN->ST.SSG_param,a,v);
4644 break;
4645
4646 case 1: /* data port 0 */
4647 if (F2612->addr_A1 != 0)
4648 break; /* verified on real YM2608 */
4649
4650 addr = OPN->ST.address;
4651 F2612->REGS[addr] = v;
4652 switch(addr & 0xf0)
4653 {
4654 case 0x20: /* 0x20-0x2f Mode */
4655 switch( addr )
4656 {
4657 case 0x2a: /* DAC data (YM2612) */
4658 //ym2612_update_req(F2612);
4659 F2612->dacout &= 0x01;
4660 // set high 8 bit of the 9-bit DAC
4661 F2612->dacout |= ((int)v - 0x80) << 1; /* level unknown */
4662 break;
4663 case 0x2b: /* DAC Sel (YM2612) */
4664 /* b7 = dac enable */
4665 F2612->dacen = v & 0x80;
4666 break;
4667 case 0x2C: // undocumented: DAC Test Register
4668 // http://gendev.spritesmind.net/forum/viewtopic.php?p=26996#p26996
4669 // b7/b6/b4 = various test bits
4670 // b5 = makes DAC go to channels 0..3 and 5.
4671 // b3 = 9th DAC bit
4672 F2612->dac_test = v & 0x20;
4673 F2612->dacout &= ~0x01;
4674 // set DAC's 9th bit
4675 F2612->dacout |= (v & 0x08) >> 3;
4676 break;
4677 default: /* OPN section */
4678 ym2612_update_req(F2612);
4679 /* write register */
4680 OPNWriteMode(OPN,addr,v);
4681 }
4682 break;
4683 default: /* 0x30-0xff OPN section */
4684 ym2612_update_req(F2612);
4685 /* write register */
4686 OPNWriteReg(OPN,addr,v);
4687 }
4688 break;
4689
4690 case 2: /* address port 1 */
4691 OPN->ST.address = v;
4692 F2612->addr_A1 = 1;
4693 break;
4694
4695 case 3: /* data port 1 */
4696 if (F2612->addr_A1 != 1)
4697 break; /* verified on real YM2608 */
4698
4699 ym2612_update_req(F2612);
4700 addr = OPN->ST.address;
4701 F2612->REGS[addr | 0x100] = v;
4702 OPNWriteReg(OPN,addr | 0x100,v);
4703 break;
4704 }
4705 //return OPN->ST.irq;
4706 return;
4707 }
4708
4709 UINT8 ym2612_read(void *chip, UINT8 a)
4710 {
4711 YM2612 *F2612 = (YM2612 *)chip;
4712
4713 switch( a&3)
4714 {
4715 case 0: /* status 0 : YM2203 compatible */
4716 return FM_STATUS_FLAG(&F2612->OPN.ST);
4717 case 1:
4718 case 2:
4719 case 3:
4720 //LOG(LOG_WAR,("YM2612 #%p:A=%d read unmapped area\n",F2612->OPN.ST.param,a));
4721 return FM_STATUS_FLAG(&F2612->OPN.ST);
4722 }
4723 return 0;
4724 }
4725
4726 UINT8 ym2612_timer_over(void *chip,UINT8 c)
4727 {
4728 YM2612 *F2612 = (YM2612 *)chip;
4729
4730 if( c )
4731 { /* Timer B */
4732 TimerBOver( &(F2612->OPN.ST) );
4733 }
4734 else
4735 { /* Timer A */
4736 ym2612_update_req(F2612);
4737 /* timer update */
4738 TimerAOver( &(F2612->OPN.ST) );
4739 /* CSM mode key,TL controll */
4740 if ((F2612->OPN.ST.mode & 0xc0) == 0x80)
4741 { /* CSM mode total level latch and auto key on */
4742 CSMKeyControll( &F2612->OPN, &(F2612->CH[2]) );
4743 }
4744 }
4745 return F2612->OPN.ST.irq;
4746 }
4747
4748
4749 void ym2612_set_mutemask(void *chip, UINT32 MuteMask)
4750 {
4751 YM2612 *F2612 = (YM2612 *)chip;
4752 UINT8 CurChn;
4753
4754 for (CurChn = 0; CurChn < 6; CurChn ++)
4755 F2612->CH[CurChn].Muted = (MuteMask >> CurChn) & 0x01;
4756 F2612->MuteDAC = (MuteMask >> 6) & 0x01;
4757
4758 return;
4759 }
4760
4761 void ym2612_setoptions(void *chip, UINT32 Flags)
4762 {
4763 YM2612 *F2612 = (YM2612 *)chip;
4764 UINT8 PseudoStereo;
4765
4766 PseudoStereo = (Flags >> 2) & 0x01;
4767 F2612->WaveOutMode = (PseudoStereo) ? 0x01 : 0x00;
4768 F2612->OPN.LegacyMode = (Flags >> 7) & 0x01;
4769
4770 return;
4771 }
4772 #endif /* (BUILD_YM2612) */