Skip to content

Menu exit: wait for the stop line before releasing the C64, and keep DEN on while waiting - #1065

Merged
chrisgleissner merged 8 commits into
GideonZ:masterfrom
JC-000:fix/resume-raster-wait
Nov 11, 2026
Merged

chrisgleissner merged 8 commits into
GideonZ:masterfrom
JC-000:fix/resume-raster-wait

Conversation

@JC-000

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

Copy link
Copy Markdown
Contributor

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 $D012 poll that sees a jump, a pending raster IRQ taken on the wrong line).

Cause, on current master:

software/io/c64/c64.cc:551 and :562:

while ((VIC_REG(18) != rast_lo) && ((VIC_REG(17) & 0x80) != rast_hi)) { ... }

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, so rast_hi is 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, $D011 and starting again if bit 7 changed, so the 311→0 wrap and lines 0 and 256 are read correctly (also in stop()). 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 of resume() (target, wait, restoring compare, $D011 and interrupt enable) is raster_resume() in software/io/c64/raster_wait.h, so the host test runs the same code c64.cc calls. stop() also reads the VIC registers before its printf so the raster does not move on while the UART drains.

Products. c64.cc is 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: runs raster_resume() from software/io/c64/raster_wait.h, the function c64.cc calls in resume(), 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 of raster_read().
  • make mutants: 15 broken variants, each built from a copy of raster_wait.h with one edit (the old condition, $D011 cleared 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:

  • before this change: 584250385 (c64.cc identical to master, no raster_wait.h)
  • condition reverted: 2ce81da1d (the first version of this change, 88a7154, with only the condition put back)
  • previous version: 88a7154 (target = the line before the stop line on every product)
  • this change: 8acde80, and the branch head 748a8fb where a row says so
  • wiring slip: 4bc55c87b (748a8fb with the Ultimate 64 branch passing the cartridge rule)

An interrupts-off program records every step of $D012 that 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 in raster_at_line() put back to the old either-half test (a mutant of the change); and this change.

image Ultimate 64 Elite, two runs C64 Ultimate, two runs
before this change (584250385) far jumps 3 and 3 far jumps 3 and 3
condition reverted (2ce81da1d) far jumps 3 and 3 (a repeat pair: 3 and 3) far jumps 3 and 3
this change (8acde80; 748a8fb also 0 on both) no jump at all, 0 and 0 no jump at all, 0 and 0

Video standard: those runs are NTSC (the devices' setting). With U64 Specific Settings / System Mode set 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 $D012 in 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.

image steps, one per run
previous version (88a7154), NTSC Ultimate 64 Elite 0, 0, 0; C64 Ultimate 0, -1, 0
this change (8acde80), NTSC Ultimate 64 Elite +1, 0, 0, +1; C64 Ultimate +1, +1, +1, +1
this change at the branch head (748a8fb), NTSC Ultimate 64 Elite +1, +2, +1, 0; C64 Ultimate 0, +1, 0, +1
this change (8acde80), PAL Ultimate 64 Elite +1, +1, +1; C64 Ultimate +1, +1, 0

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 in c64.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 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.

JC-000 and others added 2 commits November 7, 2026 14:24
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>
@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 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:

  1. The new test passes without the fix. It includes raster_wait.h and re-implements the resume() loop itself. With software/io/c64/c64.cc reverted to master it still gives 115000 cases and 0 failures. Only the -DRASTER_WAIT_LEGACY builds fail, and those model the old code rather than run it. A test that calls the production wait, or a hardware run with the loop condition reverted on its own, would show the fix is what turns it green.
  2. Release one line early. The description says the release lands one line before the stop line on both devices and was not investigated. The host model asserts the exact line, so the two disagree. A sentence on why this is safe, or the cause, would settle it.

Also, c64.cc is built for the cartridge targets, so the description's statement that it is not should be corrected.

I'm happy to merge once those are addressed.

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

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@JC-000 JC-000 changed the title # Menu exit: wait for the stop line before releasing the C64, and keep DEN on while waiting Menu exit: wait for the stop line before releasing the C64, and keep DEN on while waiting Nov 10, 2026
JC-000 and others added 4 commits November 10, 2026 10:00
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>
@JC-000

JC-000 commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

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.

  1. The test now runs the production sequence. The whole VIC side of resume() (the line to wait for, the $D011 value while waiting, the wait, putting the program's compare, $D011 and interrupt enable back) is raster_resume() in raster_wait.h, a template over a small bus (read register, write register, "has 1 ms passed"). c64.cc calls it with the real VIC registers; make -C target/pc/linux/rasterwait test calls the same function with a model VIC, on every line of PAL (312) and NTSC (263), with the program's raster flag set and clear. It checks the line the C64 resumes on, that DEN stayed on while waiting, and that compare, $D011, interrupt enable and the raster flag end up as they were. 0 failures in 75012 cases, 0 in 28750 cases of the register read (below).
    • Breaking the code makes it fail. make mutants builds 15 variants, each from a copy of raster_wait.h with one edit, and every one fails: the old either-half condition, the bare compare bit 8 in $D011, the $D011 write during the wait reverted, a single (not repeated) register read, $D012 read as $D011, the 1 ms timer inverted, compare bit 8 forced to 0, target without -1 on the cartridges, with -1 on the Ultimate 64, target = the raw raster, a wrong compare written, the flag clear removed, $D011 not restored, interrupt enable not restored, the restore writes in the wrong order.
    • What the host test cannot cover is the few lines in c64.cc that hand the real registers and timer to it (VicRasterBus) and fill the state. Those are covered by hardware: with only the loop condition put back to the old one, the hardware run below fails on both devices.
    • Hardware table below (firmware before the change, the change with only the condition reverted, the change).
  2. The release. fpga/cart_slot/vhdl_source/slot_server_v4.vhd:715-723 (r_no_master, under g_direct_dma, which ultimate_logic_32.vhd:122 labels "Direct DMA for U64") copies c64_stop to the stopped state on every PHI2 fall: on the Ultimate 64 and C64 Ultimate stop and release happen on any line, not on a bad line. The cartridges use slot_master_v4.vhd, which stops and releases on a bad line. I had the target wrong for the first group: waiting for the line before the stop line is right for the cartridges (release on the next bad line, which is the stop line) but one line early where the release is immediate. raster_wait_target() now waits for the stop line itself when U64 is defined, and for the line before it otherwise. On hardware the C64 now resumes on the stop line (below).
  3. Build targets. You are right. c64.cc is built for every target. I built and ran it on the Ultimate 64 Elite and the C64 Ultimate. For the cartridges I compiled the app only (make u2_rv_swonly, make u2plus_swonly, no FPGA synthesis): c64.cc compiles and the whole app links for U2 (riscv) and for U2+ (nios2). Those builds then stop at packaging, which needs FPGA files (rv700dd.chk, ultimate_run.swp). I did not run any cartridge, and did not build the U2 (zpu, microblaze) or U2+L targets.

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

@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 8299768 into GideonZ:master Nov 11, 2026
1 check passed
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.

resume() from the menu releases the C64 on an arbitrary bad line, not where it stopped

2 participants