Skip to content

firmware: CloseDMA releases /DMA past a bus snoop that takes every cycle - #106

Open
wesbiggs wants to merge 1 commit into
SensoriumEmbedded:mainfrom
wesbiggs:firmware/closedma-snoop
Open

wesbiggs wants to merge 1 commit into
SensoriumEmbedded:mainfrom
wesbiggs:firmware/closedma-snoop

Conversation

@wesbiggs

Copy link
Copy Markdown
Contributor

isrPHI2 acts on DMA_S_StartDisable only in a cycle the bus snoop declines: ISRs.c returns as soon as fBusSnoop takes a cycle, before the PHI2-low state machine. While /DMA holds the CPU, nothing drives the address bus. With Kernal Replace hooked, KernalCheck takes that floating bus as a CPU read of $E000-$FFFF on every cycle except those with BA low (VIC badlines and sprite fetches), where it declines.

With the display on, a badline comes within a raster line, so the release gets through and the problem stays hidden. With the screen blanked ($D011 DEN off) and no sprites, BA never goes low. Then CloseDMA() runs out DMA_HANDSHAKE_CEILING_mS, AbortDMAUnless() runs out its own, and /DMA is dropped from thread mode, unsynchronised to PHI2, after about 100 ms. CloseDMA() then reports a transfer that had in fact landed as failed. The comment above AbortDMAUnless() already names KernalCheck as a snoop that returns true on every cycle; this is a case where that happens on a healthy bus.

What changes

  • CloseDMA() holds the snoop off until the release (DMAControl.c only). While the release is pending, a small FASTRUN wrapper stands in front of fBusSnoop and declines the cycle, so the state machine releases on the next PHI2 low. From the cycle after the release on, it hands every cycle to the snoop it stands in for, so the CPU's first fetch is served as before. The original pointer goes back as soon as the release is seen.
  • The swap only happens when /DMA is known to be held: after a transfer, HoldC64() or an immediate start (TransferComplete, TransferReady, ActiveReady). A CloseDMA() after a start that never asserted runs as before, so a running CPU never loses a snoop cycle.
  • A snoop that changes fBusSnoop itself in the cycles between keeps its change. REU_FF00_W_Check is one that does.
  • isrPHI2 is unchanged. fKernRepl (Fab04_GlobalKernalReplace, off by default) is called before the state machine too, and is not covered here.

Verification

  • npm test passes (413).
  • Both targets build. TR+ main image: RAM1 free-for-locals unchanged (71676), flash code +776 bytes.
  • The wrapper is in ITCM: ldrb, cmp, then either return false or a tail call, with no register saves. isrPHI2's instructions are identical to main's; only its address moves (+28 bytes), with the branch targets and literals that follow from that.
  • Hardware, the same mechanism in a Special IO: we found this in TDrive, a virtual-drive Special IO built on Kernal Replace whose LOAD writes 2 KB parts into C64 RAM by DMA while the KERNAL waits. Test setup: TR+ in an NTSC C128 in C64 mode, and a program that blanks the screen and turns sprites off before loading.
    • Without the fix, every part's CloseDMA() failed, with the longest DMA at 100,994 µs (both ceilings) and the KERNAL being served throughout.
    • With the same wrapper around its CloseDMA() calls, the loads work and the longest DMA is 2,319 µs. The same wrapper also fixed a DMA border write (HoldC64, transfer, CloseDMA) under that handler.
  • Not hardware tested in this form: this upstream build itself, the remote WriteC64Mem / ReadC64Mem path under Kernal Replace with a blank screen (the same CloseDMA(), so expected to have had the same failure), REU emulation, PAL, a C64, and the plain TR.

🤖 Generated with Claude Code

isrPHI2 acts on DMA_S_StartDisable only in a cycle fBusSnoop declines. While
/DMA holds the CPU the undriven address bus is taken by KernalCheck as a
KERNAL read on every cycle except those with BA low (badlines, sprite
fetches). With the screen blanked and no sprites the release never came:
CloseDMA and AbortDMAUnless ran out both ceilings and dropped /DMA
unsynchronised after ~100 ms, reporting a transfer that had landed as failed.

While the release is pending, CloseDMA puts a small FASTRUN wrapper in front
of the snoop that declines the cycle; from the cycle after the release it
passes every cycle to the original snoop, which is restored once the release
is seen. Only when /DMA is known to be held, so a CPU that is running never
loses a snoop cycle. isrPHI2 is unchanged.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant