xref: /qemu/hw/audio/fmopl.h (revision 8f7e2c2cb731765cde6521ae252d55f4d8a9a85f)
1 #ifndef FMOPL_H
2 #define FMOPL_H
3 
4 #include <stdint.h>
5 
6 typedef void (*OPL_TIMERHANDLER)(int channel,double interval_Sec);
7 typedef void (*OPL_IRQHANDLER)(int param,int irq);
8 typedef void (*OPL_UPDATEHANDLER)(int param,int min_interval_us);
9 typedef void (*OPL_PORTHANDLER_W)(int param,unsigned char data);
10 typedef unsigned char (*OPL_PORTHANDLER_R)(int param);
11 
12 /* !!!!! here is private section , do not access there member direct !!!!! */
13 
14 /* Saving is necessary for member of the 'R' mark for suspend/resume */
15 /* ---------- OPL one of slot  ---------- */
16 typedef struct fm_opl_slot {
17 	int32_t TL;		/* total level     :TL << 8            */
18 	int32_t TLL;		/* adjusted now TL                     */
19 	uint8_t  KSR;		/* key scale rate  :(shift down bit)   */
20 	int32_t *AR;		/* attack rate     :&AR_TABLE[AR<<2]   */
21 	int32_t *DR;		/* decay rate      :&DR_TALBE[DR<<2]   */
22 	int32_t SL;		/* sustin level    :SL_TALBE[SL]       */
23 	int32_t *RR;		/* release rate    :&DR_TABLE[RR<<2]   */
24 	uint8_t ksl;		/* keyscale level  :(shift down bits)  */
25 	uint8_t ksr;		/* key scale rate  :kcode>>KSR         */
26 	uint32_t mul;		/* multiple        :ML_TABLE[ML]       */
27 	uint32_t Cnt;		/* frequency count :                   */
28 	uint32_t Incr;	/* frequency step  :                   */
29 	/* envelope generator state */
30 	uint8_t eg_typ;	/* envelope type flag                  */
31 	uint8_t evm;		/* envelope phase                      */
32 	int32_t evc;		/* envelope counter                    */
33 	int32_t eve;		/* envelope counter end point          */
34 	int32_t evs;		/* envelope counter step               */
35 	int32_t evsa;	/* envelope step for AR :AR[ksr]           */
36 	int32_t evsd;	/* envelope step for DR :DR[ksr]           */
37 	int32_t evsr;	/* envelope step for RR :RR[ksr]           */
38 	/* LFO */
39 	uint8_t ams;		/* ams flag                            */
40 	uint8_t vib;		/* vibrate flag                        */
41 	/* wave selector */
42 	int32_t **wavetable;
43 }OPL_SLOT;
44 
45 /* ---------- OPL one of channel  ---------- */
46 typedef struct fm_opl_channel {
47 	OPL_SLOT SLOT[2];
48 	uint8_t CON;			/* connection type                     */
49 	uint8_t FB;			/* feed back       :(shift down bit)   */
50 	int32_t *connect1;	/* slot1 output pointer                */
51 	int32_t *connect2;	/* slot2 output pointer                */
52 	int32_t op1_out[2];	/* slot1 output for selfeedback        */
53 	/* phase generator state */
54 	uint32_t  block_fnum;	/* block+fnum      :                   */
55 	uint8_t kcode;		/* key code        : KeyScaleCode      */
56 	uint32_t  fc;			/* Freq. Increment base                */
57 	uint32_t  ksl_base;	/* KeyScaleLevel Base step             */
58 	uint8_t keyon;		/* key on/off flag                     */
59 } OPL_CH;
60 
61 /* OPL state */
62 typedef struct fm_opl_f {
63 	int clock;			/* master clock  (Hz)                */
64 	int rate;			/* sampling rate (Hz)                */
65 	double freqbase;	/* frequency base                    */
66 	double TimerBase;	/* Timer base time (==sampling time) */
67 	uint8_t address;		/* address register                  */
68 	uint8_t status;		/* status flag                       */
69 	uint8_t statusmask;	/* status mask                       */
70 	uint32_t mode;		/* Reg.08 : CSM , notesel,etc.       */
71 	/* Timer */
72 	int T[2];			/* timer counter                     */
73 	uint8_t st[2];		/* timer enable                      */
74 	/* FM channel slots */
75 	OPL_CH *P_CH;		/* pointer of CH                     */
76 	int	max_ch;			/* maximum channel                   */
77 	/* Rhythm sention */
78 	uint8_t rhythm;		/* Rhythm mode , key flag */
79 	OPL_PORTHANDLER_R porthandler_r;
80 	OPL_PORTHANDLER_W porthandler_w;
81 	int port_param;
82 	OPL_PORTHANDLER_R keyboardhandler_r;
83 	OPL_PORTHANDLER_W keyboardhandler_w;
84 	int keyboard_param;
85 	/* time tables */
86 	int32_t AR_TABLE[75];	/* atttack rate tables */
87 	int32_t DR_TABLE[75];	/* decay rate tables   */
88 	uint32_t FN_TABLE[1024];  /* fnumber -> increment counter */
89 	/* LFO */
90 	int32_t *ams_table;
91 	int32_t *vib_table;
92 	int32_t amsCnt;
93 	int32_t amsIncr;
94 	int32_t vibCnt;
95 	int32_t vibIncr;
96 	/* wave selector enable flag */
97 	uint8_t wavesel;
98 	/* external event callback handler */
99 	OPL_TIMERHANDLER  TimerHandler;		/* TIMER handler   */
100 	int TimerParam;						/* TIMER parameter */
101 	OPL_IRQHANDLER    IRQHandler;		/* IRQ handler    */
102 	int IRQParam;						/* IRQ parameter  */
103 	OPL_UPDATEHANDLER UpdateHandler;	/* stream update handler   */
104 	int UpdateParam;					/* stream update parameter */
105 } FM_OPL;
106 
107 /* ---------- Generic interface section ---------- */
108 FM_OPL *OPLCreate(int clock, int rate);
109 void OPLDestroy(FM_OPL *OPL);
110 void OPLSetTimerHandler(FM_OPL *OPL,OPL_TIMERHANDLER TimerHandler,int channelOffset);
111 void OPLSetIRQHandler(FM_OPL *OPL,OPL_IRQHANDLER IRQHandler,int param);
112 void OPLSetUpdateHandler(FM_OPL *OPL,OPL_UPDATEHANDLER UpdateHandler,int param);
113 
114 void OPLResetChip(FM_OPL *OPL);
115 int OPLWrite(FM_OPL *OPL,int a,int v);
116 unsigned char OPLRead(FM_OPL *OPL,int a);
117 int OPLTimerOver(FM_OPL *OPL,int c);
118 
119 void YM3812UpdateOne(FM_OPL *OPL, int16_t *buffer, int length);
120 #endif
121