Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/genplus-gx32/core/sound/ym2612.c
2 views
1
/*
2
**
3
** software implementation of Yamaha FM sound generator (YM2612/YM3438)
4
**
5
** Original code (MAME fm.c)
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
** Additional code & fixes by Eke-Eke for Genesis Plus GX
13
**
14
** Huge thanks to Nemesis, most of those fixes came from his tests on Sega Genesis hardware
15
** More informations at http://gendev.spritesmind.net/forum/viewtopic.php?t=386
16
**
17
** TODO:
18
** - better documentation
19
** - BUSY flag emulation
20
*/
21
22
/*
23
** CHANGELOG:
24
**
25
** 01-09-2012 Eke-Eke (Genesis Plus GX):
26
** - removed input clock / output samplerate frequency ratio, chip now always run at (original) internal sample frequency
27
** - removed now uneeded extra bits of precision
28
**
29
** 2006~2012 Eke-Eke (Genesis Plus GX):
30
** - removed unused multichip support
31
** - added YM2612 Context external access functions
32
** - fixed LFO implementation:
33
** .added support for CH3 special mode: fixes various sound effects (birds in Warlock, bug sound in Aladdin...)
34
** .inverted LFO AM waveform: fixes Spider-Man & Venom : Separation Anxiety (intro), California Games (surfing event)
35
** .improved LFO timing accuracy: now updated AFTER sample output, like EG/PG updates, and without any precision loss anymore.
36
** - improved internal timers emulation
37
** - adjusted lowest EG rates increment values
38
** - fixed Attack Rate not being updated in some specific cases (Batman & Robin intro)
39
** - fixed EG behavior when Attack Rate is maximal
40
** - fixed EG behavior when SL=0 (Mega Turrican tracks 03,09...) or/and Key ON occurs at minimal attenuation
41
** - implemented EG output immediate changes on register writes
42
** - fixed YM2612 initial values (after the reset): fixes missing intro in B.O.B
43
** - implemented Detune overflow (Ariel, Comix Zone, Shaq Fu, Spiderman & many other games using GEMS sound engine)
44
** - implemented accurate CSM mode emulation
45
** - implemented accurate SSG-EG emulation (Asterix, Beavis&Butthead, Bubba'n Stix & many other games)
46
** - implemented accurate address/data ports behavior
47
** - added preliminar support for DAC precision
48
**
49
** 03-08-2003 Jarek Burczynski:
50
** - fixed YM2608 initial values (after the reset)
51
** - fixed flag and irqmask handling (YM2608)
52
** - fixed BUFRDY flag handling (YM2608)
53
**
54
** 14-06-2003 Jarek Burczynski:
55
** - implemented all of the YM2608 status register flags
56
** - implemented support for external memory read/write via YM2608
57
** - implemented support for deltat memory limit register in YM2608 emulation
58
**
59
** 22-05-2003 Jarek Burczynski:
60
** - fixed LFO PM calculations (copy&paste bugfix)
61
**
62
** 08-05-2003 Jarek Burczynski:
63
** - fixed SSG support
64
**
65
** 22-04-2003 Jarek Burczynski:
66
** - implemented 100% correct LFO generator (verified on real YM2610 and YM2608)
67
**
68
** 15-04-2003 Jarek Burczynski:
69
** - added support for YM2608's register 0x110 - status mask
70
**
71
** 01-12-2002 Jarek Burczynski:
72
** - fixed register addressing in YM2608, YM2610, YM2610B chips. (verified on real YM2608)
73
** The addressing patch used for early Neo-Geo games can be removed now.
74
**
75
** 26-11-2002 Jarek Burczynski, Nicola Salmoria:
76
** - recreated YM2608 ADPCM ROM using data from real YM2608's output which leads to:
77
** - added emulation of YM2608 drums.
78
** - output of YM2608 is two times lower now - same as YM2610 (verified on real YM2608)
79
**
80
** 16-08-2002 Jarek Burczynski:
81
** - binary exact Envelope Generator (verified on real YM2203);
82
** identical to YM2151
83
** - corrected 'off by one' error in feedback calculations (when feedback is off)
84
** - corrected connection (algorithm) calculation (verified on real YM2203 and YM2610)
85
**
86
** 18-12-2001 Jarek Burczynski:
87
** - added SSG-EG support (verified on real YM2203)
88
**
89
** 12-08-2001 Jarek Burczynski:
90
** - corrected sin_tab and tl_tab data (verified on real chip)
91
** - corrected feedback calculations (verified on real chip)
92
** - corrected phase generator calculations (verified on real chip)
93
** - corrected envelope generator calculations (verified on real chip)
94
** - corrected FM volume level (YM2610 and YM2610B).
95
** - changed YMxxxUpdateOne() functions (YM2203, YM2608, YM2610, YM2610B, YM2612) :
96
** this was needed to calculate YM2610 FM channels output correctly.
97
** (Each FM channel is calculated as in other chips, but the output of the channel
98
** gets shifted right by one *before* sending to accumulator. That was impossible to do
99
** with previous implementation).
100
**
101
** 23-07-2001 Jarek Burczynski, Nicola Salmoria:
102
** - corrected YM2610 ADPCM type A algorithm and tables (verified on real chip)
103
**
104
** 11-06-2001 Jarek Burczynski:
105
** - corrected end of sample bug in ADPCMA_calc_cha().
106
** Real YM2610 checks for equality between current and end addresses (only 20 LSB bits).
107
**
108
** 08-12-98 hiro-shi:
109
** rename ADPCMA -> ADPCMB, ADPCMB -> ADPCMA
110
** move ROM limit check.(CALC_CH? -> 2610Write1/2)
111
** test program (ADPCMB_TEST)
112
** move ADPCM A/B end check.
113
** ADPCMB repeat flag(no check)
114
** change ADPCM volume rate (8->16) (32->48).
115
**
116
** 09-12-98 hiro-shi:
117
** change ADPCM volume. (8->16, 48->64)
118
** replace ym2610 ch0/3 (YM-2610B)
119
** change ADPCM_SHIFT (10->8) missing bank change 0x4000-0xffff.
120
** add ADPCM_SHIFT_MASK
121
** change ADPCMA_DECODE_MIN/MAX.
122
*/
123
124
/************************************************************************/
125
/* comment of hiro-shi(Hiromitsu Shioya) */
126
/* YM2610(B) = OPN-B */
127
/* YM2610 : PSG:3ch FM:4ch ADPCM(18.5KHz):6ch DeltaT ADPCM:1ch */
128
/* YM2610B : PSG:3ch FM:6ch ADPCM(18.5KHz):6ch DeltaT ADPCM:1ch */
129
/************************************************************************/
130
131
#include "shared.h"
132
133
/* envelope generator */
134
#define ENV_BITS 10
135
#define ENV_LEN (1<<ENV_BITS)
136
#define ENV_STEP (128.0/ENV_LEN)
137
138
#define MAX_ATT_INDEX (ENV_LEN-1) /* 1023 */
139
#define MIN_ATT_INDEX (0) /* 0 */
140
141
#define EG_ATT 4
142
#define EG_DEC 3
143
#define EG_SUS 2
144
#define EG_REL 1
145
#define EG_OFF 0
146
147
/* phase generator (detune mask) */
148
#define DT_BITS 17
149
#define DT_LEN (1 << DT_BITS)
150
#define DT_MASK (DT_LEN - 1)
151
152
/* operator unit */
153
#define SIN_BITS 10
154
#define SIN_LEN (1<<SIN_BITS)
155
#define SIN_MASK (SIN_LEN-1)
156
157
#define TL_RES_LEN (256) /* 8 bits addressing (real chip) */
158
159
#define TL_BITS 14 /* channel output */
160
161
/* TL_TAB_LEN is calculated as:
162
* 13 - sinus amplitude bits (Y axis)
163
* 2 - sinus sign bit (Y axis)
164
* TL_RES_LEN - sinus resolution (X axis)
165
*/
166
#define TL_TAB_LEN (13*2*TL_RES_LEN)
167
static signed int tl_tab[TL_TAB_LEN];
168
169
#define ENV_QUIET (TL_TAB_LEN>>3)
170
171
/* sin waveform table in 'decibel' scale */
172
static unsigned int sin_tab[SIN_LEN];
173
174
/* sustain level table (3dB per step) */
175
/* bit0, bit1, bit2, bit3, bit4, bit5, bit6 */
176
/* 1, 2, 4, 8, 16, 32, 64 (value)*/
177
/* 0.75, 1.5, 3, 6, 12, 24, 48 (dB)*/
178
179
/* 0 - 15: 0, 3, 6, 9,12,15,18,21,24,27,30,33,36,39,42,93 (dB)*/
180
/* attenuation value (10 bits) = (SL << 2) << 3 */
181
#define SC(db) (UINT32) ( db * (4.0/ENV_STEP) )
182
static const UINT32 sl_table[16]={
183
SC( 0),SC( 1),SC( 2),SC(3 ),SC(4 ),SC(5 ),SC(6 ),SC( 7),
184
SC( 8),SC( 9),SC(10),SC(11),SC(12),SC(13),SC(14),SC(31)
185
};
186
#undef SC
187
188
189
#define RATE_STEPS (8)
190
static const UINT8 eg_inc[19*RATE_STEPS]={
191
192
/*cycle:0 1 2 3 4 5 6 7*/
193
194
/* 0 */ 0,1, 0,1, 0,1, 0,1, /* rates 00..11 0 (increment by 0 or 1) */
195
/* 1 */ 0,1, 0,1, 1,1, 0,1, /* rates 00..11 1 */
196
/* 2 */ 0,1, 1,1, 0,1, 1,1, /* rates 00..11 2 */
197
/* 3 */ 0,1, 1,1, 1,1, 1,1, /* rates 00..11 3 */
198
199
/* 4 */ 1,1, 1,1, 1,1, 1,1, /* rate 12 0 (increment by 1) */
200
/* 5 */ 1,1, 1,2, 1,1, 1,2, /* rate 12 1 */
201
/* 6 */ 1,2, 1,2, 1,2, 1,2, /* rate 12 2 */
202
/* 7 */ 1,2, 2,2, 1,2, 2,2, /* rate 12 3 */
203
204
/* 8 */ 2,2, 2,2, 2,2, 2,2, /* rate 13 0 (increment by 2) */
205
/* 9 */ 2,2, 2,4, 2,2, 2,4, /* rate 13 1 */
206
/*10 */ 2,4, 2,4, 2,4, 2,4, /* rate 13 2 */
207
/*11 */ 2,4, 4,4, 2,4, 4,4, /* rate 13 3 */
208
209
/*12 */ 4,4, 4,4, 4,4, 4,4, /* rate 14 0 (increment by 4) */
210
/*13 */ 4,4, 4,8, 4,4, 4,8, /* rate 14 1 */
211
/*14 */ 4,8, 4,8, 4,8, 4,8, /* rate 14 2 */
212
/*15 */ 4,8, 8,8, 4,8, 8,8, /* rate 14 3 */
213
214
/*16 */ 8,8, 8,8, 8,8, 8,8, /* rates 15 0, 15 1, 15 2, 15 3 (increment by 8) */
215
/*17 */ 16,16,16,16,16,16,16,16, /* rates 15 2, 15 3 for attack */
216
/*18 */ 0,0, 0,0, 0,0, 0,0, /* infinity rates for attack and decay(s) */
217
};
218
219
220
#define O(a) (a*RATE_STEPS)
221
222
/*note that there is no O(17) in this table - it's directly in the code */
223
static const UINT8 eg_rate_select[32+64+32]={ /* Envelope Generator rates (32 + 64 rates + 32 RKS) */
224
/* 32 infinite time rates (same as Rate 0) */
225
O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),
226
O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),
227
O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),
228
O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),
229
230
/* rates 00-11 */
231
/*
232
O( 0),O( 1),O( 2),O( 3),
233
O( 0),O( 1),O( 2),O( 3),
234
*/
235
O(18),O(18),O( 0),O( 0),
236
O( 0),O( 0),O( 2),O( 2), /* Nemesis's tests */
237
238
O( 0),O( 1),O( 2),O( 3),
239
O( 0),O( 1),O( 2),O( 3),
240
O( 0),O( 1),O( 2),O( 3),
241
O( 0),O( 1),O( 2),O( 3),
242
O( 0),O( 1),O( 2),O( 3),
243
O( 0),O( 1),O( 2),O( 3),
244
O( 0),O( 1),O( 2),O( 3),
245
O( 0),O( 1),O( 2),O( 3),
246
O( 0),O( 1),O( 2),O( 3),
247
O( 0),O( 1),O( 2),O( 3),
248
249
/* rate 12 */
250
O( 4),O( 5),O( 6),O( 7),
251
252
/* rate 13 */
253
O( 8),O( 9),O(10),O(11),
254
255
/* rate 14 */
256
O(12),O(13),O(14),O(15),
257
258
/* rate 15 */
259
O(16),O(16),O(16),O(16),
260
261
/* 32 dummy rates (same as 15 3) */
262
O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16),
263
O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16),
264
O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16),
265
O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16)
266
267
};
268
#undef O
269
270
/*rate 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15*/
271
/*shift 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0 */
272
/*mask 2047, 1023, 511, 255, 127, 63, 31, 15, 7, 3, 1, 0, 0, 0, 0, 0 */
273
274
#define O(a) (a*1)
275
static const UINT8 eg_rate_shift[32+64+32]={ /* Envelope Generator counter shifts (32 + 64 rates + 32 RKS) */
276
/* 32 infinite time rates */
277
/* O(0),O(0),O(0),O(0),O(0),O(0),O(0),O(0),
278
O(0),O(0),O(0),O(0),O(0),O(0),O(0),O(0),
279
O(0),O(0),O(0),O(0),O(0),O(0),O(0),O(0),
280
O(0),O(0),O(0),O(0),O(0),O(0),O(0),O(0), */
281
282
/* fixed (should be the same as rate 0, even if it makes no difference since increment value is 0 for these rates) */
283
O(11),O(11),O(11),O(11),O(11),O(11),O(11),O(11),
284
O(11),O(11),O(11),O(11),O(11),O(11),O(11),O(11),
285
O(11),O(11),O(11),O(11),O(11),O(11),O(11),O(11),
286
O(11),O(11),O(11),O(11),O(11),O(11),O(11),O(11),
287
288
/* rates 00-11 */
289
O(11),O(11),O(11),O(11),
290
O(10),O(10),O(10),O(10),
291
O( 9),O( 9),O( 9),O( 9),
292
O( 8),O( 8),O( 8),O( 8),
293
O( 7),O( 7),O( 7),O( 7),
294
O( 6),O( 6),O( 6),O( 6),
295
O( 5),O( 5),O( 5),O( 5),
296
O( 4),O( 4),O( 4),O( 4),
297
O( 3),O( 3),O( 3),O( 3),
298
O( 2),O( 2),O( 2),O( 2),
299
O( 1),O( 1),O( 1),O( 1),
300
O( 0),O( 0),O( 0),O( 0),
301
302
/* rate 12 */
303
O( 0),O( 0),O( 0),O( 0),
304
305
/* rate 13 */
306
O( 0),O( 0),O( 0),O( 0),
307
308
/* rate 14 */
309
O( 0),O( 0),O( 0),O( 0),
310
311
/* rate 15 */
312
O( 0),O( 0),O( 0),O( 0),
313
314
/* 32 dummy rates (same as 15 3) */
315
O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),
316
O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),
317
O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),
318
O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0)
319
320
};
321
#undef O
322
323
static const UINT8 dt_tab[4 * 32]={
324
/* this is YM2151 and YM2612 phase increment data (in 10.10 fixed point format)*/
325
/* FD=0 */
326
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
327
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
328
/* FD=1 */
329
0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2,
330
2, 3, 3, 3, 4, 4, 4, 5, 5, 6, 6, 7, 8, 8, 8, 8,
331
/* FD=2 */
332
1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5,
333
5, 6, 6, 7, 8, 8, 9,10,11,12,13,14,16,16,16,16,
334
/* FD=3 */
335
2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 6, 6, 7,
336
8 , 8, 9,10,11,12,13,14,16,17,19,20,22,22,22,22
337
};
338
339
340
/* OPN key frequency number -> key code follow table */
341
/* fnum higher 4bit -> keycode lower 2bit */
342
static const UINT8 opn_fktable[16] = {0,0,0,0,0,0,0,1,2,3,3,3,3,3,3,3};
343
344
345
/* 8 LFO speed parameters */
346
/* each value represents number of samples that one LFO level will last for */
347
static const UINT32 lfo_samples_per_step[8] = {108, 77, 71, 67, 62, 44, 8, 5};
348
349
350
/*There are 4 different LFO AM depths available, they are:
351
0 dB, 1.4 dB, 5.9 dB, 11.8 dB
352
Here is how it is generated (in EG steps):
353
354
11.8 dB = 0, 2, 4, 6, 8, 10,12,14,16...126,126,124,122,120,118,....4,2,0
355
5.9 dB = 0, 1, 2, 3, 4, 5, 6, 7, 8....63, 63, 62, 61, 60, 59,.....2,1,0
356
1.4 dB = 0, 0, 0, 0, 1, 1, 1, 1, 2,...15, 15, 15, 15, 14, 14,.....0,0,0
357
358
(1.4 dB is loosing precision as you can see)
359
360
It's implemented as generator from 0..126 with step 2 then a shift
361
right N times, where N is:
362
8 for 0 dB
363
3 for 1.4 dB
364
1 for 5.9 dB
365
0 for 11.8 dB
366
*/
367
static const UINT8 lfo_ams_depth_shift[4] = {8, 3, 1, 0};
368
369
370
371
/*There are 8 different LFO PM depths available, they are:
372
0, 3.4, 6.7, 10, 14, 20, 40, 80 (cents)
373
374
Modulation level at each depth depends on F-NUMBER bits: 4,5,6,7,8,9,10
375
(bits 8,9,10 = FNUM MSB from OCT/FNUM register)
376
377
Here we store only first quarter (positive one) of full waveform.
378
Full table (lfo_pm_table) containing all 128 waveforms is build
379
at run (init) time.
380
381
One value in table below represents 4 (four) basic LFO steps
382
(1 PM step = 4 AM steps).
383
384
For example:
385
at LFO SPEED=0 (which is 108 samples per basic LFO step)
386
one value from "lfo_pm_output" table lasts for 432 consecutive
387
samples (4*108=432) and one full LFO waveform cycle lasts for 13824
388
samples (32*432=13824; 32 because we store only a quarter of whole
389
waveform in the table below)
390
*/
391
static const UINT8 lfo_pm_output[7*8][8]={
392
/* 7 bits meaningful (of F-NUMBER), 8 LFO output levels per one depth (out of 32), 8 LFO depths */
393
/* FNUM BIT 4: 000 0001xxxx */
394
/* DEPTH 0 */ {0, 0, 0, 0, 0, 0, 0, 0},
395
/* DEPTH 1 */ {0, 0, 0, 0, 0, 0, 0, 0},
396
/* DEPTH 2 */ {0, 0, 0, 0, 0, 0, 0, 0},
397
/* DEPTH 3 */ {0, 0, 0, 0, 0, 0, 0, 0},
398
/* DEPTH 4 */ {0, 0, 0, 0, 0, 0, 0, 0},
399
/* DEPTH 5 */ {0, 0, 0, 0, 0, 0, 0, 0},
400
/* DEPTH 6 */ {0, 0, 0, 0, 0, 0, 0, 0},
401
/* DEPTH 7 */ {0, 0, 0, 0, 1, 1, 1, 1},
402
403
/* FNUM BIT 5: 000 0010xxxx */
404
/* DEPTH 0 */ {0, 0, 0, 0, 0, 0, 0, 0},
405
/* DEPTH 1 */ {0, 0, 0, 0, 0, 0, 0, 0},
406
/* DEPTH 2 */ {0, 0, 0, 0, 0, 0, 0, 0},
407
/* DEPTH 3 */ {0, 0, 0, 0, 0, 0, 0, 0},
408
/* DEPTH 4 */ {0, 0, 0, 0, 0, 0, 0, 0},
409
/* DEPTH 5 */ {0, 0, 0, 0, 0, 0, 0, 0},
410
/* DEPTH 6 */ {0, 0, 0, 0, 1, 1, 1, 1},
411
/* DEPTH 7 */ {0, 0, 1, 1, 2, 2, 2, 3},
412
413
/* FNUM BIT 6: 000 0100xxxx */
414
/* DEPTH 0 */ {0, 0, 0, 0, 0, 0, 0, 0},
415
/* DEPTH 1 */ {0, 0, 0, 0, 0, 0, 0, 0},
416
/* DEPTH 2 */ {0, 0, 0, 0, 0, 0, 0, 0},
417
/* DEPTH 3 */ {0, 0, 0, 0, 0, 0, 0, 0},
418
/* DEPTH 4 */ {0, 0, 0, 0, 0, 0, 0, 1},
419
/* DEPTH 5 */ {0, 0, 0, 0, 1, 1, 1, 1},
420
/* DEPTH 6 */ {0, 0, 1, 1, 2, 2, 2, 3},
421
/* DEPTH 7 */ {0, 0, 2, 3, 4, 4, 5, 6},
422
423
/* FNUM BIT 7: 000 1000xxxx */
424
/* DEPTH 0 */ {0, 0, 0, 0, 0, 0, 0, 0},
425
/* DEPTH 1 */ {0, 0, 0, 0, 0, 0, 0, 0},
426
/* DEPTH 2 */ {0, 0, 0, 0, 0, 0, 1, 1},
427
/* DEPTH 3 */ {0, 0, 0, 0, 1, 1, 1, 1},
428
/* DEPTH 4 */ {0, 0, 0, 1, 1, 1, 1, 2},
429
/* DEPTH 5 */ {0, 0, 1, 1, 2, 2, 2, 3},
430
/* DEPTH 6 */ {0, 0, 2, 3, 4, 4, 5, 6},
431
/* DEPTH 7 */ {0, 0, 4, 6, 8, 8, 0xa, 0xc},
432
433
/* FNUM BIT 8: 001 0000xxxx */
434
/* DEPTH 0 */ {0, 0, 0, 0, 0, 0, 0, 0},
435
/* DEPTH 1 */ {0, 0, 0, 0, 1, 1, 1, 1},
436
/* DEPTH 2 */ {0, 0, 0, 1, 1, 1, 2, 2},
437
/* DEPTH 3 */ {0, 0, 1, 1, 2, 2, 3, 3},
438
/* DEPTH 4 */ {0, 0, 1, 2, 2, 2, 3, 4},
439
/* DEPTH 5 */ {0, 0, 2, 3, 4, 4, 5, 6},
440
/* DEPTH 6 */ {0, 0, 4, 6, 8, 8, 0xa, 0xc},
441
/* DEPTH 7 */ {0, 0, 8, 0xc,0x10,0x10,0x14,0x18},
442
443
/* FNUM BIT 9: 010 0000xxxx */
444
/* DEPTH 0 */ {0, 0, 0, 0, 0, 0, 0, 0},
445
/* DEPTH 1 */ {0, 0, 0, 0, 2, 2, 2, 2},
446
/* DEPTH 2 */ {0, 0, 0, 2, 2, 2, 4, 4},
447
/* DEPTH 3 */ {0, 0, 2, 2, 4, 4, 6, 6},
448
/* DEPTH 4 */ {0, 0, 2, 4, 4, 4, 6, 8},
449
/* DEPTH 5 */ {0, 0, 4, 6, 8, 8, 0xa, 0xc},
450
/* DEPTH 6 */ {0, 0, 8, 0xc,0x10,0x10,0x14,0x18},
451
/* DEPTH 7 */ {0, 0,0x10,0x18,0x20,0x20,0x28,0x30},
452
453
/* FNUM BIT10: 100 0000xxxx */
454
/* DEPTH 0 */ {0, 0, 0, 0, 0, 0, 0, 0},
455
/* DEPTH 1 */ {0, 0, 0, 0, 4, 4, 4, 4},
456
/* DEPTH 2 */ {0, 0, 0, 4, 4, 4, 8, 8},
457
/* DEPTH 3 */ {0, 0, 4, 4, 8, 8, 0xc, 0xc},
458
/* DEPTH 4 */ {0, 0, 4, 8, 8, 8, 0xc,0x10},
459
/* DEPTH 5 */ {0, 0, 8, 0xc,0x10,0x10,0x14,0x18},
460
/* DEPTH 6 */ {0, 0,0x10,0x18,0x20,0x20,0x28,0x30},
461
/* DEPTH 7 */ {0, 0,0x20,0x30,0x40,0x40,0x50,0x60},
462
463
};
464
465
/* all 128 LFO PM waveforms */
466
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 */
467
468
/* register number to channel number , slot offset */
469
#define OPN_CHAN(N) (N&3)
470
#define OPN_SLOT(N) ((N>>2)&3)
471
472
/* slot number */
473
#define SLOT1 0
474
#define SLOT2 2
475
#define SLOT3 1
476
#define SLOT4 3
477
478
/* struct describing a single operator (SLOT) */
479
typedef struct
480
{
481
INT32 *DT; /* detune :dt_tab[DT] */
482
UINT8 KSR; /* key scale rate :3-KSR */
483
UINT32 ar; /* attack rate */
484
UINT32 d1r; /* decay rate */
485
UINT32 d2r; /* sustain rate */
486
UINT32 rr; /* release rate */
487
UINT8 ksr; /* key scale rate :kcode>>(3-KSR) */
488
UINT32 mul; /* multiple :ML_TABLE[ML] */
489
490
/* Phase Generator */
491
UINT32 phase; /* phase counter */
492
INT32 Incr; /* phase step */
493
494
/* Envelope Generator */
495
UINT8 state; /* phase type */
496
UINT32 tl; /* total level: TL << 3 */
497
INT32 volume; /* envelope counter */
498
UINT32 sl; /* sustain level:sl_table[SL] */
499
UINT32 vol_out; /* current output from EG circuit (without AM from LFO) */
500
501
UINT8 eg_sh_ar; /* (attack state) */
502
UINT8 eg_sel_ar; /* (attack state) */
503
UINT8 eg_sh_d1r; /* (decay state) */
504
UINT8 eg_sel_d1r; /* (decay state) */
505
UINT8 eg_sh_d2r; /* (sustain state) */
506
UINT8 eg_sel_d2r; /* (sustain state) */
507
UINT8 eg_sh_rr; /* (release state) */
508
UINT8 eg_sel_rr; /* (release state) */
509
510
UINT8 ssg; /* SSG-EG waveform */
511
UINT8 ssgn; /* SSG-EG negated output */
512
513
UINT8 key; /* 0=last key was KEY OFF, 1=KEY ON */
514
515
/* LFO */
516
UINT32 AMmask; /* AM enable flag */
517
518
} FM_SLOT;
519
520
typedef struct
521
{
522
FM_SLOT SLOT[4]; /* four SLOTs (operators) */
523
524
UINT8 ALGO; /* algorithm */
525
UINT8 FB; /* feedback shift */
526
INT32 op1_out[2]; /* op1 output for feedback */
527
528
INT32 *connect1; /* SLOT1 output pointer */
529
INT32 *connect3; /* SLOT3 output pointer */
530
INT32 *connect2; /* SLOT2 output pointer */
531
INT32 *connect4; /* SLOT4 output pointer */
532
533
INT32 *mem_connect; /* where to put the delayed sample (MEM) */
534
INT32 mem_value; /* delayed sample (MEM) value */
535
536
INT32 pms; /* channel PMS */
537
UINT8 ams; /* channel AMS */
538
539
UINT32 fc; /* fnum,blk */
540
UINT8 kcode; /* key code */
541
UINT32 block_fnum; /* blk/fnum value (for LFO PM calculations) */
542
} FM_CH;
543
544
545
typedef struct
546
{
547
UINT16 address; /* address register */
548
UINT8 status; /* status flag */
549
UINT32 mode; /* mode CSM / 3SLOT */
550
UINT8 fn_h; /* freq latch */
551
INT32 TA; /* timer a value */
552
INT32 TAL; /* timer a base */
553
INT32 TAC; /* timer a counter */
554
INT32 TB; /* timer b value */
555
INT32 TBL; /* timer b base */
556
INT32 TBC; /* timer b counter */
557
INT32 dt_tab[8][32]; /* DeTune table */
558
559
} FM_ST;
560
561
562
/***********************************************************/
563
/* OPN unit */
564
/***********************************************************/
565
566
/* OPN 3slot struct */
567
typedef struct
568
{
569
UINT32 fc[3]; /* fnum3,blk3: calculated */
570
UINT8 fn_h; /* freq3 latch */
571
UINT8 kcode[3]; /* key code */
572
UINT32 block_fnum[3]; /* current fnum value for this slot (can be different betweeen slots of one channel in 3slot mode) */
573
UINT8 key_csm; /* CSM mode Key-ON flag */
574
575
} FM_3SLOT;
576
577
/* OPN/A/B common state */
578
typedef struct
579
{
580
FM_ST ST; /* general state */
581
FM_3SLOT SL3; /* 3 slot mode state */
582
unsigned int pan[6*2]; /* fm channels output masks (0xffffffff = enable) */
583
584
/* EG */
585
UINT32 eg_cnt; /* global envelope generator counter */
586
UINT32 eg_timer; /* global envelope generator counter works at frequency = chipclock/144/3 */
587
588
/* LFO */
589
UINT8 lfo_cnt; /* current LFO phase (out of 128) */
590
UINT32 lfo_timer; /* current LFO phase runs at LFO frequency */
591
UINT32 lfo_timer_overflow; /* LFO timer overflows every N samples (depends on LFO frequency) */
592
UINT32 LFO_AM; /* current LFO AM step */
593
UINT32 LFO_PM; /* current LFO PM step */
594
595
} FM_OPN;
596
597
/***********************************************************/
598
/* YM2612 chip */
599
/***********************************************************/
600
typedef struct
601
{
602
FM_CH CH[6]; /* channel state */
603
UINT8 dacen; /* DAC mode */
604
INT32 dacout; /* DAC output */
605
FM_OPN OPN; /* OPN state */
606
607
} YM2612;
608
609
/* emulated chip */
610
YM2612 ym2612;
611
612
/* current chip state */
613
INT32 m2,c1,c2; /* Phase Modulation input for operators 2,3,4 */
614
INT32 mem; /* one sample delay memory */
615
INT32 out_fm[8]; /* outputs of working channels */
616
UINT32 bitmask; /* working channels output bitmasking (DAC quantization) */
617
618
619
INLINE void FM_KEYON(FM_CH *CH , int s )
620
{
621
FM_SLOT *SLOT = &CH->SLOT[s];
622
623
if (!SLOT->key && !ym2612.OPN.SL3.key_csm)
624
{
625
/* restart Phase Generator */
626
SLOT->phase = 0;
627
628
/* reset SSG-EG inversion flag */
629
SLOT->ssgn = 0;
630
631
if ((SLOT->ar + SLOT->ksr) < 94 /*32+62*/)
632
{
633
SLOT->state = (SLOT->volume <= MIN_ATT_INDEX) ? ((SLOT->sl == MIN_ATT_INDEX) ? EG_SUS : EG_DEC) : EG_ATT;
634
}
635
else
636
{
637
/* force attenuation level to 0 */
638
SLOT->volume = MIN_ATT_INDEX;
639
640
/* directly switch to Decay (or Sustain) */
641
SLOT->state = (SLOT->sl == MIN_ATT_INDEX) ? EG_SUS : EG_DEC;
642
}
643
644
/* recalculate EG output */
645
if ((SLOT->ssg&0x08) && (SLOT->ssgn ^ (SLOT->ssg&0x04)))
646
SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;
647
else
648
SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
649
}
650
651
SLOT->key = 1;
652
}
653
654
INLINE void FM_KEYOFF(FM_CH *CH , int s )
655
{
656
FM_SLOT *SLOT = &CH->SLOT[s];
657
658
if (SLOT->key && !ym2612.OPN.SL3.key_csm)
659
{
660
if (SLOT->state>EG_REL)
661
{
662
SLOT->state = EG_REL; /* phase -> Release */
663
664
/* SSG-EG specific update */
665
if (SLOT->ssg&0x08)
666
{
667
/* convert EG attenuation level */
668
if (SLOT->ssgn ^ (SLOT->ssg&0x04))
669
SLOT->volume = (0x200 - SLOT->volume);
670
671
/* force EG attenuation level */
672
if (SLOT->volume >= 0x200)
673
{
674
SLOT->volume = MAX_ATT_INDEX;
675
SLOT->state = EG_OFF;
676
}
677
678
/* recalculate EG output */
679
SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
680
}
681
}
682
}
683
684
SLOT->key = 0;
685
}
686
687
INLINE void FM_KEYON_CSM(FM_CH *CH , int s )
688
{
689
FM_SLOT *SLOT = &CH->SLOT[s];
690
691
if (!SLOT->key && !ym2612.OPN.SL3.key_csm)
692
{
693
/* restart Phase Generator */
694
SLOT->phase = 0;
695
696
/* reset SSG-EG inversion flag */
697
SLOT->ssgn = 0;
698
699
if ((SLOT->ar + SLOT->ksr) < 94 /*32+62*/)
700
{
701
SLOT->state = (SLOT->volume <= MIN_ATT_INDEX) ? ((SLOT->sl == MIN_ATT_INDEX) ? EG_SUS : EG_DEC) : EG_ATT;
702
}
703
else
704
{
705
/* force attenuation level to 0 */
706
SLOT->volume = MIN_ATT_INDEX;
707
708
/* directly switch to Decay (or Sustain) */
709
SLOT->state = (SLOT->sl == MIN_ATT_INDEX) ? EG_SUS : EG_DEC;
710
}
711
712
/* recalculate EG output */
713
if ((SLOT->ssg&0x08) && (SLOT->ssgn ^ (SLOT->ssg&0x04)))
714
SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;
715
else
716
SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
717
}
718
}
719
720
INLINE void FM_KEYOFF_CSM(FM_CH *CH , int s )
721
{
722
FM_SLOT *SLOT = &CH->SLOT[s];
723
if (!SLOT->key)
724
{
725
if (SLOT->state>EG_REL)
726
{
727
SLOT->state = EG_REL; /* phase -> Release */
728
729
/* SSG-EG specific update */
730
if (SLOT->ssg&0x08)
731
{
732
/* convert EG attenuation level */
733
if (SLOT->ssgn ^ (SLOT->ssg&0x04))
734
SLOT->volume = (0x200 - SLOT->volume);
735
736
/* force EG attenuation level */
737
if (SLOT->volume >= 0x200)
738
{
739
SLOT->volume = MAX_ATT_INDEX;
740
SLOT->state = EG_OFF;
741
}
742
743
/* recalculate EG output */
744
SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
745
}
746
}
747
}
748
}
749
750
/* CSM Key Controll */
751
INLINE void CSMKeyControll(FM_CH *CH)
752
{
753
/* all key ON (verified by Nemesis on real hardware) */
754
FM_KEYON_CSM(CH,SLOT1);
755
FM_KEYON_CSM(CH,SLOT2);
756
FM_KEYON_CSM(CH,SLOT3);
757
FM_KEYON_CSM(CH,SLOT4);
758
ym2612.OPN.SL3.key_csm = 1;
759
}
760
761
INLINE void INTERNAL_TIMER_A()
762
{
763
if (ym2612.OPN.ST.mode & 0x01)
764
{
765
ym2612.OPN.ST.TAC--;
766
if (ym2612.OPN.ST.TAC <= 0)
767
{
768
/* set status (if enabled) */
769
if (ym2612.OPN.ST.mode & 0x04)
770
ym2612.OPN.ST.status |= 0x01;
771
772
/* reload the counter */
773
ym2612.OPN.ST.TAC = ym2612.OPN.ST.TAL;
774
775
/* CSM mode auto key on */
776
if ((ym2612.OPN.ST.mode & 0xC0) == 0x80)
777
CSMKeyControll(&ym2612.CH[2]);
778
}
779
}
780
}
781
782
INLINE void INTERNAL_TIMER_B(int step)
783
{
784
if (ym2612.OPN.ST.mode & 0x02)
785
{
786
ym2612.OPN.ST.TBC-=step;
787
if (ym2612.OPN.ST.TBC <= 0)
788
{
789
/* set status (if enabled) */
790
if (ym2612.OPN.ST.mode & 0x08)
791
ym2612.OPN.ST.status |= 0x02;
792
793
/* reload the counter */
794
if (ym2612.OPN.ST.TBL)
795
ym2612.OPN.ST.TBC += ym2612.OPN.ST.TBL;
796
else
797
ym2612.OPN.ST.TBC = ym2612.OPN.ST.TBL;
798
}
799
}
800
}
801
802
/* OPN Mode Register Write */
803
INLINE void set_timers(int v )
804
{
805
/* b7 = CSM MODE */
806
/* b6 = 3 slot mode */
807
/* b5 = reset b */
808
/* b4 = reset a */
809
/* b3 = timer enable b */
810
/* b2 = timer enable a */
811
/* b1 = load b */
812
/* b0 = load a */
813
814
if ((ym2612.OPN.ST.mode ^ v) & 0xC0)
815
{
816
/* phase increment need to be recalculated */
817
ym2612.CH[2].SLOT[SLOT1].Incr=-1;
818
819
/* CSM mode disabled and CSM key ON active*/
820
if (((v & 0xC0) != 0x80) && ym2612.OPN.SL3.key_csm)
821
{
822
/* CSM Mode Key OFF (verified by Nemesis on real hardware) */
823
FM_KEYOFF_CSM(&ym2612.CH[2],SLOT1);
824
FM_KEYOFF_CSM(&ym2612.CH[2],SLOT2);
825
FM_KEYOFF_CSM(&ym2612.CH[2],SLOT3);
826
FM_KEYOFF_CSM(&ym2612.CH[2],SLOT4);
827
ym2612.OPN.SL3.key_csm = 0;
828
}
829
}
830
831
/* reload Timers */
832
if ((v&1) && !(ym2612.OPN.ST.mode&1))
833
ym2612.OPN.ST.TAC = ym2612.OPN.ST.TAL;
834
if ((v&2) && !(ym2612.OPN.ST.mode&2))
835
ym2612.OPN.ST.TBC = ym2612.OPN.ST.TBL;
836
837
/* reset Timers flags */
838
ym2612.OPN.ST.status &= (~v >> 4);
839
840
ym2612.OPN.ST.mode = v;
841
}
842
843
/* set algorithm connection */
844
INLINE void setup_connection( FM_CH *CH, int ch )
845
{
846
INT32 *carrier = &out_fm[ch];
847
848
INT32 **om1 = &CH->connect1;
849
INT32 **om2 = &CH->connect3;
850
INT32 **oc1 = &CH->connect2;
851
852
INT32 **memc = &CH->mem_connect;
853
854
switch( CH->ALGO ){
855
case 0:
856
/* M1---C1---MEM---M2---C2---OUT */
857
*om1 = &c1;
858
*oc1 = &mem;
859
*om2 = &c2;
860
*memc= &m2;
861
break;
862
case 1:
863
/* M1------+-MEM---M2---C2---OUT */
864
/* C1-+ */
865
*om1 = &mem;
866
*oc1 = &mem;
867
*om2 = &c2;
868
*memc= &m2;
869
break;
870
case 2:
871
/* M1-----------------+-C2---OUT */
872
/* C1---MEM---M2-+ */
873
*om1 = &c2;
874
*oc1 = &mem;
875
*om2 = &c2;
876
*memc= &m2;
877
break;
878
case 3:
879
/* M1---C1---MEM------+-C2---OUT */
880
/* M2-+ */
881
*om1 = &c1;
882
*oc1 = &mem;
883
*om2 = &c2;
884
*memc= &c2;
885
break;
886
case 4:
887
/* M1---C1-+-OUT */
888
/* M2---C2-+ */
889
/* MEM: not used */
890
*om1 = &c1;
891
*oc1 = carrier;
892
*om2 = &c2;
893
*memc= &mem; /* store it anywhere where it will not be used */
894
break;
895
case 5:
896
/* +----C1----+ */
897
/* M1-+-MEM---M2-+-OUT */
898
/* +----C2----+ */
899
*om1 = 0; /* special mark */
900
*oc1 = carrier;
901
*om2 = carrier;
902
*memc= &m2;
903
break;
904
case 6:
905
/* M1---C1-+ */
906
/* M2-+-OUT */
907
/* C2-+ */
908
/* MEM: not used */
909
*om1 = &c1;
910
*oc1 = carrier;
911
*om2 = carrier;
912
*memc= &mem; /* store it anywhere where it will not be used */
913
break;
914
case 7:
915
/* M1-+ */
916
/* C1-+-OUT */
917
/* M2-+ */
918
/* C2-+ */
919
/* MEM: not used*/
920
*om1 = carrier;
921
*oc1 = carrier;
922
*om2 = carrier;
923
*memc= &mem; /* store it anywhere where it will not be used */
924
break;
925
}
926
927
CH->connect4 = carrier;
928
}
929
930
/* set detune & multiple */
931
INLINE void set_det_mul(FM_CH *CH,FM_SLOT *SLOT,int v)
932
{
933
SLOT->mul = (v&0x0f)? (v&0x0f)*2 : 1;
934
SLOT->DT = ym2612.OPN.ST.dt_tab[(v>>4)&7];
935
CH->SLOT[SLOT1].Incr=-1;
936
}
937
938
/* set total level */
939
INLINE void set_tl(FM_SLOT *SLOT , int v)
940
{
941
SLOT->tl = (v&0x7f)<<(ENV_BITS-7); /* 7bit TL */
942
943
/* recalculate EG output */
944
if ((SLOT->ssg&0x08) && (SLOT->ssgn ^ (SLOT->ssg&0x04)) && (SLOT->state > EG_REL))
945
SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;
946
else
947
SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
948
}
949
950
/* set attack rate & key scale */
951
INLINE void set_ar_ksr(FM_CH *CH,FM_SLOT *SLOT,int v)
952
{
953
UINT8 old_KSR = SLOT->KSR;
954
955
SLOT->ar = (v&0x1f) ? 32 + ((v&0x1f)<<1) : 0;
956
957
SLOT->KSR = 3-(v>>6);
958
if (SLOT->KSR != old_KSR)
959
{
960
CH->SLOT[SLOT1].Incr=-1;
961
}
962
963
/* Even if it seems unnecessary to do it here, it could happen that KSR and KC */
964
/* are modified but the resulted SLOT->ksr value (kc >> SLOT->KSR) remains unchanged. */
965
/* In such case, Attack Rate would not be recalculated by "refresh_fc_eg_slot". */
966
/* This actually fixes the intro of "The Adventures of Batman & Robin" (Eke-Eke) */
967
if ((SLOT->ar + SLOT->ksr) < (32+62))
968
{
969
SLOT->eg_sh_ar = eg_rate_shift [SLOT->ar + SLOT->ksr ];
970
SLOT->eg_sel_ar = eg_rate_select[SLOT->ar + SLOT->ksr ];
971
}
972
else
973
{
974
/* verified by Nemesis on real hardware (Attack phase is blocked) */
975
SLOT->eg_sh_ar = 0;
976
SLOT->eg_sel_ar = 18*RATE_STEPS;
977
}
978
}
979
980
/* set decay rate */
981
INLINE void set_dr(FM_SLOT *SLOT,int v)
982
{
983
SLOT->d1r = (v&0x1f) ? 32 + ((v&0x1f)<<1) : 0;
984
985
SLOT->eg_sh_d1r = eg_rate_shift [SLOT->d1r + SLOT->ksr];
986
SLOT->eg_sel_d1r= eg_rate_select[SLOT->d1r + SLOT->ksr];
987
988
}
989
990
/* set sustain rate */
991
INLINE void set_sr(FM_SLOT *SLOT,int v)
992
{
993
SLOT->d2r = (v&0x1f) ? 32 + ((v&0x1f)<<1) : 0;
994
995
SLOT->eg_sh_d2r = eg_rate_shift [SLOT->d2r + SLOT->ksr];
996
SLOT->eg_sel_d2r= eg_rate_select[SLOT->d2r + SLOT->ksr];
997
}
998
999
/* set release rate */
1000
INLINE void set_sl_rr(FM_SLOT *SLOT,int v)
1001
{
1002
SLOT->sl = sl_table[ v>>4 ];
1003
1004
/* check EG state changes */
1005
if ((SLOT->state == EG_DEC) && (SLOT->volume >= (INT32)(SLOT->sl)))
1006
SLOT->state = EG_SUS;
1007
1008
SLOT->rr = 34 + ((v&0x0f)<<2);
1009
1010
SLOT->eg_sh_rr = eg_rate_shift [SLOT->rr + SLOT->ksr];
1011
SLOT->eg_sel_rr = eg_rate_select[SLOT->rr + SLOT->ksr];
1012
}
1013
1014
/* advance LFO to next sample */
1015
INLINE void advance_lfo()
1016
{
1017
if (ym2612.OPN.lfo_timer_overflow) /* LFO enabled ? */
1018
{
1019
/* increment LFO timer (every samples) */
1020
ym2612.OPN.lfo_timer ++;
1021
1022
/* when LFO is enabled, one level will last for 108, 77, 71, 67, 62, 44, 8 or 5 samples */
1023
if (ym2612.OPN.lfo_timer >= ym2612.OPN.lfo_timer_overflow)
1024
{
1025
ym2612.OPN.lfo_timer = 0;
1026
1027
/* There are 128 LFO steps */
1028
ym2612.OPN.lfo_cnt = ( ym2612.OPN.lfo_cnt + 1 ) & 127;
1029
1030
/* triangle (inverted) */
1031
/* AM: from 126 to 0 step -2, 0 to 126 step +2 */
1032
if (ym2612.OPN.lfo_cnt<64)
1033
ym2612.OPN.LFO_AM = (ym2612.OPN.lfo_cnt ^ 63) << 1;
1034
else
1035
ym2612.OPN.LFO_AM = (ym2612.OPN.lfo_cnt & 63) << 1;
1036
1037
/* PM works with 4 times slower clock */
1038
ym2612.OPN.LFO_PM = ym2612.OPN.lfo_cnt >> 2;
1039
}
1040
}
1041
}
1042
1043
1044
INLINE void advance_eg_channels(FM_CH *CH, unsigned int eg_cnt)
1045
{
1046
unsigned int i = 6; /* six channels */
1047
unsigned int j;
1048
FM_SLOT *SLOT;
1049
1050
do
1051
{
1052
SLOT = &CH->SLOT[SLOT1];
1053
j = 4; /* four operators per channel */
1054
do
1055
{
1056
switch(SLOT->state)
1057
{
1058
case EG_ATT: /* attack phase */
1059
{
1060
if (!(eg_cnt & ((1<<SLOT->eg_sh_ar)-1)))
1061
{
1062
/* update attenuation level */
1063
SLOT->volume += (~SLOT->volume * (eg_inc[SLOT->eg_sel_ar + ((eg_cnt>>SLOT->eg_sh_ar)&7)]))>>4;
1064
1065
/* check phase transition*/
1066
if (SLOT->volume <= MIN_ATT_INDEX)
1067
{
1068
SLOT->volume = MIN_ATT_INDEX;
1069
SLOT->state = (SLOT->sl == MIN_ATT_INDEX) ? EG_SUS : EG_DEC; /* special case where SL=0 */
1070
}
1071
1072
/* recalculate EG output */
1073
if ((SLOT->ssg&0x08) && (SLOT->ssgn ^ (SLOT->ssg&0x04))) /* SSG-EG Output Inversion */
1074
SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;
1075
else
1076
SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
1077
}
1078
break;
1079
}
1080
1081
case EG_DEC: /* decay phase */
1082
{
1083
if (!(eg_cnt & ((1<<SLOT->eg_sh_d1r)-1)))
1084
{
1085
/* SSG EG type */
1086
if (SLOT->ssg&0x08)
1087
{
1088
/* update attenuation level */
1089
if (SLOT->volume < 0x200)
1090
{
1091
SLOT->volume += 4 * eg_inc[SLOT->eg_sel_d1r + ((eg_cnt>>SLOT->eg_sh_d1r)&7)];
1092
1093
/* recalculate EG output */
1094
if (SLOT->ssgn ^ (SLOT->ssg&0x04)) /* SSG-EG Output Inversion */
1095
SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;
1096
else
1097
SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
1098
}
1099
}
1100
else
1101
{
1102
/* update attenuation level */
1103
SLOT->volume += eg_inc[SLOT->eg_sel_d1r + ((eg_cnt>>SLOT->eg_sh_d1r)&7)];
1104
1105
/* recalculate EG output */
1106
SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
1107
}
1108
1109
/* check phase transition*/
1110
if (SLOT->volume >= (INT32)(SLOT->sl))
1111
SLOT->state = EG_SUS;
1112
}
1113
break;
1114
}
1115
1116
case EG_SUS: /* sustain phase */
1117
{
1118
if (!(eg_cnt & ((1<<SLOT->eg_sh_d2r)-1)))
1119
{
1120
/* SSG EG type */
1121
if (SLOT->ssg&0x08)
1122
{
1123
/* update attenuation level */
1124
if (SLOT->volume < 0x200)
1125
{
1126
SLOT->volume += 4 * eg_inc[SLOT->eg_sel_d2r + ((eg_cnt>>SLOT->eg_sh_d2r)&7)];
1127
1128
/* recalculate EG output */
1129
if (SLOT->ssgn ^ (SLOT->ssg&0x04)) /* SSG-EG Output Inversion */
1130
SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;
1131
else
1132
SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
1133
}
1134
}
1135
else
1136
{
1137
/* update attenuation level */
1138
SLOT->volume += eg_inc[SLOT->eg_sel_d2r + ((eg_cnt>>SLOT->eg_sh_d2r)&7)];
1139
1140
/* check phase transition*/
1141
if ( SLOT->volume >= MAX_ATT_INDEX )
1142
SLOT->volume = MAX_ATT_INDEX;
1143
/* do not change SLOT->state (verified on real chip) */
1144
1145
/* recalculate EG output */
1146
SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
1147
}
1148
}
1149
break;
1150
}
1151
1152
case EG_REL: /* release phase */
1153
{
1154
if (!(eg_cnt & ((1<<SLOT->eg_sh_rr)-1)))
1155
{
1156
/* SSG EG type */
1157
if (SLOT->ssg&0x08)
1158
{
1159
/* update attenuation level */
1160
if (SLOT->volume < 0x200)
1161
SLOT->volume += 4 * eg_inc[SLOT->eg_sel_rr + ((eg_cnt>>SLOT->eg_sh_rr)&7)];
1162
1163
/* check phase transition */
1164
if (SLOT->volume >= 0x200)
1165
{
1166
SLOT->volume = MAX_ATT_INDEX;
1167
SLOT->state = EG_OFF;
1168
}
1169
}
1170
else
1171
{
1172
/* update attenuation level */
1173
SLOT->volume += eg_inc[SLOT->eg_sel_rr + ((eg_cnt>>SLOT->eg_sh_rr)&7)];
1174
1175
/* check phase transition*/
1176
if (SLOT->volume >= MAX_ATT_INDEX)
1177
{
1178
SLOT->volume = MAX_ATT_INDEX;
1179
SLOT->state = EG_OFF;
1180
}
1181
}
1182
1183
/* recalculate EG output */
1184
SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
1185
1186
}
1187
break;
1188
}
1189
}
1190
1191
/* next slot */
1192
SLOT++;
1193
} while (--j);
1194
1195
/* next channel */
1196
CH++;
1197
} while (--i);
1198
}
1199
1200
/* SSG-EG update process */
1201
/* The behavior is based upon Nemesis tests on real hardware */
1202
/* This is actually executed before each samples */
1203
INLINE void update_ssg_eg_channels(FM_CH *CH)
1204
{
1205
unsigned int i = 6; /* six channels */
1206
unsigned int j;
1207
FM_SLOT *SLOT;
1208
1209
do
1210
{
1211
j = 4; /* four operators per channel */
1212
SLOT = &CH->SLOT[SLOT1];
1213
1214
do
1215
{
1216
/* detect SSG-EG transition */
1217
/* this is not required during release phase as the attenuation has been forced to MAX and output invert flag is not used */
1218
/* if an Attack Phase is programmed, inversion can occur on each sample */
1219
if ((SLOT->ssg & 0x08) && (SLOT->volume >= 0x200) && (SLOT->state > EG_REL))
1220
{
1221
if (SLOT->ssg & 0x01) /* bit 0 = hold SSG-EG */
1222
{
1223
/* set inversion flag */
1224
if (SLOT->ssg & 0x02)
1225
SLOT->ssgn = 4;
1226
1227
/* force attenuation level during decay phases */
1228
if ((SLOT->state != EG_ATT) && !(SLOT->ssgn ^ (SLOT->ssg & 0x04)))
1229
SLOT->volume = MAX_ATT_INDEX;
1230
}
1231
else /* loop SSG-EG */
1232
{
1233
/* toggle output inversion flag or reset Phase Generator */
1234
if (SLOT->ssg & 0x02)
1235
SLOT->ssgn ^= 4;
1236
else
1237
SLOT->phase = 0;
1238
1239
/* same as Key ON */
1240
if (SLOT->state != EG_ATT)
1241
{
1242
if ((SLOT->ar + SLOT->ksr) < 94 /*32+62*/)
1243
{
1244
SLOT->state = (SLOT->volume <= MIN_ATT_INDEX) ? ((SLOT->sl == MIN_ATT_INDEX) ? EG_SUS : EG_DEC) : EG_ATT;
1245
}
1246
else
1247
{
1248
/* Attack Rate is maximal: directly switch to Decay or Substain */
1249
SLOT->volume = MIN_ATT_INDEX;
1250
SLOT->state = (SLOT->sl == MIN_ATT_INDEX) ? EG_SUS : EG_DEC;
1251
}
1252
}
1253
}
1254
1255
/* recalculate EG output */
1256
if (SLOT->ssgn ^ (SLOT->ssg&0x04))
1257
SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;
1258
else
1259
SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
1260
}
1261
1262
/* next slot */
1263
SLOT++;
1264
} while (--j);
1265
1266
/* next channel */
1267
CH++;
1268
} while (--i);
1269
}
1270
1271
INLINE void update_phase_lfo_slot(FM_SLOT *SLOT, INT32 pms, UINT32 block_fnum)
1272
{
1273
INT32 lfo_fn_table_index_offset = lfo_pm_table[(((block_fnum & 0x7f0) >> 4) << 8) + pms + ym2612.OPN.LFO_PM];
1274
1275
if (lfo_fn_table_index_offset) /* LFO phase modulation active */
1276
{
1277
UINT8 blk;
1278
unsigned int kc, fc;
1279
1280
/* there are 2048 FNUMs that can be generated using FNUM/BLK registers
1281
but LFO works with one more bit of a precision so we really need 4096 elements */
1282
block_fnum = block_fnum*2 + lfo_fn_table_index_offset;
1283
blk = (block_fnum&0x7000) >> 12;
1284
block_fnum = block_fnum & 0xfff;
1285
1286
/* keyscale code */
1287
kc = (blk<<2) | opn_fktable[block_fnum >> 8];
1288
1289
/* (frequency) phase increment counter */
1290
fc = (((block_fnum << 5) >> (7 - blk)) + SLOT->DT[kc]) & DT_MASK;
1291
1292
/* update phase */
1293
SLOT->phase += (fc * SLOT->mul) >> 1;
1294
}
1295
else /* LFO phase modulation = zero */
1296
{
1297
SLOT->phase += SLOT->Incr;
1298
}
1299
}
1300
1301
INLINE void update_phase_lfo_channel(FM_CH *CH)
1302
{
1303
UINT32 block_fnum = CH->block_fnum;
1304
1305
INT32 lfo_fn_table_index_offset = lfo_pm_table[(((block_fnum & 0x7f0) >> 4) << 8) + CH->pms + ym2612.OPN.LFO_PM];
1306
1307
if (lfo_fn_table_index_offset) /* LFO phase modulation active */
1308
{
1309
UINT8 blk;
1310
unsigned int kc, fc, finc;
1311
1312
/* there are 2048 FNUMs that can be generated using FNUM/BLK registers
1313
but LFO works with one more bit of a precision so we really need 4096 elements */
1314
block_fnum = block_fnum*2 + lfo_fn_table_index_offset;
1315
blk = (block_fnum&0x7000) >> 12;
1316
block_fnum = block_fnum & 0xfff;
1317
1318
/* keyscale code */
1319
kc = (blk<<2) | opn_fktable[block_fnum >> 8];
1320
1321
/* (frequency) phase increment counter */
1322
fc = (block_fnum << 5) >> (7 - blk);
1323
1324
/* apply DETUNE & MUL operator specific values */
1325
finc = (fc + CH->SLOT[SLOT1].DT[kc]) & DT_MASK;
1326
CH->SLOT[SLOT1].phase += (finc*CH->SLOT[SLOT1].mul) >> 1;
1327
1328
finc = (fc + CH->SLOT[SLOT2].DT[kc]) & DT_MASK;
1329
CH->SLOT[SLOT2].phase += (finc*CH->SLOT[SLOT2].mul) >> 1;
1330
1331
finc = (fc + CH->SLOT[SLOT3].DT[kc]) & DT_MASK;
1332
CH->SLOT[SLOT3].phase += (finc*CH->SLOT[SLOT3].mul) >> 1;
1333
1334
finc = (fc + CH->SLOT[SLOT4].DT[kc]) & DT_MASK;
1335
CH->SLOT[SLOT4].phase += (finc*CH->SLOT[SLOT4].mul) >> 1;
1336
}
1337
else /* LFO phase modulation = zero */
1338
{
1339
CH->SLOT[SLOT1].phase += CH->SLOT[SLOT1].Incr;
1340
CH->SLOT[SLOT2].phase += CH->SLOT[SLOT2].Incr;
1341
CH->SLOT[SLOT3].phase += CH->SLOT[SLOT3].Incr;
1342
CH->SLOT[SLOT4].phase += CH->SLOT[SLOT4].Incr;
1343
}
1344
}
1345
1346
/* update phase increment and envelope generator */
1347
INLINE void refresh_fc_eg_slot(FM_SLOT *SLOT , unsigned int fc , unsigned int kc )
1348
{
1349
/* add detune value */
1350
fc += SLOT->DT[kc];
1351
1352
/* (frequency) phase overflow (credits to Nemesis) */
1353
fc &= DT_MASK;
1354
1355
/* (frequency) phase increment counter */
1356
SLOT->Incr = (fc * SLOT->mul) >> 1;
1357
1358
/* ksr */
1359
kc = kc >> SLOT->KSR;
1360
1361
if( SLOT->ksr != kc )
1362
{
1363
SLOT->ksr = kc;
1364
1365
/* recalculate envelope generator rates */
1366
if ((SLOT->ar + kc) < (32+62))
1367
{
1368
SLOT->eg_sh_ar = eg_rate_shift [SLOT->ar + kc ];
1369
SLOT->eg_sel_ar = eg_rate_select[SLOT->ar + kc ];
1370
}
1371
else
1372
{
1373
/* verified by Nemesis on real hardware (Attack phase is blocked) */
1374
SLOT->eg_sh_ar = 0;
1375
SLOT->eg_sel_ar = 18*RATE_STEPS;
1376
}
1377
1378
SLOT->eg_sh_d1r = eg_rate_shift [SLOT->d1r + kc];
1379
SLOT->eg_sel_d1r= eg_rate_select[SLOT->d1r + kc];
1380
1381
SLOT->eg_sh_d2r = eg_rate_shift [SLOT->d2r + kc];
1382
SLOT->eg_sel_d2r= eg_rate_select[SLOT->d2r + kc];
1383
1384
SLOT->eg_sh_rr = eg_rate_shift [SLOT->rr + kc];
1385
SLOT->eg_sel_rr = eg_rate_select[SLOT->rr + kc];
1386
}
1387
}
1388
1389
/* update phase increment counters */
1390
INLINE void refresh_fc_eg_chan(FM_CH *CH )
1391
{
1392
if( CH->SLOT[SLOT1].Incr==-1)
1393
{
1394
int fc = CH->fc;
1395
int kc = CH->kcode;
1396
refresh_fc_eg_slot(&CH->SLOT[SLOT1] , fc , kc );
1397
refresh_fc_eg_slot(&CH->SLOT[SLOT2] , fc , kc );
1398
refresh_fc_eg_slot(&CH->SLOT[SLOT3] , fc , kc );
1399
refresh_fc_eg_slot(&CH->SLOT[SLOT4] , fc , kc );
1400
}
1401
}
1402
1403
#define volume_calc(OP) ((OP)->vol_out + (AM & (OP)->AMmask))
1404
1405
INLINE signed int op_calc(UINT32 phase, unsigned int env, unsigned int pm)
1406
{
1407
UINT32 p = (env<<3) + sin_tab[ ( (phase >> SIN_BITS) + (pm >> 1) ) & SIN_MASK ];
1408
1409
if (p >= TL_TAB_LEN)
1410
return 0;
1411
return tl_tab[p];
1412
}
1413
1414
INLINE signed int op_calc1(UINT32 phase, unsigned int env, unsigned int pm)
1415
{
1416
UINT32 p = (env<<3) + sin_tab[ ( (phase + pm ) >> SIN_BITS ) & SIN_MASK ];
1417
1418
if (p >= TL_TAB_LEN)
1419
return 0;
1420
return tl_tab[p];
1421
}
1422
1423
INLINE void chan_calc(FM_CH *CH, int num)
1424
{
1425
do
1426
{
1427
UINT32 AM = ym2612.OPN.LFO_AM >> CH->ams;
1428
unsigned int eg_out = volume_calc(&CH->SLOT[SLOT1]);
1429
1430
m2 = c1 = c2 = mem = 0;
1431
1432
*CH->mem_connect = CH->mem_value; /* restore delayed sample (MEM) value to m2 or c2 */
1433
{
1434
INT32 out = CH->op1_out[0] + CH->op1_out[1];
1435
CH->op1_out[0] = CH->op1_out[1];
1436
1437
if( !CH->connect1 ){
1438
/* algorithm 5 */
1439
mem = c1 = c2 = CH->op1_out[0];
1440
}else{
1441
/* other algorithms */
1442
*CH->connect1 += CH->op1_out[0];
1443
}
1444
1445
CH->op1_out[1] = 0;
1446
if( eg_out < ENV_QUIET ) /* SLOT 1 */
1447
{
1448
if (!CH->FB)
1449
out=0;
1450
1451
CH->op1_out[1] = op_calc1(CH->SLOT[SLOT1].phase, eg_out, (out<<CH->FB) );
1452
}
1453
}
1454
1455
eg_out = volume_calc(&CH->SLOT[SLOT3]);
1456
if( eg_out < ENV_QUIET ) /* SLOT 3 */
1457
*CH->connect3 += op_calc(CH->SLOT[SLOT3].phase, eg_out, m2);
1458
1459
eg_out = volume_calc(&CH->SLOT[SLOT2]);
1460
if( eg_out < ENV_QUIET ) /* SLOT 2 */
1461
*CH->connect2 += op_calc(CH->SLOT[SLOT2].phase, eg_out, c1);
1462
1463
eg_out = volume_calc(&CH->SLOT[SLOT4]);
1464
if( eg_out < ENV_QUIET ) /* SLOT 4 */
1465
*CH->connect4 += op_calc(CH->SLOT[SLOT4].phase, eg_out, c2);
1466
1467
1468
/* store current MEM */
1469
CH->mem_value = mem;
1470
1471
/* update phase counters AFTER output calculations */
1472
if(CH->pms)
1473
{
1474
/* add support for 3 slot mode */
1475
if ((ym2612.OPN.ST.mode & 0xC0) && (CH == &ym2612.CH[2]))
1476
{
1477
update_phase_lfo_slot(&CH->SLOT[SLOT1], CH->pms, ym2612.OPN.SL3.block_fnum[1]);
1478
update_phase_lfo_slot(&CH->SLOT[SLOT2], CH->pms, ym2612.OPN.SL3.block_fnum[2]);
1479
update_phase_lfo_slot(&CH->SLOT[SLOT3], CH->pms, ym2612.OPN.SL3.block_fnum[0]);
1480
update_phase_lfo_slot(&CH->SLOT[SLOT4], CH->pms, CH->block_fnum);
1481
}
1482
else
1483
{
1484
update_phase_lfo_channel(CH);
1485
}
1486
}
1487
else /* no LFO phase modulation */
1488
{
1489
CH->SLOT[SLOT1].phase += CH->SLOT[SLOT1].Incr;
1490
CH->SLOT[SLOT2].phase += CH->SLOT[SLOT2].Incr;
1491
CH->SLOT[SLOT3].phase += CH->SLOT[SLOT3].Incr;
1492
CH->SLOT[SLOT4].phase += CH->SLOT[SLOT4].Incr;
1493
}
1494
1495
/* next channel */
1496
CH++;
1497
} while (--num);
1498
}
1499
1500
/* write a OPN mode register 0x20-0x2f */
1501
INLINE void OPNWriteMode(int r, int v)
1502
{
1503
UINT8 c;
1504
FM_CH *CH;
1505
1506
switch(r){
1507
case 0x21: /* Test */
1508
break;
1509
1510
case 0x22: /* LFO FREQ (YM2608/YM2610/YM2610B/ym2612) */
1511
if (v&8) /* LFO enabled ? */
1512
{
1513
ym2612.OPN.lfo_timer_overflow = lfo_samples_per_step[v&7];
1514
}
1515
else
1516
{
1517
/* hold LFO waveform in reset state */
1518
ym2612.OPN.lfo_timer_overflow = 0;
1519
ym2612.OPN.lfo_timer = 0;
1520
ym2612.OPN.lfo_cnt = 0;
1521
ym2612.OPN.LFO_PM = 0;
1522
ym2612.OPN.LFO_AM = 126;
1523
}
1524
break;
1525
case 0x24: /* timer A High 8*/
1526
ym2612.OPN.ST.TA = (ym2612.OPN.ST.TA & 0x03)|(((int)v)<<2);
1527
ym2612.OPN.ST.TAL = 1024 - ym2612.OPN.ST.TA;
1528
break;
1529
case 0x25: /* timer A Low 2*/
1530
ym2612.OPN.ST.TA = (ym2612.OPN.ST.TA & 0x3fc)|(v&3);
1531
ym2612.OPN.ST.TAL = 1024 - ym2612.OPN.ST.TA;
1532
break;
1533
case 0x26: /* timer B */
1534
ym2612.OPN.ST.TB = v;
1535
ym2612.OPN.ST.TBL = (256 - v) << 4;
1536
break;
1537
case 0x27: /* mode, timer control */
1538
set_timers(v);
1539
break;
1540
case 0x28: /* key on / off */
1541
c = v & 0x03;
1542
if( c == 3 ) break;
1543
if (v&0x04) c+=3; /* CH 4-6 */
1544
CH = &ym2612.CH[c];
1545
1546
if (v&0x10) FM_KEYON(CH,SLOT1); else FM_KEYOFF(CH,SLOT1);
1547
if (v&0x20) FM_KEYON(CH,SLOT2); else FM_KEYOFF(CH,SLOT2);
1548
if (v&0x40) FM_KEYON(CH,SLOT3); else FM_KEYOFF(CH,SLOT3);
1549
if (v&0x80) FM_KEYON(CH,SLOT4); else FM_KEYOFF(CH,SLOT4);
1550
break;
1551
}
1552
}
1553
1554
/* write a OPN register (0x30-0xff) */
1555
INLINE void OPNWriteReg(int r, int v)
1556
{
1557
FM_CH *CH;
1558
FM_SLOT *SLOT;
1559
1560
UINT8 c = OPN_CHAN(r);
1561
1562
if (c == 3) return; /* 0xX3,0xX7,0xXB,0xXF */
1563
1564
if (r >= 0x100) c+=3;
1565
1566
CH = &ym2612.CH[c];
1567
1568
SLOT = &(CH->SLOT[OPN_SLOT(r)]);
1569
1570
switch( r & 0xf0 ) {
1571
case 0x30: /* DET , MUL */
1572
set_det_mul(CH,SLOT,v);
1573
break;
1574
1575
case 0x40: /* TL */
1576
set_tl(SLOT,v);
1577
break;
1578
1579
case 0x50: /* KS, AR */
1580
set_ar_ksr(CH,SLOT,v);
1581
break;
1582
1583
case 0x60: /* bit7 = AM ENABLE, DR */
1584
set_dr(SLOT,v);
1585
SLOT->AMmask = (v&0x80) ? ~0 : 0;
1586
break;
1587
1588
case 0x70: /* SR */
1589
set_sr(SLOT,v);
1590
break;
1591
1592
case 0x80: /* SL, RR */
1593
set_sl_rr(SLOT,v);
1594
break;
1595
1596
case 0x90: /* SSG-EG */
1597
SLOT->ssg = v&0x0f;
1598
1599
/* recalculate EG output */
1600
if (SLOT->state > EG_REL)
1601
{
1602
if ((SLOT->ssg&0x08) && (SLOT->ssgn ^ (SLOT->ssg&0x04)))
1603
SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;
1604
else
1605
SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
1606
}
1607
1608
/* SSG-EG envelope shapes :
1609
1610
E AtAlH
1611
1 0 0 0 \\\\
1612
1613
1 0 0 1 \___
1614
1615
1 0 1 0 \/\/
1616
___
1617
1 0 1 1 \
1618
1619
1 1 0 0 ////
1620
___
1621
1 1 0 1 /
1622
1623
1 1 1 0 /\/\
1624
1625
1 1 1 1 /___
1626
1627
1628
E = SSG-EG enable
1629
1630
1631
The shapes are generated using Attack, Decay and Sustain phases.
1632
1633
Each single character in the diagrams above represents this whole
1634
sequence:
1635
1636
- when KEY-ON = 1, normal Attack phase is generated (*without* any
1637
difference when compared to normal mode),
1638
1639
- later, when envelope level reaches minimum level (max volume),
1640
the EG switches to Decay phase (which works with bigger steps
1641
when compared to normal mode - see below),
1642
1643
- later when envelope level passes the SL level,
1644
the EG swithes to Sustain phase (which works with bigger steps
1645
when compared to normal mode - see below),
1646
1647
- finally when envelope level reaches maximum level (min volume),
1648
the EG switches to Attack phase again (depends on actual waveform).
1649
1650
Important is that when switch to Attack phase occurs, the phase counter
1651
of that operator will be zeroed-out (as in normal KEY-ON) but not always.
1652
(I havent found the rule for that - perhaps only when the output level is low)
1653
1654
The difference (when compared to normal Envelope Generator mode) is
1655
that the resolution in Decay and Sustain phases is 4 times lower;
1656
this results in only 256 steps instead of normal 1024.
1657
In other words:
1658
when SSG-EG is disabled, the step inside of the EG is one,
1659
when SSG-EG is enabled, the step is four (in Decay and Sustain phases).
1660
1661
Times between the level changes are the same in both modes.
1662
1663
1664
Important:
1665
Decay 1 Level (so called SL) is compared to actual SSG-EG output, so
1666
it is the same in both SSG and no-SSG modes, with this exception:
1667
1668
when the SSG-EG is enabled and is generating raising levels
1669
(when the EG output is inverted) the SL will be found at wrong level !!!
1670
For example, when SL=02:
1671
0 -6 = -6dB in non-inverted EG output
1672
96-6 = -90dB in inverted EG output
1673
Which means that EG compares its level to SL as usual, and that the
1674
output is simply inverted afterall.
1675
1676
1677
The Yamaha's manuals say that AR should be set to 0x1f (max speed).
1678
That is not necessary, but then EG will be generating Attack phase.
1679
1680
*/
1681
1682
1683
break;
1684
1685
case 0xa0:
1686
switch( OPN_SLOT(r) ){
1687
case 0: /* 0xa0-0xa2 : FNUM1 */
1688
{
1689
UINT32 fn = (((UINT32)((ym2612.OPN.ST.fn_h)&7))<<8) + v;
1690
UINT8 blk = ym2612.OPN.ST.fn_h>>3;
1691
/* keyscale code */
1692
CH->kcode = (blk<<2) | opn_fktable[fn >> 7];
1693
/* phase increment counter */
1694
CH->fc = (fn << 6) >> (7 - blk);
1695
1696
/* store fnum in clear form for LFO PM calculations */
1697
CH->block_fnum = (blk<<11) | fn;
1698
1699
CH->SLOT[SLOT1].Incr=-1;
1700
break;
1701
}
1702
case 1: /* 0xa4-0xa6 : FNUM2,BLK */
1703
ym2612.OPN.ST.fn_h = v&0x3f;
1704
break;
1705
case 2: /* 0xa8-0xaa : 3CH FNUM1 */
1706
if(r < 0x100)
1707
{
1708
UINT32 fn = (((UINT32)(ym2612.OPN.SL3.fn_h&7))<<8) + v;
1709
UINT8 blk = ym2612.OPN.SL3.fn_h>>3;
1710
/* keyscale code */
1711
ym2612.OPN.SL3.kcode[c]= (blk<<2) | opn_fktable[fn >> 7];
1712
/* phase increment counter */
1713
ym2612.OPN.SL3.fc[c] = (fn << 6) >> (7 - blk);
1714
ym2612.OPN.SL3.block_fnum[c] = (blk<<11) | fn;
1715
ym2612.CH[2].SLOT[SLOT1].Incr=-1;
1716
}
1717
break;
1718
case 3: /* 0xac-0xae : 3CH FNUM2,BLK */
1719
if(r < 0x100)
1720
ym2612.OPN.SL3.fn_h = v&0x3f;
1721
break;
1722
}
1723
break;
1724
1725
case 0xb0:
1726
switch( OPN_SLOT(r) ){
1727
case 0: /* 0xb0-0xb2 : FB,ALGO */
1728
{
1729
CH->ALGO = v&7;
1730
CH->FB = (v>>3)&7;
1731
setup_connection( CH, c );
1732
break;
1733
}
1734
case 1: /* 0xb4-0xb6 : L , R , AMS , PMS */
1735
/* b0-2 PMS */
1736
CH->pms = (v & 7) * 32; /* CH->pms = PM depth * 32 (index in lfo_pm_table) */
1737
1738
/* b4-5 AMS */
1739
CH->ams = lfo_ams_depth_shift[(v>>4) & 0x03];
1740
1741
/* PAN : b7 = L, b6 = R */
1742
ym2612.OPN.pan[ c*2 ] = (v & 0x80) ? bitmask : 0;
1743
ym2612.OPN.pan[ c*2+1 ] = (v & 0x40) ? bitmask : 0;
1744
break;
1745
}
1746
break;
1747
}
1748
}
1749
1750
static void reset_channels(FM_CH *CH , int num )
1751
{
1752
int c,s;
1753
1754
for( c = 0 ; c < num ; c++ )
1755
{
1756
CH[c].mem_value = 0;
1757
CH[c].op1_out[0] = 0;
1758
CH[c].op1_out[1] = 0;
1759
for(s = 0 ; s < 4 ; s++ )
1760
{
1761
CH[c].SLOT[s].Incr = -1;
1762
CH[c].SLOT[s].key = 0;
1763
CH[c].SLOT[s].phase = 0;
1764
CH[c].SLOT[s].ssgn = 0;
1765
CH[c].SLOT[s].state = EG_OFF;
1766
CH[c].SLOT[s].volume = MAX_ATT_INDEX;
1767
CH[c].SLOT[s].vol_out = MAX_ATT_INDEX;
1768
}
1769
}
1770
}
1771
1772
/* initialize generic tables */
1773
static void init_tables(void)
1774
{
1775
signed int d,i,x;
1776
signed int n;
1777
double o,m;
1778
1779
/* build Linear Power Table */
1780
for (x=0; x<TL_RES_LEN; x++)
1781
{
1782
m = (1<<16) / pow(2,(x+1) * (ENV_STEP/4.0) / 8.0);
1783
m = floor(m);
1784
1785
/* we never reach (1<<16) here due to the (x+1) */
1786
/* result fits within 16 bits at maximum */
1787
1788
n = (int)m; /* 16 bits here */
1789
n >>= 4; /* 12 bits here */
1790
if (n&1) /* round to nearest */
1791
n = (n>>1)+1;
1792
else
1793
n = n>>1;
1794
/* 11 bits here (rounded) */
1795
n <<= 2; /* 13 bits here (as in real chip) */
1796
1797
/* 14 bits (with sign bit) */
1798
tl_tab[ x*2 + 0 ] = n;
1799
tl_tab[ x*2 + 1 ] = -tl_tab[ x*2 + 0 ];
1800
1801
/* one entry in the 'Power' table use the following format, xxxxxyyyyyyyys with: */
1802
/* s = sign bit */
1803
/* yyyyyyyy = 8-bits decimal part (0-TL_RES_LEN) */
1804
/* xxxxx = 5-bits integer 'shift' value (0-31) but, since Power table output is 13 bits, */
1805
/* any value above 13 (included) would be discarded. */
1806
for (i=1; i<13; i++)
1807
{
1808
tl_tab[ x*2+0 + i*2*TL_RES_LEN ] = tl_tab[ x*2+0 ]>>i;
1809
tl_tab[ x*2+1 + i*2*TL_RES_LEN ] = -tl_tab[ x*2+0 + i*2*TL_RES_LEN ];
1810
}
1811
}
1812
1813
/* build Logarithmic Sinus table */
1814
for (i=0; i<SIN_LEN; i++)
1815
{
1816
/* non-standard sinus */
1817
m = sin( ((i*2)+1) * M_PI / SIN_LEN ); /* checked against the real chip */
1818
/* we never reach zero here due to ((i*2)+1) */
1819
1820
if (m>0.0)
1821
o = 8*log(1.0/m)/log(2); /* convert to 'decibels' */
1822
else
1823
o = 8*log(-1.0/m)/log(2); /* convert to 'decibels' */
1824
1825
o = o / (ENV_STEP/4);
1826
1827
n = (int)(2.0*o);
1828
if (n&1) /* round to nearest */
1829
n = (n>>1)+1;
1830
else
1831
n = n>>1;
1832
1833
/* 13-bits (8.5) value is formatted for above 'Power' table */
1834
sin_tab[ i ] = n*2 + (m>=0.0? 0: 1 );
1835
}
1836
1837
/* build LFO PM modulation table */
1838
for(i = 0; i < 8; i++) /* 8 PM depths */
1839
{
1840
UINT8 fnum;
1841
for (fnum=0; fnum<128; fnum++) /* 7 bits meaningful of F-NUMBER */
1842
{
1843
UINT8 value;
1844
UINT8 step;
1845
UINT32 offset_depth = i;
1846
UINT32 offset_fnum_bit;
1847
UINT32 bit_tmp;
1848
1849
for (step=0; step<8; step++)
1850
{
1851
value = 0;
1852
for (bit_tmp=0; bit_tmp<7; bit_tmp++) /* 7 bits */
1853
{
1854
if (fnum & (1<<bit_tmp)) /* only if bit "bit_tmp" is set */
1855
{
1856
offset_fnum_bit = bit_tmp * 8;
1857
value += lfo_pm_output[offset_fnum_bit + offset_depth][step];
1858
}
1859
}
1860
/* 32 steps for LFO PM (sinus) */
1861
lfo_pm_table[(fnum*32*8) + (i*32) + step + 0] = value;
1862
lfo_pm_table[(fnum*32*8) + (i*32) +(step^7)+ 8] = value;
1863
lfo_pm_table[(fnum*32*8) + (i*32) + step +16] = -value;
1864
lfo_pm_table[(fnum*32*8) + (i*32) +(step^7)+24] = -value;
1865
}
1866
}
1867
}
1868
1869
/* build DETUNE table */
1870
for (d = 0;d <= 3;d++)
1871
{
1872
for (i = 0;i <= 31;i++)
1873
{
1874
ym2612.OPN.ST.dt_tab[d][i] = (INT32) dt_tab[d*32 + i];
1875
ym2612.OPN.ST.dt_tab[d+4][i] = -ym2612.OPN.ST.dt_tab[d][i];
1876
}
1877
}
1878
1879
}
1880
1881
1882
1883
/* initialize ym2612 emulator */
1884
void YM2612Init(void)
1885
{
1886
memset(&ym2612,0,sizeof(YM2612));
1887
init_tables();
1888
}
1889
1890
/* reset OPN registers */
1891
void YM2612ResetChip(void)
1892
{
1893
int i;
1894
1895
ym2612.OPN.eg_timer = 0;
1896
ym2612.OPN.eg_cnt = 0;
1897
1898
ym2612.OPN.lfo_timer_overflow = 0;
1899
ym2612.OPN.lfo_timer = 0;
1900
ym2612.OPN.lfo_cnt = 0;
1901
ym2612.OPN.LFO_AM = 126;
1902
ym2612.OPN.LFO_PM = 0;
1903
1904
ym2612.OPN.ST.TAC = 0;
1905
ym2612.OPN.ST.TBC = 0;
1906
1907
ym2612.OPN.SL3.key_csm = 0;
1908
1909
ym2612.dacen = 0;
1910
ym2612.dacout = 0;
1911
1912
set_timers(0x30);
1913
ym2612.OPN.ST.TB = 0;
1914
ym2612.OPN.ST.TBL = 256 << 4;
1915
ym2612.OPN.ST.TA = 0;
1916
ym2612.OPN.ST.TAL = 1024;
1917
1918
reset_channels(&ym2612.CH[0] , 6 );
1919
1920
for(i = 0xb6 ; i >= 0xb4 ; i-- )
1921
{
1922
OPNWriteReg(i ,0xc0);
1923
OPNWriteReg(i|0x100,0xc0);
1924
}
1925
for(i = 0xb2 ; i >= 0x30 ; i-- )
1926
{
1927
OPNWriteReg(i ,0);
1928
OPNWriteReg(i|0x100,0);
1929
}
1930
}
1931
1932
/* ym2612 write */
1933
/* n = number */
1934
/* a = address */
1935
/* v = value */
1936
void YM2612Write(unsigned int a, unsigned int v)
1937
{
1938
v &= 0xff; /* adjust to 8 bit bus */
1939
1940
switch( a )
1941
{
1942
case 0: /* address port 0 */
1943
ym2612.OPN.ST.address = v;
1944
break;
1945
1946
case 2: /* address port 1 */
1947
ym2612.OPN.ST.address = v | 0x100;
1948
break;
1949
1950
default: /* data port */
1951
{
1952
int addr = ym2612.OPN.ST.address; /* verified by Nemesis on real YM2612 */
1953
switch( addr & 0x1f0 )
1954
{
1955
case 0x20: /* 0x20-0x2f Mode */
1956
switch( addr )
1957
{
1958
case 0x2a: /* DAC data (ym2612) */
1959
ym2612.dacout = ((int)v - 0x80) << 6; /* convert to 14-bit output */
1960
break;
1961
case 0x2b: /* DAC Sel (ym2612) */
1962
/* b7 = dac enable */
1963
ym2612.dacen = v & 0x80;
1964
break;
1965
default: /* OPN section */
1966
/* write register */
1967
OPNWriteMode(addr,v);
1968
}
1969
break;
1970
default: /* 0x30-0xff OPN section */
1971
/* write register */
1972
OPNWriteReg(addr,v);
1973
}
1974
break;
1975
}
1976
}
1977
}
1978
1979
unsigned int YM2612Read(void)
1980
{
1981
return ym2612.OPN.ST.status & 0xff;
1982
}
1983
1984
/* Generate samples for ym2612 */
1985
void YM2612Update(int *buffer, int length)
1986
{
1987
int i;
1988
int lt,rt;
1989
1990
/* refresh PG increments and EG rates if required */
1991
refresh_fc_eg_chan(&ym2612.CH[0]);
1992
refresh_fc_eg_chan(&ym2612.CH[1]);
1993
1994
if (!(ym2612.OPN.ST.mode & 0xC0))
1995
{
1996
refresh_fc_eg_chan(&ym2612.CH[2]);
1997
}
1998
else
1999
{
2000
/* 3SLOT MODE (operator order is 0,1,3,2) */
2001
if(ym2612.CH[2].SLOT[SLOT1].Incr==-1)
2002
{
2003
refresh_fc_eg_slot(&ym2612.CH[2].SLOT[SLOT1] , ym2612.OPN.SL3.fc[1] , ym2612.OPN.SL3.kcode[1] );
2004
refresh_fc_eg_slot(&ym2612.CH[2].SLOT[SLOT2] , ym2612.OPN.SL3.fc[2] , ym2612.OPN.SL3.kcode[2] );
2005
refresh_fc_eg_slot(&ym2612.CH[2].SLOT[SLOT3] , ym2612.OPN.SL3.fc[0] , ym2612.OPN.SL3.kcode[0] );
2006
refresh_fc_eg_slot(&ym2612.CH[2].SLOT[SLOT4] , ym2612.CH[2].fc , ym2612.CH[2].kcode );
2007
}
2008
}
2009
2010
refresh_fc_eg_chan(&ym2612.CH[3]);
2011
refresh_fc_eg_chan(&ym2612.CH[4]);
2012
refresh_fc_eg_chan(&ym2612.CH[5]);
2013
2014
/* buffering */
2015
for(i=0; i < length ; i++)
2016
{
2017
/* clear outputs */
2018
out_fm[0] = 0;
2019
out_fm[1] = 0;
2020
out_fm[2] = 0;
2021
out_fm[3] = 0;
2022
out_fm[4] = 0;
2023
out_fm[5] = 0;
2024
2025
/* update SSG-EG output */
2026
update_ssg_eg_channels(&ym2612.CH[0]);
2027
2028
/* calculate FM */
2029
if (!ym2612.dacen)
2030
{
2031
chan_calc(&ym2612.CH[0],6);
2032
}
2033
else
2034
{
2035
/* DAC Mode */
2036
out_fm[5] = ym2612.dacout;
2037
chan_calc(&ym2612.CH[0],5);
2038
}
2039
2040
/* advance LFO */
2041
advance_lfo();
2042
2043
/* advance envelope generator */
2044
ym2612.OPN.eg_timer ++;
2045
2046
/* EG is updated every 3 samples */
2047
if (ym2612.OPN.eg_timer >= 3)
2048
{
2049
ym2612.OPN.eg_timer = 0;
2050
ym2612.OPN.eg_cnt++;
2051
advance_eg_channels(&ym2612.CH[0], ym2612.OPN.eg_cnt);
2052
}
2053
2054
/* 14-bit accumulator channels outputs (range is -8192;+8192) */
2055
if (out_fm[0] > 8192) out_fm[0] = 8192;
2056
else if (out_fm[0] < -8192) out_fm[0] = -8192;
2057
if (out_fm[1] > 8192) out_fm[1] = 8192;
2058
else if (out_fm[1] < -8192) out_fm[1] = -8192;
2059
if (out_fm[2] > 8192) out_fm[2] = 8192;
2060
else if (out_fm[2] < -8192) out_fm[2] = -8192;
2061
if (out_fm[3] > 8192) out_fm[3] = 8192;
2062
else if (out_fm[3] < -8192) out_fm[3] = -8192;
2063
if (out_fm[4] > 8192) out_fm[4] = 8192;
2064
else if (out_fm[4] < -8192) out_fm[4] = -8192;
2065
if (out_fm[5] > 8192) out_fm[5] = 8192;
2066
else if (out_fm[5] < -8192) out_fm[5] = -8192;
2067
2068
/* stereo DAC channels outputs mixing */
2069
lt = ((out_fm[0]) & ym2612.OPN.pan[0]);
2070
rt = ((out_fm[0]) & ym2612.OPN.pan[1]);
2071
lt += ((out_fm[1]) & ym2612.OPN.pan[2]);
2072
rt += ((out_fm[1]) & ym2612.OPN.pan[3]);
2073
lt += ((out_fm[2]) & ym2612.OPN.pan[4]);
2074
rt += ((out_fm[2]) & ym2612.OPN.pan[5]);
2075
lt += ((out_fm[3]) & ym2612.OPN.pan[6]);
2076
rt += ((out_fm[3]) & ym2612.OPN.pan[7]);
2077
lt += ((out_fm[4]) & ym2612.OPN.pan[8]);
2078
rt += ((out_fm[4]) & ym2612.OPN.pan[9]);
2079
lt += ((out_fm[5]) & ym2612.OPN.pan[10]);
2080
rt += ((out_fm[5]) & ym2612.OPN.pan[11]);
2081
2082
/* buffering */
2083
*buffer++ = lt;
2084
*buffer++ = rt;
2085
2086
/* CSM mode: if CSM Key ON has occured, CSM Key OFF need to be sent */
2087
/* only if Timer A does not overflow again (i.e CSM Key ON not set again) */
2088
ym2612.OPN.SL3.key_csm <<= 1;
2089
2090
/* timer A control */
2091
INTERNAL_TIMER_A();
2092
2093
/* CSM Mode Key ON still disabled */
2094
if (ym2612.OPN.SL3.key_csm & 2)
2095
{
2096
/* CSM Mode Key OFF (verified by Nemesis on real hardware) */
2097
FM_KEYOFF_CSM(&ym2612.CH[2],SLOT1);
2098
FM_KEYOFF_CSM(&ym2612.CH[2],SLOT2);
2099
FM_KEYOFF_CSM(&ym2612.CH[2],SLOT3);
2100
FM_KEYOFF_CSM(&ym2612.CH[2],SLOT4);
2101
ym2612.OPN.SL3.key_csm = 0;
2102
}
2103
}
2104
2105
/* timer B control */
2106
INTERNAL_TIMER_B(length);
2107
}
2108
2109
void YM2612Config(unsigned char dac_bits)
2110
{
2111
int i;
2112
2113
/* DAC precision (normally 9-bit on real hardware, implemented through simple 14-bit channel output bitmasking) */
2114
bitmask = ~((1 << (TL_BITS - dac_bits)) - 1);
2115
2116
/* update L/R panning bitmasks */
2117
for (i=0; i<2*6; i++)
2118
{
2119
if (ym2612.OPN.pan[i])
2120
{
2121
ym2612.OPN.pan[i] = bitmask;
2122
}
2123
}
2124
}
2125
2126
int YM2612LoadContext(unsigned char *state)
2127
{
2128
int c,s;
2129
uint8 index;
2130
int bufferptr = 0;
2131
2132
/* restore YM2612 context */
2133
load_param(&ym2612, sizeof(ym2612));
2134
2135
/* restore DT table address pointer for each channel slots */
2136
for (c=0; c<6; c++)
2137
{
2138
for (s=0; s<4; s++)
2139
{
2140
load_param(&index,sizeof(index));
2141
bufferptr += sizeof(index);
2142
ym2612.CH[c].SLOT[s].DT = ym2612.OPN.ST.dt_tab[index&7];
2143
}
2144
}
2145
2146
/* restore outputs connections */
2147
setup_connection(&ym2612.CH[0],0);
2148
setup_connection(&ym2612.CH[1],1);
2149
setup_connection(&ym2612.CH[2],2);
2150
setup_connection(&ym2612.CH[3],3);
2151
setup_connection(&ym2612.CH[4],4);
2152
setup_connection(&ym2612.CH[5],5);
2153
2154
return bufferptr;
2155
}
2156
2157
int YM2612SaveContext(unsigned char *state)
2158
{
2159
int c,s;
2160
uint8 index;
2161
int bufferptr = 0;
2162
2163
/* save YM2612 context */
2164
save_param(&ym2612, sizeof(ym2612));
2165
2166
/* save DT table index for each channel slots */
2167
for (c=0; c<6; c++)
2168
{
2169
for (s=0; s<4; s++)
2170
{
2171
index = (ym2612.CH[c].SLOT[s].DT - ym2612.OPN.ST.dt_tab[0]) >> 5;
2172
save_param(&index,sizeof(index));
2173
bufferptr += sizeof(index);
2174
}
2175
}
2176
2177
return bufferptr;
2178
}
2179
2180