Skip to content
Lesson 13 of 13

Step 1 of 5 · Reading · ~4 min

Read

Production

Beyond CHIP-8

Your CHIP-8 emulator has the skeleton of every emulator ever written: a CPU that executes instructions, memory, a few devices, and a main loop that decides how much time each one gets. After this lesson you can give the CPU its exact share of every frame even when the speed does not divide evenly, and you will know what changes when the machine you emulate is a Game Boy or an NES.

The scheduling problem

A frame is 1/60 of a second. A CPU that runs c instructions per second should run c / 60 of them per frame. For c = 500 that is 8.33. The easy fix is to run 8 per frame, but 8 times 60 is 480: the emulated machine is 4 percent slow, forever, and a game tuned for 500 feels sluggish. Rounding up is just as wrong in the other direction.

The fix is to carry the remainder. Keep a budget. Each frame, add the speed to it, spend as many whole sixtieths as it holds, and keep what is left over:

each frame:
    budget = budget + c
    n      = budget div 60       (whole instructions this frame)
    budget = budget mod 60       (the part that did not fit)
    run n instructions

Worked trace for c = 100, a speed this lesson's tests do not use:

frame  budget before  + c   n   budget after
1      0               100   1   40
2      40              140   2   20
3      20              120   2   0
4      0               100   1   40      the pattern 1, 2, 2 repeats

Five instructions every three frames is 100 per second exactly. Everything is integer arithmetic, so there is no drift and the result is the same on every machine; a floating-point version would pick up rounding error and could not be reproduced in a test. The timers still tick once per frame, however many instructions the budget gave that frame.

The same loop, bigger machines

Nothing about the loop changes for larger machines; the numbers do.

  • Game Boy. The CPU runs 4,194,304 cycles per second. One frame is 154 scanlines of 456 cycles, 70,224 cycles, about 59.7 frames per second. So the frame rate is not even 60, and the emulator has to decide whether to run at the host's refresh rate or the machine's.
  • NES. The CPU runs at roughly 1.79 MHz on an NTSC console, and the picture chip steps three times for every CPU cycle. A frame is 262 scanlines of 341 dots, which works out to about 29,780.5 CPU cycles. The half cycle is why NES emulators count in picture-chip dots, where the number is whole.

Both machines add devices that CHIP-8 never had: a bus that routes each address to RAM, ROM or a device register, cartridge hardware that swaps banks of ROM into the address space, a picture chip that draws tiles and sprites on its own schedule, and a sound chip. The central design question is how closely the devices must stay in step. Running the CPU for a whole instruction and then letting the other chips catch up is simple and fast; it breaks games that change the picture mid-scanline. Cycle-accurate emulation steps every chip in lockstep and is far slower, but plays the awkward games correctly.

Two techniques make big emulators fast. Dynamic recompilation translates guest instructions into host machine code once and reuses it. High-level emulation skips emulating hardware and reproduces what the software asked of it. Your whole machine is already one struct of registers and memory, which also makes save states easy: copying that struct is a snapshot.

Common mistakes

  • Dividing the speed by 60 and dropping the remainder, which makes every frame the same size and the emulator slow.
  • Resetting the budget when the speed changes. The leftover belongs to the machine, not to the speed setting.
  • Using floating point for the budget.
  • Ticking the timer per instruction instead of per frame.

Your exercise

Write the frame function of a scheduler. The starter reads commands to set the speed and the delay timer and to run frames; you decide how many instructions each frame gets and keep the books.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…