home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 14 / CDACTUAL.iso / cdactual / demobin / share / program / asm / RS232DVR.ZIP / RS232.INC < prev   
Encoding:
Text File  |  1987-08-14  |  17.7 KB  |  501 lines

  1.         page
  2. ;***********************************************************
  3. ;**                                                       **
  4. ;**  Device Driver for RS232 communications               **
  5. ;**  Copyright (C) Texas Instruments 1986                 **
  6. ;**  Author: Greg Haley                                   **
  7. ;**  revisor: Joe McDaniel, Fein-Marquart          **
  8. ;**                                                       **
  9. ;**  THIS SOURCE CODE MAY BE DISTRIBUTED AND MODIFIED     **
  10. ;**  ONLY IF THE ORIGINAL COPYRIGHT AND AUTHOR CREDITS    **
  11. ;**  REMAIN INTACT.                                       **
  12. ;**                                                       **
  13. ;**  Project Start Date: 09/09/86                         **
  14. ;**                                                       **
  15. ;**  Re: 10/14/86 by Greg Haley                           **
  16. ;**    Added more delays (jmp $+2) for more tolerant      **
  17. ;**    timing. Also increased stacks to 80 words for      **
  18. ;**    Business Pro compatibility.                        **
  19. ;**                                                       **
  20. ;**  Re: 10/15/86 by Greg Haley                           **
  21. ;**    The COMM chip is no longer initialized on every    **
  22. ;**    write request.                                     **
  23. ;**                                                       **
  24. ;**  Re: 10/23/86 by Greg Haley                           **
  25. ;**    Added code to block interrupts while setting up    **
  26. ;**    and restoring the stack.                           **
  27. ;**                                                       **
  28. ;**  Re: 11/07/86 by Greg Haley                           **
  29. ;**    Added option to echo RS232 I/O to the CRT.         **
  30. ;**                                                       **
  31. ;**  Re: 11/13/86 by Greg Haley                           **
  32. ;**    Save SI and AX when calling DOS fast write.        **
  33. ;**    Changed SAR's and SAL's to SHR's and SHL's.        **
  34. ;**    Added keyboard input in parallel with RS232 input. **
  35. ;**                                                       **
  36. ;**  Re: 11/20/86 by Greg Haley                           **
  37. ;**    Modified I/O channel commands so that a more       **
  38. ;**    standard structure could be used, and to make it   **
  39. ;**    easier to add new commands in the future. Also,    **
  40. ;**    all machine dependent code was moved to a seperate **
  41. ;**    file to allow new machines to be added much more   **
  42. ;**    easily.                                            **
  43. ;**                                                       **
  44. ;**  Re: 12/29/86 by Greg Haley                           **
  45. ;**    Fixed bug in XON-XOFF busy handling where XON      **
  46. ;**    was not sent to reset the busy condition.          **
  47. ;**                                                       **
  48. ;**  Re: 8/10/87 by Joe McDaniel              **
  49. ;**    1. Fixed bugs in xon/xoff handling. It now works      **
  50. ;**    2. Removed echoing options and keyboard input      **
  51. ;**    3. Added interrupt driven transmit          **
  52. ;**    4. Fixed bugs with RAW mode calls          **
  53. ;**    4. Removed the TI version as I had no way to test  **
  54. ;**       whether it would still work.              **
  55. ;**    5. Fixed the 19,200 baud stuff              **
  56. ;**    6. Fixed circular buffer bugs              **
  57. ;***********************************************************
  58.  
  59. cr      equ     13
  60. lf      equ     10
  61.  
  62. com1_tbl:                               ;header for device
  63.         dw      -1
  64.         dw      -1
  65.         dw      1100000000000000B       ; CHAR device with I/O control
  66.         dw      strategy
  67.         dw      entry
  68.         db      'AUX     '              ; Device name to use
  69.  
  70. ;       command jump table
  71. cmdtbl:
  72.         dw      init
  73.         dw      exit
  74.         dw      exit
  75.         dw      ioctl_in
  76.         dw      read
  77.         dw      nd_read
  78.         dw      input_status
  79.         dw      input_flush
  80.         dw      write
  81.         dw      write
  82.         dw      output_status
  83.         dw      exit
  84.         dw      ioctl_out
  85.         dw      exit            ; open
  86.         dw      exit            ; close
  87.         dw      exit
  88.         dw      exit
  89.  
  90. oldss   dw      0                       ; Old SS reg
  91. oldsp   dw      0                       ; Old SP reg
  92.         dw      80 dup (?)              ; New stack
  93. e_stack label   word
  94. ptrsav  dd      0                       ; DOS cmd block pointer
  95. init_once db    0                       ; flag for init
  96. dcw_len db      0                       ; DCW (0) or Length (not 0) mode
  97. tx_cnt    dw    0            ; count of bytes sent
  98.         page
  99. ;***********************************************************
  100. ;**  Device Driver Entry                                  **
  101. ;***********************************************************
  102. cmdlen  =       0       ;length of this command
  103. unit    =       1       ;sub unit specifier
  104. cmd     =       2       ;command code
  105. status  =       3       ;status
  106. media   =       13      ;media descriptor
  107. trans   =       14      ;transfer address
  108. count   =       18      ;count of blocks or characters
  109. start   =       20      ;first block to transfer
  110.  
  111. ;***********************************************************
  112. ;**  Strategy Routine                                     **
  113. ;***********************************************************
  114. stratp  proc    far
  115.  
  116. strategy:
  117.         mov     word ptr cs:[ptrsav],bx
  118.         mov     word ptr cs:[ptrsav+2],es
  119.         ret
  120.  
  121. stratp  endp
  122.  
  123. ;***********************************************************
  124. ;**  Entry Routine                                        **
  125. ;***********************************************************
  126. entry:
  127.         cli                             ; disable ints
  128.         mov     cs:word ptr oldss,ss
  129.         mov     cs:word ptr oldsp,sp
  130.         mov     sp,cs
  131.         mov     ss,sp
  132.         mov     sp,offset e_stack
  133.         sti                             ; enable ints
  134.         push    si
  135.         push    ax
  136.     push    cx
  137.         push    dx
  138.         push    di
  139.         push    bp
  140.         push    ds
  141.         push    es
  142.         push    bx
  143.  
  144.         lds     bx,cs:[ptrsav]  ;get pointer to i/o packet
  145.         mov     al,byte ptr ds:[bx].cmd ; al = command
  146.  
  147.         cbw
  148.         mov     si,offset cmdtbl
  149.         add     si,ax
  150.         add     si,ax
  151.         cmp     al,16
  152.         ja      cmderr
  153.  
  154.         les     di,dword ptr ds:[bx].trans    ; es:di = tranfer address
  155.     mov    cx,ds:[bx].count        ; cx=count
  156.  
  157.         push    cs
  158.         pop     ds
  159.         cld
  160.  
  161.         assume  ds:code
  162.  
  163.         jmp     word ptr [si]           ;go do command
  164.  
  165.         page
  166. ;***********************************************************
  167. ;**  Device Driver Exit                                   **
  168. ;***********************************************************
  169. bus$exit:                               ;device busy exit
  170.         mov     ah,00000011b
  171.         jmp     short done
  172.  
  173. cmderr:
  174.         mov     al,3                    ;unknown command error
  175.  
  176. err$exit:
  177.         mov     ah,10000001b            ;mark error return
  178.         jmp     short done
  179.  
  180. exitp   proc    far
  181.  
  182. exit:
  183.         mov     ax,0000000100000000b    ; mark as done
  184. done:
  185.         lds     bx,cs:[ptrsav]
  186.         mov     ds:word ptr [bx].status,ax ;mark operation complete
  187.  
  188.         pop     bx
  189.         pop     es
  190.         pop     ds
  191.         pop     bp
  192.         pop     di
  193.         pop     dx
  194.     pop    cx
  195.         pop     ax
  196.         pop     si
  197.  
  198. ; restore stack
  199.         cli                             ; disable ints
  200.         mov     ss,cs:word ptr oldss
  201.         mov     sp,cs:word ptr oldsp
  202.         sti                             ; enable ints
  203.  
  204.         ret                        ;restore regs and return
  205. exitp   endp
  206.  
  207.         page
  208. ;***********************************************************
  209. ;**  Write Routine                                        **
  210. ;**                                                       **
  211. ;**  ES:DI contain xfer adrs on entry                     **
  212. ;**  CX contains num chars to write                       **
  213. ;**                                                       **
  214. ;**                                                       **
  215. ;***********************************************************
  216. write   proc    near
  217.  
  218.     jcxz    write_2            ; if nothing to send, exit immediately
  219.  
  220.         push    es                      ; Move pointer to DS:SI
  221.         pop     ds
  222.         mov     si,di
  223.  
  224.         mov     dx,cs:word ptr tq_head    ; get transmitter head into dx
  225.  
  226.  
  227. send_chars:
  228.     mov    ax,tran_limit        ; see if room for more
  229.     cli
  230.     sub    ax,cs:tq_len        ; subtract current count
  231.     sti
  232.     jne    write_1            ; there is still room
  233.  
  234.     jmp    write_2            ; no more room
  235.  
  236. write_1:
  237.         lodsb                           ; al now has char
  238.         mov     di,offset tqueue        ; Queue char
  239.         add     di,dx            ; add in tq_head value
  240.         mov    cs:byte ptr [di],al    ; store char in trans_buffer
  241.     inc    dx            ; update header pointer, too
  242.     and    dx,tran_limit        ; and adjust for overflow
  243.         mov     cs:word ptr tq_head,dx  ; update head
  244.     cli
  245.         inc     cs:word ptr tq_len      ; Adjust queue length
  246.     sti
  247.  
  248.         loop    send_chars              ; look for more chars
  249.  
  250. write_2:
  251.     call    update_count        ; sets count=count-cx
  252.  
  253.     cli
  254.     cmp    cs:byte ptr xmit_busy,0    ; is transmitter busy?
  255.     je    write_3
  256.         jmp     write_4            ; it was, so we can just exit
  257.  
  258. write_3:
  259.     call    xmit_mt            ; force first character out
  260. write_4:
  261.     sti
  262.     jmp    exit
  263. write   endp
  264.  
  265.         page
  266. ;***********************************************************
  267. ;**  Non Destructive Read Routine                         **
  268. ;**                                                       **
  269. ;**  On entry, ES points to buffer segment                **
  270. ;***********************************************************
  271. nd_read         proc    near
  272.         mov     di,13                   ; point to byte buffer
  273.  
  274.         cmp     cs:word ptr rq_len,0    ; Any chars in receive buffer?
  275.         jnz     nd_read_1               ; Yes, continue
  276.         mov     ah,00000011b            ; Set busy & done
  277.         jmp     done                    ; We're done
  278.  
  279. nd_read_1:
  280.         mov     bx,offset rqueue        ; Get queue ptr
  281.         mov     dx,cs:word ptr rq_tail
  282.         add     bx,dx                   ; point at char
  283.         mov     al,byte ptr[bx]         ; al now has char
  284.  
  285.  
  286.         stosb                           ; store char in buffer
  287.         jmp     exit
  288. nd_read         endp
  289.  
  290.         page
  291. ;***********************************************************
  292. ;**  Get Input Status                                     **
  293. ;***********************************************************
  294. input_status    proc    near
  295.         cmp     cs:word ptr rq_len,0    ; Any chars in receive buffer?
  296.         jnz     xit_in_stat             ; Yes, continue
  297.         mov     ah,00000011b            ; No, set busy & done
  298.         jmp     done                    ; We're done
  299.  
  300. xit_in_stat:
  301.         jmp     exit                    ; Normal exit code
  302. input_status    endp
  303.  
  304.         page
  305. ;***********************************************************
  306. ;**  Flush Input Buffer                                   **
  307. ;***********************************************************
  308. input_flush     proc    near
  309.         mov     cs:word ptr rq_head,0   ; Head = 0
  310.         mov     cs:word ptr rq_tail,0   ; Tail = 0
  311.         mov     cs:word ptr rq_len,0    ; make queue length 0
  312.         jmp     exit                    ; Normal exit code
  313. input_flush     endp
  314.  
  315.         page
  316. ;***********************************************************
  317. ;**  Get Output Status                                    **
  318. ;***********************************************************
  319. output_status   proc    near
  320.         cmp     cs:byte ptr t_busy,0    ; Are we busy?
  321.         je      xit_out_stat            ; No, continue
  322.         mov     ah,00000011b            ; Set busy & done
  323.         jmp     done                    ; We're done
  324.  
  325. xit_out_stat:
  326.         jmp     exit                    ; Normal exit code
  327. output_status   endp
  328.  
  329.         page
  330. ;***********************************************************
  331. ;**  Read Routine                                         **
  332. ;**                                                       **
  333. ;**  ES:DI contain xfer adrs on entry                     **
  334. ;**  CX contains num chars requested                      **
  335. ;***********************************************************
  336. read    proc    near
  337.     cli
  338.         mov     ax,cs:word ptr rq_len   ; Any chars in recv buffer?
  339.     sti
  340.         or      ax,ax
  341.         jnz     read_1                  ; Yes, continue
  342.         call    update_count        ; count=count-cx (therefore, 0)
  343.         jmp     exit                    ; We're done
  344.  
  345. read_1:
  346. ; figure out how many chars to get
  347.         cmp     ax,cx                   ; More than DOS buffer?
  348.         jg      read_2                  ;   Yes, CX already has count
  349.         mov     cx,ax                   ;   No, only send what we have
  350.         lds     bx,cs:[ptrsav]            ;get pointer to i/o packet
  351.     mov    word ptr ds:[bx].count,cx    ;cx = count of bytes remaining 
  352.                         ;to process
  353.  
  354. read_2:
  355.         mov     dx,cs:word ptr rq_tail
  356.  
  357. read_3:
  358.         mov     si,offset rqueue        ; Get queue ptr
  359.         add     si,dx                   ; point at char
  360.         mov     al,cs:byte ptr[si]    ; al now has char
  361.  
  362.  
  363.         stosb                           ; store char in buffer
  364.         inc     dx
  365.         and     dx,recv_limit           ; wrap if >= limit
  366.         mov     cs:word ptr rq_tail,dx  ; update tail
  367.     cli
  368.         dec     cs:word ptr rq_len      ; Adjust queue length
  369.     sti
  370.         loop    read_3                  ; Loop again
  371.  
  372.         call    update_count            ; count=count-[cx]
  373.  
  374. ; reset busy if we can
  375.     cli
  376.         cmp     cs:word ptr rq_len,not_busy_len ; Room for more chars?
  377.     sti
  378.         jg      read_4                  ; No, skip
  379.         mov     cs:byte ptr r_busy,0    ; Yes, reset busy
  380.     mov    al,xon
  381.     cli
  382.         call    send_xon                ; Send XON char if needed
  383.     sti
  384. read_4:
  385.  
  386.         jmp     exit
  387. read    endp
  388.  
  389.         page
  390. ;***********************************************************
  391. ;**  Write I/O Control Channel                            **
  392. ;**                                                       **
  393. ;**  ES:DI contain xfer adrs on entry                     **
  394. ;**  CX contains count                                    **
  395. ;***********************************************************
  396. ioctl_out       proc    near
  397.         push    es                      ; Move pointer to DS:SI
  398.         pop     ds
  399.         mov     si,di
  400.  
  401.         cmp     cx,2                    ; Is it a DCW?
  402.         je      ioo_1                   ;  Yes, continue
  403.         cmp     cx,1                    ; Is it a CSB?
  404.         jz      ioo_nc0                 ;  Yes, continue
  405.         jmp     exit                    ;  No, exit
  406.  
  407. ioo_nc0:
  408.         lodsb                           ;  Yes, get the byte
  409.  
  410.         push    cs                      ; DS = CS
  411.         pop     ds
  412.  
  413.         mov     ah,al                   ;  Update DCW/LEN mode
  414.         and     ah,00000100b
  415.         mov     dcw_len,ah
  416.  
  417.         test    al,00000001b            ;  Drop RS232 signals?
  418.         jnz     ioo_nc1                 ;  No, skip
  419.         call    de_init                 ;  Yes, disconnect
  420.         mov     byte ptr dcw_len,0      ; Reset to DCW mode
  421. ioo_nc1:
  422.  
  423.         test    al,00000010b            ;  Flush input buffer?
  424.         jz      ioo_nc2                 ;  No, skip
  425.         jmp     input_flush             ;  Yes, go flush it
  426. ioo_nc2:
  427.  
  428.         jmp     exit                    ;  exit
  429. ioo_1:
  430.         lodsw                           ; get DCW in AX
  431.  
  432. ; make ds = cs
  433.         push    cs
  434.         pop     ds
  435.  
  436. ; check for serial device
  437.         test    ah,80h                  ; Serial device?
  438.         jnz     ioo_2                   ;  Yes, continue
  439.         jmp     exit                    ;  No, exit
  440. ioo_2:
  441.  
  442.         call    set_dcw                 ; Go set DCW
  443.  
  444.         mov     word ptr dcw,ax         ; Save new DCW
  445.         call    init_comm               ; init comm chip
  446.         jmp     exit
  447.  
  448. ioctl_out       endp
  449.  
  450.         page
  451. ;***********************************************************
  452. ;**  Read I/O Control Channel                             **
  453. ;**                                                       **
  454. ;**  ES:DI contain xfer adrs on entry                     **
  455. ;***********************************************************
  456. ioctl_in        proc    near
  457.         cmp     cx,2                    ; Is it a word?
  458.         je      is_2byte                ;  Yes, go get it
  459.         cmp     cx,1                    ; Is it a byte?
  460.         jne     ioi_xit                 ;  No, exit
  461.  
  462.         xor     al,al                   ; Clear status byte
  463.         or      al,cs:byte ptr m_stat   ; Update current modem status
  464.         stosb                           ; and pass it to DOS
  465.         jmp     exit
  466.  
  467. is_2byte:
  468.         cmp     cs:byte ptr dcw_len,0   ; DCW mode?
  469.         jz      ioi_1                   ; Yes, get DCW
  470.  
  471.         mov     ax,cs:rq_len            ; No, get receive queue length
  472.         jmp     short ioi_2
  473.  
  474. ioi_1:
  475.         mov     ax,cs:dcw               ; Get DCW
  476. ioi_2:
  477.         stosw                           ; and pass it to DOS
  478.  
  479. ioi_xit:
  480.         jmp     exit
  481. ioctl_in        endp
  482.  
  483.         page
  484. ;***********************************************************
  485. ;**  Return Number of Chars Processed                     **
  486. ;**                                                       **
  487. ;**  CX contains number of characters NOT processed       **
  488. ;***********************************************************
  489. update_count    proc near
  490.         push    ds
  491.         push    bx
  492.  
  493.         lds     bx,cs:[ptrsav]  ;get pointer to i/o packet
  494.     sub    word ptr ds:[bx].count,cx    ;cx = count of bytes remaining 
  495.                         ;to process
  496.  
  497.     pop     bx
  498.         pop     ds
  499.         ret
  500. update_count    endp
  501.