Skip to content

Menu exit: restore the raster compare when the raster IRQ was off - #1064

Merged
chrisgleissner merged 1 commit into
GideonZ:masterfrom
JC-000:fix/resume-raster-compare
Nov 11, 2026
Merged

chrisgleissner merged 1 commit into
GideonZ:masterfrom
JC-000:fix/resume-raster-compare

Conversation

@JC-000

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

Copy link
Copy Markdown
Contributor

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 ($D012 plus $D011 bit 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:

  1. 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), so resume() writes back a stale vic_d012 and a $D011 whose bit 7 (raster bit 8) is the live raster line rather than the saved compare bit.

  2. 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, and restore_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 $D012 at backup — not the program's compare.

What changed and why

Two changes in software/io/c64/c64.cc:

  • In stop(), call determine_d012() whether or not the program's raster interrupt was enabled. On exit resume() writes the raster compare back either way, and $D019 latches 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.
  • Bound 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, $D011 bit 7 set) instead of being replaced with wherever the raster happened to be.

Products. stop(), resume() and determine_d012() are not under #if U64, and c64.cc is 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 same c64.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):

  • None: c64.cc has no host build and a model would only restate the code. The evidence is the hardware measurement below and review.

Hardware.

Device Firmware Ultimate FPGA C64 core
Ultimate 64 Elite ac0304316 126 1.50
C64 Ultimate b0f40a17b 126 1.50

An interrupts-off program (situation 1) records where $D019 latches across a menu cycle:

RED GREEN
frames with the IRQ on the wrong line (of 640) 460 0

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):

  1. Set User Interface Settings / Interface Type to Freeze. (With "Overlay on HDMI" the C64 keeps running and nothing is tested.)
  2. Reset the C64. Write the program below to $C000, set $C100 to 0, and fill $C200-$C6FF with $EE. Type SYS49152.
  3. The program turns VIC interrupts off, sets the raster compare to line 261 ($D012 = $05, $D011 bit 7 set), and once per frame clears the $D019 raster flag, waits for it, and stores $D012 and $D011 & $80 at the moment it latches. It records 640 frames at $C200-$C6FF and then sets $C100 to 1.
  4. About 3 s after the 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.
  5. After 20 s read $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.
        sei
        lda #$00 : sta $d01a        ; VIC interrupts off
        lda #$05 : sta $d012        ; compare = line 261
        lda $d011 : ora #$80 : sta $d011
        lda #$00 : sta $fb : lda #$c2 : sta $fc
frame:  lda #$01 : sta $d019        ; clear the raster flag
wait:   lda $d019 : and #$01 : beq wait
        ldy #$00 : lda $d012 : sta ($fb),y
        iny : lda $d011 : and #$80 : sta ($fb),y
        clc : lda $fb : adc #$02 : sta $fb : bcc +: inc $fc
+       lda $fc : cmp #$c7 : bne frame
        lda #$01 : sta $c100 : cli : rts

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 60

Standard profile (./run-tests --profile standard), red = master, green = a build carrying this landing set of fixes (this change included), report from python3 tools/e2e_report.py:

  • U64 Elite: 71 of 72 suites pass on master and 71 of 72 with the fixes. The one failure is the same on both, iec-partition-config: it stops at a key-send step because the upstream test's REST backend has no F2 key alias (rest_backend.py:556), a test-side limitation, not an app difference.
  • C64 Ultimate: 71 of 72 on master and 71 of 72 with the fixes. The one failure is again 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

Written with Claude Opus 5.5 in Claude Code.

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>
@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 self-requested a review November 10, 2026 12:30
@chrisgleissner

Copy link
Copy Markdown
Collaborator

@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:

  1. Evidence in the tree. c64.cc has no host build, so the only proof is your 640-frame run, and that harness is not in the repo. Could you add it, or describe the procedure in the PR so it can be repeated on a U64 and a C64U?
  2. The issue link. The description says the no-bad-line case is not fixed. Fixes #1053 would close the issue with that case still open, so Refs or a split issue fits better.
  3. Build targets. stop(), resume() and determine_d012() are not under #if U64, so the cartridge targets compile this change as well. Please correct that statement and list which targets you ran it on.

It composes cleanly with #1065 in either order. Happy to merge once the above is settled.

@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 @JC-000. Requesting changes for the points in my comment above; I will merge once they are addressed.

@JC-000 JC-000 changed the title # Menu exit: restore the raster compare when the raster IRQ was off Menu exit: restore the raster compare when the raster IRQ was off Nov 10, 2026
@JC-000

JC-000 commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review. All three points are addressed in the description (the new text is below); the code is unchanged.

  1. Evidence in the tree. The 640-frame run is a 6502 program plus a few steps, so I have written the procedure into the description rather than adding a harness to the repo. It needs nothing beyond a way to type SYS49152, press the menu button, and read C64 memory (REST or any other route), so it repeats as written on a U64 and on a C64U. See "How to repeat the hardware run" below. If you would rather have the script in the tree, say where it belongs and I will add it.
  2. Issue link. Changed Fixes #1053 to Refs #1053. The no-bad-line case stays open and is described under "Untested".
  3. Build targets. You are right. stop(), resume() and determine_d012() are not under #if U64, and c64.cc is part of the Makefiles of every product target (U2, U2+, U2+L, Ultimate 64, Ultimate 64 Elite II / C64 Ultimate), so the cartridge builds compile this change too. I built and ran it for two of them (below). The full cartridge images cannot be built here (they need the FPGA toolchain on the CI runner), but the app-only builds can: make u2_rv_swonly and make u2plus_swonly compile c64.cc and link the whole app for U2 (riscv) and U2+ (nios2) and then stop at packaging, which needs FPGA files. This was checked on the Menu exit: wait for the stop line before releasing the C64, and keep DEN on while waiting #1065 tree, which has the same c64.cc structure; I did not run it on the Menu exit: restore the raster compare when the raster IRQ was off #1064 branch. I did not build U2 (zpu, microblaze) or U2+L, and I have not run any cartridge.

Written with Claude Opus and Claude Sonnet in Claude Code.

@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. 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.

@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

Copy link
Copy Markdown
Collaborator

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.

JC-000 added a commit to JC-000/1541ultimate that referenced this pull request Nov 12, 2026
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>
JC-000 added a commit to JC-000/1541ultimate that referenced this pull request Nov 12, 2026
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>
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.

2 participants