1 //
2 // Copyright (C) 2013-2016 Alexey Khokholov (Nuke.YKT)
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program 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 // GNU General Public License for more details.
13 //
14 //
15 //  Nuked OPL3 emulator.
16 //  Thanks:
17 //      MAME Development Team(Jarek Burczynski, Tatsuyuki Satoh):
18 //          Feedback and Rhythm part calculation information.
19 //      forums.submarine.org.uk(carbon14, opl3):
20 //          Tremolo and phase generator calculation information.
21 //      OPLx decapsulated(Matthew Gambrell, Olli Niemitalo):
22 //          OPL2 ROMs.
23 //
24 // version: 1.7.4
25 //
26 
27 #ifndef OPL_OPL3_H
28 #define OPL_OPL3_H
29 
30 #include <inttypes.h>
31 
32 #define OPL_WRITEBUF_SIZE   1024
33 #define OPL_WRITEBUF_DELAY  2
34 
35 typedef uintptr_t       Bitu;
36 typedef intptr_t        Bits;
37 typedef uint64_t        Bit64u;
38 typedef int64_t         Bit64s;
39 typedef uint32_t        Bit32u;
40 typedef int32_t         Bit32s;
41 typedef uint16_t        Bit16u;
42 typedef int16_t         Bit16s;
43 typedef uint8_t         Bit8u;
44 typedef int8_t          Bit8s;
45 
46 typedef struct _opl3_slot opl3_slot;
47 typedef struct _opl3_channel opl3_channel;
48 typedef struct _opl3_chip opl3_chip;
49 
50 struct _opl3_slot {
51     opl3_channel *channel;
52     opl3_chip *chip;
53     Bit16s out;
54     Bit16s fbmod;
55     Bit16s *mod;
56     Bit16s prout;
57     Bit16s eg_rout;
58     Bit16s eg_out;
59     Bit8u eg_inc;
60     Bit8u eg_gen;
61     Bit8u eg_rate;
62     Bit8u eg_ksl;
63     Bit8u *trem;
64     Bit8u reg_vib;
65     Bit8u reg_type;
66     Bit8u reg_ksr;
67     Bit8u reg_mult;
68     Bit8u reg_ksl;
69     Bit8u reg_tl;
70     Bit8u reg_ar;
71     Bit8u reg_dr;
72     Bit8u reg_sl;
73     Bit8u reg_rr;
74     Bit8u reg_wf;
75     Bit8u key;
76     Bit32u pg_phase;
77     Bit32u timer;
78 };
79 
80 struct _opl3_channel {
81     opl3_slot *slots[2];
82     opl3_channel *pair;
83     opl3_chip *chip;
84     Bit16s *out[4];
85     Bit8u chtype;
86     Bit16u f_num;
87     Bit8u block;
88     Bit8u fb;
89     Bit8u con;
90     Bit8u alg;
91     Bit8u ksv;
92     Bit16u cha, chb;
93 };
94 
95 typedef struct _opl3_writebuf {
96     Bit64u time;
97     Bit16u reg;
98     Bit8u data;
99 } opl3_writebuf;
100 
101 struct _opl3_chip {
102     opl3_channel channel[18];
103     opl3_slot slot[36];
104     Bit16u timer;
105     Bit8u newm;
106     Bit8u nts;
107     Bit8u rhy;
108     Bit8u vibpos;
109     Bit8u vibshift;
110     Bit8u tremolo;
111     Bit8u tremolopos;
112     Bit8u tremoloshift;
113     Bit32u noise;
114     Bit16s zeromod;
115     Bit32s mixbuff[2];
116     //OPL3L
117     Bit32s rateratio;
118     Bit32s samplecnt;
119     Bit16s oldsamples[2];
120     Bit16s samples[2];
121 
122     Bit64u writebuf_samplecnt;
123     Bit32u writebuf_cur;
124     Bit32u writebuf_last;
125     Bit64u writebuf_lasttime;
126     opl3_writebuf writebuf[OPL_WRITEBUF_SIZE];
127 };
128 
129 void OPL3_Generate(opl3_chip *chip, Bit16s *buf);
130 void OPL3_GenerateResampled(opl3_chip *chip, Bit16s *buf);
131 void OPL3_Reset(opl3_chip *chip, Bit32u samplerate);
132 void OPL3_WriteReg(opl3_chip *chip, Bit16u reg, Bit8u v);
133 void OPL3_WriteRegBuffered(opl3_chip *chip, Bit16u reg, Bit8u v);
134 void OPL3_GenerateStream(opl3_chip *chip, Bit16s *sndptr, Bit32u numsamples);
135 #endif
136