Keeping time and going back

A ZX Spectrum does not run at a round speed: a 48K makes 50.08 frames a second, and a Pentagon 48.83. To sound and move like the real machine for hours, an emulator has to keep that rate exactly, on a computer whose own clocks run at other speeds. This page explains how Xpeccy+ keeps time, what happens to the sound when the time jumps, and how Rewind keeps its history.

Time to a fraction of a nanosecond

The machine’s time is counted in steps of its clock: the dot the picture is drawn by, the processor’s tick, the sound’s sample. At 3.5 MHz a tick is 285.714… ns - not a whole number. Rounded to 286 ns, it would be 0.1% too long. A 48K would then make 50.03 frames a second instead of 50.08, a frame fewer every 20 seconds, and its music would be that much flat.

So Xpeccy+ keeps these periods in fixed point, to 1/65536 of a nanosecond. What one step leaves over is carried into the next instead of being dropped. The periods are also made from one another, not each rounded on its own: a tick is exactly two dots, and a frame is a fixed number of dots. If two periods were rounded separately, they would drift against each other: a picture standing still would creep by a dot every few seconds.

Machine

Frame

Frames a second

ZX Spectrum 48K and its kind

69888 ticks at 3.5 MHz

50.08

ZX Spectrum 128K, +2, +2A, +3

70908 ticks at 3.5469 MHz

50.02

Pentagon, ZX Evolution

71680 ticks at 3.5 MHz

48.83

What sets the pace

The computer’s clock sets the machine’s pace. Every 2 ms the emulation is given the time that has passed, as a number of sound samples to make, and it runs until it has made them. The sound card’s own clock is not used for this, because the card asks for sound only every 10 to 20 ms, which is too coarse to pace frames by.

The two clocks - the computer’s and the sound card’s - never quite agree. If nothing were done, the sound waiting to be played would slowly run out (a click) or pile up (more delay). So Xpeccy+ steers the pace by up to 0.1% to keep the waiting sound at its target. That is far too little to hear, or to see in the frame rate. If VSync locks the machine to the screen, the screen’s refreshes set the pace instead, and the sound is steered by up to 0.5%.

After a stall - a window dragged, a disk slow to answer - the machine makes up at most 100 ms. It does not race to catch up on more.

Slow Motion and Fast Forward do not change the pace. They change how much of the machine’s time each sample stands for. The sound card still gets every sample it asks for, and the sound goes down or up in pitch, like a tape played slow or fast.

When the time jumps

The sound waiting to be played follows on from the machine’s last moment. After Fast Mode, a rewind, an opened file, a reset or a switch to another machine, the machine’s new sound no longer follows on from what is waiting. So Xpeccy+ drops the waiting sound and waits until enough new sound is ready to play without running short. Then it fades from the last sample played into the new sound over 5 ms, so there is no click. A pause drops nothing, because the waiting sound is what comes next.

The frame rate in the status bar is counted over the last second, from one frame’s end to another’s, so it has no rounding of its own.

Rewind

Taking the history. Every Step frames (3 by default), the whole machine is copied in memory. It is the same copy Run ahead uses, not a file. A copy of a Pentagon 1024 or a ZX Evolution takes megabytes, so each copy is cut into pages of 4 KB. A page that is the same as in the copy before is shared, not copied again. A few frames apart, most of memory is the same. The sound is kept beside the copies, in the machine’s own time, so a stretch played fast or slow keeps its sound in step with its pictures. With the defaults the history takes 20 to 45 MB. It never takes more than 512 MB, whatever History is set to.

Going back. As you go back, each picture shown comes from the next copy back. It is not stored as a picture. Instead, the copy is loaded, and one frame is run from it and thrown away. That makes the exact picture, multicolor effects included, for the price of one frame. So going back runs Step times faster than the machine did: 30 seconds of history take about 10 at the default step. The sound plays each stretch backwards.

Letting go. The machine is put back to the copy on the screen, and everything newer is dropped. Cheats are put back as they were at that moment.

What is not in the copy. The keys and the joystick are not in it, so a key you hold now stays held. Neither are the contents of disk, hard disk and card images, which are too big to copy every few frames. That is why the history starts over when the machine writes to one of them: nothing before the write could be put back as it was. The history also starts over when you switch to another machine, when an RZX recording starts or ends, and when Apply in Options changes the machine or the rewind’s own settings. A reset, or a file opened on the same machine, keeps the history, so you can undo an accidental reset.