Program TestJoystick; { TURBO Pascal program containing stick procedure }
                      { and demonstration program. For use on IBM PC,   }
{$c-}                 { PC-Jr etc.                                      }
                      { If you have a monochrome monitor change         }
                      { 'graphcolormode' to 'graphmode' (2 occurances)  }
const
  xymin = 3;        { These are the minimium and maximum values returned }
  xymax = 106;      { from procedure stick. The numbers depend on the    }
                    { joystick and the computer you are using.           }
var
  ix,iy,b1,b2:       integer;
  xx,yy,xx1,yy1:     integer;
  x1,x2,y1,y2:       integer;
  adjx,adjy:         real;

procedure stick(var x,y,k1,k2: integer);   {procedure to get x and y}
   begin                                   {coordinates of joystick #1}
      inline                               {and to test joystick keys}
        ($ba/$01/$02/   { MOV   DX,0201H } {1=pressed  0=not pressed}
         $ec/           { IN    AL,DX    }
         $a8/$03/       { TEST  AL,03H   }  { 0CH for joystick #2 }
         $75/$fb/       { JNZ   $-3      }
         $b9/$2c/$01/   { MOV   CX,300   }
         $fa/           { CLI            }
         $ee/           { OUT   DX,AL    }
         $ec/           { IN    AL,DX    }
         $a8/$01/       { TEST  AL,01H   }  { 04H for joystick #2 }
         $e0/$fb/       { LOOPNZ $-3     }
         $bb/$2c/$01/   { MOV   BX,300   }
         $2b/$d9/       { SUB   BX,CX    }
         $c4/$be/x/     { LES   DI,SS:x[BP] }
         $26/$89/$1d/   { MOV   ES:[DI],BX  }
         $2b/$db/       { SUB   BX,BX    }
         $a8/$10/       { TEST  AL,10H   }  { 40H for joystick #2 }
         $75/$03/       { JNZ   $+5      }
         $83/$c3/$01/   { ADD   BX,1     }
         $c4/$be/k1/    { LES   DI,SS:k1[BP] }
         $26/$89/$1d/   { MOV   ES:[DI],BX   }
         $2b/$db/       { SUB   BX,BX    }
         $a8/$20/       { TEST  AL,20H   }  { 80H for joystick #2 }
         $75/$03/       { JNZ   $+5      }
         $83/$c3/$01/   { ADD   BX,1     }
         $c4/$be/k2/    { LES   DI,SS:k2[BP] }
         $26/$89/$1d/   { MOV   ES:[DI],BX   }
         $ec/           { IN    AL,DX    }
         $a8/$03/       { TEST  AL,03H   }  { 0CH for joystick #2 }
         $75/$fb/       { JNZ   $-3      }
         $b9/$2c/$01/   { MOV   CX,300   }
         $ee/           { OUT   DX,AL    }
         $ec/           { IN    AL,DX    }
         $a8/$02/       { TEST  AL,02H   }  { 08H for joystick #2 }
         $e0/$fb/       { LOOPNZ $-3     }
         $fb/           { STI            }
         $bb/$2c/$01/   { MOV   BX,300   }
         $2b/$d9/       { SUB   BX,CX    }
         $c4/$be/y/     { LES   DI,SS:y[BP] }
         $26/$89/$1d);  { MOV   ES:[DI],BX  }
   end;


begin                             {sample program to demonstrate}
  adjx:=319/(xymax-xymin);        {use of stick procedure}
  adjy:=199/(xymax-xymin);        {press key #1 to clear screen}
  graphcolormode;                 {press key #2 to terminate program}
  stick(xx1,yy1,b1,b2);
  repeat
    stick(xx,yy,b1,b2);
    if (xx>xx1+1) or (xx<xx1-1) or     { filter out very small }
    (yy>yy1+1) or (yy<yy1-1) then      { movements of stick }
    begin
{     gotoxy(1,9);             }       { use these 4     }
{     writeln('        ');     }       { statements to   }
{     gotoxy(1,9);             }       { determine xymin }
{     writeln(xx,' ',yy);      }       { and xymax       }
      x1:=trunc((xx1-xymin)*adjx);
      y1:=trunc((yy1-xymin)*adjy);
      if y1>199 then y1:=199;
      if x1>319 then x1:=319;
      x2:=trunc((xx-xymin)*adjx);
      y2:=trunc((yy-xymin)*adjy);
      if y2>199 then y2:=199;
      if x2>319 then x2:=319;
      draw(x1,y1,x2,y2,1);
      plot(x2,y2,2);
      xx1:=xx;
      yy1:=yy;
    end;

    if b1<>0 then graphcolormode;     { clear screen }
  until b2<>0;                        { return       }
end.
                         