CHIP-8 JIT

Jonas

TU Wien

2023

CHIP 8 Introduction

  • Virtual machine developed in the 70s.
  • Describes hardware (input/output) and bytecode instructions.
  • Tons of internet resources, differing in some instruction quirks.
  • Lots of interpreters, some JITs (none seemed fully working at the time).

Hardware/Memory Specification

Keypad

COSMAC VIP keypad

Display (64 x 32)

CHIP-8 display example
  • 4kB memory (3584 bytes usable).
  • About 500 Hz clock speed is sufficient for most games.
  • 16 registers.
  • 2 timers (sound, delay).
  • 32-byte program stack (for function calls).

Instructions

35 instructions

  • Arithmetic (8-bit unsigned).
  • Program flow (absolute jump, relative jump, conditional, call, return).
  • Special instructions (draw, rnd, bcd, load, store).

Load Fx55 - LD I, Vx

Store registers V0 through Vx in memory starting at location I.

Dxyn - DRW Vx, Vy, nibble

Display n-byte sprite starting at memory location I at (Vx, Vy), set VF = collision.

Sprites are XORed onto the existing screen. If this causes any pixels to be erased, VF is set to 1, otherwise to 0.

It is Working!

Components

  • Graphical frontend and input (SFML, threaded).
  • Parser:
    • Find blocks (end at jump/function call).
    • Gather used registers in block.
  • Compiler (ASMJIT):
    • Register allocation.
    • Calling conventions.
  • Dispatcher:
    • Handles dirty blocks (writes within current block).

Memory Layout

Memory layout with basic blocks

Memory Layout

Memory layout write invalidation

Speed Comparison

Runtime comparison between JIT and interpreter

Compile Percentage

Compile versus runtime percentage chart