; keyv540.s -- PCB-V540-1 keyboard event service, v0.2 KEYMD CANDIDATE.
; Alex Barragan & ChatGPT (Sol), 2026-10-14.
; NOT ASSEMBLED or runtime tested. No ORG here; owner selects card ROM2
; placement. For ai1000-lab candidate: ORG &H5200 (verify in listing).
; Include keyv540.inc ONCE before this source.
;
; Provenance: reconstructed from integrated Keystone CAPS pass
; (ROM 991e9e7e...) and migrated BOOT 0.02a INKEY/KEYSCAN algorithm.
; This is an independent derivative, NOT byte-identical donor code.
; KEYSCAN bytes are embedded below unchanged, with private K540_TAB label.
; No host C-ROM CRTKY/SYSCAL calls. Uses the native KEYMD RAM byte.
; No auto-exit to menu/warm/cold.
; OFFCHK is not called. Power/OFF behavior remains a future test gate.
; Does not use CH08_RAWREADY: that one *jumps* on BRK.
; No direct JP &H0001 in this decoder: BRK is an event even on race.
;
; RAM1:09E4h (native C-ROM KEYMD) is BORROWED by this driver while active.
; A guest must not call the old host CRTKY/KEYMD code at the same time.
; Each entry loads mode from RAM. $13 is just a scratch/return copy.
; Return $17 event class, $0 key code, $13 current mode.
; Entry UA must be 14h; every routine here restores UA=14h.
; The integration is responsible for choosing a sound lifecycle; reset
; mode to 00h at entry and at exit (via K540_RESET).
K540_KEYMD: EQU &H09E4

; Initialize/clear the borrowed native keyboard mode at RAM1:09E4h.
; Does not need the C-ROM's SYSCLR/CRTKY/KEYMD code.
K540_INIT:
K540_RESET:
        PST     UA,&H54
        PRE     IX,K540_KEYMD
        ST      $31,(IX+$SX)
        PST     UA,&H14
        LD      $13,$31
        RTN

; Read mode from borrowed RAM, result in $13, clobbers IX/UA.
; Applications must NOT rely on the $13 register surviving unrelated code.
K540_MODE:
        PST     UA,&H54
        PRE     IX,K540_KEYMD
        LD      $13,(IX+$SX)
        PST     UA,&H14
        RTN

K540_GET:
        CAL     K540_MODE       ; always refresh from RAM; $13 not persistent
K540_GET_AGAIN:
        CAL     K540_RAW
        LD      $15,$17
        SBC     $15,K540_EV_BRK
        JP      Z,K540_GOT_BRK
        AN      $17,$17
        JP      Z,K540_GET_AGAIN

; CAPS is KO2 / KY bit 20h. Single press is a sequential latch event.
        PST     IA,&H42
        GRE     KY,$1
        GRE     KY,$1
        ANC     $1,&H20
        JP      NZ,K540_GOT_CAPS

; Translate the physical scan without external language ROM calls.
        CAL     K540_DECODE
        LD      $15,$17
        SBC     $15,K540_EV_BRK
        JP      Z,K540_GOT_BRK
; Saved code survives the second RAW scan used to wait for release.
        PHS     $0
        CAL     K540_WAIT_UP
        PPS     $0
        AN      $0,$0
        JP      Z,K540_GET_AGAIN

; MENU is a service event: caller chooses warm, cold or stay.
        LD      $15,$0
        SBC     $15,&H9C
        JP      Z,K540_GOT_MENU

; KEYSCAN A..Z defaults to lowercase unless local CAPS is latched.
        LD      $15,$0
        SBC     $15,&H41
        JR      C,K540_CASE_DONE
        LD      $15,$0
        SBC     $15,&H5B
        JR      NC,K540_CASE_DONE
        LD      $15,$13
        ANC     $15,K540_MODE_CAPS
        JR      NZ,K540_CASE_DONE
        AD      $0,&H20
K540_CASE_DONE:
        LD      $17,K540_EV_CHAR
        PST     UA,&H14
        RTN

K540_GOT_MENU:
        LD      $17,K540_EV_MENU
        PST     UA,&H14
        RTN

K540_GOT_BRK:
        CAL     K540_WAIT_UP
        LD      $0,$31
        LD      $17,K540_EV_BRK
        PST     UA,&H14
        RTN

