; =====================================================================
; RS232C portable-facing transmit interface, r006 (candidate).
; Backend is RSOM_* from om52c.s for PB-2000C / OM52C only.
; Uses UA=14h and SX=0,SY=1 inherited platform convention.
; Application owns CRLF, EOF and menu; no direct 0C00 UART accesses.
;
; RS_INIT: defaults 4800 8N1 / no flow; clobbers $0,IX,flags,UA.
; RS_CONFIG: $10 speed (0=4800, 1=9600), $11 flow (0/1);
;            saves options in driver RAM; clobbers IX,UA.
; RS_OPEN: applies options; calls OM52C RSOPN; clobbers firmware ABI.
; RS_PUTC: input $16 (raw byte); sends exactly one byte.
; RS_PUTS: $10/$11 address of NUL-terminated string; does not send NUL.
; RS_WRITE: $10/$11 address, $12/$13 unsigned 16-bit length; sends
;           EXACTLY len bytes including any embedded 00h.
; RS_CLOSE: calls OM52C RSCLO; clobbers firmware ABI.
;
; RS_PUTS / RS_WRITE: source address must be accessible through IZ
; under UA=14h (same mapping as the verified ROM report). The code
; uses RS_PTR and RS_COUNT library RAM as clobbered private scratch,
; so calls are synchronous and NOT reentrant. Bank-neutral reads and
; safe access to VT21-owned RAM have NOT been established yet.
; Firmware ABI / error returns are not qualified. Do not infer PASS
; from a returned call. Future backend will supply explicit status.
; =====================================================================
RS_INIT:
        PST     UA,&H14
        LD      $0,0
        PRE     IX,RS_BAUD
        ST      $0,(IX+$SX)
        PRE     IX,RS_FLOW
        ST      $0,(IX+$SX)
        RTN

RS_CONFIG:
        PST     UA,&H14
        PRE     IX,RS_BAUD
        ST      $10,(IX+$SX)
        PRE     IX,RS_FLOW
        ST      $11,(IX+$SX)
        RTN

RS_OPEN:
        JP      RSOM_OPEN

RS_PUTC:
        JP      RSOM_PUTC

RS_CLOSE:
        JP      RSOM_CLOSE

; ROM/NUL string traversal is extracted from the r005a report loop.
; Source pointer advances before calling firmware (which clobbers IZ).
RS_PUTS:
        PST     UA,&H14
        PRE     IX,RS_PTR
        STW     $10,(IX+$SX)
RS_PUTS_LOOP:
        PST     UA,&H14
        PRE     IX,RS_PTR
        LDW     $10,(IX+$SX)
        PRE     IZ,$10
        LDI     $16,(IZ+$SX)
        AN      $16,$16
        JR      Z,RS_PUTS_DONE
        ADW     $10,$SY
        PRE     IX,RS_PTR
        STW     $10,(IX+$SX)
        CAL     RS_PUTC
        JP      RS_PUTS_LOOP
RS_PUTS_DONE:
        RTN

; Explicit length is essential for VT21 fixed-width 80-byte rows.
; Length 0 sends nothing. The buffer may contain NUL bytes.
; Count and pointer live in library RAM across calls to C-ROM.
RS_WRITE:
        PST     UA,&H14
        PRE     IX,RS_PTR
        STW     $10,(IX+$SX)
        PRE     IX,RS_COUNT
        STW     $12,(IX+$SX)
RS_WRITE_LOOP:
        PST     UA,&H14
        PRE     IX,RS_COUNT
        LDW     $10,(IX+$SX)
        ANCW    $10,$10
        JR      Z,RS_WRITE_DONE
        SBW     $10,$SY
        STW     $10,(IX+$SX)
        PRE     IX,RS_PTR
        LDW     $10,(IX+$SX)
        PRE     IZ,$10
        LDI     $16,(IZ+$SX)
        ADW     $10,$SY
        PRE     IX,RS_PTR
        STW     $10,(IX+$SX)
        CAL     RS_PUTC
        JP      RS_WRITE_LOOP
RS_WRITE_DONE:
        RTN

; Explicit-length RAM read from IX, UA=14h. This is independent from
; RS_WRITE ROM/IZ semantics and preserves NUL/trailing spaces.
; Input $10/$11 RAM address, $12/$13 byte length (0 = no send).
; Uses private RS_PTR and RS_COUNT; nonreentrant, clobbers firmware ABI.
RS_WRRAM:
        PST     UA,&H14
        PRE     IX,RS_PTR
        STW     $10,(IX+$SX)
        PRE     IX,RS_COUNT
        STW     $12,(IX+$SX)
RS_RRAM_LOOP:
        PST     UA,&H14
        PRE     IX,RS_COUNT
        LDW     $10,(IX+$SX)
        ANCW    $10,$10
        JR      Z,RS_RRAM_DONE
        SBW     $10,$SY
        STW     $10,(IX+$SX)
        PRE     IX,RS_PTR
        LDW     $10,(IX+$SX)
        PRE     IX,$10
        LD      $16,(IX+$SX)   ; RAM, not ROM/IZ
        ADW     $10,$SY
        PRE     IX,RS_PTR
        STW     $10,(IX+$SX)
        CAL     RS_PUTC
        JP      RS_RRAM_LOOP
RS_RRAM_DONE:
        RTN
