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