Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/waterbox/gpgx/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
INT32 *ym2612_lfo_pm_table; /* 128 combinations of 7 bits meaningful (of F-NUMBER), 8 LFO depths, 32 LFO output levels per one depth */
467
#define lfo_pm_table ym2612_lfo_pm_table
468
469
/* register number to channel number , slot offset */
470
#define OPN_CHAN(N) (N&3)
471
#define OPN_SLOT(N) ((N>>2)&3)
472
473
/* slot number */
474
#define SLOT1 0
475
#define SLOT2 2
476
#define SLOT3 1
477
#define SLOT4 3
478
479
/* struct describing a single operator (SLOT) */
480
typedef struct
481
{
482
INT32 *DT; /* detune :dt_tab[DT] */
483
UINT8 KSR; /* key scale rate :3-KSR */
484
UINT32 ar; /* attack rate */
485
UINT32 d1r; /* decay rate */
486
UINT32 d2r; /* sustain rate */
487
UINT32 rr; /* release rate */
488
UINT8 ksr; /* key scale rate :kcode>>(3-KSR) */
489
UINT32 mul; /* multiple :ML_TABLE[ML] */
490
491
/* Phase Generator */
492
UINT32 phase; /* phase counter */
493
INT32 Incr; /* phase step */
494
495
/* Envelope Generator */
496
UINT8 state; /* phase type */
497
UINT32 tl; /* total level: TL << 3 */
498
INT32 volume; /* envelope counter */
499
UINT32 sl; /* sustain level:sl_table[SL] */
500
UINT32 vol_out; /* current output from EG circuit (without AM from LFO) */
501
502
UINT8 eg_sh_ar; /* (attack state) */
503
UINT8 eg_sel_ar; /* (attack state) */
504
UINT8 eg_sh_d1r; /* (decay state) */
505
UINT8 eg_sel_d1r; /* (decay state) */
506
UINT8 eg_sh_d2r; /* (sustain state) */
507
UINT8 eg_sel_d2r; /* (sustain state) */
508
UINT8 eg_sh_rr; /* (release state) */
509
UINT8 eg_sel_rr; /* (release state) */
510
511
UINT8 ssg; /* SSG-EG waveform */
512
UINT8 ssgn; /* SSG-EG negated output */
513
514
UINT8 key; /* 0=last key was KEY OFF, 1=KEY ON */
515
516
/* LFO */
517
UINT32 AMmask; /* AM enable flag */
518
519
} FM_SLOT;
520
521
typedef struct
522
{
523
FM_SLOT SLOT[4]; /* four SLOTs (operators) */
524
525
UINT8 ALGO; /* algorithm */
526
UINT8 FB; /* feedback shift */
527
INT32 op1_out[2]; /* op1 output for feedback */
528
529
INT32 *connect1; /* SLOT1 output pointer */
530
INT32 *connect3; /* SLOT3 output pointer */
531
INT32 *connect2; /* SLOT2 output pointer */
532
INT32 *connect4; /* SLOT4 output pointer */
533
534
INT32 *mem_connect; /* where to put the delayed sample (MEM) */
535
INT32 mem_value; /* delayed sample (MEM) value */
536
537
INT32 pms; /* channel PMS */
538
UINT8 ams; /* channel AMS */
539
540
UINT32 fc; /* fnum,blk */
541
UINT8 kcode; /* key code */
542
UINT32 block_fnum; /* blk/fnum value (for LFO PM calculations) */
543
} FM_CH;
544
545
546
typedef struct
547
{
548
UINT16 address; /* address register */
549
UINT8 status; /* status flag */
550
UINT32 mode; /* mode CSM / 3SLOT */
551
UINT8 fn_h; /* freq latch */
552
INT32 TA; /* timer a value */
553
INT32 TAL; /* timer a base */
554
INT32 TAC; /* timer a counter */
555
INT32 TB; /* timer b value */
556
INT32 TBL; /* timer b base */
557
INT32 TBC; /* timer b counter */
558
INT32 dt_tab[8][32]; /* DeTune table */
559
560
} FM_ST;
561
562
563
/***********************************************************/
564
/* OPN unit */
565
/***********************************************************/
566
567
/* OPN 3slot struct */
568
typedef struct
569
{
570
UINT32 fc[3]; /* fnum3,blk3: calculated */
571
UINT8 fn_h; /* freq3 latch */
572
UINT8 kcode[3]; /* key code */
573
UINT32 block_fnum[3]; /* current fnum value for this slot (can be different betweeen slots of one channel in 3slot mode) */
574
UINT8 key_csm; /* CSM mode Key-ON flag */
575
576
} FM_3SLOT;
577
578
/* OPN/A/B common state */
579
typedef struct
580
{
581
FM_ST ST; /* general state */
582
FM_3SLOT SL3; /* 3 slot mode state */
583
unsigned int pan[6*2]; /* fm channels output masks (0xffffffff = enable) */
584
585
/* EG */
586
UINT32 eg_cnt; /* global envelope generator counter */
587
UINT32 eg_timer; /* global envelope generator counter works at frequency = chipclock/144/3 */
588
589
/* LFO */
590
UINT8 lfo_cnt; /* current LFO phase (out of 128) */
591
UINT32 lfo_timer; /* current LFO phase runs at LFO frequency */
592
UINT32 lfo_timer_overflow; /* LFO timer overflows every N samples (depends on LFO frequency) */
593
UINT32 LFO_AM; /* current LFO AM step */
594
UINT32 LFO_PM; /* current LFO PM step */
595
596
} FM_OPN;
597
598
/***********************************************************/
599
/* YM2612 chip */
600
/***********************************************************/
601
typedef struct
602
{
603
FM_CH CH[6]; /* channel state */
604
UINT8 dacen; /* DAC mode */
605
INT32 dacout; /* DAC output */
606
FM_OPN OPN; /* OPN state */
607
608
} YM2612;
609
610
/* emulated chip */
611
YM2612 ym2612;
612
613
/* current chip state */
614
INT32 m2,c1,c2; /* Phase Modulation input for operators 2,3,4 */
615
INT32 mem; /* one sample delay memory */
616
INT32 out_fm[8]; /* outputs of working channels */
617
UINT32 bitmask; /* working channels output bitmasking (DAC quantization) */
618
619
620
INLINE void FM_KEYON(FM_CH *CH , int s )
621
{
622
FM_SLOT *SLOT = &CH->SLOT[s];
623
624
if (!SLOT->key && !ym2612.OPN.SL3.key_csm)
625
{
626
/* restart Phase Generator */
627
SLOT->phase = 0;
628
629
/* reset SSG-EG inversion flag */
630
SLOT->ssgn = 0;
631
632
if ((SLOT->ar + SLOT->ksr) < 94 /*32+62*/)
633
{
634
SLOT->state = (SLOT->volume <= MIN_ATT_INDEX) ? ((SLOT->sl == MIN_ATT_INDEX) ? EG_SUS : EG_DEC) : EG_ATT;
635
}
636
else
637
{
638
/* force attenuation level to 0 */
639
SLOT->volume = MIN_ATT_INDEX;
640
641
/* directly switch to Decay (or Sustain) */
642
SLOT->state = (SLOT->sl == MIN_ATT_INDEX) ? EG_SUS : EG_DEC;
643
}
644
645
/* recalculate EG output */
646
if ((SLOT->ssg&0x08) && (SLOT->ssgn ^ (SLOT->ssg&0x04)))
647
SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;
648
else
649
SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
650
}
651
652
SLOT->key = 1;
653
}
654
655
INLINE void FM_KEYOFF(FM_CH *CH , int s )
656
{
657
FM_SLOT *SLOT = &CH->SLOT[s];
658
659
if (SLOT->key && !ym2612.OPN.SL3.key_csm)
660
{
661
if (SLOT->state>EG_REL)
662
{
663
SLOT->state = EG_REL; /* phase -> Release */
664
665
/* SSG-EG specific update */
666
if (SLOT->ssg&0x08)
667
{
668
/* convert EG attenuation level */
669
if (SLOT->ssgn ^ (SLOT->ssg&0x04))
670
SLOT->volume = (0x200 - SLOT->volume);
671
672
/* force EG attenuation level */
673
if (SLOT->volume >= 0x200)
674
{
675
SLOT->volume = MAX_ATT_INDEX;
676
SLOT->state = EG_OFF;
677
}
678
679
/* recalculate EG output */
680
SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
681
}
682
}
683
}
684
685
SLOT->key = 0;
686
}
687
688
INLINE void FM_KEYON_CSM(FM_CH *CH , int s )
689
{
690
FM_SLOT *SLOT = &CH->SLOT[s];
691
692
if (!SLOT->key && !ym2612.OPN.SL3.key_csm)
693
{
694
/* restart Phase Generator */
695
SLOT->phase = 0;
696
697
/* reset SSG-EG inversion flag */
698
SLOT->ssgn = 0;
699
700
if ((SLOT->ar + SLOT->ksr) < 94 /*32+62*/)
701
{
702
SLOT->state = (SLOT->volume <= MIN_ATT_INDEX) ? ((SLOT->sl == MIN_ATT_INDEX) ? EG_SUS : EG_DEC) : EG_ATT;
703
}
704
else
705
{
706
/* force attenuation level to 0 */
707
SLOT->volume = MIN_ATT_INDEX;
708
709
/* directly switch to Decay (or Sustain) */
710
SLOT->state = (SLOT->sl == MIN_ATT_INDEX) ? EG_SUS : EG_DEC;
711
}
712
713
/* recalculate EG output */
714
if ((SLOT->ssg&0x08) && (SLOT->ssgn ^ (SLOT->ssg&0x04)))
715
SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;
716
else
717
SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
718
}
719
}
720
721
INLINE void FM_KEYOFF_CSM(FM_CH *CH , int s )
722
{
723
FM_SLOT *SLOT = &CH->SLOT[s];
724
if (!SLOT->key)
725
{
726
if (SLOT->state>EG_REL)
727
{
728
SLOT->state = EG_REL; /* phase -> Release */
729
730
/* SSG-EG specific update */
731
if (SLOT->ssg&0x08)
732
{
733
/* convert EG attenuation level */
734
if (SLOT->ssgn ^ (SLOT->ssg&0x04))
735
SLOT->volume = (0x200 - SLOT->volume);
736
737
/* force EG attenuation level */
738
if (SLOT->volume >= 0x200)
739
{
740
SLOT->volume = MAX_ATT_INDEX;
741
SLOT->state = EG_OFF;
742
}
743
744
/* recalculate EG output */
745
SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
746
}
747
}
748
}
749
}
750
751
/* CSM Key Controll */
752
INLINE void CSMKeyControll(FM_CH *CH)
753
{
754
/* all key ON (verified by Nemesis on real hardware) */
755
FM_KEYON_CSM(CH,SLOT1);
756
FM_KEYON_CSM(CH,SLOT2);
757
FM_KEYON_CSM(CH,SLOT3);
758
FM_KEYON_CSM(CH,SLOT4);
759
ym2612.OPN.SL3.key_csm = 1;
760
}
761
762
INLINE void INTERNAL_TIMER_A()
763
{
764
if (ym2612.OPN.ST.mode & 0x01)
765
{
766
ym2612.OPN.ST.TAC--;
767
if (ym2612.OPN.ST.TAC <= 0)
768
{
769
/* set status (if enabled) */
770
if (ym2612.OPN.ST.mode & 0x04)
771
ym2612.OPN.ST.status |= 0x01;
772
773
/* reload the counter */
774
ym2612.OPN.ST.TAC = ym2612.OPN.ST.TAL;
775
776
/* CSM mode auto key on */
777
if ((ym2612.OPN.ST.mode & 0xC0) == 0x80)
778
CSMKeyControll(&ym2612.CH[2]);
779
}
780
}
781
}
782
783
INLINE void INTERNAL_TIMER_B(int step)
784
{
785
if (ym2612.OPN.ST.mode & 0x02)
786
{
787
ym2612.OPN.ST.TBC-=step;
788
if (ym2612.OPN.ST.TBC <= 0)
789
{
790
/* set status (if enabled) */
791
if (ym2612.OPN.ST.mode & 0x08)
792
ym2612.OPN.ST.status |= 0x02;
793
794
/* reload the counter */
795
if (ym2612.OPN.ST.TBL)
796
ym2612.OPN.ST.TBC += ym2612.OPN.ST.TBL;
797
else
798
ym2612.OPN.ST.TBC = ym2612.OPN.ST.TBL;
799
}
800
}
801
}
802
803
/* OPN Mode Register Write */
804
INLINE void set_timers(int v )
805
{
806
/* b7 = CSM MODE */
807
/* b6 = 3 slot mode */
808
/* b5 = reset b */
809
/* b4 = reset a */
810
/* b3 = timer enable b */
811
/* b2 = timer enable a */
812
/* b1 = load b */
813
/* b0 = load a */
814
815
if ((ym2612.OPN.ST.mode ^ v) & 0xC0)
816
{
817
/* phase increment need to be recalculated */
818
ym2612.CH[2].SLOT[SLOT1].Incr=-1;
819
820
/* CSM mode disabled and CSM key ON active*/
821
if (((v & 0xC0) != 0x80) && ym2612.OPN.SL3.key_csm)
822
{
823
/* CSM Mode Key OFF (verified by Nemesis on real hardware) */
824
FM_KEYOFF_CSM(&ym2612.CH[2],SLOT1);
825
FM_KEYOFF_CSM(&ym2612.CH[2],SLOT2);
826
FM_KEYOFF_CSM(&ym2612.CH[2],SLOT3);
827
FM_KEYOFF_CSM(&ym2612.CH[2],SLOT4);
828
ym2612.OPN.SL3.key_csm = 0;
829
}
830
}
831
832
/* reload Timers */
833
if ((v&1) && !(ym2612.OPN.ST.mode&1))
834
ym2612.OPN.ST.TAC = ym2612.OPN.ST.TAL;
835
if ((v&2) && !(ym2612.OPN.ST.mode&2))
836
ym2612.OPN.ST.TBC = ym2612.OPN.ST.TBL;
837
838
/* reset Timers flags */
839
ym2612.OPN.ST.status &= (~v >> 4);
840
841
ym2612.OPN.ST.mode = v;
842
}
843
844
/* set algorithm connection */
845
INLINE void setup_connection( FM_CH *CH, int ch )
846
{
847
INT32 *carrier = &out_fm[ch];
848
849
INT32 **om1 = &CH->connect1;
850
INT32 **om2 = &CH->connect3;
851
INT32 **oc1 = &CH->connect2;
852
853
INT32 **memc = &CH->mem_connect;
854
855
switch( CH->ALGO ){
856
case 0:
857
/* M1---C1---MEM---M2---C2---OUT */
858
*om1 = &c1;
859
*oc1 = &mem;
860
*om2 = &c2;
861
*memc= &m2;
862
break;
863
case 1:
864
/* M1------+-MEM---M2---C2---OUT */
865
/* C1-+ */
866
*om1 = &mem;
867
*oc1 = &mem;
868
*om2 = &c2;
869
*memc= &m2;
870
break;
871
case 2:
872
/* M1-----------------+-C2---OUT */
873
/* C1---MEM---M2-+ */
874
*om1 = &c2;
875
*oc1 = &mem;
876
*om2 = &c2;
877
*memc= &m2;
878
break;
879
case 3:
880
/* M1---C1---MEM------+-C2---OUT */
881
/* M2-+ */
882
*om1 = &c1;
883
*oc1 = &mem;
884
*om2 = &c2;
885
*memc= &c2;
886
break;
887
case 4:
888
/* M1---C1-+-OUT */
889
/* M2---C2-+ */
890
/* MEM: not used */
891
*om1 = &c1;
892
*oc1 = carrier;
893
*om2 = &c2;
894
*memc= &mem; /* store it anywhere where it will not be used */
895
break;
896
case 5:
897
/* +----C1----+ */
898
/* M1-+-MEM---M2-+-OUT */
899
/* +----C2----+ */
900
*om1 = 0; /* special mark */
901
*oc1 = carrier;
902
*om2 = carrier;
903
*memc= &m2;
904
break;
905
case 6:
906
/* M1---C1-+ */
907
/* M2-+-OUT */
908
/* C2-+ */
909
/* MEM: not used */
910
*om1 = &c1;
911
*oc1 = carrier;
912
*om2 = carrier;
913
*memc= &mem; /* store it anywhere where it will not be used */
914
break;
915
case 7:
916
/* M1-+ */
917
/* C1-+-OUT */
918
/* M2-+ */
919
/* C2-+ */
920
/* MEM: not used*/
921
*om1 = carrier;
922
*oc1 = carrier;
923
*om2 = carrier;
924
*memc= &mem; /* store it anywhere where it will not be used */
925
break;
926
}
927
928
CH->connect4 = carrier;
929
}
930
931
/* set detune & multiple */
932
INLINE void set_det_mul(FM_CH *CH,FM_SLOT *SLOT,int v)
933
{
934
SLOT->mul = (v&0x0f)? (v&0x0f)*2 : 1;
935
SLOT->DT = ym2612.OPN.ST.dt_tab[(v>>4)&7];
936
CH->SLOT[SLOT1].Incr=-1;
937
}
938
939
/* set total level */
940
INLINE void set_tl(FM_SLOT *SLOT , int v)
941
{
942
SLOT->tl = (v&0x7f)<<(ENV_BITS-7); /* 7bit TL */
943
944
/* recalculate EG output */
945
if ((SLOT->ssg&0x08) && (SLOT->ssgn ^ (SLOT->ssg&0x04)) && (SLOT->state > EG_REL))
946
SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;
947
else
948
SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
949
}
950
951
/* set attack rate & key scale */
952
INLINE void set_ar_ksr(FM_CH *CH,FM_SLOT *SLOT,int v)
953
{
954
UINT8 old_KSR = SLOT->KSR;
955
956
SLOT->ar = (v&0x1f) ? 32 + ((v&0x1f)<<1) : 0;
957
958
SLOT->KSR = 3-(v>>6);
959
if (SLOT->KSR != old_KSR)
960
{
961
CH->SLOT[SLOT1].Incr=-1;
962
}
963
964
/* Even if it seems unnecessary to do it here, it could happen that KSR and KC */
965
/* are modified but the resulted SLOT->ksr value (kc >> SLOT->KSR) remains unchanged. */
966
/* In such case, Attack Rate would not be recalculated by "refresh_fc_eg_slot". */
967
/* This actually fixes the intro of "The Adventures of Batman & Robin" (Eke-Eke) */
968
if ((SLOT->ar + SLOT->ksr) < (32+62))
969
{
970
SLOT->eg_sh_ar = eg_rate_shift [SLOT->ar + SLOT->ksr ];
971
SLOT->eg_sel_ar = eg_rate_select[SLOT->ar + SLOT->ksr ];
972
}
973
else
974
{
975
/* verified by Nemesis on real hardware (Attack phase is blocked) */
976
SLOT->eg_sh_ar = 0;
977
SLOT->eg_sel_ar = 18*RATE_STEPS;
978
}
979
}
980
981
/* set decay rate */
982
INLINE void set_dr(FM_SLOT *SLOT,int v)
983
{
984
SLOT->d1r = (v&0x1f) ? 32 + ((v&0x1f)<<1) : 0;
985
986
SLOT->eg_sh_d1r = eg_rate_shift [SLOT->d1r + SLOT->ksr];
987
SLOT->eg_sel_d1r= eg_rate_select[SLOT->d1r + SLOT->ksr];
988
989
}
990
991
/* set sustain rate */
992
INLINE void set_sr(FM_SLOT *SLOT,int v)
993
{
994
SLOT->d2r = (v&0x1f) ? 32 + ((v&0x1f)<<1) : 0;
995
996
SLOT->eg_sh_d2r = eg_rate_shift [SLOT->d2r + SLOT->ksr];
997
SLOT->eg_sel_d2r= eg_rate_select[SLOT->d2r + SLOT->ksr];
998
}
999
1000
/* set release rate */
1001
INLINE void set_sl_rr(FM_SLOT *SLOT,int v)
1002
{
1003
SLOT->sl = sl_table[ v>>4 ];
1004
1005
/* check EG state changes */
1006
if ((SLOT->state == EG_DEC) && (SLOT->volume >= (INT32)(SLOT->sl)))
1007
SLOT->state = EG_SUS;
1008
1009
SLOT->rr = 34 + ((v&0x0f)<<2);
1010
1011
SLOT->eg_sh_rr = eg_rate_shift [SLOT->rr + SLOT->ksr];
1012
SLOT->eg_sel_rr = eg_rate_select[SLOT->rr + SLOT->ksr];
1013
}
1014
1015
/* advance LFO to next sample */
1016
INLINE void advance_lfo()
1017
{
1018
if (ym2612.OPN.lfo_timer_overflow) /* LFO enabled ? */
1019
{
1020
/* increment LFO timer (every samples) */
1021
ym2612.OPN.lfo_timer ++;
1022
1023
/* when LFO is enabled, one level will last for 108, 77, 71, 67, 62, 44, 8 or 5 samples */
1024
if (ym2612.OPN.lfo_timer >= ym2612.OPN.lfo_timer_overflow)
1025
{
1026
ym2612.OPN.lfo_timer = 0;
1027
1028
/* There are 128 LFO steps */
1029
ym2612.OPN.lfo_cnt = ( ym2612.OPN.lfo_cnt + 1 ) & 127;
1030
1031
/* triangle (inverted) */
1032
/* AM: from 126 to 0 step -2, 0 to 126 step +2 */
1033
if (ym2612.OPN.lfo_cnt<64)
1034
ym2612.OPN.LFO_AM = (ym2612.OPN.lfo_cnt ^ 63) << 1;
1035
else
1036
ym2612.OPN.LFO_AM = (ym2612.OPN.lfo_cnt & 63) << 1;
1037
1038
/* PM works with 4 times slower clock */
1039
ym2612.OPN.LFO_PM = ym2612.OPN.lfo_cnt >> 2;
1040
}
1041
}
1042
}
1043
1044
1045
INLINE void advance_eg_channels(FM_CH *CH, unsigned int eg_cnt)
1046
{
1047
unsigned int i = 6; /* six channels */
1048
unsigned int j;
1049
FM_SLOT *SLOT;
1050
1051
do
1052
{
1053
SLOT = &CH->SLOT[SLOT1];
1054
j = 4; /* four operators per channel */
1055
do
1056
{
1057
switch(SLOT->state)
1058
{
1059
case EG_ATT: /* attack phase */
1060
{
1061
if (!(eg_cnt & ((1<<SLOT->eg_sh_ar)-1)))
1062
{
1063
/* update attenuation level */
1064
SLOT->volume += (~SLOT->volume * (eg_inc[SLOT->eg_sel_ar + ((eg_cnt>>SLOT->eg_sh_ar)&7)]))>>4;
1065
1066
/* check phase transition*/
1067
if (SLOT->volume <= MIN_ATT_INDEX)
1068
{
1069
SLOT->volume = MIN_ATT_INDEX;
1070
SLOT->state = (SLOT->sl == MIN_ATT_INDEX) ? EG_SUS : EG_DEC; /* special case where SL=0 */
1071
}
1072
1073
/* recalculate EG output */
1074
if ((SLOT->ssg&0x08) && (SLOT->ssgn ^ (SLOT->ssg&0x04))) /* SSG-EG Output Inversion */
1075
SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;
1076
else
1077
SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
1078
}
1079
break;
1080
}
1081
1082
case EG_DEC: /* decay phase */
1083
{
1084
if (!(eg_cnt & ((1<<SLOT->eg_sh_d1r)-1)))
1085
{
1086
/* SSG EG type */
1087
if (SLOT->ssg&0x08)
1088
{
1089
/* update attenuation level */
1090
if (SLOT->volume < 0x200)
1091
{
1092
SLOT->volume += 4 * eg_inc[SLOT->eg_sel_d1r + ((eg_cnt>>SLOT->eg_sh_d1r)&7)];
1093
1094
/* recalculate EG output */
1095
if (SLOT->ssgn ^ (SLOT->ssg&0x04)) /* SSG-EG Output Inversion */
1096
SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;
1097
else
1098
SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
1099
}
1100
}
1101
else
1102
{
1103
/* update attenuation level */
1104
SLOT->volume += eg_inc[SLOT->eg_sel_d1r + ((eg_cnt>>SLOT->eg_sh_d1r)&7)];
1105
1106
/* recalculate EG output */
1107
SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
1108
}
1109
1110
/* check phase transition*/
1111
if (SLOT->volume >= (INT32)(SLOT->sl))
1112
SLOT->state = EG_SUS;
1113
}
1114
break;
1115
}
1116
1117
case EG_SUS: /* sustain phase */
1118
{
1119
if (!(eg_cnt & ((1<<SLOT->eg_sh_d2r)-1)))
1120
{
1121
/* SSG EG type */
1122
if (SLOT->ssg&0x08)
1123
{
1124
/* update attenuation level */
1125
if (SLOT->volume < 0x200)
1126
{
1127
SLOT->volume += 4 * eg_inc[SLOT->eg_sel_d2r + ((eg_cnt>>SLOT->eg_sh_d2r)&7)];
1128
1129
/* recalculate EG output */
1130
if (SLOT->ssgn ^ (SLOT->ssg&0x04)) /* SSG-EG Output Inversion */
1131
SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;
1132
else
1133
SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
1134
}
1135
}
1136
else
1137
{
1138
/* update attenuation level */
1139
SLOT->volume += eg_inc[SLOT->eg_sel_d2r + ((eg_cnt>>SLOT->eg_sh_d2r)&7)];
1140
1141
/* check phase transition*/
1142
if ( SLOT->volume >= MAX_ATT_INDEX )
1143
SLOT->volume = MAX_ATT_INDEX;
1144
/* do not change SLOT->state (verified on real chip) */
1145
1146
/* recalculate EG output */
1147
SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
1148
}
1149
}
1150
break;
1151
}
1152
1153
case EG_REL: /* release phase */
1154
{
1155
if (!(eg_cnt & ((1<<SLOT->eg_sh_rr)-1)))
1156
{
1157
/* SSG EG type */
1158
if (SLOT->ssg&0x08)
1159
{
1160
/* update attenuation level */
1161
if (SLOT->volume < 0x200)
1162
SLOT->volume += 4 * eg_inc[SLOT->eg_sel_rr + ((eg_cnt>>SLOT->eg_sh_rr)&7)];
1163
1164
/* check phase transition */
1165
if (SLOT->volume >= 0x200)
1166
{
1167
SLOT->volume = MAX_ATT_INDEX;
1168
SLOT->state = EG_OFF;
1169
}
1170
}
1171
else
1172
{
1173
/* update attenuation level */
1174
SLOT->volume += eg_inc[SLOT->eg_sel_rr + ((eg_cnt>>SLOT->eg_sh_rr)&7)];
1175
1176
/* check phase transition*/
1177
if (SLOT->volume >= MAX_ATT_INDEX)
1178
{
1179
SLOT->volume = MAX_ATT_INDEX;
1180
SLOT->state = EG_OFF;
1181
}
1182
}
1183
1184
/* recalculate EG output */
1185
SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
1186
1187
}
1188
break;
1189
}
1190
}
1191
1192
/* next slot */
1193
SLOT++;
1194
} while (--j);
1195
1196
/* next channel */
1197
CH++;
1198
} while (--i);
1199
}
1200
1201
/* SSG-EG update process */
1202
/* The behavior is based upon Nemesis tests on real hardware */
1203
/* This is actually executed before each samples */
1204
INLINE void update_ssg_eg_channels(FM_CH *CH)
1205
{
1206
unsigned int i = 6; /* six channels */
1207
unsigned int j;
1208
FM_SLOT *SLOT;
1209
1210
do
1211
{
1212
j = 4; /* four operators per channel */
1213
SLOT = &CH->SLOT[SLOT1];
1214
1215
do
1216
{
1217
/* detect SSG-EG transition */
1218
/* this is not required during release phase as the attenuation has been forced to MAX and output invert flag is not used */
1219
/* if an Attack Phase is programmed, inversion can occur on each sample */
1220
if ((SLOT->ssg & 0x08) && (SLOT->volume >= 0x200) && (SLOT->state > EG_REL))
1221
{
1222
if (SLOT->ssg & 0x01) /* bit 0 = hold SSG-EG */
1223
{
1224
/* set inversion flag */
1225
if (SLOT->ssg & 0x02)
1226
SLOT->ssgn = 4;
1227
1228
/* force attenuation level during decay phases */
1229
if ((SLOT->state != EG_ATT) && !(SLOT->ssgn ^ (SLOT->ssg & 0x04)))
1230
SLOT->volume = MAX_ATT_INDEX;
1231
}
1232
else /* loop SSG-EG */
1233
{
1234
/* toggle output inversion flag or reset Phase Generator */
1235
if (SLOT->ssg & 0x02)
1236
SLOT->ssgn ^= 4;
1237
else
1238
SLOT->phase = 0;
1239
1240
/* same as Key ON */
1241
if (SLOT->state != EG_ATT)
1242
{
1243
if ((SLOT->ar + SLOT->ksr) < 94 /*32+62*/)
1244
{
1245
SLOT->state = (SLOT->volume <= MIN_ATT_INDEX) ? ((SLOT->sl == MIN_ATT_INDEX) ? EG_SUS : EG_DEC) : EG_ATT;
1246
}
1247
else
1248
{
1249
/* Attack Rate is maximal: directly switch to Decay or Substain */
1250
SLOT->volume = MIN_ATT_INDEX;
1251
SLOT->state = (SLOT->sl == MIN_ATT_INDEX) ? EG_SUS : EG_DEC;
1252
}
1253
}
1254
}
1255
1256
/* recalculate EG output */
1257
if (SLOT->ssgn ^ (SLOT->ssg&0x04))
1258
SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;
1259
else
1260
SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
1261
}
1262
1263
/* next slot */
1264
SLOT++;
1265
} while (--j);
1266
1267
/* next channel */
1268
CH++;
1269
} while (--i);
1270
}
1271
1272
INLINE void update_phase_lfo_slot(FM_SLOT *SLOT, INT32 pms, UINT32 block_fnum)
1273
{
1274
INT32 lfo_fn_table_index_offset = lfo_pm_table[(((block_fnum & 0x7f0) >> 4) << 8) + pms + ym2612.OPN.LFO_PM];
1275
1276
if (lfo_fn_table_index_offset) /* LFO phase modulation active */
1277
{
1278
UINT8 blk;
1279
unsigned int kc, fc;
1280
1281
/* there are 2048 FNUMs that can be generated using FNUM/BLK registers
1282
but LFO works with one more bit of a precision so we really need 4096 elements */
1283
block_fnum = block_fnum*2 + lfo_fn_table_index_offset;
1284
blk = (block_fnum&0x7000) >> 12;
1285
block_fnum = block_fnum & 0xfff;
1286
1287
/* keyscale code */
1288
kc = (blk<<2) | opn_fktable[block_fnum >> 8];
1289
1290
/* (frequency) phase increment counter */
1291
fc = (((block_fnum << 5) >> (7 - blk)) + SLOT->DT[kc]) & DT_MASK;
1292
1293
/* update phase */
1294
SLOT->phase += (fc * SLOT->mul) >> 1;
1295
}
1296
else /* LFO phase modulation = zero */
1297
{
1298
SLOT->phase += SLOT->Incr;
1299
}
1300
}
1301
1302
INLINE void update_phase_lfo_channel(FM_CH *CH)
1303
{
1304
UINT32 block_fnum = CH->block_fnum;
1305
1306
INT32 lfo_fn_table_index_offset = lfo_pm_table[(((block_fnum & 0x7f0) >> 4) << 8) + CH->pms + ym2612.OPN.LFO_PM];
1307
1308
if (lfo_fn_table_index_offset) /* LFO phase modulation active */
1309
{
1310
UINT8 blk;
1311
unsigned int kc, fc, finc;
1312
1313
/* there are 2048 FNUMs that can be generated using FNUM/BLK registers
1314
but LFO works with one more bit of a precision so we really need 4096 elements */
1315
block_fnum = block_fnum*2 + lfo_fn_table_index_offset;
1316
blk = (block_fnum&0x7000) >> 12;
1317
block_fnum = block_fnum & 0xfff;
1318
1319
/* keyscale code */
1320
kc = (blk<<2) | opn_fktable[block_fnum >> 8];
1321
1322
/* (frequency) phase increment counter */
1323
fc = (block_fnum << 5) >> (7 - blk);
1324
1325
/* apply DETUNE & MUL operator specific values */
1326
finc = (fc + CH->SLOT[SLOT1].DT[kc]) & DT_MASK;
1327
CH->SLOT[SLOT1].phase += (finc*CH->SLOT[SLOT1].mul) >> 1;
1328
1329
finc = (fc + CH->SLOT[SLOT2].DT[kc]) & DT_MASK;
1330
CH->SLOT[SLOT2].phase += (finc*CH->SLOT[SLOT2].mul) >> 1;
1331
1332
finc = (fc + CH->SLOT[SLOT3].DT[kc]) & DT_MASK;
1333
CH->SLOT[SLOT3].phase += (finc*CH->SLOT[SLOT3].mul) >> 1;
1334
1335
finc = (fc + CH->SLOT[SLOT4].DT[kc]) & DT_MASK;
1336
CH->SLOT[SLOT4].phase += (finc*CH->SLOT[SLOT4].mul) >> 1;
1337
}
1338
else /* LFO phase modulation = zero */
1339
{
1340
CH->SLOT[SLOT1].phase += CH->SLOT[SLOT1].Incr;
1341
CH->SLOT[SLOT2].phase += CH->SLOT[SLOT2].Incr;
1342
CH->SLOT[SLOT3].phase += CH->SLOT[SLOT3].Incr;
1343
CH->SLOT[SLOT4].phase += CH->SLOT[SLOT4].Incr;
1344
}
1345
}
1346
1347
/* update phase increment and envelope generator */
1348
INLINE void refresh_fc_eg_slot(FM_SLOT *SLOT , unsigned int fc , unsigned int kc )
1349
{
1350
/* add detune value */
1351
fc += SLOT->DT[kc];
1352
1353
/* (frequency) phase overflow (credits to Nemesis) */
1354
fc &= DT_MASK;
1355
1356
/* (frequency) phase increment counter */
1357
SLOT->Incr = (fc * SLOT->mul) >> 1;
1358
1359
/* ksr */
1360
kc = kc >> SLOT->KSR;
1361
1362
if( SLOT->ksr != kc )
1363
{
1364
SLOT->ksr = kc;
1365
1366
/* recalculate envelope generator rates */
1367
if ((SLOT->ar + kc) < (32+62))
1368
{
1369
SLOT->eg_sh_ar = eg_rate_shift [SLOT->ar + kc ];
1370
SLOT->eg_sel_ar = eg_rate_select[SLOT->ar + kc ];
1371
}
1372
else
1373
{
1374
/* verified by Nemesis on real hardware (Attack phase is blocked) */
1375
SLOT->eg_sh_ar = 0;
1376
SLOT->eg_sel_ar = 18*RATE_STEPS;
1377
}
1378
1379
SLOT->eg_sh_d1r = eg_rate_shift [SLOT->d1r + kc];
1380
SLOT->eg_sel_d1r= eg_rate_select[SLOT->d1r + kc];
1381
1382
SLOT->eg_sh_d2r = eg_rate_shift [SLOT->d2r + kc];
1383
SLOT->eg_sel_d2r= eg_rate_select[SLOT->d2r + kc];
1384
1385
SLOT->eg_sh_rr = eg_rate_shift [SLOT->rr + kc];
1386
SLOT->eg_sel_rr = eg_rate_select[SLOT->rr + kc];
1387
}
1388
}
1389
1390
/* update phase increment counters */
1391
INLINE void refresh_fc_eg_chan(FM_CH *CH )
1392
{
1393
if( CH->SLOT[SLOT1].Incr==-1)
1394
{
1395
int fc = CH->fc;
1396
int kc = CH->kcode;
1397
refresh_fc_eg_slot(&CH->SLOT[SLOT1] , fc , kc );
1398
refresh_fc_eg_slot(&CH->SLOT[SLOT2] , fc , kc );
1399
refresh_fc_eg_slot(&CH->SLOT[SLOT3] , fc , kc );
1400
refresh_fc_eg_slot(&CH->SLOT[SLOT4] , fc , kc );
1401
}
1402
}
1403
1404
#define volume_calc(OP) ((OP)->vol_out + (AM & (OP)->AMmask))
1405
1406
INLINE signed int op_calc(UINT32 phase, unsigned int env, unsigned int pm)
1407
{
1408
UINT32 p = (env<<3) + sin_tab[ ( (phase >> SIN_BITS) + (pm >> 1) ) & SIN_MASK ];
1409
1410
if (p >= TL_TAB_LEN)
1411
return 0;
1412
return tl_tab[p];
1413
}
1414
1415
INLINE signed int op_calc1(UINT32 phase, unsigned int env, unsigned int pm)
1416
{
1417
UINT32 p = (env<<3) + sin_tab[ ( (phase + pm ) >> SIN_BITS ) & SIN_MASK ];
1418
1419
if (p >= TL_TAB_LEN)
1420
return 0;
1421
return tl_tab[p];
1422
}
1423
1424
INLINE void chan_calc(FM_CH *CH, int num)
1425
{
1426
do
1427
{
1428
UINT32 AM = ym2612.OPN.LFO_AM >> CH->ams;
1429
unsigned int eg_out = volume_calc(&CH->SLOT[SLOT1]);
1430
1431
m2 = c1 = c2 = mem = 0;
1432
1433
*CH->mem_connect = CH->mem_value; /* restore delayed sample (MEM) value to m2 or c2 */
1434
{
1435
INT32 out = CH->op1_out[0] + CH->op1_out[1];
1436
CH->op1_out[0] = CH->op1_out[1];
1437
1438
if( !CH->connect1 ){
1439
/* algorithm 5 */
1440
mem = c1 = c2 = CH->op1_out[0];
1441
}else{
1442
/* other algorithms */
1443
*CH->connect1 += CH->op1_out[0];
1444
}
1445
1446
CH->op1_out[1] = 0;
1447
if( eg_out < ENV_QUIET ) /* SLOT 1 */
1448
{
1449
if (!CH->FB)
1450
out=0;
1451
1452
CH->op1_out[1] = op_calc1(CH->SLOT[SLOT1].phase, eg_out, (out<<CH->FB) );
1453
}
1454
}
1455
1456
eg_out = volume_calc(&CH->SLOT[SLOT3]);
1457
if( eg_out < ENV_QUIET ) /* SLOT 3 */
1458
*CH->connect3 += op_calc(CH->SLOT[SLOT3].phase, eg_out, m2);
1459
1460
eg_out = volume_calc(&CH->SLOT[SLOT2]);
1461
if( eg_out < ENV_QUIET ) /* SLOT 2 */
1462
*CH->connect2 += op_calc(CH->SLOT[SLOT2].phase, eg_out, c1);
1463
1464
eg_out = volume_calc(&CH->SLOT[SLOT4]);
1465
if( eg_out < ENV_QUIET ) /* SLOT 4 */
1466
*CH->connect4 += op_calc(CH->SLOT[SLOT4].phase, eg_out, c2);
1467
1468
1469
/* store current MEM */
1470
CH->mem_value = mem;
1471
1472
/* update phase counters AFTER output calculations */
1473
if(CH->pms)
1474
{
1475
/* add support for 3 slot mode */
1476
if ((ym2612.OPN.ST.mode & 0xC0) && (CH == &ym2612.CH[2]))
1477
{
1478
update_phase_lfo_slot(&CH->SLOT[SLOT1], CH->pms, ym2612.OPN.SL3.block_fnum[1]);
1479
update_phase_lfo_slot(&CH->SLOT[SLOT2], CH->pms, ym2612.OPN.SL3.block_fnum[2]);
1480
update_phase_lfo_slot(&CH->SLOT[SLOT3], CH->pms, ym2612.OPN.SL3.block_fnum[0]);
1481
update_phase_lfo_slot(&CH->SLOT[SLOT4], CH->pms, CH->block_fnum);
1482
}
1483
else
1484
{
1485
update_phase_lfo_channel(CH);
1486
}
1487
}
1488
else /* no LFO phase modulation */
1489
{
1490
CH->SLOT[SLOT1].phase += CH->SLOT[SLOT1].Incr;
1491
CH->SLOT[SLOT2].phase += CH->SLOT[SLOT2].Incr;
1492
CH->SLOT[SLOT3].phase += CH->SLOT[SLOT3].Incr;
1493
CH->SLOT[SLOT4].phase += CH->SLOT[SLOT4].Incr;
1494
}
1495
1496
/* next channel */
1497
CH++;
1498
} while (--num);
1499
}
1500
1501
/* write a OPN mode register 0x20-0x2f */
1502
INLINE void OPNWriteMode(int r, int v)
1503
{
1504
UINT8 c;
1505
FM_CH *CH;
1506
1507
switch(r){
1508
case 0x21: /* Test */
1509
break;
1510
1511
case 0x22: /* LFO FREQ (YM2608/YM2610/YM2610B/ym2612) */
1512
if (v&8) /* LFO enabled ? */
1513
{
1514
ym2612.OPN.lfo_timer_overflow = lfo_samples_per_step[v&7];
1515
}
1516
else
1517
{
1518
/* hold LFO waveform in reset state */
1519
ym2612.OPN.lfo_timer_overflow = 0;
1520
ym2612.OPN.lfo_timer = 0;
1521
ym2612.OPN.lfo_cnt = 0;
1522
ym2612.OPN.LFO_PM = 0;
1523
ym2612.OPN.LFO_AM = 126;
1524
}
1525
break;
1526
case 0x24: /* timer A High 8*/
1527
ym2612.OPN.ST.TA = (ym2612.OPN.ST.TA & 0x03)|(((int)v)<<2);
1528
ym2612.OPN.ST.TAL = 1024 - ym2612.OPN.ST.TA;
1529
break;
1530
case 0x25: /* timer A Low 2*/
1531
ym2612.OPN.ST.TA = (ym2612.OPN.ST.TA & 0x3fc)|(v&3);
1532
ym2612.OPN.ST.TAL = 1024 - ym2612.OPN.ST.TA;
1533
break;
1534
case 0x26: /* timer B */
1535
ym2612.OPN.ST.TB = v;
1536
ym2612.OPN.ST.TBL = (256 - v) << 4;
1537
break;
1538
case 0x27: /* mode, timer control */
1539
set_timers(v);
1540
break;
1541
case 0x28: /* key on / off */
1542
c = v & 0x03;
1543
if( c == 3 ) break;
1544
if (v&0x04) c+=3; /* CH 4-6 */
1545
CH = &ym2612.CH[c];
1546
1547
if (v&0x10) FM_KEYON(CH,SLOT1); else FM_KEYOFF(CH,SLOT1);
1548
if (v&0x20) FM_KEYON(CH,SLOT2); else FM_KEYOFF(CH,SLOT2);
1549
if (v&0x40) FM_KEYON(CH,SLOT3); else FM_KEYOFF(CH,SLOT3);
1550
if (v&0x80) FM_KEYON(CH,SLOT4); else FM_KEYOFF(CH,SLOT4);
1551
break;
1552
}
1553
}
1554
1555
/* write a OPN register (0x30-0xff) */
1556
INLINE void OPNWriteReg(int r, int v)
1557
{
1558
FM_CH *CH;
1559
FM_SLOT *SLOT;
1560
1561
UINT8 c = OPN_CHAN(r);
1562
1563
if (c == 3) return; /* 0xX3,0xX7,0xXB,0xXF */
1564
1565
if (r >= 0x100) c+=3;
1566
1567
CH = &ym2612.CH[c];
1568
1569
SLOT = &(CH->SLOT[OPN_SLOT(r)]);
1570
1571
switch( r & 0xf0 ) {
1572
case 0x30: /* DET , MUL */
1573
set_det_mul(CH,SLOT,v);
1574
break;
1575
1576
case 0x40: /* TL */
1577
set_tl(SLOT,v);
1578
break;
1579
1580
case 0x50: /* KS, AR */
1581
set_ar_ksr(CH,SLOT,v);
1582
break;
1583
1584
case 0x60: /* bit7 = AM ENABLE, DR */
1585
set_dr(SLOT,v);
1586
SLOT->AMmask = (v&0x80) ? ~0 : 0;
1587
break;
1588
1589
case 0x70: /* SR */
1590
set_sr(SLOT,v);
1591
break;
1592
1593
case 0x80: /* SL, RR */
1594
set_sl_rr(SLOT,v);
1595
break;
1596
1597
case 0x90: /* SSG-EG */
1598
SLOT->ssg = v&0x0f;
1599
1600
/* recalculate EG output */
1601
if (SLOT->state > EG_REL)
1602
{
1603
if ((SLOT->ssg&0x08) && (SLOT->ssgn ^ (SLOT->ssg&0x04)))
1604
SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;
1605
else
1606
SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
1607
}
1608
1609
/* SSG-EG envelope shapes :
1610
1611
E AtAlH
1612
1 0 0 0 \\\\
1613
1614
1 0 0 1 \___
1615
1616
1 0 1 0 \/\/
1617
___
1618
1 0 1 1 \
1619
1620
1 1 0 0 ////
1621
___
1622
1 1 0 1 /
1623
1624
1 1 1 0 /\/\
1625
1626
1 1 1 1 /___
1627
1628
1629
E = SSG-EG enable
1630
1631
1632
The shapes are generated using Attack, Decay and Sustain phases.
1633
1634
Each single character in the diagrams above represents this whole
1635
sequence:
1636
1637
- when KEY-ON = 1, normal Attack phase is generated (*without* any
1638
difference when compared to normal mode),
1639
1640
- later, when envelope level reaches minimum level (max volume),
1641
the EG switches to Decay phase (which works with bigger steps
1642
when compared to normal mode - see below),
1643
1644
- later when envelope level passes the SL level,
1645
the EG swithes to Sustain phase (which works with bigger steps
1646
when compared to normal mode - see below),
1647
1648
- finally when envelope level reaches maximum level (min volume),
1649
the EG switches to Attack phase again (depends on actual waveform).
1650
1651
Important is that when switch to Attack phase occurs, the phase counter
1652
of that operator will be zeroed-out (as in normal KEY-ON) but not always.
1653
(I havent found the rule for that - perhaps only when the output level is low)
1654
1655
The difference (when compared to normal Envelope Generator mode) is
1656
that the resolution in Decay and Sustain phases is 4 times lower;
1657
this results in only 256 steps instead of normal 1024.
1658
In other words:
1659
when SSG-EG is disabled, the step inside of the EG is one,
1660
when SSG-EG is enabled, the step is four (in Decay and Sustain phases).
1661
1662
Times between the level changes are the same in both modes.
1663
1664
1665
Important:
1666
Decay 1 Level (so called SL) is compared to actual SSG-EG output, so
1667
it is the same in both SSG and no-SSG modes, with this exception:
1668
1669
when the SSG-EG is enabled and is generating raising levels
1670
(when the EG output is inverted) the SL will be found at wrong level !!!
1671
For example, when SL=02:
1672
0 -6 = -6dB in non-inverted EG output
1673
96-6 = -90dB in inverted EG output
1674
Which means that EG compares its level to SL as usual, and that the
1675
output is simply inverted afterall.
1676
1677
1678
The Yamaha's manuals say that AR should be set to 0x1f (max speed).
1679
That is not necessary, but then EG will be generating Attack phase.
1680
1681
*/
1682
1683
1684
break;
1685
1686
case 0xa0:
1687
switch( OPN_SLOT(r) ){
1688
case 0: /* 0xa0-0xa2 : FNUM1 */
1689
{
1690
UINT32 fn = (((UINT32)((ym2612.OPN.ST.fn_h)&7))<<8) + v;
1691
UINT8 blk = ym2612.OPN.ST.fn_h>>3;
1692
/* keyscale code */
1693
CH->kcode = (blk<<2) | opn_fktable[fn >> 7];
1694
/* phase increment counter */
1695
CH->fc = (fn << 6) >> (7 - blk);
1696
1697
/* store fnum in clear form for LFO PM calculations */
1698
CH->block_fnum = (blk<<11) | fn;
1699
1700
CH->SLOT[SLOT1].Incr=-1;
1701
break;
1702
}
1703
case 1: /* 0xa4-0xa6 : FNUM2,BLK */
1704
ym2612.OPN.ST.fn_h = v&0x3f;
1705
break;
1706
case 2: /* 0xa8-0xaa : 3CH FNUM1 */
1707
if(r < 0x100)
1708
{
1709
UINT32 fn = (((UINT32)(ym2612.OPN.SL3.fn_h&7))<<8) + v;
1710
UINT8 blk = ym2612.OPN.SL3.fn_h>>3;
1711
/* keyscale code */
1712
ym2612.OPN.SL3.kcode[c]= (blk<<2) | opn_fktable[fn >> 7];
1713
/* phase increment counter */
1714
ym2612.OPN.SL3.fc[c] = (fn << 6) >> (7 - blk);
1715
ym2612.OPN.SL3.block_fnum[c] = (blk<<11) | fn;
1716
ym2612.CH[2].SLOT[SLOT1].Incr=-1;
1717
}
1718
break;
1719
case 3: /* 0xac-0xae : 3CH FNUM2,BLK */
1720
if(r < 0x100)
1721
ym2612.OPN.SL3.fn_h = v&0x3f;
1722
break;
1723
}
1724
break;
1725
1726
case 0xb0:
1727
switch( OPN_SLOT(r) ){
1728
case 0: /* 0xb0-0xb2 : FB,ALGO */
1729
{
1730
CH->ALGO = v&7;
1731
CH->FB = (v>>3)&7;
1732
setup_connection( CH, c );
1733
break;
1734
}
1735
case 1: /* 0xb4-0xb6 : L , R , AMS , PMS */
1736
/* b0-2 PMS */
1737
CH->pms = (v & 7) * 32; /* CH->pms = PM depth * 32 (index in lfo_pm_table) */
1738
1739
/* b4-5 AMS */
1740
CH->ams = lfo_ams_depth_shift[(v>>4) & 0x03];
1741
1742
/* PAN : b7 = L, b6 = R */
1743
ym2612.OPN.pan[ c*2 ] = (v & 0x80) ? bitmask : 0;
1744
ym2612.OPN.pan[ c*2+1 ] = (v & 0x40) ? bitmask : 0;
1745
break;
1746
}
1747
break;
1748
}
1749
}
1750
1751
static void reset_channels(FM_CH *CH , int num )
1752
{
1753
int c,s;
1754
1755
for( c = 0 ; c < num ; c++ )
1756
{
1757
CH[c].mem_value = 0;
1758
CH[c].op1_out[0] = 0;
1759
CH[c].op1_out[1] = 0;
1760
for(s = 0 ; s < 4 ; s++ )
1761
{
1762
CH[c].SLOT[s].Incr = -1;
1763
CH[c].SLOT[s].key = 0;
1764
CH[c].SLOT[s].phase = 0;
1765
CH[c].SLOT[s].ssgn = 0;
1766
CH[c].SLOT[s].state = EG_OFF;
1767
CH[c].SLOT[s].volume = MAX_ATT_INDEX;
1768
CH[c].SLOT[s].vol_out = MAX_ATT_INDEX;
1769
}
1770
}
1771
}
1772
1773
/* initialize generic tables */
1774
static void init_tables(void)
1775
{
1776
signed int d,i,x;
1777
signed int n;
1778
double o,m;
1779
1780
/* build Linear Power Table */
1781
for (x=0; x<TL_RES_LEN; x++)
1782
{
1783
m = (1<<16) / pow(2,(x+1) * (ENV_STEP/4.0) / 8.0);
1784
m = floor(m);
1785
1786
/* we never reach (1<<16) here due to the (x+1) */
1787
/* result fits within 16 bits at maximum */
1788
1789
n = (int)m; /* 16 bits here */
1790
n >>= 4; /* 12 bits here */
1791
if (n&1) /* round to nearest */
1792
n = (n>>1)+1;
1793
else
1794
n = n>>1;
1795
/* 11 bits here (rounded) */
1796
n <<= 2; /* 13 bits here (as in real chip) */
1797
1798
/* 14 bits (with sign bit) */
1799
tl_tab[ x*2 + 0 ] = n;
1800
tl_tab[ x*2 + 1 ] = -tl_tab[ x*2 + 0 ];
1801
1802
/* one entry in the 'Power' table use the following format, xxxxxyyyyyyyys with: */
1803
/* s = sign bit */
1804
/* yyyyyyyy = 8-bits decimal part (0-TL_RES_LEN) */
1805
/* xxxxx = 5-bits integer 'shift' value (0-31) but, since Power table output is 13 bits, */
1806
/* any value above 13 (included) would be discarded. */
1807
for (i=1; i<13; i++)
1808
{
1809
tl_tab[ x*2+0 + i*2*TL_RES_LEN ] = tl_tab[ x*2+0 ]>>i;
1810
tl_tab[ x*2+1 + i*2*TL_RES_LEN ] = -tl_tab[ x*2+0 + i*2*TL_RES_LEN ];
1811
}
1812
}
1813
1814
/* build Logarithmic Sinus table */
1815
for (i=0; i<SIN_LEN; i++)
1816
{
1817
/* non-standard sinus */
1818
m = sin( ((i*2)+1) * M_PI / SIN_LEN ); /* checked against the real chip */
1819
/* we never reach zero here due to ((i*2)+1) */
1820
1821
if (m>0.0)
1822
o = 8*log(1.0/m)/log(2); /* convert to 'decibels' */
1823
else
1824
o = 8*log(-1.0/m)/log(2); /* convert to 'decibels' */
1825
1826
o = o / (ENV_STEP/4);
1827
1828
n = (int)(2.0*o);
1829
if (n&1) /* round to nearest */
1830
n = (n>>1)+1;
1831
else
1832
n = n>>1;
1833
1834
/* 13-bits (8.5) value is formatted for above 'Power' table */
1835
sin_tab[ i ] = n*2 + (m>=0.0? 0: 1 );
1836
}
1837
1838
/* build LFO PM modulation table */
1839
for(i = 0; i < 8; i++) /* 8 PM depths */
1840
{
1841
UINT8 fnum;
1842
for (fnum=0; fnum<128; fnum++) /* 7 bits meaningful of F-NUMBER */
1843
{
1844
UINT8 value;
1845
UINT8 step;
1846
UINT32 offset_depth = i;
1847
UINT32 offset_fnum_bit;
1848
UINT32 bit_tmp;
1849
1850
for (step=0; step<8; step++)
1851
{
1852
value = 0;
1853
for (bit_tmp=0; bit_tmp<7; bit_tmp++) /* 7 bits */
1854
{
1855
if (fnum & (1<<bit_tmp)) /* only if bit "bit_tmp" is set */
1856
{
1857
offset_fnum_bit = bit_tmp * 8;
1858
value += lfo_pm_output[offset_fnum_bit + offset_depth][step];
1859
}
1860
}
1861
/* 32 steps for LFO PM (sinus) */
1862
lfo_pm_table[(fnum*32*8) + (i*32) + step + 0] = value;
1863
lfo_pm_table[(fnum*32*8) + (i*32) +(step^7)+ 8] = value;
1864
lfo_pm_table[(fnum*32*8) + (i*32) + step +16] = -value;
1865
lfo_pm_table[(fnum*32*8) + (i*32) +(step^7)+24] = -value;
1866
}
1867
}
1868
}
1869
1870
/* build DETUNE table */
1871
for (d = 0;d <= 3;d++)
1872
{
1873
for (i = 0;i <= 31;i++)
1874
{
1875
ym2612.OPN.ST.dt_tab[d][i] = (INT32) dt_tab[d*32 + i];
1876
ym2612.OPN.ST.dt_tab[d+4][i] = -ym2612.OPN.ST.dt_tab[d][i];
1877
}
1878
}
1879
1880
}
1881
1882
1883
1884
/* initialize ym2612 emulator */
1885
void YM2612Init(void)
1886
{
1887
memset(&ym2612,0,sizeof(YM2612));
1888
init_tables();
1889
}
1890
1891
/* reset OPN registers */
1892
void YM2612ResetChip(void)
1893
{
1894
int i;
1895
1896
ym2612.OPN.eg_timer = 0;
1897
ym2612.OPN.eg_cnt = 0;
1898
1899
ym2612.OPN.lfo_timer_overflow = 0;
1900
ym2612.OPN.lfo_timer = 0;
1901
ym2612.OPN.lfo_cnt = 0;
1902
ym2612.OPN.LFO_AM = 126;
1903
ym2612.OPN.LFO_PM = 0;
1904
1905
ym2612.OPN.ST.TAC = 0;
1906
ym2612.OPN.ST.TBC = 0;
1907
1908
ym2612.OPN.SL3.key_csm = 0;
1909
1910
ym2612.dacen = 0;
1911
ym2612.dacout = 0;
1912
1913
set_timers(0x30);
1914
ym2612.OPN.ST.TB = 0;
1915
ym2612.OPN.ST.TBL = 256 << 4;
1916
ym2612.OPN.ST.TA = 0;
1917
ym2612.OPN.ST.TAL = 1024;
1918
1919
reset_channels(&ym2612.CH[0] , 6 );
1920
1921
for(i = 0xb6 ; i >= 0xb4 ; i-- )
1922
{
1923
OPNWriteReg(i ,0xc0);
1924
OPNWriteReg(i|0x100,0xc0);
1925
}
1926
for(i = 0xb2 ; i >= 0x30 ; i-- )
1927
{
1928
OPNWriteReg(i ,0);
1929
OPNWriteReg(i|0x100,0);
1930
}
1931
}
1932
1933
/* ym2612 write */
1934
/* n = number */
1935
/* a = address */
1936
/* v = value */
1937
void YM2612Write(unsigned int a, unsigned int v)
1938
{
1939
v &= 0xff; /* adjust to 8 bit bus */
1940
1941
switch( a )
1942
{
1943
case 0: /* address port 0 */
1944
ym2612.OPN.ST.address = v;
1945
break;
1946
1947
case 2: /* address port 1 */
1948
ym2612.OPN.ST.address = v | 0x100;
1949
break;
1950
1951
default: /* data port */
1952
{
1953
int addr = ym2612.OPN.ST.address; /* verified by Nemesis on real YM2612 */
1954
switch( addr & 0x1f0 )
1955
{
1956
case 0x20: /* 0x20-0x2f Mode */
1957
switch( addr )
1958
{
1959
case 0x2a: /* DAC data (ym2612) */
1960
ym2612.dacout = ((int)v - 0x80) << 6; /* convert to 14-bit output */
1961
break;
1962
case 0x2b: /* DAC Sel (ym2612) */
1963
/* b7 = dac enable */
1964
ym2612.dacen = v & 0x80;
1965
break;
1966
default: /* OPN section */
1967
/* write register */
1968
OPNWriteMode(addr,v);
1969
}
1970
break;
1971
default: /* 0x30-0xff OPN section */
1972
/* write register */
1973
OPNWriteReg(addr,v);
1974
}
1975
break;
1976
}
1977
}
1978
}
1979
1980
unsigned int YM2612Read(void)
1981
{
1982
return ym2612.OPN.ST.status & 0xff;
1983
}
1984
1985
/* Generate samples for ym2612 */
1986
void YM2612Update(int *buffer, int length)
1987
{
1988
int i;
1989
int lt,rt;
1990
1991
/* refresh PG increments and EG rates if required */
1992
refresh_fc_eg_chan(&ym2612.CH[0]);
1993
refresh_fc_eg_chan(&ym2612.CH[1]);
1994
1995
if (!(ym2612.OPN.ST.mode & 0xC0))
1996
{
1997
refresh_fc_eg_chan(&ym2612.CH[2]);
1998
}
1999
else
2000
{
2001
/* 3SLOT MODE (operator order is 0,1,3,2) */
2002
if(ym2612.CH[2].SLOT[SLOT1].Incr==-1)
2003
{
2004
refresh_fc_eg_slot(&ym2612.CH[2].SLOT[SLOT1] , ym2612.OPN.SL3.fc[1] , ym2612.OPN.SL3.kcode[1] );
2005
refresh_fc_eg_slot(&ym2612.CH[2].SLOT[SLOT2] , ym2612.OPN.SL3.fc[2] , ym2612.OPN.SL3.kcode[2] );
2006
refresh_fc_eg_slot(&ym2612.CH[2].SLOT[SLOT3] , ym2612.OPN.SL3.fc[0] , ym2612.OPN.SL3.kcode[0] );
2007
refresh_fc_eg_slot(&ym2612.CH[2].SLOT[SLOT4] , ym2612.CH[2].fc , ym2612.CH[2].kcode );
2008
}
2009
}
2010
2011
refresh_fc_eg_chan(&ym2612.CH[3]);
2012
refresh_fc_eg_chan(&ym2612.CH[4]);
2013
refresh_fc_eg_chan(&ym2612.CH[5]);
2014
2015
/* buffering */
2016
for(i=0; i < length ; i++)
2017
{
2018
/* clear outputs */
2019
out_fm[0] = 0;
2020
out_fm[1] = 0;
2021
out_fm[2] = 0;
2022
out_fm[3] = 0;
2023
out_fm[4] = 0;
2024
out_fm[5] = 0;
2025
2026
/* update SSG-EG output */
2027
update_ssg_eg_channels(&ym2612.CH[0]);
2028
2029
/* calculate FM */
2030
if (!ym2612.dacen)
2031
{
2032
chan_calc(&ym2612.CH[0],6);
2033
}
2034
else
2035
{
2036
/* DAC Mode */
2037
out_fm[5] = ym2612.dacout;
2038
chan_calc(&ym2612.CH[0],5);
2039
}
2040
2041
/* advance LFO */
2042
advance_lfo();
2043
2044
/* advance envelope generator */
2045
ym2612.OPN.eg_timer ++;
2046
2047
/* EG is updated every 3 samples */
2048
if (ym2612.OPN.eg_timer >= 3)
2049
{
2050
ym2612.OPN.eg_timer = 0;
2051
ym2612.OPN.eg_cnt++;
2052
advance_eg_channels(&ym2612.CH[0], ym2612.OPN.eg_cnt);
2053
}
2054
2055
/* 14-bit accumulator channels outputs (range is -8192;+8192) */
2056
if (out_fm[0] > 8192) out_fm[0] = 8192;
2057
else if (out_fm[0] < -8192) out_fm[0] = -8192;
2058
if (out_fm[1] > 8192) out_fm[1] = 8192;
2059
else if (out_fm[1] < -8192) out_fm[1] = -8192;
2060
if (out_fm[2] > 8192) out_fm[2] = 8192;
2061
else if (out_fm[2] < -8192) out_fm[2] = -8192;
2062
if (out_fm[3] > 8192) out_fm[3] = 8192;
2063
else if (out_fm[3] < -8192) out_fm[3] = -8192;
2064
if (out_fm[4] > 8192) out_fm[4] = 8192;
2065
else if (out_fm[4] < -8192) out_fm[4] = -8192;
2066
if (out_fm[5] > 8192) out_fm[5] = 8192;
2067
else if (out_fm[5] < -8192) out_fm[5] = -8192;
2068
2069
/* stereo DAC channels outputs mixing */
2070
lt = ((out_fm[0]) & ym2612.OPN.pan[0]);
2071
rt = ((out_fm[0]) & ym2612.OPN.pan[1]);
2072
lt += ((out_fm[1]) & ym2612.OPN.pan[2]);
2073
rt += ((out_fm[1]) & ym2612.OPN.pan[3]);
2074
lt += ((out_fm[2]) & ym2612.OPN.pan[4]);
2075
rt += ((out_fm[2]) & ym2612.OPN.pan[5]);
2076
lt += ((out_fm[3]) & ym2612.OPN.pan[6]);
2077
rt += ((out_fm[3]) & ym2612.OPN.pan[7]);
2078
lt += ((out_fm[4]) & ym2612.OPN.pan[8]);
2079
rt += ((out_fm[4]) & ym2612.OPN.pan[9]);
2080
lt += ((out_fm[5]) & ym2612.OPN.pan[10]);
2081
rt += ((out_fm[5]) & ym2612.OPN.pan[11]);
2082
2083
/* buffering */
2084
*buffer++ = lt;
2085
*buffer++ = rt;
2086
2087
/* CSM mode: if CSM Key ON has occured, CSM Key OFF need to be sent */
2088
/* only if Timer A does not overflow again (i.e CSM Key ON not set again) */
2089
ym2612.OPN.SL3.key_csm <<= 1;
2090
2091
/* timer A control */
2092
INTERNAL_TIMER_A();
2093
2094
/* CSM Mode Key ON still disabled */
2095
if (ym2612.OPN.SL3.key_csm & 2)
2096
{
2097
/* CSM Mode Key OFF (verified by Nemesis on real hardware) */
2098
FM_KEYOFF_CSM(&ym2612.CH[2],SLOT1);
2099
FM_KEYOFF_CSM(&ym2612.CH[2],SLOT2);
2100
FM_KEYOFF_CSM(&ym2612.CH[2],SLOT3);
2101
FM_KEYOFF_CSM(&ym2612.CH[2],SLOT4);
2102
ym2612.OPN.SL3.key_csm = 0;
2103
}
2104
}
2105
2106
/* timer B control */
2107
INTERNAL_TIMER_B(length);
2108
}
2109
2110
void YM2612Config(unsigned char dac_bits)
2111
{
2112
int i;
2113
2114
/* DAC precision (normally 9-bit on real hardware, implemented through simple 14-bit channel output bitmasking) */
2115
bitmask = ~((1 << (TL_BITS - dac_bits)) - 1);
2116
2117
/* update L/R panning bitmasks */
2118
for (i=0; i<2*6; i++)
2119
{
2120
if (ym2612.OPN.pan[i])
2121
{
2122
ym2612.OPN.pan[i] = bitmask;
2123
}
2124
}
2125
}
2126
2127