Skip to content

Whole-RAM copies of the C64: suspend the scheduler instead of masking interrupts - #1070

Merged
chrisgleissner merged 8 commits into
GideonZ:masterfrom
JC-000:fix/c64-ram-copy-suspend
Nov 12, 2026
Merged

chrisgleissner merged 8 commits into
GideonZ:masterfrom
JC-000:fix/c64-ram-copy-suspend

Conversation

@JC-000

@JC-000 JC-000 commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1059

Problem

Operations that copy the C64's whole 64 KB through the memory window run the entire copy inside one critical section with all interrupts masked: "Reboot (Clr Mem)", the boot-time memory clear, and "Save C64 Memory". On the U64 Elite that is a ~68 ms blackout of the tick, RMII, UART and USB interrupts. The UCI command SAVEMEM does the same copy by itself, from the running machine.

Cause, on current master: U64Machine::clear_ram (software/u64/u64_machine.cc) and U64Machine::get_all_memory each copy 64 KB through the window inside a portENTER_CRITICAL() with C64_DMA_MEMONLY = 1. clear_ram has its own critical section; get_all_memory enters it through before_memory_access. Both stop the C64 first. ControlTarget::save_u64_memory (UCI SAVEMEM) has its own critical section and its own memcpy from the window, and does not stop the C64.

What changed and why

clear_ram and get_all_memory do the 64 KB copy between vTaskSuspendAll() and xTaskResumeAll() (scheduler suspended, interrupts on) instead of a critical section. The C64 is stopped before the copy and resumed after it, as before; a C64 that was already stopped, or the freezer menu, is left as it was, and in the freezer menu the 1K-4K RAM still comes from the backups. With the scheduler suspended no other task can touch the window or C64_DMA_MEMONLY, which is what the critical section gave, and interrupts are served. The interrupt blackout is removed.

SAVEMEM (ControlTarget::save_u64_memory) now saves through get_all_memory, the copy "Save C64 Memory" uses, instead of copying from the window itself. The C64 therefore pauses for about 68 ms per SAVEMEM (a code-comment estimate, not measured), and the saved file is one consistent moment of the machine. The one interrupt that uses the window, U64Config::unlock_irq, is raised by the C64's own unlock write, and a stopped C64 cannot write it, so no change to u64_config.cc and no c64_window.h helper is needed; neither is in this PR. C64 declares friend class ControlTarget so SAVEMEM may call get_all_memory, as the other friends do.

The (void)ram[0xFFFF] read-back in clear_ram is not part of this PR.

Products. the Ultimate 64, Ultimate 64 Elite II and C64 Ultimate only (u64_machine.cc and the SAVEMEM command are built only for those; not for the Ultimate-II cartridges). Built and run on the C64 Ultimate (u64ii). The Ultimate 64 Elite and Elite II targets also compile u64_machine.cc and control_target.cc; I did not build them for this change.

How I tested it

Host: macOS, with the device-free tests run in an Ubuntu 24.04 container (g++ 13.3). The hardware runs used the test harness from the same machine.

Host tests (device-free, in an Ubuntu 24.04 container, g++ 13.3):

  • make -B -C target/pc/linux/c64mem test and make -B -C target/pc/linux/ucitargets test (both are in make host_tests)
    • with the change: c64mem 7 passed, 0 failed; ucitargets 8 passed, sliced_transfer 18 passed, u64 savemem 1 passed
    • the c64mem cases check, for clear_ram and get_all_memory on a running, a stopped and a frozen machine: all 64K are copied in one suspended stretch, none inside a critical section, the C64 is stopped and resumed outside that stretch, and the window mode is left cleared
    • the SAVEMEM case (u64/savemem_test.cc) requires the saved file to be the machine's own get_all_memory copy and that copy to run once
    • RED, with u64_machine.cc from master: c64mem 2 passed, 5 failed (the "scheduler suspended, not interrupts off", "leaves a stopped machine stopped" and both frozen cases)
    • RED, with control_target.cc from master: the SAVEMEM test does not build, because master copies from the window itself (portENTER_CRITICAL, C64_DMA_MEMONLY), which the test's fakes do not provide
    • the host fake declares C64::get_all_memory private with friend class ControlTarget, as c64.h does; with the friend removed the host build fails, so it would have caught the private-access error that the firmware build found

