Repository navigation
Menu exit: restore the raster compare when the raster IRQ was off - #1064
Conversation
Opening the menu stops the C64 on a bad line, and resume() writes the raster compare ($D012, and bit 8 in $D011) back on the way out. stop() found the compare only when the raster interrupt was enabled. With it disabled, resume() wrote whatever an earlier freeze had found, or 0, and $D011 as read, whose bit 7 is the current raster, not the compare. A program that polls the $D019 raster flag, or enables the interrupt later without writing $D012, then sees its line moved. Find the compare in both cases. A compare on a line that exists matches within one frame, so the search now gives up after 22 ms instead of 40. When nothing matches, the compare is past the last line; put back line 511, which never matches either, instead of the line the raster happened to be on. C64 never waits: the C64 gets back the raster compare it had. The search costs menu entry up to 22 ms, while the C64 is already stopped: up to one frame when the compare is on a real line, and the full 22 ms when it is past the last line (40 ms before, where the search already ran). No host test: stop() and resume() live in c64.cc, which has no host build; it is checked on the device. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@JC-000 thanks for tracking this down and for the measured before/after on hardware. The change reads correctly to me, including the lines with raster bit 8 set and a compare that never matches. Before merging I'd like three things:
It composes cleanly with #1065 in either order. Happy to merge once the above is settled. |
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.
|
Thanks for the review. All three points are addressed in the description (the new text is below); the code is unchanged.
Written with Claude Opus and Claude Sonnet in Claude Code. |
chrisgleissner
left a comment
There was a problem hiding this comment.
Thanks. Refs instead of Fixes, the corrected build-target statement and the repeat procedure in the description cover my three points. Approving. It touches different parts of c64.cc than #1065, so the two can merge in either order.
|
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
|
|
This is merged now. Just wanted to thank you for your excellent PRs @JC-000 . Your PR descriptions are also very good and easy to follow. |
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>
Refs #1053
Problem
When the menu or freezer stops the C64 it halts the CPU, and on exit
resume()restores the VIC-II raster compare ($D012plus$D011bit 7, which holds raster bit 8) the running program was using. That compare can be restored stale, and a raster interrupt the program takes afterwards then fires on the wrong line for the rest of the session — not just for one frame. It happens in two situations:The CPU was halted on a bad line, but the program's raster IRQ was off. The usual case: with the screen on,
stop()halts the CPU on a VIC bad line (software/io/c64/c64.cc:451). It reads the raster compare back (determine_d012()) only when the program had its raster interrupt enabled (c64.cc:509); if the IRQ was off it just copies$D011(c64.cc:512), soresume()writes back a stalevic_d012and a$D011whose bit 7 (raster bit 8) is the live raster line rather than the saved compare bit.No bad line was available to halt on. If the screen is blanked or the program runs an effect with no bad lines,
stop()cannot halt on a bad line and falls through to halting on a safe bus read/write (c64.cc:478) or a forced halt (c64.cc:499). In both,determine_d012()is never called, andrestore_io()writes the whole VIC register bank captured at backup time back to the chip (c64.cc:1114), including the raster value that was in$D012at backup — not the program's compare.What changed and why
Two changes in
software/io/c64/c64.cc:stop(), calldetermine_d012()whether or not the program's raster interrupt was enabled. On exitresume()writes the raster compare back either way, and$D019latches a match either way, so the compare has to be recovered either way. The old code recovered it only when the raster IRQ was on; otherwise it copied the live$D011, whose bit 7 is the current raster line, not the saved compare bit.determine_d012()'s search to one frame. It steps the compare looking for the line the program set; a compare on a line that exists matches within one frame (20 ms PAL, 17 ms NTSC), so 22 ms (one partial plus 21 whole 1 ms ticks) is enough, where the old loop ran to 40. If no line matches within a whole frame the compare is past the last visible line and never fires, so it is left that way ($D012 = 0xFF,$D011bit 7 set) instead of being replaced with wherever the raster happened to be.Products.
stop(),resume()anddetermine_d012()are not under#if U64, andc64.ccis built for every target, so this change is in the Ultimate-II family (U2, U2+, U2+L) as well as the Ultimate 64, Ultimate 64 Elite II and C64 Ultimate. Built and run on hardware: the Ultimate 64 Elite (target/u64/nios2) and the C64 Ultimate (target/u64ii/riscv). Not run by me on any cartridge, and no full cartridge image built (that needs the FPGA toolchain; CI builds them). An app-only build of the samec64.cc(make u2_rv_swonly,make u2plus_swonly, on the #1065 tree) compiles and links for U2 riscv and U2+; U2 (zpu, microblaze) and U2+L were not built.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):
c64.cchas no host build and a model would only restate the code. The evidence is the hardware measurement below and review.Hardware.
An interrupts-off program (situation 1) records where
$D019latches across a menu cycle:How to repeat the hardware run (Ultimate 64 or C64 Ultimate; needs a way to type into the C64, press the menu button, and read C64 memory, for example the REST API):
Freeze. (With "Overlay on HDMI" the C64 keeps running and nothing is tested.)$C000, set$C100to 0, and fill$C200-$C6FFwith$EE. TypeSYS49152.$D012 = $05,$D011bit 7 set), and once per frame clears the$D019raster flag, waits for it, and stores$D012and$D011 & $80at the moment it latches. It records 640 frames at$C200-$C6FFand then sets$C100to 1.SYS, press the menu button, wait 2.5 s, press it again. Do not read or write C64 memory while the program runs: a REST memory access stops the CPU too.$C100(must be 1) and$C200-$C6FF. Count the frames whose pair is not$05 $80. Before the change 460 of 640 frames latch on the wrong line ($00 $00, the compare moved to line 0); with it, 0.The machine code, 78 bytes:
78 A9 00 8D 1A D0 A9 05 8D 12 D0 AD 11 D0 09 80 8D 11 D0 A9 00 85 FB A9 C2 85 FC A9 01 8D 19 D0 AD 19 D0 29 01 F0 F9 A0 00 AD 12 D0 91 FB C8 AD 11 D0 29 80 91 FB 18 A5 FB 69 02 85 FB 90 02 E6 FC A5 FC C9 C7 D0 D4 A9 01 8D 00 C1 58 60Standard 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.ccis also edited by open PRs Machine code monitor debugger #705, 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.