1 /*
2  * Copyright (C) 2013-2016 Alexey Khokholov (Nuke.YKT)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  *
18  *  Nuked OPL3 emulator.
19  *  Thanks:
20  *      MAME Development Team(Jarek Burczynski, Tatsuyuki Satoh):
21  *          Feedback and Rhythm part calculation information.
22  *      forums.submarine.org.uk(carbon14, opl3):
23  *          Tremolo and phase generator calculation information.
24  *      OPLx decapsulated(Matthew Gambrell, Olli Niemitalo):
25  *          OPL2 ROMs.
26  *
27  * version: 1.7.4
28  */
29 
30 #ifndef OPL_OPL3_H
31 #define OPL_OPL3_H
32 
33 #include <inttypes.h>
34 #include <stdint.h>
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 
41 #define OPL_WRITEBUF_SIZE   1024
42 #define OPL_WRITEBUF_DELAY  2
43 
44 typedef uintptr_t       Bitu;
45 typedef intptr_t        Bits;
46 typedef uint64_t        Bit64u;
47 typedef int64_t         Bit64s;
48 typedef uint32_t        Bit32u;
49 typedef int32_t         Bit32s;
50 typedef uint16_t        Bit16u;
51 typedef int16_t         Bit16s;
52 typedef uint8_t         Bit8u;
53 typedef int8_t          Bit8s;
54 
55 typedef struct _opl3_slot opl3_slot;
56 typedef struct _opl3_channel opl3_channel;
57 typedef struct _opl3_chip opl3_chip;
58 
59 struct _opl3_slot {
60     opl3_channel *channel;
61     opl3_chip *chip;
62     Bit16s out;
63     Bit16s fbmod;
64     Bit16s *mod;
65     Bit16s prout;
66     Bit16s eg_rout;
67     Bit16s eg_out;
68     Bit8u eg_inc;
69     Bit8u eg_gen;
70     Bit8u eg_rate;
71     Bit8u eg_ksl;
72     Bit8u *trem;
73     Bit8u reg_vib;
74     Bit8u reg_type;
75     Bit8u reg_ksr;
76     Bit8u reg_mult;
77     Bit8u reg_ksl;
78     Bit8u reg_tl;
79     Bit8u reg_ar;
80     Bit8u reg_dr;
81     Bit8u reg_sl;
82     Bit8u reg_rr;
83     Bit8u reg_wf;
84     Bit8u key;
85     Bit32u pg_phase;
86     Bit32u timer;
87 
88     Bit16u maskzero;
89     Bit8u  signpos;
90     Bit8u  phaseshift;
91 };
92 
93 struct _opl3_channel {
94     opl3_slot *slotz[2];/*Don't use "slots" keyword to avoid conflict with Qt applications*/
95     opl3_channel *pair;
96     opl3_chip *chip;
97     Bit16s *out[4];
98     Bit8u chtype;
99     Bit16u f_num;
100     Bit8u block;
101     Bit8u fb;
102     Bit8u con;
103     Bit8u alg;
104     Bit8u ksv;
105     Bit16u cha, chb;
106 };
107 
108 typedef struct _opl3_writebuf {
109     Bit64u time;
110     Bit16u reg;
111     Bit8u data;
112 } opl3_writebuf;
113 
114 struct _opl3_chip {
115     opl3_channel channel[18];
116     opl3_slot chipslot[36];
117     Bit16u timer;
118     Bit8u newm;
119     Bit8u nts;
120     Bit8u rhy;
121     Bit8u vibpos;
122     Bit8u vibshift;
123     Bit8u tremolo;
124     Bit8u tremolopos;
125     Bit8u tremoloshift;
126     Bit32u noise;
127     Bit16s zeromod;
128     Bit32s mixbuff[2];
129     /* OPL3L */
130     Bit32s rateratio;
131     Bit32s samplecnt;
132     Bit16s oldsamples[2];
133     Bit16s samples[2];
134 
135     Bit64u writebuf_samplecnt;
136     Bit32u writebuf_cur;
137     Bit32u writebuf_last;
138     Bit64u writebuf_lasttime;
139     opl3_writebuf writebuf[OPL_WRITEBUF_SIZE];
140 };
141 
142 void OPL3v17_Generate(opl3_chip *chip, Bit16s *buf);
143 void OPL3v17_GenerateResampled(opl3_chip *chip, Bit16s *buf);
144 void OPL3v17_Reset(opl3_chip *chip, Bit32u samplerate);
145 void OPL3v17_WriteReg(opl3_chip *chip, Bit16u reg, Bit8u v);
146 void OPL3v17_WriteRegBuffered(opl3_chip *chip, Bit16u reg, Bit8u v);
147 void OPL3v17_GenerateStream(opl3_chip *chip, Bit16s *sndptr, Bit32u numsamples);
148 void OPL3v17_GenerateStreamMix(opl3_chip *chip, Bit16s *sndptr, Bit32u numsamples);
149 
150 #ifdef __cplusplus
151 }
152 #endif
153 
154 #endif
155