aboutsummaryrefslogtreecommitdiff
path: root/audio/opl2lpt.cpp
blob: 3b30b605014a5e29d06e65994c8d0130fa614031 (plain)
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
/* 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.
 *
 */

/* OPL implementation for OPL2LPT and OPL3LPT through libieee1284.
 */

#define FORBIDDEN_SYMBOL_ALLOW_ALL
#include "common/scummsys.h"

#include "common/config-manager.h"
#include "common/debug.h"
#include "common/str.h"
#include "common/textconsole.h"
#include "audio/fmopl.h"

#include <unistd.h>
#include <ieee1284.h>

static const uint8 OPL2LPTRegisterSelect[] = {
	(C1284_NSELECTIN | C1284_NSTROBE | C1284_NINIT) ^ C1284_INVERTED,
	(C1284_NSELECTIN | C1284_NSTROBE) ^ C1284_INVERTED,
	(C1284_NSELECTIN | C1284_NSTROBE | C1284_NINIT) ^ C1284_INVERTED
};

static const uint8 OPL3LPTRegisterSelect[] = {
	(C1284_NSTROBE | C1284_NINIT) ^ C1284_INVERTED,
	C1284_NSTROBE ^ C1284_INVERTED,
	(C1284_NSTROBE | C1284_NINIT) ^ C1284_INVERTED
};

static const uint8 OPL2LPTRegisterWrite[] = {
	(C1284_NSELECTIN | C1284_NINIT) ^ C1284_INVERTED,
	C1284_NSELECTIN ^ C1284_INVERTED,
	(C1284_NSELECTIN | C1284_NINIT) ^ C1284_INVERTED
};

namespace OPL {
namespace OPL2LPT {

class OPL : public ::OPL::RealOPL {
private:
	struct parport *_pport;
	Config::OplType _type;
	int index;

public:
	explicit OPL(Config::OplType type);
	~OPL();

	bool init();
	void reset();

	void write(int a, int v);
	byte read(int a);

	void writeReg(int r, int v);
};

OPL::OPL(Config::OplType type) : _pport(nullptr), _type(type), index(0) {
}

OPL::~OPL() {
	if (_pport) {
		stop();
		reset();
		ieee1284_close(_pport);
	}
}

bool OPL::init() {
	struct parport_list parports = {};
	const Common::String parportName = ConfMan.get("opl2lpt_parport");

	// Look for available parallel ports
	if (ieee1284_find_ports(&parports, 0) != E1284_OK) {
		return false;
	}
	for (int i = 0; i < parports.portc; i++) {
		if (parportName == "null" ||
		    parportName == parports.portv[i]->name) {
			int caps = CAP1284_RAW;
			_pport = parports.portv[i];
			if (ieee1284_open(_pport, F1284_EXCL, &caps) != E1284_OK) {
				warning("cannot open parallel port %s", _pport->name);
			}
			if (ieee1284_claim(_pport) != E1284_OK) {
				warning("cannot claim parallel port %s", _pport->name);
				ieee1284_close(_pport);
				continue;
			}
			reset();
			// Safe to free ports here, opened ports are refcounted.
			ieee1284_free_ports(&parports);
			return true;
		}
	}
	_pport = nullptr;
	ieee1284_free_ports(&parports);
	return false;
}

void OPL::reset() {
	for(int i = 0; i < 256; i ++) {
		writeReg(i, 0);
	}
	if (_type == Config::kOpl3) {
		for (int i = 0; i < 256; i++) {
			writeReg(i + 256, 0);
		}
	}
	index = 0;
}

void OPL::write(int port, int val) {
	if (port & 1) {
		writeReg(index, val);
	} else {
		switch (_type) {
		case Config::kOpl2:
			index = val & 0xff;
			break;
		case Config::kOpl3:
			index = (val & 0xff) | ((port << 7) & 0x100);
			break;
		default:
			warning("OPL2LPT: unsupported OPL mode %d", _type);
			break;
		}
	}
}

byte OPL::read(int port) {
	// No read support for the OPL2LPT
	return 0;
}

void OPL::writeReg(int r, int v) {
	if (_type == Config::kOpl3) {
		r &= 0x1ff;
	} else {
		r &= 0xff;
	}
	v &= 0xff;

	ieee1284_write_data(_pport, r & 0xff);
	if (r < 0x100) {
		ieee1284_write_control(_pport, OPL2LPTRegisterSelect[0]);
		ieee1284_write_control(_pport, OPL2LPTRegisterSelect[1]);
		ieee1284_write_control(_pport, OPL2LPTRegisterSelect[2]);
	} else {
		ieee1284_write_control(_pport, OPL3LPTRegisterSelect[0]);
		ieee1284_write_control(_pport, OPL3LPTRegisterSelect[1]);
		ieee1284_write_control(_pport, OPL3LPTRegisterSelect[2]);
	}
	usleep(4);		// 3.3 us

	ieee1284_write_data(_pport, v);
	ieee1284_write_control(_pport, OPL2LPTRegisterWrite[0]);
	ieee1284_write_control(_pport, OPL2LPTRegisterWrite[1]);
	ieee1284_write_control(_pport, OPL2LPTRegisterWrite[2]);
	usleep(23);
}

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

} // End of namespace OPL2LPT
} // End of namespace OPL