/*1**2** software implementation of Yamaha FM sound generator (YM2612/YM3438)3**4** Original code (MAME fm.c)5**6** Copyright (C) 2001, 2002, 2003 Jarek Burczynski (bujar at mame dot net)7** Copyright (C) 1998 Tatsuyuki Satoh , MultiArcadeMachineEmulator development8**9** Version 1.4 (final beta)10**11** Additional code & fixes by Eke-Eke for Genesis Plus GX12**13** Huge thanks to Nemesis, most of those fixes came from his tests on Sega Genesis hardware14** More informations at http://gendev.spritesmind.net/forum/viewtopic.php?t=38615**16** TODO:17** - better documentation18** - BUSY flag emulation19*/2021/*22** CHANGELOG:23**24** 01-09-2012 Eke-Eke (Genesis Plus GX):25** - removed input clock / output samplerate frequency ratio, chip now always run at (original) internal sample frequency26** - removed now uneeded extra bits of precision27**28** 2006~2012 Eke-Eke (Genesis Plus GX):29** - removed unused multichip support30** - added YM2612 Context external access functions31** - fixed LFO implementation:32** .added support for CH3 special mode: fixes various sound effects (birds in Warlock, bug sound in Aladdin...)33** .inverted LFO AM waveform: fixes Spider-Man & Venom : Separation Anxiety (intro), California Games (surfing event)34** .improved LFO timing accuracy: now updated AFTER sample output, like EG/PG updates, and without any precision loss anymore.35** - improved internal timers emulation36** - adjusted lowest EG rates increment values37** - fixed Attack Rate not being updated in some specific cases (Batman & Robin intro)38** - fixed EG behavior when Attack Rate is maximal39** - fixed EG behavior when SL=0 (Mega Turrican tracks 03,09...) or/and Key ON occurs at minimal attenuation40** - implemented EG output immediate changes on register writes41** - fixed YM2612 initial values (after the reset): fixes missing intro in B.O.B42** - implemented Detune overflow (Ariel, Comix Zone, Shaq Fu, Spiderman & many other games using GEMS sound engine)43** - implemented accurate CSM mode emulation44** - implemented accurate SSG-EG emulation (Asterix, Beavis&Butthead, Bubba'n Stix & many other games)45** - implemented accurate address/data ports behavior46** - added preliminar support for DAC precision47**48** 03-08-2003 Jarek Burczynski:49** - fixed YM2608 initial values (after the reset)50** - fixed flag and irqmask handling (YM2608)51** - fixed BUFRDY flag handling (YM2608)52**53** 14-06-2003 Jarek Burczynski:54** - implemented all of the YM2608 status register flags55** - implemented support for external memory read/write via YM260856** - implemented support for deltat memory limit register in YM2608 emulation57**58** 22-05-2003 Jarek Burczynski:59** - fixed LFO PM calculations (copy&paste bugfix)60**61** 08-05-2003 Jarek Burczynski:62** - fixed SSG support63**64** 22-04-2003 Jarek Burczynski:65** - implemented 100% correct LFO generator (verified on real YM2610 and YM2608)66**67** 15-04-2003 Jarek Burczynski:68** - added support for YM2608's register 0x110 - status mask69**70** 01-12-2002 Jarek Burczynski:71** - fixed register addressing in YM2608, YM2610, YM2610B chips. (verified on real YM2608)72** The addressing patch used for early Neo-Geo games can be removed now.73**74** 26-11-2002 Jarek Burczynski, Nicola Salmoria:75** - recreated YM2608 ADPCM ROM using data from real YM2608's output which leads to:76** - added emulation of YM2608 drums.77** - output of YM2608 is two times lower now - same as YM2610 (verified on real YM2608)78**79** 16-08-2002 Jarek Burczynski:80** - binary exact Envelope Generator (verified on real YM2203);81** identical to YM215182** - corrected 'off by one' error in feedback calculations (when feedback is off)83** - corrected connection (algorithm) calculation (verified on real YM2203 and YM2610)84**85** 18-12-2001 Jarek Burczynski:86** - added SSG-EG support (verified on real YM2203)87**88** 12-08-2001 Jarek Burczynski:89** - corrected sin_tab and tl_tab data (verified on real chip)90** - corrected feedback calculations (verified on real chip)91** - corrected phase generator calculations (verified on real chip)92** - corrected envelope generator calculations (verified on real chip)93** - corrected FM volume level (YM2610 and YM2610B).94** - changed YMxxxUpdateOne() functions (YM2203, YM2608, YM2610, YM2610B, YM2612) :95** this was needed to calculate YM2610 FM channels output correctly.96** (Each FM channel is calculated as in other chips, but the output of the channel97** gets shifted right by one *before* sending to accumulator. That was impossible to do98** with previous implementation).99**100** 23-07-2001 Jarek Burczynski, Nicola Salmoria:101** - corrected YM2610 ADPCM type A algorithm and tables (verified on real chip)102**103** 11-06-2001 Jarek Burczynski:104** - corrected end of sample bug in ADPCMA_calc_cha().105** Real YM2610 checks for equality between current and end addresses (only 20 LSB bits).106**107** 08-12-98 hiro-shi:108** rename ADPCMA -> ADPCMB, ADPCMB -> ADPCMA109** move ROM limit check.(CALC_CH? -> 2610Write1/2)110** test program (ADPCMB_TEST)111** move ADPCM A/B end check.112** ADPCMB repeat flag(no check)113** change ADPCM volume rate (8->16) (32->48).114**115** 09-12-98 hiro-shi:116** change ADPCM volume. (8->16, 48->64)117** replace ym2610 ch0/3 (YM-2610B)118** change ADPCM_SHIFT (10->8) missing bank change 0x4000-0xffff.119** add ADPCM_SHIFT_MASK120** change ADPCMA_DECODE_MIN/MAX.121*/122123/************************************************************************/124/* comment of hiro-shi(Hiromitsu Shioya) */125/* YM2610(B) = OPN-B */126/* YM2610 : PSG:3ch FM:4ch ADPCM(18.5KHz):6ch DeltaT ADPCM:1ch */127/* YM2610B : PSG:3ch FM:6ch ADPCM(18.5KHz):6ch DeltaT ADPCM:1ch */128/************************************************************************/129130#include "shared.h"131132/* envelope generator */133#define ENV_BITS 10134#define ENV_LEN (1<<ENV_BITS)135#define ENV_STEP (128.0/ENV_LEN)136137#define MAX_ATT_INDEX (ENV_LEN-1) /* 1023 */138#define MIN_ATT_INDEX (0) /* 0 */139140#define EG_ATT 4141#define EG_DEC 3142#define EG_SUS 2143#define EG_REL 1144#define EG_OFF 0145146/* phase generator (detune mask) */147#define DT_BITS 17148#define DT_LEN (1 << DT_BITS)149#define DT_MASK (DT_LEN - 1)150151/* operator unit */152#define SIN_BITS 10153#define SIN_LEN (1<<SIN_BITS)154#define SIN_MASK (SIN_LEN-1)155156#define TL_RES_LEN (256) /* 8 bits addressing (real chip) */157158#define TL_BITS 14 /* channel output */159160/* TL_TAB_LEN is calculated as:161* 13 - sinus amplitude bits (Y axis)162* 2 - sinus sign bit (Y axis)163* TL_RES_LEN - sinus resolution (X axis)164*/165#define TL_TAB_LEN (13*2*TL_RES_LEN)166static signed int tl_tab[TL_TAB_LEN];167168#define ENV_QUIET (TL_TAB_LEN>>3)169170/* sin waveform table in 'decibel' scale */171static unsigned int sin_tab[SIN_LEN];172173/* sustain level table (3dB per step) */174/* bit0, bit1, bit2, bit3, bit4, bit5, bit6 */175/* 1, 2, 4, 8, 16, 32, 64 (value)*/176/* 0.75, 1.5, 3, 6, 12, 24, 48 (dB)*/177178/* 0 - 15: 0, 3, 6, 9,12,15,18,21,24,27,30,33,36,39,42,93 (dB)*/179/* attenuation value (10 bits) = (SL << 2) << 3 */180#define SC(db) (UINT32) ( db * (4.0/ENV_STEP) )181static const UINT32 sl_table[16]={182SC( 0),SC( 1),SC( 2),SC(3 ),SC(4 ),SC(5 ),SC(6 ),SC( 7),183SC( 8),SC( 9),SC(10),SC(11),SC(12),SC(13),SC(14),SC(31)184};185#undef SC186187188#define RATE_STEPS (8)189static const UINT8 eg_inc[19*RATE_STEPS]={190191/*cycle:0 1 2 3 4 5 6 7*/192193/* 0 */ 0,1, 0,1, 0,1, 0,1, /* rates 00..11 0 (increment by 0 or 1) */194/* 1 */ 0,1, 0,1, 1,1, 0,1, /* rates 00..11 1 */195/* 2 */ 0,1, 1,1, 0,1, 1,1, /* rates 00..11 2 */196/* 3 */ 0,1, 1,1, 1,1, 1,1, /* rates 00..11 3 */197198/* 4 */ 1,1, 1,1, 1,1, 1,1, /* rate 12 0 (increment by 1) */199/* 5 */ 1,1, 1,2, 1,1, 1,2, /* rate 12 1 */200/* 6 */ 1,2, 1,2, 1,2, 1,2, /* rate 12 2 */201/* 7 */ 1,2, 2,2, 1,2, 2,2, /* rate 12 3 */202203/* 8 */ 2,2, 2,2, 2,2, 2,2, /* rate 13 0 (increment by 2) */204/* 9 */ 2,2, 2,4, 2,2, 2,4, /* rate 13 1 */205/*10 */ 2,4, 2,4, 2,4, 2,4, /* rate 13 2 */206/*11 */ 2,4, 4,4, 2,4, 4,4, /* rate 13 3 */207208/*12 */ 4,4, 4,4, 4,4, 4,4, /* rate 14 0 (increment by 4) */209/*13 */ 4,4, 4,8, 4,4, 4,8, /* rate 14 1 */210/*14 */ 4,8, 4,8, 4,8, 4,8, /* rate 14 2 */211/*15 */ 4,8, 8,8, 4,8, 8,8, /* rate 14 3 */212213/*16 */ 8,8, 8,8, 8,8, 8,8, /* rates 15 0, 15 1, 15 2, 15 3 (increment by 8) */214/*17 */ 16,16,16,16,16,16,16,16, /* rates 15 2, 15 3 for attack */215/*18 */ 0,0, 0,0, 0,0, 0,0, /* infinity rates for attack and decay(s) */216};217218219#define O(a) (a*RATE_STEPS)220221/*note that there is no O(17) in this table - it's directly in the code */222static const UINT8 eg_rate_select[32+64+32]={ /* Envelope Generator rates (32 + 64 rates + 32 RKS) */223/* 32 infinite time rates (same as Rate 0) */224O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),225O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),226O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),227O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),228229/* rates 00-11 */230/*231O( 0),O( 1),O( 2),O( 3),232O( 0),O( 1),O( 2),O( 3),233*/234O(18),O(18),O( 0),O( 0),235O( 0),O( 0),O( 2),O( 2), /* Nemesis's tests */236237O( 0),O( 1),O( 2),O( 3),238O( 0),O( 1),O( 2),O( 3),239O( 0),O( 1),O( 2),O( 3),240O( 0),O( 1),O( 2),O( 3),241O( 0),O( 1),O( 2),O( 3),242O( 0),O( 1),O( 2),O( 3),243O( 0),O( 1),O( 2),O( 3),244O( 0),O( 1),O( 2),O( 3),245O( 0),O( 1),O( 2),O( 3),246O( 0),O( 1),O( 2),O( 3),247248/* rate 12 */249O( 4),O( 5),O( 6),O( 7),250251/* rate 13 */252O( 8),O( 9),O(10),O(11),253254/* rate 14 */255O(12),O(13),O(14),O(15),256257/* rate 15 */258O(16),O(16),O(16),O(16),259260/* 32 dummy rates (same as 15 3) */261O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16),262O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16),263O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16),264O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16)265266};267#undef O268269/*rate 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15*/270/*shift 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0 */271/*mask 2047, 1023, 511, 255, 127, 63, 31, 15, 7, 3, 1, 0, 0, 0, 0, 0 */272273#define O(a) (a*1)274static const UINT8 eg_rate_shift[32+64+32]={ /* Envelope Generator counter shifts (32 + 64 rates + 32 RKS) */275/* 32 infinite time rates */276/* O(0),O(0),O(0),O(0),O(0),O(0),O(0),O(0),277O(0),O(0),O(0),O(0),O(0),O(0),O(0),O(0),278O(0),O(0),O(0),O(0),O(0),O(0),O(0),O(0),279O(0),O(0),O(0),O(0),O(0),O(0),O(0),O(0), */280281/* fixed (should be the same as rate 0, even if it makes no difference since increment value is 0 for these rates) */282O(11),O(11),O(11),O(11),O(11),O(11),O(11),O(11),283O(11),O(11),O(11),O(11),O(11),O(11),O(11),O(11),284O(11),O(11),O(11),O(11),O(11),O(11),O(11),O(11),285O(11),O(11),O(11),O(11),O(11),O(11),O(11),O(11),286287/* rates 00-11 */288O(11),O(11),O(11),O(11),289O(10),O(10),O(10),O(10),290O( 9),O( 9),O( 9),O( 9),291O( 8),O( 8),O( 8),O( 8),292O( 7),O( 7),O( 7),O( 7),293O( 6),O( 6),O( 6),O( 6),294O( 5),O( 5),O( 5),O( 5),295O( 4),O( 4),O( 4),O( 4),296O( 3),O( 3),O( 3),O( 3),297O( 2),O( 2),O( 2),O( 2),298O( 1),O( 1),O( 1),O( 1),299O( 0),O( 0),O( 0),O( 0),300301/* rate 12 */302O( 0),O( 0),O( 0),O( 0),303304/* rate 13 */305O( 0),O( 0),O( 0),O( 0),306307/* rate 14 */308O( 0),O( 0),O( 0),O( 0),309310/* rate 15 */311O( 0),O( 0),O( 0),O( 0),312313/* 32 dummy rates (same as 15 3) */314O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),315O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),316O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),317O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0)318319};320#undef O321322static const UINT8 dt_tab[4 * 32]={323/* this is YM2151 and YM2612 phase increment data (in 10.10 fixed point format)*/324/* FD=0 */3250, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,3260, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,327/* FD=1 */3280, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2,3292, 3, 3, 3, 4, 4, 4, 5, 5, 6, 6, 7, 8, 8, 8, 8,330/* FD=2 */3311, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5,3325, 6, 6, 7, 8, 8, 9,10,11,12,13,14,16,16,16,16,333/* FD=3 */3342, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 6, 6, 7,3358 , 8, 9,10,11,12,13,14,16,17,19,20,22,22,22,22336};337338339/* OPN key frequency number -> key code follow table */340/* fnum higher 4bit -> keycode lower 2bit */341static const UINT8 opn_fktable[16] = {0,0,0,0,0,0,0,1,2,3,3,3,3,3,3,3};342343344/* 8 LFO speed parameters */345/* each value represents number of samples that one LFO level will last for */346static const UINT32 lfo_samples_per_step[8] = {108, 77, 71, 67, 62, 44, 8, 5};347348349/*There are 4 different LFO AM depths available, they are:3500 dB, 1.4 dB, 5.9 dB, 11.8 dB351Here is how it is generated (in EG steps):35235311.8 dB = 0, 2, 4, 6, 8, 10,12,14,16...126,126,124,122,120,118,....4,2,03545.9 dB = 0, 1, 2, 3, 4, 5, 6, 7, 8....63, 63, 62, 61, 60, 59,.....2,1,03551.4 dB = 0, 0, 0, 0, 1, 1, 1, 1, 2,...15, 15, 15, 15, 14, 14,.....0,0,0356357(1.4 dB is loosing precision as you can see)358359It's implemented as generator from 0..126 with step 2 then a shift360right N times, where N is:3618 for 0 dB3623 for 1.4 dB3631 for 5.9 dB3640 for 11.8 dB365*/366static const UINT8 lfo_ams_depth_shift[4] = {8, 3, 1, 0};367368369370/*There are 8 different LFO PM depths available, they are:3710, 3.4, 6.7, 10, 14, 20, 40, 80 (cents)372373Modulation level at each depth depends on F-NUMBER bits: 4,5,6,7,8,9,10374(bits 8,9,10 = FNUM MSB from OCT/FNUM register)375376Here we store only first quarter (positive one) of full waveform.377Full table (lfo_pm_table) containing all 128 waveforms is build378at run (init) time.379380One value in table below represents 4 (four) basic LFO steps381(1 PM step = 4 AM steps).382383For example:384at LFO SPEED=0 (which is 108 samples per basic LFO step)385one value from "lfo_pm_output" table lasts for 432 consecutive386samples (4*108=432) and one full LFO waveform cycle lasts for 13824387samples (32*432=13824; 32 because we store only a quarter of whole388waveform in the table below)389*/390static const UINT8 lfo_pm_output[7*8][8]={391/* 7 bits meaningful (of F-NUMBER), 8 LFO output levels per one depth (out of 32), 8 LFO depths */392/* FNUM BIT 4: 000 0001xxxx */393/* DEPTH 0 */ {0, 0, 0, 0, 0, 0, 0, 0},394/* DEPTH 1 */ {0, 0, 0, 0, 0, 0, 0, 0},395/* DEPTH 2 */ {0, 0, 0, 0, 0, 0, 0, 0},396/* DEPTH 3 */ {0, 0, 0, 0, 0, 0, 0, 0},397/* DEPTH 4 */ {0, 0, 0, 0, 0, 0, 0, 0},398/* DEPTH 5 */ {0, 0, 0, 0, 0, 0, 0, 0},399/* DEPTH 6 */ {0, 0, 0, 0, 0, 0, 0, 0},400/* DEPTH 7 */ {0, 0, 0, 0, 1, 1, 1, 1},401402/* FNUM BIT 5: 000 0010xxxx */403/* DEPTH 0 */ {0, 0, 0, 0, 0, 0, 0, 0},404/* DEPTH 1 */ {0, 0, 0, 0, 0, 0, 0, 0},405/* DEPTH 2 */ {0, 0, 0, 0, 0, 0, 0, 0},406/* DEPTH 3 */ {0, 0, 0, 0, 0, 0, 0, 0},407/* DEPTH 4 */ {0, 0, 0, 0, 0, 0, 0, 0},408/* DEPTH 5 */ {0, 0, 0, 0, 0, 0, 0, 0},409/* DEPTH 6 */ {0, 0, 0, 0, 1, 1, 1, 1},410/* DEPTH 7 */ {0, 0, 1, 1, 2, 2, 2, 3},411412/* FNUM BIT 6: 000 0100xxxx */413/* DEPTH 0 */ {0, 0, 0, 0, 0, 0, 0, 0},414/* DEPTH 1 */ {0, 0, 0, 0, 0, 0, 0, 0},415/* DEPTH 2 */ {0, 0, 0, 0, 0, 0, 0, 0},416/* DEPTH 3 */ {0, 0, 0, 0, 0, 0, 0, 0},417/* DEPTH 4 */ {0, 0, 0, 0, 0, 0, 0, 1},418/* DEPTH 5 */ {0, 0, 0, 0, 1, 1, 1, 1},419/* DEPTH 6 */ {0, 0, 1, 1, 2, 2, 2, 3},420/* DEPTH 7 */ {0, 0, 2, 3, 4, 4, 5, 6},421422/* FNUM BIT 7: 000 1000xxxx */423/* DEPTH 0 */ {0, 0, 0, 0, 0, 0, 0, 0},424/* DEPTH 1 */ {0, 0, 0, 0, 0, 0, 0, 0},425/* DEPTH 2 */ {0, 0, 0, 0, 0, 0, 1, 1},426/* DEPTH 3 */ {0, 0, 0, 0, 1, 1, 1, 1},427/* DEPTH 4 */ {0, 0, 0, 1, 1, 1, 1, 2},428/* DEPTH 5 */ {0, 0, 1, 1, 2, 2, 2, 3},429/* DEPTH 6 */ {0, 0, 2, 3, 4, 4, 5, 6},430/* DEPTH 7 */ {0, 0, 4, 6, 8, 8, 0xa, 0xc},431432/* FNUM BIT 8: 001 0000xxxx */433/* DEPTH 0 */ {0, 0, 0, 0, 0, 0, 0, 0},434/* DEPTH 1 */ {0, 0, 0, 0, 1, 1, 1, 1},435/* DEPTH 2 */ {0, 0, 0, 1, 1, 1, 2, 2},436/* DEPTH 3 */ {0, 0, 1, 1, 2, 2, 3, 3},437/* DEPTH 4 */ {0, 0, 1, 2, 2, 2, 3, 4},438/* DEPTH 5 */ {0, 0, 2, 3, 4, 4, 5, 6},439/* DEPTH 6 */ {0, 0, 4, 6, 8, 8, 0xa, 0xc},440/* DEPTH 7 */ {0, 0, 8, 0xc,0x10,0x10,0x14,0x18},441442/* FNUM BIT 9: 010 0000xxxx */443/* DEPTH 0 */ {0, 0, 0, 0, 0, 0, 0, 0},444/* DEPTH 1 */ {0, 0, 0, 0, 2, 2, 2, 2},445/* DEPTH 2 */ {0, 0, 0, 2, 2, 2, 4, 4},446/* DEPTH 3 */ {0, 0, 2, 2, 4, 4, 6, 6},447/* DEPTH 4 */ {0, 0, 2, 4, 4, 4, 6, 8},448/* DEPTH 5 */ {0, 0, 4, 6, 8, 8, 0xa, 0xc},449/* DEPTH 6 */ {0, 0, 8, 0xc,0x10,0x10,0x14,0x18},450/* DEPTH 7 */ {0, 0,0x10,0x18,0x20,0x20,0x28,0x30},451452/* FNUM BIT10: 100 0000xxxx */453/* DEPTH 0 */ {0, 0, 0, 0, 0, 0, 0, 0},454/* DEPTH 1 */ {0, 0, 0, 0, 4, 4, 4, 4},455/* DEPTH 2 */ {0, 0, 0, 4, 4, 4, 8, 8},456/* DEPTH 3 */ {0, 0, 4, 4, 8, 8, 0xc, 0xc},457/* DEPTH 4 */ {0, 0, 4, 8, 8, 8, 0xc,0x10},458/* DEPTH 5 */ {0, 0, 8, 0xc,0x10,0x10,0x14,0x18},459/* DEPTH 6 */ {0, 0,0x10,0x18,0x20,0x20,0x28,0x30},460/* DEPTH 7 */ {0, 0,0x20,0x30,0x40,0x40,0x50,0x60},461462};463464/* all 128 LFO PM waveforms */465INT32 *ym2612_lfo_pm_table; /* 128 combinations of 7 bits meaningful (of F-NUMBER), 8 LFO depths, 32 LFO output levels per one depth */466#define lfo_pm_table ym2612_lfo_pm_table467468/* register number to channel number , slot offset */469#define OPN_CHAN(N) (N&3)470#define OPN_SLOT(N) ((N>>2)&3)471472/* slot number */473#define SLOT1 0474#define SLOT2 2475#define SLOT3 1476#define SLOT4 3477478/* struct describing a single operator (SLOT) */479typedef struct480{481INT32 *DT; /* detune :dt_tab[DT] */482UINT8 KSR; /* key scale rate :3-KSR */483UINT32 ar; /* attack rate */484UINT32 d1r; /* decay rate */485UINT32 d2r; /* sustain rate */486UINT32 rr; /* release rate */487UINT8 ksr; /* key scale rate :kcode>>(3-KSR) */488UINT32 mul; /* multiple :ML_TABLE[ML] */489490/* Phase Generator */491UINT32 phase; /* phase counter */492INT32 Incr; /* phase step */493494/* Envelope Generator */495UINT8 state; /* phase type */496UINT32 tl; /* total level: TL << 3 */497INT32 volume; /* envelope counter */498UINT32 sl; /* sustain level:sl_table[SL] */499UINT32 vol_out; /* current output from EG circuit (without AM from LFO) */500501UINT8 eg_sh_ar; /* (attack state) */502UINT8 eg_sel_ar; /* (attack state) */503UINT8 eg_sh_d1r; /* (decay state) */504UINT8 eg_sel_d1r; /* (decay state) */505UINT8 eg_sh_d2r; /* (sustain state) */506UINT8 eg_sel_d2r; /* (sustain state) */507UINT8 eg_sh_rr; /* (release state) */508UINT8 eg_sel_rr; /* (release state) */509510UINT8 ssg; /* SSG-EG waveform */511UINT8 ssgn; /* SSG-EG negated output */512513UINT8 key; /* 0=last key was KEY OFF, 1=KEY ON */514515/* LFO */516UINT32 AMmask; /* AM enable flag */517518} FM_SLOT;519520typedef struct521{522FM_SLOT SLOT[4]; /* four SLOTs (operators) */523524UINT8 ALGO; /* algorithm */525UINT8 FB; /* feedback shift */526INT32 op1_out[2]; /* op1 output for feedback */527528INT32 *connect1; /* SLOT1 output pointer */529INT32 *connect3; /* SLOT3 output pointer */530INT32 *connect2; /* SLOT2 output pointer */531INT32 *connect4; /* SLOT4 output pointer */532533INT32 *mem_connect; /* where to put the delayed sample (MEM) */534INT32 mem_value; /* delayed sample (MEM) value */535536INT32 pms; /* channel PMS */537UINT8 ams; /* channel AMS */538539UINT32 fc; /* fnum,blk */540UINT8 kcode; /* key code */541UINT32 block_fnum; /* blk/fnum value (for LFO PM calculations) */542} FM_CH;543544545typedef struct546{547UINT16 address; /* address register */548UINT8 status; /* status flag */549UINT32 mode; /* mode CSM / 3SLOT */550UINT8 fn_h; /* freq latch */551INT32 TA; /* timer a value */552INT32 TAL; /* timer a base */553INT32 TAC; /* timer a counter */554INT32 TB; /* timer b value */555INT32 TBL; /* timer b base */556INT32 TBC; /* timer b counter */557INT32 dt_tab[8][32]; /* DeTune table */558559} FM_ST;560561562/***********************************************************/563/* OPN unit */564/***********************************************************/565566/* OPN 3slot struct */567typedef struct568{569UINT32 fc[3]; /* fnum3,blk3: calculated */570UINT8 fn_h; /* freq3 latch */571UINT8 kcode[3]; /* key code */572UINT32 block_fnum[3]; /* current fnum value for this slot (can be different betweeen slots of one channel in 3slot mode) */573UINT8 key_csm; /* CSM mode Key-ON flag */574575} FM_3SLOT;576577/* OPN/A/B common state */578typedef struct579{580FM_ST ST; /* general state */581FM_3SLOT SL3; /* 3 slot mode state */582unsigned int pan[6*2]; /* fm channels output masks (0xffffffff = enable) */583584/* EG */585UINT32 eg_cnt; /* global envelope generator counter */586UINT32 eg_timer; /* global envelope generator counter works at frequency = chipclock/144/3 */587588/* LFO */589UINT8 lfo_cnt; /* current LFO phase (out of 128) */590UINT32 lfo_timer; /* current LFO phase runs at LFO frequency */591UINT32 lfo_timer_overflow; /* LFO timer overflows every N samples (depends on LFO frequency) */592UINT32 LFO_AM; /* current LFO AM step */593UINT32 LFO_PM; /* current LFO PM step */594595} FM_OPN;596597/***********************************************************/598/* YM2612 chip */599/***********************************************************/600typedef struct601{602FM_CH CH[6]; /* channel state */603UINT8 dacen; /* DAC mode */604INT32 dacout; /* DAC output */605FM_OPN OPN; /* OPN state */606607} YM2612;608609/* emulated chip */610YM2612 ym2612;611612/* current chip state */613INT32 m2,c1,c2; /* Phase Modulation input for operators 2,3,4 */614INT32 mem; /* one sample delay memory */615INT32 out_fm[8]; /* outputs of working channels */616UINT32 bitmask; /* working channels output bitmasking (DAC quantization) */617618619INLINE void FM_KEYON(FM_CH *CH , int s )620{621FM_SLOT *SLOT = &CH->SLOT[s];622623if (!SLOT->key && !ym2612.OPN.SL3.key_csm)624{625/* restart Phase Generator */626SLOT->phase = 0;627628/* reset SSG-EG inversion flag */629SLOT->ssgn = 0;630631if ((SLOT->ar + SLOT->ksr) < 94 /*32+62*/)632{633SLOT->state = (SLOT->volume <= MIN_ATT_INDEX) ? ((SLOT->sl == MIN_ATT_INDEX) ? EG_SUS : EG_DEC) : EG_ATT;634}635else636{637/* force attenuation level to 0 */638SLOT->volume = MIN_ATT_INDEX;639640/* directly switch to Decay (or Sustain) */641SLOT->state = (SLOT->sl == MIN_ATT_INDEX) ? EG_SUS : EG_DEC;642}643644/* recalculate EG output */645if ((SLOT->ssg&0x08) && (SLOT->ssgn ^ (SLOT->ssg&0x04)))646SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;647else648SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;649}650651SLOT->key = 1;652}653654INLINE void FM_KEYOFF(FM_CH *CH , int s )655{656FM_SLOT *SLOT = &CH->SLOT[s];657658if (SLOT->key && !ym2612.OPN.SL3.key_csm)659{660if (SLOT->state>EG_REL)661{662SLOT->state = EG_REL; /* phase -> Release */663664/* SSG-EG specific update */665if (SLOT->ssg&0x08)666{667/* convert EG attenuation level */668if (SLOT->ssgn ^ (SLOT->ssg&0x04))669SLOT->volume = (0x200 - SLOT->volume);670671/* force EG attenuation level */672if (SLOT->volume >= 0x200)673{674SLOT->volume = MAX_ATT_INDEX;675SLOT->state = EG_OFF;676}677678/* recalculate EG output */679SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;680}681}682}683684SLOT->key = 0;685}686687INLINE void FM_KEYON_CSM(FM_CH *CH , int s )688{689FM_SLOT *SLOT = &CH->SLOT[s];690691if (!SLOT->key && !ym2612.OPN.SL3.key_csm)692{693/* restart Phase Generator */694SLOT->phase = 0;695696/* reset SSG-EG inversion flag */697SLOT->ssgn = 0;698699if ((SLOT->ar + SLOT->ksr) < 94 /*32+62*/)700{701SLOT->state = (SLOT->volume <= MIN_ATT_INDEX) ? ((SLOT->sl == MIN_ATT_INDEX) ? EG_SUS : EG_DEC) : EG_ATT;702}703else704{705/* force attenuation level to 0 */706SLOT->volume = MIN_ATT_INDEX;707708/* directly switch to Decay (or Sustain) */709SLOT->state = (SLOT->sl == MIN_ATT_INDEX) ? EG_SUS : EG_DEC;710}711712/* recalculate EG output */713if ((SLOT->ssg&0x08) && (SLOT->ssgn ^ (SLOT->ssg&0x04)))714SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;715else716SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;717}718}719720INLINE void FM_KEYOFF_CSM(FM_CH *CH , int s )721{722FM_SLOT *SLOT = &CH->SLOT[s];723if (!SLOT->key)724{725if (SLOT->state>EG_REL)726{727SLOT->state = EG_REL; /* phase -> Release */728729/* SSG-EG specific update */730if (SLOT->ssg&0x08)731{732/* convert EG attenuation level */733if (SLOT->ssgn ^ (SLOT->ssg&0x04))734SLOT->volume = (0x200 - SLOT->volume);735736/* force EG attenuation level */737if (SLOT->volume >= 0x200)738{739SLOT->volume = MAX_ATT_INDEX;740SLOT->state = EG_OFF;741}742743/* recalculate EG output */744SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;745}746}747}748}749750/* CSM Key Controll */751INLINE void CSMKeyControll(FM_CH *CH)752{753/* all key ON (verified by Nemesis on real hardware) */754FM_KEYON_CSM(CH,SLOT1);755FM_KEYON_CSM(CH,SLOT2);756FM_KEYON_CSM(CH,SLOT3);757FM_KEYON_CSM(CH,SLOT4);758ym2612.OPN.SL3.key_csm = 1;759}760761INLINE void INTERNAL_TIMER_A()762{763if (ym2612.OPN.ST.mode & 0x01)764{765ym2612.OPN.ST.TAC--;766if (ym2612.OPN.ST.TAC <= 0)767{768/* set status (if enabled) */769if (ym2612.OPN.ST.mode & 0x04)770ym2612.OPN.ST.status |= 0x01;771772/* reload the counter */773ym2612.OPN.ST.TAC = ym2612.OPN.ST.TAL;774775/* CSM mode auto key on */776if ((ym2612.OPN.ST.mode & 0xC0) == 0x80)777CSMKeyControll(&ym2612.CH[2]);778}779}780}781782INLINE void INTERNAL_TIMER_B(int step)783{784if (ym2612.OPN.ST.mode & 0x02)785{786ym2612.OPN.ST.TBC-=step;787if (ym2612.OPN.ST.TBC <= 0)788{789/* set status (if enabled) */790if (ym2612.OPN.ST.mode & 0x08)791ym2612.OPN.ST.status |= 0x02;792793/* reload the counter */794if (ym2612.OPN.ST.TBL)795ym2612.OPN.ST.TBC += ym2612.OPN.ST.TBL;796else797ym2612.OPN.ST.TBC = ym2612.OPN.ST.TBL;798}799}800}801802/* OPN Mode Register Write */803INLINE void set_timers(int v )804{805/* b7 = CSM MODE */806/* b6 = 3 slot mode */807/* b5 = reset b */808/* b4 = reset a */809/* b3 = timer enable b */810/* b2 = timer enable a */811/* b1 = load b */812/* b0 = load a */813814if ((ym2612.OPN.ST.mode ^ v) & 0xC0)815{816/* phase increment need to be recalculated */817ym2612.CH[2].SLOT[SLOT1].Incr=-1;818819/* CSM mode disabled and CSM key ON active*/820if (((v & 0xC0) != 0x80) && ym2612.OPN.SL3.key_csm)821{822/* CSM Mode Key OFF (verified by Nemesis on real hardware) */823FM_KEYOFF_CSM(&ym2612.CH[2],SLOT1);824FM_KEYOFF_CSM(&ym2612.CH[2],SLOT2);825FM_KEYOFF_CSM(&ym2612.CH[2],SLOT3);826FM_KEYOFF_CSM(&ym2612.CH[2],SLOT4);827ym2612.OPN.SL3.key_csm = 0;828}829}830831/* reload Timers */832if ((v&1) && !(ym2612.OPN.ST.mode&1))833ym2612.OPN.ST.TAC = ym2612.OPN.ST.TAL;834if ((v&2) && !(ym2612.OPN.ST.mode&2))835ym2612.OPN.ST.TBC = ym2612.OPN.ST.TBL;836837/* reset Timers flags */838ym2612.OPN.ST.status &= (~v >> 4);839840ym2612.OPN.ST.mode = v;841}842843/* set algorithm connection */844INLINE void setup_connection( FM_CH *CH, int ch )845{846INT32 *carrier = &out_fm[ch];847848INT32 **om1 = &CH->connect1;849INT32 **om2 = &CH->connect3;850INT32 **oc1 = &CH->connect2;851852INT32 **memc = &CH->mem_connect;853854switch( CH->ALGO ){855case 0:856/* M1---C1---MEM---M2---C2---OUT */857*om1 = &c1;858*oc1 = &mem;859*om2 = &c2;860*memc= &m2;861break;862case 1:863/* M1------+-MEM---M2---C2---OUT */864/* C1-+ */865*om1 = &mem;866*oc1 = &mem;867*om2 = &c2;868*memc= &m2;869break;870case 2:871/* M1-----------------+-C2---OUT */872/* C1---MEM---M2-+ */873*om1 = &c2;874*oc1 = &mem;875*om2 = &c2;876*memc= &m2;877break;878case 3:879/* M1---C1---MEM------+-C2---OUT */880/* M2-+ */881*om1 = &c1;882*oc1 = &mem;883*om2 = &c2;884*memc= &c2;885break;886case 4:887/* M1---C1-+-OUT */888/* M2---C2-+ */889/* MEM: not used */890*om1 = &c1;891*oc1 = carrier;892*om2 = &c2;893*memc= &mem; /* store it anywhere where it will not be used */894break;895case 5:896/* +----C1----+ */897/* M1-+-MEM---M2-+-OUT */898/* +----C2----+ */899*om1 = 0; /* special mark */900*oc1 = carrier;901*om2 = carrier;902*memc= &m2;903break;904case 6:905/* M1---C1-+ */906/* M2-+-OUT */907/* C2-+ */908/* MEM: not used */909*om1 = &c1;910*oc1 = carrier;911*om2 = carrier;912*memc= &mem; /* store it anywhere where it will not be used */913break;914case 7:915/* M1-+ */916/* C1-+-OUT */917/* M2-+ */918/* C2-+ */919/* MEM: not used*/920*om1 = carrier;921*oc1 = carrier;922*om2 = carrier;923*memc= &mem; /* store it anywhere where it will not be used */924break;925}926927CH->connect4 = carrier;928}929930/* set detune & multiple */931INLINE void set_det_mul(FM_CH *CH,FM_SLOT *SLOT,int v)932{933SLOT->mul = (v&0x0f)? (v&0x0f)*2 : 1;934SLOT->DT = ym2612.OPN.ST.dt_tab[(v>>4)&7];935CH->SLOT[SLOT1].Incr=-1;936}937938/* set total level */939INLINE void set_tl(FM_SLOT *SLOT , int v)940{941SLOT->tl = (v&0x7f)<<(ENV_BITS-7); /* 7bit TL */942943/* recalculate EG output */944if ((SLOT->ssg&0x08) && (SLOT->ssgn ^ (SLOT->ssg&0x04)) && (SLOT->state > EG_REL))945SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;946else947SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;948}949950/* set attack rate & key scale */951INLINE void set_ar_ksr(FM_CH *CH,FM_SLOT *SLOT,int v)952{953UINT8 old_KSR = SLOT->KSR;954955SLOT->ar = (v&0x1f) ? 32 + ((v&0x1f)<<1) : 0;956957SLOT->KSR = 3-(v>>6);958if (SLOT->KSR != old_KSR)959{960CH->SLOT[SLOT1].Incr=-1;961}962963/* Even if it seems unnecessary to do it here, it could happen that KSR and KC */964/* are modified but the resulted SLOT->ksr value (kc >> SLOT->KSR) remains unchanged. */965/* In such case, Attack Rate would not be recalculated by "refresh_fc_eg_slot". */966/* This actually fixes the intro of "The Adventures of Batman & Robin" (Eke-Eke) */967if ((SLOT->ar + SLOT->ksr) < (32+62))968{969SLOT->eg_sh_ar = eg_rate_shift [SLOT->ar + SLOT->ksr ];970SLOT->eg_sel_ar = eg_rate_select[SLOT->ar + SLOT->ksr ];971}972else973{974/* verified by Nemesis on real hardware (Attack phase is blocked) */975SLOT->eg_sh_ar = 0;976SLOT->eg_sel_ar = 18*RATE_STEPS;977}978}979980/* set decay rate */981INLINE void set_dr(FM_SLOT *SLOT,int v)982{983SLOT->d1r = (v&0x1f) ? 32 + ((v&0x1f)<<1) : 0;984985SLOT->eg_sh_d1r = eg_rate_shift [SLOT->d1r + SLOT->ksr];986SLOT->eg_sel_d1r= eg_rate_select[SLOT->d1r + SLOT->ksr];987988}989990/* set sustain rate */991INLINE void set_sr(FM_SLOT *SLOT,int v)992{993SLOT->d2r = (v&0x1f) ? 32 + ((v&0x1f)<<1) : 0;994995SLOT->eg_sh_d2r = eg_rate_shift [SLOT->d2r + SLOT->ksr];996SLOT->eg_sel_d2r= eg_rate_select[SLOT->d2r + SLOT->ksr];997}998999/* set release rate */1000INLINE void set_sl_rr(FM_SLOT *SLOT,int v)1001{1002SLOT->sl = sl_table[ v>>4 ];10031004/* check EG state changes */1005if ((SLOT->state == EG_DEC) && (SLOT->volume >= (INT32)(SLOT->sl)))1006SLOT->state = EG_SUS;10071008SLOT->rr = 34 + ((v&0x0f)<<2);10091010SLOT->eg_sh_rr = eg_rate_shift [SLOT->rr + SLOT->ksr];1011SLOT->eg_sel_rr = eg_rate_select[SLOT->rr + SLOT->ksr];1012}10131014/* advance LFO to next sample */1015INLINE void advance_lfo()1016{1017if (ym2612.OPN.lfo_timer_overflow) /* LFO enabled ? */1018{1019/* increment LFO timer (every samples) */1020ym2612.OPN.lfo_timer ++;10211022/* when LFO is enabled, one level will last for 108, 77, 71, 67, 62, 44, 8 or 5 samples */1023if (ym2612.OPN.lfo_timer >= ym2612.OPN.lfo_timer_overflow)1024{1025ym2612.OPN.lfo_timer = 0;10261027/* There are 128 LFO steps */1028ym2612.OPN.lfo_cnt = ( ym2612.OPN.lfo_cnt + 1 ) & 127;10291030/* triangle (inverted) */1031/* AM: from 126 to 0 step -2, 0 to 126 step +2 */1032if (ym2612.OPN.lfo_cnt<64)1033ym2612.OPN.LFO_AM = (ym2612.OPN.lfo_cnt ^ 63) << 1;1034else1035ym2612.OPN.LFO_AM = (ym2612.OPN.lfo_cnt & 63) << 1;10361037/* PM works with 4 times slower clock */1038ym2612.OPN.LFO_PM = ym2612.OPN.lfo_cnt >> 2;1039}1040}1041}104210431044INLINE void advance_eg_channels(FM_CH *CH, unsigned int eg_cnt)1045{1046unsigned int i = 6; /* six channels */1047unsigned int j;1048FM_SLOT *SLOT;10491050do1051{1052SLOT = &CH->SLOT[SLOT1];1053j = 4; /* four operators per channel */1054do1055{1056switch(SLOT->state)1057{1058case EG_ATT: /* attack phase */1059{1060if (!(eg_cnt & ((1<<SLOT->eg_sh_ar)-1)))1061{1062/* update attenuation level */1063SLOT->volume += (~SLOT->volume * (eg_inc[SLOT->eg_sel_ar + ((eg_cnt>>SLOT->eg_sh_ar)&7)]))>>4;10641065/* check phase transition*/1066if (SLOT->volume <= MIN_ATT_INDEX)1067{1068SLOT->volume = MIN_ATT_INDEX;1069SLOT->state = (SLOT->sl == MIN_ATT_INDEX) ? EG_SUS : EG_DEC; /* special case where SL=0 */1070}10711072/* recalculate EG output */1073if ((SLOT->ssg&0x08) && (SLOT->ssgn ^ (SLOT->ssg&0x04))) /* SSG-EG Output Inversion */1074SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;1075else1076SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;1077}1078break;1079}10801081case EG_DEC: /* decay phase */1082{1083if (!(eg_cnt & ((1<<SLOT->eg_sh_d1r)-1)))1084{1085/* SSG EG type */1086if (SLOT->ssg&0x08)1087{1088/* update attenuation level */1089if (SLOT->volume < 0x200)1090{1091SLOT->volume += 4 * eg_inc[SLOT->eg_sel_d1r + ((eg_cnt>>SLOT->eg_sh_d1r)&7)];10921093/* recalculate EG output */1094if (SLOT->ssgn ^ (SLOT->ssg&0x04)) /* SSG-EG Output Inversion */1095SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;1096else1097SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;1098}1099}1100else1101{1102/* update attenuation level */1103SLOT->volume += eg_inc[SLOT->eg_sel_d1r + ((eg_cnt>>SLOT->eg_sh_d1r)&7)];11041105/* recalculate EG output */1106SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;1107}11081109/* check phase transition*/1110if (SLOT->volume >= (INT32)(SLOT->sl))1111SLOT->state = EG_SUS;1112}1113break;1114}11151116case EG_SUS: /* sustain phase */1117{1118if (!(eg_cnt & ((1<<SLOT->eg_sh_d2r)-1)))1119{1120/* SSG EG type */1121if (SLOT->ssg&0x08)1122{1123/* update attenuation level */1124if (SLOT->volume < 0x200)1125{1126SLOT->volume += 4 * eg_inc[SLOT->eg_sel_d2r + ((eg_cnt>>SLOT->eg_sh_d2r)&7)];11271128/* recalculate EG output */1129if (SLOT->ssgn ^ (SLOT->ssg&0x04)) /* SSG-EG Output Inversion */1130SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;1131else1132SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;1133}1134}1135else1136{1137/* update attenuation level */1138SLOT->volume += eg_inc[SLOT->eg_sel_d2r + ((eg_cnt>>SLOT->eg_sh_d2r)&7)];11391140/* check phase transition*/1141if ( SLOT->volume >= MAX_ATT_INDEX )1142SLOT->volume = MAX_ATT_INDEX;1143/* do not change SLOT->state (verified on real chip) */11441145/* recalculate EG output */1146SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;1147}1148}1149break;1150}11511152case EG_REL: /* release phase */1153{1154if (!(eg_cnt & ((1<<SLOT->eg_sh_rr)-1)))1155{1156/* SSG EG type */1157if (SLOT->ssg&0x08)1158{1159/* update attenuation level */1160if (SLOT->volume < 0x200)1161SLOT->volume += 4 * eg_inc[SLOT->eg_sel_rr + ((eg_cnt>>SLOT->eg_sh_rr)&7)];11621163/* check phase transition */1164if (SLOT->volume >= 0x200)1165{1166SLOT->volume = MAX_ATT_INDEX;1167SLOT->state = EG_OFF;1168}1169}1170else1171{1172/* update attenuation level */1173SLOT->volume += eg_inc[SLOT->eg_sel_rr + ((eg_cnt>>SLOT->eg_sh_rr)&7)];11741175/* check phase transition*/1176if (SLOT->volume >= MAX_ATT_INDEX)1177{1178SLOT->volume = MAX_ATT_INDEX;1179SLOT->state = EG_OFF;1180}1181}11821183/* recalculate EG output */1184SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;11851186}1187break;1188}1189}11901191/* next slot */1192SLOT++;1193} while (--j);11941195/* next channel */1196CH++;1197} while (--i);1198}11991200/* SSG-EG update process */1201/* The behavior is based upon Nemesis tests on real hardware */1202/* This is actually executed before each samples */1203INLINE void update_ssg_eg_channels(FM_CH *CH)1204{1205unsigned int i = 6; /* six channels */1206unsigned int j;1207FM_SLOT *SLOT;12081209do1210{1211j = 4; /* four operators per channel */1212SLOT = &CH->SLOT[SLOT1];12131214do1215{1216/* detect SSG-EG transition */1217/* this is not required during release phase as the attenuation has been forced to MAX and output invert flag is not used */1218/* if an Attack Phase is programmed, inversion can occur on each sample */1219if ((SLOT->ssg & 0x08) && (SLOT->volume >= 0x200) && (SLOT->state > EG_REL))1220{1221if (SLOT->ssg & 0x01) /* bit 0 = hold SSG-EG */1222{1223/* set inversion flag */1224if (SLOT->ssg & 0x02)1225SLOT->ssgn = 4;12261227/* force attenuation level during decay phases */1228if ((SLOT->state != EG_ATT) && !(SLOT->ssgn ^ (SLOT->ssg & 0x04)))1229SLOT->volume = MAX_ATT_INDEX;1230}1231else /* loop SSG-EG */1232{1233/* toggle output inversion flag or reset Phase Generator */1234if (SLOT->ssg & 0x02)1235SLOT->ssgn ^= 4;1236else1237SLOT->phase = 0;12381239/* same as Key ON */1240if (SLOT->state != EG_ATT)1241{1242if ((SLOT->ar + SLOT->ksr) < 94 /*32+62*/)1243{1244SLOT->state = (SLOT->volume <= MIN_ATT_INDEX) ? ((SLOT->sl == MIN_ATT_INDEX) ? EG_SUS : EG_DEC) : EG_ATT;1245}1246else1247{1248/* Attack Rate is maximal: directly switch to Decay or Substain */1249SLOT->volume = MIN_ATT_INDEX;1250SLOT->state = (SLOT->sl == MIN_ATT_INDEX) ? EG_SUS : EG_DEC;1251}1252}1253}12541255/* recalculate EG output */1256if (SLOT->ssgn ^ (SLOT->ssg&0x04))1257SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;1258else1259SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;1260}12611262/* next slot */1263SLOT++;1264} while (--j);12651266/* next channel */1267CH++;1268} while (--i);1269}12701271INLINE void update_phase_lfo_slot(FM_SLOT *SLOT, INT32 pms, UINT32 block_fnum)1272{1273INT32 lfo_fn_table_index_offset = lfo_pm_table[(((block_fnum & 0x7f0) >> 4) << 8) + pms + ym2612.OPN.LFO_PM];12741275if (lfo_fn_table_index_offset) /* LFO phase modulation active */1276{1277UINT8 blk;1278unsigned int kc, fc;12791280/* there are 2048 FNUMs that can be generated using FNUM/BLK registers1281but LFO works with one more bit of a precision so we really need 4096 elements */1282block_fnum = block_fnum*2 + lfo_fn_table_index_offset;1283blk = (block_fnum&0x7000) >> 12;1284block_fnum = block_fnum & 0xfff;12851286/* keyscale code */1287kc = (blk<<2) | opn_fktable[block_fnum >> 8];12881289/* (frequency) phase increment counter */1290fc = (((block_fnum << 5) >> (7 - blk)) + SLOT->DT[kc]) & DT_MASK;12911292/* update phase */1293SLOT->phase += (fc * SLOT->mul) >> 1;1294}1295else /* LFO phase modulation = zero */1296{1297SLOT->phase += SLOT->Incr;1298}1299}13001301INLINE void update_phase_lfo_channel(FM_CH *CH)1302{1303UINT32 block_fnum = CH->block_fnum;13041305INT32 lfo_fn_table_index_offset = lfo_pm_table[(((block_fnum & 0x7f0) >> 4) << 8) + CH->pms + ym2612.OPN.LFO_PM];13061307if (lfo_fn_table_index_offset) /* LFO phase modulation active */1308{1309UINT8 blk;1310unsigned int kc, fc, finc;13111312/* there are 2048 FNUMs that can be generated using FNUM/BLK registers1313but LFO works with one more bit of a precision so we really need 4096 elements */1314block_fnum = block_fnum*2 + lfo_fn_table_index_offset;1315blk = (block_fnum&0x7000) >> 12;1316block_fnum = block_fnum & 0xfff;13171318/* keyscale code */1319kc = (blk<<2) | opn_fktable[block_fnum >> 8];13201321/* (frequency) phase increment counter */1322fc = (block_fnum << 5) >> (7 - blk);13231324/* apply DETUNE & MUL operator specific values */1325finc = (fc + CH->SLOT[SLOT1].DT[kc]) & DT_MASK;1326CH->SLOT[SLOT1].phase += (finc*CH->SLOT[SLOT1].mul) >> 1;13271328finc = (fc + CH->SLOT[SLOT2].DT[kc]) & DT_MASK;1329CH->SLOT[SLOT2].phase += (finc*CH->SLOT[SLOT2].mul) >> 1;13301331finc = (fc + CH->SLOT[SLOT3].DT[kc]) & DT_MASK;1332CH->SLOT[SLOT3].phase += (finc*CH->SLOT[SLOT3].mul) >> 1;13331334finc = (fc + CH->SLOT[SLOT4].DT[kc]) & DT_MASK;1335CH->SLOT[SLOT4].phase += (finc*CH->SLOT[SLOT4].mul) >> 1;1336}1337else /* LFO phase modulation = zero */1338{1339CH->SLOT[SLOT1].phase += CH->SLOT[SLOT1].Incr;1340CH->SLOT[SLOT2].phase += CH->SLOT[SLOT2].Incr;1341CH->SLOT[SLOT3].phase += CH->SLOT[SLOT3].Incr;1342CH->SLOT[SLOT4].phase += CH->SLOT[SLOT4].Incr;1343}1344}13451346/* update phase increment and envelope generator */1347INLINE void refresh_fc_eg_slot(FM_SLOT *SLOT , unsigned int fc , unsigned int kc )1348{1349/* add detune value */1350fc += SLOT->DT[kc];13511352/* (frequency) phase overflow (credits to Nemesis) */1353fc &= DT_MASK;13541355/* (frequency) phase increment counter */1356SLOT->Incr = (fc * SLOT->mul) >> 1;13571358/* ksr */1359kc = kc >> SLOT->KSR;13601361if( SLOT->ksr != kc )1362{1363SLOT->ksr = kc;13641365/* recalculate envelope generator rates */1366if ((SLOT->ar + kc) < (32+62))1367{1368SLOT->eg_sh_ar = eg_rate_shift [SLOT->ar + kc ];1369SLOT->eg_sel_ar = eg_rate_select[SLOT->ar + kc ];1370}1371else1372{1373/* verified by Nemesis on real hardware (Attack phase is blocked) */1374SLOT->eg_sh_ar = 0;1375SLOT->eg_sel_ar = 18*RATE_STEPS;1376}13771378SLOT->eg_sh_d1r = eg_rate_shift [SLOT->d1r + kc];1379SLOT->eg_sel_d1r= eg_rate_select[SLOT->d1r + kc];13801381SLOT->eg_sh_d2r = eg_rate_shift [SLOT->d2r + kc];1382SLOT->eg_sel_d2r= eg_rate_select[SLOT->d2r + kc];13831384SLOT->eg_sh_rr = eg_rate_shift [SLOT->rr + kc];1385SLOT->eg_sel_rr = eg_rate_select[SLOT->rr + kc];1386}1387}13881389/* update phase increment counters */1390INLINE void refresh_fc_eg_chan(FM_CH *CH )1391{1392if( CH->SLOT[SLOT1].Incr==-1)1393{1394int fc = CH->fc;1395int kc = CH->kcode;1396refresh_fc_eg_slot(&CH->SLOT[SLOT1] , fc , kc );1397refresh_fc_eg_slot(&CH->SLOT[SLOT2] , fc , kc );1398refresh_fc_eg_slot(&CH->SLOT[SLOT3] , fc , kc );1399refresh_fc_eg_slot(&CH->SLOT[SLOT4] , fc , kc );1400}1401}14021403#define volume_calc(OP) ((OP)->vol_out + (AM & (OP)->AMmask))14041405INLINE signed int op_calc(UINT32 phase, unsigned int env, unsigned int pm)1406{1407UINT32 p = (env<<3) + sin_tab[ ( (phase >> SIN_BITS) + (pm >> 1) ) & SIN_MASK ];14081409if (p >= TL_TAB_LEN)1410return 0;1411return tl_tab[p];1412}14131414INLINE signed int op_calc1(UINT32 phase, unsigned int env, unsigned int pm)1415{1416UINT32 p = (env<<3) + sin_tab[ ( (phase + pm ) >> SIN_BITS ) & SIN_MASK ];14171418if (p >= TL_TAB_LEN)1419return 0;1420return tl_tab[p];1421}14221423INLINE void chan_calc(FM_CH *CH, int num)1424{1425do1426{1427UINT32 AM = ym2612.OPN.LFO_AM >> CH->ams;1428unsigned int eg_out = volume_calc(&CH->SLOT[SLOT1]);14291430m2 = c1 = c2 = mem = 0;14311432*CH->mem_connect = CH->mem_value; /* restore delayed sample (MEM) value to m2 or c2 */1433{1434INT32 out = CH->op1_out[0] + CH->op1_out[1];1435CH->op1_out[0] = CH->op1_out[1];14361437if( !CH->connect1 ){1438/* algorithm 5 */1439mem = c1 = c2 = CH->op1_out[0];1440}else{1441/* other algorithms */1442*CH->connect1 += CH->op1_out[0];1443}14441445CH->op1_out[1] = 0;1446if( eg_out < ENV_QUIET ) /* SLOT 1 */1447{1448if (!CH->FB)1449out=0;14501451CH->op1_out[1] = op_calc1(CH->SLOT[SLOT1].phase, eg_out, (out<<CH->FB) );1452}1453}14541455eg_out = volume_calc(&CH->SLOT[SLOT3]);1456if( eg_out < ENV_QUIET ) /* SLOT 3 */1457*CH->connect3 += op_calc(CH->SLOT[SLOT3].phase, eg_out, m2);14581459eg_out = volume_calc(&CH->SLOT[SLOT2]);1460if( eg_out < ENV_QUIET ) /* SLOT 2 */1461*CH->connect2 += op_calc(CH->SLOT[SLOT2].phase, eg_out, c1);14621463eg_out = volume_calc(&CH->SLOT[SLOT4]);1464if( eg_out < ENV_QUIET ) /* SLOT 4 */1465*CH->connect4 += op_calc(CH->SLOT[SLOT4].phase, eg_out, c2);146614671468/* store current MEM */1469CH->mem_value = mem;14701471/* update phase counters AFTER output calculations */1472if(CH->pms)1473{1474/* add support for 3 slot mode */1475if ((ym2612.OPN.ST.mode & 0xC0) && (CH == &ym2612.CH[2]))1476{1477update_phase_lfo_slot(&CH->SLOT[SLOT1], CH->pms, ym2612.OPN.SL3.block_fnum[1]);1478update_phase_lfo_slot(&CH->SLOT[SLOT2], CH->pms, ym2612.OPN.SL3.block_fnum[2]);1479update_phase_lfo_slot(&CH->SLOT[SLOT3], CH->pms, ym2612.OPN.SL3.block_fnum[0]);1480update_phase_lfo_slot(&CH->SLOT[SLOT4], CH->pms, CH->block_fnum);1481}1482else1483{1484update_phase_lfo_channel(CH);1485}1486}1487else /* no LFO phase modulation */1488{1489CH->SLOT[SLOT1].phase += CH->SLOT[SLOT1].Incr;1490CH->SLOT[SLOT2].phase += CH->SLOT[SLOT2].Incr;1491CH->SLOT[SLOT3].phase += CH->SLOT[SLOT3].Incr;1492CH->SLOT[SLOT4].phase += CH->SLOT[SLOT4].Incr;1493}14941495/* next channel */1496CH++;1497} while (--num);1498}14991500/* write a OPN mode register 0x20-0x2f */1501INLINE void OPNWriteMode(int r, int v)1502{1503UINT8 c;1504FM_CH *CH;15051506switch(r){1507case 0x21: /* Test */1508break;15091510case 0x22: /* LFO FREQ (YM2608/YM2610/YM2610B/ym2612) */1511if (v&8) /* LFO enabled ? */1512{1513ym2612.OPN.lfo_timer_overflow = lfo_samples_per_step[v&7];1514}1515else1516{1517/* hold LFO waveform in reset state */1518ym2612.OPN.lfo_timer_overflow = 0;1519ym2612.OPN.lfo_timer = 0;1520ym2612.OPN.lfo_cnt = 0;1521ym2612.OPN.LFO_PM = 0;1522ym2612.OPN.LFO_AM = 126;1523}1524break;1525case 0x24: /* timer A High 8*/1526ym2612.OPN.ST.TA = (ym2612.OPN.ST.TA & 0x03)|(((int)v)<<2);1527ym2612.OPN.ST.TAL = 1024 - ym2612.OPN.ST.TA;1528break;1529case 0x25: /* timer A Low 2*/1530ym2612.OPN.ST.TA = (ym2612.OPN.ST.TA & 0x3fc)|(v&3);1531ym2612.OPN.ST.TAL = 1024 - ym2612.OPN.ST.TA;1532break;1533case 0x26: /* timer B */1534ym2612.OPN.ST.TB = v;1535ym2612.OPN.ST.TBL = (256 - v) << 4;1536break;1537case 0x27: /* mode, timer control */1538set_timers(v);1539break;1540case 0x28: /* key on / off */1541c = v & 0x03;1542if( c == 3 ) break;1543if (v&0x04) c+=3; /* CH 4-6 */1544CH = &ym2612.CH[c];15451546if (v&0x10) FM_KEYON(CH,SLOT1); else FM_KEYOFF(CH,SLOT1);1547if (v&0x20) FM_KEYON(CH,SLOT2); else FM_KEYOFF(CH,SLOT2);1548if (v&0x40) FM_KEYON(CH,SLOT3); else FM_KEYOFF(CH,SLOT3);1549if (v&0x80) FM_KEYON(CH,SLOT4); else FM_KEYOFF(CH,SLOT4);1550break;1551}1552}15531554/* write a OPN register (0x30-0xff) */1555INLINE void OPNWriteReg(int r, int v)1556{1557FM_CH *CH;1558FM_SLOT *SLOT;15591560UINT8 c = OPN_CHAN(r);15611562if (c == 3) return; /* 0xX3,0xX7,0xXB,0xXF */15631564if (r >= 0x100) c+=3;15651566CH = &ym2612.CH[c];15671568SLOT = &(CH->SLOT[OPN_SLOT(r)]);15691570switch( r & 0xf0 ) {1571case 0x30: /* DET , MUL */1572set_det_mul(CH,SLOT,v);1573break;15741575case 0x40: /* TL */1576set_tl(SLOT,v);1577break;15781579case 0x50: /* KS, AR */1580set_ar_ksr(CH,SLOT,v);1581break;15821583case 0x60: /* bit7 = AM ENABLE, DR */1584set_dr(SLOT,v);1585SLOT->AMmask = (v&0x80) ? ~0 : 0;1586break;15871588case 0x70: /* SR */1589set_sr(SLOT,v);1590break;15911592case 0x80: /* SL, RR */1593set_sl_rr(SLOT,v);1594break;15951596case 0x90: /* SSG-EG */1597SLOT->ssg = v&0x0f;15981599/* recalculate EG output */1600if (SLOT->state > EG_REL)1601{1602if ((SLOT->ssg&0x08) && (SLOT->ssgn ^ (SLOT->ssg&0x04)))1603SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;1604else1605SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;1606}16071608/* SSG-EG envelope shapes :16091610E AtAlH16111 0 0 0 \\\\161216131 0 0 1 \___161416151 0 1 0 \/\/1616___16171 0 1 1 \161816191 1 0 0 ////1620___16211 1 0 1 /162216231 1 1 0 /\/\162416251 1 1 1 /___162616271628E = SSG-EG enable162916301631The shapes are generated using Attack, Decay and Sustain phases.16321633Each single character in the diagrams above represents this whole1634sequence:16351636- when KEY-ON = 1, normal Attack phase is generated (*without* any1637difference when compared to normal mode),16381639- later, when envelope level reaches minimum level (max volume),1640the EG switches to Decay phase (which works with bigger steps1641when compared to normal mode - see below),16421643- later when envelope level passes the SL level,1644the EG swithes to Sustain phase (which works with bigger steps1645when compared to normal mode - see below),16461647- finally when envelope level reaches maximum level (min volume),1648the EG switches to Attack phase again (depends on actual waveform).16491650Important is that when switch to Attack phase occurs, the phase counter1651of that operator will be zeroed-out (as in normal KEY-ON) but not always.1652(I havent found the rule for that - perhaps only when the output level is low)16531654The difference (when compared to normal Envelope Generator mode) is1655that the resolution in Decay and Sustain phases is 4 times lower;1656this results in only 256 steps instead of normal 1024.1657In other words:1658when SSG-EG is disabled, the step inside of the EG is one,1659when SSG-EG is enabled, the step is four (in Decay and Sustain phases).16601661Times between the level changes are the same in both modes.166216631664Important:1665Decay 1 Level (so called SL) is compared to actual SSG-EG output, so1666it is the same in both SSG and no-SSG modes, with this exception:16671668when the SSG-EG is enabled and is generating raising levels1669(when the EG output is inverted) the SL will be found at wrong level !!!1670For example, when SL=02:16710 -6 = -6dB in non-inverted EG output167296-6 = -90dB in inverted EG output1673Which means that EG compares its level to SL as usual, and that the1674output is simply inverted afterall.167516761677The Yamaha's manuals say that AR should be set to 0x1f (max speed).1678That is not necessary, but then EG will be generating Attack phase.16791680*/168116821683break;16841685case 0xa0:1686switch( OPN_SLOT(r) ){1687case 0: /* 0xa0-0xa2 : FNUM1 */1688{1689UINT32 fn = (((UINT32)((ym2612.OPN.ST.fn_h)&7))<<8) + v;1690UINT8 blk = ym2612.OPN.ST.fn_h>>3;1691/* keyscale code */1692CH->kcode = (blk<<2) | opn_fktable[fn >> 7];1693/* phase increment counter */1694CH->fc = (fn << 6) >> (7 - blk);16951696/* store fnum in clear form for LFO PM calculations */1697CH->block_fnum = (blk<<11) | fn;16981699CH->SLOT[SLOT1].Incr=-1;1700break;1701}1702case 1: /* 0xa4-0xa6 : FNUM2,BLK */1703ym2612.OPN.ST.fn_h = v&0x3f;1704break;1705case 2: /* 0xa8-0xaa : 3CH FNUM1 */1706if(r < 0x100)1707{1708UINT32 fn = (((UINT32)(ym2612.OPN.SL3.fn_h&7))<<8) + v;1709UINT8 blk = ym2612.OPN.SL3.fn_h>>3;1710/* keyscale code */1711ym2612.OPN.SL3.kcode[c]= (blk<<2) | opn_fktable[fn >> 7];1712/* phase increment counter */1713ym2612.OPN.SL3.fc[c] = (fn << 6) >> (7 - blk);1714ym2612.OPN.SL3.block_fnum[c] = (blk<<11) | fn;1715ym2612.CH[2].SLOT[SLOT1].Incr=-1;1716}1717break;1718case 3: /* 0xac-0xae : 3CH FNUM2,BLK */1719if(r < 0x100)1720ym2612.OPN.SL3.fn_h = v&0x3f;1721break;1722}1723break;17241725case 0xb0:1726switch( OPN_SLOT(r) ){1727case 0: /* 0xb0-0xb2 : FB,ALGO */1728{1729CH->ALGO = v&7;1730CH->FB = (v>>3)&7;1731setup_connection( CH, c );1732break;1733}1734case 1: /* 0xb4-0xb6 : L , R , AMS , PMS */1735/* b0-2 PMS */1736CH->pms = (v & 7) * 32; /* CH->pms = PM depth * 32 (index in lfo_pm_table) */17371738/* b4-5 AMS */1739CH->ams = lfo_ams_depth_shift[(v>>4) & 0x03];17401741/* PAN : b7 = L, b6 = R */1742ym2612.OPN.pan[ c*2 ] = (v & 0x80) ? bitmask : 0;1743ym2612.OPN.pan[ c*2+1 ] = (v & 0x40) ? bitmask : 0;1744break;1745}1746break;1747}1748}17491750static void reset_channels(FM_CH *CH , int num )1751{1752int c,s;17531754for( c = 0 ; c < num ; c++ )1755{1756CH[c].mem_value = 0;1757CH[c].op1_out[0] = 0;1758CH[c].op1_out[1] = 0;1759for(s = 0 ; s < 4 ; s++ )1760{1761CH[c].SLOT[s].Incr = -1;1762CH[c].SLOT[s].key = 0;1763CH[c].SLOT[s].phase = 0;1764CH[c].SLOT[s].ssgn = 0;1765CH[c].SLOT[s].state = EG_OFF;1766CH[c].SLOT[s].volume = MAX_ATT_INDEX;1767CH[c].SLOT[s].vol_out = MAX_ATT_INDEX;1768}1769}1770}17711772/* initialize generic tables */1773static void init_tables(void)1774{1775signed int d,i,x;1776signed int n;1777double o,m;17781779/* build Linear Power Table */1780for (x=0; x<TL_RES_LEN; x++)1781{1782m = (1<<16) / pow(2,(x+1) * (ENV_STEP/4.0) / 8.0);1783m = floor(m);17841785/* we never reach (1<<16) here due to the (x+1) */1786/* result fits within 16 bits at maximum */17871788n = (int)m; /* 16 bits here */1789n >>= 4; /* 12 bits here */1790if (n&1) /* round to nearest */1791n = (n>>1)+1;1792else1793n = n>>1;1794/* 11 bits here (rounded) */1795n <<= 2; /* 13 bits here (as in real chip) */17961797/* 14 bits (with sign bit) */1798tl_tab[ x*2 + 0 ] = n;1799tl_tab[ x*2 + 1 ] = -tl_tab[ x*2 + 0 ];18001801/* one entry in the 'Power' table use the following format, xxxxxyyyyyyyys with: */1802/* s = sign bit */1803/* yyyyyyyy = 8-bits decimal part (0-TL_RES_LEN) */1804/* xxxxx = 5-bits integer 'shift' value (0-31) but, since Power table output is 13 bits, */1805/* any value above 13 (included) would be discarded. */1806for (i=1; i<13; i++)1807{1808tl_tab[ x*2+0 + i*2*TL_RES_LEN ] = tl_tab[ x*2+0 ]>>i;1809tl_tab[ x*2+1 + i*2*TL_RES_LEN ] = -tl_tab[ x*2+0 + i*2*TL_RES_LEN ];1810}1811}18121813/* build Logarithmic Sinus table */1814for (i=0; i<SIN_LEN; i++)1815{1816/* non-standard sinus */1817m = sin( ((i*2)+1) * M_PI / SIN_LEN ); /* checked against the real chip */1818/* we never reach zero here due to ((i*2)+1) */18191820if (m>0.0)1821o = 8*log(1.0/m)/log(2); /* convert to 'decibels' */1822else1823o = 8*log(-1.0/m)/log(2); /* convert to 'decibels' */18241825o = o / (ENV_STEP/4);18261827n = (int)(2.0*o);1828if (n&1) /* round to nearest */1829n = (n>>1)+1;1830else1831n = n>>1;18321833/* 13-bits (8.5) value is formatted for above 'Power' table */1834sin_tab[ i ] = n*2 + (m>=0.0? 0: 1 );1835}18361837/* build LFO PM modulation table */1838for(i = 0; i < 8; i++) /* 8 PM depths */1839{1840UINT8 fnum;1841for (fnum=0; fnum<128; fnum++) /* 7 bits meaningful of F-NUMBER */1842{1843UINT8 value;1844UINT8 step;1845UINT32 offset_depth = i;1846UINT32 offset_fnum_bit;1847UINT32 bit_tmp;18481849for (step=0; step<8; step++)1850{1851value = 0;1852for (bit_tmp=0; bit_tmp<7; bit_tmp++) /* 7 bits */1853{1854if (fnum & (1<<bit_tmp)) /* only if bit "bit_tmp" is set */1855{1856offset_fnum_bit = bit_tmp * 8;1857value += lfo_pm_output[offset_fnum_bit + offset_depth][step];1858}1859}1860/* 32 steps for LFO PM (sinus) */1861lfo_pm_table[(fnum*32*8) + (i*32) + step + 0] = value;1862lfo_pm_table[(fnum*32*8) + (i*32) +(step^7)+ 8] = value;1863lfo_pm_table[(fnum*32*8) + (i*32) + step +16] = -value;1864lfo_pm_table[(fnum*32*8) + (i*32) +(step^7)+24] = -value;1865}1866}1867}18681869/* build DETUNE table */1870for (d = 0;d <= 3;d++)1871{1872for (i = 0;i <= 31;i++)1873{1874ym2612.OPN.ST.dt_tab[d][i] = (INT32) dt_tab[d*32 + i];1875ym2612.OPN.ST.dt_tab[d+4][i] = -ym2612.OPN.ST.dt_tab[d][i];1876}1877}18781879}1880188118821883/* initialize ym2612 emulator */1884void YM2612Init(void)1885{1886memset(&ym2612,0,sizeof(YM2612));1887init_tables();1888}18891890/* reset OPN registers */1891void YM2612ResetChip(void)1892{1893int i;18941895ym2612.OPN.eg_timer = 0;1896ym2612.OPN.eg_cnt = 0;18971898ym2612.OPN.lfo_timer_overflow = 0;1899ym2612.OPN.lfo_timer = 0;1900ym2612.OPN.lfo_cnt = 0;1901ym2612.OPN.LFO_AM = 126;1902ym2612.OPN.LFO_PM = 0;19031904ym2612.OPN.ST.TAC = 0;1905ym2612.OPN.ST.TBC = 0;19061907ym2612.OPN.SL3.key_csm = 0;19081909ym2612.dacen = 0;1910ym2612.dacout = 0;19111912set_timers(0x30);1913ym2612.OPN.ST.TB = 0;1914ym2612.OPN.ST.TBL = 256 << 4;1915ym2612.OPN.ST.TA = 0;1916ym2612.OPN.ST.TAL = 1024;19171918reset_channels(&ym2612.CH[0] , 6 );19191920for(i = 0xb6 ; i >= 0xb4 ; i-- )1921{1922OPNWriteReg(i ,0xc0);1923OPNWriteReg(i|0x100,0xc0);1924}1925for(i = 0xb2 ; i >= 0x30 ; i-- )1926{1927OPNWriteReg(i ,0);1928OPNWriteReg(i|0x100,0);1929}1930}19311932/* ym2612 write */1933/* n = number */1934/* a = address */1935/* v = value */1936void YM2612Write(unsigned int a, unsigned int v)1937{1938v &= 0xff; /* adjust to 8 bit bus */19391940switch( a )1941{1942case 0: /* address port 0 */1943ym2612.OPN.ST.address = v;1944break;19451946case 2: /* address port 1 */1947ym2612.OPN.ST.address = v | 0x100;1948break;19491950default: /* data port */1951{1952int addr = ym2612.OPN.ST.address; /* verified by Nemesis on real YM2612 */1953switch( addr & 0x1f0 )1954{1955case 0x20: /* 0x20-0x2f Mode */1956switch( addr )1957{1958case 0x2a: /* DAC data (ym2612) */1959ym2612.dacout = ((int)v - 0x80) << 6; /* convert to 14-bit output */1960break;1961case 0x2b: /* DAC Sel (ym2612) */1962/* b7 = dac enable */1963ym2612.dacen = v & 0x80;1964break;1965default: /* OPN section */1966/* write register */1967OPNWriteMode(addr,v);1968}1969break;1970default: /* 0x30-0xff OPN section */1971/* write register */1972OPNWriteReg(addr,v);1973}1974break;1975}1976}1977}19781979unsigned int YM2612Read(void)1980{1981return ym2612.OPN.ST.status & 0xff;1982}19831984/* Generate samples for ym2612 */1985void YM2612Update(int *buffer, int length)1986{1987int i;1988int lt,rt;19891990/* refresh PG increments and EG rates if required */1991refresh_fc_eg_chan(&ym2612.CH[0]);1992refresh_fc_eg_chan(&ym2612.CH[1]);19931994if (!(ym2612.OPN.ST.mode & 0xC0))1995{1996refresh_fc_eg_chan(&ym2612.CH[2]);1997}1998else1999{2000/* 3SLOT MODE (operator order is 0,1,3,2) */2001if(ym2612.CH[2].SLOT[SLOT1].Incr==-1)2002{2003refresh_fc_eg_slot(&ym2612.CH[2].SLOT[SLOT1] , ym2612.OPN.SL3.fc[1] , ym2612.OPN.SL3.kcode[1] );2004refresh_fc_eg_slot(&ym2612.CH[2].SLOT[SLOT2] , ym2612.OPN.SL3.fc[2] , ym2612.OPN.SL3.kcode[2] );2005refresh_fc_eg_slot(&ym2612.CH[2].SLOT[SLOT3] , ym2612.OPN.SL3.fc[0] , ym2612.OPN.SL3.kcode[0] );2006refresh_fc_eg_slot(&ym2612.CH[2].SLOT[SLOT4] , ym2612.CH[2].fc , ym2612.CH[2].kcode );2007}2008}20092010refresh_fc_eg_chan(&ym2612.CH[3]);2011refresh_fc_eg_chan(&ym2612.CH[4]);2012refresh_fc_eg_chan(&ym2612.CH[5]);20132014/* buffering */2015for(i=0; i < length ; i++)2016{2017/* clear outputs */2018out_fm[0] = 0;2019out_fm[1] = 0;2020out_fm[2] = 0;2021out_fm[3] = 0;2022out_fm[4] = 0;2023out_fm[5] = 0;20242025/* update SSG-EG output */2026update_ssg_eg_channels(&ym2612.CH[0]);20272028/* calculate FM */2029if (!ym2612.dacen)2030{2031chan_calc(&ym2612.CH[0],6);2032}2033else2034{2035/* DAC Mode */2036out_fm[5] = ym2612.dacout;2037chan_calc(&ym2612.CH[0],5);2038}20392040/* advance LFO */2041advance_lfo();20422043/* advance envelope generator */2044ym2612.OPN.eg_timer ++;20452046/* EG is updated every 3 samples */2047if (ym2612.OPN.eg_timer >= 3)2048{2049ym2612.OPN.eg_timer = 0;2050ym2612.OPN.eg_cnt++;2051advance_eg_channels(&ym2612.CH[0], ym2612.OPN.eg_cnt);2052}20532054/* 14-bit accumulator channels outputs (range is -8192;+8192) */2055if (out_fm[0] > 8192) out_fm[0] = 8192;2056else if (out_fm[0] < -8192) out_fm[0] = -8192;2057if (out_fm[1] > 8192) out_fm[1] = 8192;2058else if (out_fm[1] < -8192) out_fm[1] = -8192;2059if (out_fm[2] > 8192) out_fm[2] = 8192;2060else if (out_fm[2] < -8192) out_fm[2] = -8192;2061if (out_fm[3] > 8192) out_fm[3] = 8192;2062else if (out_fm[3] < -8192) out_fm[3] = -8192;2063if (out_fm[4] > 8192) out_fm[4] = 8192;2064else if (out_fm[4] < -8192) out_fm[4] = -8192;2065if (out_fm[5] > 8192) out_fm[5] = 8192;2066else if (out_fm[5] < -8192) out_fm[5] = -8192;20672068/* stereo DAC channels outputs mixing */2069lt = ((out_fm[0]) & ym2612.OPN.pan[0]);2070rt = ((out_fm[0]) & ym2612.OPN.pan[1]);2071lt += ((out_fm[1]) & ym2612.OPN.pan[2]);2072rt += ((out_fm[1]) & ym2612.OPN.pan[3]);2073lt += ((out_fm[2]) & ym2612.OPN.pan[4]);2074rt += ((out_fm[2]) & ym2612.OPN.pan[5]);2075lt += ((out_fm[3]) & ym2612.OPN.pan[6]);2076rt += ((out_fm[3]) & ym2612.OPN.pan[7]);2077lt += ((out_fm[4]) & ym2612.OPN.pan[8]);2078rt += ((out_fm[4]) & ym2612.OPN.pan[9]);2079lt += ((out_fm[5]) & ym2612.OPN.pan[10]);2080rt += ((out_fm[5]) & ym2612.OPN.pan[11]);20812082/* buffering */2083*buffer++ = lt;2084*buffer++ = rt;20852086/* CSM mode: if CSM Key ON has occured, CSM Key OFF need to be sent */2087/* only if Timer A does not overflow again (i.e CSM Key ON not set again) */2088ym2612.OPN.SL3.key_csm <<= 1;20892090/* timer A control */2091INTERNAL_TIMER_A();20922093/* CSM Mode Key ON still disabled */2094if (ym2612.OPN.SL3.key_csm & 2)2095{2096/* CSM Mode Key OFF (verified by Nemesis on real hardware) */2097FM_KEYOFF_CSM(&ym2612.CH[2],SLOT1);2098FM_KEYOFF_CSM(&ym2612.CH[2],SLOT2);2099FM_KEYOFF_CSM(&ym2612.CH[2],SLOT3);2100FM_KEYOFF_CSM(&ym2612.CH[2],SLOT4);2101ym2612.OPN.SL3.key_csm = 0;2102}2103}21042105/* timer B control */2106INTERNAL_TIMER_B(length);2107}21082109void YM2612Config(unsigned char dac_bits)2110{2111int i;21122113/* DAC precision (normally 9-bit on real hardware, implemented through simple 14-bit channel output bitmasking) */2114bitmask = ~((1 << (TL_BITS - dac_bits)) - 1);21152116/* update L/R panning bitmasks */2117for (i=0; i<2*6; i++)2118{2119if (ym2612.OPN.pan[i])2120{2121ym2612.OPN.pan[i] = bitmask;2122}2123}2124}212521262127