The chip8 timers, sound and input emulation.


Not much is known about the timing behind the Chip8 system which is why games run at different speeds and also why the graphics flicker. The main technique emulator programmers use to get the feel right is to tweak the timing until it works. I usually put the amount of opcodes to execute each second in my emulators ini file so the users can change it so it feels right for their system. Although all chip8 emulators timings are inaccurate this doesnt cause bugs in games unlike all other systems. The way I emulate time is to cap my framerate to 60 frames per second and each 1/60th of a second I update the cpu. The cpu update simply executes the required number of opcodes this particular frame. If the emulator is trying to emulate 600 opcodes a second then the amount of opcodes to emulate each frame is 600/60.

The Chip8 has two timers the delay timer and the sound timer. Both count down at a rate of 60Hz (60 times a second) if their values are greater than 0. As explained above I update the emulate once every 1/60th of a second which is perfect for decrementing the timers every update.


The Chip8 can only play the one sound which is a beep noise. The sound is played whenever the sound timer is greater than 0. So every time I decrement the sound timer I then check its value to see if it is greater than 0, if so I play a beep sound. This is a really very primitive way of emulating sound. Unfortunately this wont teach you much about real sound emulation which is widely regarded as being the most difficult part of emulation.


The Chip8 has 16 keys ranged 0x0-0xF which the player uses to control the game (obviously). The best way to emulate the keyboard is to have a byte array of 16 emulates (one for each key). The emulator should then map the qwerty keyboard keys to a Chip8 key. For example I map keyboard key 'q' to Chip8 key 0x4. When I detect keyboard key 'q' has been pressed I m_Input[0x4] = 1 and when keyboard key 'q' is released I m_Input[0x4] = 0. It is as simple as that. I use SDL events to detect the pressed and released keys. Click here to find the correct mappings of the Chip8 keypad.