ScummVM API documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
nuked.h
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 #ifndef OPL_ENABLE_STEREOEXT
38 #define OPL_ENABLE_STEREOEXT 0
39 #endif
40 
41 #define OPL_WRITEBUF_SIZE 1024
42 #define OPL_WRITEBUF_DELAY 2
43 
44 namespace OPL {
45 namespace NUKED {
46 
47 typedef struct _opl3_slot opl3_slot;
48 typedef struct _opl3_channel opl3_channel;
49 typedef struct _opl3_chip opl3_chip;
50 
51 struct _opl3_slot {
52  opl3_channel *channel;
53  opl3_chip *chip;
54  int16_t out;
55  int16_t fbmod;
56  int16_t *mod;
57  int16_t prout;
58  uint16_t eg_rout;
59  uint16_t eg_out;
60  uint8_t eg_inc;
61  uint8_t eg_gen;
62  uint8_t eg_rate;
63  uint8_t eg_ksl;
64  uint8_t *trem;
65  uint8_t reg_vib;
66  uint8_t reg_type;
67  uint8_t reg_ksr;
68  uint8_t reg_mult;
69  uint8_t reg_ksl;
70  uint8_t reg_tl;
71  uint8_t reg_ar;
72  uint8_t reg_dr;
73  uint8_t reg_sl;
74  uint8_t reg_rr;
75  uint8_t reg_wf;
76  uint8_t key;
77  uint32_t pg_reset;
78  uint32_t pg_phase;
79  uint16_t pg_phase_out;
80  uint8_t slot_num;
81 };
82 
83 struct _opl3_channel {
84  opl3_slot *slotz[2];/*Don't use "slots" keyword to avoid conflict with Qt applications*/
85  opl3_channel *pair;
86  opl3_chip *chip;
87  int16_t *out[4];
88 
89 #if OPL_ENABLE_STEREOEXT
90  int32_t leftpan;
91  int32_t rightpan;
92 #endif
93 
94  uint8_t chtype;
95  uint16_t f_num;
96  uint8_t block;
97  uint8_t fb;
98  uint8_t con;
99  uint8_t alg;
100  uint8_t ksv;
101  uint16_t cha, chb;
102  uint16_t chc, chd;
103  uint8_t ch_num;
104 };
105 
106 typedef struct _opl3_writebuf {
107  uint64_t time;
108  uint16_t reg;
109  uint8_t data;
110 } opl3_writebuf;
111 
112 struct _opl3_chip {
113  opl3_channel channel[18];
114  opl3_slot slot[36];
115  uint16_t timer;
116  uint64_t eg_timer;
117  uint8_t eg_timerrem;
118  uint8_t eg_state;
119  uint8_t eg_add;
120  uint8_t eg_timer_lo;
121  uint8_t newm;
122  uint8_t nts;
123  uint8_t rhy;
124  uint8_t vibpos;
125  uint8_t vibshift;
126  uint8_t tremolo;
127  uint8_t tremolopos;
128  uint8_t tremoloshift;
129  uint32_t noise;
130  int16_t zeromod;
131  int32_t mixbuff[4];
132  uint8_t rm_hh_bit2;
133  uint8_t rm_hh_bit3;
134  uint8_t rm_hh_bit7;
135  uint8_t rm_hh_bit8;
136  uint8_t rm_tc_bit3;
137  uint8_t rm_tc_bit5;
138 
139 #if OPL_ENABLE_STEREOEXT
140  uint8_t stereoext;
141 #endif
142 
143  /* OPL3L */
144  int32_t rateratio;
145  int32_t samplecnt;
146  int16_t oldsamples[4];
147  int16_t samples[4];
148 
149  uint64_t writebuf_samplecnt;
150  uint32_t writebuf_cur;
151  uint32_t writebuf_last;
152  uint64_t writebuf_lasttime;
153  opl3_writebuf writebuf[OPL_WRITEBUF_SIZE];
154 };
155 
156 void OPL3_Generate(opl3_chip *chip, int16_t *buf);
157 void OPL3_GenerateResampled(opl3_chip *chip, int16_t *buf);
158 void OPL3_Reset(opl3_chip *chip, uint32_t samplerate);
159 void OPL3_WriteReg(opl3_chip *chip, uint16_t reg, uint8_t v);
160 void OPL3_WriteRegBuffered(opl3_chip *chip, uint16_t reg, uint8_t v);
161 void OPL3_GenerateStream(opl3_chip *chip, int16_t *sndptr, uint32_t numsamples);
162 
163 void OPL3_Generate4Ch(opl3_chip *chip, int16_t *buf4);
164 void OPL3_Generate4ChResampled(opl3_chip *chip, int16_t *buf4);
165 void OPL3_Generate4ChStream(opl3_chip *chip, int16_t *sndptr1, int16_t *sndptr2, uint32_t numsamples);
166 
167 class OPL : public ::OPL::OPL, public Audio::EmulatedChip {
168 private:
169  Config::OplType _type;
170  uint _rate;
171  opl3_chip chip;
172  uint address[2];
173  void dualWrite(uint8 index, uint8 reg, uint8 val);
174 
175 public:
176  OPL(Config::OplType type);
177  ~OPL();
178 
179  bool init();
180  void reset();
181 
182  void write(int a, int v);
183 
184  void writeReg(int r, int v);
185 
186  bool isStereo() const { return true; }
187 
188 protected:
189  void generateSamples(int16 *buffer, int length);
190 };
191 
192 }
193 }
194 
195 #endif // !DISABLE_NUKED_OPL
196 
197 #endif
Definition: nuked.h:51
Definition: nuked.h:112
bool isStereo() const
Definition: nuked.h:186
OplType
Definition: fmopl.h:58
Definition: nuked.h:83
Definition: chip.h:112
Definition: fmopl.h:35
Definition: nuked.h:106
Definition: fmopl.h:115