z80replacement · a Z80 CPU replacement with an FPGA
Recovery
the recovery levels, the BOOTSEL button, and the rules that keep them available
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/recovery.md
This document is about the recovery of a module with bad firmware, a bad FPGA bitstream or bad flash contents. It gives the recovery levels, the BOOTSEL button, and the rules that keep each level available.
Summary
| Level | What is bad | Recovery | Status |
|---|---|---|---|
| 0 | The RP2354A firmware (USB does not start) | The BOOTSEL button SW1 and the USB boot loader in the boot ROM | Hardware done |
| 1 | An RP2354A firmware update | A/B firmware images with "try before you buy" (TBYB) | Firmware done, host test only |
| 2 | The bitstream, the selector or a ROM slot in the flash U17 | JTAG through USB: openFPGALoader writes the flash | Works now (test on the board) |
| 3 | A bitstream that configures but does not operate correctly | A safe mode: the RP2354A erases the FPGA and loads an emergency bitstream | Firmware done, host test only. Emergency bitstream done (simulation test and a check of the pads after place and route, no board test) |
Level 0: the BOOTSEL button
SW1 is a C&K KXT311LHS tactile switch (3.0 × 2.0 × 0.6 mm, top actuated). It is on the top side at the USB end: the top side is the only side that you can touch with the module in the socket. SW1 connects QSPI_SS of the RP2354A to GND_USB through 1 kΩ, as the BOOTSEL button of the Pico 2.
To load new RP2354A firmware:
- Push and hold SW1.
- Connect the USB-C cable (or disconnect it and connect it again).
- Release SW1. The computer shows the drive
RP2350. - Copy the UF2 file of the firmware to the drive, or use
picotool load.
The USB boot loader is in the mask ROM of the RP2354A. A bad flash cannot
erase it. The firmware can also read SW1 when it operates (the pico-examples
example picoboard/button shows how), so that SW1 can also start the
safe mode of level 3.
The SWD test pads (SWCLK, SWDIO, RUN) are the last path for the RP2354A, with a Raspberry Pi Debug Probe.
Level 1: firmware images
The 2 MB flash in the RP2354A package can hold two firmware images (A and B) and a recovery image:
- The updater writes a new image into the other partition of the A/B
pair. The boot ROM starts it in TBYB mode. The image must call
explicit_buy()within 16.7 s, after USB starts and after a read of the ECP5 IDCODE. If it does not, the watchdog resets the chip and the boot ROM starts the old image. explicit_buy()erases the first sector of the other partition. Thus the recovery image (DirtyJTAG only) must be a separate partition, not the other half of the A/B pair.
How the firmware does it
firmware/partition_table.json has the partition table, and the build
embeds it with pico_embed_pt_in_binary. The partitions are:
- A and B, 256 kB each (B is the B partition of A). The firmware has about 27 kB.
recovery, 256 kB, for the DirtyJTAG-only image. It is a separate partition, as the text above says.safe_bitstream, 1 MB, for the emergency bitstream of level 3.
The same binary operates in A and in B, because the boot ROM maps the running partition to the start of the XIP window.
The build option -DZ80R_TBYB=ON sets the TBYB flag in the image. Use it
for the update image that goes into the other partition. Without the
option, the image has no TBYB flag, and the load with BOOTSEL (level 0)
does not need explicit_buy().
firmware/src/tbyb.c decides when the image calls explicit_buy(). The
self-test is:
- The host has mounted the USB device (
tud_mounted()). - A fresh read of the ECP5 gives a valid IDCODE (
er1_idcode_ok()). The firmware reads the IDCODE again when the last read is 1 s old. The value is not valid while openFPGALoader uses the JTAG pins. - Both are true for 1 s without a break (
TBYB_HOLD_US).
If the call fails, the firmware tries it again, 3 times at most
(TBYB_MAX_TRIES). Then the firmware does not buy. The boot ROM resets
the chip after 16.7 s and starts the old image. In a normal boot, the
call does nothing.
An update thus needs power on the FPGA side (5 V on the socket, or the USB power option: JP1 and JP2 closed and the eFuse on, usb_power.md). Without power, the IDCODE is not valid, and the image does not buy. The old image then comes back.
The update with picotool uses the other partition:
picotool load -u -v -x z80r_usb.uf2
Not tested on a board: the load, the TBYB start, the reset by the boot
ROM, and explicit_buy() in the flash-update boot.
Level 2: the flash of the FPGA
The JTAG port of the ECP5 is a dedicated port. It is always on, and it has a higher priority than PROGRAMN (Lattice FPGA-TN-02039). The flash contents do not change this. openFPGALoader writes the flash U17 through the SPI pass-through of the ECP5 JTAG port. This needs no correct bitstream. Thus one command through the USB-C port can write the bitstream, the selector sector and each ROM slot again (memory.md).
Conditions:
- The FPGA side must have power: 5 V on the socket, or the USB power
option. For the USB power option, close JP1 (bottom side, +5V from USB)
and JP2 (top side, GND to USB), then turn the eFuse U22 on with
python3 firmware/tools/efuse.py on. The firmware does not turn the eFuse on by itself (usb_power.md). - The RP2354A firmware must operate (level 0 repairs it).
- The test of a write with an empty or a bad flash on the board is to do.
An empty FPGA (no bitstream, or an erased configuration) does not drive the Z80 bus: 10 kΩ pull-ups hold the output enables of the level translators off (bus_interface.md).
Level 3: the safe mode
The automatic golden-image fallback of the ECP5 (dual boot) starts only on a CRC error or on a DONE time-out. A bitstream that configures but does not operate correctly does not start it. Also, you cannot trust a bad bitstream to read a button. Thus the safe mode uses JTAG from the RP2354A:
- The firmware sees SW1 pushed for 5 s, or a USB command.
- It erases the FPGA configuration through JTAG. The bus outputs go off immediately.
- It loads an emergency bitstream directly into the FPGA SRAM. This bitstream does not come from the flash U17. It keeps the bus off and the JTAG path alive, so the flash programming path of level 2 stays open (safe_bitstream.md).
How the firmware does it
firmware/src/safe.c does the safe mode. The triggers are:
- SW1 pushed for 5 s without a break (
SAFE_HOLD_US). The firmware reads SW1 every 10 ms through QSPI_SS (hal_sw1_down()). If you push SW1 at the start of the firmware, release SW1 first. One push gives one start. - A break on the console port (the CDC request SEND_BREAK on interface 1
and 2, for example
send_break()of pyserial). A break on the debug port is still the/CORE_RESETpulse.
The firmware refuses the safe mode while openFPGALoader uses the JTAG pins. There is no automatic trigger: the firmware cannot know that a bitstream does not operate correctly.
The sequence is:
- Check the emergency bitstream in the partition
safe_bitstream: the magicZ80E, the length and a CRC-32. If the image is not valid or not there, the firmware stops. The result isSAFE_BAD_IMAGE, and the FPGA is as it was. - Read the IDCODE. If it is not valid, the FPGA side has no power. The firmware does nothing more.
ISC_ENABLEandISC_ERASE(SRAM). The bus outputs go off. The firmware pollsLSC_CHECK_BUSYfor 2 s at most. If the load fails after this step, the FPGA stays empty. An empty FPGA does not drive the bus.LSC_INIT_ADDRESS,LSC_BITSTREAM_BURSTwith the bitstream (most significant bit of each byte first), andISC_DISABLE.- Wait up to 1 s for DONE.
The firmware writes the result on the console port. After the safe mode, the debug port probes the FPGA again.
The image format is in firmware/src/safe.h. firmware/tools/mk_safe_image.py
makes the image from a compressed bitstream (ecppack --compress).
A bitstream of the LFE5U-85F is 1948588 bytes (about 1.9 MB) without compression. That is larger than the 1 MB partition. With compression, the bitstream of the full design is 408529 bytes. Thus you must compress the emergency bitstream. (Until 11 November 2026 this text said "about 2.4 MB".)
The emergency bitstream is gateware/top/z80r_safe.sv
(safe_bitstream.md): it is 282 kB (281896 bytes) for the 85F, so it
fits in the partition. Not done: the load of the image into the partition
of a real module, and the test of the JTAG sequence on a real ECP5. The host
test uses a model of the ECP5, and the gateware test is a simulation with a check of the pads after place and route.
A first stage in the flash (a small bitstream at address 0 that only selects the main image or the emergency image and jumps to it, as on the apfaudio Tiliqua) is a possible addition. W25Q block protection on that first stage prevents an accidental overwrite.
There is no spare isolator channel (the eight signals use all eight channels of U3 and U4, board_pins.md). Thus the RP2354A cannot pull PROGRAMN. It uses the JTAG REFRESH instruction instead.
First load of a new board
A new board has an empty RP2354A flash and an empty configuration flash U17. The board needs no JTAG adapter and no SWD probe. The RP2354A is the JTAG adapter, and all loads go through the USB-C port. This procedure has no board test yet.
Do the electrical steps 1 and 2 of plan.md, "Phase 4: bring-up", before this procedure.
Build the files first:
- The firmware:
firmware/build/z80r_usb.uf2(firmware.md, "Build"). - The bitstream:
mingw32-make bit-85kingateware/topmakesbuild/top-85k.bitfor the flash U17. - The ROM files:
tools/z80r_romimg.py(memory.md, "The USB tool").
Load the RP2354A firmware:
- Push and hold SW1 (BOOTSEL).
- Connect the USB-C cable to the computer.
- Release SW1. The computer shows the drive
RP2350. - Copy
z80r_usb.uf2to the driveRP2350. The RP2354A then starts the firmware.
Note: an RP2354A with an empty flash starts the USB boot loader without SW1. The boot loader is in the mask ROM of the RP2354A.
Load the bitstream:
-
Apply 5 V to socket pin 11 and GND to socket pin 29 from a current-limited supply. As an alternative, use the USB power option (usb_power.md): close JP1 and JP2, then run
python3 firmware/tools/efuse.py on. -
Read the IDCODE of the ECP5 and the ID of the flash U17. The command must show the LFE5U-85F and a Winbond flash of 128 Mbit.
openFPGALoader -c dirtyJtag --detect -f -
Write the bitstream to the flash U17. With
--verify, openFPGALoader reads the data back and compares the data.openFPGALoader -c dirtyJtag -f --verify build/top-85k.bitNote: openFPGALoader does not write the text header of the
.bitfile. Thus the data in the flash U17 start after the header, and a flash dump is not equal totop-85k.bit. Use--verifyfor the bitstream. -
Remove the 5 V supply and apply the 5 V supply again. The ECP5 then loads the bitstream from the flash U17. Measure DONE: DONE must be high.
Load the ROM:
-
Make the slot file and the selector file, and write the two files to the flash U17. This example puts a ROM in slot 0 and selects slot 0.
python3 tools/z80r_romimg.py --map C003 --ro 0003 --kb 16 --slot 0 rom.bin@0000 -o slot0.bin openFPGALoader -c dirtyJtag -f --verify -o 0x500000 slot0.bin python3 tools/z80r_romimg.py --select 0 -o select.bin openFPGALoader -c dirtyJtag -f --verify -o 0x400000 select.bin -
On the first board, read the slot and the selector back from the flash U17. Compare each dump with its file (memory.md, the CAUTION in "The USB tool").
--file-sizeis the size of the file in bytes.cmpprints nothing when the two files are equal.CAUTION: GIVE A NEW FILE NAME FOR EACH DUMP. openFPGALoader writes the dump into the file of the command line and overwrites that file.
openFPGALoader -c dirtyJtag --dump-flash -o 0x500000 --file-size SIZE slot0-dump.bin cmp slot0.bin slot0-dump.bin openFPGALoader -c dirtyJtag --dump-flash -o 0x400000 --file-size SIZE select-dump.bin cmp select.bin select-dump.bin -
Do a power cycle. The bitstream copies the selected slot into the block RAM, and then the Z80 core starts.
If the firmware load fails, use the SWD test pads (SWCLK, SWDIO, RUN) with a Raspberry Pi Debug Probe. The SWD test pads are the last path to the RP2354A ("Level 0" above). The JTAG pins of the ECP5 have no second path: the JTAG pins connect only to the RP2354A.
When Z80 code removed the debug unit
Z80 code can turn off the debug unit and the other parts with the control
port (z80_control.md). Any reset gives the parts back. The
firmware pulses /CORE_RESET when the host sends a break on the debug
port. The pulse needs the bus clock. Without the bus clock, use a power
cycle or level 3. Level 2 does not depend on the control port.
Rules
- Do not write the OTP of the RP2354A. Do not set secure boot, debug disable or the BOOTSEL disable bits.
- Do not set the security, the password or the one-time options of the ECP5 in a bitstream.
- Do not use the Z80 RESET_N for recovery: the host drives it.
History
Statements that the firmware work made out of date. They stay here as a record.
- Level 1 said "This level is not in the firmware yet." The firmware has TBYB now (see "How the firmware does it" under level 1).
- Level 3 gave the trigger as "SW1 pushed for 5 s, or a USB command" with no firmware. The firmware has both triggers now. The USB command is a break on the console port.
- 2026-11-10: Level 3 said that the emergency bitstream "has a small monitor ROM and the flash programming path", and "Not done: the emergency bitstream itself (gateware)". The bitstream has no monitor ROM and no Z80 core: level 2 does the flash programming through the SPI pass-through of the JTAG port, and the bitstream keeps that path free. The bitstream is done (simulation test only).
- The summary table gave level 1 as "Firmware to do" and level 3 as "Gateware and firmware to do". The table now says what is done.
Other projects
| Project | Recovery |
|---|---|
| picoZ80 | No buttons. BOOTSEL is a header pin that you connect to GND with a jumper. Two firmware slots with a checksum, updates through Wi-Fi into the inactive slot. No golden image. |
| tranZPUter Fusion | A JTAG header only |
| apfaudio Tiliqua | ECP5 and RP2040: a boot loader bitstream at address 0, the RP2040 reconfigures the ECP5 through JTAG |
Source of this page: web/ and docs/ in the
z80replacement repository. Hardware: CERN-OHL-S-2.0. Other files: GPL-3.0-or-later.