uses Dos, Crt;

Var

Cti : Char;
Hour, Min, Sec, Hund, Prev_Sec : Word;
Kmitocet, Zpomal, ALFArev, DELTArev : Integer;
ALFA, DELTA,ALFAtrvaleVZAD, ALFAtrvaleVPRED,
DELTAtrvaleVZAD, DELTAtrvaleVPRED : Integer;
Rychlost, Pauza, PRESUN, Tlacitko : Integer;
Hodnota09, Hodnota11 : Integer;
portA, portB : Integer;
portA7, portA6, portA5, portA4, portA3, portA2, portA1, portA0 : Integer;
portB7, portB6, portB5, portB4, portB3, portB2, portB1, portB0 : Integer;
{****************************************************************}
{****************************************************************}
{****************************************************************}
{****************************************************************}
Begin;

Cti := ' ';
Prev_Sec := 0;
Kmitocet := 0;
Zpomal := 1;
ALFA := 0;
DELTA := 0;
ALFAtrvaleVPRED := 0;
ALFAtrvaleVZAD := 0;
DELTAtrvaleVPRED := 0;
DELTAtrvaleVZAD := 0;
ALFArev := 0;
DELTArev := 0;
PRESUN := 0;
Tlacitko := 0;
Pauza := 500;
Rychlost := 5;

TextBackGround(Blue);
ClrScr;

TextColor(Black);
TextBackGround(LightGray);
GotoXY(1, 1);
Write('                                                                                ');

TextColor(Red);
TextBackGround(LightGray);
GotoXY(1, 25);
Write(' Esc        Q      A                                                           ');
 TextColor(Black);
 GotoXY(6, 25);
 Write('Konec');
  GotoXY(15, 25);
  Write('+Hz');
   GotoXY(22, 25);
   Write('-Hz');


{****************************************************************}
{****************************************************************}
{****************************************************************}
{****************************************************************}
Repeat;
{****************************************************************}
Cti := ' ';
Hodnota09 := 0;
Hodnota11 := 0;
GetTime( Hour, Min, Sec, Hund);
If KeyPressed then Cti := ReadKey;
{****************************************************************}
{
If Cti='z' then ALFArev := 0;
If Cti='x' then ALFArev := 8;
If ( ALFA = 0 ) then ALFArev := 0;
If Cti='w' then DELTArev := 0;
If Cti='s' then DELTArev := 1;
If ( DELTA = 0 ) then DELTArev := 0;
If ( Cti='1') and ( ALFA=4 ) then Begin;ALFA:=0;Cti:=' ';End;
If ( Cti='1') and ( ALFA=0 ) then Begin;ALFA:=4;Cti:=' ';End;
If ( Cti='2') and ( DELTA=2 ) then Begin;DELTA:=0;Cti:=' ';End;
If ( Cti='2') and ( DELTA=0 ) then Begin;DELTA:=2;Cti:=' ';End;
If Cti='a' then Zpomal := Zpomal + 1;
If Cti='q' then Zpomal := Zpomal - 1;
If Zpomal < 0 then Zpomal := 1;
If Cti='e' then Zpomal := 120;
If Cti='r' then Zpomal := 1;
}

 If ( PRESUN = 0) then Begin;
 ALFAtrvaleVPRED:=0;ALFAtrvaleVZAD:=0;
 DELTAtrvaleVPRED:=0;DELTAtrvaleVZAD:=0;End;
ALFA := 0;
DELTA := 0;
ALFArev := 0;
DELTArev := 0;
If ( portA4 = 0 ) then HODNOTA11 := HODNOTA11 + 4;
 If ( portA4 = 0) and ( PRESUN = 1) then
  Begin;ALFAtrvaleVPRED:=1;ALFAtrvaleVZAD:=0;End;
   If ( ALFAtrvaleVPRED = 1)  and ( PRESUN = 1) then Begin;HODNOTA11 := HODNOTA11 + 4;End;
