aboutsummaryrefslogtreecommitdiff
path: root/engines/sky/music/adlibchannel.cpp
blob: 87de3cf1b9604204bec5e65509a079d1088b0468 (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
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
/* 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.
 *
 * $URL$
 * $Id$
 *
 */


#include "common/endian.h"
#include "common/util.h"
#include "sky/music/adlibchannel.h"
#include "sky/sky.h"

namespace Sky {

AdlibChannel::AdlibChannel(FM_OPL *opl, uint8 *pMusicData, uint16 startOfData) {
	_opl = opl;
	_musicData = pMusicData;
	_channelData.loopPoint = startOfData;
	_channelData.eventDataPtr = startOfData;
	_channelData.channelActive = true;
	_channelData.tremoVibro = 0;
	_channelData.assignedInstrument = 0xFF;
	_channelData.channelVolume = 0x7F;
	_channelData.nextEventTime = getNextEventTime();

	_channelData.adlibChannelNumber = _channelData.lastCommand = _channelData.note =
		_channelData.adlibReg1 = _channelData.adlibReg2 = _channelData.freqOffset = 0;
	_channelData.frequency = 0;
	_channelData.instrumentData = NULL;

	uint16 instrumentDataLoc;

	if (SkyEngine::_systemVars.gameVersion == 109) {
		//instrumentDataLoc = (_musicData[0x11D0] << 8) | _musicData[0x11CF];
		//_frequenceTable = (uint16*)(_musicData + 0x835);
		//_registerTable = _musicData + 0xE35;
		//_opOutputTable = _musicData + 0xE47;
		//_adlibRegMirror = _musicData + 0xF4A;

		instrumentDataLoc = READ_LE_UINT16(_musicData + 0x1204);
		_frequenceTable = (uint16*)(_musicData + 0x868);
		_registerTable = _musicData + 0xE68;
		_opOutputTable = _musicData + 0xE7A;
		_adlibRegMirror = _musicData + 0xF7D;
	} else if (SkyEngine::_systemVars.gameVersion == 267) {
		instrumentDataLoc = READ_LE_UINT16(_musicData + 0x11FB);
		_frequenceTable = (uint16*)(_musicData + 0x7F4);
		_registerTable = _musicData + 0xDF4;
		_opOutputTable = _musicData + 0xE06;
		_adlibRegMirror = _musicData + 0xF55;
	} else {
		instrumentDataLoc = READ_LE_UINT16(_musicData + 0x1205);
		_frequenceTable = (uint16*)(_musicData + 0x7FE);
		_registerTable = _musicData + 0xDFE;
		_opOutputTable = _musicData + 0xE10;
		_adlibRegMirror = _musicData + 0xF5F;
	}

	_instrumentMap = _musicData+instrumentDataLoc;
	_instruments = (InstrumentStruct*)(_instrumentMap+0x80);
}

AdlibChannel::~AdlibChannel() {
	stopNote();
}

bool AdlibChannel::isActive() {
	return _channelData.channelActive;
}

void AdlibChannel::updateVolume(uint16 pVolume) {
	// Do nothing. The mixer handles the music volume for us.
}

/*	This class uses the same area for the register mirror as the original
	asm driver did (_musicData[0xF5F..0x105E]), so the cache is indeed shared
	by all instances of the class.
*/
void AdlibChannel::setRegister(uint8 regNum, uint8 value) {
	if (_adlibRegMirror[regNum] != value) {
		OPLWriteReg (_opl, regNum, value);
		_adlibRegMirror[regNum] = value;
	}
}

void AdlibChannel::stopNote() {
	if (_channelData.note & 0x20) {
		_channelData.note &= ~0x20;
		setRegister(0xB0 | _channelData.adlibChannelNumber, _channelData.note);
	}
}

int32 AdlibChannel::getNextEventTime() {
	int32 retV = 0;
	uint8 cnt, lVal = 0;
	for (cnt = 0; cnt < 4; cnt++) {
		lVal = _musicData[_channelData.eventDataPtr];
		_channelData.eventDataPtr++;
		retV = (retV << 7) | (lVal & 0x7F);
		if (!(lVal & 0x80))
			break;
	}
	if (lVal & 0x80) {
		return -1; // should never happen
	} else
		return retV;
}

uint8 AdlibChannel::process(uint16 aktTime) {
	if (!_channelData.channelActive) {
		return 0;
	}

	uint8 returnVal = 0;

	_channelData.nextEventTime -= aktTime;
	uint8 opcode;
	while ((_channelData.nextEventTime < 0) && (_channelData.channelActive)) {
		opcode = _musicData[_channelData.eventDataPtr];
		_channelData.eventDataPtr++;
		if (opcode & 0x80) {
			if (opcode == 0xFF) {
				// dummy opcode
			} else if (opcode >= 0x90) {
				switch (opcode&0xF) {
				case 0: com90_caseNoteOff(); break;
				case 1: com90_stopChannel(); break;
				case 2: com90_setupInstrument(); break;
				case 3:
					returnVal = com90_updateTempo();
					break;
				case 5: com90_getFreqOffset(); break;
				case 6: com90_getChannelVolume(); break;
				case 7: com90_getTremoVibro(); break;
				case 8: com90_loopMusic(); break;
				case 9: com90_keyOff(); break;
				case 12: com90_setLoopPoint(); break;

				default:
					error("AdlibChannel: Unknown music opcode 0x%02X", opcode);
					break;
				}
			} else {
				// new adlib channel assignment
				_channelData.adlibChannelNumber = opcode & 0xF;
				_channelData.adlibReg1 = _registerTable[((opcode & 0xF) << 1) | 0];
				_channelData.adlibReg2 = _registerTable[((opcode & 0xF) << 1) | 1];
			}
		} else {
			_channelData.lastCommand = opcode;
			stopNote();
			// not sure why this "if" is necessary...either a bug in my
			// code or a bug in the music data (section 1, music 2)
			if (_channelData.instrumentData || _channelData.tremoVibro) {
				setupInstrument(opcode);

				opcode = _musicData[_channelData.eventDataPtr];
				_channelData.eventDataPtr++;
				setupChannelVolume(opcode);
			} else
				_channelData.eventDataPtr++;
		}
		if (_channelData.channelActive)
			_channelData.nextEventTime += getNextEventTime();
	}
	return returnVal;
}

void AdlibChannel::setupInstrument(uint8 opcode) {
	uint16 nextNote;
	if (_channelData.tremoVibro) {
		uint8 newInstrument = _instrumentMap[opcode];
		if (newInstrument != _channelData.assignedInstrument) {
			_channelData.assignedInstrument = newInstrument;
			_channelData.instrumentData = _instruments + newInstrument;
			adlibSetupInstrument();
		}
		_channelData.lastCommand = _channelData.instrumentData->bindedEffect;
		nextNote = getNextNote(_channelData.lastCommand);
	} else {
		nextNote = getNextNote(opcode - 0x18 + _channelData.instrumentData->bindedEffect);
	}
	_channelData.frequency = nextNote;
	setRegister(0xA0 | _channelData.adlibChannelNumber, (uint8)nextNote);
	setRegister(0xB0 | _channelData.adlibChannelNumber, (uint8)((nextNote >> 8) | 0x20));
	_channelData.note = (uint8)((nextNote >> 8) | 0x20);
}

void AdlibChannel::setupChannelVolume(uint8 volume) {
	uint8 resultOp;
	uint32 resVol = ((volume + 1) * (_channelData.instrumentData->totOutLev_Op2 + 1)) << 1;
	resVol &= 0xFFFF;
	resVol *= (_channelData.channelVolume + 1) << 1;
	resVol >>= 16;
	assert(resVol < 0x81);
	resultOp = ((_channelData.instrumentData->scalingLevel << 6) & 0xC0) | _opOutputTable[resVol];
	setRegister(0x40 | _channelData.adlibReg2, resultOp);
	if (_channelData.instrumentData->feedBack & 1) {
		resVol = ((volume + 1) * (_channelData.instrumentData->totOutLev_Op1 + 1)) << 1;
		resVol &= 0xFFFF;
		resVol *= (_channelData.channelVolume + 1) << 1;
		resVol >>= 16;
	} else
		resVol = _channelData.instrumentData->totOutLev_Op1;
	assert(resVol < 0x81);
	resultOp = ((_channelData.instrumentData->scalingLevel << 2) & 0xC0) | _opOutputTable[resVol];
	setRegister(0x40 | _channelData.adlibReg1, resultOp);
}

void AdlibChannel::adlibSetupInstrument() {
	setRegister(0x60 | _channelData.adlibReg1, _channelData.instrumentData->ad_Op1);
	setRegister(0x60 | _channelData.adlibReg2, _channelData.instrumentData->ad_Op2);
	setRegister(0x80 | _channelData.adlibReg1, _channelData.instrumentData->sr_Op1);
	setRegister(0x80 | _channelData.adlibReg2, _channelData.instrumentData->sr_Op2);
	setRegister(0xE0 | _channelData.adlibReg1, _channelData.instrumentData->waveSelect_Op1);
	setRegister(0xE0 | _channelData.adlibReg2, _channelData.instrumentData->waveSelect_Op2);
	setRegister(0xC0 | _channelData.adlibChannelNumber, _channelData.instrumentData->feedBack);
	setRegister(0x20 | _channelData.adlibReg1, _channelData.instrumentData->ampMod_Op1);
	setRegister(0x20 | _channelData.adlibReg2, _channelData.instrumentData->ampMod_Op2);
}

uint16 AdlibChannel::getNextNote(uint8 param) {
	int16 freqIndex = ((int16)_channelData.freqOffset) - 0x40;
	if (freqIndex >= 0x3F)
		freqIndex++;
	freqIndex *= 2;
	freqIndex += param << 6;
	uint16 freqData = FROM_LE_16(_frequenceTable[freqIndex % 0x300]);
	if ((freqIndex % 0x300 >= 0x1C0) || (freqIndex / 0x300 > 0)) {
		return (((freqIndex / 0x300) - 1) << 10) + (freqData & 0x7FF);
	} else {
		// looks like a bug. dunno why. It's what the ASM code says.
		return (uint16)(((int16)freqData) >> 1);
	}
}

//- command 90h routines

void AdlibChannel::com90_caseNoteOff() {
	if (_musicData[_channelData.eventDataPtr] == _channelData.lastCommand)
		stopNote();
	_channelData.eventDataPtr++;
}

void AdlibChannel::com90_stopChannel() {
	stopNote();
	_channelData.channelActive = false;
}

void AdlibChannel::com90_setupInstrument() {
	_channelData.channelVolume = 0x7F;
	_channelData.freqOffset = 0x40;
	_channelData.assignedInstrument = _musicData[_channelData.eventDataPtr];
	_channelData.eventDataPtr++;
	_channelData.instrumentData = _instruments + _channelData.assignedInstrument;
	adlibSetupInstrument();
}

uint8 AdlibChannel::com90_updateTempo() {
	return _musicData[_channelData.eventDataPtr++];
}

void AdlibChannel::com90_getFreqOffset() {
	_channelData.freqOffset = _musicData[_channelData.eventDataPtr++];
	if (_channelData.note & 0x20) {
		uint16 nextNote = getNextNote(
			_channelData.lastCommand - 0x18 + _channelData.instrumentData->bindedEffect);
		setRegister(0xA0 | _channelData.adlibChannelNumber, (uint8)nextNote);
		setRegister(0xB0 | _channelData.adlibChannelNumber, (uint8)((nextNote >> 8) | 0x20));
		_channelData.note = (uint8)(nextNote >> 8) | 0x20;
	}
}

void AdlibChannel::com90_getChannelVolume() {
	_channelData.channelVolume = _musicData[_channelData.eventDataPtr++];
}

void AdlibChannel::com90_getTremoVibro() {
	_channelData.tremoVibro = _musicData[_channelData.eventDataPtr++];
}

void AdlibChannel::com90_loopMusic() {
	_channelData.eventDataPtr = _channelData.loopPoint;
}

void AdlibChannel::com90_keyOff() {
	stopNote();
}

void AdlibChannel::com90_setLoopPoint() {
	_channelData.loopPoint = _channelData.eventDataPtr;
}

} // End of namespace Sky