program TstKeyHandler;

{ define TEST}     { vypisy vsech BIOS promennych - pouze pro ladeni }
{$define Kit386EXR}
{ define KitV40}

uses
 Crt,
 NumToStr,
 PCKeyb,
{$ifdef Kit386EXR}
 ChnCom
{$endif}
{$ifdef KitV40}
 ChnV40
{$endif}
 ;

var
 aString:String;
 gNum:Integer;
 gbFlag:Boolean;
 gwKey:Word;
 lChar:Char;
 ptr:pChnVirt;

{$ifdef TEST}
var           { promenne jsou zkopirovany z PCKeyb unity }
 ubKbFlag      : byte absolute $0040:$0017;
 ubKbFlag1     : byte absolute $0040:$0018;
 ubKbAlt       : byte absolute $0040:$0019; {* akumulovana hodnota pro Alt+Keypad}
 uwKbBufH      : word absolute $0040:$001A; {* adresa pristiho byte       }
 uwKbBufT      : word absolute $0040:$001C; {* adresa posledniho platneho }
 uwKbBuf       : word absolute $0040:$001E; {* buffer klavesnice }
 uwKbBufEnd    : word absolute $0040:$003E; {* konec bufferu klavesnice }
 ubBiosBreak   : byte absolute $0040:$0071; {* priznak CtrlBreak }
 uwResetFlag   : word absolute $0040:$0072; {* RESET FLAG }
 uwBufferStart : word absolute $0040:$0080; {* offset pocatku bufferu }
 uwBufferEnd   : word absolute $0040:$0082; {* offset konce bufferu }
 ubKbFlag2     : byte absolute $0040:$0096; {* priznaky klavesnice }
 ubKbFlag3     : byte absolute $0040:$0097; {* priznaky LED klavesnice }

 uwBuf : array [0..32] of Byte absolute $0040:$001E; {* buffer klavwsnice }
{$endif}

begin
  TextMode(CO80+Font8x8);
  Writeln('Init ');

{$ifdef Kit386EXR}
  ptr:=New(pChnCom,ChInitParam('NAM=COM COM=2 IRQ=3 BD=4800 BIT=8 PAR=E STOP=1 LRB=50'));
  if ptr=nil then RunError(203);
  if InitPCKeyb(ptr,'RATE=10 DELAY=2 INT=9') then
{$endif}
{$ifdef KitV40}
  Writeln('Zapoj modul s klavesnici na X5(nutno prevodnik RS232->5V');
  Delay(4000);
  Writeln('Ocekavam pripojenou klavesnici -- zapnuti testu ');
  ptr:=New(pChnV40,ChInitParam('NAM=V40 BD=4800 BIT=8 PAR=E STOP=1 LRB=50'));
  if ptr=nil then RunError(203);
  if InitPCKeyb(ptr,'RATE=10 DELAY=2 INT=10') then
{$endif}
  begin
    Writeln('Reset keyboard -- prg');
    if not ResetKeyboard then
    begin
      writeln('Reset -- ERROR ');
      repeat until false;
    end;

{$ifdef TEST}
    gNum:=0;
    Writeln('First init ');
    writeln('******************************************');
    writeln('************** PC Hodnoty ****************');
    writeln('******************************************');
    writeln('PC: klavesa z bufferu := ',WordStrHex(gwKey));
    writeln('KbFlag   KbFlag1  KbAlt  BIOSbreak  KbFlag2  KbFlag3');
    writeln('  ',ByteStrHex(ubKbFlag),'       ',ByteStrHex(ubKbFlag1),
            '      ',ByteStrHex(ubKbAlt),'       ',ByteStrHex(ubBiosBreak),
            '        ',ByteStrHex(ubKbFlag2),'       ',ByteStrHex(ubKbFlag3));
    writeln('KbBufH  KbBufT  ResetFlag  BufferStart  BufferEnd');
    writeln(' ',WordStrHex(uwKbBufH),'    ',WordStrHex(uwKbBufT),
            '     ',WordStrHex(uwResetFlag),'       ',
            WordStrHex(uwBufferStart),'         ',
            WordStrHex(uwBufferEnd));
    Writeln('Spusteni smycky ');
    repeat
      gbFlag:=False;
      asm
        mov ah,11h { AT obsluha }

{         mov ah,01h { XT obsluha }
       int 16h
       jz @@Konec
       mov gbFlag,True
       mov gwKey,ax
    @@Konec:
      end;
      if gbFlag then
{         if KeyPressed then}
      begin
        Writeln('Nova klavesa :=  ',gNum);
        Inc(gNum);
        writeln('******************************************');
        writeln('************** PC Hodnoty ****************');
        writeln('******************************************');
        writeln('PC: klavesa z bufferu := ',WordStrHex(gwKey));
        writeln('KbFlag   KbFlag1  KbAlt  BIOSbreak  KbFlag2  KbFlag3');
        writeln('  ',ByteStrHex(ubKbFlag),'       ',ByteStrHex(ubKbFlag1),
                '      ',ByteStrHex(ubKbAlt),'       ',ByteStrHex(ubBiosBreak),
                '        ',ByteStrHex(ubKbFlag2),'       ',ByteStrHex(ubKbFlag3));
        writeln('KbBufH  KbBufT  ResetFlag  BufferStart  BufferEnd');
        writeln(' ',WordStrHex(uwKbBufH),'    ',WordStrHex(uwKbBufT),
                '     ',WordStrHex(uwResetFlag),'       ',
                WordStrHex(uwBufferStart),'         ',
                WordStrHex(uwBufferEnd));

        asm
         mov ah,10h { AT obsluha }

{          mov ah,00h { XT obsluha }
         int 16h
         mov lChar,al
         mov cx,ax
         mov ah,05h
         int 16h
        end;
        Writeln('Znak z bufferu :=  ',ByteStrHex(Ord(lChar)),
                '     znak :=  ',lChar);

        lChar:=Readkey;
        Writeln('Klavesa dle Pascalu :=  ',ByteStrHex(Ord(lChar)),
                '     znak :=  ',lChar);
        Writeln;
        Writeln;
      end;
    until false;
{$else}
    repeat
      Write('Zadej retezec :=   ');
      Readln(aString);
      Writeln('Precteny string := ',aString);
      writeln('Zmackni klavesu ');
      repeat
      until Keypressed;
      if Keypressed then
      begin
       Writeln('Zmacknuta klavesa je :=  ',Readkey);
      end;
    until false;
{$endif}
  end
 else
  Writeln('Init -- ERROR ');
 repeat until false;
end.
