home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / MADTRB20.ZIP / JOYSTICK.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1985-11-23  |  2.8 KB  |  83 lines

  1. Program Joystick;
  2. Var
  3.  I    : integer;
  4.  Temp : byte;
  5.  
  6. Function ButtonPressed(WhichOne: Char):Boolean; {True if button pressed}
  7. Const
  8.  Joyport = $201;     {Location of the game port}
  9. Var
  10.  Mask : Byte;
  11. Begin
  12.  If not (WhichOne in ['A'..'D']) then WhichOne := 'A';
  13.  Case WhichOne of
  14.   'A' : Mask := 16;
  15.   'B' : Mask := 32;
  16.   'C' : Mask := 64;
  17.   'D' : Mask := 128;
  18.  end;
  19.  ButtonPressed := (Port[Joyport] and Mask) = 0;
  20. End; {ButtonPressed}
  21.  
  22. Function JoystickPos(WhichOne : Char): integer;
  23.      {With a kraft joystick, values returned are in the range of 4 to
  24.       about 140.  If your machine runs faster than a standard IBM PC or
  25.       if you modify your game adapter card with bigger capacitors, you
  26.       will get larger counts and you must modify "MaxCount".  Calling
  27.       a joystick that is not in use or one that has gone over range (count
  28.       reached maxcount) yields a value of 0. }
  29. Const
  30.  Maxcount = 500;    {Modify this if you can get longer counts}
  31.  Joyport  = $201;   {Location of game input port}
  32. Var
  33.  Counter : integer;
  34.  Mask    : byte;
  35. Begin
  36.  If not (WhichOne in ['A'..'D']) then WhichOne := 'A';
  37.  Case WhichOne of
  38.   'A' : Mask := 1;
  39.   'B' : Mask := 2;
  40.   'C' : Mask := 4;
  41.   'D' : Mask := 8;
  42.  end;
  43.      {This assembly code causes the CX register to count down from MAXCOUNT
  44.       toward zero.  When CX reaches zero or when the one-shot on the game
  45.       adapter times out, the looping stops and counter is assigned the
  46.       number of counts that took place.  MAXCOUNT should be chosen so that CX
  47.       never reaches 0 so that the usable range of the joystick will not be
  48.       limited.  }
  49.  InLine(
  50.   $B9/MAXCOUNT/     {  MOV CX,MAXCOUNT     Initialize down counter}
  51.   $BA/JOYPORT/      {  MOV DX,JOYPORT      Port address of Joysticks}
  52.   $8A/$A6/MASK/     {  MOV AH,MASK[BP]     Mask for desired one-shot}
  53.   $EE/              {  OUT DX,AL           Start the one-shot}
  54.   $EC/              {Read:  IN AL,DX       Read the one-shot}
  55.   $84/$C4/          { TEST AL,AH           Check desired one-shot}
  56.   $E0/$FB/          { LOOPNZ READ          Repeat until timed out}
  57.   $89/$8E/COUNTER); {  MOV COUNTER[BP],CX  This makes CX available to turbo}
  58.  If Counter = 0 then
  59.   JoystickPos := 0        {Over range or not in use}
  60.  else
  61.   JoystickPos := Maxcount - Counter;
  62. End;  {JoystickPos}
  63.  
  64. Begin      {********* Demo Program Main Code *********}
  65.  ClrScr;
  66.  GotoXY(1,2);
  67.  Writeln('Joysticks':10, 'Buttons':10);
  68.  Write('A':5, 'B':5, 'A':5, 'B':5);
  69.  While True do   {Press CTRL-C to stop the program}
  70.   Begin
  71.    GotoXY(1,5);
  72.    Write(JoystickPos('A'):5, JoystickPos('B'):5);
  73.    If ButtonPressed('A') then
  74.     Write('Pres':5)
  75.    else
  76.     Write('Up':5);
  77.    If ButtonPressed('B') then
  78.     Write('Pres':5)
  79.    else
  80.     Write('Up':5);
  81.   End;
  82. END.
  83.