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