This is part of a series of posts analysing the Chip-8 interpreter on the RCA COSMAC VIP computer. These posts may be useful if you are building a Chip-8 interpreter on another platform or if you have an interest in the operation of the COSMAC VIP. For other posts in the series refer to the index or instruction index.
INSTRUCTION GROUP: FX18
Set the sound timer with the value in VX
Sound on the unexpanded COSMAC VIP Is about as simple as sound can get short of having none. The COSMAC VIP uses a simple oscillator circuit based around a CA555 timer to generate a fixed frequency tone, which is used to drive a small speaker. This tone generator is switched on or off with a special single bit output of the CDP1802 processor, labelled Q. This can be set or reset with a SEQ or REQ instruction respectively.
Chip-8 has a timer that determines when a tone is generated. The timer holds an eight bit value (0 to 255). Whenever this timer holds a non-zero value Q is set and a tone is generated. When the timer is at zero Q is reset and no tone is generated. The timer is decremented every time an interrupt is generated, which is 60 times a second. That means the duration of the sound can theoretically range from 1/60th of a second (if the timer is set to a value of 1) to about 4.25 seconds (if the timer is set to a value of 255). Note that the speaker on the original COSMAC VIP hardware would not respond at all to a count of 1 on the timer. I discuss the code for the interrupt routine at length in a previous post.
Given the simplicity of tone generation on the COSMAC VIP it should come as no surprise that Chip-8 has just one sound-related instruction. This is FX18, which simply sets the sound timer to the value stored in VX. This will cause a tone of the appropriate length to be generated.
Here’s the code that achieves this:
|Address (hex)||Code (hex)||Labels||Assembly||Comments|
|0118||06||FX18:||LDN 6||Get the value in VX (which is pointed to by R6)|
|0119||A8||PLO 8||Copy it into the sound timer (R8.0)|
|011A||D4||SEP 4||Return to the fetch and decode routine|
As this is part of the FXXX instruction group it is subject to a small secondary stage of decoding which is discussed in an earlier post.
In addition to the secondary decoding overhead, execution time for this instruction is 6 machine cycles (27.24 microseconds).
One thing to note about this instruction is that a count sent to the sound timer that is still in progress will be overwritten by any subsequent use of this instruction. So you can turn the tone off simply by using this instruction to send a zero value to the the sound timer.
You should also note that FX0A instruction makes use of the sound timer to beep when a key is pressed. This will end any sound that was previously playing when the FX0A instruction was executed.
A programmer of a contemporary interpreter could potentially ignore this instruction and Chip-8 programmes would still function. Ideally it should be implemented and generate a tone at a frequency set by the programmer.