Skip to content
avwohlPublic

About

A C compiler for the Z80 processor and CP/M. It optimizes for small code size, and it made smaller binaries than z88dk in all 47 tests of a public test suite.

Topics

Resources

Stars

14 stars

Watchers

2 watching

Forks

Repository files navigation

uc80 - ANSI C Compiler for Z80

A C compiler targeting the Z80 processor and CP/M operating system. Produces assembly compatible with the um80 assembler and linker toolchain.

Installation

pip install uc80

Or from source:

pip install -e .

Requires the um80 assembler/linker toolchain:

pip install um80

Quick Start

# Compile, assemble, and link a C program
LIB=$(uc80 --print-lib-dir)
uc80 hello.c -o hello.mac
um80 hello.mac -o hello.rel
ul80 hello.rel $LIB/libc.lib $LIB/runtime.lib -o hello.com

uc80 --print-lib-dir prints the library directory. In a git checkout, run uc80 --build-libs once first. Details are in docs/libraries.md.

For the smallest binaries, compile all .c files in one invocation, so uc80 can optimize the whole program. Separate compilation needs --no-whole-program and crt0.rel; see docs/compiler_options.md.

Features

  • ANSI C (C11/C23) with most standard features
  • Z80 code generation with peephole optimization
  • IEEE 754 single-precision float
  • Configurable integer sizes (--int=16|32, --long=32|64); default is 16-bit int, 32-bit long, 64-bit long long
  • Structs, unions, bitfields, enums
  • Full preprocessor (#include, #define, #if, #pragma, etc.)
  • Modular library with selective linking
  • Whole-program optimization
  • Basic inline assembly (asm("...")), emitted verbatim and never optimized
  • CP/M console line endings (CR LF by default, --no-crlf for raw LF)
  • CP/M target with embedded crt0
  • Libraries ship in the wheel and are discoverable (uc80 --print-lib-dir, uc80.lib_dir())

Binary Size and Tests

uc80 produces the smallest known binaries for Z80/CP/M among current compilers. On the Fujitsu compiler-test-suite, uc80 output is less than half the size of z88dk output. uc80 passes 220 of the 220 c-testsuite tests. Numbers are in docs/benchmarks.md.

Documentation

  • Finding the libraries - --print-lib-dir, --build-libs, UC80_LIB_DIR
  • Compiler options - whole-program optimization, --printf, CR LF line endings, --int/--long, separate compilation
  • Calling convention - register and stack convention, which functions qualify, --no-register-cc
  • Register locals - up to four byte locals (or two words) of a function live in B, C, D and E, and one pointer lives in IY, --no-register-locals
  • Address CSE - the compiler computes a repeated array element address once, a = b = K stores K to each target, --no-addr-cse, --no-chain-store
  • Dead locals - the compiler removes a local that nothing reads, and a store that a later store overwrites, --no-dead-store
  • Inline block operations - memset and memcpy with a constant size become LDIR or LDI code, --no-blockops
  • Pair peephole rules - a discarded comma computes no value, copies between BC, DE and HL go away, --no-discard-comma, --no-peep-pairs
  • Loop-invariant code motion - an integer expression that a loop does not change is computed before the loop, --no-licm
  • Value ranges - a counter in 0 to 32767 uses the short unsigned compare, --no-range-cmp
  • Index narrowing - a 32-bit array index is computed in 16 bits, --no-narrow-index
  • Family J passes - fast BSS clear, putchar statements, loop rotation, fill loops, parameters in registers, loads out of loops, byte registers, and more: one page for each, one --no- switch for each
  • Family K step 1 - less library code in the linked image: printf("\n"), 32-bit and 16-bit divide, qsort of a few elements, one --no- switch for each
  • Family L step LA - a static pointer to one object becomes a fixed address, a word qsort with a register comparator, no call of an empty function, one divide for x / y and x % y, one --no- switch for each
  • Family L step LB - external functions in registers (#pragma uc80 regcall), a char result in L, cheaper compares of register locals, smaller ROM kernels
  • Family K step 2 - smaller generated code: counted for loops, if/else arms, a liveness pass, divide and puts routines, one --no- switch for each
  • Inline assembly - asm("...") rules and limits
  • Volatile objects - every volatile access happens as written, and how each pass keeps that
  • Debug markers - -g (0.8.0 and later) writes source line markers for a debugger, and the code stays the same
  • optnone - --optnone, #pragma uc80 optnone and [[uc80::optnone]] keep one function unoptimized for a debugger (0.8.0 and later)
  • Binary size and test results - speed and size against 8 compilers, and external test suite pass rates
  • C standard compliance - implemented and missing C features
  • Compile time - why long expressions and large programs compiled slowly, and the repairs
  • Build a Z80 ROM - --target=rom, crt0, vectors, ul80 ROM link, libc_rom.lib, limits
  • Interrupt functions and hardware access - #pragma uc80 interrupt, <z80.h> port I/O, critical sections, __at, SDCC __interrupt and __sfr
  • Neo Geo sound driver example - NMI command, timer IRQ, YM busy wait and bank select in one C file
  • ROM test runner - run a Z80 ROM, log port I/O and T-states, inject IRQ and NMI
  • Types and conversions - enumeration types, tags in blocks, bit-field assignment, float and integer conversions
  • Float status - IEEE 754 single-precision support
  • printf, program exit and files, console output - conversions and limits, atexit, fopen modes, ftell and fseek, TAB
  • Math library, scanf and strtol, library modules in C, float interpreter - accuracy, limits, how the modules are made
  • CHANGELOG - release history

Related Projects

  • 80un - Unpacker for the CP/M archive and compression formats LBR, ARC, squeeze, crunch, and CrLZH.
  • cpmdroid - Z80/CP/M emulator for Android phones and tablets. It emulates the RomWBW HBIOS interface and a VT100 terminal.
  • cpmemu - Z80/CP/M emulator for Linux and Windows, with Z80 and 8080 CPU cores. It translates the BDOS and BIOS calls of CP/M 2.2 programs to the host file system.
  • ioscpm - Z80/CP/M emulator for iOS and macOS. It emulates the RomWBW HBIOS interface and runs CP/M 2.2 and CP/M 3.
  • learn-ada-z80 - Collection of more than 90 Ada example programs for uada80, the Ada compiler for the Z80 processor and CP/M.
  • mbasic - Python interpreter for MBASIC 5.21, the Microsoft BASIC-80 for CP/M. Two compiler backends compile the programs to CP/M .COM files or to JavaScript.
  • mbasic2025 - Reconstruction of the lost source code of MBASIC 5.21, the Microsoft BASIC-80 for CP/M. The MACRO-80 source code assembles to a binary that matches mbasic.com byte for byte.
  • mbasicc - C++17 interpreter for MBASIC 5.21, the Microsoft BASIC-80 for CP/M. It runs on Linux and macOS.
  • mbasicc_web - Web browser interpreter for MBASIC 5.21, the Microsoft BASIC-80 for CP/M. Emscripten compiles the mbasicc interpreter to WebAssembly.
  • mpm2 - Z80 emulator for MP/M II, the multi-user CP/M operating system. Users connect over SSH, and SFTP clients transfer files.
  • romwbw_emu - Hardware-level Z80/CP/M emulator for Linux and macOS. It emulates the RomWBW HBIOS interface and switches banks in 512 KB of ROM and 512 KB of RAM.
  • scelbal - Floating-point BASIC interpreter for the 8080 processor and CP/M. A translator converts the original 8008 source code to 8080 source code.
  • uada80 - Ada compiler for the Z80 processor and CP/M 2.2. It compiles a subset of Ada 2012 to CP/M .COM files.
  • uc386 - C23 compiler for the i386 processor and MS-DOS. This sibling backend shares the uc_core frontend.
  • uc_core - Shared C23 frontend and AST optimizer for the uc80 and uc386 compilers.
  • ucow - Cowgol compiler for the Z80 processor and CP/M. It runs on Linux in Python.
  • um80_and_friends - Linux toolchain that is compatible with Microsoft MACRO-80. It has an assembler, a linker, a librarian, and a disassembler.
  • upeepz80 - Peephole optimizer for Z80 compilers. It shortens jumps to jr, builds djnz loops, and removes dead stores.
  • uplm80 - PL/M-80 compiler for the Z80 processor and CP/M. It writes Intel 8080 and Zilog Z80 assembly language.
  • uplox - LR(1) and GLR parser generator. It writes the lexer and parser tables for the C23 frontend of uc_core from examples/c23.uplox.
  • z80cpmw - Z80/CP/M emulator for Windows. It emulates the RomWBW HBIOS interface and boots CP/M from disk images.

License

GPL-3.0-or-later. See LICENSE.

About

A C compiler for the Z80 processor and CP/M. It optimizes for small code size, and it made smaller binaries than z88dk in all 47 tests of a public test suite.

Topics

Resources

Stars

14 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages