home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / TASMSWAN.ZIP / ASYNCH.ASM < prev    next >
Assembly Source File  |  1989-07-17  |  7KB  |  287 lines

  1. %TITLE  "Asynchronous Serial Communications Module"
  2.  
  3.     IDEAL
  4.     DOSSEG
  5.     MODEL    small
  6.  
  7.     PUBLIC    ComPort
  8.  
  9. ComPort = 0
  10.  
  11. IF    ComPort EQ 0
  12.         Port        EQU    03F8h
  13.         VectorNum    EQU    0Ch
  14.         EnableIRQ    EQU    0EFh
  15.         DisableIRQ    EQU    10h
  16. ELSEIF    ComPort EQ 1
  17.         Port        EQU    02F8h
  18.         VectorNum       EQU    0Bh
  19.         EnableIRQ    EQU    0F7h
  20.         DisableIRQ    EQU    08h
  21. ELSE
  22.     %DISPLAY "ComPort must be 0 or 1"
  23.     ERR
  24. ENDIF
  25.  
  26. TxRegister    =    Port + 0
  27. RxRegister    =    Port + 0
  28. IntEnable    =    Port + 1
  29. IntIdent    =    Port + 2
  30. LineControl    =    Port + 3
  31. ModemControl    =    Port + 4
  32. LineStatus    =    Port + 5
  33. ModemStatus    =    Port + 6
  34.  
  35. Ctrl8259_0    EQU    020h
  36. Ctrl8259_1    EQU    021h
  37. EOI        EQU    020h
  38. BufSize        EQU    2048
  39.  
  40.  
  41.     DATASEG
  42.  
  43. vectorSeg    dw    ?
  44. vectorOfs    dw    ?
  45. bufHead        dw    ?
  46. bufTail        dw    ?
  47. buffer        db    BufSize DUP (?)
  48.  
  49.     CODESEG
  50.  
  51.     PUBLIC    AsynchInit, AsynchStop, AsynchStat
  52.     PUBLIC    AsynchOut, AsynchIn, AsynchInStat
  53.  
  54. %NEWPAGE
  55. ;------------------------------------------------------------------------
  56. ; EmptyBuffer - empty the input buffer
  57. ;------------------------------------------------------------------------
  58. ;     NOTE:  private to module
  59. ;    Input:  none
  60. ;    Output:  none
  61. ;       Registers:  none
  62. ;------------------------------------------------------------------------
  63. PROC    EmptyBuffer
  64.     cli
  65.     push    ax
  66.     mov    ax, offset buffer
  67.     mov    [bufHead], ax
  68.     mov    [bufTail], ax
  69.     pop    ax
  70.     sti
  71.     ret
  72. ENDP    EmptyBuffer
  73.  
  74. %NEWPAGE
  75. ;------------------------------------------------------------------------
  76. ; AsynchInit - initialize serial port and install ISR
  77. ;------------------------------------------------------------------------
  78. ;    Input:  none
  79. ;    Output:  none
  80. ;         NOTE: precede (usually) with call to int 14h
  81. ;            to set baud rate
  82. ;        NOTE: interrupt-driven input begins immediately
  83. ;            upon exit from this routine
  84. ;       WARNING: you MUST call AsynchStop before your program ends
  85. ;                   to avoid a !!! SYSTEM CRASH !!!
  86. ;       Registers:  ax,bx,dx
  87. ;------------------------------------------------------------------------
  88. PROC    AsynchInit
  89.     call    EmptyBuffer
  90.     push    ds
  91.     push    es
  92.     mov    ax, 3500h + VectorNum
  93.     int    21h
  94.     mov    [vectorSeg], es
  95.     mov    [vectorOfs], bx
  96.     push    cs
  97.     pop    ds
  98.     mov    ds, offset AsynchISR
  99.     mov    ax, 2500h + VectorNum
  100.     int    21h
  101.     pop    es
  102.     pop    ds
  103.     in    al, Ctrl8259_1
  104.     and    al, EnableIRQ
  105.     out    Ctrl8259_1,al
  106.     mov    dx,LineControl
  107.     in    al,dx
  108.     and    al,07Fh
  109.     out    dx,al
  110.     mov    dx,IntEnable
  111.     mov    al,1
  112.     out    dx,al
  113. @@10:
  114.     mov    dx,RxRegister
  115.     in    al,dx
  116.     mov    dx,LineStatus
  117.     in    al,dx
  118.     mov    dx,ModemStatus
  119.     in    al,dx
  120.     mov    dx,IntIdent
  121.     in    al,dx
  122.     test    al,1
  123.     jz    @@10
  124.     mov    dx,ModemControl
  125.     in    al,dx
  126.     or    al,08h
  127.     out    dx,al
  128.     call    EmptyBuffer
  129.     ret
  130. ENDP    AsynchInit
  131.  
  132. %NEWPAGE
  133. ;------------------------------------------------------------------------
  134. ; AsynchStop - uninstall asynch ISR
  135. ;------------------------------------------------------------------------
  136. ;    Input:  none
  137. ;    Output:  none
  138. ;       WARNING: you MUST call AsynchStop before your program ends
  139. ;                   to avoid a !!! SYSTEM CRASH !!!
  140. ;       Registers:  al,dx
  141. ;------------------------------------------------------------------------
  142. PROC    AsynchStop
  143.     in    al, Ctrl8259_1
  144.     or    al, DisableIRQ
  145.     out    Ctrl8259_1,al
  146.     mov    dx,LineControl
  147.     in    al,dx
  148.     and    al,07Fh
  149.     out    dx,al
  150.     mov    dx, IntEnable
  151.     xor    al,al
  152.     out    dx,al
  153.     mov    dx,ModemControl
  154.     in    al,dx
  155.     and    al, 0F7h
  156.     out    dx,al
  157.     push    ds
  158.     mov    ax, 2500h + VectorNum
  159.     mov    dx, [vectorOfs]
  160.     mov    ds,[vectorSeg]
  161.     int    21h
  162.     pop    ds
  163.     ret
  164. ENDP    AsynchStop
  165.  
  166. %NEWPAGE
  167. ;------------------------------------------------------------------------
  168. ; AsynchStat - get status for output
  169. ;------------------------------------------------------------------------
  170. ;    Input:  none
  171. ;    Output:  ah = line status,  al = modem status
  172. ;       Registers:  ax,dx
  173. ;------------------------------------------------------------------------
  174. PROC    AsynchStat
  175.     mov    ah,3
  176.     mov    dx, ComPort
  177.     int    14h
  178.     ret
  179. ENDP    AsynchStat
  180.  
  181. %NEWPAGE
  182. ;------------------------------------------------------------------------
  183. ; AsynchOut -  output a byte (to the output port)
  184. ;------------------------------------------------------------------------
  185. ;    Input:  al = character (or byte) to output
  186. ;    Output:  none
  187. ;       Registers:  none
  188. ;------------------------------------------------------------------------
  189. PROC    AsynchOut
  190.     push    dx
  191.     push    ax
  192. @@10:
  193.     mov    dx, LineStatus
  194.     in    al,dx
  195.     and    al,020h
  196.     jz    @@10
  197.     pop    ax
  198.     mov    dx, TxRegister
  199.     out    dx,al
  200.     pop    dx
  201.     ret
  202. ENDP    AsynchOut
  203.  
  204. %NEWPAGE
  205. ;------------------------------------------------------------------------
  206. ; AsynchIn -  input a byte (from buffer)
  207. ;------------------------------------------------------------------------
  208. ;    Input:  none
  209. ;    Output:  al = char from buffer
  210. ;            NOTE: if buffer is empty, al will be 0, with
  211. ;            no indication that this is not an input value.
  212. ;            Precede with call to AsynchInStat to avoid reads
  213. ;            from an empty buffer.
  214. ;       Registers:  al,bx
  215. ;------------------------------------------------------------------------
  216. PROC    AsynchIn
  217.     xor    al,al
  218.     mov    bx,[bufTail]
  219.     cmp    bx,[bufHead]
  220.     je    @@99
  221.     mov    al,[byte ptr bx]
  222.     inc    [bufTail]
  223.     cmp    [word ptr bufTail], offset buffer + BufSize
  224.     jb    @@99
  225.     mov    [bufTail], offset buffer
  226. @@99:
  227.     ret
  228. ENDP    AsynchIn
  229.  
  230. %NEWPAGE
  231. ;------------------------------------------------------------------------
  232. ; AsynchInStat -  get status of input buffer
  233. ;------------------------------------------------------------------------
  234. ;    Input:  none
  235. ;    Output:  dx = number of bytes (or chars) in buffer
  236. ;       Registers:  dx
  237. ;------------------------------------------------------------------------
  238. PROC    AsynchInStat
  239.     mov    dx,[bufHead]
  240.     sub    dx,[bufTail]
  241.     jge    @@99
  242.     add    dx,BufSize
  243. @@99:
  244.     ret
  245. ENDP    AsynchInStat
  246.  
  247. %NEWPAGE
  248. ;------------------------------------------------------------------------
  249. ; AsynchISR -  asynchronous input Interrupt Service Routine (ISR)
  250. ;------------------------------------------------------------------------
  251. ;    Input:  none
  252. ;    Output:  none (char read and deposited in buffer)
  253. ;            NOTE: this version ignores buffer overflows
  254. ;       Registers:  none
  255. ;------------------------------------------------------------------------
  256. PROC    AsynchISR
  257.     push    ax
  258.     push    bx
  259.     push    ds
  260.     push    dx
  261.     mov    ax,@data
  262.     mov    ds,ax
  263.     mov    dx,RxRegister
  264.     in    al,dx
  265.     mov    bx,[bufHead]
  266.     mov    [byte ptr bx], al
  267.     inc    bx
  268.     cmp    bx,offset buffer + BufSize
  269.     jb    @@10
  270.     mov    bx, offset buffer
  271. @@10:
  272.     cmp    bx,[bufTail]
  273.     jne    @@20
  274.     mov    bx,[bufHead]
  275. @@20:
  276.     mov    [bufHead],bx
  277.     mov    al,EOI
  278.     out    Ctrl8259_0,al
  279.     pop    dx
  280.     pop    ds
  281.     pop    bx
  282.     pop    ax
  283.     iret
  284. ENDP    AsynchISR
  285.  
  286.     END
  287.