program Example_CrtCom_2;

uses
  CrtType,      {typy Video a Keyboard BIOSu}
 {$ifdef MCP}
  Crt2Mem,      {obsluhy Video a Keyboard BIOSu pro komunikaci na KitXxx}
 {$endif}
  Crt,          {Pascalske funkce pro VGA a klavesnici}
  Kernel,       {o.s.Retos}
  CrtWin,       {Obsluha oken pod o.s.Retos}
  NumToStr,     {Prevody cisel na string}
  ChnVirt,      {jednotne rozhranni vsech komunikacnich knihoven}
  ChnCom,       {komunikacni knihovna pro fyzicke rozhranni UART i8250}
  ChnCrt,       {komunikacni protokol pro prenos Video a Keyboard}
  CrtComSl;     {komunikacni automat pro prenos Video a Keyboard}

{----------------------------------------------------------------------------}
const
  cAppVer = '01.00,07.06.2004'; {verze tohoto prikladu}

const
  Proc1Win   : pCrtWin = nil; {okno procesu 1}
  Proc2Win   : pCrtWin = nil; {okno procesu 2}
  Proc3Win   : pCrtWin = nil; {okno procesu 3}
  cProc1TerRect : tRect = (X: 2; Y: 4; W:38; H:22);
  cProc2TerRect : tRect = (X:42; Y: 4; W:38; H:22);
  cProc3TerRect : tRect = (X: 2; Y:28; W:78; H:22);

const
  Counter1 : word = 0; {citac pro priklad}
  Counter2 : word = 0; {citac pro priklad}
  Counter3 : word = 0; {citac pro priklad}

const
  FlEnd : boolean = False; {priznak pro ukonceni aplikace}

{----------------------------------------------------------------------------}
procedure Proces1; far;
var K:char;
begin
  {inicializace okna}
  Proc1Win:=New(PCrtWin,Init(cProc1TerRect,' Proces 1 ',FrameD,
             (Blue shl 4) or White{barva nadpisu},
             (Blue shl 4) or White{barva ramecku},
             (Blue shl 4) or Yellow{barva textu},
             $20,$0){tvar kurzoru:neviditelny});
  Proc1Win^.LockWin;
   ClrScr;
  Proc1Win^.UnLockWin;

  Start('Proces1',4096{Stack},99{SPRio},254{DPrio}); Exit;
  repeat
    Inc(Counter1);
    Proc1Win^.LockWin;
     gotoxy(1,1);
     writeln('Counter1: ',Counter1:5);
     writeln('Counter2: ',Counter2:5);
     writeln('Counter3: ',Counter3:5);
     writeln;
     TextColor(White);
     writeln('Komunikace s CRT terminalem');
     TextColor(Yellow);
     writeln(' Pocet prijatych zprav: ',CounterRecMess:5);
     writeln(' Pocet chyb pri prijmu: ',CounterErrMess:5);
     writeln(' Kod posledni chyby   : ',WordStrHex(LastErrMess),'h');
     gotoxy(1,10); TextAttr:=TextAttr or Blink; ClrEol;
     write('- Zmackni <Enter> pro zadani textu -');
     if keypressed then
     begin
       K:=Readkey;
       if K=#$0D{Enter} then
       begin
         gotoxy(1,10); TextAttr:=TextAttr and not Blink; ClrEol;
         write('Zadej text a potom <Enter> :');
         Proc1Win^.SetCursor(6,7);
         readln;
         Proc1Win^.HiddenCursor;
       end;
       if K=#$1B{Esc} then FlEnd:=True;{ukonceni programu}
     end;
     TextAttr:=TextAttr and not Blink;
    Proc1Win^.UnLockWin;
     Wait(1);
  until FlEnd;
  Terminate;
end;

procedure Proces2; far;
begin
  {inicializace okna}
  Proc2Win:=New(PCrtWin,Init(cProc2TerRect,' Proces 2 ',FrameD,
             (Cyan shl 4) or White{barva nadpisu},
             (Cyan shl 4) or White{barva ramecku},
             (Cyan shl 4) or Black{barva textu},
             $20,$0){tvar kurzoru:neviditelny});
  Proc2Win^.LockWin;
   ClrScr;
  Proc2Win^.UnLockWin;

  Start('Proces2',4096{Stack},100{SPRio},254{DPrio}); Exit;
  repeat
    Inc(Counter2);
    Proc2Win^.LockWin;
     if Random(200)=0 then ClrScr; {jednou za cas vymazeme okno}
     write(Counter2,',');
    Proc2Win^.UnLockWin;
    Wait(2);
  until FlEnd;
  Terminate;
end;

procedure Proces3; far;
begin
  {inicializace okna}
  Proc3Win:=New(PCrtWin,Init(cProc3TerRect,' Proces 3 ',FrameD,
             (Green shl 4) or LightRed{barva nadpisu},
             (Green shl 4) or White{barva ramecku},
             (Green shl 4) or LightRed{barva textu},
             $20,$0){tvar kurzoru:neviditelny});
  Proc3Win^.LockWin;
   ClrScr;
  Proc3Win^.UnLockWin;

  Start('Proces3',4096{Stack},101{SPRio},254{DPrio}); Exit;
  repeat
    Inc(Counter3);
    Proc3Win^.LockWin;
     if Random(150)=0 then ClrScr;{jednou za cas vymazeme okno}
     if Random(30)=0 then TextColor(Random(16)); {jednou za cas zmenime barvu textu}
     gotoxy(Random(cProc3TerRect.W)+1,Random(cProc3TerRect.H)+1);{skok na nahodnou pozici v okne}
     write(Counter3);
    Proc3Win^.UnLockWin;
    Wait(4);
  until FlEnd;
  Terminate;
end;

{----------------------------------------------------------------------------}
Begin
 {Inicializace komunikacniho automatu}
 {$ifdef Kit188}
  if InitCrtCom('NAM=CRT MAS=SLAVE LSB=200 '+
                'NAM=COM ADD=$2100 IRQ=$E BD=115200 PAR=E STO=1 LRB=500') <> res_Ok then RunError(10);
 {$else}
  if InitCrtCom('NAM=CRT MAS=SLAVE LSB=500 '+
                'NAM=COM COM=2 IRQ=3 BD=115200 PAR=E STO=1 LRB=500') <> res_Ok then RunError(10);
 {$endif}

 {inicializace o.s.Retos}
  StartMain(200{SPrio},254{DPrio});
  InitInterruptStack(1,254);
  StartTimeSlicing(8);

 {inicializace vypisoveho prostredi}
  TextMode(CO80+Font8x8);{textovy rezim 16 barev 80x50 znaku}
  CrtInit;               {inicializace CrtWin}

  MainWin^.LockWin;
  MainWin^.HiddenCursor;
  TextAttr:=White;
  ClrScr;
  write('CrtCom Example 2, ver "',cAppVer,'"');
  MainWin^.UnLockWin;

 {spusteni procesu 1 az 3}
  Proces1;
  Proces2;
  Proces3;

 {telo hlavniho procesu}
  repeat
    MainWin^.LockWin;
    gotoxy(65,1);write('Clock: ',TimeToStrPT(GetPackTime,'HH:MM:SS'));
    MainWin^.UnLockWin;

   {krok komunikacniho automatu}
    CrtComMake;

    Wait(1);
  until FlEnd;

  TerminateMain;
 {$ifdef MCP}{reset na KitXxx}
  asm cli end;
  repeat until False;
 {$endif}
End.
