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