Hardware. The C64 Ultimate ran each image below, loaded over JTAG (RAM only; the flashed image is untouched), with Command Interface set to Enabled (a runtime setting, not saved). A C64 program fills $0400-$FFFF with a known pattern and issues SAVEMEM five times: the first with interrupts off, four with a 1 kHz interrupt whose handler writes the unlock key ($AB to $D038, $CD to $D036). Each saved file is fetched and compared byte for byte with the pattern. Two legs per image; the device's reported git hash was checked before each leg. Firmware 3.15a, Ultimate FPGA 126, C64 core 1.50.

image role files equal to the pattern, leg 1 leg 2 pause
bb3077254 bench build of the earlier no-stop design: copies from the running machine under vTaskSuspendAll 1 of 5 (the 4 with interrupts differ at $D038) 1 of 5 (same) none
3db34d2 master 5 of 5 5 of 5 not measured
f0d0924 this PR 5 of 5 5 of 5 not measured

The differing files each differ in one byte, $D038 (00 in the file, A9 in the pattern). Master also passes this test, so the test does not tell master from this PR; it shows that the earlier no-stop design differs from the pattern and that this PR does not. The pause is the code-comment estimate above; the script does not measure it.

Earlier Ultimate 64 Elite and C64 Ultimate runs (images ac0304316 and b0f40a17b: the runtime profiler no longer showing the ~68 ms interrupts-off stretch at clear_ram/get_all_memory, and a 100 ms pinger losing 0 pings across Clear Memory runs) were taken on the earlier design, which also had the c64_window.h helper. The suspended-scheduler copy in clear_ram and get_all_memory is the same code in both designs, but those runs were not repeated on f0d0924, and the $D038 unlock-path check from them no longer applies because the helper is gone. The standard-profile and soak results were also taken on the earlier design: 71 of 72 suites on master and with the change on both devices, the one failure (iec-partition-config) the same on both; the soaks (mount-cache-leak, listener-soak, network-connection) passed on both.

Untested

  • Ultimate-II cartridge (U2+ / U2+L): not tested. I do not have one. Not built either for the cartridges, because the build environment I use cannot build those targets; CI builds them.
  • The scheduler is still suspended about 68 ms per copy, so other tasks still see a late reply; only the interrupt blackout is removed. The C64 now also pauses for that time during SAVEMEM, which stopped nothing before.
  • "Save C64 Memory" (the menu) already stopped the C64 on master, because get_all_memory stops it, so its snapshot was one moment of the machine before and is after. Only SAVEMEM could produce a torn snapshot, and it no longer can. I did not measure the menu's pause either.
  • The Ultimate 64 Elite and Elite II were not run against f0d0924; the Elite results above are from the earlier design.

Written with Claude Opus and Claude Sonnet in Claude Code.

U64Machine::clear_ram (at boot and from Reboot (Clr Mem)) and
get_all_memory (Save C64 Memory) each copy all 64K of C64 RAM through
the memory window, about 68 ms, inside portENTER_CRITICAL. The UCI
command SAVEMEM makes the same copy on its own, from the running
machine.
On a U64 Elite, timing the interrupts-off sections with a free-running
counter put 68 ms at clear_ram: the tick and the network, UART and USB
interrupts waited for the copy.

The critical section kept other tasks off the window and
C64_DMA_MEMONLY for the copy. vTaskSuspendAll does that part alone:
no other task runs until xTaskResumeAll, and interrupts are served
(what they wake runs after the copy). The copy stays in one piece, so
everything that could not interleave with it before still cannot.
clear_ram also reads the last byte back before the mode changes, as
poke_visible does, since a window write does not stall on the bus.

UCI SAVEMEM makes the same copy, from the running machine, and gets
the same change; it still does not stop the C64, so the program that
issued it keeps its music and raster.

With interrupts on, one interrupt handler can meet the copy:
U64Config::unlock_irq pokes $D038 through the window, which under
MEMONLY=1 reaches RAM rather than the I/O register. The copies now
mark themselves with c64_ram_copy_active (u64/c64_window.h), raised
and lowered together with the mode in a two-store critical section,
inside the suspended stretch. unlock_irq writes through
c64_isr_poke_io: with no copy under way that is the same single
C64_POKE as before, plus one flag load and branch (no window read,
no mode access); during a copy it
lets the task's last window write land, switches to bus mode, pokes,
lets that land and sets MEMONLY back to 1. Both reads are of $0000,
RAM in either mode, since a read of an I/O register can have side
effects. No other interrupt handler uses the window.

Risk: the scheduler is still held for the 68 ms copy, so tasks wait as
they did; only interrupts no longer do. An unlock during a copy (the
C64 is stopped then) takes the longer path in the interrupt handler;
that path rests on the window behaving as poke_visible describes and
is the part still to be shown on hardware. SAVEMEM still copies from
a running C64, so its image can be torn, as before.

