1 //
2 // Copyright (C) 2013-2018 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 //      siliconpr0n.org(John McMaster, digshadow):
24 //          YMF262 and VRC VII decaps and die shots.
25 //
26 // version: 1.8
27 //
28 
29 #ifndef AUDIO_SOFTSYNTH_OPL_NUKED_H
30 #define AUDIO_SOFTSYNTH_OPL_NUKED_H
31 
32 #include "common/scummsys.h"
33 #include "audio/fmopl.h"
34 
35 #ifndef DISABLE_NUKED_OPL
36 
37 #define OPL_WRITEBUF_SIZE   1024
38 #define OPL_WRITEBUF_DELAY  2
39 
40 namespace OPL {
41 namespace NUKED {
42 
43 typedef uint            Bitu;
44 typedef int             Bits;
45 typedef uint64          Bit64u;
46 typedef int64           Bit64s;
47 typedef uint32          Bit32u;
48 typedef int32           Bit32s;
49 typedef uint16          Bit16u;
50 typedef int16           Bit16s;
51 typedef uint8           Bit8u;
52 typedef int8            Bit8s;
53 
54 typedef struct _opl3_slot opl3_slot;
55 typedef struct _opl3_channel opl3_channel;
56 typedef struct _opl3_chip opl3_chip;
57 
58 struct _opl3_slot {
59     opl3_channel *channel;
60     opl3_chip *chip;
61     Bit16s out;
62     Bit16s fbmod;
63     Bit16s *mod;
64     Bit16s prout;
65     Bit16s eg_rout;
66     Bit16s eg_out;
67     Bit8u eg_inc;
68     Bit8u eg_gen;
69     Bit8u eg_rate;
70     Bit8u eg_ksl;
71     Bit8u *trem;
72     Bit8u reg_vib;
73     Bit8u reg_type;
74     Bit8u reg_ksr;
75     Bit8u reg_mult;
76     Bit8u reg_ksl;
77     Bit8u reg_tl;
78     Bit8u reg_ar;
79     Bit8u reg_dr;
80     Bit8u reg_sl;
81     Bit8u reg_rr;
82     Bit8u reg_wf;
83     Bit8u key;
84     Bit32u pg_reset;
85     Bit32u pg_phase;
86     Bit16u pg_phase_out;
87     Bit8u slot_num;
88 };
89 
90 struct _opl3_channel {
91     opl3_slot *slots[2];
92     opl3_channel *pair;
93     opl3_chip *chip;
94     Bit16s *out[4];
95     Bit8u chtype;
96     Bit16u f_num;
97     Bit8u block;
98     Bit8u fb;
99     Bit8u con;
100     Bit8u alg;
101     Bit8u ksv;
102     Bit16u cha, chb;
103     Bit8u ch_num;
104 };
105 
106 typedef struct _opl3_writebuf {
107     Bit64u time;
108     Bit16u reg;
109     Bit8u data;
110 } opl3_writebuf;
111 
112 struct _opl3_chip {
113     opl3_channel channel[18];
114     opl3_slot slot[36];
115     Bit16u timer;
116     Bit64u eg_timer;
117     Bit8u eg_timerrem;
118     Bit8u eg_state;
119     Bit8u eg_add;
120     Bit8u newm;
121     Bit8u nts;
122     Bit8u rhy;
123     Bit8u vibpos;
124     Bit8u vibshift;
125     Bit8u tremolo;
126     Bit8u tremolopos;
127     Bit8u tremoloshift;
128     Bit32u noise;
129     Bit16s zeromod;
130     Bit32s mixbuff[2];
131     Bit8u rm_hh_bit2;
132     Bit8u rm_hh_bit3;
133     Bit8u rm_hh_bit7;
134     Bit8u rm_hh_bit8;
135     Bit8u rm_tc_bit3;
136     Bit8u rm_tc_bit5;
137     //OPL3L
138     Bit32s rateratio;
139     Bit32s samplecnt;
140     Bit16s oldsamples[2];
141     Bit16s samples[2];
142 
143     Bit64u writebuf_samplecnt;
144     Bit32u writebuf_cur;
145     Bit32u writebuf_last;
146     Bit64u writebuf_lasttime;
147     opl3_writebuf writebuf[OPL_WRITEBUF_SIZE];
148 };
149 
150 void OPL3_Generate(opl3_chip *chip, Bit16s *buf);
151 void OPL3_GenerateResampled(opl3_chip *chip, Bit16s *buf);
152 void OPL3_Reset(opl3_chip *chip, Bit32u samplerate);
153 void OPL3_WriteReg(opl3_chip *chip, Bit16u reg, Bit8u v);
154 void OPL3_WriteRegBuffered(opl3_chip *chip, Bit16u reg, Bit8u v);
155 void OPL3_GenerateStream(opl3_chip *chip, Bit16s *sndptr, Bit32u numsamples);
156 
157 class OPL : public ::OPL::EmulatedOPL {
158 private:
159 	Config::OplType _type;
160 	uint _rate;
161 	opl3_chip chip;
162 	uint address[2];
163 	void dualWrite(uint8 index, uint8 reg, uint8 val);
164 
165 public:
166 	OPL(Config::OplType type);
167 	~OPL();
168 
169 	bool init();
170 	void reset();
171 
172 	void write(int a, int v);
173 	byte read(int a);
174 
175 	void writeReg(int r, int v);
176 
isStereo()177 	bool isStereo() const { return true; }
178 
179 protected:
180 	void generateSamples(int16 *buffer, int length);
181 };
182 
183 }
184 }
185 
186 #endif // !DISABLE_NUKED_OPL
187 
188 #endif
189