home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / TPTOOL3.ZIP / IBMCOM2.INC next >
Encoding:
Text File  |  1987-03-28  |  5.3 KB  |  253 lines

  1.  
  2. const ibmcom2_tag: string[90]
  3.    = #0'@(#)CURRENT_FILE LAST_UPDATE COM port interrupt library 1.0'#0;
  4. #log COM port interrupt library 1.0
  5.  
  6. (*
  7.  * package summary:
  8.  *
  9.  *    procedure ComLowerDtr;
  10.  *    procedure ComRaiseDtr;
  11.  *    function ComCarrierPresent: Boolean;
  12.  *    function ComRxChars: Integer;
  13.  *    procedure ComSetParams (Parity: Byte);
  14.  *                     ComEven, ComOdd, ComNone, ComMark, ComSpace
  15.  *    procedure ComSetSpeed (Rate: Integer);
  16.  *    procedure ComFlushRx;
  17.  *    procedure ComInstall (PortNum: Byte);
  18.  *    procedure ComRemove;
  19.  *    procedure ComTx (S: ComString);
  20.  *    procedure ComRx (var Ch: Char);
  21.  *
  22.  *)
  23.  
  24. const
  25.   ComRxQueueSize = 1000;
  26.  
  27. var
  28.   ComRxQueue     : Array [1..ComRxQueueSize] of Byte;
  29.   ComRxQueueIn   : Integer;
  30.   ComRxQueueOut  : Integer;
  31.   ComRxQueueChars: Integer;
  32.  
  33. const
  34.   ComDsSav: Integer = 0;
  35.  
  36. var
  37.   ComOldVecSeg: Integer;
  38.   ComOldvecOfs: Integer;
  39.   ComBase     : Integer;
  40.   ComInt      : Byte;
  41.   Com_8259bit : Byte;
  42.  
  43. var
  44.   ComRegw: record
  45.     ax, bx, cx, dx, bp, si, di, ds, es, flags: Integer
  46.   end;
  47.  
  48. type
  49.   ComRegbyte = record
  50.     al, ah, bl, bh, cl, ch, dl, dh: Byte
  51.   end;
  52.  
  53. var
  54.   ComRegb: ComRegbyte absolute ComRegw;
  55.  
  56. const
  57.   ComEven = $1a;
  58.   ComOdd  = $0a;
  59.   ComNone = $03;
  60.   ComMark = $2a;
  61.   ComSpace= $3a;
  62.  
  63. type
  64.   ComString = string[255];
  65.  
  66.  
  67. procedure ComLowerDtr;
  68. begin
  69.   Port [ComBase + 4] := Port [ComBase + 4] and not 1
  70. end;
  71.  
  72. procedure ComRaiseDtr;
  73. begin
  74.   Port [ComBase + 4] := Port [ComBase + 4] or 1
  75. end;
  76.  
  77. function ComCarrierPresent: Boolean;
  78. begin
  79.   ComCarrierPresent := Odd (Port [ComBase + 6] shr 7)
  80. end;
  81.  
  82. function ComRxChars: Integer;
  83. begin
  84.   ComRxChars := ComRxQueueChars
  85. end;
  86.  
  87. procedure ComSetParams (Parity: Byte);
  88. begin
  89.   InLine ($fa);
  90.   Port [ComBase + 3] := Port [ComBase + 3] and $bf or Parity;
  91.   InLine ($fb)
  92. end;
  93.  
  94. procedure ComSetSpeed (Rate: Integer);
  95. var
  96.   Divisor: Integer;
  97. begin
  98.   Divisor := Round (115200. / Rate);
  99.   Inline ($fa);
  100.   Port [ComBase + 3] := Port [ComBase + 3] or $80;
  101.   Port [ComBase] := Lo (Divisor);
  102.   Port [ComBase + 1] := Hi (Divisor);
  103.   Port [ComBase + 3] := Port [ComBase + 3] and not $80;
  104.   Inline ($fb)
  105. end;
  106.  
  107. procedure ComFlushRx;
  108. begin
  109.   InLine ($fa);
  110.   ComRxQueueIn    := 1;
  111.   ComRxQueueOut   := 1;
  112.   ComRxQueueChars := 0;
  113.   InLine ($fb)
  114. end;
  115.  
  116. procedure ComInterruptDriver;
  117. var
  118.   Data: Byte;
  119. begin
  120.   InLine
  121.    ($50/          {Push ax      }
  122.     $53/          {Push bx      }
  123.     $51/          {Push cx      }
  124.     $52/          {Push dx      }
  125.     $57/          {Push di      }
  126.     $56/          {Push si      }
  127.     $06/          {Push es      }
  128.     $1E/          {Push ds      }
  129.     $2E/          {cs:          }
  130.     $A1/ComDsSav/ {Mov  ax,DsSav}
  131.     $8E/$D8);     {Mov  ds,ax   }
  132.  
  133.   while Odd (Port [ComBase + 5] ) do begin
  134.     Data := Port [ComBase];
  135.  
  136.     if ComRxQueueChars < ComRxQueueSize then begin
  137.       ComRxQueue [ComRxQueueIn] := Data;
  138.       if ComRxQueueIn < ComRxQueueSize then
  139.         ComRxQueueIn := ComRxQueueIn + 1
  140.       else
  141.         ComRxQueueIn := 1;
  142.  
  143.       ComRxQueueChars := ComRxQueueChars + 1
  144.     end
  145.   end;
  146.  
  147.   Port [$20] := $20;
  148.  
  149.   InLine
  150.    ($1F/       {Pop  ds      }
  151.     $07/       {Pop  es      }
  152.     $5E/       {Pop  si      }
  153.     $5F/       {Pop  di      }
  154.     $5A/       {Pop  dx      }
  155.     $59/       {Pop  cx      }
  156.     $5B/       {Pop  bx      }
  157.     $58/       {Pop  ax      }
  158.     $89/$EC/   {Mov  Sp,bp   }
  159.     $5D/       {Pop  bp      }
  160.     $CF)       {IRet         }
  161. end;
  162.  
  163. procedure ComInstall (PortNum: Byte);
  164. begin
  165.   with ComRegb do 
  166.   with ComRegw do begin
  167.  
  168.     case PortNum of
  169.       1: begin
  170.           ComBase     := $3f8;
  171.           ComInt      := $0c;
  172.           Com_8259bit := $10;
  173.         end;
  174.  
  175.       2: begin
  176.           ComBase     := $2f8;
  177.           ComInt      := $0b;
  178.           Com_8259bit := $08;
  179.         end
  180.     end;
  181.  
  182.     ComDsSav := Dseg;
  183.     ah := $35;
  184.     al := ComInt;
  185.     MsDos (ComRegw);
  186.  
  187.     ComOldVecSeg := es;
  188.     ComOldVecOfs := bx;
  189.     ah := $25;
  190.     al := ComInt;
  191.     dx := Ofs (ComInterruptDriver);
  192.     ds := CSeg;
  193.     MsDos (ComRegw);
  194.  
  195.     InLine ($fa);
  196.     Port [ComBase + 3] := Port [ComBase + 3] and not $80;
  197.     Port [ComBase + 1] := $01;
  198.     Port [ComBase + 4] := $0B;
  199.     Port [$21] := Port [$21] and not Com_8259bit;
  200.     InLine ($fb);
  201.  
  202.     ComFlushRx
  203.   end
  204. end;
  205.  
  206. procedure ComRemove;
  207. begin
  208.   with ComRegb do 
  209.   with ComRegw do begin
  210.     Inline ($fa);
  211.     Port [$21] := Port [$21] or Com_8259bit;
  212.     Port [ComBase + 4] := Port [ComBase + 4] and $f7;
  213.     InLine ($fb);
  214.  
  215.     ah := $25;
  216.     al := ComInt;
  217.     ds := ComOldVecSeg;
  218.     dx := ComOldVecOfs;
  219.     MsDos (ComRegw)
  220.   end
  221. end;
  222.  
  223. procedure ComTx (S: ComString);
  224. var
  225.   i: integer;
  226. begin
  227.   for i := 1 to length(s) do
  228.   begin
  229.      InLine ($fb);
  230.      while not Odd (Port [ComBase + 5] shr 5) do ;
  231.      InLine ($fa);
  232.      Port [ComBase] := Ord (S[i]);
  233.      InLine ($fb)
  234.   end;
  235. end;
  236.  
  237. procedure ComRx (var Ch: Char);
  238. begin
  239.   InLine ($fb);
  240.   repeat until ComRxQueueChars > 0;
  241.   InLine ($fa);
  242.  
  243.   Ch := Chr (ComRxQueue [ComRxQueueOut] );
  244.   if ComRxQueueOut < ComRxQueueSize then
  245.     ComRxQueueOut := ComRxQueueOut + 1
  246.   else
  247.     ComRxQueueOut := 1;
  248.  
  249.   ComRxQueueChars := ComRxQueueChars - 1;
  250.   InLine ($fb)
  251. end;
  252.  
  253.