Host tests:
- target/pc/linux/c64mem (new, in make host_tests) builds
  u64_machine.cc and c64_window.h against a simulated window. It
  records the bytes moved inside critical sections and suspended
  stretches (65536 in one critical section before, none now), and it
  models a posted window write for the interrupt poke.
- target/pc/linux/ucitargets builds control_target.cc with U64 to
  check that SAVEMEM copies with the scheduler suspended.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chrisgleissner

Copy link
Copy Markdown
Collaborator

@JC-000 The same comment applies here as elsewhere: If we see a problem affecting the firmware, we should not just step around it, but address it so that others can benefit from the infrastructure fix. It seems you kept running into the same issue with an E2E test that failed for iec-partition-config. It would be good to fix this. All tests for all profiles (not just the standard profile - but all profiles) passed for me when I last tried them about 3 weeks ago on a Kubuntu 24.04 machine. It may be that some fell into disrepair with other changes, or that they fail on your environment, but that is not normal and should be fixed.

@JC-000

JC-000 commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

The iec-partition-config failure was a test-side problem on master, in overlay mode. It is fixed in #1081, which passes on both an Ultimate 64 Elite and a C64 Ultimate, in overlay and freeze mode. Details are in #1081.

Written with Claude Sonnet and Claude Opus in Claude Code.

@chrisgleissner chrisgleissner added bug Existing behavior is incorrect, broken, or regressed; includes supported documentation defects. system CPU, turbo, reset, core memory, boot/update/recovery, configuration, RTC, LEDs, and firmware builds. labels Nov 10, 2026

@chrisgleissner chrisgleissner left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JC-000 thanks for the measurements. The suspended region itself is sound: it only holds memcpy and nesting-safe critical sections, so nothing blocks or calls a FreeRTOS function that is not allowed under a suspended scheduler. With u64_machine.cc, u64_config.cc and control_target.cc reverted to master, the host tests fail (c64mem 5 failed, savemem 2 failed), so they check the change.

Requesting changes on one point. The branch that switches the window mode while a copy is active can only run when the C64 is running during a copy. clear_ram and get_all_memory stop the C64, so that leaves SAVEMEM (control_target.cc:534), which does not. Your hardware runs (Clear Memory with a pinger) never execute that branch, and the host test uses your own model of the window. Please either run SAVEMEM on the C64U with the C64 running and an unlock interrupt forced pending, and compare the saved file with the RAM pattern on master and on this branch, or stop the machine in save_u64_memory, which would make the helper and the flag unnecessary.

Also, the (void)ram[0xFFFF] posted-write flush in clear_ram is a separate fix from the latency change. Please say why it belongs here, or move it. #1069 carries the same harness commit as this PR, so whichever merges second will need master merged in.

@chrisgleissner
chrisgleissner self-requested a review November 10, 2026 12:51
@JC-000 JC-000 changed the title # Whole-RAM copies of the C64: suspend the scheduler instead of masking interrupts Whole-RAM copies of the C64: suspend the scheduler instead of masking interrupts Nov 10, 2026
JC-000 and others added 4 commits November 10, 2026 09:56
…round

The branch let the unlock interrupt handler put the memory window's mode
back during a copy, because the scheduler-suspended copy leaves
interrupts on and U64Config::unlock_irq pokes $D038 through the window.
That was needed only for SAVEMEM, which copied from the running C64: the
other two whole-RAM copies (clear_ram, get_all_memory) stop the C64
first, and only the C64's own unlock write raises that interrupt.

SAVEMEM now saves through U64Machine::get_all_memory, the copy "Save C64
Memory" uses. It stops the C64 for the copy, so the file is one moment of
the machine, and no unlock interrupt can come while the scheduler is
suspended. The helper (u64/c64_window.h), its flag and the change to
unlock_irq are gone; u64_config.cc is back to master.

Also move the read-back flush out of clear_ram: it is a separate
posted-write question that master has as well, and does not belong in a
latency change.

Tests: the c64mem host test loses the window model and the interrupt
poke cases and keeps the scheduler-suspended and stop/resume checks; the
SAVEMEM host test now requires the file to be the machine's own copy
(it fails against master's control_target.cc, which copies from the
window itself).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
get_all_memory is private in C64; the host fakes declare it public, so
only the firmware build saw the access error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
io/c64/c64.h keeps get_all_memory private and lists ControlTarget as a
friend. The fake declared it public, so the host build accepted a call the
firmware build rejected. The fake now has the same access and the same
friend, so the host build fails for that call too.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@JC-000

