z80replacement · a Z80 CPU replacement with an FPGA

RAM loader

the RAM loader through the debug 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/loader.md

tools/z80r_load.py writes program and data images into the RAM of the Z80 system through the debug port of the module. The loader uses the commands of the debug unit, so the loader needs no other hardware. The debugger.md file gives the path from the host to the unit.

STATUS: TESTED IN SIMULATION ONLY. test_load.py examines the loader on the cosim of the module. No board test is done yet.

What the loader does

  1. The loader stops the CPU core with the H command of the debug unit.
  2. The loader writes each image with the P command. Each write is a memory write cycle on the Z80 bus (debugger.md, item 5).
  3. The loader reads each image back with the M command, and compares the bytes.
  4. The loader then keeps the core stopped, starts the core at an address, or lets the core continue.

The loader writes to RAM, to the internal pages of memory mode 1 (memory.md, "Mode 1 in the gateware"), and to the banked ROM and RAM of memory mode 2 in the DRAM ("Mode 2 in the gateware"). The unit can write an internal page or a ROM bank that is read-only for the CPU. Thus the loader can load a ROM image into the internal ROM or into the ROM banks of mode 2 (--bank 00 to --bank 0F). A ROM or a flash chip on a memory board does not accept the writes. The read-back then shows the bytes that are different, and the loader keeps the core stopped.

Requirements

  • Python 3.8 or later.
  • pyserial (pip install pyserial), for the debug port of the module.
  • A z80fpga checkout. The loader uses the unit code of tools/z80dbg_bridge.py. The loader finds the checkout from --z80fpga, or from the variable Z80FPGA, or in ../z80fpga next to this repository.

Load an image

The debug port and the GDB bridge use the same serial port. Stop the bridge before you start the loader.

To load a binary file at 9000H and start the program:

python3 tools/z80r_load.py --serial COM5 prog.bin@9000 --run 9000

To load an Intel HEX file into bank 85H:

python3 tools/z80r_load.py --serial /dev/cu.usbmodem3 --bank 85 data.hex

On macOS and Linux, the debug port is the second CDC port of the module. On Windows, the debug port is the second COM port of the module.

Images

Form Example Address
Binary file prog.bin@9000 The address after @, in hex
Intel HEX file (.hex, .ihx) data.hex The addresses of the records

You can give more than one image. The loader stops before the first write if two images write the same address. The loader also stops if an image goes past FFFFH, or if a HEX record has an address above FFFFH.

Options

Option Function
--serial PORT The debug port of the module
--vvp-jtag FILE The cosim of the module, for the tests
--bank XX Write the low 32 KB in bank XX (hex)
--no-verify Do not read the images back
--run ADDR Set PC to ADDR (hex) and start the core
--resume Start the core at the PC where the core stopped
--sp ADDR Set SP before the core starts. Needs --run or --resume.
--di Clear IFF1 and IFF2 before the core starts. Needs --run or --resume.
--z80fpga DIR The z80fpga checkout

Without --run and --resume, the core stays stopped. GDB or udb80 can then connect through the bridge and start the program.

A write to PC also clears the HALT state of the core. Thus --run also starts a core that stopped on a HALT instruction.

Banks

--bank XX uses the K command of the unit. For each access to the low 32 KB, the debug memory port does these steps:

  1. The port writes bank XX to ports 78H and 7CH.
  2. The port does the memory cycle.
  3. The port writes the old values back to 78H and 7CH.

Ports 78H and 7CH are the bank select of RomWBW on an SBC memory board (debugger.md, item 6). An access to the high 32 KB uses the common bank, with no I/O writes. After the load, the loader turns the override off.

CAUTION: Do not use --bank on a Z80 system with a different bank scheme. The I/O writes to 78H and 7CH can change a different register on that system.

Speed

Each byte needs one scan of ER1, and the read-back needs a second scan. debugger.md and usb_power.md, "Load times", give the speed of the debug port. Measure the time of a 512 KB load on the first board, and put the value in usb_power.md.

Test

In gateware/dbg:

source ../../../z80fpga/tools/ossenv.sh
mingw32-make test-load            (make test-load on macOS and Linux)

The test runs the loader on the cosim (tb_dbg_cosim.vvp), with the ER1 scans of the firmware. The test examines these items:

  1. A program and 4 KB of random bytes in the high 32 KB.
  2. An Intel HEX file in bank 85H. The CPU bank keeps its bytes, and bank 85H holds the file. The override is off after the load.
  3. A start at 9000H with IFF1 and IFF2 off and a new SP. The test then stops the core and reads the results of the program.
  4. The loader as a command, and an option error.
  5. The image errors, and the report of a byte that is different after the write.
  6. On the cosim with memory mode 1 (tb_dbg_cosim_ovl.vvp): a ROM image into the internal pages, with no cycle on the bus, and a program that runs from the internal ROM.
  7. On the cosim with memory mode 2 (tb_dbg_cosim_mem2.vvp, with the model of the SRAM from gateware/sram. Until 9 November 2026, this was the LiteDRAM simulation core): a program into ROM bank 0 and data into the common RAM, and different data into RAM banks 80H and 81H at the same address with --bank. The test reads each bank back, runs the program from ROM bank 0, and examines that the write of the program to ROM has no effect and that the bus sees no memory cycle.

The last line is PASS or FAIL.

When the debug unit is off

Z80 code can turn the debug unit off with the control port (z80_control.md). The loader then gets a time-out. It prints "debug disabled by target?" and exits with code 3. A reset of the module (a break on the debug port makes the firmware pulse /CORE_RESET) gives the unit back.

Not done yet

  • /CORE_RESET. The loader stops the core with the debug unit, so the loader does not need /CORE_RESET. The firmware holds /CORE_RESET high.
  • A faster load with a wider register on ER2 (usb_power.md, "Load times").
  • A board test.

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