algorithm Arduino electronics PCB software

Making music with a Yamaha YM2413 Synthesizer – Part 2

The first part concluded with the YM2413 hardware and an amplifier on a test Arduino Uno shield. In this and the next part we explore the interface to the device and how to control the hardware to make music.

Communicating with the Hardware

The data interface between the Arduino MCU and the YM2413 uses 8 digital output pins from the MCU. Additional AO and WE digital outputs are used to load the data into the YM2413 IC.

The connections between the MCU and YM2413 shield are mapped as shown below, with the numbers in brackets the actual pin number. The Arduino pin numbers are arbitrary but hardwired as described on the YM2413 test shield.

Arduino PinYM2413
D0 [D8]D0 [17]
D1 [D9]D1 [18]
D2 [D7]D2 [2]
D3 [D6]D3 [3]
D4 [A0]D4 [4]
D5 [A1]D5 [5]
D6 [A2]D6 [6]
D7 [A3]D7 [7]
AO [D4]/AO [10]
WE [8]/WE [5]

The communications protocol is relatively straightforward and consist of writing an address to to IC followed by the data for that address, using the following sequence:

  1. Set A0 low to activate the address register.
  2. Set data D0-D7 with the register address. D0 is the least significant bit of the address.
  3. Set /WE low to latch the address.
  4. Wait 12 YM2413 clock cycles for the request to be process (approx 4 uS).
  5. Set /WE high.
  6. Set A0 high to activate the data register.
  7. Set data D0-D7 with the relevant data byte.
  8. Set /WE low to latch the address.
  9. Wait 84 YM2413 clock cycles for the request to be process (approx 25 uS).
  10. Set /WE high.

When writing data consecutively to the same register, setting the register address can be skipped (ie, the register address is retained by the YM2413 IC until it is overwritten).

Making a sound

Once the basics of setting data in the IC is sorted out, the next step is to make the IC produce a sound. The simplest way to do this is to set one of the channels to be an instrument (like piano) and send the right sequence to play a note.

The YM2413 Application Manual has a full list of all the registers and values for the different modes, and it takes some detailed reading to get it all straight. The register map (shown below) can be roughly split into 3 functional parts.

  • Registers 0x00 to 0x07 are used to define the custom instrument parameters (more on this later).
  • Register 0x0E and 0x36 to 0x38 are used to enable the percussion instruments and switch individual percussion instruments on/off. When percussion is enabled, registers 0x36 through 0x38 are used for percussion volume control.
  • Registers 0x10 to 0x38 are used to set and manage the 9 melodic synthesizer channels, with 0x36 to 0x38 repurposed when percussion instruments are enabled.

So the simplest way to make a sound is to:

  1. Assign an instrument to channel 0 (register 0x30 bite 4-7),
  2. Set the volume to maximum (register 0x30 bits 0-3),
  3. Set the frequency (note) to play (register 0x20 bits 0-3 and 0x10), and
  4. Turn the note on (register 0x20 bit 4). The note must also be explicitly turned off using the same register.

The frequency attributes for a channel are based on a calculation given in the application manual, resulting in a ‘Block’ number and an ‘F-Number’ that are used in the appropriate registers. A gotcha here is that Yamaha defines middle C as C3 (C in ISO octave 3), while ISO define middle C as C4.

Playing percussion instruments

To play percussion instruments, the YM2413 must be placed into a special mode by setting register 0x0e bit5. This reduces the number of melodic channels to 6 and repurposes the volume controls at registers 0x36 to 0x38 for these alternative instruments.

The note on/off controls for each percussion instrument are register 0x0e bits 0-4.

As this was starting to look like a complicated device, I found it easiest to write and document a library to support the bit-twiddling and control mechanisms. This library (MD_YM2413) is available from my code repository.

The next and final instalment outlines how the library is implemented and how the YM2413 output sounds.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s