     ----------------------------------------------------------------------
     -- Title       :  CommAKD
     -- Version     :  1.0
     -- Copyright   :  (C) 1994 David Wallace Croft.  All rights reserved.
     -- Author      :  David Wallace Croft, CompuServe [76600,102]
     -- Compiler    :  Meridian OpenAda for DOS
     -- Description :  Serial communications
     ----------------------------------------------------------------------

     with BitsAKD; use BitsAKD;

     package CommAKD is
     ----------------------------------------------------------------------
     ----------------------------------------------------------------------
     Copyright : constant string :=
       "CommAKD (C) 1994 David Wallace Croft.  All rights reserved.";
     ----------------------------------------------------------------------
     type Baud_Rate_Type is
       ( B110, B150, B300, B600, B1200, B2400, B4800, B9600 );
     subtype BPS_Type is long_integer range 2..115_200;
	     -- 2 BPS is minimum because BPS must be > or = (115,200/16#FFFF#).
	     -- 115,200 BPS = UART clock rate (1.8432 MHz) / 16.
     type    Parity_Type is ( None, Odd, Nada, Even );
     type    Parity_Type_2 is ( Ignore, dummy1, dummy2, dummy3,
				Odd,    Mark,   Even,   Space );
     type    Port_Type is ( COM1, COM2, COM3, COM4 );
     type    Register_8250_Type is
	       ( THR_RDR, BRDL, IER, BRDH, IIR, LCR, MCR, LSR, MSR );
     type    Registers_Type is
       record
	 THR_RDR: Byte_Type;
	 BRDL:    Byte_Type;
	 IER:     Byte_Type;
	 BRDH:    Byte_Type;
	 IIR:     Byte_Type;
	 LCR:     Byte_Type;
	 MCR:     Byte_Type;
	 LSR:     Byte_Type;
	 MSR:     Byte_Type;
       end record;
     subtype Status_Type is Word_Type;
     type    Stop_Bits_Type is ( S1, S2 );
     type    Char_Length_Type is ( C5, C6, C7, C8 );
     ---------------------------------------------------------------------------
     Offset: constant array ( THR_RDR..MSR ) of integer
       := ( 0, 0, 1, 1, 2, 3, 4, 5, 6 );
     Port_Base_Address: constant array ( Com1..Com4 ) of integer
       := ( 16#03F8#, 16#02F8#, 16#03F8#, 16#02F8# );
     ---------------------------------------------------------------------------
     type MCR_Record_Type is
       record
	 DTR_Active:     boolean;
	 RTS_Active:     boolean;
	 Hayes_Reset:    boolean;
	 Enable_Ints:    boolean;
	 UART_Loopback:  boolean;
       end record;
     ---------------------------------------------------------------------------
     function  Data_Is_Ready ( Status: in integer ) return boolean;
     procedure Demo;
     function  Get_IIR ( Port: in Port_Type ) return Byte_Type;
     function  Get_LSR ( Port: in Port_Type ) return Byte_Type;
     function  Get_MCR ( Port: in Port_Type ) return MCR_Record_Type;
     function  Get_MCR ( Port: in Port_Type ) return Byte_Type;
     function  Get_MSR ( Port: in Port_Type ) return Byte_Type;
     function  Get_RDR ( Port: in Port_Type ) return Byte_Type;
     procedure Interrupt_Handling ( Port: in Port_Type; On: in boolean );
     procedure Read_Char
		 ( Port: in Port_Type;
		   Is_Found: out boolean;
		   C: out character );
     procedure Registers_Display ( Registers: in Registers_Type );
     function  Registers_Fetch ( Port: in Port_Type ) return Registers_Type;
     function  Send
		 ( Port : in Port_Type;
		   C    : in Byte_Type
		 ) return boolean;
     procedure Set_BPS ( Port: in Port_Type; BPS: in BPS_Type );
     procedure Set_IER
		 ( Port:                in Port_Type;
		   IE_Data_Received:    in boolean;
		   IE_THR_Empty:        in boolean;
		   IE_Data_Error_Break: in boolean;
		   IE_MSR_Change:       in boolean
		 );
     procedure Set_LCR
		 ( Port:           in Port_Type;
		   Char_Length:    in Char_Length_Type;
		   Stop_Bits:      in Stop_Bits_Type;
		   Parity:         in Parity_Type_2;
		   Break_Enabled:  in boolean;
		   Port_Alternate: in boolean
		 );
     procedure Set_MCR ( Port: in Port_Type; MCR_Record: in MCR_Record_Type );
     procedure Set_MCR
		 ( Port:           in Port_Type;
		   DTR_Active:     in boolean;
		   RTS_Active:     in boolean;
		   Hayes_Reset:    in boolean;
		   Enable_Ints:    in boolean;
		   UART_Loopback:  in boolean
		 );
     procedure Setup
       ( Port:                in Port_Type        := Com1;
	 BPS:                 in BPS_Type         := 1200;
	 Char_Length:         in Char_Length_Type := C8;
	 Stop_Bits:           in Stop_Bits_Type   := S1;
	 Parity:              in Parity_Type_2    := Ignore;
	 Break_Enabled:       in boolean          := false;
	 Port_Alternate:      in boolean          := false;
	 DTR_Active:          in boolean          := true;
	 RTS_Active:          in boolean          := true;
	 Hayes_Reset:         in boolean          := false;
	 Enable_Ints:         in boolean          := true;
	 UART_Loopback:       in boolean          := false;
	 IE_Data_Received:    in boolean          := false;
	 IE_THR_Empty:        in boolean          := false;
	 IE_Data_Error_Break: in boolean          := false;
	 IE_MSR_Change:       in boolean          := false
       );
     procedure Setup
       ( Port:            in Port_Type;
	 Baud_Rate:       in Baud_Rate_Type;
	 Char_Length:     in Char_Length_Type;
	 Parity:          in Parity_Type;
	 Stop_Bits:       in Stop_Bits_Type;
	 Status:         out integer
       );
     procedure Status_Display ( Status: in Status_Type );
     function  Status_Fetch ( Port: in Port_Type ) return Status_Type;
     function  Status_Set ( LSR, MSR: in Byte_Type ) return Status_Type;
     ---------------------------------------------------------------------------
     ---------------------------------------------------------------------------
     end CommAKD;
