1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
/*
* Based on AdLib emulation code of DOSBox
* Copyright (C) 2002-2009 The DOSBox Team
* Licensed under GPLv2+
* http://www.dosbox.com
*/
#ifndef DISABLE_DOSBOX_OPL
#include "dosbox.h"
#include "dbopl.h"
#include "audio/mixer.h"
#include "common/system.h"
#include "common/scummsys.h"
#include "common/util.h"
#include <math.h>
#include <string.h>
namespace OPL {
namespace DOSBox {
Timer::Timer() {
masked = false;
overflow = false;
enabled = false;
counter = 0;
delay = 0;
}
void Timer::update(double time) {
if (!enabled || !delay)
return;
double deltaStart = time - startTime;
// Only set the overflow flag when not masked
if (deltaStart >= 0 && !masked)
overflow = 1;
}
void Timer::reset(double time) {
overflow = false;
if (!delay || !enabled)
return;
double delta = (time - startTime);
double rem = fmod(delta, delay);
double next = delay - rem;
startTime = time + next;
}
void Timer::stop() {
enabled = false;
}
void Timer::start(double time, int scale) {
//Don't enable again
if (enabled)
return;
enabled = true;
delay = 0.001 * (256 - counter) * scale;
startTime = time + delay;
}
bool Chip::write(uint32 reg, uint8 val) {
switch (reg) {
case 0x02:
timer[0].counter = val;
return true;
case 0x03:
timer[1].counter = val;
return true;
case 0x04:
{
double time = g_system->getMillis() / 1000.0;
if (val & 0x80) {
timer[0].reset(time);
timer[1].reset(time);
} else {
timer[0].update(time);
timer[1].update(time);
if (val & 0x1)
timer[0].start(time, 80);
else
timer[0].stop();
timer[0].masked = (val & 0x40) > 0;
if (timer[0].masked)
timer[0].overflow = false;
if (val & 0x2)
timer[1].start(time, 320);
else
timer[1].stop();
timer[1].masked = (val & 0x20) > 0;
if (timer[1].masked)
timer[1].overflow = false;
}
}
return true;
default:
break;
}
return false;
}
uint8 Chip::read() {
double time = g_system->getMillis() / 1000.0;
timer[0].update(time);
timer[1].update(time);
uint8 ret = 0;
// Overflow won't be set if a channel is masked
if (timer[0].overflow) {
ret |= 0x40;
ret |= 0x80;
}
if (timer[1].overflow) {
ret |= 0x20;
ret |= 0x80;
}
return ret;
}
OPL::OPL(Config::OplType type) : _type(type), _rate(0), _emulator(0) {
}
OPL::~OPL() {
stop();
free();
}
void OPL::free() {
delete _emulator;
_emulator = 0;
}
bool OPL::init() {
free();
memset(&_reg, 0, sizeof(_reg));
memset(_chip, 0, sizeof(_chip));
_emulator = new DBOPL::Chip();
if (!_emulator)
return false;
DBOPL::InitTables();
_rate = g_system->getMixer()->getOutputRate();
_emulator->Setup(_rate);
if (_type == Config::kDualOpl2) {
// Setup opl3 mode in the hander
_emulator->WriteReg(0x105, 1);
}
return true;
}
void OPL::reset() {
init();
}
void OPL::write(int port, int val) {
if (port&1) {
switch (_type) {
case Config::kOpl2:
case Config::kOpl3:
if (!_chip[0].write(_reg.normal, val))
_emulator->WriteReg(_reg.normal, val);
break;
case Config::kDualOpl2:
// Not a 0x??8 port, then write to a specific port
if (!(port & 0x8)) {
byte index = (port & 2) >> 1;
dualWrite(index, _reg.dual[index], val);
} else {
//Write to both ports
dualWrite(0, _reg.dual[0], val);
dualWrite(1, _reg.dual[1], val);
}
break;
default:
break;
}
} else {
// Ask the handler to write the address
// Make sure to clip them in the right range
switch (_type) {
case Config::kOpl2:
_reg.normal = _emulator->WriteAddr(port, val) & 0xff;
break;
case Config::kOpl3:
_reg.normal = _emulator->WriteAddr(port, val) & 0x1ff;
break;
case Config::kDualOpl2:
// Not a 0x?88 port, when write to a specific side
if (!(port & 0x8)) {
byte index = (port & 2) >> 1;
_reg.dual[index] = val & 0xff;
} else {
_reg.dual[0] = val & 0xff;
_reg.dual[1] = val & 0xff;
}
break;
default:
break;
}
}
}
byte OPL::read(int port) {
switch (_type) {
case Config::kOpl2:
if (!(port & 1))
//Make sure the low bits are 6 on opl2
return _chip[0].read() | 0x6;
break;
case Config::kOpl3:
if (!(port & 1))
return _chip[0].read();
break;
case Config::kDualOpl2:
// Only return for the lower ports
if (port & 1)
return 0xff;
// Make sure the low bits are 6 on opl2
return _chip[(port >> 1) & 1].read() | 0x6;
default:
break;
}
return 0;
}
void OPL::writeReg(int r, int v) {
int tempReg = 0;
switch (_type) {
case Config::kOpl2:
case Config::kDualOpl2:
case Config::kOpl3:
// We can't use _handler->writeReg here directly, since it would miss timer changes.
// Backup old setup register
tempReg = _reg.normal;
// We directly allow writing to secondary OPL3 registers by using
// register values >= 0x100.
if (_type == Config::kOpl3 && r >= 0x100) {
// We need to set the register we want to write to via port 0x222,
// since we want to write to the secondary register set.
write(0x222, r);
// Do the real writing to the register
write(0x223, v);
} else {
// We need to set the register we want to write to via port 0x388
write(0x388, r);
// Do the real writing to the register
write(0x389, v);
}
// Restore the old register
if (_type == Config::kOpl3 && tempReg >= 0x100) {
write(0x222, tempReg & ~0x100);
} else {
write(0x388, tempReg);
}
break;
default:
break;
};
}
void OPL::dualWrite(uint8 index, uint8 reg, uint8 val) {
// Make sure you don't use opl3 features
// Don't allow write to disable opl3
if (reg == 5)
return;
// Only allow 4 waveforms
if (reg >= 0xE0 && reg <= 0xE8)
val &= 3;
// Write to the timer?
if (_chip[index].write(reg, val))
return;
// Enabling panning
if (reg >= 0xC0 && reg <= 0xC8) {
val &= 15;
val |= index ? 0xA0 : 0x50;
}
uint32 fullReg = reg + (index ? 0x100 : 0);
_emulator->WriteReg(fullReg, val);
}
void OPL::generateSamples(int16 *buffer, int length) {
// For stereo OPL cards, we divide the sample count by 2,
// to match stereo AudioStream behavior.
if (_type != Config::kOpl2)
length >>= 1;
const uint bufferLength = 512;
int32 tempBuffer[bufferLength * 2];
if (_emulator->opl3Active) {
while (length > 0) {
const uint readSamples = MIN<uint>(length, bufferLength);
_emulator->GenerateBlock3(readSamples, tempBuffer);
for (uint i = 0; i < (readSamples << 1); ++i)
buffer[i] = tempBuffer[i];
buffer += (readSamples << 1);
length -= readSamples;
}
} else {
while (length > 0) {
const uint readSamples = MIN<uint>(length, bufferLength << 1);
_emulator->GenerateBlock2(readSamples, tempBuffer);
for (uint i = 0; i < readSamples; ++i)
buffer[i] = tempBuffer[i];
buffer += readSamples;
length -= readSamples;
}
}
}
} // End of namespace DOSBox
} // End of namespace OPL
#endif // !DISABLE_DOSBOX_ADLIB
|