z80replacement · a Z80 CPU replacement with an FPGA

The emergency bitstream

The emergency bitstream

Status, 29 November 2026: nobody has built this module yet. The first sample run of 2 boards comes soon. Wait until the project has tested these boards before you send an order or a build request to a factory.

Repository file: docs/safe_bitstream.md

This document is about the emergency bitstream of the safe mode (recovery.md, level 3). The RP2354A loads this bitstream into the FPGA through JTAG when the normal bitstream does not operate correctly.

The bitstream is small and does nothing. It only keeps the module quiet and keeps the JTAG path alive.

What the bitstream does

  • The four translator output enables (aoe_n, doe_n, coe_n and soe_n) are high. The six 74LVC4245A are off, and the host bus is high impedance. The two DIR pins (adir and ddir) are high (A to B), so a translator that is on does not drive the bus.
  • The FPGA side of the bus has constant values: the address is 0, the strobes are high, and the data balls are high impedance. The data balls (bus, SRAM, flash) are input ports with no driver. Do not use assign d = 8'hzz: yosys makes a BIDIR pad with T not connected, and nextpnr then drives the ball low.
  • The SRAM is not selected (CE#, OE# and /WE are high, the data balls are high impedance).
  • The configuration flash U17 is not used. /CS is high and the data balls are high impedance. Thus the SPI pass-through of the JTAG port can write the flash (level 2) while the emergency bitstream operates.
  • The JTAG port is on, as it is a dedicated port. The IDCODE and the USERCODE work. The user register ER1 gives an identification (see below).
  • There is no Z80 core, no debug unit, no console link and no monitor ROM. The level 2 recovery (openFPGALoader) does not need them.
  • The two LEDs blink in turn, about 1.5 Hz. A person can see the safe mode. D2 is on the top side, and D3 is on the bottom side. The host socket hides D3 when the module is in the socket, thus a person sees only D2 there.

The bitstream uses 88 flip-flops, 32 LUT4 and 87 balls on the 85F. No block RAM, no PLL.

How to identify the bitstream

  • USERCODE: 0x5A383045 ("Z80E"). Read it with the JTAG instruction USERCODE. The normal bitstreams have USERCODE 0.
  • ER1 (instruction 0x32): a scan of 64 bits gives 0x5A38304553414645 ("Z80ESAFE"), least significant bit first. The debug unit of the normal bitstream uses the same register ER1. Thus the bridge sees an ER1 that is not the debug unit. The identification lets the host tell the two cases apart.

Files

  • gateware/top/z80r_safe_core.sv: the logic, with no primitive.
  • gateware/top/z80r_safe.sv: the top level (JTAGG and the core). The port list is that of z80r_top, so the same LPF z80r_mg285.lpf applies.
  • gateware/top/tb_safe.sv: the test bench.
  • gateware/top/check_safe_pins.py: the check of the pad cells after place and route.

Build

In gateware/top (the OSS CAD suite on the PATH, see z80fpga/tools/ossenv.sh):

mingw32-make test
mingw32-make safe

test runs the test bench. safe makes, for the 85F and for the 12F:

  • build/safe-85k.pnr.json and build/safe-12k.pnr.json: the netlist after place and route. The build runs check_safe_pins.py on each file and stops if the check fails (see "The pin check").

  • build/safe-85k.bit and build/safe-12k.bit: the bitstream (yosys, nextpnr-ecp5, ecppack $(SRAMOPT) --usercode 1513304133). SRAMOPT is --compress. The file has no master clock setting and no SPI mode command (see "Load time" in fpga.md)

  • build/safe-85k.img and build/safe-12k.img: the image for the partition safe_bitstream, with the header of firmware/src/safe.h (firmware/tools/mk_safe_image.py)

Use the image that fits the FPGA of the module. A bitstream for the 12F does not load into an 85F, and the other way round.

Sizes (compressed, 11 November 2026): 281896 bytes for the 85F and 100795 bytes for the 12F. The images are 281908 bytes and 100807 bytes. The partition has 1 MB (1048576 bytes), with 12 bytes for the header.

The RP2354A loads this bitstream through the JTAG pins only (firmware/src/safe.c). The options --freq and --spimode are for a load from the flash U17, and the emergency bitstream does not have them. --spimode adds the command 79 49 to a bitstream, and a JTAG load into the SRAM of the FPGA can stop with this command.

The pin check

The test bench checks the RTL only. It cannot see what yosys and nextpnr do with a constant z. check_safe_pins.py reads the nextpnr JSON file. It fails if:

  • one of the 20 data balls (d, sram_d, flash_d) is an OUTPUT pad, or a BIDIR pad with T not tied to VCC. These balls must be INPUT pads.
  • one of the 19 balls that must be high (the four enables, adir, ddir, the strobes, flash_cs_n, the SRAM selects, uart_tx) is not an OUTPUT pad with I tied to VCC.

The check fails on the first version of the bitstream (assign d = 8'hzz gave 20 BIDIR pads with T not connected and I = GND, so the balls were low). It also fails if aoe_n is 0. It passes on the present code.

Load the image into the partition

Find the address of the partition, and load the image:

picotool partition info
picotool load -o <address> build/safe-85k.img

The test

The test bench tb_safe.sv checks these items:

  • All translator output enables and DIR pins are high. The address, the strobes, the SRAM pins, the flash /CS and the UART output have their quiet values. The data balls (bus, SRAM, flash) are high impedance. The check runs at the start, and after each of 50 random changes of all inputs. Some changes put an outside driver on the data balls.
  • The two LEDs blink in turn.
  • A scan of ER1 gives the identification.

Not tested without hardware:

  • The load into a real ECP5 with the sequence of safe.c, and DONE.
  • The pin states on the real balls, and the translator state (the check is on the ports of the module, not on the board).
  • The JTAGG primitive: the sampling edge of TDO of ER1 and the capture behaviour. The test bench models these with a simple clock.
  • The USERCODE value that ecppack writes, and its read with JTAG.
  • The write of the flash U17 with openFPGALoader while the emergency bitstream operates.
  • The timing: the design has no input constraint other than the 25 MHz clock, and nextpnr gives more than 290 MHz.

Source of this page: web/ and docs/ in the z80replacement repository. Hardware: CERN-OHL-S-2.0. Other files: GPL-3.0-or-later.