| 1 |
// Emacs style mode select -*- C -*- |
| 2 |
//----------------------------------------------------------------------------- |
| 3 |
// |
| 4 |
// Copyright(C) 2009 Simon Howard |
| 5 |
// |
| 6 |
// This program is free software; you can redistribute it and/or |
| 7 |
// modify it under the terms of the GNU General Public License |
| 8 |
// as published by the Free Software Foundation; either version 2 |
| 9 |
// of the License, or (at your option) any later version. |
| 10 |
// |
| 11 |
// This program is distributed in the hope that it will be useful, |
| 12 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
// GNU General Public License for more details. |
| 15 |
// |
| 16 |
// You should have received a copy of the GNU General Public License |
| 17 |
// along with this program; if not, write to the Free Software |
| 18 |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 19 |
// 02111-1307, USA. |
| 20 |
// |
| 21 |
// DESCRIPTION: |
| 22 |
// OPL interface. |
| 23 |
// |
| 24 |
//----------------------------------------------------------------------------- |
| 25 |
|
| 26 |
#include "config.h" |
| 27 |
|
| 28 |
#include <stdio.h> |
| 29 |
#include <stdlib.h> |
| 30 |
|
| 31 |
#include "opl3.h" |
| 32 |
#include "opl.h" |
| 33 |
#include "opl_queue.h" |
| 34 |
|
| 35 |
#include "dsda/configuration.h" |
| 36 |
|
| 37 |
static int init_stage_reg_writes = 1; |
| 38 |
|
| 39 |
unsigned int opl_sample_rate = 22050; |
| 40 |
|
| 41 |
#define MAX_SOUND_SLICE_TIME 100 /* ms */ |
| 42 |
|
| 43 |
typedef struct |
| 44 |
{ |
| 45 |
unsigned int rate; // Number of times the timer is advanced per sec. |
| 46 |
unsigned int enabled; // Non-zero if timer is enabled. |
| 47 |
unsigned int value; // Last value that was set. |
| 48 |
uint64_t expire_time; // Calculated time that timer will expire. |
| 49 |
} opl_timer_t; |
| 50 |
|
| 51 |
// Queue of callbacks waiting to be invoked. |
| 52 |
static opl_callback_queue_t *callback_queue; |
| 53 |
|
| 54 |
// Current time, in us since startup: |
| 55 |
static uint64_t current_time; |
| 56 |
|
| 57 |
// If non-zero, playback is currently paused. |
| 58 |
static int opl_paused; |
| 59 |
|
| 60 |
// Time offset (in us) due to the fact that callbacks |
| 61 |
// were previously paused. |
| 62 |
static uint64_t pause_offset; |
| 63 |
|
| 64 |
// OPL software emulator structure. |
| 65 |
static opl3_chip opl_chip; |
| 66 |
static int opl_opl3mode; |
| 67 |
|
| 68 |
// Register number that was written. |
| 69 |
static int register_num = 0; |
| 70 |
|
| 71 |
// Timers; DBOPL does not do timer stuff itself. |
| 72 |
static opl_timer_t timer1 = { 12500, 0, 0, 0 }; |
| 73 |
static opl_timer_t timer2 = { 3125, 0, 0, 0 }; |
| 74 |
|
| 75 |
// Initialize the OPL library. Returns true if initialized |
| 76 |
// successfully. |
| 77 |
int OPL_Init (unsigned int rate) |
| 78 |
{ |
| 79 |
opl_opl3mode = dsda_IntConfig(dsda_config_mus_opl_opl3mode); |
| 80 |
|
| 81 |
opl_sample_rate = rate; |
| 82 |
opl_paused = 0; |
| 83 |
pause_offset = 0; |
| 84 |
|
| 85 |
// Queue structure of callbacks to invoke. |
| 86 |
|
| 87 |
callback_queue = OPL_Queue_Create(); |
| 88 |
current_time = 0; |
| 89 |
|
| 90 |
// Create the emulator structure: |
| 91 |
OPL3_Reset(&opl_chip, opl_sample_rate); |
| 92 |
|
| 93 |
OPL_InitRegisters(opl_opl3mode); |
| 94 |
|
| 95 |
init_stage_reg_writes = 0; |
| 96 |
|
| 97 |
return 1; |
| 98 |
} |
| 99 |
|
| 100 |
// Shut down the OPL library. |
| 101 |
void OPL_Shutdown(void) |
| 102 |
{ |
| 103 |
if (callback_queue) |
| 104 |
{ |
| 105 |
OPL_Queue_Destroy(callback_queue); |
| 106 |
|
| 107 |
callback_queue = NULL; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
void OPL_SetCallback(uint64_t us, |
| 112 |
opl_callback_t callback, |
| 113 |
void *data) |
| 114 |
{ |
| 115 |
OPL_Queue_Push(callback_queue, callback, data, |
| 116 |
current_time - pause_offset + us); |
| 117 |
} |
| 118 |
|
| 119 |
void OPL_ClearCallbacks(void) |
| 120 |
{ |
| 121 |
OPL_Queue_Clear(callback_queue); |
| 122 |
} |
| 123 |
|
| 124 |
static void OPLTimer_CalculateEndTime(opl_timer_t *timer) |
| 125 |
{ |
| 126 |
int tics; |
| 127 |
|
| 128 |
// If the timer is enabled, calculate the time when the timer |
| 129 |
// will expire. |
| 130 |
if (timer->enabled) |
| 131 |
{ |
| 132 |
tics = 0x100 - timer->value; |
| 133 |
timer->expire_time = current_time |
| 134 |
+ ((uint64_t) tics * OPL_SECOND) / timer->rate; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
static void WriteRegister(unsigned int reg_num, unsigned int value) |
| 139 |
{ |
| 140 |
switch (reg_num) |
| 141 |
{ |
| 142 |
case OPL_REG_TIMER1: |
| 143 |
timer1.value = value; |
| 144 |
OPLTimer_CalculateEndTime(&timer1); |
| 145 |
break; |
| 146 |
|
| 147 |
case OPL_REG_TIMER2: |
| 148 |
timer2.value = value; |
| 149 |
OPLTimer_CalculateEndTime(&timer2); |
| 150 |
break; |
| 151 |
|
| 152 |
case OPL_REG_TIMER_CTRL: |
| 153 |
if (value & 0x80) |
| 154 |
{ |
| 155 |
timer1.enabled = 0; |
| 156 |
timer2.enabled = 0; |
| 157 |
} |
| 158 |
else |
| 159 |
{ |
| 160 |
if ((value & 0x40) == 0) |
| 161 |
{ |
| 162 |
timer1.enabled = (value & 0x01) != 0; |
| 163 |
OPLTimer_CalculateEndTime(&timer1); |
| 164 |
} |
| 165 |
|
| 166 |
if ((value & 0x20) == 0) |
| 167 |
{ |
| 168 |
timer1.enabled = (value & 0x02) != 0; |
| 169 |
OPLTimer_CalculateEndTime(&timer2); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
break; |
| 174 |
|
| 175 |
case OPL_REG_NEW: |
| 176 |
opl_opl3mode = value & 0x01; |
| 177 |
|
| 178 |
default: |
| 179 |
OPL3_WriteRegBuffered(&opl_chip, reg_num, value); |
| 180 |
break; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
static void OPL_AdvanceTime(unsigned int nsamples) |
| 185 |
{ |
| 186 |
opl_callback_t callback; |
| 187 |
void *callback_data; |
| 188 |
uint64_t us; |
| 189 |
|
| 190 |
// Advance time. |
| 191 |
us = ((uint64_t) nsamples * OPL_SECOND) / opl_sample_rate; |
| 192 |
current_time += us; |
| 193 |
|
| 194 |
if (opl_paused) |
| 195 |
{ |
| 196 |
pause_offset += us; |
| 197 |
} |
| 198 |
|
| 199 |
// Are there callbacks to invoke now? Keep invoking them |
| 200 |
// until there are none more left. |
| 201 |
while (!OPL_Queue_IsEmpty(callback_queue) |
| 202 |
&& current_time >= OPL_Queue_Peek(callback_queue) + pause_offset) |
| 203 |
{ |
| 204 |
// Pop the callback from the queue to invoke it. |
| 205 |
if (!OPL_Queue_Pop(callback_queue, &callback, &callback_data)) |
| 206 |
{ |
| 207 |
break; |
| 208 |
} |
| 209 |
|
| 210 |
callback(callback_data); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
void OPL_AdjustCallbacks(float factor) |
| 215 |
{ |
| 216 |
OPL_Queue_AdjustCallbacks(callback_queue, current_time, factor); |
| 217 |
} |
| 218 |
|
| 219 |
void OPL_Render_Samples (void *dest, unsigned buffer_len) |
| 220 |
{ |
| 221 |
unsigned int filled = 0; |
| 222 |
int16_t *buffer = (int16_t *) dest; |
| 223 |
|
| 224 |
// Repeatedly call the OPL emulator update function until the buffer is |
| 225 |
// full. |
| 226 |
while (filled < buffer_len) |
| 227 |
{ |
| 228 |
uint64_t next_callback_time; |
| 229 |
uint64_t nsamples; |
| 230 |
|
| 231 |
// Work out the time until the next callback waiting in |
| 232 |
// the callback queue must be invoked. We can then fill the |
| 233 |
// buffer with this many samples. |
| 234 |
if (opl_paused || OPL_Queue_IsEmpty(callback_queue)) |
| 235 |
{ |
| 236 |
nsamples = buffer_len - filled; |
| 237 |
} |
| 238 |
else |
| 239 |
{ |
| 240 |
next_callback_time = OPL_Queue_Peek(callback_queue) + pause_offset; |
| 241 |
|
| 242 |
nsamples = (next_callback_time - current_time) * opl_sample_rate; |
| 243 |
nsamples = (nsamples + OPL_SECOND - 1) / OPL_SECOND; |
| 244 |
|
| 245 |
if (nsamples > buffer_len - filled) |
| 246 |
{ |
| 247 |
nsamples = buffer_len - filled; |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
// Add emulator output to buffer. |
| 252 |
OPL3_GenerateStream(&opl_chip, buffer + filled * 2, nsamples); |
| 253 |
filled += nsamples; |
| 254 |
|
| 255 |
// Invoke callbacks for this point in time. |
| 256 |
OPL_AdvanceTime(nsamples); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
void OPL_WritePort(opl_port_t port, unsigned int value) |
| 261 |
{ |
| 262 |
if (port == OPL_REGISTER_PORT) |
| 263 |
{ |
| 264 |
register_num = value; |
| 265 |
} |
| 266 |
else if (port == OPL_REGISTER_PORT_OPL3) |
| 267 |
{ |
| 268 |
register_num = value | 0x100; |
| 269 |
} |
| 270 |
else if (port == OPL_DATA_PORT) |
| 271 |
{ |
| 272 |
WriteRegister(register_num, value); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
unsigned int OPL_ReadPort(opl_port_t port) |
| 277 |
{ |
| 278 |
unsigned int result = 0; |
| 279 |
|
| 280 |
if (port == OPL_REGISTER_PORT_OPL3) |
| 281 |
{ |
| 282 |
return 0xff; |
| 283 |
} |
| 284 |
|
| 285 |
if (timer1.enabled && current_time > timer1.expire_time) |
| 286 |
{ |
| 287 |
result |= 0x80; // Either have expired |
| 288 |
result |= 0x40; // Timer 1 has expired |
| 289 |
} |
| 290 |
|
| 291 |
if (timer2.enabled && current_time > timer2.expire_time) |
| 292 |
{ |
| 293 |
result |= 0x80; // Either have expired |
| 294 |
result |= 0x20; // Timer 2 has expired |
| 295 |
} |
| 296 |
|
| 297 |
return result; |
| 298 |
} |
| 299 |
|
| 300 |
// |
| 301 |
// Higher-level functions, based on the lower-level functions above |
| 302 |
// (register write, etc). |
| 303 |
// |
| 304 |
unsigned int OPL_ReadStatus(void) |
| 305 |
{ |
| 306 |
return OPL_ReadPort(OPL_REGISTER_PORT); |
| 307 |
} |
| 308 |
|
| 309 |
// Write an OPL register value |
| 310 |
void OPL_WriteRegister(int reg, int value) |
| 311 |
{ |
| 312 |
int i; |
| 313 |
|
| 314 |
if (reg & 0x100) |
| 315 |
{ |
| 316 |
OPL_WritePort(OPL_REGISTER_PORT_OPL3, reg); |
| 317 |
} |
| 318 |
else |
| 319 |
{ |
| 320 |
OPL_WritePort(OPL_REGISTER_PORT, reg); |
| 321 |
} |
| 322 |
|
| 323 |
// For timing, read the register port six times after writing the |
| 324 |
// register number to cause the appropriate delay |
| 325 |
for (i=0; i<6; ++i) |
| 326 |
{ |
| 327 |
// An oddity of the Doom OPL code: at startup initialization, |
| 328 |
// the spacing here is performed by reading from the register |
| 329 |
// port; after initialization, the data port is read, instead. |
| 330 |
if (init_stage_reg_writes) |
| 331 |
{ |
| 332 |
OPL_ReadPort(OPL_REGISTER_PORT); |
| 333 |
} |
| 334 |
else |
| 335 |
{ |
| 336 |
OPL_ReadPort(OPL_DATA_PORT); |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
OPL_WritePort(OPL_DATA_PORT, value); |
| 341 |
|
| 342 |
// Read the register port 24 times after writing the value to |
| 343 |
// cause the appropriate delay |
| 344 |
for (i=0; i<24; ++i) |
| 345 |
{ |
| 346 |
OPL_ReadStatus(); |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
// Initialize registers on startup |
| 351 |
void OPL_InitRegisters(int opl3) |
| 352 |
{ |
| 353 |
int r; |
| 354 |
|
| 355 |
// Initialize level registers |
| 356 |
for (r=OPL_REGS_LEVEL; r <= OPL_REGS_LEVEL + OPL_NUM_OPERATORS; ++r) |
| 357 |
{ |
| 358 |
OPL_WriteRegister(r, 0x3f); |
| 359 |
} |
| 360 |
|
| 361 |
// Initialize other registers |
| 362 |
// These two loops write to registers that actually don't exist, |
| 363 |
// but this is what Doom does ... |
| 364 |
// Similarly, the <= is also intenational. |
| 365 |
for (r=OPL_REGS_ATTACK; r <= OPL_REGS_WAVEFORM + OPL_NUM_OPERATORS; ++r) |
| 366 |
{ |
| 367 |
OPL_WriteRegister(r, 0x00); |
| 368 |
} |
| 369 |
|
| 370 |
// More registers ... |
| 371 |
for (r=1; r < OPL_REGS_LEVEL; ++r) |
| 372 |
{ |
| 373 |
OPL_WriteRegister(r, 0x00); |
| 374 |
} |
| 375 |
|
| 376 |
// Re-initialize the low registers: |
| 377 |
|
| 378 |
// Reset both timers and enable interrupts: |
| 379 |
OPL_WriteRegister(OPL_REG_TIMER_CTRL, 0x60); |
| 380 |
OPL_WriteRegister(OPL_REG_TIMER_CTRL, 0x80); |
| 381 |
|
| 382 |
// "Allow FM chips to control the waveform of each operator": |
| 383 |
OPL_WriteRegister(OPL_REG_WAVEFORM_ENABLE, 0x20); |
| 384 |
|
| 385 |
if (opl3) |
| 386 |
{ |
| 387 |
OPL_WriteRegister(OPL_REG_NEW, 0x01); |
| 388 |
|
| 389 |
// Initialize level registers |
| 390 |
for (r=OPL_REGS_LEVEL; r <= OPL_REGS_LEVEL + OPL_NUM_OPERATORS; ++r) |
| 391 |
{ |
| 392 |
OPL_WriteRegister(r | 0x100, 0x3f); |
| 393 |
} |
| 394 |
|
| 395 |
// Initialize other registers |
| 396 |
// These two loops write to registers that actually don't exist, |
| 397 |
// but this is what Doom does ... |
| 398 |
// Similarly, the <= is also intenational. |
| 399 |
for (r=OPL_REGS_ATTACK; r <= OPL_REGS_WAVEFORM + OPL_NUM_OPERATORS; ++r) |
| 400 |
{ |
| 401 |
OPL_WriteRegister(r | 0x100, 0x00); |
| 402 |
} |
| 403 |
|
| 404 |
// More registers ... |
| 405 |
for (r=1; r < OPL_REGS_LEVEL; ++r) |
| 406 |
{ |
| 407 |
OPL_WriteRegister(r | 0x100, 0x00); |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
// Keyboard split point on (?) |
| 412 |
OPL_WriteRegister(OPL_REG_FM_MODE, 0x40); |
| 413 |
|
| 414 |
if (opl3) |
| 415 |
{ |
| 416 |
OPL_WriteRegister(OPL_REG_NEW, 0x01); |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
void OPL_SetPaused(int paused) |
| 421 |
{ |
| 422 |
opl_paused = paused; |
| 423 |
} |