; Original Chapter 11-style LCD/platform adapter (new candidate owner).
; Source-derived, PB-2000C evidence only; AI-1000 still untested.
; r012a dual-machine platform candidate based on accepted CH08 S003.
; The successful S003 cold/warm path never invokes the C-ROM
; SYSCLR/SETLCD/SET4LN/CROFF/CSREN services.  Retain source/public
; entry symbols here without invoking those model-dependent routines.
; Source-inspected and HOST tested only; AI-1000/PB new ROM not yet built.
SYS_INIT:
; Intentionally no C-ROM SYSCAL.  SCREEN_INIT below performs the
; portable processor-ROM startup.  Keep this entry for existing clients.
        PST     UA,&H14
        RTN

SCREEN_INIT:
; CH08_INIT accepted S003 instruction sequence, followed by the
; direct processor-ROM &H018D 128-cell clearing idiom.
        PST     UA,&H14
        CAL     OFFCHK
        PPO     &HC0
        LDW     $0,&H12B5
SI_DELAY:
        SBW     $0,$SY
        JR      NZ,SI_DELAY
        CAL     &H02D4
        PPO     &HC3
        LD      $0,&H0C
        LDW     $1,&H3000
        STLM    $0,3
        CAL     &H0304
        CAL     &H0306
        LD      $6,$SX
        LD      $8,128
        CAL     &H018D
        PST     UA,&H14
        RTN

CUR_OFF:
; Same policy as S003 CH08_HIDE_CURSOR: do NOT call C-ROM CROFF.
; SYS540 owns visible editor cursor overlays and their lifecycle.
; No native cursor-off effect is promised by this candidate stub.
        RTN

; Legacy EDCSR write is *not* established as cross-model portable.
; This entry is kept for API/source continuity but is not called by
; any r012a Lab, Input, Menu or VT20 path.  Do not use on AI-1000.
SETCUR:
        PST     UA,&H54
        PRE     IX,EDCSR
        ST      $6,(IX+$SX)
        PST     UA,&H14
        RTN

CUR_ON:
; Same portability restriction as CUR_OFF; no host C-ROM CSREN.
; Use SYS540 overlay service for the portable application cursor.
        RTN

; -------------------------- low-level display -------------------------
PUTC:
        LD      $7,$16
        LD      $0,$7
        DIU     $7
        DID     $0
        OR      $7,$SZ
        CAL     &H0197
        RTN

; IZ -> zero-terminated ROM string, $6 = firmware cursor.
LCD_PUTZ:
        LDI     $7,(IZ+$SX)
        LD      $0,$7
        DIU     $7
        DID     $0
        OR      $7,$SZ
        RTN     Z
        CAL     &H0197
        JR      LCD_PUTZ

; Exactly 32 ROM bytes at IZ.  $6 selects physical row start.
LCD_ROM32:
        LD      $20,32
LR32_LOOP:
        LDI     $16,(IZ+$SX)
        PHS     $20
        CAL     PUTC
        PPS     $20
        SB      $20,$SY
        JR      NZ,LR32_LOOP
        RTN

; $10/$11 RAM pointer, $13 length, $6 cursor.
LCD_RAMN:
        AN      $13,$13
        RTN     Z
LRN_LOOP:
        PRE     IX,$10
        LD      $16,(IX+$SX)
        CAL     PUTC
        ADW     $10,$SY
        SB      $13,$SY
        JR      NZ,LRN_LOOP
        RTN

LCD_CLEAR:
        LD      $6,&H00
        PRE     IZ,BLANK32
        CAL     LCD_ROM32
        LD      $6,&H20
        PRE     IZ,BLANK32
        CAL     LCD_ROM32
        LD      $6,&H40
        PRE     IZ,BLANK32
        CAL     LCD_ROM32
        LD      $6,&H60
        PRE     IZ,BLANK32
        CAL     LCD_ROM32
        CAL     CUR_OFF
        RTN


RAW_READY:
        LD      $0,&H4B
RR_LOOP:
        PST     IA,$0
        GRE     KY,$1
        GRE     KY,$1
        DID     $2
        ANCW    $1,$1
        JR      NZ,RR_FOUND
        SB      $0,$30
        JR      LNZ,RR_LOOP
        LD      $17,$31
        RTN
RR_FOUND:
        SBC     $0,&H41
        JR      NZ,RR_NORMAL
        LD      $17,2
        RTN
RR_NORMAL:
        LD      $17,$30
        RTN

; Blocking semantic keyboard adapter: S003 K540_GET, not CRTKY.
; K540_GET waits for complete press/release, returns $17=event class
; and $0 translated code.  KEY_WAIT maintains its original Lab ABI:
;    $17=1 decoded key (including MENU), $0=logical code
;    $17=2 BRK, $0=0
; CAPS mode-change is consumed internally; on the next call K540_GET
; continues with the stored CAPS latch.  Existing menu/VT consumers
; do not need to know a physical matrix or C-ROM key encoding.
; REQUIREMENT: KBD_INIT or K540_INIT at card startup.  No host CRTKY.
KEY_WAIT:
KW_LOOP:
        CAL     K540_GET
        SBC     $17,K540_EV_BRK
        JR      Z,KW_BRK
        SBC     $17,K540_EV_CAPS
        JR      Z,KW_LOOP
        LD      $17,$30
        RTN
KW_BRK:
        LD      $17,2
        RTN


BLANK32: DB      "                                "
