Simple PIC SID Chip Emulator
Collecting 1970's and 1980's computers is one of my hobbies, although, not so much these days as I do not have the funds for it since the bubble was started on them, and people now just buy them as assets and not because of a love for them.
One of the most popular computers of the 1980's was the Commodore 64, not only because it had a great graphics chip, but also because it had a great sound chip, namely the "SID chip", or to give its technical name, the MOS6581, later changed to the MOS8580.
I've produced a simple SID chip sound emulator using the Positron8 PIC BASIC Compiler, which I think is the first of its kind for a PICmicro, and certainly for an 8-bit device written in BASIC, which just goes to illustrate the power and flexibility of the compiler's language
There are certain limitations, in that the SID emulator does not, yet, support the filters that the original SID chip contained. This is not a hardware issue, or a compiler language issue, but the fact that I don't fully understand the theory and practice of IIR or FIR filters enough to create them with any efficiency. So if anyone with a knowledge of IIR or FIR filters is prepared to pick up the baton and create the filters required within the code, they would be more than welcome to do so.
However, even without the filters, the sound produced is pretty good.
The code is for use with a PIC18F25K20 device using a 16MHz crystal, internally boosted to 64MHz by the PLL. However, there is no reason that the code could not be adapted for any other 18FxxK devices. It could also be very easily enabled to use the PICmicro's internal oscillator, meaning all that is required is a single resistor and capacitor at the output.
The microcontroller source code is in the Positron8_Simple_PIC_SID.zip file.
It also includes an application to convert a .SID file to a .txt file
And a Delphi Application that converts the .txt file to a Positron8 BASIC .inc file so that the emulator can play the music.
The firmware uses the microcontroller's hardware PWM (Pulse Width Modulation) peripheral as a 10-bit DAC, so all that is required is a simple R/C low pass filter connected from pin PORTC.2 (CCP1) to the input of an amplifier. Headphones will suffice, but the volume will be low.
The firmware for the SID emulator looks complex, but in fact it's rather simple. The reason it looks complex is because it has been rolled out in order to increase readability and speed of execution. It consists of a Timer0 overflow interrupt to create and mix the 3 channels of sound, a channel waveform creator, and an ADSR. The player code also looks complicated, and I must admit, it is more complicated than it should be, and could be simplified. However, all it does is read the sequence of SID registers from a very large data table, pass these to the SID emulator, create non-blocking delays, and trigger the ADSR at the appropriate times. The player is not perfect, and could be improved upon significantly, but it served its purpose of testing the main emulator code.
A SID chip emulator is of limited use if the original SID tunes cannot be played, so I created a very simple Delphi application that converts the output of another, third partly, application into Dim as Code statements that the compiler can understand. The Delphi application, and its source code is included in the zip file. The third party application is named Sid2midiw.exe, however, not only does the application produce midi files based upon actual SID tunes, it also produces a txt file that contains the register changes required for the SID chip for a particular tune. This is what the Delphi application uses for its conversion. This is also included in the zip file along with some txt files produced by it, ready for conversion to Flash memory tables.
To convert a tune ready for the emulator and player:
Download some SID tunes from the internet. There are literally hundreds, if not thousands, of SID tunes out there.
Run Sid2midiw.exe and create a .txt file for the chosen .SID file.
Place the .txt file into the Delphi application's "Raw Tunes" folder.
Run the Delphi app SID_MusicConverter.exe. Press the convert button and point to the file to be converted. The .inc include file ready to place into the compiler will be produced in the application's "Converted Tunes" folder.
It's important to note that because of a combination of the lack of filters within the emulator, and the Sid2midiw.exe application not always producing the exact output, and my player firmware's simplistic nature, not all SID tunes will be re-created as they originally sounded on the Commodore 64.
It is very straightforward to use the SID emulator as a standard synthesiser, and all that is required are a few subroutines that adjust the waveform and ADSR etc. When called, the synthesiser will produce a sound, or three simultaneously.