JC-000 commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

SAVEMEM now stops the C64 for the copy, so the helper and the flag are gone.

SAVEMEM now saves through U64Machine::get_all_memory, the copy "Save C64 Memory" already uses. That stops the C64 first, as clear_ram and get_all_memory do, so only the C64's own unlock write can raise the interrupt that uses the window, and a stopped C64 cannot write it. The C64 now pauses for the copy, about 68 ms per SAVEMEM by the estimate in the copy's code comment (not measured). That is deliberate: the saved file is one consistent moment of the machine instead of a torn snapshot, and no helper or flag is needed to keep the unlock interrupt out of the window. u64/c64_window.h, c64_ram_copy_active and the change to U64Config::unlock_irq are removed; u64_config.cc is back to master. The host tests drop the window model and keep the checks that each copy runs with the scheduler suspended, outside any critical section, with the C64 stopped and resumed around it. The SAVEMEM host test now requires the file to be the machine's own copy. Against master's control_target.cc it does not build (master copies from the window itself, which the test's fakes do not provide), so the failing case for SAVEMEM is the hardware run below, not a host test.

The (void)ram[0xFFFF] read-back in clear_ram is removed from this PR. It is a separate question about a posted window write that master has as well, and it does not belong in a latency change.

Hardware (C64 Ultimate, RAM loads over JTAG, settings in RAM only): each leg has the C64 fill $0400-$FFFF with a known pattern and run SAVEMEM five times, the first with interrupts off and four with a 1 kHz interrupt that writes the unlock key. Each saved file is compared byte for byte with the pattern. Two legs per image.

image role files equal to the pattern, leg 1 leg 2 pause
bb3077254 bench build of the earlier no-stop design: copies from the running machine under vTaskSuspendAll 1 of 5 (the 4 with interrupts differ at $D038) 1 of 5 (same) none
3db34d2 master 5 of 5 5 of 5 not measured
f0d0924 this PR 5 of 5 5 of 5 not measured

The firmware build caught a private-access error that the host fakes hid: get_all_memory is private in C64, and the fake declared it public. It is fixed in f0d0924 (friend class ControlTarget), and the fake now has the same access as the real class (fb54c2f), so the host build fails without the friend.

The iec-partition-config failure is fixed in #1081, which is merged; nothing further is needed here. Master is merged into the branch, and the test harness commit that #1069 also carried is now only in master, so the branch has no duplicate of it.

Written with Claude Opus and Claude Sonnet in Claude Code.

Makefile host_tests: keep both the c64mem and the new socketdma test lines.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@chrisgleissner chrisgleissner left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. SAVEMEM now saves through get_all_memory with the C64 stopped, so the window workaround and the flag are gone, and the unrelated read-back in clear_ram is out. The hardware table shows the case I asked about: 5 of 5 files equal the pattern with the unlock interrupt firing, against 1 of 5 for the earlier design. The pause of about 68 ms per SAVEMEM is an estimate, not a measurement; that is not a blocker. Approving.

@chrisgleissner chrisgleissner self-assigned this Nov 11, 2026
@chrisgleissner

Copy link
Copy Markdown
Collaborator

I'm assigning this to myself. I'm investigating whether it is merge ready by running the standard E2E profile locally on an Ultimate 64 Elite and an Ultimate II+L, with this PR combined with the other approved bug fixes, and comparing against master. I'll report the result here.

@chrisgleissner

Copy link
Copy Markdown
Collaborator

Test result for the batch this PR is part of (#1043, #1064, #1065, #1066, #1068, #1070).

Firmware: upstream master b57d894a0 plus these six PRs (build 5c1492908), compared with plain master.
Devices: Ultimate 64 Elite (JTAG, from RAM) and Ultimate II+L in a C64 Ultimate (flashed). Standard E2E profile, overlay mode.

Run Ultimate 64 Elite Ultimate II+L in C64 Ultimate
Batch build, full standard profile 67 of 72 suites passed 39 of 48 suites passed
Failed suites rerun after installing the missing host Python modules (tests/requirements.txt) all 5 pass 6 of 8 fail
Master, same suites all 5 pass the same 6 of 8 fail

@chrisgleissner
chrisgleissner merged commit 18bea13 into GideonZ:master Nov 12, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Existing behavior is incorrect, broken, or regressed; includes supported documentation defects. system CPU, turbo, reset, core memory, boot/update/recovery, configuration, RTC, LEDs, and firmware builds.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Whole-RAM C64 copies hold all interrupts off for ~68 ms (Clear Memory, Save C64 Memory)

2 participants