If ( portA5 = 0) then Begin;HODNOTA11 := HODNOTA11 + 4 + 8;End;
 If ( portA5 = 0) and ( PRESUN = 1) then
  Begin;ALFAtrvaleVZAD:=1;ALFAtrvaleVPRED:=0;End;
   If ( ALFAtrvaleVZAD = 1)  and ( PRESUN = 1) then Begin;HODNOTA11 := HODNOTA11 + 4 + 8;End;


If ( portA7 = 1 ) then HODNOTA11 := HODNOTA11 + 2;
 If ( portA7 = 1) and ( PRESUN = 1) then
  Begin;DELTAtrvaleVPRED:=1;DELTAtrvaleVZAD:=0;End;
   If ( DELTAtrvaleVPRED = 1)  and ( PRESUN = 1) then Begin;HODNOTA11 := HODNOTA11 + 2;End;

If ( portB4 = 0) then Begin;HODNOTA11 := HODNOTA11 + 2 + 1;End;
 If ( portB4 = 0) and ( PRESUN = 1) then
  Begin;DELTAtrvaleVZAD:=1;DELTAtrvaleVPRED:=0;End;
   If ( DELTAtrvaleVZAD = 1)  and ( PRESUN = 1) then Begin;HODNOTA11 := HODNOTA11 + 2 + 1;End;


If ( portA3 = 0 ) and ( Tlacitko = 0 )
   then Begin;Rychlost := Rychlost + 1;Tlacitko:=Pauza;End;
 If ( Rychlost < 1 ) then Rychlost := 1;
If ( portA6 = 0 ) and ( Tlacitko = 0 )
   then Begin;Rychlost := Rychlost - 1;Tlacitko:=Pauza;End;
 If ( Rychlost > 5 ) then Rychlost := 5;
If ( Rychlost = 5 ) then Zpomal := 1;
 If ( Rychlost = 4 ) then Zpomal := 5;
  If ( Rychlost = 3 ) then Zpomal := 80;
   If ( Rychlost = 2 ) then Zpomal := 120;
    If ( Rychlost = 1 ) then Zpomal := 150;
If ( Rychlost = 1 ) then Hodnota09 := Hodnota09 + 128;
 If ( Rychlost = 2 ) then Hodnota09 := Hodnota09 + 128 + 64;
  If ( Rychlost = 3 ) then Hodnota09 := Hodnota09 + 64;
   If ( Rychlost = 4 ) then Hodnota09 := Hodnota09 + 64 + 32;
    If ( Rychlost = 5 ) then Hodnota09 := Hodnota09 + 32;



If ( portA3 = 0 ) and ( portA6 = 0 )
and ( PRESUN = 0 ) and ( Tlacitko < 10 ) then
Begin;Tlacitko:=Pauza;PRESUN := 1;End;

If ( portA3 = 0 ) and ( portA6 = 0 )
and ( PRESUN = 1 ) and ( Tlacitko < 10 )
then Begin;PRESUN := 0;Tlacitko:=Pauza;;End;

If ( Tlacitko > 0 ) then Tlacitko := Tlacitko - 1 - Zpomal;
If ( Tlacitko < 0 ) then Tlacitko := 0;




Port[ $378 + 2 ] := 11;
Port[ $378 ] := Hodnota11;

Port[ $378 + 2 ] := 9;
Port[ $378 ] := Hodnota09;
{****************************************************************}
{******************** Vyhodnoceni portA *************************}
{****************************************************************}
portA7 := 0;
 portA6 := 0;
  portA5 := 0;
   portA4 := 0;
    portA3 := 0;
     portA2 := 0;
      portA1 := 0;
       portA0 := 0;
Port[ $378 + 2 ] := 0;
 portA := Port[ $378 + 1 ];
