The R8 CPU

 
 

R8 is a CPU architecture and instruction set designed to be easy to learn and understand, especially for computer systems engineering students and those writing emulators.

Origins

I’m about the same age as the microprocessor, though I seem to be slowing down while it’s only getting faster. All the same, we grew up together, and early chips like the Z80 and 6502 were my childhood companions. I never quite got my head around things like assembly-language programming at the time, though I valiantly attempted to write machine code by hand by POKEing numbers into REM statements.

Understanding at least something about how a CPU works turned out to be pretty helpful in my future career, though. One of the most enjoyable and enlightening parts of my computer science degree course was writing an emulator for a (laughably) simple processor, and it really helped me get the hang of things like memory, opcodes, and machine language.

Now I’m on the teaching side (which would no doubt greatly amuse my former professors), and I use the same project with my software engineering students: build a simple emulator for a simple machine.

I’ve found that real CPUs aren’t a good first choice: the Z80 is too complicated, the 6502 is too constrained, and so on. On the other hand, toy CPUs with just seven or eight instructions are boring. What we need is a realistic CPU that’s also not a nightmare to emulate.

So I designed my own. The R8 is a lovingly-detailed retro fantasy CPU architecture that might have been made in 1976, but wasn’t. I call it a “fan fiction” design. I’ve tried to combine the power of the Z80, the simplicity of the 6502, and some of the neat features of the 68000. Enthusiasts of these and similar chips will find the R8 architecture very familiar. Yet it’s also very logical, very regular, and very easy to emulate.

Here’s what the assembly language looks like:

PRINT_BYTES_FREE:
    ld ab, sp     ; highest usable RAM address
    clc
    add b, 0x01   ; bytes free is one more than that
    add a, 0x00   ; propagate carry
    sec
    sub a, 0x01   ; less 256 bytes for trap table, etc
    call PRINT_HEX
    ld cd, READY_MSG
    call PRINT_STRING

Architecture

The R8 has eight 8-bit general-purpose registers A, B, C, D, E, F, G, and H. These can also be addressed in pairs, as the 16-bit registers AB, CD, EF, and GH.

Arithmetic and logic operations are 16-bit, and the chip has a 16-bit address space, with an 8-bit data bus.

R8 block diagram

Comparisons

The R8’s closest architectural relatives are the Zilog Z80 and MOS 6502. Like the Z80, the R8 is a pointer-flavoured machine that likes to use its 16-bit registers to address memory. Unlike the Z80, though, the R8 has a more regular instruction set. For example, where the Z80 can only address a memory operand through the HL register, the R8 can use any register pair for this purpose.

Like the 6502, the R8 has a minimalist instruction set, making it easy to implement either in hardware or in software. Unlike the 6502, though, the R8 has much more register space (8 bytes compared to 3), eliminating the need for a zero-page addressing mode.

Technical information

  • R8 Programmer’s Manual — the definitive technical documentation for the R8 architecture, instruction set, and assembly language
  • R8 Opcode Table — quick-reference cheat sheet for the R8 instruction set
  • Welcome to the Machine (coming soon) — a book guiding readers through building an R8 emulator from scratch, step by step, with example code in both Go and Rust

Tutorials

A guided introduction to building a high-level R8 emulator can be found here:

Libraries

Software packages of interest to R8 emulator developers:

  • r8cpu — core types for R8 emulators, such as registers, flags, and instruction kinds (Rust)

Tools

Utilities for those writing R8 programs or implementations.

  • r8asm — an assembler / disassembler for R8

Other resources

  • awesome-r8 — a GitHub repo collecting together all the known R8 emulators, tools, and support packages