DOSBox-X
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
src/hardware/opl.h
00001 /*
00002  *  Copyright (C) 2002-2013  The DOSBox Team
00003  *  OPL2/OPL3 emulation library
00004  *
00005  *  This library is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Lesser General Public
00007  *  License as published by the Free Software Foundation; either
00008  *  version 2.1 of the License, or (at your option) any later version.
00009  * 
00010  *  This library is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *  Lesser General Public License for more details.
00014  * 
00015  *  You should have received a copy of the GNU Lesser General Public
00016  *  License along with this library; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00018  */
00019 
00020 
00021 /*
00022  * Originally based on ADLIBEMU.C, an AdLib/OPL2 emulation library by Ken Silverman
00023  * Copyright (C) 1998-2001 Ken Silverman
00024  * Ken Silverman's official web site: "http://www.advsys.net/ken"
00025  */
00026 
00027 
00028 #define fltype double
00029 
00030 /*
00031         define Bits, Bitu, Bit32s, Bit32u, Bit16s, Bit16u, Bit8s, Bit8u here
00032 */
00033 /*
00034 #include <stdint.h>
00035 typedef uintptr_t       Bitu;
00036 typedef intptr_t        Bits;
00037 typedef uint32_t        Bit32u;
00038 typedef int32_t         Bit32s;
00039 typedef uint16_t        Bit16u;
00040 typedef int16_t         Bit16s;
00041 typedef uint8_t         Bit8u;
00042 typedef int8_t          Bit8s;
00043 */
00044 
00045 
00046 /*
00047         define attribution that inlines/forces inlining of a function (optional)
00048 */
00049 #define OPL_INLINE INLINE
00050 
00051 
00052 #undef NUM_CHANNELS
00053 #if defined(OPLTYPE_IS_OPL3)
00054 #define NUM_CHANNELS    18
00055 #else
00056 #define NUM_CHANNELS    9
00057 #endif
00058 
00059 #define MAXOPERATORS    (NUM_CHANNELS*2)
00060 
00061 
00062 #define FL05    ((fltype)0.5)
00063 #define FL2             ((fltype)2.0)
00064 #define PI              ((fltype)3.1415926535897932384626433832795)
00065 
00066 
00067 #define FIXEDPT                 0x10000         // fixed-point calculations using 16+16
00068 #define FIXEDPT_LFO             0x1000000       // fixed-point calculations using 8+24
00069 
00070 #define WAVEPREC                1024            // waveform precision (10 bits)
00071 
00072 #define INTFREQU                ((fltype)(14318180.0 / 288.0))          // clocking of the chip
00073 
00074 
00075 #define OF_TYPE_ATT                     0
00076 #define OF_TYPE_DEC                     1
00077 #define OF_TYPE_REL                     2
00078 #define OF_TYPE_SUS                     3
00079 #define OF_TYPE_SUS_NOKEEP      4
00080 #define OF_TYPE_OFF                     5
00081 
00082 #define ARC_CONTROL                     0x00
00083 #define ARC_TVS_KSR_MUL         0x20
00084 #define ARC_KSL_OUTLEV          0x40
00085 #define ARC_ATTR_DECR           0x60
00086 #define ARC_SUSL_RELR           0x80
00087 #define ARC_FREQ_NUM            0xa0
00088 #define ARC_KON_BNUM            0xb0
00089 #define ARC_PERC_MODE           0xbd
00090 #define ARC_FEEDBACK            0xc0
00091 #define ARC_WAVE_SEL            0xe0
00092 
00093 #define ARC_SECONDSET           0x100   // second operator set for OPL3
00094 
00095 
00096 #define OP_ACT_OFF                      0x00
00097 #define OP_ACT_NORMAL           0x01    // regular channel activated (bitmasked)
00098 #define OP_ACT_PERC                     0x02    // percussion channel activated (bitmasked)
00099 
00100 #define BLOCKBUF_SIZE           512
00101 
00102 
00103 // vibrato constants
00104 #define VIBTAB_SIZE                     8
00105 #define VIBFAC                          70/50000                // no braces, integer mul/div
00106 
00107 // tremolo constants and table
00108 #define TREMTAB_SIZE            53
00109 #define TREM_FREQ                       ((fltype)(3.7))                 // tremolo at 3.7hz
00110 
00111 
00112 /* operator struct definition
00113      For OPL2 all 9 channels consist of two operators each, carrier and modulator.
00114      Channel x has operators x as modulator and operators (9+x) as carrier.
00115      For OPL3 all 18 channels consist either of two operators (2op mode) or four
00116      operators (4op mode) which is determined through register4 of the second
00117      adlib register set.
00118      Only the channels 0,1,2 (first set) and 9,10,11 (second set) can act as
00119      4op channels. The two additional operators for a channel y come from the
00120      2op channel y+3 so the operatorss y, (9+y), y+3, (9+y)+3 make up a 4op
00121      channel.
00122 */
00123 typedef struct operator_struct {
00124         Bit32s cval, lastcval;                  // current output/last output (used for feedback)
00125         Bit32u tcount, wfpos, tinc;             // time (position in waveform) and time increment
00126         fltype amp, step_amp;                   // and amplification (envelope)
00127         fltype vol;                                             // volume
00128         fltype sustain_level;                   // sustain level
00129         Bit32s mfbi;                                    // feedback amount
00130         fltype a0, a1, a2, a3;                  // attack rate function coefficients
00131         fltype decaymul, releasemul;    // decay/release rate functions
00132         Bit32u op_state;                                // current state of operator (attack/decay/sustain/release/off)
00133         Bit32u toff;
00134         Bit32s freq_high;                               // highest three bits of the frequency, used for vibrato calculations
00135         Bit16s* cur_wform;                              // start of selected waveform
00136         Bit32u cur_wmask;                               // mask for selected waveform
00137         Bit32u act_state;                               // activity state (regular, percussion)
00138         bool sus_keep;                                  // keep sustain level when decay finished
00139         bool vibrato,tremolo;                   // vibrato/tremolo enable bits
00140         
00141         // variables used to provide non-continuous envelopes
00142         Bit32u generator_pos;                   // for non-standard sample rates we need to determine how many samples have passed
00143         Bits cur_env_step;                              // current (standardized) sample position
00144         Bits env_step_a,env_step_d,env_step_r;  // number of std samples of one step (for attack/decay/release mode)
00145         Bit8u step_skip_pos_a;                  // position of 8-cyclic step skipping (always 2^x to check against mask)
00146         Bits env_step_skip_a;                   // bitmask that determines if a step is skipped (respective bit is zero then)
00147 
00148 #if defined(OPLTYPE_IS_OPL3)
00149         bool is_4op,is_4op_attached;    // base of a 4op channel/part of a 4op channel
00150         Bit32s left_pan,right_pan;              // opl3 stereo panning amount
00151 #endif
00152 } op_type;
00153 
00154 // per-chip variables
00155 Bitu chip_num;
00156 op_type op[MAXOPERATORS];
00157 
00158 Bits int_samplerate;
00159         
00160 Bit8u status;
00161 Bit32u opl_index;
00162 #if defined(OPLTYPE_IS_OPL3)
00163 Bit8u adlibreg[512];    // adlib register set (including second set)
00164 Bit8u wave_sel[44];             // waveform selection
00165 #else
00166 Bit8u adlibreg[256];    // adlib register set
00167 Bit8u wave_sel[22];             // waveform selection
00168 #endif
00169 
00170 
00171 // vibrato/tremolo increment/counter
00172 Bit32u vibtab_pos;
00173 Bit32u vibtab_add;
00174 Bit32u tremtab_pos;
00175 Bit32u tremtab_add;
00176 
00177 
00178 // enable an operator
00179 void enable_operator(Bitu regbase, op_type* op_pt);
00180 
00181 // functions to change parameters of an operator
00182 void change_frequency(Bitu chanbase, Bitu regbase, op_type* op_pt);
00183 
00184 void change_attackrate(Bitu regbase, op_type* op_pt);
00185 void change_decayrate(Bitu regbase, op_type* op_pt);
00186 void change_releaserate(Bitu regbase, op_type* op_pt);
00187 void change_sustainlevel(Bitu regbase, op_type* op_pt);
00188 void change_waveform(Bitu regbase, op_type* op_pt);
00189 void change_keepsustain(Bitu regbase, op_type* op_pt);
00190 void change_vibrato(Bitu regbase, op_type* op_pt);
00191 void change_feedback(Bitu chanbase, op_type* op_pt);
00192 
00193 // general functions
00194 void adlib_init(Bit32u samplerate);
00195 void adlib_write(Bitu idx, Bit8u val);
00196 void adlib_getsample(Bit16s* sndptr, Bits numsamples);
00197 
00198 Bitu adlib_reg_read(Bitu port);
00199 void adlib_write_index(Bitu port, Bit8u val);
00200 
00201 static Bit32u generator_add;    // should be a chip parameter