        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:
; lab experimental port: suppress C-ROM BRSTR while probing a shared
; processor-ROM startup. No BRK handler installed here; card's raw
; menu scanner still detects BRK when it owns the keyboard loop.
        RTN

CH08_SETBRK:
; Not a portable API; do not call host ROM at 3280h.
; Keep the entry symbol for transplanted guests if any refer to it.
        RTN

CH08_INIT:
; lab experimental port: minimal LCD power/init proven with the exact
; same ROM on AI-1000 and PB-2000C (portable-menu-control).
; Deliberately NO C-ROM SYSCLR/SETLCD/SET4LN, no SYSCAL, no MODEL.
        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,&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:
; C-ROM CROFF at 22FAh is NOT portable. No-op for the first probe.
; Cursor refinement is deliberately deferred until both machines
; reach splash/menu with identical ROM bytes.
        RTN

CH08_GETKEY:
; lab experimental port: read a single translated key using card-side
; BOOT 0.02a INKEY and the already included KEYSCAN table at 5000h.
; In contrast to r016, there is no call into host C-ROM CRTKY at 0460h.
; This is a first-pass function-key decoder, not proof of all modifier
; state semantics. Raw readiness and BRK handling remain below.
C8_WAITRAW:
        CAL     CH08_RAWREADY
        SBC     $17,$SZ
        JR      Z,C8_WAITRAW
        CAL     INKEY
        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
