z80replacement · a Z80 CPU replacement with an FPGA

RP2354A firmware

the RP2354A firmware: build, host test, USB functions

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/firmware.md

The firmware in firmware/ runs on the RP2354A of the module. It makes one USB device with three functions: a JTAG adapter for openFPGALoader, the console port, and the debug port for GDB and udb80. usb_power.md gives the design, and debugger.md gives the debug path.

STATUS: BUILT AND TESTED ON THE HOST ONLY. The host test has 213 checks. The firmware compiles for the RP2354A, and a host test examines its JTAG logic on a model of the ECP5 TAP. No board test is done yet.

CAUTION: firmware/src/board.h has the GPIO numbers of board_pins.md, the pin list for the schematic. The schematic (schematic.md) uses these GPIO numbers. check_sch.py compares the schematic with the isolator table of board_pins.md (PASS on 29 November 2026). If the schematic changes a GPIO, change board.h and board_pins.md together.

Tools

Tool Version Use
pico-sdk 2.1.1, with its submodules (TinyUSB 0.18) SDK and USB stack
Arm GNU Toolchain (arm-none-eabi) 14.2.Rel1 Compiler for the RP2354A
CMake 3.13 or later Build files
Ninja any Build
A host C compiler (gcc or cc) any picotool, and the host test

The pico-sdk downloads and builds picotool at the first build. picotool makes the UF2 file.

Install the tools

On the Windows test computer the tools are in C:\temp\tools:

  1. Get the pico-sdk:

    git clone --depth 1 --branch 2.1.1 https://github.com/raspberrypi/pico-sdk.git C:/temp/tools/pico-sdk
    cd C:/temp/tools/pico-sdk
    git submodule update --init --depth 1
    
  2. Download the zip of the Arm GNU Toolchain 14.2.Rel1 for mingw-w64-x86_64-arm-none-eabi from developer.arm.com.

  3. Compare the SHA-256 of the zip with the .sha256asc file from the same page.

  4. Extract the zip into C:\temp\tools\arm-gnu-14.2. The zip has no top folder, so extract it into an empty folder.

On macOS and Linux, use the same pico-sdk clone and the Arm GNU Toolchain for that system.

Build

Put the bin folder of the toolchain first on PATH. Then, in firmware/:

cmake -S . -B build -G Ninja -DPICO_SDK_PATH=C:/temp/tools/pico-sdk -DCMAKE_BUILD_TYPE=Release
cmake --build build

The result is build/z80r_usb.uf2. The build uses -Werror, so a warning stops the build. The code is about 27 kB, and the flash is 2 MB.

The build embeds the partition table firmware/partition_table.json. The partitions are A, B, recovery and safe_bitstream (recovery.md). For an update image that uses try before you buy, add -DZ80R_TBYB=ON.

To load the firmware, hold the BOOTSEL input of the RP2354A low during a reset. The RP2354A then shows a USB drive. Copy the UF2 file to the drive. The DirtyJTAG command GOTOBOOTLOADER (08H) also restarts the RP2354A in this mode.

Host test

The host test runs tap.c, djtag.c and er1.c on a model of the ECP5 TAP (firmware/test/hal_host.c). The model has the instruction register, IDCODE, BYPASS, ER1 with the link rules of z80fpga dbg_jtag_link.sv, DONE and the power of the Z80 side. The debug unit of the model takes the bytes slowly at random times, so the firmware sees RJ.

cd firmware/test
make            (mingw32-make CC=gcc on Windows)

The test examines these items:

  1. The IDCODE through the TAP code.
  2. The debug port: 3000 random bytes to the unit and back, in order, with rejects. Also a slow unit, and a host port with little room.
  3. Power: when the Z80 side has no power, the firmware reports it and makes no scans. When the power comes back, the firmware reports it, and the debug port operates again.
  4. DONE: no scans until DONE is high.
  5. DirtyJTAG: the packets of openFPGALoader (dirtyJtag.cpp) read the IDCODE. A command in two parts. No ER1 scan while openFPGALoader uses the pins, and the debug port operates again after the idle time.
  6. EFUSE_FLT_N: hal_board_init() turns on the pull-up of GPIO 13. The eFuse policy: the eFuse is off at reset. The eFuse does not turn on without a command. A fault shorter than 20 ms does not turn the eFuse off. A fault of 20 ms does, one time. The trip stays until a command clears the trip.
  7. The safe mode. The trigger (SW1 for 5 s, release before the next start, the USB command). The check of the image (magic, length, CRC-32). The JTAG sequence on the model of the ECP5: erase, wait for busy, load the stream, DONE. The cases with no power, a failed load, and a bad image or no partition (the FPGA is not touched). The decode of the partition location word of the boot ROM.
  8. Try before you buy: no buy without USB or without the IDCODE. A buy after 1 s of both. A loss restarts the time. 3 tries at most.
  9. The IDCODE flag for the self-test follows the power. It is not valid while DirtyJTAG is active, and it is not stale when data streams without a gap.

