(*>>>>>>>>>>>>KERMINIT>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*)

(*$I-*)
(*$R-*)
(*$S+*)
(*$V-*)

UNIT KERMINIT;      INTRINSIC  CODE  29;

INTERFACE

USES  kermglob,
      kermacia,
      kermutil;

PROCEDURE initialize;


IMPLEMENTATION

PROCEDURE initialize;

 reads system.miscinfo and the default parameter file kermit.data and 
 does other necessary initializations 


PROCEDURE  logo;

begin
  page( output );
  gotoxy(17,2);  write('K    E    R    M    I    T');
  gotoxy(21,4);  write('VERSION ', version );
  gotoxy(28,5);  write('for');
  gotoxy(17,6);  write('Apple ][(e) UCSD p-system');
  gotoxy(10,17); write('Adapted from the IBM PC UCSD version by :');
  gotoxy(24,19); write('P. Terpstra');
  gotoxy(20,20); write('University Groningen');
  gotoxy(23,21); write('Nijenborgh 16');
  gotoxy(25,22); write('Groningen');
  gotoxy(22,23); write('The Netherlands');
end;  { logo }


PROCEDURE  check_io ( io_status : integer;  filename : string );

begin
  if io_status <> 0 then
    begin
      io_error( io_status );
      gotoxy( 50, error_line );
      write( filename );
      exit( program );
    end;
end;   {  check_io  }

 
 
PROCEDURE get_crt_info;

 read system.miscinfo to get terminal independent screen operations 

var      byte : 0..255;
         i : integer;
 
begin
  reset( untyped_f, sys_misc_file );
  check_io( ioresult, sys_misc_file );
  i := blockread( untyped_f, file_buf[1], 1 );
  if i <> 1 then check_io( 64, sys_misc_file );
  close( untyped_f );
  prefix := file_buf[63];        byte := ord( file_buf[73] );
  prefixed[ sc_up      ] := odd( byte );         {cursor up}
  prefixed[ sc_right   ] := odd( byte div   2 ); {cursor right}
  prefixed[ sc_clreol  ] := odd( byte div   4 ); {clear to end of line}
  prefixed[ sc_clreos  ] := odd( byte div   8 ); {clear to end of screen}
  prefixed[ sc_home    ] := odd( byte div  16 ); {cursor home}
  prefixed[ sc_delchar ] := odd( byte div  32 ); {often not implemented}
  prefixed[ sc_clrall  ] := odd( byte div  64 ); {clear whole screen}
  prefixed[ sc_clrline ] := odd( byte div 128 ); {often not implemented}
  prefixed[ sc_left    ] := false; {cursor left:no prefix in sys.miscinfo}
  prefixed[ sc_down    ] := false; {cursor down:no prefix in sys.miscinfo}
  rlf       := file_buf[68]; {reverse line feed: cursor up}
  ndfs      := file_buf[67]; {non destructive space forward: cursor right}
  eraseol   := file_buf[66]; {from cursor: clear to end of line}
  eraseos   := file_buf[65]; {from cursor: clear to end of screen}
  home      := file_buf[64]; {cursor to x=0, y=0}
  delchar   := file_buf[69]; {backspace with deleting: often same as backsp}
  clrscreen := file_buf[72]; {clear whole screen}
  clrline   := file_buf[71]; {clear one whole line: often not possible}
  backsp    := chr( backspace ); {see kermglob: mostly chr(8) }
  lf        := chr( linefeed  ); {see kermglob: mostly chr(10) }
  cr        := chr( eoline );    {see kermglob: mostly chr(13) }
  ff        := chr( formfeed );  {see kermglob: mostly chr(12) }
end;  { procedure get_crt_info }
 

PROCEDURE read_defaults;

var temp : integer;
    temp_bool : boolean;

begin
  reset( def, setup_file );
  check_io( ioresult, setup_file );
  esc_char        := chr( def^ ); get( def );
  eoln_char       := chr( def^ ); get( def );
  my_quote        := chr( def^ ); get( def );
  my_pchar        := chr( def^ ); get( def );
  my_pad          :=      def^  ; get( def );
  soh_char        := chr( def^ ); get( def );
  int_key         := chr( def^ ); get( def );
  xon_char        := chr( def^ ); get( def );
  xoff_char       := chr( def^ ); get( def );
  xoff_w_time     :=      def^  ; get( def );
  max_pack        :=      def^  ; get( def );
  if (maxpack < 20) or (maxpack > def_maxpack) then  maxpack := def_maxpack;
  max_try         :=      def^  ; get( def );
  my_time         :=      def^  ; get( def );
  if my_time < 1 then my_time := 1 else if my_time > 31 then my_time := 31;
  half_duplex     := odd( def^ ); get( def );
  debug           := odd( def^ ); get( def );
  fwarn           := odd( def^ ); get( def );
  text_file       := odd( def^ ); get( def );
  no_ffeed        := odd( def^ ); get( def );
  rejectcntrlchar := odd( def^ ); get( def );
  temp_bool       := odd( def^ ); get( def );
  if temp_bool then bs_to_del := chr(del) else bs_to_del := backsp;
  temp            :=      def^  ; get( def );
  acia_implem     := unknown;
  if temp = 1 then acia_implem := A6551;
  if temp = 2 then acia_implem := A6850;
  acia_comm_reg   :=      def^  ; get( def );
  acia_cntrl_reg  :=      def^  ; get( def );
  new_baud        :=      def^  ; get( def );
  new_dbit        :=      def^  ; get( def );
  new_stopbit     :=      def^  ; get( def );
  temp            :=      def^  ; get( def );
  new_par := no_par;
  case temp of
    0 : new_par := no_par;
    1 : new_par := odd_par;
    2 : new_par := even_par;
    3 : new_par := mark_par;
    4 : new_par := space_par;
  end;
  temp           :=      def^   ;
  if temp = 80 then begin
                      no_sfb_char := no_sp_char;
                      sfb_char    := stop_flush_break_sp_char;
                    end
               else begin
                      no_sfb_char := scr_40_sp_char;
                      sfb_char    := all_sp_char;
                    end;
  close( def );
end;    {  read_defaults  }


PROCEDURE  other_defaults;

begin
  ibm       :=   false;
  emulate   :=   false;
  pr_out    :=   false;
  xdle_char :=   chr( xdle );
  xeol_char :=   eoln_char;
  quote     :=   my_quote;
  pad       :=   my_pad;
  pad_char  :=   my_pchar;
  xtime     :=   my_time;
  prefix_vol:=   ':';
  init_try  :=   5 * max_try;
  print_enable := test_printer;
  err_string   := 'Error at host';
  reset( p, cs_file );
  ctl_set := [ chr(0)..chr(31), chr(del) ];
  check_apple_char( no_sfb_char );
  check_apple_char( mask_msbit_remin );
  with controlword do
    begin
      channel := inp;
      purpose := status;
      reserved:= 0;
      special_req := none;
      filler  := 0
    end;
  baud := new_baud;
  if (acia_implem=A6551) or (acia_implem=A6850)
    then begin
           set_acia_parms( new_par, new_dbit, new_stopbit, new_baud );
           get_acia_parms( parity, databit, stopbit, baud );
         end
    else begin
           parity  := new_par;
           stopbit := new_stopbit;
           databit := new_dbit;
         end;
end;   {  other_defaults }


begin   {  initialize  }
  logo;
  get_crt_info;
  read_defaults;
  other_defaults;
  writeln;
  writeln;
end;   {  initialize  }


begin
end.   {  kerminit  }
