Debian Codegraph @

src/hardware/opl.h

raw symbols SCIP
1 /*
2 * Copyright (C) 2002-2010 The DOSBox Team
3 * OPL2/OPL3 emulation library
4 *
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.
9 *
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.
14 *
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
18 */
19
20
21 /*
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"
25 */
26
27
28 #define fltype double
29
30 /*
31 define Bits, Bitu, Bit32s, Bit32u, Bit16s, Bit16u, Bit8s, Bit8u here
32 */
33 /*
34 #include <stdint.h>
35 typedef uintptr_t Bitu;
36 typedef intptr_t Bits;
37 typedef uint32_t Bit32u;
38 typedef int32_t Bit32s;
39 typedef uint16_t Bit16u;
40 typedef int16_t Bit16s;
41 typedef uint8_t Bit8u;
42 typedef int8_t Bit8s;
43 */
44
45
46 /*
47 define attribution that inlines/forces inlining of a function (optional)
48 */
49 #define OPL_INLINE INLINE
50
51
52 #undef NUM_CHANNELS
53 #if defined(OPLTYPE_IS_OPL3)
54 #define NUM_CHANNELS 18
55 #else
56 #define NUM_CHANNELS 9
57 #endif
58
59 #define MAXOPERATORS (NUM_CHANNELS*2)
60
61
62 #define FL05 ((fltype)0.5)
63 #define FL2 ((fltype)2.0)
64 #define PI ((fltype)3.1415926535897932384626433832795)
65
66
67 #define FIXEDPT 0x10000 // fixed-point calculations using 16+16
68 #define FIXEDPT_LFO 0x1000000 // fixed-point calculations using 8+24
69
70 #define WAVEPREC 1024 // waveform precision (10 bits)
71
72 #define INTFREQU ((fltype)(14318180.0 / 288.0)) // clocking of the chip
73
74
75 #define OF_TYPE_ATT 0
76 #define OF_TYPE_DEC 1
77 #define OF_TYPE_REL 2
78 #define OF_TYPE_SUS 3
79 #define OF_TYPE_SUS_NOKEEP 4
80 #define OF_TYPE_OFF 5
81
82 #define ARC_CONTROL 0x00
83 #define ARC_TVS_KSR_MUL 0x20
84 #define ARC_KSL_OUTLEV 0x40
85 #define ARC_ATTR_DECR 0x60
86 #define ARC_SUSL_RELR 0x80
87 #define ARC_FREQ_NUM 0xa0
88 #define ARC_KON_BNUM 0xb0
89 #define ARC_PERC_MODE 0xbd
90 #define ARC_FEEDBACK 0xc0
91 #define ARC_WAVE_SEL 0xe0
92
93 #define ARC_SECONDSET 0x100 // second operator set for OPL3
94
95
96 #define OP_ACT_OFF 0x00
97 #define OP_ACT_NORMAL 0x01 // regular channel activated (bitmasked)
98 #define OP_ACT_PERC 0x02 // percussion channel activated (bitmasked)
99
100 #define BLOCKBUF_SIZE 512
101
102
103 // vibrato constants
104 #define VIBTAB_SIZE 8
105 #define VIBFAC 70/50000 // no braces, integer mul/div
106
107 // tremolo constants and table
108 #define TREMTAB_SIZE 53
109 #define TREM_FREQ ((fltype)(3.7)) // tremolo at 3.7hz
110
111
112 /* operator struct definition
113 For OPL2 all 9 channels consist of two operators each, carrier and modulator.
114 Channel x has operators x as modulator and operators (9+x) as carrier.
115 For OPL3 all 18 channels consist either of two operators (2op mode) or four
116 operators (4op mode) which is determined through register4 of the second
117 adlib register set.
118 Only the channels 0,1,2 (first set) and 9,10,11 (second set) can act as
119 4op channels. The two additional operators for a channel y come from the
120 2op channel y+3 so the operatorss y, (9+y), y+3, (9+y)+3 make up a 4op
121 channel.
122 */
123 typedef struct operator_struct {
124 Bit32s cval, lastcval; // current output/last output (used for feedback)
125 Bit32u tcount, wfpos, tinc; // time (position in waveform) and time increment
126 fltype amp, step_amp; // and amplification (envelope)
127 fltype vol; // volume
128 fltype sustain_level; // sustain level
129 Bit32s mfbi; // feedback amount
130 fltype a0, a1, a2, a3; // attack rate function coefficients
131 fltype decaymul, releasemul; // decay/release rate functions
132 Bit32u op_state; // current state of operator (attack/decay/sustain/release/off)
133 Bit32u toff;
134 Bit32s freq_high; // highest three bits of the frequency, used for vibrato calculations
135 Bit16s* cur_wform; // start of selected waveform
136 Bit32u cur_wmask; // mask for selected waveform
137 Bit32u act_state; // activity state (regular, percussion)
138 bool sus_keep; // keep sustain level when decay finished
139 bool vibrato,tremolo; // vibrato/tremolo enable bits
140
141 // variables used to provide non-continuous envelopes
142 Bit32u generator_pos; // for non-standard sample rates we need to determine how many samples have passed
143 Bits cur_env_step; // current (standardized) sample position
144 Bits env_step_a,env_step_d,env_step_r; // number of std samples of one step (for attack/decay/release mode)
145 Bit8u step_skip_pos_a; // position of 8-cyclic step skipping (always 2^x to check against mask)
146 Bits env_step_skip_a; // bitmask that determines if a step is skipped (respective bit is zero then)
147
148 #if defined(OPLTYPE_IS_OPL3)
149 bool is_4op,is_4op_attached; // base of a 4op channel/part of a 4op channel
150 Bit32s left_pan,right_pan; // opl3 stereo panning amount
151 #endif
152 } op_type;
153
154 // per-chip variables
155 Bitu chip_num;
156 op_type op[MAXOPERATORS];
157
158 Bits int_samplerate;
159
160 Bit8u status;
161 Bit32u opl_index;
162 #if defined(OPLTYPE_IS_OPL3)
163 Bit8u adlibreg[512]; // adlib register set (including second set)
164 Bit8u wave_sel[44]; // waveform selection
165 #else
166 Bit8u adlibreg[256]; // adlib register set
167 Bit8u wave_sel[22]; // waveform selection
168 #endif
169
170
171 // vibrato/tremolo increment/counter
172 Bit32u vibtab_pos;
173 Bit32u vibtab_add;
174 Bit32u tremtab_pos;
175 Bit32u tremtab_add;
176
177
178 // enable an operator
179 void enable_operator(Bitu regbase, op_type* op_pt);
180
181 // functions to change parameters of an operator
182 void change_frequency(Bitu chanbase, Bitu regbase, op_type* op_pt);
183
184 void change_attackrate(Bitu regbase, op_type* op_pt);
185 void change_decayrate(Bitu regbase, op_type* op_pt);
186 void change_releaserate(Bitu regbase, op_type* op_pt);
187 void change_sustainlevel(Bitu regbase, op_type* op_pt);
188 void change_waveform(Bitu regbase, op_type* op_pt);
189 void change_keepsustain(Bitu regbase, op_type* op_pt);
190 void change_vibrato(Bitu regbase, op_type* op_pt);
191 void change_feedback(Bitu chanbase, op_type* op_pt);
192
193 // general functions
194 void adlib_init(Bit32u samplerate);
195 void adlib_write(Bitu idx, Bit8u val);
196 void adlib_getsample(Bit16s* sndptr, Bits numsamples);
197
198 Bitu adlib_reg_read(Bitu port);
199 void adlib_write_index(Bitu port, Bit8u val);
200
201 static Bit32u generator_add; // should be a chip parameter