The last line is PASS or FAIL.

USB functions

Interface Class Endpoints Function
0 Vendor-specific, DirtyJTAG OUT 01H, IN 82H JTAG adapter for openFPGALoader
1, 2 CDC ACM with an IAD 83H, 04H, 84H Console port
3, 4 CDC ACM with an IAD 85H, 06H, 86H Debug port

The VID is 1209H and the PID is C0CAH, the IDs of DirtyJTAG. Thus openFPGALoader finds the module with -c dirtyJtag and no other options. MS OS 2.0 descriptors give interface 0 the compatible ID WINUSB. Thus Windows uses WinUSB for interface 0 with no driver installation, and libusb can open interface 0. The two CDC ports use the CDC driver of the system.

DirtyJTAG

The firmware answers CMD_INFO with DJTAG2. With version 2, each command of openFPGALoader fits in one packet of 64 bytes. The firmware does the commands STOP, INFO, FREQ, XFER (with NO_READ and EXTEND_LENGTH), SETSIG, GETSIG, CLK and GOTOBOOTLOADER.

The highest TCK is 6 MHz (TCK_MAX_KHZ in board.h). TDO goes through the isolator two times before the firmware samples it (usb_power.md, "JTAG clock"). A higher FREQ gives 6 MHz.

Debug port

The debug port moves the bytes of the CDC port on interfaces 3 and 4 through the link register of the debug unit on ER1. The rules are those of JtagLink in z80fpga tools/z80dbg_bridge.py. The bridge opens the debug port with --serial, and the bridge needs no change.

  • At the start, the firmware reads the IDCODE. If the IDCODE is all 0 or all 1, the Z80 side has no power. If the Z80 side has power and DONE is high, the firmware loads ER1 (32H) into the instruction register.
  • The firmware sends the bytes from the host in batches of up to 32 scans. After a rejected byte, the firmware sends one scan with HV at 0, and then sends again from the rejected byte.
  • With no bytes to send, the firmware polls with HV at 0. The time between polls starts at 50 µs and goes up to 1 ms.
  • When the host does not read the replies, the firmware stops the scans. The bytes then wait in the FPGA.
  • After 1 s with no bytes, the firmware does the IDCODE check again. The check finds a loss of power, and the firmware reports the loss on the console port.

openFPGALoader and the debug port use the same JTAG pins. The first DirtyJTAG command gives the pins to openFPGALoader. The debug port then keeps its bytes. After 500 ms with no DirtyJTAG command, the debug port waits for DONE, does the IDCODE check and loads ER1 again.

Console port

The console port connects the CDC port on interfaces 1 and 2 to the console UART in the FPGA (console.md). The link to the FPGA always operates at 115200 baud, 8N1 (CONSOLE_BAUD in board.h). The baud rate that the terminal program sets has no effect. The console port has no flow control. When the Z80 side loses its power, the firmware writes a message on the console port. The firmware writes a second message when the power comes back.

Recovery pulse

A break on the debug port (the CDC request SEND_BREAK, for example send_break() of pyserial) makes the firmware pulse /CORE_RESET low for CORE_RESET_PULSE_US (100 µs). The pulse gives back the parts that Z80 code removed (z80_control.md). The pulse does not drive the bus /RESET. The gateware sees the pulse only while the bus clock runs. tud_cdc_send_break_cb in main.c does this, and hal_core_reset() in hal.h makes the pulse.

Board inputs

hal_board_init() runs at the start. GPIO 13 reads EFUSE_FLT_N, the open-drain fault output of the eFuse U22. The board has no pull-up resistor for this signal (board_pins.md), so the function turns on the internal pull-up of GPIO 13. hal_efuse_fault() returns true when the pin is low. hal_efuse_enable() drives GPIO 12 (EFUSE_EN).

