OPL2 Audio Board: an AdLib sound card for Arduino

Vincent Bernat

In a pre­vi­ous ar­ti­cle, I pre­sented the OPL2LPT, a sound card for the par­al­lel port fea­tur­ing a Yamaha YM3812 chip, also known as OPL2—the chip of the AdLib sound card. The OPL2 Au­dio Board for Ar­duino is an­other in­die sound card us­ing this chip. How­ever, in­stead of re­ly­ing on a par­al­lel port, it uses a se­rial in­ter­face, which can be driven from an Ar­duino board or a Rasp­berry Pi. While the OPL2LPT tar­gets ret­rogamers with real hard­ware, the OPL2 Au­dio Board can­not be used in the same way. None­the­less, it can also be op­er­ated from ScummVM and DOSBox!

OPL2 Audio Board for Arduino
The OPL2 Audio Board over a “Grim Fandango” box.

Unboxing#

The OPL2 Au­dio Board can be pur­chased on Tindie, ei­ther as a kit or fully as­sem­bled. I have paired it with a cheap clone of the Ar­duino Nano. A li­brary to drive the board is avail­able on GitHub, along with some examples.

One of them is DemoTune.​ino. It plays a short tune on three chan­nels. It can be com­piled and up­loaded to the Ar­duino with PlatformIO—in­stal­lable with pip install platformio—us­ing the fol­low­ing command:1

$ platformio ci \
>   --board nanoatmega328 \
>   --lib ../../src \
>   --project-option="targets=upload" \
>   --project-option="upload_port=/dev/ttyUSB0" \
>   DemoTune.ino
[…]
PLATFORM: Atmel AVR > Arduino Nano ATmega328
SYSTEM: ATMEGA328P 16MHz 2KB RAM (30KB Flash)
Converting DemoTune.ino
[…]
Configuring upload protocol...
AVAILABLE: arduino
CURRENT: upload_protocol = arduino
Looking for upload port...
Use manually specified: /dev/ttyUSB0
Uploading .pioenvs/nanoatmega328/firmware.hex
[…]
avrdude: 6618 bytes of flash written
[…]
===== [SUCCESS] Took 5.94 seconds =====

Im­me­di­ately af­ter the up­load, the Ar­duino plays the tune. 🎶

The next in­ter­est­ing ex­am­ple is SerialIface.​ino. It turns the au­dio board into a sound card over se­rial port. Once the code has been pushed to the Ar­duino, you can use the play.py pro­gram in the same di­rec­tory to play VGM files. They are a sam­ple-ac­cu­rate sound for­mat for many sound chips. They log the ex­act com­mands sent. There are many of them on VGMRips. Be sure to choose the ones for the YM3812/OPL2! Here is a small se­lection:

The OPL2 Audio Board playing some VGM files. It is connected to an Arduino Nano. You can see the LEDs blinking when the Arduino receives the commands from the serial port.

Usage with DOSBox & ScummVM#

Notice

The sup­port for the se­rial pro­to­col used in this sec­tion has not been merged: in­stead, a sim­pler pro­to­col was de­signed. You need to grab SerialIface.​ino from my pull re­quest: git checkout 50e1717.

When the Ar­duino is flashed with SerialIface.​ino, the board can be driven through a sim­ple pro­to­col over the se­rial port. By patch­ing DOSBox and ScummVM, we can make them use this un­usual sound card. Here are some ex­am­ples of games:

  • 0:00, with DOSBox, the first level of Doom 🎮 (1993)
  • 1:06, with DOSBox, the in­tro­duc­tion of Loom 🎼 (1990)
  • 2:38, with DOSBox, the first level of Lem­mings 🐹 (1991)
  • 3:32, with DOSBox, the in­tro­duc­tion of Leg­end of Kyran­dia 🃏 (1992)
  • 6:47, with ScummVM, the in­tro­duc­tion of Day of the Ten­ta­cle ☢️ (1993)
  • 11:10, with DOSBox, the in­tro­duc­tion of An­other World 🐅 (1991)

An­other World (also known as Out of This World), de­signed by Éric Chahi, is us­ing sam­pled sounds at 5 kHz or 10 kHz. With a se­rial port op­er­at­ing at 115,200 bits/s, the 5 kHz op­tion is just within our reach. How­ever, I have no idea if the ren­der­ing is faithful.

