Skip to content
Eliott65wiki
Contents

Hibernation: the whole machine in a file

All of the 6502’s memory lives inside the RP2040, so it can be written to a file and loaded back later. One difficulty remained: the processor’s registers, which nobody can see.

The idea

A conventional computer that hibernates has to dump its memory to disk, then load it back. On the Neo6502kbd, it comes almost for free: the 6502’s 64 KiB are already an array in the RP2040’s memory. All it takes is to write it to a file, together with the state of the screen and keyboard.

The real difficulty: the registers

The 6502 has registers (A, X, Y, the status P, the stack pointer S, the program counter PC). They are inside the chip, not in memory, and they never go over the bus. So the RP2040 cannot read them.

The solution is inspired by setjmp and longjmp, well known to C programmers:

  1. the program calls the library routine e65_hiberner;
  2. the routine itself stores Y, P and S in its own bytes, and therefore in the memory that is about to be saved;
  3. it calls the kernel (SYSTEME HIBERNER), giving it its resume address.

The kernel then writes a .HIB file: a 512-byte header (signature E65H, resume address, screen state, keyboard queue, layout), followed by the 64 KiB as they are, without copying, straight from the 6502’s memory.

Resuming

To resume, you run the .HIB file as a program. The kernel recognises the signature, freezes the 6502, reloads the memory and state, then restarts the 6502 at the resume address.

One more trick: the 6502 always starts where its RESET vector points. So the kernel points that vector, for a moment, at the resume address, then puts the real vector back 200 µs later.

The routine restores S, Y and P, and returns to its caller. The program tells the two cases apart by the value of A: $00 just after saving, $FF on returning from hibernation.

On the board

The test program HIBER.E65 displays a running counter.

  1. Press H: the file ETAT.HIB is written (66,048 bytes: the header and the 64 KiB).
  2. Exit to the shell: the counter disappears.
  3. Type etat.hib: the program resumes, its counter carries on from the saved value (not from zero), and it displays “REPRISE !”.

A second cycle, which replaces the file, resumes just as well.

The PC tests go further: after hibernation, the memory and state are completely scrambled, and resuming must then restore everything identically. An invalid .HIB file is rejected without even freezing the 6502.

Living again alongside the shell

With multitasking, a hibernated machine can be restarted in the background: ETAT.HIB & in the shell, and the program resumes alongside it, with its own screen. See Several programs at once.

How long does it take?

With optimised access to the USB stick (several blocks per command, deferred FAT), writing a 64 KiB file takes about 0.24 s and reading it back about 0.36 s on the test stick.

Sources in the repository : docs/journal/10-fichiers.md, docs/adr/ADR-0014-hibernation.md, docs/journal/11-multitache.md.