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 #include <stdint.h>
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 
43 #define OPL_WRITEBUF_SIZE   1024
44 #define OPL_WRITEBUF_DELAY  2
45 #define OPL_FAST_WAVEGEN    1 /* optimized waveform generation */
46 
47 typedef uintptr_t       Bitu;
48 typedef intptr_t        Bits;
49 typedef uint64_t        Bit64u;
50 typedef int64_t         Bit64s;
51 typedef uint32_t        Bit32u;
52 typedef int32_t         Bit32s;
53 typedef uint16_t        Bit16u;
54 typedef int16_t         Bit16s;
55 typedef uint8_t         Bit8u;
56 typedef int8_t          Bit8s;
57 
58 typedef struct _opl3_slot opl3_slot;
59 typedef struct _opl3_channel opl3_channel;
60 typedef struct _opl3_chip opl3_chip;
61 
62 struct _opl3_slot {
63     opl3_channel *channel;
64     opl3_chip *chip;
65     Bit16s out;
66     Bit16s fbmod;
67     Bit16s *mod;
68     Bit16s prout;
69     Bit16s eg_rout;
70     Bit16s eg_out;
71     Bit8u eg_inc;
72     Bit8u eg_gen;
73     Bit8u eg_rate;
74     Bit8u eg_ksl;
75     Bit8u *trem;
76     Bit8u reg_vib;
77     Bit8u reg_type;
78     Bit8u reg_ksr;
79     Bit8u reg_mult;
80     Bit8u reg_ksl;
81     Bit8u reg_tl;
82     Bit8u reg_ar;
83     Bit8u reg_dr;
84     Bit8u reg_sl;
85     Bit8u reg_rr;
86     Bit8u reg_wf;
87     Bit8u key;
88     Bit32u pg_reset;
89     Bit32u pg_phase;
90     Bit16u pg_phase_out;
91     Bit8u slot_num;
92 
93 #if OPL_FAST_WAVEGEN
94     Bit16u maskzero;
95     Bit8u  signpos;
96     Bit8u  phaseshift;
97 #endif
98 };
99 
100 struct _opl3_channel {
101     opl3_slot *slotz[2];/*Don't use "slots" keyword to avoid conflict with Qt applications*/
102     opl3_channel *pair;
103     opl3_chip *chip;
104     Bit16s *out[4];
105     Bit8u chtype;
106     Bit16u f_num;
107     Bit8u block;
108     Bit8u fb;
109     Bit8u con;
110     Bit8u alg;
111     Bit8u ksv;
112     Bit16u cha, chb;
113     Bit16u chl, chr;
114     Bit8u ch_num;
115 };
116 
117 typedef struct _opl3_writebuf {
118     Bit64u time;
119     Bit16u reg;
120     Bit8u data;
121 } opl3_writebuf;
122 
123 struct _opl3_chip {
124     opl3_channel channel[18];
125     opl3_slot slot[36];
126     Bit16u timer;
127     Bit64u eg_timer;
128     Bit8u eg_timerrem;
129     Bit8u eg_state;
130     Bit8u eg_add;
131     Bit8u newm;
132     Bit8u nts;
133     Bit8u rhy;
134     Bit8u vibpos;
135     Bit8u vibshift;
136     Bit8u tremolo;
137     Bit8u tremolopos;
138     Bit8u tremoloshift;
139     Bit32u noise;
140     Bit16s zeromod;
141     Bit32s mixbuff[2];
142     Bit8u rm_hh_bit2;
143     Bit8u rm_hh_bit3;
144     Bit8u rm_hh_bit7;
145     Bit8u rm_hh_bit8;
146     Bit8u rm_tc_bit3;
147     Bit8u rm_tc_bit5;
148     /* OPL3L */
149     Bit32s rateratio;
150     Bit32s samplecnt;
151     Bit16s oldsamples[2];
152     Bit16s samples[2];
153 
154     Bit64u writebuf_samplecnt;
155     Bit32u writebuf_cur;
156     Bit32u writebuf_last;
157     Bit64u writebuf_lasttime;
158     opl3_writebuf writebuf[OPL_WRITEBUF_SIZE];
159 };
160 
161 void OPL3_Generate(opl3_chip *chip, Bit16s *buf);
162 void OPL3_GenerateResampled(opl3_chip *chip, Bit16s *buf);
163 void OPL3_Reset(opl3_chip *chip, Bit32u samplerate);
164 void OPL3_WriteReg(opl3_chip *chip, Bit16u reg, Bit8u v);
165 void OPL3_WriteRegBuffered(opl3_chip *chip, Bit16u reg, Bit8u v);
166 void OPL3_WritePan(opl3_chip *chip, Bit16u reg, Bit8u v);
167 void OPL3_GenerateStream(opl3_chip *chip, Bit16s *sndptr, Bit32u numsamples);
168 void OPL3_GenerateStreamMix(opl3_chip *chip, Bit16s *sndptr, Bit32u numsamples);
169 
170 #ifdef __cplusplus
171 }
172 #endif
173 
174 #endif
175