Up­date (2018-05)

Af­ter some dis­cus­sions with Wal­ter van Niftrik, we came to the con­clu­sion that, above 1 kHz, DOSBox doesn’t “time-ac­cu­rately” ex­e­cute OPL com­mands. It is us­ing a time slice of one mil­lisec­ond dur­ing which it ex­e­cutes ei­ther a fixed num­ber of CPU cy­cles or as many as pos­si­ble (with cycles=​max). In both cases, em­u­lated CPU in­struc­tions are ex­e­cuted as fast as pos­si­ble and I/O de­lays are sim­u­lated by re­mov­ing a fixed num­ber of cy­cles from the al­lo­ca­tion of the cur­rent time slice.

DOSBox#

The se­rial pro­to­col is de­scribed in the SerialIface.​ino file:

/*
 * A very simple serial protocol is used.
 *
 * - Initial 3-way handshake to overcome reset delay / serial noise issues.
 * - 5-byte binary commands to write registers.
 *   - (uint8)  OPL2 register address
 *   - (uint8)  OPL2 register data
 *   - (int16)  delay (milliseconds); negative -> pre-delay; positive -> post-delay
 *   - (uint8)  delay (microseconds / 4)
 *
 * Example session:
 *
 * Arduino: HLO!
 * PC:      BUF?
 * Arduino: 256 (switches to binary mode)
 * PC:      0xb80a014f02 (write OPL register and delay)
 * Arduino: k
 *
 * A variant of this protocol is available without the delays. In this
 * case, the BUF? command should be sent as B0F? The binary protocol
 * is now using 2-byte binary commands:
 *   - (uint8)  OPL2 register address
 *   - (uint8)  OPL2 register data
 */

Adding sup­port for this pro­to­col in DOSBox is rel­a­tively sim­ple (patch). For best per­for­mance, we use the 2-byte vari­ant (5000 ops/s). The bi­nary com­mands are pipelined and a ded­i­cated thread col­lects the ac­knowl­edg­ments. A sem­a­phore cap­tures the num­ber of free slots in the re­ceive buf­fer. As it is not pos­si­ble to read reg­is­ters, we rely on DOSBox to em­u­late the timers, which are mostly used to let the var­i­ous games de­tect the OPL2.

The patch is tested only on Linux but should work on any POSIX sys­tem—not Win­dows. To test it, you need to build DOSBox from source:

$ sudo apt build-dep dosbox
$ git clone https://github.com/vincentbernat/dosbox.git -b feature/opl2audioboard
$ cd dosbox
$ ./autogen.sh
$ ./configure && make

Re­place the sblaster sec­tion of ~/.​dosbox/​dosbox-SVN.conf:

[sblaster]
sbtype=none
oplmode=opl2
oplrate=49716
oplemu=opl2arduino
opl2arduino=/dev/ttyUSB0

Then, run DOSBox with ./src/​dosbox. That’s it!

You will likely get the “OPL2Arduino: too slow, consider increasing buffer” mes­sage a lot. To fix this, you need to re­com­pile SerialIface.​ino with a big­ger re­ceive buffer:

$ platformio ci \
>   --board nanoatmega328 \
>   --lib ../../src \
>   --project-option="targets=upload" \
>   --project-option="upload_port=/dev/ttyUSB0" \
>   --project-option="build_flags=-DSERIAL_RX_BUFFER_SIZE=512" \
>   SerialIface.ino

ScummVM#

The same code can be adapted for ScummVM (patch). To test, build it from source:

$ sudo apt build-dep scummvm
$ git clone https://github.com/vincentbernat/scummvm.git -b feature/opl2audioboard
$ cd scummvm
$ ./configure --disable-all-engines --enable-engine=scumm && make

Then, you can start ScummVM with ./scummvm. Se­lect “AdLib Em­u­la­tor” as the mu­sic de­vice and “OPL2 Ar­duino” as the AdLib em­u­la­tor.2 Like for DOSBox, watch the con­sole to check if you need a larger re­ceive buffer.

Enjoy! 🎶