; ======================================================================
; lib-set-01 / SPLASH -- r005 source candidate.
; SPL_SHOW input: $10/$11 = ROM pointer to four consecutive NUL-
;              terminated ASCII strings; $12 = number of TM transitions.
;              Each line may contain 0..32 visible bytes, including blank.
; Result: $0 = 0 accepted/returned, 1 invalid line (>32 chars).
; Rejects invalid content before altering the LCD or waiting.
; Centers each line independently in its physical 32-character row,
; begins at physical row 1, clears unused cells, and hides the cursor.
; Uses TMR_WAIT; does not own a timer or choose cold/warm lifecycle.
; ROM data is read through IZ; UA=14 RAM state (SPL_PTR,SPL_WAITN).
; Clobbers $0,$2,$3,$6,$7,$8,$9,$10,$11,$13,$16,$20,$22..$25,
; IX/IZ and flags, plus platform LCD/TMR side effects. Non-reentrant.
; ======================================================================
SPL_SHOW:
        PST     UA,&H14
        LDW     $2,$10
        PRE     IX,SPL_PTR
        STW     $2,(IX+$SX)
        PRE     IX,SPL_WAITN
        ST      $12,(IX+$SX)
; Validate all four strings without touching the screen.
        LD      $25,4
SPL_CROW:
        LD      $13,0
        PRE     IZ,$10
SPL_CCHAR:
        LDI     $7,(IZ+$SX)
        ADW     $10,$SY
        AN      $7,$7
        JR      Z,SPL_CNEXT
        AD      $13,$SY
        SBC     $13,33
        JR      NC,SPL_BAD
        JR      SPL_CCHAR
SPL_CNEXT:
        SB      $25,$SY
        JR      NZ,SPL_CROW
; Text is valid. Clear the physical 32x4 canvas and draw centered lines.
        CAL     LCD_CLEAR
        PRE     IX,SPL_PTR
        LDW     $10,(IX+$SX)
        LD      $24,0             ; physical row start 0,32,64,96
        LD      $25,4
SPL_ROW:
        LDW     $20,$10           ; beginning of the current NUL string
        LD      $13,0
        PRE     IZ,$10
SPL_LEN:
        LDI     $7,(IZ+$SX)
        ADW     $10,$SY
        AN      $7,$7
        JR      Z,SPL_PAD
        AD      $13,$SY
        JR      SPL_LEN
SPL_PAD:
; Indent = floor((32 - length)/2), no rounding surprises.
        LD      $2,32
        SB      $2,$13
        LD      $3,0
SPL_HALF:
        SBC     $2,2
        JR      C,SPL_POS
        SB      $2,2
        AD      $3,$SY
        JR      SPL_HALF
SPL_POS:
        LD      $6,$24
        AD      $6,$3
; LCD firmware is not assumed to preserve our row and next-string state.
        PHS     $24
        PHS     $25
        PHS     $10
        PHS     $11
        PRE     IZ,$20
        CAL     LCD_PUTZ
        PPS     $11
        PPS     $10
        PPS     $25
        PPS     $24
        AD      $24,32
        SB      $25,$SY
        JR      NZ,SPL_ROW
; A zero duration returns immediately; positive values wait for TM changes.
        PRE     IX,SPL_WAITN
        LD      $10,(IX+$SX)
        CAL     TMR_WAIT
        LD      $0,0
        RTN
SPL_BAD:
        LD      $0,1
        RTN
