type workout_bol=array[1..8]of boolean;

procedure cursor_off;
var r:registers;
begin
 r.ax:=$0100;
 r.cx:=$2607;
 intr(16,r);
end;

procedure cursor_on;
var r:registers;
begin
 r.ax:=$0100;
 r.cx:=$0506;
 intr(16,r);
end;

function readscan:word;
var r:registers;
begin
 fillchar(r,sizeof(r),0);
 r.ah:=0;
 intr($16,r);
 readscan:=r.ax;
end;

function readascii:byte;
var r:registers;
begin
 fillchar(r,sizeof(r),0);
 r.ah:=0;
 intr($16,r);
 readascii:=r.al;
end;

function keypressed:boolean;
var b:byte;
begin
 asm
  mov ah,1
  int $16
  jz @not
  mov b,1
  jmp @ok
@not:
  mov b,0
@ok:
 end;
 if b=0 then keypressed:=false else keypressed:=true;
end;

function readkey:char;
var r:registers;
begin
 fillchar(r,sizeof(r),0);
 r.ah:=0;
 intr($16,r);
 readkey:=chr(r.al);
end;

procedure workout(s:byte;var b:workout_bol);
var count,pos:word;
begin
 for count:=1 to 8 do b[count]:=false;
 count:=128;
 pos:=8;
 while (s<>0)or(pos>1) do
  if s>=count then
   begin
    s:=s-count;
    count:=count div 2;
    b[pos]:=true;
    dec(pos);
   end
   else
    begin
     dec(pos);
     count:=count div 2;
    end;
end;

function key(k:byte):boolean;
var a,b:byte;
    bool:workout_bol;
begin
 asm
  mov ah,12h
  int $16
  mov b,ah
  mov a,al
 end;
 if(b>0)or(a>0)then
  begin
   workout(b,bool);
   case k of
    1:key:=bool[1];
    2:key:=bool[2];
    3:key:=bool[3];
    4:key:=bool[4];
    5:key:=bool[5];
    6:key:=bool[6];
    7:key:=bool[7];
    8:key:=bool[8];
   end;
   workout(a,bool);
   case k of
    9:key:=bool[1];
    10:key:=bool[2];
    11:key:=bool[5];
    12:key:=bool[6];
    13:key:=bool[7];
    14:key:=bool[8];
   end;
  end;
end;