        ORG     &H3100

CH08_COLD:
        PRE     SS,&H0100
        PST     UA,&H14
; Bootstrap bank-call template in RAM using frozen BOOT/keystone idiom.
        PRE     IX,RCS_TOP
        PRE     IY,RCS_END-1
        PRE     IZ,RCS
        PST     UA,&H44
        BUP
        PST     UA,&H14
 ; Cold-only two-byte ROM2 -> RAM1 sentinel copy.
; PRE IX/IY select source, PRE IZ selects destination.
; UA 44 selects RAM1 for IZ; BUP follows proven RCS copy idiom.
        PRE     IX,C8_MARK
        PRE     IY,C8_MARK+1
        PRE     IZ,&H2E00
        PST     UA,&H44
        BUP
        PST     UA,&H14
        PRE     SS,SSPTP
        PRE     US,USPTP
        CAL     CH08_INIT
        CAL     CH08_ARM_BRK
        CAL     CH08_SPLASH
        JP      CH08_MENU

CH08_WARM:
; Warm return. Life's RAM mirror spans 0E6C..2C9D in bank 1,
; which covers the BOOT RCS trampoline at 0EFE. Reconstitute
; this bridge without replaying splash or card cold startup.
; Life must rebuild its own copied bytes on next entry.
; This boundary remains UNTESTED at r001 shell-only checkpoint.
        PRE     SS,&H0100
        PST     UA,&H14
        PRE     IX,RCS_TOP
        PRE     IY,RCS_END-1
        PRE     IZ,RCS
        PST     UA,&H44
        BUP
        PST     UA,&H14
        PRE     SS,SSPTP
        PRE     US,USPTP
        CAL     CH08_INIT
        CAL     CH08_ARM_BRK
        JP      CH08_MENU

CH08_ARM_BRK:
; No RAM trampoline: ACJMP in C-ROM cannot select the card ROM.
; BOOT ROM 0.02a registers 0000 for a native recovery/reset.
; While the card is idle we instead catch BRK in raw keyboard scan.
        CAL     CH08_SETBRK
        RTN

CH08_SETBRK:
; C-ROM fallback ONLY: an in-flight firmware BRK takes native restart,
; not an invalid jump into C-ROM data at 30C0. A fallback cold reboot
; remains a contract limitation to be fixed separately if observed.
        LDW     $2,$30
        LDW     $28,&H3280
        CAL     SYSCAL
        PST     UA,&H14
        RTN

CH08_INIT:
; Known keystone routine sequence, but new call boundary untested.
        LDW     $28,SYSCLR
        CAL     SYSCAL
        PST     UA,&H14
        LDW     $28,SETLCD
        CAL     SYSCAL
        PST     UA,&H14
        LDW     $28,SET4LN
        CAL     SYSCAL
        PST     UA,&H14
        CAL     OFFCHK
        PPO     &HC0
        LDW     $0,&H12B5
C8_DELAY:
        SBW     $0,$SY
        JR      NZ,C8_DELAY
        CAL     &H02D4
        PPO     &HC3
        LD      $0,&H0C
        LDW     $1,&H3000
        STLM    $0,3
        CAL     &H0304
        CAL     &H0306
        PST     UA,&H54
        PRE     IX,KEYMD
        ST      $31,(IX+$SX)
        PST     UA,&H14
        RTN

CH08_CLEAR:
        LD      $6,$SX
        PRE     IZ,C8_CLR11
        CAL     CH08_PRINT
        LD      $8,117
        CAL     &H018D
        RTN

CH08_SPLASH:
        CAL     CH08_CLEAR
        LD      $6,$SX
        PRE     IZ,C8_SPL_TX
        CAL     CH08_PRINT
        CAL     CH08_HIDE_CURSOR
; Stable TM sampling. Two distinct timer changes, no EXE requirement.
; This does not modify the Tiny benchmark, and bounds splash duration by
; timer progress rather than CPU loop speed. Validate observed duration.
        GST     TM,$8
        GST     TM,$8
        LD      $10,$8
        LD      $11,2
C8_SPLWAIT:
        CAL     OFFCHK
        GST     TM,$8
        GST     TM,$8
        LD      $12,$8
        SBC     $12,$10
        JR      Z,C8_SPLWAIT
        LD      $10,$8
        SB      $11,$30
        JR      NZ,C8_SPLWAIT
        RTN

CH08_MENU:
        CAL     CH08_CLEAR
        LD      $6,$SX
        PRE     IZ,C8_MENU_TX
        CAL     CH08_PRINT
; Launcher is not an editor. Selection is via FK only, hide cursor.
        CAL     CH08_HIDE_CURSOR
C8_MENULP:
; No firmware work in the idle scanning loop.
        CAL     CH08_GETKEY
        LD      $12,$0
        SBC     $12,&HFC
        JP      Z,C8_TINY
        LD      $12,$0
        SBC     $12,&HFD
        JP      Z,C8_LIFE
        LD      $12,$0
        SBC     $12,&HFE
        JP      Z,C8_KEYST
        LD      $12,$0
        SBC     $12,&HFF
        JP      Z,C8_LIMON
        JR      C8_MENULP

CH08_HIDE_CURSOR:
; Preserved C-ROM CROFF at 22FA disables visible blinking cursor.
; BOOT ROM calls the same C-ROM service through SYSCAL.
; Keystone reinitializes its own cursor on K8_ENTRY.
        LDW     $28,CROFF
        CAL     SYSCAL
        PST     UA,&H14
        RTN

CH08_GETKEY:
; Card-side nonblocking raw readiness gate. While idle, NEVER enter
; C-ROM CRTKY: its C-ROM BRK callback runs before SYSCAL can reselect
; the application card, so ACJMP from firmware selects the wrong ROM.
; C-ROM CRTKY remains responsible for ALL NORMAL KEY DECODING, including
; sequential S/CAPS/KANA semantics; the gate only detects any key and
; distinguishes BRK. Capture explicit BRK from the active card context.
C8_WAITRAW:
        CAL     CH08_RAWREADY
        SBC     $17,$SZ
        JR      Z,C8_WAITRAW
        LDW     $28,CRTKY
        CAL     SYSCAL
        PST     UA,&H14
        RTN

CH08_RAWREADY:
; BOOT 0.02a INKEY hardware scan idiom, but NO translation here.
; Scan all columns even if the key maps to zero (S/CAPS/KANA).
; 17=0: no key; 17=1: any ordinary key, caller may invoke CRTKY.
; BRK is IA=41h: wait for key release, then enter WARM directly.
; No OFFCHK here: r006 invoked it before testing BRK and therefore
; never isolated the scan from a possible native firmware BRK path.
; NOTE: power-OFF handling in prolonged idle needs separate proof.
; This protects idle menus/Keystone waits. BRK coincident with an
; already-running firmware decode is not yet proven safe.
        LD      $0,&H4B
C8_RAWLOOP:
        PST     IA,$0
        GRE     KY,$1
        GRE     KY,$1
        DID     $2
        ANCW    $1,$1
        JR      NZ,C8_RAWFOUND
        SB      $0,$30
        JR      LNZ,C8_RAWLOOP
        LD      $17,$31
        RTN
C8_RAWFOUND:
        SBC     $0,&H41
        JR      NZ,C8_RAWNORM
C8_BRK_RELEASE:
; Hold-off: avoid repeated warm resets on a still-held BRK key.
        LD      $0,&H41
        PST     IA,$0
        GRE     KY,$1
        GRE     KY,$1
        DID     $2
        ANCW    $1,$1
        JR      NZ,C8_BRK_RELEASE
        JP      CH08_WARM
C8_RAWNORM:
        LD      $17,$30
        RTN

C8_TINY:
; ROM-resident Tiny 0.1b. Lifecycle: bounded CH08 r011 probe.
        JP      T8_ENTRY
C8_LIFE:
; Remove the launcher/menu remnants before Life takes over the LCD.
; Reuse the proven CH08_CLEAR; do not change Life's drawing routines.
        CAL     CH08_CLEAR
; Life image is stored ROM2 at its original addresses; its loader
; copies LF_MAIN..LF_LOADER_END-1 to RAM1 before RAM execution.
; CH08_WARM restores shared RCS after Life owns that RAM span.
        JP      LF_MAIN
C8_KEYST:
; Real (adapted) keystone guest, no longer a placeholder.
        JP      K8_ENTRY
C8_LIMON:
; Full limon-09 C-ROM-derived monitor body, symbolically rebased to 8000+.
; Its adapted E command is strictly write-guarded; normal exits COLD.
        JP      LIMON_ENTRY

C8_PENDING:
; REAL APP DISPATCH NOT YET IMPLEMENTED.
        LD      $6,$SX
        CAL     CH08_PRINT
        CAL     CH08_GETKEY
        JP      CH08_MENU

CH08_PRINT:
        LDI     $7,(IZ+$SX)
        LD      $0,$7
        DIU     $7
        DID     $0
        OR      $7,$SZ
        RTN     Z
        CAL     &H0197
        JR      CH08_PRINT

C8_CLR11:
        DB      "           ",0
C8_SPL_TX:
        DB      "         Xandrian Labs          "
        DB      "        CS617 / REFACTOR        "
        DB      "      Four Lives, One ROM       "
        DB      "                                "
        DB      0
C8_MENU_TX:
        DB      "CS617 Refactor              08  "
        DB      "                                "
        DB      "                                "
        DB      "[tiny  ][life  ][key   ][limon ]"
        DB      0
C8_MSG1:
        DB      "Tiny not integrated. Press key",0
C8_MSG2:
        DB      "Life not integrated. Press key",0
C8_MSG4:
        DB      "limon not integrated. Press key",0
