home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / hf / dsp / source / sci.asm < prev    next >
Encoding:
Assembly Source File  |  1992-08-09  |  4.3 KB  |  233 lines

  1.     page    132,63,1,1
  2.     opt    rc
  3.     title    'SCI Handling'
  4.  
  5. ;***************************************************************
  6. ;* SCI.ASM -- Asyncronous Serial Interface Routines           *
  7. ;*                                   *
  8. ;* Provides interrupt based input/output routines for the      *
  9. ;* serial line.                            *
  10. ;*                                   *
  11. ;* This module reserves registers as follows:               *
  12. ;*  r3 - input/output sample pointer                   *
  13. ;*                                   *
  14. ;* Copyright (C) 1992 by Alef Null. All rights reserved.       *
  15. ;* Author(s): Jarkko Vuori, OH2LNS                   *
  16. ;* Modification(s):                           *
  17. ;*    09-Aug-1992: corrected xmit interrupt handler bug      *
  18. ;***************************************************************
  19.  
  20.     section SCI
  21.     xdef    sci_ini,putc,getc
  22.     xdef    sci_rec,sci_err,sci_xmt
  23.     xref    scix,scir
  24.  
  25.     nolist
  26.     include 'ioequlc.asm'
  27.     include 'macros.asm'
  28.     list
  29.  
  30.     org    p:
  31.  
  32. ; SCI parameters
  33. baud    equ    9600                    ; SCI baud rate
  34. xtal    equ    20763000                ; XTAL frq (in MHz)
  35.  
  36. ; SCI flags
  37. rempty    equ    0
  38. rfull    equ    1
  39. xempty    equ    2
  40. xfull    equ    3
  41.  
  42.  
  43. ;****************************
  44. ;*    SCI initialization    *
  45. ;****************************
  46. sci_ini
  47. ; initialize SCI
  48.     movep            #$0b02,x:m_scr        ; 8,n,1
  49.     movep            #xtal/(2*2*16*baud),x:m_sccr
  50.  
  51. ; initialize queue handling
  52.     move            #outbuf,a1
  53.     move            a1,x:xhead
  54.     move            a1,x:xtail
  55.     move            #inbuf,a1
  56.     move            a1,x:rhead
  57.     move            a1,x:rtail
  58.  
  59.     move            #buflen-1,m3
  60.  
  61. ; and other data structures
  62.     movi    (1<<rempty)|(1<<xempty),y:flags     ; buffer empty at first
  63.  
  64.     rts
  65.  
  66.  
  67. ;****************************
  68. ;*  Put character to queue  *
  69. ;****************************
  70. ; byte in x0
  71. ; returns  Z if buffer full
  72. ;      NZ otherwise
  73. putc    ori    #$03,mr                 ; disable interrupts
  74.     move            x:xtail,a
  75.     move            x:xhead,x1
  76.     move            x1,r3
  77.  
  78. ; check for idling xmitter
  79.     bset    #m_tie,x:m_scr
  80.     jcc    <kickput
  81.  
  82. ; xmitter was running, check if there are free space left
  83.     bclr    #xempty,y:flags
  84.     jcs    <putc1
  85.     cmp    x1,a
  86.     jne    <putc1
  87.  
  88. ; buffer full state reached (ignore given data)
  89.     bset    #xfull,y:flags
  90.     jmp    <putce
  91.  
  92. ; there was free space left, write character to the buffer
  93. putc1    move            x0,p:(r3)+
  94.     move            r3,x:xhead
  95.     jmp    <putce
  96.  
  97. ; xmitter was stopped, kick it up
  98. kickput movep            x0,x:m_stxl
  99.  
  100. putce    andi    #$fc,mr
  101.     rts
  102.  
  103.  
  104. ;****************************
  105. ;* Get character from queue *
  106. ;****************************
  107. ; byte in x0
  108. ; returns  C if no data available
  109. ;      NC if data available
  110. getc    ori    #$03,mr                 ; disable interrupts
  111.     nop
  112.     nop
  113.     nop
  114.     nop
  115.     move            x:rtail,r3
  116.  
  117. ; check if there are data available
  118.     btst    #rempty,y:flags
  119.     jcs    <getce
  120.  
  121. ; yes, take it from the queue
  122.     bclr    #rfull,y:flags
  123.     move            p:(r3)+,x0
  124.     move            r3,x:rtail
  125.  
  126. ; check if buffer gets empty
  127.     move            x:rhead,a
  128.     move            r3,x1
  129.     cmp    x1,a
  130.     andi    #$fe,ccr
  131.     jne    <getce
  132.  
  133. ; yes, set empty flag
  134.     bset    #rempty,y:flags
  135.  
  136. getce    andi    #$fc,mr
  137.     rts
  138.  
  139.  
  140. ;****************************
  141. ;*    SCI xmit interrupt    *
  142. ;****************************
  143. sci_xmt enter    scix
  144.  
  145.     move            x:xtail,r3
  146.     bclr    #xfull,y:flags
  147.     jcs    <scix1
  148.  
  149. ; check if buffer empty
  150.     move            x:xhead,x0
  151.     move            r3,a
  152.     cmp    x0,a
  153.     jeq    <scixep
  154.  
  155. ; no, put data out
  156. scix1    movep            p:(r3)+,x:m_stxl
  157.     move            r3,x:xtail
  158.     jmp    <scixe
  159.  
  160. ; yes, shut down xmitter
  161. scixep    bclr    #m_tie,x:m_scr
  162.     bset    #xempty,y:flags
  163.  
  164. scixe    leave    scix
  165.  
  166.  
  167. ;****************************
  168. ;*   SCI receive interrupt  *
  169. ;****************************
  170. sci_rec enter    scir
  171.  
  172.     movep            x:m_srxl,a
  173.  
  174. ; first check that there are room left in the buffer
  175.     btst    #rfull,y:flags
  176.     jcs    <scire
  177.  
  178. ; yes, the put data there
  179.     move            x:rhead,r3
  180.     bclr    #rempty,y:flags
  181.     move            a1,p:(r3)+
  182.     move            r3,x:rhead
  183.  
  184. ; check buffer full condition
  185.     move            r3,a
  186.     move            x:rtail,x1
  187.     cmp    x1,a
  188.     jne    <scire
  189.     bset    #rfull,y:flags
  190.  
  191. scire    leave    scir
  192.  
  193.  
  194. ;****************************
  195. ;*  SCI receive with errors *
  196. ;*      interrupt        *
  197. ;****************************
  198. sci_err movep            x:m_ssr,y:tmp        ; clear SCI interrupts
  199.     movep            x:m_srxl,y:tmp
  200.  
  201.     rti
  202.  
  203.  
  204.     org    x:
  205.  
  206. rhead    ds    1
  207. rtail    ds    1
  208. xhead    ds    1
  209. xtail    ds    1
  210.  
  211.  
  212.     org    y:
  213.  
  214. flags    ds    1
  215. tmp    ds    1
  216.  
  217.     endsec
  218.  
  219.  
  220.     section SCIbuf
  221.  
  222.     xdef    buflen
  223.     xdef    inbuf,outbuf
  224.  
  225. buflen    equ    512                    ; SCI input/output buffer lenght
  226.  
  227. inbuf    ds    buflen
  228. outbuf    ds    buflen
  229.  
  230.     endsec
  231.  
  232.     end
  233.