Write a CHIP-8 Emulator in C From Scratch
Build a complete CHIP-8 interpreter in C11: memory, registers, the font, all 35 opcodes, timers, a 64x32 display buffer and a text-mode renderer to test it.

CHIP-8 is the smallest machine that still feels like a computer. It was designed in the 1970s as a virtual machine for writing simple games on hobby computers, and it has 35 instructions, a 64 by 32 monochrome display, sixteen keys and two timers. A complete emulator fits in a few hundred lines of C, which makes it the standard first project: you meet memory, registers, the fetch-decode-execute loop, flags, timers, graphics and input, with nothing to hide behind. This post builds one in C11, compiled with GCC 14 or Clang 19, as a single self-contained program with a plain-text renderer so you can test it with no graphics library. It follows the instruction semantics in Cowgod's CHIP-8 Technical Reference and the notes of the CHIP-8 community, and a sibling post writes the same machine in Rust. If the fetch-decode-execute idea is new, read the post on the CPU loop first.
The scope is the original COSMAC VIP behaviour for each instruction, with the places where interpreters disagree marked as quirks. The next post in this series covers the quirks in depth. Here, the choices follow what most ROMs expect.
The machine
CHIP-8 has these parts:
4096 bytes of memory. Programs load at
0x200. The first 512 bytes were once the interpreter itself, and today they hold the built-in font.Sixteen 8-bit registers
V0toVF.VFdoubles as a flag register set by arithmetic and drawing, so programs should not use it for data.A 16-bit index register
I, used for memory addresses.A program counter and a call stack of sixteen return addresses.
Two 8-bit timers, delay and sound, each counting down at 60 Hz while non-zero.
A 64 by 32 one-bit display, drawn by XORing sprites onto it.
A sixteen-key hex keypad, keys
0toF.
#ifndef CHIP8_H
#define CHIP8_H
#include <stdbool.h>
#include <stdint.h>
#define SCREEN_W 64
#define SCREEN_H 32
typedef struct {
uint8_t memory[4096];
uint8_t v[16];
uint16_t i;
uint16_t pc;
uint16_t stack[16];
uint8_t sp;
uint8_t delay_timer;
uint8_t sound_timer;
uint8_t screen[SCREEN_W * SCREEN_H]; /* one byte per pixel: 0 or 1 */
bool keys[16];
bool draw; /* set when the screen changed */
uint32_t rng;
} Chip8;
void chip8_init(Chip8 *c);
void chip8_load(Chip8 *c, const uint8_t *rom, unsigned len);
void chip8_cycle(Chip8 *c);
void chip8_tick_timers(Chip8 *c);
#endifInitialization and the font
Programs draw digits by pointing I at a built-in 4 by 5 pixel font, with each glyph five bytes. The conventional place is 0x050, and the instruction FX29 computes the address of the glyph for the digit in VX. Each byte uses the top four bits as pixels. The glyph for 0 is F0 90 90 90 F0:
****
* *
* *
* *
****#include <string.h>
#include "chip8.h"
static const uint8_t font[80] = {
0xF0,0x90,0x90,0x90,0xF0, /* 0 */ 0x20,0x60,0x20,0x20,0x70, /* 1 */
0xF0,0x10,0xF0,0x80,0xF0, /* 2 */ 0xF0,0x10,0xF0,0x10,0xF0, /* 3 */
0x90,0x90,0xF0,0x10,0x10, /* 4 */ 0xF0,0x80,0xF0,0x10,0xF0, /* 5 */
0xF0,0x80,0xF0,0x90,0xF0, /* 6 */ 0xF0,0x10,0x20,0x40,0x40, /* 7 */
0xF0,0x90,0xF0,0x90,0xF0, /* 8 */ 0xF0,0x90,0xF0,0x10,0xF0, /* 9 */
0xF0,0x90,0xF0,0x90,0x90, /* A */ 0xE0,0x90,0xE0,0x90,0xE0, /* B */
0xF0,0x80,0x80,0x80,0xF0, /* C */ 0xE0,0x90,0x90,0x90,0xE0, /* D */
0xF0,0x80,0xF0,0x80,0xF0, /* E */ 0xF0,0x80,0xF0,0x80,0x80, /* F */
};
void chip8_init(Chip8 *c)
{
memset(c, 0, sizeof *c);
memcpy(&c->memory[0x050], font, sizeof font);
c->pc = 0x200;
c->rng = 0x1234abcd;
}
void chip8_load(Chip8 *c, const uint8_t *rom, unsigned len)
{
if (len > 4096 - 0x200)
len = 4096 - 0x200;
memcpy(&c->memory[0x200], rom, len);
}The cycle
One cycle fetches a two-byte big-endian opcode, advances the counter and executes. All the work is in the switch. Some design points: arithmetic on VX and VY must wrap at 8 bits, which the uint8_t type gives you for free as long as you cast back after the operation. The flag register VF must be written after the result, because X might be 15. And the order of that matters in 8XY4, where VX is both an input and, possibly, VF.
#include "chip8.h"
static uint8_t rand8(Chip8 *c)
{
c->rng = c->rng * 1664525u + 1013904223u; /* a small linear congruential generator */
return (uint8_t)(c->rng >> 24);
}
void chip8_cycle(Chip8 *c)
{
uint16_t op = (uint16_t)((c->memory[c->pc] << 8) | c->memory[c->pc + 1]);
c->pc += 2;
unsigned x = (op >> 8) & 0xF;
unsigned y = (op >> 4) & 0xF;
unsigned n = op & 0xF;
uint8_t nn = op & 0xFF;
uint16_t nnn = op & 0xFFF;
switch (op >> 12) {
case 0x0:
if (op == 0x00E0) { /* clear the screen */
for (int p = 0; p < SCREEN_W * SCREEN_H; p++) c->screen[p] = 0;
c->draw = true;
} else if (op == 0x00EE) { /* return from subroutine */
c->pc = c->stack[--c->sp];
}
break;
case 0x1: c->pc = nnn; break; /* jump */
case 0x2: /* call */
c->stack[c->sp++] = c->pc;
c->pc = nnn;
break;
case 0x3: if (c->v[x] == nn) c->pc += 2; break;
case 0x4: if (c->v[x] != nn) c->pc += 2; break;
case 0x5: if (c->v[x] == c->v[y]) c->pc += 2; break;
case 0x6: c->v[x] = nn; break;
case 0x7: c->v[x] = (uint8_t)(c->v[x] + nn); break;
case 0x8:
switch (n) {
case 0x0: c->v[x] = c->v[y]; break;
case 0x1: c->v[x] |= c->v[y]; c->v[0xF] = 0; break;
case 0x2: c->v[x] &= c->v[y]; c->v[0xF] = 0; break;
case 0x3: c->v[x] ^= c->v[y]; c->v[0xF] = 0; break;
case 0x4: {
unsigned sum = c->v[x] + c->v[y];
c->v[x] = (uint8_t)sum;
c->v[0xF] = sum > 0xFF;
break;
}
case 0x5: {
uint8_t no_borrow = c->v[x] >= c->v[y];
c->v[x] = (uint8_t)(c->v[x] - c->v[y]);
c->v[0xF] = no_borrow;
break;
}
case 0x6: {
uint8_t out = c->v[y] & 1;
c->v[x] = c->v[y] >> 1;
c->v[0xF] = out;
break;
}
case 0x7: {
uint8_t no_borrow = c->v[y] >= c->v[x];
c->v[x] = (uint8_t)(c->v[y] - c->v[x]);
c->v[0xF] = no_borrow;
break;
}
case 0xE: {
uint8_t out = (c->v[y] >> 7) & 1;
c->v[x] = (uint8_t)(c->v[y] << 1);
c->v[0xF] = out;
break;
}
}
break;
case 0x9: if (c->v[x] != c->v[y]) c->pc += 2; break;
case 0xA: c->i = nnn; break;
case 0xB: c->pc = (uint16_t)(nnn + c->v[0]); break;
case 0xC: c->v[x] = rand8(c) & nn; break;
case 0xD: { /* draw sprite */
unsigned px = c->v[x] % SCREEN_W;
unsigned py = c->v[y] % SCREEN_H;
c->v[0xF] = 0;
for (unsigned row = 0; row < n; row++) {
if (py + row >= SCREEN_H) break;
uint8_t bits = c->memory[(c->i + row) & 0xFFF];
for (unsigned col = 0; col < 8; col++) {
if (px + col >= SCREEN_W) break;
if (bits & (0x80 >> col)) {
uint8_t *p = &c->screen[(py + row) * SCREEN_W + px + col];
if (*p) c->v[0xF] = 1;
*p ^= 1;
}
}
}
c->draw = true;
break;
}
case 0xE:
if (nn == 0x9E && c->keys[c->v[x] & 0xF]) c->pc += 2;
if (nn == 0xA1 && !c->keys[c->v[x] & 0xF]) c->pc += 2;
break;
case 0xF:
switch (nn) {
case 0x07: c->v[x] = c->delay_timer; break;
case 0x0A: { /* wait for a key */
bool pressed = false;
for (unsigned k = 0; k < 16; k++)
if (c->keys[k]) { c->v[x] = (uint8_t)k; pressed = true; break; }
if (!pressed) c->pc -= 2; /* repeat this instruction */
break;
}
case 0x15: c->delay_timer = c->v[x]; break;
case 0x18: c->sound_timer = c->v[x]; break;
case 0x1E: c->i = (uint16_t)(c->i + c->v[x]); break;
case 0x29: c->i = (uint16_t)(0x050 + (c->v[x] & 0xF) * 5); break;
case 0x33:
c->memory[(c->i) & 0xFFF] = c->v[x] / 100;
c->memory[(c->i + 1) & 0xFFF] = (c->v[x] / 10) % 10;
c->memory[(c->i + 2) & 0xFFF] = c->v[x] % 10;
break;
case 0x55:
for (unsigned k = 0; k <= x; k++) c->memory[(c->i + k) & 0xFFF] = c->v[k];
c->i = (uint16_t)(c->i + x + 1);
break;
case 0x65:
for (unsigned k = 0; k <= x; k++) c->v[k] = c->memory[(c->i + k) & 0xFFF];
c->i = (uint16_t)(c->i + x + 1);
break;
}
break;
}
}
void chip8_tick_timers(Chip8 *c)
{
if (c->delay_timer) c->delay_timer--;
if (c->sound_timer) c->sound_timer--;
}Several of these choices are quirks, and they are worth naming. The logic instructions 8XY1, 8XY2 and 8XY3 reset VF on the original VIP, the shifts 8XY6 and 8XYE read VY and not VX, and FX55 and FX65 increment I. Many later interpreters, such as those for the SUPER-CHIP, behave differently, and a ROM written for one can fail on the other. The code above matches the original; the quirks post shows how to make each switchable.
The draw instruction clips sprites at the screen edge but wraps the starting coordinates, which is also the behaviour of the original machine. VF becomes 1 if any lit pixel is erased by the XOR, which is how games detect collisions.
A text renderer and a test program
A real emulator opens a window, but you can verify the whole thing in a terminal. The function below prints the 64 by 32 buffer as characters.
#include <stdio.h>
#include "chip8.h"
static void render(const Chip8 *c)
{
for (int y = 0; y < SCREEN_H; y++) {
for (int x = 0; x < SCREEN_W; x++)
putchar(c->screen[y * SCREEN_W + x] ? '#' : '.');
putchar('\n');
}
}
int main(void)
{
static Chip8 c;
chip8_init(&c);
/* 6000 V0 = 0 (x)
6100 V1 = 0 (y)
A050 I = font glyph "0"
D015 draw 5 rows at (V0, V1)
1208 jump to self */
const uint8_t rom[] = {
0x60,0x00, 0x61,0x00, 0xA0,0x50, 0xD0,0x15, 0x12,0x08,
};
chip8_load(&c, rom, sizeof rom);
for (int i = 0; i < 6; i++)
chip8_cycle(&c);
for (int y = 0; y < 5; y++) {
for (int x = 0; x < 8; x++)
putchar(c.screen[y * SCREEN_W + x] ? '#' : '.');
putchar('\n');
}
printf("VF=%u pc=0x%03X\n", c.v[0xF], c.pc);
(void)render;
return 0;
}The program draws the built-in digit 0 at the top-left corner. Printing the first eight columns of the first five rows shows the glyph:
####....
#..#....
#..#....
#..#....
####....
VF=0 pc=0x208VF is 0 because nothing was erased: the screen was empty. The counter stops at 0x208, the self-jump, since six cycles executed the four setup instructions and then the jump twice, and 1208 always sets the counter back to 0x208.
Timing and input
The timers decrement at 60 Hz, independent of the instruction rate. The host loop therefore calls chip8_tick_timers once per frame, and runs several cycles per frame so games play at a sensible speed. A typical choice is 10 to 15 cycles per frame, which gives 600 to 900 instructions per second.
#include "chip8.h"
void run_frame(Chip8 *c, int cycles_per_frame)
{
for (int i = 0; i < cycles_per_frame; i++)
chip8_cycle(c);
chip8_tick_timers(c);
/* then: if (c->draw) { render; c->draw = false; } and beep if sound_timer > 0 */
}Input maps the sixteen CHIP-8 keys to the host keyboard. The usual layout takes the left block of a QWERTY keyboard: 1 2 3 4 / Q W E R / A S D F / Z X C V for the keypad's 1 2 3 C / 4 5 6 D / 7 8 9 E / A 0 B F. The host sets c->keys[k] on key down and clears it on key up, and the machine only ever reads the array.
How the draw instruction really works
The draw instruction is the part beginners find hardest, so it is worth reading slowly. A sprite is a column of bytes in memory, starting at the address in I, and each byte is one row of eight pixels, with the most significant bit as the leftmost pixel. DXYN draws N rows at the screen position held in registers VX and VY.
The pixels are combined with the screen by XOR, not by overwriting. A lit sprite pixel flips the screen pixel: off becomes on, and on becomes off. That has two consequences. Drawing the same sprite twice at the same place erases it, which is how games move things without a separate erase step. And an overlap that turns a lit pixel off is detectable, which the instruction reports by setting VF to 1. A game checks VF after drawing the ball or the player to learn that it hit something.
The mask 0x80 >> col is the key expression. For column 0 it is 0x80, which tests the top bit, and for column 7 it is 0x01, which tests the bottom bit. Shifting a single set bit right by one place each iteration walks across the byte from left to right.
When you debug graphics, print the screen after each draw and compare with what the ROM should show. A sprite that looks mirrored means the bit order is reversed, a sprite that is shifted means the coordinates were not wrapped, and a screen that fills with noise usually means I points at the wrong memory.
Testing against a known ROM
Do not trust a CHIP-8 emulator because it runs one game. The community maintains test ROMs, for example Timendus's chip8-test-suite, which display a pass or fail result for each instruction group, including flags and quirks. Run each ROM, compare the screen to the documented expected image and fix the first failure before moving on. Because the instruction set is small, a clean pass over the suite is a strong guarantee.


