Skip to content

firmware: a C128-mode launch drops Kernal Replace as the next IO handler - #98

Merged
SensoriumEmbedded merged 1 commit into
SensoriumEmbedded:mainfrom
kfox:fix/c128-skip-c64-handlers
Nov 11, 2026
Merged

SensoriumEmbedded merged 1 commit into
SensoriumEmbedded:mainfrom
kfox:fix/c128-skip-c64-handlers

Conversation

@kfox

@kfox kfox commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

On a TR+, a C128 BASIC program launched in 128 mode no longer gets Kernal Replace as its IO handler. This fixes the case @wesbiggs raised on #96.

The problem

If Special IO is set to Kernal Replace and a C64-mode launch earlier in the session loaded a kernal, KernalBin is still allocated. When the C128 boot ROM writes rCtlRunningPRG, fBusSnoop is HandshakeSnoop, so InitHndlr_KernalReplace() stages KernalCheckArm(). In 128 mode the MMU ignores GAME and EXROM, so the internal KERNAL stays mapped at $E000-$FFFF. KernalCheck then drives KernalBin onto the data bus against the C128's own ROM on every read there, including the MMU registers at $FF00. With the DMA HIRAM sense, which Auto picks on a C128, it also asserts DMA on $00/$01 accesses.

The fix

LaunchC128BasicPrg() replaces Kernal Replace with IOH_None as the next IO handler, for that launch only. A CRT overrides rwRegNextIOHndlr the same way, and InitHndlr_TeensyROM() restores the setting from EEPROM on the next return to the menu. The override comes after both refusal checks, so a refused launch leaves the setting alone. It covers the program launch and Exit to C128 BASIC.

Kernal Replace is the only handler this applies to. TR BASIC is C64-only too, but every menu item associated with it loads at $0801, so none of them takes the C128 path. Handlers that work in 128 mode, such as Swiftlink, MIDI and REU, are left alone. Kernal Replace exists only on the TR+ (Fab04_KernalReplace), so the plain TR compiles none of this.

Verification

  • npm test: 366 pass on macOS. Under Linux GCC (node:24, linux/amd64): 324 pass and 8 skip, where acme isn't installed. The new tests in Source/Teensy/tests/c128-prg.test.js cover a program launch and Exit to C128 BASIC, which drop Kernal Replace; Swiftlink, which is kept; and a refused launch on a C64 or for an oversized program, which keeps Kernal Replace. They fail with the override removed, and with it moved after IOHandlerSelectInit().
  • Builds: build:tr-plus and build:tr (Docker linux/amd64, Teensyduino 1.61.0).
  • Hardware: TR+ in a flat NTSC C128, main image, base d1a7ab7 vs this branch at d3047fe, with Special IO set to Kernal Replace and a JiffyDOS kernal selected. Launched over the remote protocol: a C64 PRG (JiffyDOS banner, Kernal replace Enabled, DMA sense), then a C128 BASIC PRG.
    • Base: the C128 launch ends with Loading IO handler: Kernal Replace. The program still ran, and a checksum of $E000-$FEFF in BANK 15 matched this branch, so this run showed no contention on CPU reads. Most likely the DMA sense judged HIRAM as RAM. The handler was armed all the same.
    • This branch: the C128 launch ends with Loading IO handler: None, the program runs, and the checksum is the same. Exit to C128 BASIC selects None and starts 128 mode. A C64 PRG launched afterwards loads the kernal and selects Kernal Replace again.
    • Not tested: PAL.

Memory

Main image, base d1a7ab7 vs this branch, same build configuration:

TR+ base TR+ branch TR base TR branch
FLASH code 286404 286420 253548 253548
FLASH data 1456804 1456804 1444584 1444584
RAM1 variables 215044 215044 204932 204932
RAM1 code 203576 203576 190536 190536
RAM1 free for local variables 79868 79868 122748 122748
RAM2 unchanged unchanged

MinimalBoot and the extension image are unchanged.

In 128 mode the MMU ignores GAME and EXROM, so the internal KERNAL stays
mapped at $E000-$FFFF. If a C64-mode launch earlier in the session loaded
a kernal, InitHndlr_KernalReplace re-stages KernalCheck when the C128 boot
ROM signals rCtlRunningPRG, and the Teensy then drives KernalBin onto the
bus against the C128's own ROM on every KERNAL read, and asserts DMA for
the HIRAM sense on $00/$01 accesses.

LaunchC128BasicPrg now overrides the Special IO setting for that launch
only, the way a CRT does; InitHndlr_TeensyROM restores it from EEPROM on
the next return to the menu.

Reported by Wes Biggs on SensoriumEmbedded#96.
@kfox
kfox force-pushed the fix/c128-skip-c64-handlers branch from 2493f60 to d3047fe Compare November 11, 2026 04:01
@kfox
kfox marked this pull request as ready for review November 11, 2026 04:32

@SensoriumEmbedded SensoriumEmbedded left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the quick fix, and for the tests that pin it down! :) And thank you @wesbiggs for catching this one on #96, a great find that could have caused C128 bus contention.

I reviewed this merged onto current main (d1a7ab7) and built both targets. There's nothing I'd like you to change before merging.

Memory: TR+ flash code grows 16 bytes, and nothing else changes on either target. The plain TR image is identical, since the change is TR+ only.

Hardware tested

NTSC C128, TR+ main image built from this branch, Special IO = Kernal Replace with a JiffyDOS kernal:

  • A C64 PRG loads the kernal and selects Kernal Replace (JiffyDOS banner)
  • A C128 BASIC PRG launched next, with that kernal still loaded, selects no IO handler and runs in 128 mode
  • Exit to C128 BASIC selects no IO handler and comes up in empty BASIC 7.0
  • A C64 PRG launched after that gets Kernal Replace again
  • A C128 cartridge launched with that kernal loaded runs, and Kernal Replace doesn't arm: the C128 boots on its own kernal

Hardware testing: Just to document. I'll hit these at some point, not a request or requirement! :)

  • PAL C128

Thank you!

@SensoriumEmbedded
SensoriumEmbedded merged commit 24bd3a1 into SensoriumEmbedded:main Nov 11, 2026
4 checks passed
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.

2 participants