An Introduction to Chip-8/Octo

F8Z

A Pixels.camp lightning talk by João Costa / @JD557

Overview

  • What is Chip8
  • What is Octo
  • Awesome for Emulator Developers
  • Awesome for Game Developers
  • References

What is Chip-8

  • Simple interpreted bytecode created by Joseph Weisbecker in the 70's
  • Designed with video games in mind
  • Spawned multiple variants (SuperChip, XO-Chip,...)
  • Someone has probably already written an interpreter for your toaster
Space Invaders title screen
Telmac 1800
Garry's mod Chip-8 emulator

What is Octo

  • High-level assembler for the Chip-8 VM
  • Includes an online version (https://johnearnest.github.io/Octo/) with:
    • Assembler
    • Disassembler
    • Emulator/Interpreter
    • Sprite Editor
    • Social Sharing
Octo screenshot
Octojam logo

http://www.awfuljams.com/octojam-iii

Awesome for Emulator Developers

  • Simple architecture
    • Simple instruction set
    • Simple registers
    • Simple memory layout
    • Simple keyboard
    • Simple display
    • Sound = Buzzer
  • Lots of games to test with

Instruction Set

  • RISC: 34 Instructions
  • Fixed Instruction size: 2 bytes (4 nibbles)
  • All instructions are 16-bit aligned

Registers

  • 16x 8-bit general purpose registers: V0 - VF (VF also holds the carry flag)
  • 1x 16-bit program counter: PC
  • 1x 8-bit address pointer: I
  • 1x 8-bit stack pointer: SP
  • 2x 8-bit timed registers (Dec. at 60Hz): DT (Delay) and ST (Sound)

Keyboard

keyboard
BytecodeCommandDescription
Ex9ESKP VxSkip instruction if Vx is pressed
ExA1SKNP VxSkip instruction if Vx is not pressed
Fx0ALD Vx KWaits for a key and stores it in Vx

Memory Layout

  • 4096 Bytes
  • 0x000 - 0x1FF: Reserved space (512 Bytes)
    • Includes 16 8x5 character sprites (0-F)
  • 0x200 - 0xFFF: Program + Data (3584 Bytes)
    • Program starts at 0x200

Display

  • 64x32 monochrome display
  • 8x7 sprites (variable number of lines)
  • Rending a sprite = XOR
F8Z

Awesome for Game Developers

Octo's Syntax

Octo's syntax looks like a high-level programming language

:alias px v3
:alias py v4
: main
	px := random 0b0011111
	py := random 0b0001111
	i  := person
	sprite px py 8
	loop
		sprite px py 8
		v0 := 5   if v0 key then py += -1 # keyboard W
		v0 := 8   if v0 key then py +=  1 # keyboard S
		sprite px py 8
	again
: person
	0x70 0x70 0x20 0x70 0xA8 0x20 0x50 0x50
            

Registers

  • 16x 8-bit general purpose registers (no need to spill a lot)
  • Constant framerate is easy using DT (delay timer)

Special Instructions

BytecodeCommandDescription
00E0CLSClears the screen
DxynDRW Vx Vy nDraws a sprite (I) with n lines at (Vx,Vy)
CxkkRND Vx kStores a masked random number in Vx (rand AND k)
Fx29LD F VxPoints I to a sprite with the character Vx (0 to F)
Fx33LD B VxStores the BCD representation of Vx in memory

Special Instructions (in Octo)

CommandDescription
clearClears the screen
sprite vx vy nDraws a sprite (I) with n lines at (Vx,Vy)
vx := random kStores a masked random number in Vx (rand AND k)
i := hex vxPoints I to a sprite with the character Vx (0 to F)
bcd vxStores the BCD representation of Vx in memory

Display

  • The draw instruction includes collision detection
  • No need to worry about VSync
  • No need to count instructions (usually)

References