Skip to Content
Under the HoodEmulator InternalsThe ZX Spectrum Next core and the FPGA

The ZX Spectrum Next Core and the FPGA

A ZX Spectrum Next is not built from a Z80 chip and a handful of custom ICs. Its hardware is a design for an FPGA, written in VHDL, a hardware description language. The Z80N CPU, the ULA, Layer 2, the sprites, the tilemap, the Copper, the DMA and the sound chips are all VHDL modules.

That gives an emulator author something rare: the machine’s actual source. Klive’s Next core uses it as the reference for every behaviour. This article explains how the core relates to the VHDL and how a test harness checks the core against it.

One emulator, in C

The Next emulator is a C core compiled to WebAssembly, like Klive’s other machines (see How the emulator cores are built). It is the only Next emulator in Klive. There used to be a TypeScript Next machine with about 25 device classes. It was removed once the C core reached parity with it.

The core lives in src/emu/machines/zxNext/wasm/zxnext/: 33 C files, roughly one per piece of hardware. Among them are zxnext-copper.c, zxnext-dma.c, zxnext-sprites.c, zxnext-layer2.c, zxnext-tilemap.c, zxnext-ula.c, zxnext-ctc.c, zxnext-divmmc.c, zxnext-multiface.c, zxnext-uart.c, zxnext-i2c.c and zxnext-sd.c. The CPU is the shared Klive Z80 that every core includes, with the Next’s own hooks.

Around the core sits a TypeScript host:

Z80Cpu -> Z80MachineBase -> ZxNextWasmHost -> ZxNextWasmV2Machine

ZxNextWasmHost is plumbing that does not depend on how the hardware is emulated: frame pacing, the key-stroke queue, code injection, partition names, system variables. The IDE reads machine state only through one interface, IZxNextIdeMachine.

This shape is enforced. ZxNextWasmV2Machine used to extend the TypeScript machine, so several paths (the keyboard, code injection, frame pacing, four IDE panels) quietly kept running TypeScript emulation beside the core. A test now fails if an emulated device class becomes reachable from the machine or its loader again, even through a type import.

The VHDL is the reference

The FPGA sources are kept in the repository, in dev/vendor/next-fpga/3.02.01/. The src/ folder holds 73 .vhd files, grouped the way the hardware is:

FolderWhat is in it
src/zxnext.vhd, the top level that wires everything together (about 7,500 lines)
cpu/The T80N, the Z80N CPU
video/The ULA and its timing, Layer 2, LoRes, sprites, the tilemap, HDMI
device/The Copper, the DMA, the CTC, DivMMC, the Multiface, the IM2 interrupt logic
audio/The YM2149 (AY), TurboSound, the DACs, the audio mixer
serial/The UARTs and the SPI master
ram/, rom/, input/, misc/Memories, the boot ROM, the joystick connector, helpers

Beside them, nextreg.txt and ports.txt document every NextReg and every I/O port.

The project’s policy is plain: Next behaviour is implemented in C, against these sources, and a claim in a test is expected to cite them. TypeScript changes are for behaviour that belongs to the host rather than the machine: IDE and UI policy, finding resources, saving files, test setup.

The C sources follow the same habit. Most of the core’s files carry comments that point at the VHDL they implement: a module name and an approximate line number. For example, the Copper’s code notes which lines of zxula_timing.vhd load its line counter, and which lines of zxnext.vhd decide what else a write to NextReg $60 stores.

The hardware test harness

To check the core against the VHDL, Klive has a test harness in test/harness/zxnext/. Its main idea fits in one sentence: it runs the real machine, the production WebAssembly core, and drives and observes it only through what the hardware exposes. That means ports, NextRegs, memory, CPU registers, the displayed picture and the mixed audio. There are no mocks, no device objects tested in isolation, and no stand-in for a frame.

It replaced an older style of test that poked device objects directly, simulated clocks by hand or faked the machine around a device. Such a test can pass while the real path that sets the state is broken.

The harness has two front ends on one engine:

Front endUse it whenLives in
Scripted tests (createSession)You check registers, ports, memory, timing, audio or a few pixelstest/zxnext-hw/<component>/
Declarative screen cases (case.json)The result is a picture, judged by probes, motion tracking, golden hashes and AI reviewtest/visual/<suite>/<case>/

The scripted tests are the bulk: 112 test files in 33 component folders, from audio, copper, ctc and dma to sprites, tilemap, uart and video.

A scripted test

A session assembles a short Z80N program, loads it, runs real frames, and then looks at the hardware. This is the shape of the sprite collision test:

