Repository navigation
Menu exit: wait for the stop line before releasing the C64, and keep DEN on while waiting - #1065
Conversation
Entering the menu stops the C64 on a bad line. On the way out, resume() is meant to wait until the raster reaches the line before it, so that the gateware, which releases the CPU on the next bad line, lets it run on from about the line it stopped on. The wait looped while the low byte did not match AND bit 8 did not match, so it ended as soon as either matched. Bad lines are all below line 256, so the wait ended at once anywhere in lines 0-255: the C64 ran on from whatever bad line came next, up to a frame away from where it stopped, and a raster IRQ pending at the stop was taken there. Wait until both parts match (raster_at_line()). $D011 is read before $D012: the other order reads the step from line 311 to line 0 as line 55, which is the target for a stop on line 56. The wait is still capped at 40 ms; it now takes up to one frame. C64 never waits: the wait ends on the line before the stop line, and measured on hardware the C64 is consistently released one line before the line it stopped on, instead of up to a frame away, so raster-timed code sees a jump of one line at most; the extra wait is at most a frame while it is already stopped. The new host test target/pc/linux/rasterwait models the loop against a moving raster for every bad line and start line; make legacy runs the old condition, which fails. It is part of make host_tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When the raster flag was set at the stop, resume() moves the compare to the line before the stop line and waits for it, and it wrote $D011 as just the compare's bit 8. That also cleared DEN. Now that the wait can last up to a frame, it often crosses line $30, and a frame with DEN off at line $30 has no bad lines: the gateware, which releases the CPU on a bad line, released it a frame late, after a blank frame. Change only bit 7 of the program's $D011 (d011_while_waiting()). stop() printed to the UART before it read the raster line, so the line it recorded, and resume() waits for, could be tens of lines late. Read the registers first. C64 never waits: the C64 runs on from one line before the line it stopped on (the release the previous change gives), without a lost frame. Test: target/pc/linux/rasterwait models bad lines (DEN at line $30) and the release; make legacy-d011 clears DEN during the wait and fails. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@JC-000 thanks for the careful analysis. The loop condition was wrong, and the fix, including keeping DEN on while waiting, looks right to me. Two points before merging:
Also, I'm happy to merge once those are addressed. |
chrisgleissner
left a comment
There was a problem hiding this comment.
Thanks @JC-000. Requesting changes for the points in my comment above; I will merge once they are addressed.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The test re-implemented the resume() loop through raster_wait.h, so it passed with c64.cc reverted to master. Move the whole wait into raster_wait.h as raster_wait_for_line(), a template over the register and timer access. c64.cc calls it with the VIC registers and the 1 ms timer; the host test calls the same function with a model VIC. The legacy builds now edit a copy of raster_wait.h back to the old code (old condition, bare compare bit 8 in $D011, $D012 read before $D011), so each failure comes from the code c64.cc runs. make test also checks that c64.cc calls raster_wait_for_line() and holds no loop of its own. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The comments assumed the gateware releases the CPU on the next bad line, so the stop line itself. On the Ultimate 64 Elite and the C64 Ultimate the C64 resumes on the line the wait ends on, one line before the stop line (measured on both with a program that logs every $D012 change). Only the cartridge gateware, which is in this tree, releases on the next bad line. Comments only. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…top line on the U64 H1: the test called only the wait loop, so the DEN write, the target line and the compare/flag/enable restore were not covered. raster_resume() in raster_wait.h is now the whole sequence over a Bus (register read/write and the 1 ms timer); c64.cc calls it and the host test runs the same function against a model VIC on every line of PAL and NTSC. The call-site grep is gone; make mutants builds 14 broken variants from copies of raster_wait.h and all fail. H2: slot_server_v4.vhd (g_direct_dma, used for the Ultimate 64) stops and releases on any line, so on the U64 builds the wait target is the stop line itself; the cartridges (slot_master_v4.vhd) release on the next bad line and keep the line before. #if U64 picks. M1: raster_read() reads $D011, $D012, $D011 again and retries if bit 7 changed; stop() and the wait use it, so a stop on line 0 or 256 is no longer misread. L1/L3: no unused dummy initialiser; the bus is in an anonymous namespace. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… a survivor The U64 check is the exact stop line. The restore writes are checked in order ($D012, $D011, the flag clear, $D01A) and a 15th mutant swaps them. make mutants exits non-zero if any mutant survives. raster_read() retries at most four times. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
Thanks for the review. I reworked the change so that the test runs the production code, and found the reason for the early release. New commits are on the branch; the description changes are below.
Written with Claude Opus and Claude Sonnet in Claude Code. |
Makefile host_tests: keep both the rasterwait 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. The VIC side of resume() is now raster_resume() in raster_wait.h and c64.cc calls it, so the host test runs the production sequence, and all 15 mutants fail. The early release is explained from slot_server_v4.vhd (immediate on the Ultimate 64 and C64 Ultimate, next bad line on the cartridges). Approving. Hardware covers the Elite and C64 Ultimate; the cartridge targets are compiled only, as the description says.
|
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
|
Brings in GideonZ#1043 (send the whole HTTP request, give up on a stalled server), GideonZ#1064, GideonZ#1065 and GideonZ#1068, which this change is stacked on. Conflicts, all from this branch carrying the pre-merge form of GideonZ#1043: - build.yml, test_http_target.cc, http_request.cc, send_all.h, send_all_test.cpp, test_http_target/Makefile: kept this branch's side, which is upstream's plus this PR's own change. - io/network/tests/Makefile: took upstream's (only target order differed). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Merges the updated fix/uci-abort-reset-deferral (890ef6d), which carries upstream master with GideonZ#1043, GideonZ#1064, GideonZ#1065 and GideonZ#1068 merged in. Conflict: software/io/network/tests/Makefile, only in the target order (this branch had send-all after sys-arch-red, upstream before it). Kept upstream's order and added the network-target target and variables. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Fixes #1054
Problem
When the C64 is stopped for the menu/freezer and then resumed, raster-timed code can run on from the wrong line for the rest of that frame: a one-frame glitch per menu exit (torn raster splits, a
$D012poll that sees a jump, a pending raster IRQ taken on the wrong line).Cause, on current master:
software/io/c64/c64.cc:551and:562:The loop ends when either half of the raster compare matches (
&&between two "not equal" tests).stop(true)only ever stops on a bad line, sorast_hiis always 0 and the wait falls straight through on lines 0-255. The C64 is then released (at once on the Ultimate 64 and C64 Ultimate, on the next bad line on the cartridges), so it resumes on whatever line the wait fell through on, up to a frame away from where it stopped.What changed and why
Wait until both halves of the raster compare match, reading
$D011,$D012,$D011and starting again if bit 7 changed, so the 311→0 wrap and lines 0 and 256 are read correctly (also instop()). Keep DEN set while waiting. The line waited for is the stop line on the Ultimate 64 and C64 Ultimate, where the gateware releases the CPU at once (fpga/cart_slot/vhdl_source/slot_server_v4.vhd:715-723,g_direct_dma), and the line before it on the cartridges, which release on the next bad line (slot_master_v4.vhd). The whole VIC side ofresume()(target, wait, restoring compare,$D011and interrupt enable) israster_resume()insoftware/io/c64/raster_wait.h, so the host test runs the same codec64.cccalls.stop()also reads the VIC registers before itsprintfso the raster does not move on while the UART drains.Products.
c64.ccis built for every target. Run on hardware: the Ultimate 64 Elite and the C64 Ultimate. Compiled and linked as part of an app-only build (make u2_rv_swonly,make u2plus_swonly): U2 riscv and U2+. Not built: U2 (zpu, microblaze) and U2+L. Not run on any cartridge; I do not have one. On the cartridges the wait targets the line before the stop line, because they release on the next bad line (slot_master_v4.vhd); on the Ultimate 64 and C64 Ultimate it targets the stop line, because stop and release are immediate there (slot_server_v4.vhd,g_direct_dma).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, run before and after the change):
make -B -C target/pc/linux/rasterwait test: runsraster_resume()fromsoftware/io/c64/raster_wait.h, the functionc64.cccalls inresume(), against a model VIC on every line of PAL and NTSC, for both release behaviours, with the raster flag set and clear. 0 failures in 75012 cases; 0 in 28750 cases ofraster_read().make mutants: 15 broken variants, each built from a copy ofraster_wait.hwith one edit (the old condition,$D011cleared during the wait, a single register read, a wrong target, and so on); all 15 fail; it exits non-zero if one survives.Hardware. Ultimate 64 Elite and C64 Ultimate, Ultimate FPGA 126, C64 core 1.50. The menu-exit runs below are cold RAM loads of these images, each holding only what its row says:
c64.ccidentical to master, noraster_wait.h)An interrupts-off program records every step of
$D012that is not to the next line, across 3 menu open/close cycles ("far" = more than 2 lines). Three firmware images per device: the firmware before this change, which has no raster wait fix at all; this change with only the condition inraster_at_line()put back to the old either-half test (a mutant of the change); and this change.Video standard: those runs are NTSC (the devices' setting). With
U64 Specific Settings / System Modeset to PAL (a runtime setting, not saved) this change gives no jump at all in 2 runs on each device. I did not run the earlier images in PAL.Where the C64 resumes. A second program reads
$D012in a tight loop together with the high byte of CIA timer A, and records an event whenever the timer jumps (the 2.4 s menu freeze) or the line moves by more than 3. Every run has its freeze event. "Step" is the first line read after the freeze minus the last line read before it. One loop iteration is about 64 cycles against 63 or 65 per line, so a C64 that resumes on the stop line shows 0 or +1; one released early shows -1 or 0; one released late shows +1 or +2.Reading it: one step in 22 runs of this change is +2 (a bad-line stall can cost the loop a line), none is larger, and the 0 steps show it is not late. The interrupts-off program from the table above records no jump at all on any run of this change (24 menu exits, NTSC and PAL, both devices), which a release one line early would show as a step back, as it did on the previous version (jumps of -1 in 15 of 17, +2 in 2, over 16 menu exits). So the C64 resumes on the stop line, to the resolution of the loop (a line, not a cycle).
What the hardware runs cover, and what they do not. The host test runs
raster_resume(); the glue inc64.cc(the bus that reads the real registers and the timer, and the fill of the state) is not host-tested. To check that glue, I built the change with one slip in it: the Ultimate 64 branch passing the cartridge rule (target = stop line - 1). The trace above does not separate it reliably (steps +1, +1, -1, 0 on the C64 Ultimate and +1, 0, 0, +1 on the Ultimate 64 Elite, which overlap the exact range, because 0 is possible for both). The interrupts-off program does: the slip shows jumps of -1 (3 jumps in 3 menu exits on the Ultimate 64 Elite, 2 on the C64 Ultimate) where this change shows none. So the hardware runs catch a wrong target in the glue, via that program, in one run per device; they do not catch other slips in the glue, and the host test does not see the glue at all.A wiring slip that the line trace cannot see (for example the interrupt-enable value or the order of the restore writes) is covered by the host test only where the model sees it, and not on hardware.
The standard profile and soaks ran on ac0304316 (Elite) and b0f40a17b (C64 Ultimate), builds that carried the first version of this change with the rest of the series.
Standard profile (
./run-tests --profile standard), red = master, green = a build carrying this landing set of fixes (this change included), report frompython3 tools/e2e_report.py:iec-partition-config: it stops at a key-send step because the upstream test's REST backend has noF2key alias (rest_backend.py:556), a test-side limitation, not an app difference.iec-partition-config, here at a "menu screen unavailable" step that also fails on master, so it is not from these changes.Soaks pass on both devices: mount-cache-leak, listener-soak, network-connection.
Untested
c64.ccthat pass the real registers and the 1 ms timer toraster_resume(); the hardware runs do.c64.ccis also edited by open PR Machine code monitor debugger #705 (a textual conflict), Magic Desk Plus cartridge support (#727) #844 and Save a changed cartridge back to the file it came from #929.Written with Claude Opus 5.5 in Claude Code.