RSS icon Home icon
  • Microcontroller music

    Posted on March 27th, 2015 admin No comments

    Many years ago I wanted to create an indoor game which requires an explosion sound effect. I opted for a microcontroller because it’s cheap and easy to program. But didn’t know exactly how to produce that explosion sound. It was obvious that I have to synthesize the sound instead of playing a recorded sample.
    First a PRNG (Pseudorandom Number Generator) came to my mind but it produces a white noise which is not quite like a boom effect. That’s why I turned to ZX Spectrum beeper music. Those guys in 80s did great sound effects with an 8-bit CPU and a simple buzzer.

    The Sinclair ZX Spectrum 48K (released on 1982) has very minimalist sound capabilities. Sounds generated by a piezo buzzer, controlled by one bit. Changing this bit at the right frequency produces audible noise. Without much support from the hardware there are many awesome tunes created. Even multichannel beeper musics exist with 10 or more channels mixed.

    So I started to disassemble and analyse sound engines in ZX games. While searching for an adequate routine I became interested in multichannel music. I programmed two music into PIC microcontroller: Title tune from Gyroscope and Title tune from Dynamite Dan.

    The PIC 16F84A only has 1K program memory but it turned out the tunes fit nicely. The implementation wasn’t just converting the ripped out Z80 machine code to PIC assembly. I had to understand how those routines produce sound and the timing of the instructions also took into consideration. The Z80 CPU in the ZX Spectrum clocked at 3.5 MHz while the PIC runs at 4 MHz. The Z80 instructions have different timing, while almost all instructions on the PIC are single-cycle. This PIC has only 68 bytes of RAM, and only 64 bytes of EEPROM so the songs have to reside in the program memory. The program memory cannot be read directly so I made the song tables with many RETLW instructions.

    The Dynamite Dan (by Rod Bowkett) tune is a simple one channel song. There are two tables made up the music: one table is the song pattern, the other contains predefined tones. The music routine iterates through the pattern data which refers to tune data, and there is an inner loop which toggles the speaker based on tune data. That’s all simple.

    The Gyroscope (by Mark Alexander) music engine is a bit advanced two channel one. It is very similar to the routine found in “Wham! The Music Box” utility (except drums), also made by Mark Alexander. The engine alternates between the two channels very fast (at 13.7 us to be exact, which is equivalent of ~73 kHz sample rate). There is no tone length stored, the song’s tempo determines the length of every tone. The two channels’ pattern list stored at separate memory addresses.

    You can download the disassembled source of the original music engines, the PIC source codes and the assembled hex binaries. The Gyroscope archive contains a bbsong conversion which can be played in Beepola.

    Download Gyroscope music source here.

    Download Dynamite Dan music source here.

    Leave a Reply

    Your email address will not be published. Required fields are marked *