File: rwopl3.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (236 lines) | stat: -rw-r--r-- 6,134 bytes parent folder | download | duplicates (2)
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
/* 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */

/*
 * Based on RetroWave OPL3 integration code of DOSBox-X
 */

// Allow forbidden symbols for included library headers
#define FORBIDDEN_SYMBOL_ALLOW_ALL

#include "rwopl3.h"

#include <RetroWaveLib/Board/OPL3.h>
#include <RetroWaveLib/Platform/Linux_SPI.h>
#include <RetroWaveLib/Platform/POSIX_SerialPort.h>
#include <RetroWaveLib/Platform/Win32_SerialPort.h>

#include "common/config-manager.h"
#include "common/debug.h"
#include "common/tokenizer.h"

namespace OPL {
namespace RetroWaveOPL3 {

OPL::OPL(Config::OplType type) : _type(type), _activeReg(0), _initialized(false), _connType(RWCONNTYPE_POSIX_SERIAL), _useBuffer(true) {
	_rwMutex = new Common::Mutex();
	_retrowaveGlobalContext = { nullptr, nullptr, nullptr, 0, 0, 0 };
}

OPL::~OPL() {
	_rwMutex->lock();

	if (_initialized) {
		reset();
		retrowave_deinit(&_retrowaveGlobalContext);

		switch (_connType) {
		case RWCONNTYPE_POSIX_SERIAL:
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
			retrowave_deinit_posix_serialport(&_retrowaveGlobalContext);
#endif
			break;
		case RWCONNTYPE_WIN32_SERIAL:
#ifdef WIN32
			retrowave_deinit_win32_serialport(&_retrowaveGlobalContext);
#endif
			break;
		case RWCONNTYPE_LINUX_SPI:
#if defined(__linux__)
			retrowave_deinit_linux_spi(&_retrowaveGlobalContext);
#endif
			break;
		}

		_initialized = false;
	}

	_rwMutex->unlock();
}

bool OPL::init() {
	Common::String bus = ConfMan.get("retrowaveopl3_bus");
	Common::String port = ConfMan.get("retrowaveopl3_port");
	Common::String spiCs = ConfMan.get("retrowaveopl3_spi_cs");
	_useBuffer = ConfMan.hasKey("retrowaveopl3_disable_buffer") ? !ConfMan.getBool("retrowaveopl3_disable_buffer") : true;

	int rc = -1;

	_rwMutex->lock();

	if (bus == "serial") {
		if (port.empty()) {
			warning("RWOPL3: Missing port specification.");
		} else {
#if defined(__linux__) || defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
			char buf[128];
			snprintf(buf, sizeof(buf) - 1, "/dev/%s", port.c_str());

			rc = retrowave_init_posix_serialport(&_retrowaveGlobalContext, buf);
			_connType = RWCONNTYPE_POSIX_SERIAL;
#endif

#ifdef WIN32
			rc = retrowave_init_win32_serialport(&_retrowaveGlobalContext, port.c_str());
			_connType = RWCONNTYPE_WIN32_SERIAL;
#endif
		}
	} else if (bus == "spi") {
#if defined(__linux__)
		Common::StringTokenizer *st = new Common::StringTokenizer(spiCs, ",");
		Common::String gpiochip = st->nextToken();
		Common::String line = st->nextToken();
		int scg[2] = {0};

		if (gpiochip.empty() || line.empty()) {
			warning("RWOPL3: Bad GPIO specification. Please use the 'gpiochip,line' format.");
		} else {
			scg[0] = strtol(gpiochip.c_str(), nullptr, 10);
			scg[1] = strtol(line.c_str(), nullptr, 10);

			debug("RWOPL3: SPI CS: chip=%d, line=%d\n", scg[0], scg[1]);

			rc = retrowave_init_linux_spi(&_retrowaveGlobalContext, port.c_str(), scg[0], scg[1]);
			_connType = RWCONNTYPE_LINUX_SPI;
		}
#else
		warning("RWOPL3: SPI is not supported on your platform.");
#endif
	} else {
		warning("RWOPL3: Bad bus specification. Valid values are \"serial\" and \"spi\".");
	}

	if (rc < 0) {
		warning("RWOPL3: Failed to init board - init returned %d.", rc);
	} else {
		retrowave_io_init(&_retrowaveGlobalContext);
		_initialized = true;

		initDualOpl2OnOpl3(_type);
	}

	_rwMutex->unlock();

	return rc >= 0;
}

void OPL::reset() {
	_rwMutex->lock();

	if (_initialized) {
		retrowave_opl3_reset(&_retrowaveGlobalContext);

		writeReg(0x01, 0, true);
		writeReg(0x02, 0, true);
		writeReg(0x03, 0, true);
		writeReg(0x04, 0x60, true);
		writeReg(0x04, 0x80, true);
		writeReg(0x104, 0, true);
		writeReg(0x105, 0, true);
		writeReg(0x08, 0, true);

		for (int offset = 0; offset <= 0x100; offset += 0x100) {
			for (int reg = 0x20; reg <= 0xF5; reg++) {
				writeReg(offset + reg, 0, true);
			}
		}

		initDualOpl2OnOpl3(_type);
	}

	_rwMutex->unlock();
}

void OPL::write(int portAddress, int value) {
	if (portAddress & 1) {
		writeReg(_activeReg, value);
	} else {
		if (_type == Config::kOpl2) {
			_activeReg = value & 0xFF;
		} else {
			_activeReg = ((portAddress & 2) << 7) | value;
		}
	}
}

void OPL::writeReg(int reg, int value) {
	if (emulateDualOpl2OnOpl3(reg, value, _type)) {
		writeReg(reg, value, false);
	}
}

void OPL::writeReg(int reg, int value, bool forcePort) {
	int portReg = reg & 0xFF;
	value &= 0xFF;

	_rwMutex->lock();

	if (_initialized) {
		if (reg & 0x100 && (forcePort || _type != Config::kOpl2)) {
			// Write to the second register set.
			if (_useBuffer) {
				retrowave_opl3_queue_port1(&_retrowaveGlobalContext, portReg, value);
			} else {
				retrowave_opl3_emit_port1(&_retrowaveGlobalContext, portReg, value);
			}
		} else {
			// Write to the first register set.
			if (_useBuffer) {
				retrowave_opl3_queue_port0(&_retrowaveGlobalContext, portReg, value);
			} else {
				retrowave_opl3_emit_port0(&_retrowaveGlobalContext, portReg, value);
			}
		}
	}

	_rwMutex->unlock();
}

void OPL::onTimer() {
	if (_useBuffer) {
		_rwMutex->lock();

		if (_initialized)
			retrowave_flush(&_retrowaveGlobalContext);

		_rwMutex->unlock();
	}

	Audio::RealChip::onTimer();
}

OPL *create(Config::OplType type) {
	return new OPL(type);
}

} // End of namespace RetroWaveOPL3
} // End of namespace OPL