;=============================================================================
; Purpose: putchar function (for ST72264 devices).
;-----------------------------------------------------------------------------
; Input:       A   character to print.
; Output:      A   the character written.
; Read only:   A, X, Y, BH, BL, CH, CL
; Modified:    -
;-----------------------------------------------------------------------------
; Notes:
; - In projects that do not use the (un)getchar functionality, some extra code
; can be removed from the present file. Just set the NO_UNGETCHAR macro to 1
; and unnecessary conditional assembly blocks will be removed.
; - If you do not want the '\n' (Line Feed) character converted to CR/LF, set
; the NO_CRLF_CONV macro to 1 and unnecessary instructions will be removed.
;-----------------------------------------------------------------------------
; (c) Copyright Raisonance S.A.S. 1989-2008, All rights reserved.
;=============================================================================

;$SET(NO_UNGETCHAR = 1)   ; Uncomment if you do not use the ungetchar function
;$SET(NO_CRLF_CONV = 1)   ; Uncomment if you do not want LF to CR/LF conversion

    ; Exports public symbols
    PUBLIC putchar
    PUBLIC ?putchar
    ; These publics are required for getchar
    PUBLIC  SCISR
    PUBLIC  SCIDR


    ; Symbols for SCI registers depend on the target architecture
    SCISR    DATA   050h        ; SCI Status Register
    SCIDR    DATA   051h        ; SCI Data Register
    SCIBRR   DATA   052h        ; SCI Baud Rate Register
    SCICR1   DATA   053h        ; SCI Control Register 1
    SCICR2   DATA   054h        ; SCI Control Register 2
    SCIERPR  DATA   055h        ; SCI Receive Prescaler
    SCIETPR  DATA   056h        ; SCI Transmit Prescaler
    EXT_SCI  EQU    1           ; Indicates SCIERPR and SCIETPR registers are available
    BDRATDIV EQU    000h        ; Baud rate divisor (depends on the CPU frequency)
    EXTBDRAT EQU    027h        ; Extended baud rate divisor

    ; Serial Communications Interface Control Register 1 bit definitions
    R8          EQU     7       ; Receive data bit 8
    T8          EQU     6       ; Transmit data bit 8
    SCID        EQU     5       ; Disabled for low power consumption
    M           EQU     4       ; Word length
    WAKE        EQU     3       ; Wake-Up method
    PCE         EQU     2       ; Parity control enable
    PS          EQU     1       ; Parity selection
    PIE         EQU     0       ; Parity interrupt enable

    ; Serial Communications Interface Control Register 2 bit definitions
    TIE         EQU     7       ; Transmitter interrupt enable.
    TCIE        EQU     6       ; Transmission complete interrupt enable
    RIE         EQU     5       ; Receiver interrupt enable.
    ILIE        EQU     4       ; Idle line interrupt enable.
    TE          EQU     3       ; Transmitter enable.
    RE          EQU     2       ; Receiver enable.
    RWU         EQU     1       ; Receiver wake-up.
    SBK         EQU     0       ; Send break.

    ; SCISR bit definitions
    TDRE        EQU     7       ; Transmit data register empty
    TC          EQU     6       ; Transmission complete
    RDRF        EQU     5       ; Received data ready flag
    IDLE        EQU     4       ; Idle line detect
    OERR        EQU     3       ; Overrun error
    NF          EQU     2       ; Noise flag
    FE          EQU     1       ; Framing error
    PE          EQU     0       ; Parity error


    ; Character Codes
    LF      EQU     0Ah         ; Line Feed
    CR      EQU     0Dh         ; Carriage Return


$IF(NO_UNGETCHAR <> 1)          ; This can be skipped if getchar/ungetchar are not necessary

;---------------------------------------------------------------------------
; Flag for getchar/ungetchar SCI buffer full bit
;---------------------------------------------------------------------------

    PUBLIC  FULLB_SCI

    RSG_BTM1_GETFLAG    SEGMENT BIT

    RSEG                RSG_BTM1_GETFLAG

FULLB_SCI:    DBIT    1   ; Read bit indicates whether the buffer has been read

$ENDIF

;---------------------------------------------------------------------------
; Actual putchar function.
; Note that the SCI initialization is performed by this function upon its
; first call, hence it is not necessary to initialize the SCI elsewhere.
; For this reason, it is necessary to call putchar at least once before
; calling the getchar function, otherwise the SCI will not be initialized.
;---------------------------------------------------------------------------

    CSTDLIB             SEGMENT CODE INSECTION0
    RSEG                CSTDLIB

?putchar:
putchar:

;-----------------------------------------------------------------------------
; Initialize the SCI if not yet done
;-----------------------------------------------------------------------------

    BTJT    SCICR2, #TE, _convert      ; Jump to _convert if SCI already initialized

    ; Save the character to print
    PUSH    A

    ; Load Default value into control registers
    CLR     SCICR1
    CLR     SCICR2

    ; Clear all the flags in Status register (8 bits data, no parity, no HW control)
    LD      A, SCISR
    LD      A, SCIDR
    CLR     SCIDR

    ; Set baudrate to 9600 bauds @ 6 MHz (Half REva osc.)
    LD      A, #BDRATDIV
    LD      SCIBRR, A

$IF(EXT_SCI)
    LD      A, #EXTBDRAT
    LD      SCIERPR, A
    LD      SCIETPR, A
$ENDIF

    ; Enable the SCI
    BSET    SCICR2, #TE
    BSET    SCICR2, #RE

    ; Specifies that no char is in the getchar/ungetchar buffer yet
$IF(NO_UNGETCHAR <> 1)                 ; This can be skipped if getchar/ungetchar are not necessary
    BRES    FULLB_SCI
$ENDIF

    ; Restore the character to print
    POP     A

;-----------------------------------------------------------------------------
; Convert LF to CR/LF (to keep Hyperterminal happy)
;-----------------------------------------------------------------------------

_convert:
$IF(NO_CRLF_CONV <> 1)                 ; This can be skipped if CR/LF conversion is not necessary
    CP      A, #LF
    JRNE    _emit
    ; Reentrant call to putchar
    PUSH    A
    LD      A, #CR
    CALL    putchar
    POP     A
$ENDIF

_emit:

    BTJF    SCISR, #TDRE, $            ; Wait end of (previous) transmission
    LD      SCIDR, A                   ; Send the character to the SCI
    BTJF    SCISR, #TC, $              ; Wait end of transmission

    RET

END