If ( portA > 127 ) then Begin; portA7 := 1; portA := portA - 128; End;
If ( portA >  63 ) then Begin; portA6 := 1; portA := portA -  64; End;
If ( portA >  31 ) then Begin; portA5 := 1; portA := portA -  32; End;
If ( portA >  15 ) then Begin; portA4 := 1; portA := portA -  16; End;
If ( portA >   7 ) then Begin; portA3 := 1; portA := portA -   8; End;
If ( portA >   3 ) then Begin; portA2 := 1; portA := portA -   4; End;
If ( portA >   1 ) then Begin; portA1 := 1; portA := portA -   2; End;
If ( portA >   0 ) then Begin; portA0 := 1; portA := portA -   1; End;
{****************************************************************}
{****************************************************************}
{******************** Vyhodnoceni portB *************************}
{****************************************************************}
portB7 := 0;
 portB6 := 0;
  portB5 := 0;
   portB4 := 0;
    portB3 := 0;
     portB2 := 0;
      portB1 := 0;
       portB0 := 0;
Port[ $378 + 2 ] := 4;
 portB := Port[ $378 + 1 ];
If ( portB > 127 ) then Begin; portB7 := 1; portB := portB - 128; End;
If ( portB >  63 ) then Begin; portB6 := 1; portB := portB -  64; End;
If ( portB >  31 ) then Begin; portB5 := 1; portB := portB -  32; End;
If ( portB >  15 ) then Begin; portB4 := 1; portB := portB -  16; End;
If ( portB >   7 ) then Begin; portB3 := 1; portB := portB -   8; End;
If ( portB >   3 ) then Begin; portB2 := 1; portB := portB -   4; End;
If ( portB >   1 ) then Begin; portB1 := 1; portB := portB -   2; End;
If ( portB >   0 ) then Begin; portB0 := 1; portB := portB -   1; End;
{****************************************************************}

  GotoXY(29, 11);
  Write(PRESUN);

Port[ $378 + 2 ] := 15;
Port[ $378 ] := 0;




{
----------------------------------------
              I  N
----------------------------------------
  - - - portA - - -
#  0,   8 - frekvence +
#  0,  16 - alfa doleva
#  0,  32 - alfa doprava
#  0,  64 - frekvence -
#  0, 128 - delta nahoru
  - - - portB - - -
#  4,  16 - delta dolu
----------------------------------------
             O  U  T
----------------------------------------
#  9,  1 - zelena  - delta dolu
#  9,  2 - zelena  - alfa doprava
#  9,  4 - zluta   - trvaly beh motoru
#  9,  8 - zelena  - nahoru
#  9, 16 - zelena  - alfa doleva
#  9, 32 - cervena - frekvence max
#  9, 64 - zluta - frekvence stred
#  9,128 - zelena - frekvence minimalni
----------------------------------------
# 11,  1 - delta reverzace
# 11,  2 - delta krokovani
# 11,  4 - alfa krokovani
# 11,  8 - alfa reverzace
# 11, 16 -
# 11, 32 -
# 11,128 -
----------------------------------------
}
{****************************************************************}
{  TextColor(Yellow);
If Sec <> Prev_Sec then Begin;
  GotoXY(29, 11);
  Write(Kmitocet);
Kmitocet := 0;
                        End;}
{****************************************************************}
{
GotoXY(70, 25);
TextBackGround(LightGray);
TextColor(Blue);
Write(' 00:00:00 ');
GotoXY(71, 25);
If Hour < 10 then GotoXY(72, 25);
 Write(Hour);
 GotoXY(74, 25);
  If Min < 10 then GotoXY(75, 25);
  Write(Min);
  GotoXY(77, 25);
   If Sec < 10 then GotoXY(78, 25);
   Write(Sec);
    Prev_Sec := Sec;
}
{----------------------------------------------------------------}
{
TextBackGround(LightGray);
TextColor(Blue);
GotoXY(28, 10);
Write('  Hz  ');
GotoXY(35, 10);
Write('Delay');
 TextColor(White);
 TextBackGround(LightGray);


GotoXY(35, 11);
Write('     ');
 TextColor(Black);
 GotoXY(37, 11);
 Write(Zpomal);

TextColor(Blue);
TextBackGround(Blue);
}

{****************************************************************}
Delay(Zpomal);
Kmitocet := Kmitocet + 1;
{****************************************************************}
Port[ $378 + 2 ] := 11;
Port[ $378 ] := ALFArev + DELTArev;
{****************************************************************}
Until Cti = Char(27);
End.