1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- #include "fmopl.h"
- #ifndef PI
- #define PI 3.14159265358979323846
- #endif
- # define INLINE
- #if (OPL_SAMPLE_BITS==16)
- #define FINAL_SH (0)
- #define MAXOUT (+32767)
- #define MINOUT (-32768)
- #else
- #define FINAL_SH (8)
- #define MAXOUT (+127)
- #define MINOUT (-128)
- #endif
- #define FREQ_SH 16
- #define EG_SH 16
- #define LFO_SH 24
- #define TIMER_SH 16
- #define FREQ_MASK ((1<<FREQ_SH)-1)
- /* envelope output entries */
- #define ENV_BITS 10
- #define ENV_LEN (1<<ENV_BITS)
- #define ENV_STEP (128.0/ENV_LEN)
- #define MAX_ATT_INDEX ((1<<(ENV_BITS-1))-1) /*511*/
- #define MIN_ATT_INDEX (0)
- /* sinwave entries */
- #define SIN_BITS 10
- #define SIN_LEN (1<<SIN_BITS)
- #define SIN_MASK (SIN_LEN-1)
- #define TL_RES_LEN (256) /* 8 bits addressing (real chip) */
- /* register number to channel number , slot offset */
- #define SLOT1 0
- #define SLOT2 1
- /* Envelope Generator phases */
- #define EG_ATT 4
- #define EG_DEC 3
- #define EG_SUS 2
- #define EG_REL 1
- #define EG_OFF 0
- /* save output as raw 16-bit sample */
- /*#define SAVE_SAMPLE*/
- #ifdef SAVE_SAMPLE
- INLINE signed int acc_calc(signed int value)
- {
- if (value>=0)
- {
- if (value < 0x0200)
- return (value & ~0);
- if (value < 0x0400)
- return (value & ~1);
- if (value < 0x0800)
- return (value & ~3);
- if (value < 0x1000)
- return (value & ~7);
- if (value < 0x2000)
- return (value & ~15);
- if (value < 0x4000)
- return (value & ~31);
- return (value & ~63);
- }
-
- if (value > -0x0200)
- return (~abs(value) & ~0);
- if (value > -0x0400)
- return (~abs(value) & ~1);
- if (value > -0x0800)
- return (~abs(value) & ~3);
- if (value > -0x1000)
- return (~abs(value) & ~7);
- if (value > -0x2000)
- return (~abs(value) & ~15);
- if (value > -0x4000)
- return (~abs(value) & ~31);
- return (~abs(value) & ~63);
- }
- static FILE *sample[1];
- #if 1
- #define SAVE_ALL_CHANNELS \
- { signed int pom = acc_calc(lt); \
- fputc((unsigned short)pom&0xff,sample[0]); \
- fputc(((unsigned short)pom>>8)&0xff,sample[0]); \
- }
- #else
- #define SAVE_ALL_CHANNELS \
- { signed int pom = lt; \
- fputc((unsigned short)pom&0xff,sample[0]); \
- fputc(((unsigned short)pom>>8)&0xff,sample[0]); \
- pom = rt; \
- fputc((unsigned short)pom&0xff,sample[0]); \
- fputc(((unsigned short)pom>>8)&0xff,sample[0]); \
- }
- #endif
- #endif
- #ifdef LOG_CYM_FILE
- FILE * cymfile = NULL;
- #endif
- static const int slot_array[32]=
- {
- 0, 2, 4, 1, 3, 5,-1,-1,
- 6, 8,10, 7, 9,11,-1,-1,
- 12,14,16,13,15,17,-1,-1,
- -1,-1,-1,-1,-1,-1,-1,-1
- };
- #define DV (0.1875/2.0)
- static const UINT32 ksl_tab[8*16]=
- {
-
- 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
- 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
- 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
- 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
-
- 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
- 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
- 0.000/DV, 0.750/DV, 1.125/DV, 1.500/DV,
- 1.875/DV, 2.250/DV, 2.625/DV, 3.000/DV,
-
- 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
- 0.000/DV, 1.125/DV, 1.875/DV, 2.625/DV,
- 3.000/DV, 3.750/DV, 4.125/DV, 4.500/DV,
- 4.875/DV, 5.250/DV, 5.625/DV, 6.000/DV,
-
- 0.000/DV, 0.000/DV, 0.000/DV, 1.875/DV,
- 3.000/DV, 4.125/DV, 4.875/DV, 5.625/DV,
- 6.000/DV, 6.750/DV, 7.125/DV, 7.500/DV,
- 7.875/DV, 8.250/DV, 8.625/DV, 9.000/DV,
-
- 0.000/DV, 0.000/DV, 3.000/DV, 4.875/DV,
- 6.000/DV, 7.125/DV, 7.875/DV, 8.625/DV,
- 9.000/DV, 9.750/DV,10.125/DV,10.500/DV,
- 10.875/DV,11.250/DV,11.625/DV,12.000/DV,
-
- 0.000/DV, 3.000/DV, 6.000/DV, 7.875/DV,
- 9.000/DV,10.125/DV,10.875/DV,11.625/DV,
- 12.000/DV,12.750/DV,13.125/DV,13.500/DV,
- 13.875/DV,14.250/DV,14.625/DV,15.000/DV,
-
- 0.000/DV, 6.000/DV, 9.000/DV,10.875/DV,
- 12.000/DV,13.125/DV,13.875/DV,14.625/DV,
- 15.000/DV,15.750/DV,16.125/DV,16.500/DV,
- 16.875/DV,17.250/DV,17.625/DV,18.000/DV,
-
- 0.000/DV, 9.000/DV,12.000/DV,13.875/DV,
- 15.000/DV,16.125/DV,16.875/DV,17.625/DV,
- 18.000/DV,18.750/DV,19.125/DV,19.500/DV,
- 19.875/DV,20.250/DV,20.625/DV,21.000/DV
- };
- #undef DV
- #define SC(db) (UINT32) ( db * (2.0/ENV_STEP) )
- static const UINT32 sl_tab[16]={
- SC( 0),SC( 1),SC( 2),SC(3 ),SC(4 ),SC(5 ),SC(6 ),SC( 7),
- SC( 8),SC( 9),SC(10),SC(11),SC(12),SC(13),SC(14),SC(31)
- };
- #undef SC
- #define RATE_STEPS (8)
- static const unsigned char eg_inc[15*RATE_STEPS]={
- 0,1, 0,1, 0,1, 0,1,
- 0,1, 0,1, 1,1, 0,1,
- 0,1, 1,1, 0,1, 1,1,
- 0,1, 1,1, 1,1, 1,1,
- 1,1, 1,1, 1,1, 1,1,
- 1,1, 1,2, 1,1, 1,2,
- 1,2, 1,2, 1,2, 1,2,
- 1,2, 2,2, 1,2, 2,2,
- 2,2, 2,2, 2,2, 2,2,
- 2,2, 2,4, 2,2, 2,4,
- 2,4, 2,4, 2,4, 2,4,
- 2,4, 4,4, 2,4, 4,4,
- 4,4, 4,4, 4,4, 4,4,
- 8,8, 8,8, 8,8, 8,8,
- 0,0, 0,0, 0,0, 0,0,
- };
- #define O(a) (a*RATE_STEPS)
- static const unsigned char eg_rate_select[16+64+16]={
- O(14),O(14),O(14),O(14),O(14),O(14),O(14),O(14),
- O(14),O(14),O(14),O(14),O(14),O(14),O(14),O(14),
- O( 0),O( 1),O( 2),O( 3),
- O( 0),O( 1),O( 2),O( 3),
- O( 0),O( 1),O( 2),O( 3),
- O( 0),O( 1),O( 2),O( 3),
- O( 0),O( 1),O( 2),O( 3),
- O( 0),O( 1),O( 2),O( 3),
- O( 0),O( 1),O( 2),O( 3),
- O( 0),O( 1),O( 2),O( 3),
- O( 0),O( 1),O( 2),O( 3),
- O( 0),O( 1),O( 2),O( 3),
- O( 0),O( 1),O( 2),O( 3),
- O( 0),O( 1),O( 2),O( 3),
- O( 0),O( 1),O( 2),O( 3),
- O( 4),O( 5),O( 6),O( 7),
- O( 8),O( 9),O(10),O(11),
- O(12),O(12),O(12),O(12),
- O(12),O(12),O(12),O(12),O(12),O(12),O(12),O(12),
- O(12),O(12),O(12),O(12),O(12),O(12),O(12),O(12),
- };
- #undef O
- #define O(a) (a*1)
- static const unsigned char eg_rate_shift[16+64+16]={
- O(0),O(0),O(0),O(0),O(0),O(0),O(0),O(0),
- O(0),O(0),O(0),O(0),O(0),O(0),O(0),O(0),
- O(12),O(12),O(12),O(12),
- O(11),O(11),O(11),O(11),
- O(10),O(10),O(10),O(10),
- O( 9),O( 9),O( 9),O( 9),
- O( 8),O( 8),O( 8),O( 8),
- O( 7),O( 7),O( 7),O( 7),
- O( 6),O( 6),O( 6),O( 6),
- O( 5),O( 5),O( 5),O( 5),
- O( 4),O( 4),O( 4),O( 4),
- O( 3),O( 3),O( 3),O( 3),
- O( 2),O( 2),O( 2),O( 2),
- O( 1),O( 1),O( 1),O( 1),
- O( 0),O( 0),O( 0),O( 0),
- O( 0),O( 0),O( 0),O( 0),
- O( 0),O( 0),O( 0),O( 0),
- O( 0),O( 0),O( 0),O( 0),
- O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),
- O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),
- };
- #undef O
- #define ML 2
- static const UINT8 mul_tab[16]= {
- 0.50*ML, 1.00*ML, 2.00*ML, 3.00*ML, 4.00*ML, 5.00*ML, 6.00*ML, 7.00*ML,
- 8.00*ML, 9.00*ML,10.00*ML,10.00*ML,12.00*ML,12.00*ML,15.00*ML,15.00*ML
- };
- #undef ML
- #define TL_TAB_LEN (12*2*TL_RES_LEN)
- static signed int tl_tab[TL_TAB_LEN];
- #define ENV_QUIET (TL_TAB_LEN>>4)
- static unsigned int sin_tab[SIN_LEN * 4];
- #define LFO_AM_TAB_ELEMENTS 210
- static const UINT8 lfo_am_table[LFO_AM_TAB_ELEMENTS] = {
- 0,0,0,0,0,0,0,
- 1,1,1,1,
- 2,2,2,2,
- 3,3,3,3,
- 4,4,4,4,
- 5,5,5,5,
- 6,6,6,6,
- 7,7,7,7,
- 8,8,8,8,
- 9,9,9,9,
- 10,10,10,10,
- 11,11,11,11,
- 12,12,12,12,
- 13,13,13,13,
- 14,14,14,14,
- 15,15,15,15,
- 16,16,16,16,
- 17,17,17,17,
- 18,18,18,18,
- 19,19,19,19,
- 20,20,20,20,
- 21,21,21,21,
- 22,22,22,22,
- 23,23,23,23,
- 24,24,24,24,
- 25,25,25,25,
- 26,26,26,
- 25,25,25,25,
- 24,24,24,24,
- 23,23,23,23,
- 22,22,22,22,
- 21,21,21,21,
- 20,20,20,20,
- 19,19,19,19,
- 18,18,18,18,
- 17,17,17,17,
- 16,16,16,16,
- 15,15,15,15,
- 14,14,14,14,
- 13,13,13,13,
- 12,12,12,12,
- 11,11,11,11,
- 10,10,10,10,
- 9,9,9,9,
- 8,8,8,8,
- 7,7,7,7,
- 6,6,6,6,
- 5,5,5,5,
- 4,4,4,4,
- 3,3,3,3,
- 2,2,2,2,
- 1,1,1,1
- };
- static const INT8 lfo_pm_table[8*8*2] = {
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 1, 0, 0, 0,-1, 0, 0, 0,
- 1, 0, 0, 0,-1, 0, 0, 0,
- 2, 1, 0,-1,-2,-1, 0, 1,
- 1, 0, 0, 0,-1, 0, 0, 0,
- 3, 1, 0,-1,-3,-1, 0, 1,
- 2, 1, 0,-1,-2,-1, 0, 1,
- 4, 2, 0,-2,-4,-2, 0, 2,
- 2, 1, 0,-1,-2,-1, 0, 1,
- 5, 2, 0,-2,-5,-2, 0, 2,
- 3, 1, 0,-1,-3,-1, 0, 1,
- 6, 3, 0,-3,-6,-3, 0, 3,
- 3, 1, 0,-1,-3,-1, 0, 1,
- 7, 3, 0,-3,-7,-3, 0, 3
- };
- static int num_lock = 0;
- static void *cur_chip = NULL;
- static OPL_SLOT *SLOT7_1, *SLOT7_2, *SLOT8_1, *SLOT8_2;
- static signed int phase_modulation;
- static signed int output[1];
- #if BUILD_Y8950
- static INT32 output_deltat[4];
- #endif
- static UINT32 LFO_AM;
- static INT32 LFO_PM;
- INLINE int limit( int val, int max, int min ) {
- if ( val > max )
- val = max;
- else if ( val < min )
- val = min;
- return val;
- }
- INLINE void OPL_STATUS_SET(FM_OPL *OPL,int flag)
- {
-
- OPL->status |= flag;
- if(!(OPL->status & 0x80))
- {
- if(OPL->status & OPL->statusmask)
- {
- OPL->status |= 0x80;
-
- if(OPL->IRQHandler) (OPL->IRQHandler)(OPL->IRQParam,1);
- }
- }
- }
- INLINE void OPL_STATUS_RESET(FM_OPL *OPL,int flag)
- {
-
- OPL->status &=~flag;
- if((OPL->status & 0x80))
- {
- if (!(OPL->status & OPL->statusmask) )
- {
- OPL->status &= 0x7f;
-
- if(OPL->IRQHandler) (OPL->IRQHandler)(OPL->IRQParam,0);
- }
- }
- }
- INLINE void OPL_STATUSMASK_SET(FM_OPL *OPL,int flag)
- {
- OPL->statusmask = flag;
-
- OPL_STATUS_SET(OPL,0);
- OPL_STATUS_RESET(OPL,0);
- }
- INLINE void advance_lfo(FM_OPL *OPL)
- {
- UINT8 tmp;
-
- OPL->lfo_am_cnt += OPL->lfo_am_inc;
- if (OPL->lfo_am_cnt >= (LFO_AM_TAB_ELEMENTS<<LFO_SH) )
- OPL->lfo_am_cnt -= (LFO_AM_TAB_ELEMENTS<<LFO_SH);
- tmp = lfo_am_table[ OPL->lfo_am_cnt >> LFO_SH ];
- if (OPL->lfo_am_depth)
- LFO_AM = tmp;
- else
- LFO_AM = tmp>>2;
- OPL->lfo_pm_cnt += OPL->lfo_pm_inc;
- LFO_PM = ((OPL->lfo_pm_cnt>>LFO_SH) & 7) | OPL->lfo_pm_depth_range;
- }
- INLINE void advance(FM_OPL *OPL)
- {
- OPL_CH *CH;
- OPL_SLOT *op;
- int i;
- OPL->eg_timer += OPL->eg_timer_add;
- while (OPL->eg_timer >= OPL->eg_timer_overflow)
- {
- OPL->eg_timer -= OPL->eg_timer_overflow;
- OPL->eg_cnt++;
- for (i=0; i<9*2; i++)
- {
- CH = &OPL->P_CH[i/2];
- op = &CH->SLOT[i&1];
-
- switch(op->state)
- {
- case EG_ATT:
- if ( !(OPL->eg_cnt & ((1<<op->eg_sh_ar)-1) ) )
- {
- op->volume += (~op->volume *
- (eg_inc[op->eg_sel_ar + ((OPL->eg_cnt>>op->eg_sh_ar)&7)])
- ) >>3;
- if (op->volume <= MIN_ATT_INDEX)
- {
- op->volume = MIN_ATT_INDEX;
- op->state = EG_DEC;
- }
- }
- break;
- case EG_DEC:
- if ( !(OPL->eg_cnt & ((1<<op->eg_sh_dr)-1) ) )
- {
- op->volume += eg_inc[op->eg_sel_dr + ((OPL->eg_cnt>>op->eg_sh_dr)&7)];
- if ( op->volume >= op->sl )
- op->state = EG_SUS;
- }
- break;
- case EG_SUS:
-
- if(op->eg_type)
- {
-
- }
- else
- {
-
- if ( !(OPL->eg_cnt & ((1<<op->eg_sh_rr)-1) ) )
- {
- op->volume += eg_inc[op->eg_sel_rr + ((OPL->eg_cnt>>op->eg_sh_rr)&7)];
- if ( op->volume >= MAX_ATT_INDEX )
- op->volume = MAX_ATT_INDEX;
- }
-
- }
- break;
- case EG_REL:
- if ( !(OPL->eg_cnt & ((1<<op->eg_sh_rr)-1) ) )
- {
- op->volume += eg_inc[op->eg_sel_rr + ((OPL->eg_cnt>>op->eg_sh_rr)&7)];
- if ( op->volume >= MAX_ATT_INDEX )
- {
- op->volume = MAX_ATT_INDEX;
- op->state = EG_OFF;
- }
- }
- break;
- default:
- break;
- }
- }
- }
- for (i=0; i<9*2; i++)
- {
- CH = &OPL->P_CH[i/2];
- op = &CH->SLOT[i&1];
-
- if(op->vib)
- {
- UINT8 block;
- unsigned int block_fnum = CH->block_fnum;
- unsigned int fnum_lfo = (block_fnum&0x0380) >> 7;
- signed int lfo_fn_table_index_offset = lfo_pm_table[LFO_PM + 16*fnum_lfo ];
- if (lfo_fn_table_index_offset)
- {
- block_fnum += lfo_fn_table_index_offset;
- block = (block_fnum&0x1c00) >> 10;
- op->Cnt += (OPL->fn_tab[block_fnum&0x03ff] >> (7-block)) * op->mul;
- }
- else
- {
- op->Cnt += op->Incr;
- }
- }
- else
- {
- op->Cnt += op->Incr;
- }
- }
-
- OPL->noise_p += OPL->noise_f;
- i = OPL->noise_p >> FREQ_SH;
- OPL->noise_p &= FREQ_MASK;
- while (i)
- {
-
-
- if (OPL->noise_rng & 1) OPL->noise_rng ^= 0x800302;
- OPL->noise_rng >>= 1;
- i--;
- }
- }
- INLINE signed int op_calc(UINT32 phase, unsigned int env, signed int pm, unsigned int wave_tab)
- {
- UINT32 p;
- p = (env<<4) + sin_tab[wave_tab + ((((signed int)((phase & ~FREQ_MASK) + (pm<<16))) >> FREQ_SH ) & SIN_MASK) ];
- if (p >= TL_TAB_LEN)
- return 0;
- return tl_tab[p];
- }
- INLINE signed int op_calc1(UINT32 phase, unsigned int env, signed int pm, unsigned int wave_tab)
- {
- UINT32 p;
- p = (env<<4) + sin_tab[wave_tab + ((((signed int)((phase & ~FREQ_MASK) + pm )) >> FREQ_SH ) & SIN_MASK) ];
- if (p >= TL_TAB_LEN)
- return 0;
- return tl_tab[p];
- }
- #define volume_calc(OP) ((OP)->TLL + ((UINT32)(OP)->volume) + (LFO_AM & (OP)->AMmask))
- INLINE void OPL_CALC_CH( OPL_CH *CH )
- {
- OPL_SLOT *SLOT;
- unsigned int env;
- signed int out;
- phase_modulation = 0;
-
- SLOT = &CH->SLOT[SLOT1];
- env = volume_calc(SLOT);
- out = SLOT->op1_out[0] + SLOT->op1_out[1];
- SLOT->op1_out[0] = SLOT->op1_out[1];
- *SLOT->connect1 += SLOT->op1_out[0];
- SLOT->op1_out[1] = 0;
- if( env < ENV_QUIET )
- {
- if (!SLOT->FB)
- out = 0;
- SLOT->op1_out[1] = op_calc1(SLOT->Cnt, env, (out<<SLOT->FB), SLOT->wavetable );
- }
-
- SLOT++;
- env = volume_calc(SLOT);
- if( env < ENV_QUIET )
- output[0] += op_calc(SLOT->Cnt, env, phase_modulation, SLOT->wavetable);
- }
- INLINE void OPL_CALC_RH( OPL_CH *CH, unsigned int noise )
- {
- OPL_SLOT *SLOT;
- signed int out;
- unsigned int env;
-
- phase_modulation = 0;
-
- SLOT = &CH[6].SLOT[SLOT1];
- env = volume_calc(SLOT);
- out = SLOT->op1_out[0] + SLOT->op1_out[1];
- SLOT->op1_out[0] = SLOT->op1_out[1];
- if (!SLOT->CON)
- phase_modulation = SLOT->op1_out[0];
-
- SLOT->op1_out[1] = 0;
- if( env < ENV_QUIET )
- {
- if (!SLOT->FB)
- out = 0;
- SLOT->op1_out[1] = op_calc1(SLOT->Cnt, env, (out<<SLOT->FB), SLOT->wavetable );
- }
-
- SLOT++;
- env = volume_calc(SLOT);
- if( env < ENV_QUIET )
- output[0] += op_calc(SLOT->Cnt, env, phase_modulation, SLOT->wavetable) * 2;
-
-
-
-
-
-
-
-
-
-
-
-
- env = volume_calc(SLOT7_1);
- if( env < ENV_QUIET )
- {
-
-
- unsigned char bit7 = ((SLOT7_1->Cnt>>FREQ_SH)>>7)&1;
- unsigned char bit3 = ((SLOT7_1->Cnt>>FREQ_SH)>>3)&1;
- unsigned char bit2 = ((SLOT7_1->Cnt>>FREQ_SH)>>2)&1;
- unsigned char res1 = (bit2 ^ bit7) | bit3;
-
-
- UINT32 phase = res1 ? (0x200|(0xd0>>2)) : 0xd0;
-
- unsigned char bit5e= ((SLOT8_2->Cnt>>FREQ_SH)>>5)&1;
- unsigned char bit3e= ((SLOT8_2->Cnt>>FREQ_SH)>>3)&1;
- unsigned char res2 = (bit3e ^ bit5e);
-
-
- if (res2)
- phase = (0x200|(0xd0>>2));
-
-
- if (phase&0x200)
- {
- if (noise)
- phase = 0x200|0xd0;
- }
- else
-
-
- {
- if (noise)
- phase = 0xd0>>2;
- }
- output[0] += op_calc(phase<<FREQ_SH, env, 0, SLOT7_1->wavetable) * 2;
- }
-
- env = volume_calc(SLOT7_2);
- if( env < ENV_QUIET )
- {
-
- unsigned char bit8 = ((SLOT7_1->Cnt>>FREQ_SH)>>8)&1;
-
-
- UINT32 phase = bit8 ? 0x200 : 0x100;
-
-
-
-
- if (noise)
- phase ^= 0x100;
- output[0] += op_calc(phase<<FREQ_SH, env, 0, SLOT7_2->wavetable) * 2;
- }
-
- env = volume_calc(SLOT8_1);
- if( env < ENV_QUIET )
- output[0] += op_calc(SLOT8_1->Cnt, env, 0, SLOT8_1->wavetable) * 2;
-
- env = volume_calc(SLOT8_2);
- if( env < ENV_QUIET )
- {
-
- unsigned char bit7 = ((SLOT7_1->Cnt>>FREQ_SH)>>7)&1;
- unsigned char bit3 = ((SLOT7_1->Cnt>>FREQ_SH)>>3)&1;
- unsigned char bit2 = ((SLOT7_1->Cnt>>FREQ_SH)>>2)&1;
- unsigned char res1 = (bit2 ^ bit7) | bit3;
-
-
- UINT32 phase = res1 ? 0x300 : 0x100;
-
- unsigned char bit5e= ((SLOT8_2->Cnt>>FREQ_SH)>>5)&1;
- unsigned char bit3e= ((SLOT8_2->Cnt>>FREQ_SH)>>3)&1;
- unsigned char res2 = (bit3e ^ bit5e);
-
-
- if (res2)
- phase = 0x300;
- output[0] += op_calc(phase<<FREQ_SH, env, 0, SLOT8_2->wavetable) * 2;
- }
- }
- static int init_tables(void)
- {
- signed int i,x;
- signed int n;
- double o,m;
- for (x=0; x<TL_RES_LEN; x++)
- {
- m = (1<<16) / pow(2, (x+1) * (ENV_STEP/4.0) / 8.0);
- m = floor(m);
-
-
- n = (int)m;
- n >>= 4;
- if (n&1)
- n = (n>>1)+1;
- else
- n = n>>1;
-
- n <<= 1;
- tl_tab[ x*2 + 0 ] = n;
- tl_tab[ x*2 + 1 ] = -tl_tab[ x*2 + 0 ];
- for (i=1; i<12; i++)
- {
- tl_tab[ x*2+0 + i*2*TL_RES_LEN ] = tl_tab[ x*2+0 ]>>i;
- tl_tab[ x*2+1 + i*2*TL_RES_LEN ] = -tl_tab[ x*2+0 + i*2*TL_RES_LEN ];
- }
- #if 0
-
- for (i=0; i<12; i++)
-
-
- #endif
- }
-
- for (i=0; i<SIN_LEN; i++)
- {
-
- m = sin( ((i*2)+1) * PI / SIN_LEN );
-
- if (m>0.0)
- o = 8*log(1.0/m)/log(2);
- else
- o = 8*log(-1.0/m)/log(2);
- o = o / (ENV_STEP/4);
- n = (int)(2.0*o);
- if (n&1)
- n = (n>>1)+1;
- else
- n = n>>1;
- sin_tab[ i ] = n*2 + (m>=0.0? 0: 1 );
-
- }
- for (i=0; i<SIN_LEN; i++)
- {
-
-
-
- if (i & (1<<(SIN_BITS-1)) )
- sin_tab[1*SIN_LEN+i] = TL_TAB_LEN;
- else
- sin_tab[1*SIN_LEN+i] = sin_tab[i];
-
-
-
- sin_tab[2*SIN_LEN+i] = sin_tab[i & (SIN_MASK>>1) ];
-
-
-
- if (i & (1<<(SIN_BITS-2)) )
- sin_tab[3*SIN_LEN+i] = TL_TAB_LEN;
- else
- sin_tab[3*SIN_LEN+i] = sin_tab[i & (SIN_MASK>>2)];
-
- }
-
- #ifdef SAVE_SAMPLE
- sample[0]=fopen("sampsum.pcm","wb");
- #endif
- return 1;
- }
- static void OPLCloseTable( void )
- {
- #ifdef SAVE_SAMPLE
- fclose(sample[0]);
- #endif
- }
- static void OPL_initalize(FM_OPL *OPL)
- {
- int i;
-
- OPL->freqbase = (OPL->rate) ? ((double)OPL->clock / 72.0) / OPL->rate : 0;
- #if 0
- OPL->rate = (double)OPL->clock / 72.0;
- OPL->freqbase = 1.0;
- #endif
-
-
- OPL->TimerBase = 1.0 / ((double)OPL->clock / 72.0 );
-
- for( i=0 ; i < 1024 ; i++ )
- {
-
- OPL->fn_tab[i] = (UINT32)( (double)i * 64 * OPL->freqbase * (1<<(FREQ_SH-10)) );
- #if 0
-
- i, OPL->fn_tab[i]>>6, OPL->fn_tab[i]>>6 );
- #endif
- }
- #if 0
- for( i=0 ; i < 16 ; i++ )
- {
-
- i, sl_tab[i] );
- }
- for( i=0 ; i < 8 ; i++ )
- {
- int j;
-
- for (j=0; j<16; j++)
- {
-
- }
-
- }
- #endif
-
-
- OPL->lfo_am_inc = (1.0 / 64.0 ) * (1<<LFO_SH) * OPL->freqbase;
-
- OPL->lfo_pm_inc = (1.0 / 1024.0) * (1<<LFO_SH) * OPL->freqbase;
-
-
- OPL->noise_f = (1.0 / 1.0) * (1<<FREQ_SH) * OPL->freqbase;
- OPL->eg_timer_add = (1<<EG_SH) * OPL->freqbase;
- OPL->eg_timer_overflow = ( 1 ) * (1<<EG_SH);
-
- }
- INLINE void FM_KEYON(OPL_SLOT *SLOT, UINT32 key_set)
- {
- if( !SLOT->key )
- {
-
- SLOT->Cnt = 0;
-
- SLOT->state = EG_ATT;
- }
- SLOT->key |= key_set;
- }
- INLINE void FM_KEYOFF(OPL_SLOT *SLOT, UINT32 key_clr)
- {
- if( SLOT->key )
- {
- SLOT->key &= key_clr;
- if( !SLOT->key )
- {
-
- if (SLOT->state>EG_REL)
- SLOT->state = EG_REL;
- }
- }
- }
- INLINE void CALC_FCSLOT(OPL_CH *CH,OPL_SLOT *SLOT)
- {
- int ksr;
-
- SLOT->Incr = CH->fc * SLOT->mul;
- ksr = CH->kcode >> SLOT->KSR;
- if( SLOT->ksr != ksr )
- {
- SLOT->ksr = ksr;
-
- if ((SLOT->ar + SLOT->ksr) < 16+62)
- {
- SLOT->eg_sh_ar = eg_rate_shift [SLOT->ar + SLOT->ksr ];
- SLOT->eg_sel_ar = eg_rate_select[SLOT->ar + SLOT->ksr ];
- }
- else
- {
- SLOT->eg_sh_ar = 0;
- SLOT->eg_sel_ar = 13*RATE_STEPS;
- }
- SLOT->eg_sh_dr = eg_rate_shift [SLOT->dr + SLOT->ksr ];
- SLOT->eg_sel_dr = eg_rate_select[SLOT->dr + SLOT->ksr ];
- SLOT->eg_sh_rr = eg_rate_shift [SLOT->rr + SLOT->ksr ];
- SLOT->eg_sel_rr = eg_rate_select[SLOT->rr + SLOT->ksr ];
- }
- }
- INLINE void set_mul(FM_OPL *OPL,int slot,int v)
- {
- OPL_CH *CH = &OPL->P_CH[slot/2];
- OPL_SLOT *SLOT = &CH->SLOT[slot&1];
- SLOT->mul = mul_tab[v&0x0f];
- SLOT->KSR = (v&0x10) ? 0 : 2;
- SLOT->eg_type = (v&0x20);
- SLOT->vib = (v&0x40);
- SLOT->AMmask = (v&0x80) ? ~0 : 0;
- CALC_FCSLOT(CH,SLOT);
- }
- INLINE void set_ksl_tl(FM_OPL *OPL,int slot,int v)
- {
- OPL_CH *CH = &OPL->P_CH[slot/2];
- OPL_SLOT *SLOT = &CH->SLOT[slot&1];
- int ksl = v>>6;
- SLOT->ksl = ksl ? 3-ksl : 31;
- SLOT->TL = (v&0x3f)<<(ENV_BITS-1-7);
- SLOT->TLL = SLOT->TL + (CH->ksl_base>>SLOT->ksl);
- }
- INLINE void set_ar_dr(FM_OPL *OPL,int slot,int v)
- {
- OPL_CH *CH = &OPL->P_CH[slot/2];
- OPL_SLOT *SLOT = &CH->SLOT[slot&1];
- SLOT->ar = (v>>4) ? 16 + ((v>>4) <<2) : 0;
- if ((SLOT->ar + SLOT->ksr) < 16+62)
- {
- SLOT->eg_sh_ar = eg_rate_shift [SLOT->ar + SLOT->ksr ];
- SLOT->eg_sel_ar = eg_rate_select[SLOT->ar + SLOT->ksr ];
- }
- else
- {
- SLOT->eg_sh_ar = 0;
- SLOT->eg_sel_ar = 13*RATE_STEPS;
- }
- SLOT->dr = (v&0x0f)? 16 + ((v&0x0f)<<2) : 0;
- SLOT->eg_sh_dr = eg_rate_shift [SLOT->dr + SLOT->ksr ];
- SLOT->eg_sel_dr = eg_rate_select[SLOT->dr + SLOT->ksr ];
- }
- INLINE void set_sl_rr(FM_OPL *OPL,int slot,int v)
- {
- OPL_CH *CH = &OPL->P_CH[slot/2];
- OPL_SLOT *SLOT = &CH->SLOT[slot&1];
- SLOT->sl = sl_tab[ v>>4 ];
- SLOT->rr = (v&0x0f)? 16 + ((v&0x0f)<<2) : 0;
- SLOT->eg_sh_rr = eg_rate_shift [SLOT->rr + SLOT->ksr ];
- SLOT->eg_sel_rr = eg_rate_select[SLOT->rr + SLOT->ksr ];
- }
- static void OPLWriteReg(FM_OPL *OPL, int r, int v)
- {
- OPL_CH *CH;
- int slot;
- int block_fnum;
-
- r &= 0xff;
- v &= 0xff;
- #ifdef LOG_CYM_FILE
- if ((cymfile) && (r!=0) )
- {
- fputc( (unsigned char)r, cymfile );
- fputc( (unsigned char)v, cymfile );
- }
- #endif
- switch(r&0xe0)
- {
- case 0x00:
- switch(r&0x1f)
- {
- case 0x01:
- if(OPL->type&OPL_TYPE_WAVESEL)
- {
- OPL->wavesel = v&0x20;
-
- }
- break;
- case 0x02:
- OPL->T[0] = (256-v)*4;
- break;
- case 0x03:
- OPL->T[1] = (256-v)*16;
- break;
- case 0x04:
- if(v&0x80)
- {
- OPL_STATUS_RESET(OPL,0x7f-0x08);
- }
- else
- {
- UINT8 st1 = v&1;
- UINT8 st2 = (v>>1)&1;
-
- OPL_STATUS_RESET(OPL, v & (0x78-0x08) );
- OPL_STATUSMASK_SET(OPL, (~v) & 0x78 );
-
- if(OPL->st[1] != st2)
- {
- double interval = st2 ? (double)OPL->T[1]*OPL->TimerBase : 0.0;
- OPL->st[1] = st2;
- if (OPL->TimerHandler) (OPL->TimerHandler)(OPL->TimerParam+1,interval);
- }
-
- if(OPL->st[0] != st1)
- {
- double interval = st1 ? (double)OPL->T[0]*OPL->TimerBase : 0.0;
- OPL->st[0] = st1;
- if (OPL->TimerHandler) (OPL->TimerHandler)(OPL->TimerParam+0,interval);
- }
- }
- break;
- #if BUILD_Y8950
- case 0x06:
- if(OPL->type&OPL_TYPE_KEYBOARD)
- {
- if(OPL->keyboardhandler_w)
- OPL->keyboardhandler_w(OPL->keyboard_param,v);
- else
-
- }
- break;
- case 0x07:
- if(OPL->type&OPL_TYPE_ADPCM)
- YM_DELTAT_ADPCM_Write(OPL->deltat,r-0x07,v);
- break;
- #endif
- case 0x08:
- OPL->mode = v;
- #if BUILD_Y8950
- if(OPL->type&OPL_TYPE_ADPCM)
- YM_DELTAT_ADPCM_Write(OPL->deltat,r-0x07,v&0x0f);
- #endif
- break;
- #if BUILD_Y8950
- case 0x09:
- case 0x0a:
- case 0x0b:
- case 0x0c:
- case 0x0d:
- case 0x0e:
- case 0x0f:
- case 0x10:
- case 0x11:
- case 0x12:
- if(OPL->type&OPL_TYPE_ADPCM)
- YM_DELTAT_ADPCM_Write(OPL->deltat,r-0x07,v);
- break;
- case 0x15:
- case 0x16:
- case 0x17:
-
- break;
- case 0x18:
- if(OPL->type&OPL_TYPE_IO)
- OPL->portDirection = v&0x0f;
- break;
- case 0x19:
- if(OPL->type&OPL_TYPE_IO)
- {
- OPL->portLatch = v;
- if(OPL->porthandler_w)
- OPL->porthandler_w(OPL->port_param,v&OPL->portDirection);
- }
- break;
- #endif
- default:
-
- break;
- }
- break;
- case 0x20:
- slot = slot_array[r&0x1f];
- if(slot < 0) return;
- set_mul(OPL,slot,v);
- break;
- case 0x40:
- slot = slot_array[r&0x1f];
- if(slot < 0) return;
- set_ksl_tl(OPL,slot,v);
- break;
- case 0x60:
- slot = slot_array[r&0x1f];
- if(slot < 0) return;
- set_ar_dr(OPL,slot,v);
- break;
- case 0x80:
- slot = slot_array[r&0x1f];
- if(slot < 0) return;
- set_sl_rr(OPL,slot,v);
- break;
- case 0xa0:
- if (r == 0xbd)
- {
- OPL->lfo_am_depth = v & 0x80;
- OPL->lfo_pm_depth_range = (v&0x40) ? 8 : 0;
- OPL->rhythm = v&0x3f;
- if(OPL->rhythm&0x20)
- {
-
- if(v&0x10)
- {
- FM_KEYON (&OPL->P_CH[6].SLOT[SLOT1], 2);
- FM_KEYON (&OPL->P_CH[6].SLOT[SLOT2], 2);
- }
- else
- {
- FM_KEYOFF(&OPL->P_CH[6].SLOT[SLOT1],~2);
- FM_KEYOFF(&OPL->P_CH[6].SLOT[SLOT2],~2);
- }
-
- if(v&0x01) FM_KEYON (&OPL->P_CH[7].SLOT[SLOT1], 2);
- else FM_KEYOFF(&OPL->P_CH[7].SLOT[SLOT1],~2);
-
- if(v&0x08) FM_KEYON (&OPL->P_CH[7].SLOT[SLOT2], 2);
- else FM_KEYOFF(&OPL->P_CH[7].SLOT[SLOT2],~2);
-
- if(v&0x04) FM_KEYON (&OPL->P_CH[8].SLOT[SLOT1], 2);
- else FM_KEYOFF(&OPL->P_CH[8].SLOT[SLOT1],~2);
-
- if(v&0x02) FM_KEYON (&OPL->P_CH[8].SLOT[SLOT2], 2);
- else FM_KEYOFF(&OPL->P_CH[8].SLOT[SLOT2],~2);
- }
- else
- {
-
- FM_KEYOFF(&OPL->P_CH[6].SLOT[SLOT1],~2);
- FM_KEYOFF(&OPL->P_CH[6].SLOT[SLOT2],~2);
-
- FM_KEYOFF(&OPL->P_CH[7].SLOT[SLOT1],~2);
-
- FM_KEYOFF(&OPL->P_CH[7].SLOT[SLOT2],~2);
-
- FM_KEYOFF(&OPL->P_CH[8].SLOT[SLOT1],~2);
-
- FM_KEYOFF(&OPL->P_CH[8].SLOT[SLOT2],~2);
- }
- return;
- }
-
- if( (r&0x0f) > 8) return;
- CH = &OPL->P_CH[r&0x0f];
- if(!(r&0x10))
- {
- block_fnum = (CH->block_fnum&0x1f00) | v;
- }
- else
- {
- block_fnum = ((v&0x1f)<<8) | (CH->block_fnum&0xff);
- if(v&0x20)
- {
- FM_KEYON (&CH->SLOT[SLOT1], 1);
- FM_KEYON (&CH->SLOT[SLOT2], 1);
- }
- else
- {
- FM_KEYOFF(&CH->SLOT[SLOT1],~1);
- FM_KEYOFF(&CH->SLOT[SLOT2],~1);
- }
- }
-
- if(CH->block_fnum != block_fnum)
- {
- UINT8 block = block_fnum >> 10;
- CH->block_fnum = block_fnum;
- CH->ksl_base = ksl_tab[block_fnum>>6];
- CH->fc = OPL->fn_tab[block_fnum&0x03ff] >> (7-block);
-
- CH->kcode = (CH->block_fnum&0x1c00)>>9;
-
-
-
- if (OPL->mode&0x40)
- CH->kcode |= (CH->block_fnum&0x100)>>8;
- else
- CH->kcode |= (CH->block_fnum&0x200)>>9;
-
- CH->SLOT[SLOT1].TLL = CH->SLOT[SLOT1].TL + (CH->ksl_base>>CH->SLOT[SLOT1].ksl);
- CH->SLOT[SLOT2].TLL = CH->SLOT[SLOT2].TL + (CH->ksl_base>>CH->SLOT[SLOT2].ksl);
-
- CALC_FCSLOT(CH,&CH->SLOT[SLOT1]);
- CALC_FCSLOT(CH,&CH->SLOT[SLOT2]);
- }
- break;
- case 0xc0:
-
- if( (r&0x0f) > 8) return;
- CH = &OPL->P_CH[r&0x0f];
- CH->SLOT[SLOT1].FB = (v>>1)&7 ? ((v>>1)&7) + 7 : 0;
- CH->SLOT[SLOT1].CON = v&1;
- CH->SLOT[SLOT1].connect1 = CH->SLOT[SLOT1].CON ? &output[0] : &phase_modulation;
- break;
- case 0xe0:
-
- if(OPL->wavesel)
- {
- slot = slot_array[r&0x1f];
- if(slot < 0) return;
- CH = &OPL->P_CH[slot/2];
- CH->SLOT[slot&1].wavetable = (v&0x03)*SIN_LEN;
- }
- break;
- }
- }
- #ifdef LOG_CYM_FILE
- static void cymfile_callback (int n)
- {
- if (cymfile)
- {
- fputc( (unsigned char)0, cymfile );
- }
- }
- #endif
- static int OPL_LockTable(void)
- {
- num_lock++;
- if(num_lock>1) return 0;
-
- cur_chip = NULL;
-
- if( !init_tables() )
- {
- num_lock--;
- return -1;
- }
- #ifdef LOG_CYM_FILE
- cymfile = fopen("3812_.cym","wb");
- if (cymfile)
- timer_pulse ( TIME_IN_HZ(110), 0, cymfile_callback);
- else
-
- #endif
- return 0;
- }
- static void OPL_UnLockTable(void)
- {
- if(num_lock) num_lock--;
- if(num_lock) return;
-
- cur_chip = NULL;
- OPLCloseTable();
- #ifdef LOG_CYM_FILE
- fclose (cymfile);
- cymfile = NULL;
- #endif
- }
- void OPLResetChip(FM_OPL *OPL)
- {
- int c,s;
- int i;
- OPL->eg_timer = 0;
- OPL->eg_cnt = 0;
- OPL->noise_rng = 1;
- OPL->mode = 0;
- OPL_STATUS_RESET(OPL,0x7f);
-
- OPLWriteReg(OPL,0x01,0);
- OPLWriteReg(OPL,0x02,0);
- OPLWriteReg(OPL,0x03,0);
- OPLWriteReg(OPL,0x04,0);
- for(i = 0xff ; i >= 0x20 ; i-- ) OPLWriteReg(OPL,i,0);
-
- for( c = 0 ; c < 9 ; c++ )
- {
- OPL_CH *CH = &OPL->P_CH[c];
- for(s = 0 ; s < 2 ; s++ )
- {
-
- CH->SLOT[s].wavetable = 0;
- CH->SLOT[s].state = EG_OFF;
- CH->SLOT[s].volume = MAX_ATT_INDEX;
- }
- }
- #if BUILD_Y8950
- if(OPL->type&OPL_TYPE_ADPCM)
- {
- YM_DELTAT *DELTAT = OPL->deltat;
- DELTAT->freqbase = OPL->freqbase;
- DELTAT->output_pointer = &output_deltat[0];
- DELTAT->portshift = 5;
- DELTAT->output_range = 1<<23;
- YM_DELTAT_ADPCM_Reset(DELTAT,0,YM_DELTAT_EMULATION_MODE_NORMAL);
- }
- #endif
- }
- FM_OPL *OPLCreate(int type, int clock, int rate)
- {
- char *ptr;
- FM_OPL *OPL;
- int state_size;
- if (OPL_LockTable() ==-1) return NULL;
-
- state_size = sizeof(FM_OPL);
- #if BUILD_Y8950
- if (type&OPL_TYPE_ADPCM) state_size+= sizeof(YM_DELTAT);
- #endif
-
- ptr = malloc(state_size);
- if (ptr==NULL)
- return NULL;
-
- memset(ptr,0,state_size);
- OPL = (FM_OPL *)ptr;
- ptr += sizeof(FM_OPL);
- #if BUILD_Y8950
- if (type&OPL_TYPE_ADPCM)
- {
- OPL->deltat = (YM_DELTAT *)ptr;
- }
- ptr += sizeof(YM_DELTAT);
- #endif
- OPL->type = type;
- OPL->clock = clock;
- OPL->rate = rate;
-
- OPL_initalize(OPL);
- return OPL;
- }
- void OPLDestroy(FM_OPL *OPL)
- {
- OPL_UnLockTable();
- free(OPL);
- }
- static void OPLSetTimerHandler(FM_OPL *OPL,OPL_TIMERHANDLER TimerHandler,int channelOffset)
- {
- OPL->TimerHandler = TimerHandler;
- OPL->TimerParam = channelOffset;
- }
- static void OPLSetIRQHandler(FM_OPL *OPL,OPL_IRQHANDLER IRQHandler,int param)
- {
- OPL->IRQHandler = IRQHandler;
- OPL->IRQParam = param;
- }
- static void OPLSetUpdateHandler(FM_OPL *OPL,OPL_UPDATEHANDLER UpdateHandler,int param)
- {
- OPL->UpdateHandler = UpdateHandler;
- OPL->UpdateParam = param;
- }
- int OPLWrite(FM_OPL *OPL,int a,int v)
- {
- if( !(a&1) )
- {
- OPL->address = v & 0xff;
- }
- else
- {
- if(OPL->UpdateHandler) OPL->UpdateHandler(OPL->UpdateParam,0);
- OPLWriteReg(OPL,OPL->address,v);
- }
- return OPL->status>>7;
- }
- static unsigned char OPLRead(FM_OPL *OPL,int a)
- {
- if( !(a&1) )
- {
-
- #if BUILD_Y8950
- if(OPL->type&OPL_TYPE_ADPCM)
- {
- return (OPL->status & (OPL->statusmask|0x80)) | (OPL->deltat->PCM_BSY&1);
- }
-
- #endif
-
-
- return OPL->status & (OPL->statusmask|0x80);
- }
- #if BUILD_Y8950
-
- switch(OPL->address)
- {
- case 0x05:
- if(OPL->type&OPL_TYPE_KEYBOARD)
- {
- if(OPL->keyboardhandler_r)
- return OPL->keyboardhandler_r(OPL->keyboard_param);
- else
-
- }
- return 0;
- case 0x0f:
- if(OPL->type&OPL_TYPE_ADPCM)
- {
- UINT8 val;
- val = YM_DELTAT_ADPCM_Read(OPL->deltat);
-
- return val;
- }
- return 0;
- case 0x19:
- if(OPL->type&OPL_TYPE_IO)
- {
- if(OPL->porthandler_r)
- return OPL->porthandler_r(OPL->port_param);
- else
-
- }
- return 0;
- case 0x1a:
- if(OPL->type&OPL_TYPE_ADPCM)
- {
-
- return 0x80;
- }
- return 0;
- }
- #endif
- return 0xff;
- }
- INLINE void CSMKeyControll(OPL_CH *CH)
- {
- FM_KEYON (&CH->SLOT[SLOT1], 4);
- FM_KEYON (&CH->SLOT[SLOT2], 4);
-
- FM_KEYOFF(&CH->SLOT[SLOT1], ~4);
- FM_KEYOFF(&CH->SLOT[SLOT2], ~4);
- }
- static int OPLTimerOver(FM_OPL *OPL,int c)
- {
- if( c )
- {
- OPL_STATUS_SET(OPL,0x20);
- }
- else
- {
- OPL_STATUS_SET(OPL,0x40);
-
- if( OPL->mode & 0x80 )
- {
- int ch;
- if(OPL->UpdateHandler) OPL->UpdateHandler(OPL->UpdateParam,0);
- for(ch=0; ch<9; ch++)
- CSMKeyControll( &OPL->P_CH[ch] );
- }
- }
-
- if (OPL->TimerHandler) (OPL->TimerHandler)(OPL->TimerParam+c,(double)OPL->T[c]*OPL->TimerBase);
- return OPL->status>>7;
- }
- #define MAX_OPL_CHIPS 2
- #if (BUILD_YM3812)
- static FM_OPL *OPL_YM3812[MAX_OPL_CHIPS];
- static int YM3812NumChips = 0;
- int YM3812Init(int num, int clock, int rate)
- {
- int i;
- if (YM3812NumChips)
- return -1;
- YM3812NumChips = num;
- for (i = 0;i < YM3812NumChips; i++)
- {
-
- OPL_YM3812[i] = OPLCreate(OPL_TYPE_YM3812,clock,rate);
- if(OPL_YM3812[i] == NULL)
- {
-
- YM3812NumChips = 0;
- return -1;
- }
-
- YM3812ResetChip(i);
- }
- return 0;
- }
- void YM3812Shutdown(void)
- {
- int i;
- for (i = 0;i < YM3812NumChips; i++)
- {
-
- OPLDestroy(OPL_YM3812[i]);
- OPL_YM3812[i] = NULL;
- }
- YM3812NumChips = 0;
- }
- void YM3812ResetChip(int which)
- {
- OPLResetChip(OPL_YM3812[which]);
- }
- int YM3812Write(int which, int a, int v)
- {
- return OPLWrite(OPL_YM3812[which], a, v);
- }
- unsigned char YM3812Read(int which, int a)
- {
-
- return OPLRead(OPL_YM3812[which], a) | 0x06 ;
- }
- int YM3812TimerOver(int which, int c)
- {
- return OPLTimerOver(OPL_YM3812[which], c);
- }
- void YM3812SetTimerHandler(int which, OPL_TIMERHANDLER TimerHandler, int channelOffset)
- {
- OPLSetTimerHandler(OPL_YM3812[which], TimerHandler, channelOffset);
- }
- void YM3812SetIRQHandler(int which,OPL_IRQHANDLER IRQHandler,int param)
- {
- OPLSetIRQHandler(OPL_YM3812[which], IRQHandler, param);
- }
- void YM3812SetUpdateHandler(int which,OPL_UPDATEHANDLER UpdateHandler,int param)
- {
- OPLSetUpdateHandler(OPL_YM3812[which], UpdateHandler, param);
- }
- void YM3812UpdateOne(FM_OPL *OPL, INT16 *buffer, int length)
- {
- UINT8 rhythm = OPL->rhythm&0x20;
- OPLSAMPLE *buf = buffer;
- int i;
- if( (void *)OPL != cur_chip ){
- cur_chip = (void *)OPL;
-
- SLOT7_1 = &OPL->P_CH[7].SLOT[SLOT1];
- SLOT7_2 = &OPL->P_CH[7].SLOT[SLOT2];
- SLOT8_1 = &OPL->P_CH[8].SLOT[SLOT1];
- SLOT8_2 = &OPL->P_CH[8].SLOT[SLOT2];
- }
- for( i=0; i < length ; i++ )
- {
- int lt;
- output[0] = 0;
- advance_lfo(OPL);
-
- OPL_CALC_CH(&OPL->P_CH[0]);
- OPL_CALC_CH(&OPL->P_CH[1]);
- OPL_CALC_CH(&OPL->P_CH[2]);
- OPL_CALC_CH(&OPL->P_CH[3]);
- OPL_CALC_CH(&OPL->P_CH[4]);
- OPL_CALC_CH(&OPL->P_CH[5]);
- if(!rhythm)
- {
- OPL_CALC_CH(&OPL->P_CH[6]);
- OPL_CALC_CH(&OPL->P_CH[7]);
- OPL_CALC_CH(&OPL->P_CH[8]);
- }
- else
- {
- OPL_CALC_RH(&OPL->P_CH[0], (OPL->noise_rng>>0)&1 );
- }
- lt = output[0];
- lt >>= FINAL_SH;
-
- lt = limit( lt , MAXOUT, MINOUT );
- #ifdef SAVE_SAMPLE
- if (which==0)
- {
- SAVE_ALL_CHANNELS
- }
- #endif
-
- buf[i] = lt;
- advance(OPL);
- }
- }
- #endif
- #if (BUILD_YM3526)
- static FM_OPL *OPL_YM3526[MAX_OPL_CHIPS];
- static int YM3526NumChips = 0;
- int YM3526Init(int num, int clock, int rate)
- {
- int i;
- if (YM3526NumChips)
- return -1;
- YM3526NumChips = num;
- for (i = 0;i < YM3526NumChips; i++)
- {
-
- OPL_YM3526[i] = OPLCreate(OPL_TYPE_YM3526,clock,rate);
- if(OPL_YM3526[i] == NULL)
- {
-
- YM3526NumChips = 0;
- return -1;
- }
-
- YM3526ResetChip(i);
- }
- return 0;
- }
- void YM3526Shutdown(void)
- {
- int i;
- for (i = 0;i < YM3526NumChips; i++)
- {
-
- OPLDestroy(OPL_YM3526[i]);
- OPL_YM3526[i] = NULL;
- }
- YM3526NumChips = 0;
- }
- void YM3526ResetChip(int which)
- {
- OPLResetChip(OPL_YM3526[which]);
- }
- int YM3526Write(int which, int a, int v)
- {
- return OPLWrite(OPL_YM3526[which], a, v);
- }
- unsigned char YM3526Read(int which, int a)
- {
-
- return OPLRead(OPL_YM3526[which], a) | 0x06 ;
- }
- int YM3526TimerOver(int which, int c)
- {
- return OPLTimerOver(OPL_YM3526[which], c);
- }
- void YM3526SetTimerHandler(int which, OPL_TIMERHANDLER TimerHandler, int channelOffset)
- {
- OPLSetTimerHandler(OPL_YM3526[which], TimerHandler, channelOffset);
- }
- void YM3526SetIRQHandler(int which,OPL_IRQHANDLER IRQHandler,int param)
- {
- OPLSetIRQHandler(OPL_YM3526[which], IRQHandler, param);
- }
- void YM3526SetUpdateHandler(int which,OPL_UPDATEHANDLER UpdateHandler,int param)
- {
- OPLSetUpdateHandler(OPL_YM3526[which], UpdateHandler, param);
- }
- void YM3526UpdateOne(int which, INT16 *buffer, int length)
- {
- FM_OPL *OPL = OPL_YM3526[which];
- UINT8 rhythm = OPL->rhythm&0x20;
- OPLSAMPLE *buf = buffer;
- int i;
- if( (void *)OPL != cur_chip ){
- cur_chip = (void *)OPL;
-
- SLOT7_1 = &OPL->P_CH[7].SLOT[SLOT1];
- SLOT7_2 = &OPL->P_CH[7].SLOT[SLOT2];
- SLOT8_1 = &OPL->P_CH[8].SLOT[SLOT1];
- SLOT8_2 = &OPL->P_CH[8].SLOT[SLOT2];
- }
- for( i=0; i < length ; i++ )
- {
- int lt;
- output[0] = 0;
- advance_lfo(OPL);
-
- OPL_CALC_CH(&OPL->P_CH[0]);
- OPL_CALC_CH(&OPL->P_CH[1]);
- OPL_CALC_CH(&OPL->P_CH[2]);
- OPL_CALC_CH(&OPL->P_CH[3]);
- OPL_CALC_CH(&OPL->P_CH[4]);
- OPL_CALC_CH(&OPL->P_CH[5]);
- if(!rhythm)
- {
- OPL_CALC_CH(&OPL->P_CH[6]);
- OPL_CALC_CH(&OPL->P_CH[7]);
- OPL_CALC_CH(&OPL->P_CH[8]);
- }
- else
- {
- OPL_CALC_RH(&OPL->P_CH[0], (OPL->noise_rng>>0)&1 );
- }
- lt = output[0];
- lt >>= FINAL_SH;
-
- lt = limit( lt , MAXOUT, MINOUT );
- #ifdef SAVE_SAMPLE
- if (which==0)
- {
- SAVE_ALL_CHANNELS
- }
- #endif
-
- buf[i] = lt;
- advance(OPL);
- }
- }
- #endif
- #if BUILD_Y8950
- static FM_OPL *OPL_Y8950[MAX_OPL_CHIPS];
- static int Y8950NumChips = 0;
- static void Y8950_deltat_status_set(UINT8 which, UINT8 changebits)
- {
- OPL_STATUS_SET(OPL_Y8950[which], changebits);
- }
- static void Y8950_deltat_status_reset(UINT8 which, UINT8 changebits)
- {
- OPL_STATUS_RESET(OPL_Y8950[which], changebits);
- }
- int Y8950Init(int num, int clock, int rate)
- {
- int i;
- if (Y8950NumChips)
- return -1;
- Y8950NumChips = num;
- for (i = 0;i < Y8950NumChips; i++)
- {
-
- OPL_Y8950[i] = OPLCreate(OPL_TYPE_Y8950,clock,rate);
- if(OPL_Y8950[i] == NULL)
- {
-
- Y8950NumChips = 0;
- return -1;
- }
- OPL_Y8950[i]->deltat->status_set_handler = Y8950_deltat_status_set;
- OPL_Y8950[i]->deltat->status_reset_handler = Y8950_deltat_status_reset;
- OPL_Y8950[i]->deltat->status_change_which_chip = i;
- OPL_Y8950[i]->deltat->status_change_EOS_bit = 0x10;
- OPL_Y8950[i]->deltat->status_change_BRDY_bit = 0x08;
-
-
-
- Y8950ResetChip(i);
- }
- return 0;
- }
- void Y8950Shutdown(void)
- {
- int i;
- for (i = 0;i < Y8950NumChips; i++)
- {
-
- OPLDestroy(OPL_Y8950[i]);
- OPL_Y8950[i] = NULL;
- }
- Y8950NumChips = 0;
- }
- void Y8950ResetChip(int which)
- {
- OPLResetChip(OPL_Y8950[which]);
- }
- int Y8950Write(int which, int a, int v)
- {
- return OPLWrite(OPL_Y8950[which], a, v);
- }
- unsigned char Y8950Read(int which, int a)
- {
- return OPLRead(OPL_Y8950[which], a);
- }
- int Y8950TimerOver(int which, int c)
- {
- return OPLTimerOver(OPL_Y8950[which], c);
- }
- void Y8950SetTimerHandler(int which, OPL_TIMERHANDLER TimerHandler, int channelOffset)
- {
- OPLSetTimerHandler(OPL_Y8950[which], TimerHandler, channelOffset);
- }
- void Y8950SetIRQHandler(int which,OPL_IRQHANDLER IRQHandler,int param)
- {
- OPLSetIRQHandler(OPL_Y8950[which], IRQHandler, param);
- }
- void Y8950SetUpdateHandler(int which,OPL_UPDATEHANDLER UpdateHandler,int param)
- {
- OPLSetUpdateHandler(OPL_Y8950[which], UpdateHandler, param);
- }
- void Y8950SetDeltaTMemory(int which, void * deltat_mem_ptr, int deltat_mem_size )
- {
- FM_OPL *OPL = OPL_Y8950[which];
- OPL->deltat->memory = (UINT8 *)(deltat_mem_ptr);
- OPL->deltat->memory_size = deltat_mem_size;
- }
- void Y8950UpdateOne(int which, INT16 *buffer, int length)
- {
- int i;
- FM_OPL *OPL = OPL_Y8950[which];
- UINT8 rhythm = OPL->rhythm&0x20;
- YM_DELTAT *DELTAT = OPL->deltat;
- OPLSAMPLE *buf = buffer;
- if( (void *)OPL != cur_chip ){
- cur_chip = (void *)OPL;
-
- SLOT7_1 = &OPL->P_CH[7].SLOT[SLOT1];
- SLOT7_2 = &OPL->P_CH[7].SLOT[SLOT2];
- SLOT8_1 = &OPL->P_CH[8].SLOT[SLOT1];
- SLOT8_2 = &OPL->P_CH[8].SLOT[SLOT2];
- }
- for( i=0; i < length ; i++ )
- {
- int lt;
- output[0] = 0;
- output_deltat[0] = 0;
- advance_lfo(OPL);
-
- if( DELTAT->portstate&0x80 )
- YM_DELTAT_ADPCM_CALC(DELTAT);
-
- OPL_CALC_CH(&OPL->P_CH[0]);
- OPL_CALC_CH(&OPL->P_CH[1]);
- OPL_CALC_CH(&OPL->P_CH[2]);
- OPL_CALC_CH(&OPL->P_CH[3]);
- OPL_CALC_CH(&OPL->P_CH[4]);
- OPL_CALC_CH(&OPL->P_CH[5]);
- if(!rhythm)
- {
- OPL_CALC_CH(&OPL->P_CH[6]);
- OPL_CALC_CH(&OPL->P_CH[7]);
- OPL_CALC_CH(&OPL->P_CH[8]);
- }
- else
- {
- OPL_CALC_RH(&OPL->P_CH[0], (OPL->noise_rng>>0)&1 );
- }
- lt = output[0] + (output_deltat[0]>>11);
- lt >>= FINAL_SH;
-
- lt = limit( lt , MAXOUT, MINOUT );
- #ifdef SAVE_SAMPLE
- if (which==0)
- {
- SAVE_ALL_CHANNELS
- }
- #endif
-
- buf[i] = lt;
- advance(OPL);
- }
- }
- void Y8950SetPortHandler(int which,OPL_PORTHANDLER_W PortHandler_w,OPL_PORTHANDLER_R PortHandler_r,int param)
- {
- FM_OPL *OPL = OPL_Y8950[which];
- OPL->porthandler_w = PortHandler_w;
- OPL->porthandler_r = PortHandler_r;
- OPL->port_param = param;
- }
- void Y8950SetKeyboardHandler(int which,OPL_PORTHANDLER_W KeyboardHandler_w,OPL_PORTHANDLER_R KeyboardHandler_r,int param)
- {
- FM_OPL *OPL = OPL_Y8950[which];
- OPL->keyboardhandler_w = KeyboardHandler_w;
- OPL->keyboardhandler_r = KeyboardHandler_r;
- OPL->keyboard_param = param;
- }
- #endif
|