K540_GOT_CAPS:
        XR      $13,K540_MODE_CAPS
        PST     UA,&H54
        PRE     IX,K540_KEYMD
        ST      $13,(IX+$SX)  ; persist CAPS in native RAM state
        PST     UA,&H14
        CAL     K540_WAIT_UP
        LD      $0,$31
        LD      $17,K540_EV_CAPS
        PST     UA,&H14
        RTN

; Non-decoding matrix readiness. NO WARM/COLD/BOOT exit.
; $17 = 0 no key, 1 non-BRK, 3 physical BRK column.
; Follows the tested CH08_RAWREADY double-GRE scan idiom.
K540_RAW:
        LD      $0,&H4B
K540_RAW_LP:
        PST     IA,$0
        GRE     KY,$1
        GRE     KY,$1
        DID     $2
        ANCW    $1,$1
        JR      NZ,K540_RAW_FOUND
        SB      $0,$30
        JR      LNZ,K540_RAW_LP
        LD      $17,K540_EV_NONE
        RTN
K540_RAW_FOUND:
        SBC     $0,&H41
        JR      NZ,K540_RAW_NORMAL
        LD      $17,K540_EV_BRK
        RTN
K540_RAW_NORMAL:
        LD      $17,K540_EV_CHAR
        RTN

K540_WAIT_UP:
K540_WAIT_UP_LP:
        CAL     K540_RAW
        AN      $17,$17
        JR      NZ,K540_WAIT_UP_LP
        RTN

; Card-local derivative of BOOT 0.02a INKEY.
; Differences from inherited INKEY: no OFFCHK; no WORK1 write;
; no JP 0001 on BRK; no host keyboard CALL. KEYSCAN table local.
; $17: 0 no translated key, 1 character, 3 BRK; $0: code.
K540_DECODE:
        LD      $17,K540_EV_NONE
        LD      $0,&H4B
K540_DEC_SCAN:
        PST     IA,$0
        GRE     KY,$1
        GRE     KY,$1
        DID     $2
        ANCW    $1,$1
        JR      Z,K540_DEC_NEXT
        SBC     $0,&H41
        JP      Z,K540_DEC_BRK
        LD      $3,$31
K540_DEC_BIT:
        BIDW    $2
        JR      C,K540_DEC_COL
        AD      $3,$30,K540_DEC_BIT
K540_DEC_COL:
        LD      $5,$0
        SB      $5,$30
        BIU     $5
        BIU     $5
        LD      $4,$5
        BIU     $4
        BIU     $4
        SB      $4,$5
        PST     UA,&H44
        PRE     IX,K540_TAB
        LDD     $0,(IX+$4)
        LDD     $0,(IX+$3)
        PST     UA,&H14
        JP      K540_DEC_RESULT
K540_DEC_NEXT:
        SB      $0,$30
        JR      LNZ,K540_DEC_SCAN
        LD      $0,$31
K540_DEC_RESULT:
        AN      $0,$0
        JR      Z,K540_DEC_END
        LD      $17,K540_EV_CHAR
K540_DEC_END:
        PST     UA,&H14
        RTN
K540_DEC_BRK:
        LD      $0,$31
        LD      $17,K540_EV_BRK
        PST     UA,&H14
        RTN

; K540_TAB is appended from KEYSCAN original 132 table bytes (11x12).
K540_TAB:
K540_ROW01:
        DB      0,0,0,0,0,0,0,1,0,0,0,0

K540_ROW02:
        DB      "W","Q","A",0,"Z",0,0,0,0,0,"'",7

K540_ROW03:
        DB      "R","E","D","S","C","X",0,0,&HFC,0,&H29,&H28

K540_ROW04:
        DB      "Y","T","G","F","B","V",0,0,&HFD,0,&H5D,&H5B

K540_ROW05:
        DB      "I","U","J","H","M","N",0,0,&HFE,0,&H90,&H7C

K540_ROW06:
        DB      "P","O","L","K",&H20,",",0,0,&HFF,0,&H93,&H94

K540_ROW07:
        DB      0,"=",":",";",0,0,0,0,&H95,0,0,&H9B

K540_ROW08:
        DB      "5","4","2","1",0,"0",0,"7",0,0,0,0

K540_ROW09:
        DB      "-","6","+","3",&H0D,".",0,"9",0,0,0,0

K540_ROW10:
        DB      &H9D,&H1C,"/",&H08,0,0,0,"*",0,0,&H9C,&H11

K540_ROW11:
        DB      &H1F,&H1D,&H0C,0,0,0,0,"8",0,0,&H1E,&H12
