z80replacement · a Z80 CPU replacement with an FPGA
Z80 control of the extra hardware
the control port: Z80 code turns the extra parts on or off
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/z80_control.md
Requirement R17 (requirements.md): Z80 code can use or remove each extra part of the module. With all parts removed, the module is a plain Z80 on the bus. The USB side can restore the parts.
The module has one control port. The port is an 8-bit I/O port. After each
reset the port is locked. A key of four writes opens the port. The
code is in gateware/bus/z80r_ctl.sv. The test benches are in
Tests.
What Z80 code can and cannot control
| Part | Bit | What the command does |
|---|---|---|
| Memory mode 1 (the overlay, memory.md) | 0 OVL | Off: every memory cycle goes to the bus. The block RAM keeps its contents. |
| Memory mode 2 (the banked SRAM, memory.md) | 1 M2 | Off: memory cycles go to the bus. The cache and the SRAM keep their contents. |
| Console ACIA (console.md) | 2 ACIA | Off: the ports of the ACIA go to the bus, and /INT is free. |
| Debug unit (debugger.md) | 3 DBG | Off: the unit cannot stop the core, and cannot use the bus. |
These limits come from the design:
- A bitstream has mode 1 or mode 2, not both (
z80r_bus.svbuilds the overlay only ifMEM2is 0). Z80 code turns the one mode of the bitstream on or off. Z80 code cannot change mode 1 to mode 2. The bitstream (the flash sector) decides the mode. - The boot copy ends before the core starts, because the core stays in reset until the copy ends. Z80 code cannot undo the copy. A part that is off keeps its contents, and the part has the same contents when you turn the part on again.
- The debug port is JTAG on the USB side. No Z80 address reaches the port. Z80 code can remove only the effect of the debug unit on the Z80 side: the stop of the core, the idle bus, the bus hold and the cycles of the memory port. User register ER2 stays reserved for the wide loader register (usb_power.md). Recovery does not use ER2.
The control port
The port decodes A7 to A0. It ignores A15 to A8, as a real OUT (n),A
does. The port address is the parameter CTL_BASE (default EEH). The flash
header can set a different address (see Reset state).
The lock
The port has four states.
- LOCKED (the state after each reset). The port is not internal. Reads
and writes at
CTL_BASEgo to the bus as on a real Z80, so a device at that port sees them. The module only watches the end of each I/O write toCTL_BASE. - Unlock. Four writes in a row to
CTL_BASEwith the bytes 5AH 38H 30H 52H ("Z80R", the magic of the flash header) open the port. Any other write toCTL_BASEstarts the count again. A 5AH counts as the first byte. Reads and writes to other ports do not change the count. The four key bytes also go to the bus. ThusCTL_BASEmust be a free port. - OPEN. The next I/O cycles at
CTL_BASEare internal: no /IORQ, /RD or /WR on the bus. A read gives the status byte and does not close the port. The first write is the command. The command closes the port. - GONE. After a command with HIDE, the port does not watch, does not decode and does not answer, until the next reset. The I/O map then equals the map of a real Z80.
A stray write cannot remove a part. It needs the whole key and a valid command.
Status byte (read, only when OPEN)
The byte has the form {present[3:0], state[3:0]}. The order of the bits in
each group is DBG, ACIA, M2, OVL (bit 3 to bit 0).
presentshows the parts that the bitstream and the flash contain. OVL is present if the bitstream hasOVL_MAPnot 0 orBOOT, and has no mode 2. M2 is present withMEM2. ACIA is present with the ACIA build and the ACIA flag of the flash. DBG is present withDEBUG.stateshows the parts that are on.
Bits of absent parts read 0. A command cannot set a bit of an absent part.
Command byte (write, only when OPEN)
| Bit | Name | Meaning |
|---|---|---|
| 0 | OVL | Memory mode 1 on |
| 1 | M2 | Memory mode 2 on |
| 2 | ACIA | Console ACIA on |
| 3 | DBG | Debug unit active on the Z80 side |
| 4, 5 | Must be 0. If not, the module ignores the command and closes the port. | |
| 6 | MOVE | The next write to the port is the new CTL_BASE (8 bits). Then the port closes. |
| 7 | HIDE | The port is gone until the next reset. HIDE wins if MOVE is also set. |
The command 80H removes all parts and hides the port. Then the module acts as a plain Z80. The enables change on the edge that ends the command write (T-state T3 of the I/O cycle). No cycle changes in flight, and the next T1 sees the new state.
A program that removes the region where the program runs crashes by
design. The program must run from the bus before the program turns off
OVL or M2. The test ctl_prog.z80 shows how: the code runs from an address
where the bus has the same bytes.
Reset state
Every low level of the core reset loads the state of the control port:
the host /RESET, /CORE_RESET, and the hold during the boot copy. The
settings are valid when the core starts, because the boot copy holds the
reset. Thus the settings need no synchronizer (the same reason as the
other settings in z80r_bus.sv).
| Item | Value after reset |
|---|---|
| OVL | present and flag 4 of the flash is 0 |
| M2 | MEM2 and flag 3 of the flash is 0 |
| ACIA | the ACIA build and flag 1 of the flash (existing meaning) |
| DBG | always 1 (recovery rule) |
| GONE | flag 2 of the flash |
| Port address | byte 12 of the flash header, or CTL_BASE if byte 12 is 0 |
The header of the flash image (memory.md, "The boot copy") uses flags 2 to 5 of byte 10 and byte 12. All of these were 0 in the earlier images, and the polarity keeps the earlier behavior. The version stays 1. Without a valid header, the parameters give the same defaults.
| Byte 10 bit | Set means |
|---|---|
| 2 | The control port is hidden from power-up |
| 3 | Memory mode 2 is off at reset (Z80 code can turn the mode on) |
| 4 | Memory mode 1 is off at reset (Z80 code can turn the mode on) |
| 5 | Z80 code may not remove the debug unit (bit 3 of the command stays 1) |
Flag 1 (the ACIA) keeps its meaning: with flag 1 at 0, the ACIA is not a part of the module, and Z80 code cannot turn the ACIA on.
tools/z80r_romimg.py has the options --ctl-base, --ctl-hidden,
--mem2-off, --ovl-off and --dbg-locked.
The parameter CTL of z80r_bus is 0 by default, so the existing
benches stay unchanged. In that build, the flash flags 2 to 5 have no
effect. z80r_top has CTL 1 and CTL_BASE EEH by default.
Behavior of a removed part
A removed part gives no answer. The cycle goes to the bus.
- OVL off.
z80r_ovl.svhas the inputen. Withenlow there is no hit and no write to the block RAM. The block RAM keeps its contents. - M2 off.
z80r_mem2.svhas the inputen. Withenlow there is no hit, no write to the FIFO of the SRAM, no line request and no wait state. The internal /WAIT is 1. The bank register of the MMU still follows the writes to ports 78H and 7CH, and the writes also go to the bus. When you turn M2 on again, the bank is correct. - ACIA off. The reset input of the ACIA is held low while the ACIA is off. The reset clears the FIFOs, the receive interrupt enable and the transmit interrupt enable. Thus a removed ACIA cannot hold /INT low. The ACIA drops the bytes of the console while it is off. The console port of the RP2354A stays alive, with no consumer in the Z80 system.
- DBG off. The debug unit stays in reset. The reset clears the stop
state and the four breakpoints. Breakpoints do not return when you turn
the unit on again. The unit does not stop the core, the bus is never
idle, and the memory port makes no cycle. The JTAG link still takes
scans, but the unit sends no reply. The loader and the bridge get a
time-out.
tools/z80r_load.pyprints "debug disabled by target?" and exits with code 3.
Recovery
Any reset loads the state of the flash again. The state includes DBG at 1 and (unless flag 2 is set) a visible control port.
- The RP2354A can pulse
/CORE_RESET. The firmware does this when the host sends a break on the debug port (the CDC request SEND_BREAK, for exampleserial.send_break()of pyserial). The pulse is 100 µs low (CORE_RESET_PULSE_US). The pulse resets only the core and the FPGA state. The pulse does not drive the host bus /RESET. - The control flops run on the bus clock.
/CORE_RESETworks only while the bus clock runs. Suppose the host stops the clock after Z80 code removed the debug unit. Then only a power cycle restores the unit. The safe mode of recovery.md, level 3, also restores the unit: it loads the bitstream again. - Level 2 of recovery.md (the JTAG write of the flash, and the configuration of the FPGA) does not depend on the control port. The USB side can always load the FPGA again.
- The flag 5 of the flash header ("Z80 may not remove debug") is for systems where the loader must always work.
Decisions for the project owner
The design made these choices. Each is the conservative default. The owner can change them.
- Default
CTL_BASEis EEH. The value is a guess. Ports 78H to 7FH (the bank ports,z80_mmu.svdecodes 78H and 7CH) and 80H to 8FH (ACIA, and serial boards of RC2014 that can repeat the ACIA) are not used. Check the I/O map of the target system. - The port is transparent until the key opens the port. The four key bytes also appear on the bus. The other choice is a port that is internal from power-up. Then the key bytes never reach the bus. But a program cannot detect the module with a plain read, and the port always hides a host device.
- The key is 5AH 38H 30H 52H. No interrupt and no other I/O cycle closes the window.
- Z80 code can remove the debug unit (flag 5 forbids this). The other choice is to never allow it, so the USB side is never locked out.
- Breakpoints and the unit state are lost when DBG goes off. Recovery uses
/CORE_RESET(needs the bus clock) or a power cycle. The module has no USB-side override through ER1. An override needs a change of the unit and of the bridge. ER2 stays reserved. - Every reset clears GONE, also the bus /RESET. A system that must keep the port hidden after a reset needs a different rule.
- The reset state follows the flash, and DBG is 1 at reset. The encoding is flags 2 to 5 of byte 10 and byte 12.
- Mode 1 and mode 2 stay exclusive at build time. Both together need both block RAM sets and a priority rule.
- A command that loads the ROM image again from the flash is not in the design. The boot copy needs the core in reset, and the running Z80 cannot do that by itself.
- The firmware pulses
/CORE_RESETon a break of the debug port. Other triggers (the SW1 push, or the bridge after a failed ER1 scan) are possible. - The loader shows "debug disabled by target?" on a time-out. The bridge from z80fpga (outside this repository) shows its own time-out text.
Tests
mingw32-make test in each folder runs the benches. Each bench prints
PASS or FAIL as the last line.
-
gateware/bus/tb_ctl.svandctl_prog.z80(at 7.4 MHz, 20 MHz and 2 MHz). The bench checks these cases:- The locked port goes to the bus.
- A wrong key, a partial key and a key with a write in the middle leave the port locked.
- The bytes 5AH 5AH 38H 30H 52H open the port.
- The status read makes no cycle on the bus and does not close the port.
- Reserved bits close the port with no effect.
- The overlay and the ACIA go off and on.
- A removed ACIA with a byte in and the receive interrupt on does not hold /INT low.
- Bytes of the console that come while the ACIA is off are not there later.
- MOVE and HIDE work, and a bus /RESET restores the state.
The bench compares the I/O cycles on the bus with an exact list. It checks that there is no /RD or /WR without /MREQ or /IORQ, and no two drivers on the data bus.
-
tb_ctl.svwithPLAIN=1andctl_plain_prog.z80. One system removes all parts and hides the port, then runs a payload. A second system has no control port, no ACIA and no overlay, and runs the payload. The bench compares the bus cycles one by one: kind, address, data and length in clocks. The bench does not compare the refresh address, because the R register differs. -
tb_ctl_boot.svandctl_boot_prog.z80. The bench tests these headers of the flash:- an old header (zeros in the new fields)
- hidden at power-up
- overlay off
- a new port address
- no ACIA
- flags that have no effect in this build
- an erased flash (the parameters apply)
-
gateware/sram/tb_mem2_ctl.sv: M2 off and on. While off there is no line request, no SRAM write, no wait state, and the cycles go to the bus. The write to port 78H while off goes to the bus, and the bank register follows it. The contents come back when M2 goes on. The bench runs at 10 MHz, 4 MHz, 20 MHz and with the 1 KB cache. -
gateware/dbg/tb_dbg_ctl.sv: Z80 code removes the debug unit while the host asks for a stop again and again. The core keeps running, the bus is never idle, the unit never stops the core at the breakpoint, and the host gets no reply. A reset gives the unit back, with no breakpoint. Alsotest_load.pyruns the loader against the cosim with a removed unit, and checks the time-out and the message. -
firmware/test: the host test callshal_core_reset().
Cost
Place and route of z80r_top on the LFE5U-85F with the debug unit, the
ACIA, 16 KB of overlay (OVL_MAP C003H) and the boot copy
(make report-85k OVL_MAP=49155 OVL_RO=3 OVL_KB=16 BOOT=1 in
gateware/top, seeds 1 to 5, 10 November 2026). The first column is the
commit before the control port.
| Item | Before | With the control port (CTL 1) |
|---|---|---|
| LUT4 (yosys) | 6785 | 6646 |
| TRELLIS_COMB (nextpnr) | 7673 | 7532 |
| Flip-flops | 1171 | 1198 |
| Block RAM (DP16KD) | 9 | 9 |
| Highest bus clock, seed 1 to 5 | 16.8, 18.4, 18.1, 17.9, 17.9 MHz | 17.2, 17.4, 17.0, 17.1, 17.2 MHz |
| Highest 25 MHz clock, seed 1 to 5 | 106, 107, 109, 103, 109 MHz | 98, 100, 106, 95, 103 MHz |
z80r_ctl alone needs 17 flip-flops and 104 LUT4 (yosys, with open
inputs). In the full build the inputs are constants, and the total LUT4
count falls. The reason is the reset of the debug unit: it comes from a
register (u_rst_r in z80r_bus.sv). Before that change, the first run
gave 6918 LUT4 (133 more than before) and a highest bus clock of 15.5 to
17.2 MHz.
The 25 MHz clock is about 7 MHz lower, and it still has a margin of more than 3 times.
The path that limits the bus clock is the /WAIT path through the debug unit
and the core, as in debugger.md. The path does not pass
through z80r_ctl. The mean of the highest bus clock is 0.6 MHz lower than
before (17.2 against 17.8 MHz), and the spread of the seeds is smaller.
Run more seeds on the board build before you trust a difference of this
size.
Source of this page: web/ and docs/ in the
z80replacement repository. Hardware: CERN-OHL-S-2.0. Other files: GPL-3.0-or-later.