Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 7

CHIP-8 Timers: Delay & Sound

Recommended resources

Welcome back. In our last session, we completed our look at the CHIP-8's primary I/O systems by examining the 16-key hexadecimal keypad. We have now covered memory, registers, the display, and user input. This brings us to the final component of the CHIP-8's abstract architecture: its timing system.

This lesson concludes our first module by explaining the CHIP-8 timer system. We will explore the delay timer and the sound timer, both of which are fundamental for controlling game logic, animations, and providing audio feedback. Understanding how these timers operate at a fixed frequency, independent of the main CPU speed, is a crucial concept in emulation. Once we master this, we will have a complete architectural blueprint, ready to begin our transition to implementation with JavaScript in the next module.

An Introduction to CHIP-8 Timers

The CHIP-8 virtual machine includes two simple, yet effective, 8-bit timers. As with the other components we've discussed, they serve specific functions within the system.

A block diagram of the CHIP-8 system. The Delay Timer (DT) and Sound Timer (ST) are shown as distinct hardware components, highlighting their role in the overall architecture.

The two timers are:

  1. The Delay Timer (DT): A general-purpose timer that programs can use to manage time-based events, such as animation speed or enforcing a delay.
  2. The Sound Timer (ST): A specialized timer used exclusively for generating a simple audio beep.

Both timers operate on the same fundamental principle: they are 8-bit registers that function as countdown timers. When set to a non-zero value, they decrement towards zero at a fixed rate.


The 60Hz Decrement Rate

The most important characteristic of these timers is their update frequency. Both the Delay Timer and the Sound Timer are specified to decrement at a rate of 60 times per second (60Hz), as long as their value is greater than zero.

This is a critical design feature. The timer decrement rate is independent of the CPU's execution speed. In a physical system, this would be managed by a separate clock signal. In our emulator, we will need to create a mechanism that updates the timers at this fixed 60Hz interval, separate from our main CPU instruction loop. This ensures that timing-dependent game logic behaves consistently, regardless of how fast the emulator is running on the host machine.

The following reading provides a concise overview of this mechanism.

Guide to making a CHIP-8 emulator - Tvil

This section from Tobias V. Langhoff's guide clearly explains the function of both timers, their size, and their 60Hz decrement rate.

Please read the section under the heading Timers. Pay close attention to the statement that the decrementing "is independent of the speed of the fetch/decode/execute loop."


The Delay Timer (DT) in Practice

The Delay Timer is a tool for the CHIP-8 program. When a game needs to time an event—for example, controlling how quickly a player's shot moves across the screen or how long an enemy remains in a certain state—it will load a value into the DT. The program can then periodically check the value of the DT. The timer itself does not halt or "delay" the CPU; it simply provides a register that counts down at a predictable rate, which the program's logic can then act upon.

Programs interact with the DT using two specific instructions:

  • FX15 - LD DT, Vx: Loads the value from a general-purpose register Vx into the Delay Timer, starting the countdown.
  • FX07 - LD Vx, DT: Reads the current value of the Delay Timer and stores it in a register Vx.

The Sound Timer (ST) and Audio Feedback

The Sound Timer functions almost identically to the Delay Timer: it's an 8-bit register that decrements at 60Hz when its value is greater than zero. However, it has one special property: as long as the ST's value is greater than 0, the system should produce a sound.

This provides a simple mechanism for audio feedback. For example, to make a beep that lasts for half a second, a program would load the ST with the value 30 (since it decrements 60 times per second).

Unlike the Delay Timer, there is no instruction to read the Sound Timer's value. Its state is only relevant for controlling the audio output.

  • FX18 - LD ST, Vx: Loads the value from register Vx into the Sound Timer.

The following reading details the timers and their associated instructions.

CHIP-8 virtual machine specification - Langur Monkey

This documentation provides a clear specification for both timers and the opcodes used to manipulate them.

Start by reading the section under the heading Timers. This gives a full description. Then, quickly review the short instruction descriptions for LD Vx, DT, LD DT, Vx, and LD ST, Vx to see how they are formally defined.


Visualizing the Timer Update Loop

To make this concept more concrete, it's helpful to see how it's handled in an actual emulator. The key is that the timer update logic belongs in its own loop, synchronized to a 60Hz clock, which is often tied to the display refresh rate in modern applications.

This video demonstrates the implementation of the timer update logic and its effect on a game.

CHIP-8 Emulator p4: Finish Opcodes, Keypad, CPU Speed (C / SDL2)

The presenter in this video from the Queso Fuego channel implements the timer update logic and then demonstrates its effect.

First, watch the segment where a dedicated update_timers function is created, from this point. Notice how the logic is simply if (timer > 0) { timer--; }, and how it's intended to be called at a 60Hz frequency. Then, watch the demonstration of a Tetris ROM from this section. The falling pieces are controlled by the Delay Timer, providing a clear visual example of its purpose in game mechanics.


Conclusion

This lesson completes our architectural survey of the CHIP-8 virtual machine. We have seen that its timing system, while simple, is a crucial element for creating interactive and dynamic programs.

The key takeaways are:

  • The CHIP-8 has two 8-bit timers: the Delay Timer (DT) for general-purpose timing and the Sound Timer (ST) for audio.
  • Both timers decrement at a fixed frequency of 60Hz when their value is greater than zero.
  • This timing mechanism is independent of the CPU's main execution loop, a critical detail for accurate emulation.
  • The ST produces a beep for as long as its value is non-zero.
  • Specific opcodes allow programs to set the timers and read the value of the DT.

We have now explored the memory layout, registers, instruction format, display, keypad, and timers. With this complete theoretical model, you have all the architectural knowledge needed to begin building the emulator.

In our next lesson, we will pivot from theory to practice. As per your preference, we will start by exploring the specific web technologies we'll need, beginning with a deep dive into JavaScript's Typed Arrays. These are essential for efficiently managing the CHIP-8's memory and registers, and understanding them is the first step in translating our architectural model into working code.