z80replacement · a Z80 CPU replacement with an FPGA
Debugger over USB-C
GDB and udb80 through the USB-C port
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/debugger.md
This file tells how GDB and udb80 debug the Z80 program in the module through the USB-C port. The module uses the debug unit and the GDB bridge of z80fpga. The same debugger tools work with the emulators of the Z80 tool family (cpmemu, romwbw_emu, ioscpm, z80cpmw, cpmdroid).
STATUS: SIMULATION ONLY.
- The gateware for items 2 to 7 is in
gateware/dbg/and in the top level. It passes its test benches, and the bridge operates with it in a cosim (Tests, item 1). No board test is done yet. - The RP2354A firmware (item 1) is in
firmware/(firmware.md). It compiles, and a host test examines it on a model of the TAP. - The FPGA-memory path of item 5 exists for mode 1. The path for mode 2 waits for mode 2.
The path
GDB or udb80 --TCP--> z80dbg_bridge.py --USB--> RP2354A --JTAG--> ECP5
(GDB remote protocol) (debug port) (ER1) debug unit
- GDB (18.1, the z80 target) or udb80 (um80 0.4.2 or later) connects to
the bridge
tools/z80dbg_bridge.pyof z80fpga, on a TCP port. - The bridge speaks the GDB remote serial protocol (RSP) to GDB, and the byte protocol of the debug unit to the module.
- The bridge opens the debug port of the module (a USB serial port, see
usb_power.md) with
--serial. - The RP2354A firmware moves each byte through the 10-bit link register of the debug unit, on the JTAG user register ER1 of the ECP5.
- The debug unit in the FPGA stops, steps and reads the core.
The console port, the debug port and the JTAG adapter for openFPGALoader are three functions of one USB device. The console stays with the Z80 program while GDB operates.
Commands on the host
python3 tools/z80dbg_bridge.py --serial /dev/ttyACM1 --listen 1234
gdb
(gdb) set architecture z80
(gdb) target remote :1234
On Windows the debug port is a COM port, for example --serial COM7. On
macOS it is a /dev/cu.usbmodem device. The bridge needs pyserial.
The bridge gives the same packets and monitor commands as the emulator
stubs: monitor bank, monitor break-bank, monitor wait-program and
monitor status. z80fpga docs/debug_unit.md describes them. For a CP/M
program, use monitor wait-program (GDB) or udb80 --wait-program, as on
the emulators.
What already exists
| Part | Where | Status |
|---|---|---|
| Debug unit: stop at an instruction boundary, step, 4 hardware breakpoints with a bank, registers, memory | z80fpga rtl/soc/dbg_unit.sv |
Tested in simulation and on the Nexys A7 |
| JTAG link: the 10-bit register, the reject rules, the clock crossing | z80fpga rtl/soc/dbg_jtag_link.sv |
Tested; TCK can be faster or slower than the unit clock |
GDB bridge: RSP, Z0/Z1 breakpoints, swbreak, wait-program, --listen-addr |
z80fpga tools/z80dbg_bridge.py |
Tested with GDB 18.1 and udb80 0.4.2 |
| Register hooks of the core | z80fpga rtl/core/z80_core.sv, DEBUG_UNIT |
Exist |
The breakpoints are hardware breakpoints. The unit never writes a breakpoint byte into memory, so a breakpoint works in the ROM of the Z80 system too.
What the module must add
1. RP2354A firmware: the debug port
The firmware moves bytes between the debug port and the link register on
ER1. The rules of the link register are in z80fpga docs/debug_unit.md,
"JTAG link". JtagLink in z80dbg_bridge.py is the model for the
firmware:
- Each scan is CAPTURE-DR, 10 SHIFT-DR cycles and UPDATE-DR, then one Run-Test/Idle cycle.
- A byte from the host goes in bits 7 to 0 with HV (bit 8) at 1. If the FPGA rejects the byte (RJ, bit 9), send one scan with HV at 0, and then send again from the first rejected byte.
- A byte from the FPGA is new when FV (bit 8) is 1. Poll with HV at 0 scans when the host has nothing to send.
- Send all the bytes of a command in one burst. The unit drops a command that stops for longer than its timeout (item 4).
- While openFPGALoader uses the JTAG function, stop the ER1 scans and keep the bytes. After a new configuration, wait for DONE and start again.
The bridge then sees a plain byte stream, as on a serial line, and needs no change.
firmware/src/er1.c does these rules, and firmware/src/djtag.c does the
JTAG adapter for openFPGALoader. firmware.md gives the
build and the host test.
2. ECP5 JTAGG instead of BSCANE2
The link of z80fpga uses the Xilinx BSCANE2. The ECP5 has the JTAGG
primitive, with the user instructions ER1 (0x32) and ER2 (0x38). A small
wrapper makes the signals of the link from the JTAGG outputs:
| Link input | From JTAGG |
|---|---|
jt_tck |
JTCK |
jt_tdi |
JTDI |
jt_sel |
JCE1 held from CAPTURE-DR to UPDATE-DR (ER1 is the instruction) |
jt_capture |
JCE1 and not JSHIFT |
jt_shift |
JCE1 and JSHIFT |
jt_update |
JUPDATE, while ER1 is the instruction |
jt_reset |
not JRSTN |
jt_tdo |
to JTDO1 |
Before you write the wrapper, read the ECP5 sysCONFIG guide for the exact
timing of JCE1 and JUPDATE. Test the wrapper in a cosim with
a model of the TAP, as z80fpga tests BSCANE2.
3. The bus while the core is stopped
The unit stops the core in the first T-state of an opcode fetch (T1). On
the Nexys board, the core then waits. On a real bus, a core that
waits in T1 holds /M1, /MREQ and /RD low with no end. gateware/bus/z80r_bus.sv
and gateware/dbg/z80r_dbg_mem.sv do these items while the unit holds the
core:
- The bus is idle: /M1, /MREQ, /IORQ, /RD, /WR and /RFSH are high, and the data bus is not driven. The address stays.
- On a resume, the bus gets a complete T1 again before the core moves on. The module holds the core for one more rising edge, and makes /M1 and the strobes as in a normal T1.
- Refresh (
DBG_REFRESH, 1 by default). A Z80 system with DRAM needs a refresh cycle in each instruction. While the core is stopped, the memory port runs one refresh cycle each 4 T-states, as a real Z80 does in HALT. A refresh cycle has /RFSH and /MREQ as in T3 and T4 of an opcode fetch, but no /M1 and no /RD. The reason: some Z80 peripherals reset on /M1 without /IORQ or /RD. The row in A6 to A0 counts 0 to 127. The R register of the core does not change at a stop. - Bus hold (
DBG_BUSRQ, 1 by default). While the core is stopped, a low /BUSREQ gets the bus with /BUSACK, with the edges of the hold of a running core. The bus floats, and no refresh occurs. A memory command of the debugger waits for the end of the hold. - A resume waits for the end of a refresh cycle and of a hold.
tb_dbg.sv tests the refresh cycles at a stop (step 1) and a hold at a
stop with a resume during the hold (step 3c).
4. The clock of the unit
The core runs on the bus CLK (bus_interface.md). The unit uses the clock enable of the core, so the unit also runs on the bus CLK. Two results:
- If the Z80 system stops its clock, the debugger cannot talk to the unit. The core does not run then either.
- The unit drops a command after
TIMEOUT_CLKSclocks without a byte. The bridge waits 12 ms before it sends again after an error. SetTIMEOUT_CLKSfor the lowest bus clock that the module must debug: for example 20,000 clocks gives 10 ms at 2 MHz and 1 ms at 20 MHz. The firmware sends each command in one burst (item 1), so 1 ms is enough.
5. Memory access at a stop
The unit reads and writes memory while the core is stopped. GDB needs this
for the stack (backtrace), for variables, and for wait-program, which
reads the bytes at 0100H. On the Nexys board the unit uses the memory port
of the SoC. On the module, the memory is on the Z80 bus (memory mode 0), or
in the FPGA (memory modes 1 and 2, memory.md).
The module needs a memory port for the unit with two paths:
- The Z80 bus. While the core is stopped, the port makes a real memory read or memory write cycle on the bus. The cycle has the timing of the Z84C00 and obeys /WAIT. The pin stage makes the edges, as for the core.
- The memory of the FPGA. In memory modes 1 and 2, the port reads and writes the block RAM or the SRAM (the DDR3L until 9 November 2026), as the core does.
In mode 1, the address selects the path, as for the core.
gateware/dbg/z80r_dbg_mem.sv has the Z80-bus path. In mode 1, the cycle
of the port goes through the decode of the overlay in the pin stage
(gateware/bus/z80r_ovl.sv). A cycle to an internal page uses the block
RAM, and the bus does not see the cycle (memory.md, "Mode 1
in the gateware"). Mode 2 does not exist yet in this part of the design, so the path to the SRAM
(the DDR3L until 9 November 2026) does not exist yet. Add the path together
with mode 2.
6. The bank of the Z80 system
The bridge and the unit use the bank of the program for break-bank,
wait-program (bank 8EH for the CP/M program of RomWBW) and monitor bank. On the Nexys board, the MMU of the SoC holds the bank. On the
module, the bank register is on the memory board of the Z80 system (memory
modes 0 and 1), or in the FPGA (mode 2).
- In modes 0 and 1, the memory board has the bank scheme. The unit knows
the RomWBW SBC scheme: 32 KB banks, with the bank select at ports 78h
and 7Ch. For a board with this scheme, the module watches the I/O writes
to the two ports, and keeps a copy of the bank. The copy goes to
cur_bankof the unit. Other schemes, for example the 16 KB pages of the RC2014 512K ROM 512K RAM module, need a different bank copy, and the bridge needs their bank numbers. monitor bank XX(theKcommand) shows another bank. In modes 0 and 1, the debug memory port (gateware/dbg/z80r_dbg_mem.sv) does each access to the low 32 KB in five bus cycles. First it writes the bank to port 78h and to port 7Ch, as the bank select of RomWBW does. Then it does the memory cycle. Then it writes the old values back to 78h and 7Ch. The module keeps a copy of each port, from the I/O writes of the core. An access to the high 32 KB has no I/O writes.- For a Z80 system with a different bank scheme, set
DBG_BUS_BANKofz80r_busto 0. ThenKhas no effect on the bus, because I/O writes to 78h and 7Ch can change a different register on such a system. Kneeds z80fpga 0.2.1 or a later version. A write withPon the bus takes more time than one JTAG scan. Before 0.2.1, the JTAG link dropped a byte that came during the write.
7. Interrupts
The module connects /INT and /NMI to the core. The unit does not stop at
an interrupt acknowledge, because the acknowledge is part of the
instruction before it. Thus a step that ends where the core accepts an
interrupt stops at the first instruction of the handler. The return
address is on the stack. In GDB, stepi can thus go into a handler.
If an NMI edge comes while the core is stopped, the core keeps the NMI request. The core does not move, and the bus stays idle. The next step or run starts the NMI response.
gateware/dbg/tb_dbg.sv tests these cases (steps 6 to 8 of the bench):
- a step over an instruction when /INT is low: the step stops at 0038H (IM 1), with the return address on the stack
- an NMI edge while the core is stopped: the next step stops at 0066H, with IFF1 0 and IFF2 1
- a breakpoint in an interrupt handler, then a step in the handler while /INT is low: IFF1 is 0, so the step does not accept the interrupt
Before the release, do the same cases on the first board (Tests, item 4).
Z80 control of the debug unit
Z80 code can remove the effect of the debug unit on the Z80 side with the
control port (z80_control.md). The unit then stays in
reset: it does not stop the core, the bus is never idle, and the
breakpoints are lost. The JTAG link gives no reply. A reset gives the unit
back. gateware/dbg/tb_dbg_ctl.sv tests this.
Tests
- A cosim of the module with the debug unit: the pin stage, the core, the
unit, a model of
JTAGGand the bus model oftb_pins.sv. The bridge connects to the cosim, as z80fpga does with--vvp-jtag.gateware/dbg/tb_dbg_cosim.svis this cosim.mingw32-make test-bridgeingateware/dbgstarts the bridge on the cosim. Thentest_bridge.pysends the packets of GDB and examines the replies. The test covers registers, memory, breakpoints, a step, the bank, and a stop with Ctrl-C. - GDB and udb80 against the cosim:
monitor wait-program,break,step,bt, as in z80fpgadocs/debug_unit.md, "What is measured".mingw32-make test-udb80ingateware/dbgdoes a udb80 session in batch mode. um80 and ul80 build the program again with debug information. Then udb80 sets a breakpoint on a label, steps, makes a backtrace through the stack, and reads the results.mingw32-make test-gdbdoes the same session with GDB.dbg2elf(um80 0.4.2) makes an ELF file from the debug information. GDB then loads the unwinderudb80_unwind.pyof um80. The test examines a breakpoint on a label,info registers,stepi, a backtrace,monitor status, a memory write, and the results of the program. The GDB must have the z80 target. On Windows, use thegdb-multiarch18.1 of MSYS2 (C:\msys64\ucrt64\bin). Thegdbof WinLibs has no z80 target. SetZ80R_GDBto use a different GDB. If no GDB has the z80 target, the test reportsSKIP. SetZ80R_REQUIRE_GDB=1to make the test fail in that case. - The RAM loader (
tools/z80r_load.py) against the cosim:mingw32-make test-loadingateware/dbg. loader.md gives the test. - On the first board: the same steps through the USB-C port, with the module in an RC2014.
Cost
z80fpga measured the unit at 410 LUTs and 353 flip-flops on the Artix-7, and the JTAG link at 43 LUTs and 50 flip-flops. The unit uses no block RAM.
gateware/top builds the module with DEBUG = 1 (the default of
z80r_top). nextpnr gives these values for seeds 1 to 3:
| Item | LFE5U-12F | LFE5U-85F |
|---|---|---|
| LUT4 (TRELLIS_COMB) | 7052 (58% of the 12F) | 7052 |
| Flip-flops | 844 | 844 |
| JTAGG | 1 of 1 | 1 of 1 |
| Highest bus clock | 16.7 MHz to 17.5 MHz | 16.9 MHz to 17.6 MHz |
| Highest TCK | more than 300 MHz | more than 300 MHz |
The path that limits the bus clock is the /WAIT path of
bus_timing.md. The path starts at /WAIT on the falling
edge and must fit in half a T-state. The debug unit adds the multiplexer of
its register writes to this path, so the path takes about 30 ns. At 10 MHz,
half a T-state is 50 ns, so the margin is about 1.67. Without the debug
unit (DEBUG = 0), the highest bus clock is 17.4 MHz to 18.5 MHz.
make timing in gateware/top measures the pin groups with DEBUG = 1.
All pin groups meet the Z84C00 limits at 10 MHz and at 20 MHz
(routing_study.md, "With the debug unit").
Source of this page: web/ and docs/ in the
z80replacement repository. Hardware: CERN-OHL-S-2.0. Other files: GPL-3.0-or-later.