Repository navigation
Whole-RAM copies of the C64: suspend the scheduler instead of masking interrupts - #1070
Conversation
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>
|
@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 |
chrisgleissner
left a comment
There was a problem hiding this comment.
@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.
…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>
|
SAVEMEM now stops the C64 for the copy, so the helper and the flag are gone. SAVEMEM now saves through The 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.
The firmware build caught a private-access error that the host fakes hid: The 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
left a comment
There was a problem hiding this comment.
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.
|
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. |
|
Test result for the batch this PR is part of (#1043, #1064, #1065, #1066, #1068, #1070). Firmware: upstream master
|
# Conflicts: # Makefile
# Conflicts: # Makefile
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) andU64Machine::get_all_memoryeach copy 64 KB through the window inside aportENTER_CRITICAL()withC64_DMA_MEMONLY = 1.clear_ramhas its own critical section;get_all_memoryenters it throughbefore_memory_access. Both stop the C64 first.ControlTarget::save_u64_memory(UCI SAVEMEM) has its own critical section and its ownmemcpyfrom the window, and does not stop the C64.What changed and why
clear_ramandget_all_memorydo the 64 KB copy betweenvTaskSuspendAll()andxTaskResumeAll()(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 orC64_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 throughget_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 tou64_config.ccand noc64_window.hhelper is needed; neither is in this PR.C64declaresfriend class ControlTargetso SAVEMEM may callget_all_memory, as the other friends do.The
(void)ram[0xFFFF]read-back inclear_ramis not part of this PR.Products. the Ultimate 64, Ultimate 64 Elite II and C64 Ultimate only (
u64_machine.ccand 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 compileu64_machine.ccandcontrol_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 testandmake -B -C target/pc/linux/ucitargets test(both are inmake host_tests)clear_ramandget_all_memoryon 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 clearedu64/savemem_test.cc) requires the saved file to be the machine's ownget_all_memorycopy and that copy to run onceu64_machine.ccfrom master: c64mem 2 passed, 5 failed (the "scheduler suspended, not interrupts off", "leaves a stopped machine stopped" and both frozen cases)control_target.ccfrom 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 provideC64::get_all_memoryprivate withfriend class ControlTarget, asc64.hdoes; with the friend removed the host build fails, so it would have caught the private-access error that the firmware build foundHardware. The C64 Ultimate ran each image below, loaded over JTAG (RAM only; the flashed image is untouched), with
Command Interfaceset to Enabled (a runtime setting, not saved). A C64 program fills$0400-$FFFFwith 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 ($ABto$D038,$CDto$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.vTaskSuspendAll$D038)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 thec64_window.hhelper. The suspended-scheduler copy inclear_ramandget_all_memoryis the same code in both designs, but those runs were not repeated on f0d0924, and the$D038unlock-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
get_all_memorystops 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.Written with Claude Opus and Claude Sonnet in Claude Code.