Building an NES Emulator in Reaktor, Part II
In this tutorial, I’ll continue the work from last week, building an NES emulator in Reaktor from scratch (well, the audio parts anyway!). In the previous tutorial, I explained many of the difficulties around the emulation process, and designed a simple device to calculate the number of NES clock cycles to process for each tick of the sample clock in Reaktor.
THE APU CLOCK
In my work last week, I forgot that there are actually a couple different clocks used for the oscillators in the NES. The triangle oscillator uses the CPU Clock which we made last week, the pulse and noise oscillators use the APU clock, which runs at half the speed of the CPU clock. It is easy enough to add this:
Now we are ready to get on with making the oscillators, beginning with some common elements.
CREATING A TIMER
A timer, or divider, is a component used by all of the sound generators in the NES. It has a fairly simple operation: it outputs a single clock event for every T+1 events received. The value, T, is given by a simple equation:
T = CPU/(16*F) – 1
Where CPU is the clock speed in Hz, and F is the frequency of the oscillator. In Reaktor, this looks like so:
You may have noticed that T is an integer. This leads to a rounding error in regards to frequency, usually small enough to pass unnoticed.
With a T value of 8 or less, the oscillator shuts off entirely. Since this translates to a frequency of over 12KHz, which is rarely used in synthesis, so I have omitted this detail for the time being, however this would be easy to implement if desired.
Let’s talk for a moment about the exact operation of the timer. It stores a value called the counter, that is initialized to the value of T. For each incoming clock, the counter is checked. If it is greater than zero, the value is decreased by one. If it is equal to zero, it is reset to T, and a clock is sent to the output.
Depending on the value of T, the Timer might send out several triggers per Reaktor sample. We are receing either 20 or 21 APU clocks per sample. With a mininum T value of 8, this means that there could be as many as three outputs from the Timer that we need to calculate at once.
With this in mind, I developed the following timer device:
Here, we are receiving three pieces of information: the current value of the timer (Cnt), the number of APU clocks left to process (Clk), and our previously calculated value of T.
If the counter is higher than the number of events, we simply pass the number of clock values to the ‘Events’ output, subtract it from the counter, and send zero clock events to the next Timer macro.
Otherwise, the ‘Events’ output get the number held by the counter, the counter is reset to T, and whatever clock events remain are sent to the next Timer macro.
Three of these macros connected together will give us the all of the information we need to continue:
Okay, so the values Clk and Cnt are being passed from macro to macro, as they are changing each time. The output ‘Events’ is the number of events to calculate at a given position.
This whole setup can be pretty confusing. The numbers coming out the ‘Zero’ output are events that occured before anything changed with the oscillator, IE before it received any events from the timer. The outputs ‘One’ and ‘Two’ require that the oscillator be clocked.
Next week, I’ll show how these Timers can be used to control NES oscillators. If you have any questions, suggestions, or comments, please leave them below!