⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sci_int.mod

📁 该应用软件可以实现大多数单片机的仿真实验
💻 MOD
📖 第 1 页 / 共 2 页
字号:
                    rts

; Purpose: Read SCI char, is present, into RegA
; Output : A holds character if Carry Clear
;        : Carry set if character not available
?GetPossibleChar    ldx       #?RX.index
                    sei
                    jsr       ?DeQ
                    brset     CCR_,y,I.,?GetPChar.NoInt
                    cli
?GetPChar.NoInt     bcs       ?GetPChar.Zero      ;check if zero
?GetPChar.OK        sta       A_,y
                    clc                           ;never an error from here
                    rts
?GetPChar.Zero      tsta
                    bne       ?GetPChar.OK        ;other it was last char
                    ldb       B_,y                ;avoid destroying original B
                    sec
                    rts


; Purpose: Read SCI char into RegA
; Output : A holds character
?GetChar            ldx       #?RX.index
                    sei
                    bsr       ?DeQ
                    brset     CCR_,y,I.,?GetChar.NoInt
                    cli
?GetChar.NoInt      bcs       ?GetChar.Zero       ;check if zero
?GetChar.OK         sta       A_,y
                    clc                           ;never an error from here
                    rts
?GetChar.Zero       tsta
                    beq       ?GetChar            ;if zero, wait for char
                    bra       ?GetChar.OK         ;other it was last char

; Purpose: Write RegA character to the SCI
; Input  : A holds character
?PutChar            lda       A_,y
?PutChar.Local      pshx
                    ldx       #?TX.index          ;point to TX buffer
?PutChar.Loop       sei
                    bsr       ?EnQ                ;attempt to enqueue char
                    brset     CCR_,y,I.,?PutChar.NoInt
                    cli
?PutChar.NoInt      bcs       ?PutChar.Loop       ;wait if not ready yet
                    bsr       ?TXEnable           ;now, tell TCs can start working again
                    pulx
                    clc                           ;never an error from here
                    rts

; Purpose: Write (send) a string to the SCI
; Input  : X->buffer of Pascal-like string, ie., LengthByte,Char1,Char2,...
; Note(s): buffer should not be used for incoming chars, for it will be
;          messed up.
?Write              ldx       X_,y
                    ldb       ,x                  ;get length of string
                    beq       ?Write.NoError      ;if 0, nothing to send
?Write.Loop         inx                           ;point to data
                    lda       ,x
                    bsr       ?PutChar.Local
                    decb
                    bne       ?Write.Loop
?Write.NoError      clc
                    rts

; Purpose: Writeln (send) a string to the SCI followed by a CR,LF pair
; Input  : X->buffer of Pascal-like string, ie., LengthByte,Char1,Char2,...
?Writeln            bsr       ?Write              ;do regular fWrite
                    bcs       ?Writeln.Exit       ;on failure, exit
; Purpose: Advance a line sending a CR,LF pair to the SCI
?NewLine            lda       #CR                 ;send a CR
                    bsr       ?PutChar.Local
                    lda       #LF                 ;send a LF
                    bsr       ?PutChar.Local
                    clc
?Writeln.Exit       rts

; Purpose: Write (send) a string to the SCI
; Input  : X->ASCIZ string, ie., Char1,Char2,...,0
; Note(s): buffer should not be used for incoming chars, for it will be
;          messed up.
?WriteZ             ldx       X_,y
?WriteZ.Loop        lda       ,x
                    beq       ?WriteZ.NoError
                    bsr       ?PutChar.Local
                    inx
                    bne       ?WriteZ.Loop        ;avoids infinite loops
                    ldb       #errOutOfRange      ;on memory wrap-around
                    sec
                    rts
?WriteZ.NoError     clc
                    rts

; Purpose: Writeln (send) a string to the SCI followed by a CR,LF pair
; Input  : X->ASCIZ string, ie., Char1,Char2,...,0
?WritelnZ           bsr       ?WriteZ             ;do regular fWriteZ
                    bcs       ?Writeln.Exit       ;on failure, exit
                    bra       ?NewLine

; EnQueue character in RegA to buffer pointed by X (RX.buffer or TX.buffer)
?EnQ                pshb
                    ldb       ,x                  ;B := index
                    incb                          ;index := index + 1
                    cmpb      #?MAXBUF            ;if index > ?MAXBUF then
                    bhi       ?Q.Error            ;  buffer full, exit
                    stb       ,x                  ;save updated index
                    pshx
                    abx                           ;point to buffer[index]
                    sta       ,x                  ;and save character
                    pulx
                    pulb
                    clc
                    rts

?Q.Error            pulb                          ;common for ?EnQ and ?DeQ
?Q.Error1           sec
                    rts

; DeQueue in RegA a character from buffer pointed by X (RX.index or TX.index)
?DeQ                clra                          ;in case of error, A := NUL
                    tst       ,x                  ;if index = 0 then
                    beq       ?Q.Error1           ;  exit with error
                    lda       1,x                 ;A := buffer[1] (always return the first char)
?Q.Shrink           pshx
                    pshd
                    ldb       ,x                  ;get new length
                    dec       ,x                  ;decrement buffer length
?Q.Shrink.Loop      decb
                    beq       ?Q.Shrink.Exit
                    lda       2,x                 ;get next character
                    sta       1,x                 ;into current character
                    inx
                    bra       ?Q.Shrink.Loop
?Q.Shrink.Exit      puld
                    pulx
                    tst       ,x                  ;if new index = 0 then
                    beq       ?Q.Error1           ;  exit with error
                    clc
                    rts

; Enable TX interrupts
?TXEnable           psha
                    lda       SCCR2
                    ora       #TDRF.
                    sta       SCCR2
                    pula
                    rts

; SCI-related interrupt requests come here
SCI_Handler         lda       SCSR                ;get status to determine cause of interrupt
                    psha                          ;save it for later
                    bita      #$20                ;Anything received?
                    beq       SCI_Transmit        ;No, check TC flag

                    lda       SCDR                ;get received character
                    ldx       #?RX.index
                    bsr       ?EnQ                ;put character in RX queue
                    bcs       SCI_Exit            ;if save not possible, exit

SCI_Transmit        pula                          ;restore SCSR status in A
                    bita      #TDRF.              ;SCDR empty?
                    beq       SCI_Exit1           ;no, get out

                    ldx       #?TX.index          ;point to string
                    bsr       ?DeQ                ;get a character from TX queue
                    bcc       SCI_Exit2           ;not done yet, leave TC ints active
                    tsta                          ;if A <> 0, send then exit
                    beq       SCI_DisableTX
                    sta       SCDR                ;send it (clearing TC flag)
SCI_DisableTX       lda       SCCR2               ;on empty queue...
                    anda      #NOT^TDRF.
                    sta       SCCR2               ;terminate further ints
SCI_Exit1           rti                           ;exit without stack adjustment
SCI_Exit2           sta       SCDR                ;send it (clearing TC flag)
                    rti

SCI_Exit            ins                           ;adjust the stack for saved SCSR
                    rti                           ;then exit

#ifmain
                    #include  OS11/DISPATCH.MOD
#endif
                    #VECTORS
?vectors            equ       *

                    org       $FFD6
                    dw        SCI_Handler

                    org       ?vectors

                    #ROM

                    end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -