2 * Copyright (C) 2002-2009 The DOSBox Team
3 * OPL2/OPL3 emulation library
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 * Originally based on ADLIBEMU.C, an AdLib/OPL2 emulation library by Ken Silverman
23 * Copyright (C) 1998-2001 Ken Silverman
24 * Ken Silverman's official web site: "http://www.advsys.net/ken"
37 define Bits, Bitu, Bit32s, Bit32u, Bit16s, Bit16u, Bit8s, Bit8u here
41 typedef uintptr_t Bitu
;
42 typedef intptr_t Bits
;
43 typedef uint32_t Bit32u
;
44 typedef int32_t Bit32s
;
45 typedef uint16_t Bit16u
;
46 typedef int16_t Bit16s
;
47 typedef uint8_t Bit8u
;
53 define attribution that inlines/forces inlining of a function (optional)
55 #define OPL_INLINE INLINE
59 #if defined(OPLTYPE_IS_OPL3)
60 #define NUM_CHANNELS 18
62 #define NUM_CHANNELS 9
65 #define MAXOPERATORS (NUM_CHANNELS*2)
68 #define FL05 ((fltype)0.5)
69 #define FL2 ((fltype)2.0)
70 #define PI ((fltype)3.1415926535897932384626433832795)
73 #define FIXEDPT 0x10000 // fixed-point calculations using 16+16
74 #define FIXEDPT_LFO 0x1000000 // fixed-point calculations using 8+24
76 #define WAVEPREC 1024 // waveform precision (10 bits)
78 #define INTFREQU ((fltype)(14318180.0 / 288.0)) // clocking of the chip
85 #define OF_TYPE_SUS_NOKEEP 4
88 #define ARC_CONTROL 0x00
89 #define ARC_TVS_KSR_MUL 0x20
90 #define ARC_KSL_OUTLEV 0x40
91 #define ARC_ATTR_DECR 0x60
92 #define ARC_SUSL_RELR 0x80
93 #define ARC_FREQ_NUM 0xa0
94 #define ARC_KON_BNUM 0xb0
95 #define ARC_PERC_MODE 0xbd
96 #define ARC_FEEDBACK 0xc0
97 #define ARC_WAVE_SEL 0xe0
99 #define ARC_SECONDSET 0x100 // second operator set for OPL3
102 #define OP_ACT_OFF 0x00
103 #define OP_ACT_NORMAL 0x01 // regular channel activated (bitmasked)
104 #define OP_ACT_PERC 0x02 // percussion channel activated (bitmasked)
106 #define BLOCKBUF_SIZE 512
110 #define VIBTAB_SIZE 8
111 #define VIBFAC 70/50000 // no braces, integer mul/div
113 // tremolo constants and table
114 #define TREMTAB_SIZE 53
115 #define TREM_FREQ ((fltype)(3.7)) // tremolo at 3.7hz
118 /* operator struct definition
119 For OPL2 all 9 channels consist of two operators each, carrier and modulator.
120 Channel x has operators x as modulator and operators (9+x) as carrier.
121 For OPL3 all 18 channels consist either of two operators (2op mode) or four
122 operators (4op mode) which is determined through register4 of the second
124 Only the channels 0,1,2 (first set) and 9,10,11 (second set) can act as
125 4op channels. The two additional operators for a channel y come from the
126 2op channel y+3 so the operatorss y, (9+y), y+3, (9+y)+3 make up a 4op
129 typedef struct operator_struct
{
130 Bit32s cval
, lastcval
; // current output/last output (used for feedback)
131 Bit32u tcount
, wfpos
, tinc
; // time (position in waveform) and time increment
132 fltype amp
, step_amp
; // and amplification (envelope)
133 fltype vol
; // volume
134 fltype sustain_level
; // sustain level
135 Bit32s mfbi
; // feedback amount
136 fltype a0
, a1
, a2
, a3
; // attack rate function coefficients
137 fltype decaymul
, releasemul
; // decay/release rate functions
138 Bit32u op_state
; // current state of operator (attack/decay/sustain/release/off)
140 Bit32s freq_high
; // highest three bits of the frequency, used for vibrato calculations
141 Bit16s
* cur_wform
; // start of selected waveform
142 Bit32u cur_wmask
; // mask for selected waveform
143 Bit32u act_state
; // activity state (regular, percussion)
144 bool sus_keep
; // keep sustain level when decay finished
145 bool vibrato
,tremolo
; // vibrato/tremolo enable bits
147 // variables used to provide non-continuous envelopes
148 Bit32u generator_pos
; // for non-standard sample rates we need to determine how many samples have passed
149 Bits cur_env_step
; // current (standardized) sample position
150 Bits env_step_a
,env_step_d
,env_step_r
; // number of std samples of one step (for attack/decay/release mode)
151 Bit8u step_skip_pos_a
; // position of 8-cyclic step skipping (always 2^x to check against mask)
152 Bits env_step_skip_a
; // bitmask that determines if a step is skipped (respective bit is zero then)
154 #if defined(OPLTYPE_IS_OPL3)
155 bool is_4op
,is_4op_attached
; // base of a 4op channel/part of a 4op channel
156 Bit32s left_pan
,right_pan
; // opl3 stereo panning amount
160 // per-chip variables
161 EXTERN Bitu chip_num
;
162 EXTERN op_type op
[MAXOPERATORS
];
164 EXTERN Bits int_samplerate
;
167 EXTERN Bit32u opl_index
;
168 #if defined(OPLTYPE_IS_OPL3)
169 EXTERN Bit8u adlibreg
[512]; // adlib register set (including second set)
170 EXTERN Bit8u wave_sel
[44]; // waveform selection
172 EXTERN Bit8u adlibreg
[256]; // adlib register set
173 EXTERN Bit8u wave_sel
[22]; // waveform selection
177 // vibrato/tremolo increment/counter
178 EXTERN Bit32u vibtab_pos
;
179 EXTERN Bit32u vibtab_add
;
180 EXTERN Bit32u tremtab_pos
;
181 EXTERN Bit32u tremtab_add
;
184 // enable an operator
185 void enable_operator(Bitu regbase
, op_type
* op_pt
);
187 // functions to change parameters of an operator
188 void change_frequency(Bitu chanbase
, Bitu regbase
, op_type
* op_pt
);
190 void change_attackrate(Bitu regbase
, op_type
* op_pt
);
191 void change_decayrate(Bitu regbase
, op_type
* op_pt
);
192 void change_releaserate(Bitu regbase
, op_type
* op_pt
);
193 void change_sustainlevel(Bitu regbase
, op_type
* op_pt
);
194 void change_waveform(Bitu regbase
, op_type
* op_pt
);
195 void change_keepsustain(Bitu regbase
, op_type
* op_pt
);
196 void change_vibrato(Bitu regbase
, op_type
* op_pt
);
197 void change_feedback(Bitu chanbase
, op_type
* op_pt
);
200 void adlib_init(Bit32u samplerate
);
201 void adlib_write(Bitu idx
, Bit8u val
);
202 void adlib_getsample(Bit16s
* sndptr
, Bits numsamples
);
204 Bitu
adlib_reg_read(Bitu port
);
205 void adlib_write_index(Bitu port
, Bit8u val
);
208 static Bit32u generator_add
; // should be a chip parameter