home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / JSTICK.ZIP / JOYSTICK.PAS
Encoding:
Pascal/Delphi Source File  |  1985-10-02  |  2.5 KB  |  83 lines

  1. program JoyStick;
  2.  
  3. var
  4.    I    : integer;
  5.    Temp : byte;
  6.  
  7.  
  8. function Button_Pressed(Which_One : char):boolean;
  9. { Returns 'true' if specified button is pressed }
  10. const
  11.    JoyPort = $201;
  12. var
  13.    Mask : byte;
  14. begin
  15.    if not(Which_One in ['A'..'D']) then Which_One := 'A';
  16.    case Which_One of
  17.       'A' : Mask := 16;
  18.       'B' : Mask := 32;
  19.       'C' : Mask := 64;
  20.       'D' : Mask := 128;
  21.    end;
  22.    Button_Pressed := (port[JoyPort] and Mask) = 0;
  23. end;
  24.  
  25.  
  26. function JoyStick_Pos(Which_One : Char):integer;
  27. const
  28.    MaxCount = 400;
  29.    JoyPort  = $201;
  30. var
  31.    Counter : integer;
  32.    Mask    : byte;
  33. begin
  34.    if not(Which_One in ['A'..'D']) then Which_One := 'A';
  35.    case Which_One of
  36.       'A' : Mask := 1;
  37.       'B' : Mask := 2;
  38.       'C' : Mask := 4;
  39.       'D' : Mask := 8;
  40.    end;
  41.  
  42. { This assembly code causes the CX register to count down from MaxCount toward
  43.   zero.  When CX reaches zero or when the one-shot on the game adapter times
  44.   out the looping stops and Counter is assigned the number of counts that were
  45.   accumulated.  MaxCount should be chosen so that CX never reaches 0 so that
  46.   the usable range of the joystick will not be limited. }
  47.  
  48.    inline(
  49.       $B9/MaxCount/     {       MOV  CX,MaxCount   Initialize down counter   }
  50.       $BA/JoyPort/      {       MOV  DX,JoyPort    Port address of joysticks }
  51.       $8A/$A6/Mask/     {       MOV  AH,Mask[BP]   Mask for desired one-shot }
  52.       $EE/              {       OUT  DX,AL         Start the one-shots       }
  53.       $EC/              { READ: IN   AL,DX         Read the one-shots        }
  54.       $84/$C4/          {       TEST AL,AH         Check desired one-shot    }
  55.       $E0/$FB/          {       LOOPNZ READ        Repeat until timed out    }
  56.       $89/$8E/Counter); {       MOV  Counter[BP],CX  This makes CX available }
  57.                         {                            to Turbo Pascal         }
  58.    if Counter = 0 then
  59.       JoyStick_Pos := 0                { Exceeded useful range or not in use }
  60.    else
  61.       JoyStick_Pos := MaxCount - Counter;
  62. end;
  63.  
  64.  
  65. begin  { Mainline }
  66.    clrscr;
  67.    gotoxy(1,2);
  68.    writeln('Joysticks':12,'Buttons':12);
  69.    write('A':6,'B':6,'A':6,'B':6);
  70.    repeat
  71.       gotoxy(1,5);
  72.       write(JoyStick_Pos('A'):6,JoyStick_Pos('B'):6);
  73.       if Button_Pressed('A') then
  74.          write('PRES':6)
  75.       else
  76.          write('UP':6);
  77.       if Button_Pressed('B') then
  78.          write('PRES':6)
  79.       else
  80.          write('UP':6);
  81.    until keypressed;
  82. end.
  83.