1 /*
2  * Copyright (C) 2013-2018 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 GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; 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  *      siliconpr0n.org(John McMaster, digshadow):
27  *          YMF262 and VRC VII decaps and die shots.
28  *
29  * version: 1.8
30  */
31 
32 #ifndef OPL_OPL3_H
33 #define OPL_OPL3_H
34 
35 #include <inttypes.h>
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
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_reset;
86     Bit32u pg_phase;
87     Bit16u pg_phase_out;
88     Bit8u slot_num;
89 };
90 
91 struct _opl3_channel {
92     opl3_slot *slotz[2];/*Don't use "slots" keyword to avoid conflict with Qt applications*/
93     opl3_channel *pair;
94     opl3_chip *chip;
95     Bit16s *out[4];
96     Bit8u chtype;
97     Bit16u f_num;
98     Bit8u block;
99     Bit8u fb;
100     Bit8u con;
101     Bit8u alg;
102     Bit8u ksv;
103     Bit16u cha, chb;
104     Bit8u ch_num;
105 };
106 
107 typedef struct _opl3_writebuf {
108     Bit64u time;
109     Bit16u reg;
110     Bit8u data;
111 } opl3_writebuf;
112 
113 struct _opl3_chip {
114     opl3_channel channel[18];
115     opl3_slot slot[36];
116     Bit16u timer;
117     Bit64u eg_timer;
118     Bit8u eg_timerrem;
119     Bit8u eg_state;
120     Bit8u eg_add;
121     Bit8u newm;
122     Bit8u nts;
123     Bit8u rhy;
124     Bit8u vibpos;
125     Bit8u vibshift;
126     Bit8u tremolo;
127     Bit8u tremolopos;
128     Bit8u tremoloshift;
129     Bit32u noise;
130     Bit16s zeromod;
131     Bit32s mixbuff[2];
132     Bit8u rm_hh_bit2;
133     Bit8u rm_hh_bit3;
134     Bit8u rm_hh_bit7;
135     Bit8u rm_hh_bit8;
136     Bit8u rm_tc_bit3;
137     Bit8u rm_tc_bit5;
138     /* OPL3L */
139     Bit32s rateratio;
140     Bit32s samplecnt;
141     Bit16s oldsamples[2];
142     Bit16s samples[2];
143 
144     Bit64u writebuf_samplecnt;
145     Bit32u writebuf_cur;
146     Bit32u writebuf_last;
147     Bit64u writebuf_lasttime;
148     opl3_writebuf writebuf[OPL_WRITEBUF_SIZE];
149 };
150 
151 void OPL3_Generate(opl3_chip *chip, Bit16s *buf);
152 void OPL3_GenerateResampled(opl3_chip *chip, Bit16s *buf);
153 void OPL3_Reset(opl3_chip *chip, Bit32u samplerate);
154 void OPL3_WriteReg(opl3_chip *chip, Bit16u reg, Bit8u v);
155 void OPL3_WriteRegBuffered(opl3_chip *chip, Bit16u reg, Bit8u v);
156 void OPL3_GenerateStream(opl3_chip *chip, Bit16s *sndptr, Bit32u numsamples);
157 void OPL3_GenerateStreamMix(opl3_chip *chip, Bit16s *sndptr, Bit32u numsamples);
158 
159 #ifdef __cplusplus
160 }
161 #endif
162 
163 #endif
164