const s = await createSession(); await s.loadCode(` .org $8000 nextreg $15,$01 ; sprites visible ; ... upload a pattern through port $5B, attributes through $57 ... nextreg $7F,$A5 ; ready jr $ `); s.runUntilReady().runFrames(2); expect(s.in(0x303b) & 0x01).toBe(1); // collision flag set expect(s.in(0x303b) & 0x01).toBe(0); // reading $303B cleared it

The program writes NextRegs and ports itself, as real software would. runUntilReady runs frames until the program writes $A5 to NextReg $7F. The assertions read port $303B with every side effect: the first read returns the flag and clears it, so the second read sees zero. The comment at the top of the real test file quotes the line of sprites.vhd that sets the flag.

The session offers many more methods: out and in, setNextReg and readNextReg (through ports $243B and $253B, as Z80 code would), peek and poke through the current MMU paging, runTo to stop before an instruction, step, pixel and screen for the displayed picture, startAudio and audio for the mixed samples. Inputs go in at the hardware level too: membrane keys, joysticks, PS/2 mouse packets, a UART peer, the real-time clock on the I2C bus, an SD card image.

The rules

The harness README gives six rules for a good hardware test:

  1. Expectations come from the hardware, never from emulator output. Cite the VHDL or the official documentation next to the assertion.
  2. Drive through the hardware interface: Z80 code, out, setNextReg, poke.
  3. Observe through the hardware interface: in, peek, registers, pixels, audio.
  4. Let frames run. Timing-dependent devices are exercised by running frames, never by calling a device’s clock method.
  5. Do not assume reset values that the firmware changes, when a test may also run through NextZXOS.
  6. Write every palette entry you rely on. The FPGA’s palette RAM has no reset contents.

When a test needs something the session cannot do, a contributor adds a method to the session. Tests never reach into the machine object directly.

When an old test disagreed with the VHDL, the VHDL won, and the old test turned out to encode an emulator bug. A difference from the VHDL is a finding: either the core is fixed, or the test is marked as a known failure with a comment naming the cause. The expectation is never loosened.

An example: the Copper’s wrap-around

The Copper test for list wrap-around shows how the VHDL drives a test. Its header comment reads the hardware like a specification. In copper.vhd, the Copper’s list address is a 10-bit counter that every MOVE and every matching WAIT advances, so after instruction 1023 the Copper fetches instruction 0 again. Nothing stops it at the end of the list. A MOVE to register 0 with value 0 is a NOP: no write, but the address moves on. The Copper executes one instruction per 28 MHz tick.

The test builds a full list of 1024 instructions: a WAIT for line 50, a MOVE that sets NextReg $14, NOPs, and a MOVE to NextReg $4A in the last slot. It runs two frames and checks that both registers were written. Then it overwrites them and runs two more frames. If the list wrapped, the Copper wrote both registers again.

Measuring to the T-state

Some tests must place a raster event to the exact T-state. Taking an interrupt adds jitter, so these tests avoid interrupts. From a fixed start (a frame boundary with the CPU parked), the program runs a delay of exact length and reads a latch or counter with one IN. A binary search over the delay finds where the value changes. Two events read by the same IN instruction cancel the CPU’s own timing. The video timing test finds the ULA interrupt and the line change this way, and the border timing test sweeps a border OUT one T-state at a time.

Screen cases and the real operating system

Some behaviour is best judged as a picture. A screen case is a folder with a program, a case.json, an expect.md and golden hashes. The runner compiles the program, runs it, captures chosen frames as PNG files, and judges them by pixel probes, motion specs, golden hashes and an AI review prompt. The Copper suite alone has cases from a calibration picture to per-line scrolling and a moving bar.

npm run test:visual runs the cases headless. With --tier browser, the harness runs the core in Chrome, boots NextZXOS from a cloned SD card, and loads the program with the real .nexload. That checks what the direct loader cannot: the ROM, NextRegs and system variables as the operating system leaves them.

What the harness does not cover

test/wasm/zxNext/ keeps the tests the harness cannot reach: the loader and memory layout, the machine lifecycle, checkpoints, the debug loop, boot traces, and the state the IDE reads. New behaviour of the Next’s hardware goes into test/zxnext-hw/, through the harness.

The harness README (test/harness/zxnext/README.md) lists every session method, the pitfalls, and how to add a method. The core’s README (src/emu/machines/zxNext/wasm/README.md) describes the machine’s structure and its maintenance policy.

Last updated on