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 NUKEDOPL_H
31 #define NUKEDOPL_H
32 
33 #include <inttypes.h>
34 
35 #define OPL_WRITEBUF_SIZE   1024
36 #define OPL_WRITEBUF_DELAY  2
37 
38 typedef uintptr_t       Bitu;
39 typedef intptr_t        Bits;
40 typedef uint64_t        Bit64u;
41 typedef int64_t         Bit64s;
42 typedef uint32_t        Bit32u;
43 typedef int32_t         Bit32s;
44 typedef uint16_t        Bit16u;
45 typedef int16_t         Bit16s;
46 typedef uint8_t         Bit8u;
47 typedef int8_t          Bit8s;
48 
49 typedef struct _opl3_slot opl3_slot;
50 typedef struct _opl3_channel opl3_channel;
51 typedef struct _opl3_chip opl3_chip;
52 
53 struct _opl3_slot {
54     opl3_channel *channel;
55     opl3_chip *chip;
56     Bit16s out;
57     Bit16s fbmod;
58     Bit16s *mod;
59     Bit16s prout;
60     Bit16s eg_rout;
61     Bit16s eg_out;
62     Bit8u eg_inc;
63     Bit8u eg_gen;
64     Bit8u eg_rate;
65     Bit8u eg_ksl;
66     Bit8u *trem;
67     Bit8u reg_vib;
68     Bit8u reg_type;
69     Bit8u reg_ksr;
70     Bit8u reg_mult;
71     Bit8u reg_ksl;
72     Bit8u reg_tl;
73     Bit8u reg_ar;
74     Bit8u reg_dr;
75     Bit8u reg_sl;
76     Bit8u reg_rr;
77     Bit8u reg_wf;
78     Bit8u key;
79     Bit32u pg_phase;
80     Bit32u timer;
81 };
82 
83 struct _opl3_channel {
84     opl3_slot *slots[2];
85     opl3_channel *pair;
86     opl3_chip *chip;
87     Bit16s *out[4];
88     Bit8u chtype;
89     Bit16u f_num;
90     Bit8u block;
91     Bit8u fb;
92     Bit8u con;
93     Bit8u alg;
94     Bit8u ksv;
95     Bit16u cha, chb;
96 };
97 
98 typedef struct _opl3_writebuf {
99     Bit64u time;
100     Bit16u reg;
101     Bit8u data;
102 } opl3_writebuf;
103 
104 struct _opl3_chip {
105     opl3_channel channel[18];
106     opl3_slot slot[36];
107     Bit16u timer;
108     Bit8u newm;
109     Bit8u nts;
110     Bit8u rhy;
111     Bit8u vibpos;
112     Bit8u vibshift;
113     Bit8u tremolo;
114     Bit8u tremolopos;
115     Bit8u tremoloshift;
116     Bit32u noise;
117     Bit16s zeromod;
118     Bit32s mixbuff[2];
119     //OPL3L
120     Bit32s rateratio;
121     Bit32s samplecnt;
122     Bit16s oldsamples[2];
123     Bit16s samples[2];
124 
125     Bit64u writebuf_samplecnt;
126     Bit32u writebuf_cur;
127     Bit32u writebuf_last;
128     Bit64u writebuf_lasttime;
129     opl3_writebuf writebuf[OPL_WRITEBUF_SIZE];
130 };
131 
132 void OPL3_Generate(opl3_chip *chip, Bit16s *buf);
133 void OPL3_GenerateResampled(opl3_chip *chip, Bit16s *buf);
134 void OPL3_Reset(opl3_chip *chip, Bit32u samplerate);
135 void OPL3_WriteReg(opl3_chip *chip, Bit16u reg, Bit8u v);
136 void OPL3_WriteRegBuffered(opl3_chip *chip, Bit16u reg, Bit8u v);
137 void OPL3_GenerateStream(opl3_chip *chip, Bit16s *sndptr, Bit32u numsamples);
138 #endif
139