z80replacement · a Z80 CPU replacement with an FPGA
Pin stage
the pin stage that makes the Z80 bus edges, and its test bench
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/pin_stage.md
The pin stage is the logic between the z80fpga core and the level translators. The core changes its state on the rising edge of CLK, one T-state for each edge. A real Z80 changes some signals on the falling edge in the middle of a T-state, and samples some inputs there. The pin stage makes these edges. It also controls the output enables of the translators.
bus_timing.md has the edge table of the Z84C00 that the pin stage follows. bus_interface.md has the translators.
Files
| File | Contents |
|---|---|
gateware/bus/z80r_pins.sv |
The pin stage |
gateware/bus/z80r_bus.sv |
The core and the pin stage together, with the overlay of memory mode 1 as an option |
gateware/bus/z80r_ovl.sv |
Memory mode 1, the overlay (memory.md, "Mode 1 in the gateware") |
gateware/bus/tb_pins.sv |
The test bench |
gateware/bus/irq_prog.z80 |
The program of the interrupt test |
gateware/bus/tb_ovl.sv, ovl_prog.z80 |
The test bench of the overlay, and its program |
gateware/bus/z80r_boot.sv |
The boot copy of memory mode 1 from the configuration flash (memory.md) |
gateware/bus/z80r_acia.sv |
The console ACIA (console.md) |
gateware/bus/tb_acia.sv, acia_prog.z80 |
The test bench of the ACIA, and its program |
gateware/bus/Makefile |
The test, the lint and the place-and-route builds |
The core comes from the z80fpga repository, next to this repository. The
pin stage needs z80fpga 0.2.0 or a later version. The Makefile reads the
file VERSION of z80fpga and stops if the version is lower. The core is
built with LATE_READ, BUSREQ and REAL_ACK set to 1. The
z80fpga file docs/bus_pins.md describes the three parameters and the
status outputs bus_t, bus_cyc, bus_float and bus_resume.
How the pin stage makes each output
The pin stage reads the T-state (bus_t) and the cycle type (bus_cyc)
from the core. Both change only on the rising edge. Each output is one of
three kinds:
- Rising edge only. The output is a decode of the state of the core: the address, /M1, /RFSH and /HALT.
- Falling edge only. The output comes from a register on the falling edge. The register samples the state of the core in the middle of the T-state. Thus the register holds the value for the second half of the T-state.
- One rising edge and one falling edge. The output is the AND of one term of each kind. The two terms never change on the same edge.
| Cycle | Signal | Goes active | Goes inactive | Kind |
|---|---|---|---|---|
| Opcode fetch | /M1 | T1 rise | T3 rise | rising |
| /MREQ, /RD | T1 fall | T3 rise | both | |
| /RFSH | T3 rise | end of T4 | rising | |
| /MREQ (refresh) | T3 fall | T4 fall | falling | |
| Memory read | /MREQ, /RD | T1 fall | T3 fall | falling |
| Memory write | /MREQ | T1 fall | T3 fall | falling |
| /WR | T2 fall | T3 fall | falling | |
| Data on the bus | T1 fall | end of T3 | both | |
| I/O read or write | /IORQ, /RD or /WR | T2 rise | T3 fall | both |
| Data on the bus (write) | T1 fall | end of T3 | both | |
| Interrupt acknowledge | /M1 | T1 rise | T3 rise | rising |
| /IORQ | TW1 fall | T3 rise | both | |
| /RFSH, /MREQ (refresh) | as in the opcode fetch | |||
| NMI response | all | as in the opcode fetch | ||
| Bus hold | /BUSACK | rise after the last T-state | fall in the last T-state of the hold | both |
An I/O cycle has one automatic wait state, so its T3 is the 4th T-state. The interrupt acknowledge has two (TW1 and TW2), so its T3 is the 5th T-state.
Inputs
| Input | The real part samples it | The pin stage |
|---|---|---|
| /WAIT | At the T2 fall and at the fall in each wait state | Samples /WAIT at each falling edge. The core reads the value at the next rising edge. |
| Data, opcode fetch and vector | At the rising edge that starts T3 | Gives the live data bus to the core on din. |
| Data, memory read and I/O read | At the falling edge in T3 | Captures the data bus at that edge and holds the byte on din_late until the core takes it. Thus the live bus goes only to the opcode register and the dispatch ROM of the core, and the fetch path from the data pins is short (routing_study.md). |
| /INT, /NMI | At the rising edge of the last T-state of an instruction | One register on the rising edge, because the core reads them at the end of that T-state. |
| /BUSREQ | At the rising edge of the last T-state of each M-cycle | Goes to the core directly. The core samples it. |
| /RESET | At each rising edge | Three registers. The first rising edge that sees /RESET low clears all three, so the core stops and the outputs float on that edge. The first T1 starts on the second rising edge after the edge that sees /RESET high. |
Translator control
| Signal | Value |
|---|---|
| /AOE (U10, U11, address) | Low (enabled) while the core runs. High during /RESET and during a bus hold. |
| /COE (U13, /MREQ, /IORQ, /RD, /WR) | The same as /AOE. Pull-ups on the bus side hold the strobes high while U13 is disabled. |
| DDIR (U12, data) | 0 (toward the bus: B to A on the 74LVC4245A) only while the module drives write data. Otherwise 1: U12 points toward the FPGA, and the FPGA can read the bus. |
The address and the strobes go to high impedance on the rising edge that
lowers /BUSACK. They come back on the rising edge after /BUSACK goes high.
z80r_bus does not have /DOE (U12) or /SOE (U14). The top level of the module
holds both low after the configuration.
The test bench
tb_pins.sv runs z80r_bus on a model of the bus side of the translators:
pull-ups on the strobes, 64 KB of memory, I/O ports, and a device that
answers the interrupt acknowledge with a vector. A second core, gold, runs
the same program on the simple bus of the core, with no wait states, no bus
requests and no interrupts.
The bench checks the module in four ways:
- Each pin in each half T-state. The bench has its own copy of the edge
table, written as "active from one edge to another edge". The copy does
not share code with
z80r_pins.sv. The T-state and the cycle type come from the status outputs of the core. - The sample edges. The real part samples read data on one edge. The device puts valid data on the bus only from 20 ns before that edge to 2.5 ns after it. Before that time, the device drives a random byte while the strobe is active. /WAIT has the wanted value at each falling edge and a random value at each rising edge. Thus a module that samples on a wrong edge gets wrong data or wrong wait states.
- The bus cycles and the results. The fetches, reads and writes of the
module, with their addresses and written data, are the same as those of
gold, in the same order. The memory and the registers at the end are the same. - No contention. The module and the device never drive the data bus at the same time.
The bench has two programs:
- Bus requests. The program
sim/busrq_prog.z80of z80fpga uses all kinds of M-cycle: block moves, PUSH and CALL, EX (SP),HL, IN and OUT, and the DD CB instructions. /WAIT and /BUSREQ are random. - Interrupts.
irq_prog.z80does the same work 40 times in IM 2, with random /WAIT, /BUSREQ, /INT and /NMI. A vector that the module reads at a wrong time sends the program to a handler that marks the failure. At the end, the bench compares the results of the work withgold. It also compares the number of acknowledges and NMI responses on the bus with the counts of the handlers.
Each program runs at 10 MHz and at 4 MHz. Each program also runs at
10 MHz with four resets in the middle of the run (RESET_TEST):
- The bench pulls /RESET low at a random point in a T-state, for 3 to 6 clocks.
- On each edge that sees /RESET low, the core must stop and the outputs must float. The core must start again on the third edge that sees /RESET high.
- The fourth reset also resets
goldand loads the memory again. Thus the comparison withgoldcovers the run after a reset in the middle of a bus cycle. - The bench releases /INT and /NMI during a reset. The core takes an NMI if /NMI is low when /RESET goes high, before the program clears its count of NMI responses.
The interrupt run at 10 MHz is an example of the results: 357,329 T-states, 26,963 wait states, 30,379 T-states in a bus hold, 1217 acknowledges and 106 NMI responses, with no failures.
Tests of the bench
Eight wrong versions of the pin stage fail the bench:
- the live data bus for a memory read, in place of the byte from the T3 fall
- /WAIT from the pin, not from the falling-edge register
- /MREQ and /RD of a memory read that end at the T2 fall
- /BUSACK that goes high on the rising edge after the hold
- write data on the bus from the T1 rise
- /IORQ of the acknowledge that starts at the T2 fall
- a core without
REAL_ACK - a reset that stops the core three edges after /RESET goes low
Run the tests
source ../z80fpga/tools/ossenv.sh
cd gateware/bus
mingw32-make test # six runs, about 3 minutes
mingw32-make lint
mingw32-make report # place and route on the LFE5U-12F and LFE5U-85F
On Linux and macOS, use make in place of mingw32-make.
Size and speed
mingw32-make report placed and routed z80r_bus with nextpnr-ecp5,
csfBGA285, speed grade 6, three seeds for each part. The LPF file does not
exist yet, so nextpnr put the I/O pins where it wanted to.
| Item | LFE5U-12F | LFE5U-85F |
|---|---|---|
| LUT4 (TRELLIS_COMB) | 6435 (54% of the 12F) | 6435 (8%) |
| Flip-flops | 360 | 360 |
| Block RAM (DP16KD) | 1 | 1 |
| I/O pins | 49 | 49 |
| Highest bus clock | 17.3 MHz to 18.1 MHz | 17.4 MHz to 18.5 MHz |
The pin stage adds about 160 LUT4 to the probe build of bus_timing.md. The path that limits the bus clock is the same: /WAIT from the falling-edge register to the core at the next rising edge, in half a T-state. 10 MHz has a margin of about 1.7. 20 MHz needs a faster /WAIT path.
Known differences and open items
- Glitches on the rising-edge outputs. The address, /M1, /RFSH and /HALT are decodes of the registers of the core. When several register bits change on one rising edge, a decode can make a short pulse. A pulse on /M1 is the most important: the Z80 PIO resets when /M1 is active without /RD or /IORQ. The fix is a look-ahead status from the core, so that the pin stage can put these outputs in registers on the rising edge. Look for pulses with the logic analyzer in phase 2.
- /HALT is half a T-state late. The real part changes /HALT at the T4 fall. The core changes it at the end of T4.
- Two writes in a row. The module keeps the data on the bus during the first half of T1 of the second write. The real part releases the bus at the end of T3. No other device drives the bus at that time.
- The time after /RESET. The number of T-states between /RESET high and the first T1 follows the Z84C00 data sheet text ("two internal T cycles"). Measure it on a real Z84C00 in phase 2.
- The BUSREQ grant point. The core can give the bus away a few T-states
earlier than the real part in some instructions. Refer to z80fpga
docs/bus_pins.md. - No memory in the module.
z80r_bushas no internal memory and no memory modes yet. Refer to memory.md.
Source of this page: web/ and docs/ in the
z80replacement repository. Hardware: CERN-OHL-S-2.0. Other files: GPL-3.0-or-later.