eFuse

efuse.c controls the eFuse U22 of the USB power option (usb_power.md). The policy:

  • The eFuse is off at reset. efuse_init() drives EFUSE_EN low. Nothing turns the eFuse on by itself. The reason is in usb_power.md: with JP1 closed and a host with its own supply, two supplies would meet.
  • Two USB vendor requests (efuse.h) control the eFuse. E0H is a host-to-device request: wValue 1 turns the eFuse on, wValue 0 turns the eFuse off. E1H is a device-to-host request that returns one byte: bit 0 is the state of EFUSE_EN, bit 1 is the level of EFUSE_FLT_N (1 = low = fault), and bit 2 is "tripped". The requests use interface 0 (WinUSB) and wIndex 0. firmware/tools/efuse.py status|on|off sends them (needs pyusb).
  • The firmware accepts E0H only in the exact form: wValue 0 or 1, wIndex 0 and no data stage (wLength 0). All other forms of E0H get a STALL, and the state of the eFuse does not change (tud_vendor_control_xfer_cb in usb_descriptors.c).
  • An on command while the eFuse is on with no fault changes nothing. EFUSE_EN gets no pulse, so the supply of the host stays on.
  • In the other cases, an on command sets EFUSE_EN low for 100 µs first. These cases are an eFuse that is off, and a fault on EFUSE_FLT_N. The TPS259470A clears a latched fault when EN goes below its shutdown threshold.
  • Assume that the eFuse is on and EFUSE_FLT_N stays low for 20 ms (EFUSE_FAULT_US). Then the firmware turns the eFuse off and sets "tripped". The firmware also writes a message on the console port. The eFuse stays off until the next command. The TPS259470A (auto-retry variant) retries by itself after 110 ms. The firmware does not wait for the retry.
  • The safe mode does not change the state of the eFuse. The fault check also runs during the safe mode: usb_yield() in main.c calls efuse_task(). Thus a fault of 20 ms turns the eFuse off during the safe mode too. In that case the firmware writes no message on the console port, and bit 2 of E1H shows the trip.

Known limits:

  • The eFuse stays on when the host suspends the USB port, and when the firmware stops. The firmware has no watchdog and no suspend function for the eFuse.
  • The firmware does not read CC1 and CC2. See usb_power.md.
  • The USB descriptor declares 500 mA. The current limit of the eFuse is 1.5 A.
  • A firmware update with try before you buy needs care when the Z80 side has power only through the eFuse. The new image starts with the eFuse off, so the FPGA has no power and gives no IDCODE. The host must send the on command again immediately after the new image starts. The image calls explicit_buy() only after a valid IDCODE for the hold time of 1 s (TBYB_HOLD_US in tbyb.h, used in tbyb.c). The boot ROM resets the chip 16.7 s after the start of the new image. Without the on command in time, the boot ROM goes back to the old image.

hal_sw1_down() reads SW1. SW1 is on QSPI_SS, which is the chip select of the flash. The function runs from RAM with the interrupts off, and it overrides the pin for a short time. The main loop calls the function every 10 ms.

Safe mode

A push of SW1 for 5 s, or a break on the console port, starts the safe mode of recovery.md, level 3. safe.c erases the FPGA through JTAG and loads the emergency bitstream from the flash partition safe_bitstream. safe_run() checks the image first: a bad or absent image leaves the FPGA as it was.

safe_run() calls the USB stack every 4 kB. The result goes to the console port. Then the debug port probes the FPGA again.

Try before you buy

tbyb.c decides when the image calls explicit_buy(): when the host has mounted the USB device, and a fresh read gives a valid IDCODE, for 1 s. recovery.md gives the A/B partitions and the update steps.

Not done yet

  • Other triggers of /CORE_RESET: the SW1 push, and the bridge after a failed ER1 scan. The firmware otherwise holds /CORE_RESET high. The RAM loader (loader.md) stops the core with the debug unit, and does not need /CORE_RESET.
  • A board test of the safe mode, of try before you buy, of SW1 and of the eFuse control and fault input. The host test uses models, so these items have no test on the real chips.
  • A board test of the emergency bitstream (the gateware is done: safe_bitstream.md).
  • A board test of each function, with openFPGALoader, a terminal program and the bridge.

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