An implementation of a CHIP8 interpreter, written entirely in TypeScript.
If you want to see a demo of this in action, check out my Svelte Chip8 frontend which uses this module to display the screen output and take user input directly within a browser.
Install using NPM;
npm i typescript-chip8
import { createCpu } from "typescript-chip8";
let cpu = createCpu();
// Roms are buffers of big-endian 2-byte instructions. You can hard code these...
const rom = Buffer.from([0x60, 0xaa, 0x61, 0xbb, 0x62, 0xcc]);
/*
// Or fetch from a remote source somewhere...
const rom = await fetch(`http://example.com/roms/HelloWorld.ch8`)
.then((r) => r.arrayBuffer())
.then((data) => new Uint8Array(data));
*/
cpu.load(rom);
// perform 1 CPU cycle (you could put this in a loop/interval to "run" the cpu forever)
cpu.cycle();
This emulator is 100% unit tested and covered. Run tests with either of the following;
npm run test
npm run test -- --watch
This would not have been possible if not for these excellent guides;
- Cowgod's Chip-8 Technical Reference v1.0 - the main source I used when building this emulator. Covers every opcode.
- Tania Rasca's emulator guide - this guide is absolutely amazing and without access to Tania's code to check I was doing things correctly, I probably would not have completed this project.
- David Winter's Chip8 page - David is the author of many chip8 roms as well as one of the earliest emulators.