label

    STOP;

const

  { Insert The Following: }

    ACCESS    : String[8]  = '***-****';  { Telenet local number }
    TERMID    : String[2]  = '**';        { Terminal I.D. number }
    NAMEID    : String[20] = '*******';   { User Name on Telenet }
    PASSWORD  : String[20] = '********';  { Your secret password }

  { Smart Modem initialization string }

    SMINIT    : String[50] = 'ATQ0V1M1X0 S0=0 S2=3 S11=50';

  { Port locations for your computer }

    BAUDRP    = $00;                   { Baud rate port }
    EXTDAT    = $04;                   { External modem data port }
    EXTCTL    = $06;                   { External modem control port }

  { Masks for the status port }

    DAV       = $01;                   {Data available}
    TBE       = $04;                   {Transmit buffer empty}
    DCD       = $08;                   {Data Carrier Detect}

  { Define a carriage return }

    CR        = ^M;

type

    Str40     = String[40];

var

    BYTEIN    : Byte;
    CHARIN    : Char;
    ACODE     : String[3];
    WORDIN    : String[80];

function In_Modem : byte;
    begin
        In_Modem := port[EXTDAT]
    end;

procedure Out_Modem (Number : Str40);
    var
       J      : Byte;
    begin
       For J := 1 to length(Number) Do
           begin
                repeat                             {Loop until      }
                until (TBE and port[EXTCTL]) <> 0; {Modem ready then}
                Port[EXTDAT] := ord(Number[J]);    {Output character}
                write(Number[J]);
           end;
           Delay(1000);                            {Pause 1 sec.}
           BYTEIN := In_Modem;
           writeln;
    end;

procedure Initialize_Modem;
    begin
        port[EXTCTL] := $18;           {Reset SIO channel}
        port[EXTCTL] := 4;             {SIO register 4}
        port[EXTCTL] := $44;           {16x, 1 stop bit, no parity}
        port[EXTCTL] := 1;             {SIO register 1}
        port[EXTCTL] := $00;           {Waits, rx, tx disable}
        port[EXTCTL] := 3;             {SIO register 3}
        port[EXTCTL] := $0C1;          {8 bits/character, rx enable}
        port[EXTCTL] := 5;             {SIO register 5}
        port[EXTCTL] := $EA;           {DTR on, 8 bits/char, tx enable, RTS on}
        port[BAUDRP] := 7;             {1200 baud}
        delay(500);                    {Pause 500 ms}
        Out_Modem ('AT' + CR);         {Send modem attention}
        Out_Modem (SmInit + CR);       {Send SmartModem init string}
    end;

procedure Read_Until (TRIGGER : Char);
    begin
        repeat
            repeat
            until (DAV and port[EXTCTL]) <> 0;
            CHARIN := chr(port[EXTDAT]);
        until CHARIN = TRIGGER;
        writeln(CHARIN);
        delay(1000)
    end;

{ *************************************************************************** }
{ *                            MAIN PROGRAM                                 * }
{ *************************************************************************** }

begin
    for BYTEIN := 1 to 24 do writeln;
    writeln('                                    TELENET');
    writeln('                              Auto Log-On Program');
    writeln('                            7/25/86 by: Joseph Fall');
    delay(2000);
    for BYTEIN := 1 to 24 do writeln;
    if ParamCount = 0 then
        begin
            write('Enter Area Code please : ');
            readln(ACODE);
        end
    else
        ACODE := ParamStr(1);
    writeln('Initializing SIO and Modem ...');
    Initialize_Modem;
    Out_Modem('ATDT' + ACCESS + CR);

  { This loop assumes pin 8 (DCD) is used.  Some modems don't support this }
  { feature.  If not, insert your own routine or a sufficiently long delay }
  { to insure you have a carrier.                                          }

    repeat
        port[EXTCTL] := $10;                   { Reset status }
    until (DCD and port[EXTCTL]) <> 0;         { Data Carrier Detected }

    writeln('Connected ... Attempting to Log On.');
    delay(1000);
    Out_Modem(CR + CR);
    Read_Until('=');
    Out_Modem(TERMID + CR);
    Read_Until('@');
    Out_Modem('C DIAL' + ACODE + '/12,' + NAMEID + CR);
    Read_Until(' ');
    Out_Modem(PASSWORD + CR);
    writeln;
    writeln('+++ WAIT for TELENET reply ... then tap ANY KEY +++');
    repeat
        repeat
            if KeyPressed then goto STOP
        until ((DAV and port[EXTCTL]) <> 0);
        CHARIN := chr($7F and port[EXTDAT]);
        write(CHARIN)
    until KeyPressed;
STOP:
    writeln;
    writeln('+++ Press "H" to hang up ... "C" to continue +++');
    repeat
        read(KBD, CHARIN)
    until UpCase(CHARIN) in ['H','C'];
    if UpCase(CHARIN) = 'H' then
    begin
        writeln;
        writeln('Disconnecting ...');
        port[EXTCTL] := 5;
        port[EXTCTL] := $68;
        delay(2000);
        port[EXTCTL] := 5;
        port[EXTCTL] := $EA;
        Out_Modem('AT H' + CR);
        Halt
    end;
    writeln;
    writeln ('Completed ... return to terminal program now.');
end.
