A graphical desktop for the ZX Spectrum, written entirely in Z80 assembly

Translated by a model; the original is in Russian.

· Программы
An enthusiast going by mindbox77 has packed a windowed interface with menus, a file manager and settings into 48 KB — and it runs on a real 1982 machine.

cover.svg

A project called ZX Desk has turned up on GitHub — a full-fledged graphical desktop for the ZX Spectrum 48K, written in Z80 assembly. Overlapping windows with stacking order and focus, drop-down menus, a heap, an event queue, a storage layer with swappable backends, dialogs, a notepad, a clock, a calendar, a two-pane file manager and a settings panel that actually changes things — all of it fits into 48 KB and runs on a real machine, not just in an emulator. Dragging a window fits inside a single 69,888-cycle frame.

The author explains where the idea came from: back in the eighties he wanted an Atari ST with its GEM system — that sense that a computer is a place rather than an invitation to the command line. He could only afford a Spectrum, and for years he wondered how much of that could be reproduced. He started, gave up — and has now seen it through to the end. It isn't a port of GEM, but what the idea turns into when it runs up against a 3.5 MHz processor, a one-bit screen with attribute clash, and a video chip that steals cycles from the processor while drawing.

The value is in the measurements

The most interesting thing about the project isn't the desktop itself but the meticulous hardware measurements the author generously publishes for anyone else writing for the platform. Several of the findings break long-standing myths. The penalty for accessing screen memory turned out to be not 50%, as the folklore claims, but only 14.7% — meaning every budget built on the pessimistic figure was overstated by a third. Half the cost of drawing a window is eaten up by three short lines of text, not by the fills the author had thought were the main culprit.

A separate discovery is the behaviour of interrupts. It turned out that with interrupts disabled (DI) the Spectrum doesn't defer an interrupt — it loses it: the signal is held for just 32 cycles and then dropped. The standard trick of re-enabling interrupts between lines doesn't help, because the interrupt is no longer deferred but gone. The author found an elegant solution: fills now run with interrupts enabled, and the two bytes of the return address are guaranteed to land in something that will be overwritten anyway.

The discipline of development

To fit dragging inside a frame, the author went through four successive optimizations: compositing the window into an off-screen buffer, faster text output, redrawing only the strip that had been freed, and "racing the beam." The result — a steady 50 Hz instead of the previous 25.

Every improvement was verified by measuring against the machine's own clock, every claim of correctness by a screen checksum, every commit carried acceptance figures. A separate section is devoted to bugs that are "worth writing down": a heap that repaired itself before his eyes, a register carrying the carry, the argument and the answer all at once, and a pointer invisible in the top half of the screen on real hardware but not in an emulator. According to the author, no live bugs remain. The version for the ZX Spectrum Next became a separate system rather than a port — on a machine with a tile-based interface, a window compositor simply has nothing to do.


Source: Hacker News

Comments

Sign in, to comment.

  • No comments yet.