SWAGOLX.EXE (c) 1993 GDSOFT ALL RIGHTS RESERVED 00005 JOYSTICK ROUTINES 1 05-28-9313:49ALL SWAG SUPPORT TEAM JOYSTCK1.PAS IMPORT 22 Unit Joystick;πInterfaceπ Uses Crt;ππ { Joystick Interface For Turbo Pascal V. 4.0 and aboveπ Public Domain, November 1989 byπ JonSoft Technologies Inc.π (C) 1989 JonSoft Technologies Inc. }ππConstπ centX : Byte=80;π centY : Byte=40;π Joyst : Boolean=True;ππProcedure FastInitJS;πProcedure BetterInitJS( range : Byte );πFunction joy_X : Byte;πFunction joy_Y : Byte;πFunction button_1 : Byte;πFunction button_2 : Byte;πFunction Horiz : shortint;πFunction Vert : shortint;πππImplementationππConstπ rangexm : Byte=25;π rangeym : Byte=20;π rangexp : Byte=25;π rangeyp : Byte=25;ππFunction joy_X : Byte;π Varπ x : Word;π beginπ x := 0;π Port[$201] := $ff;π While Port[$201] and $1=1 do Inc(x);π joy_X := x;π end;ππFunction joy_Y : Byte;π Varπ y : Word;π beginπ y := 0;π Port[$201] := $0;π While Port[$201] and $2=2 do Inc(y);π joy_Y := y;π end;ππProcedure FastInitJs;π beginπ centX := joy_X;π centY := joy_Y;π end;ππFunction button_1 : Byte;π beginπ button_1 := ((Port[$201] and $10) Xor $10) ShR 4;π end;ππFunction button_2 : Byte;π beginπ button_2 := ((Port[$201] and $20) Xor $20) ShR 5;π end;ππProcedure BetterInitJs(range : Byte);π Varπ(* Ch : Char; *)π uprjoyX, uprjoyY, centrjoyX, centrjoyY, lowrjoyX, lowrjoyY : Byte;ππbeginπ WriteLN('Are you using a joystick? (Button = yes, RETURN = no)');π Repeatπ if button_1+button_2 > 0 then Joyst := True;π if KeyPressed then Joyst := False;π Until (button_1+button_2 > 0) or KeyPressed;π if Joyst = True then beginπ Repeat Until button_1+button_2 = 0;π WriteLN('Move joystick to UPPER RIGHT corner and press a button.');π Repeat Until button_1+button_2 > 0;π uprjoyX := joy_X;π uprjoyY := joy_Y;π Repeat Until button_1+button_2 = 0;π WriteLN('Move joystick to CENTER and press a button.');π Repeat Until button_1+button_2 > 0;π centrjoyX := joy_X;π centrjoyY := joy_Y;π centX := centrjoyX;π centY := centrjoyY;π Repeat Until button_1+button_2 = 0;π WriteLN('Move joystick to LOWER LEFT CorNER and press a button.');π Repeat Until button_1+button_2 > 0;π lowrjoyX := joy_X;π lowrjoyY := joy_Y;π rangexm := (centrjoyX-uprjoyX) div range;π rangexp := (lowrjoyX-centrjoyX) div range;π rangeym := (centrjoyY-uprjoyY) div range;π rangeyp := (lowrjoyY-centrjoyY) div range;π end;πend;ππFunction Horiz : shortint;π beginπ if joy_X<centX-rangexm then Horiz := -1π else if joy_X > centX+rangexp then Horiz := 1π else Horiz := 0;π end;ππFunction Vert : shortint;π beginπ if joy_Y<centY-rangeym then Vert := -1π else if joy_Y > centY+rangeyp then Vert := 1π else Vert := 0;π end;ππend.π 2 05-28-9313:49ALL SWAG SUPPORT TEAM JOYSTCK2.PAS IMPORT 30 {πAnyone know how to read the Joystick.... I only need Joy(1) read....πI have used 1 Procedure i d/led, but all it did was tell me if the buttonsπwere down (it didnt work in telling me which direction (it should of))π}πProgram JOYSTICK;ππUses Crt, Dos;ππ(*πWRITTEN BY JAMES P. MCADAMS - 25 DECEMBER 1984ππProgram DEMONSTRATinG THE USE of TURBO PASCAL to ACCESS THEπIBM-PC GAME CONTROL ADAPTER. THE TWO Function CALLS ARE EACHπCompLETE in ITSELF. EITHER ONE or BOTH CAN BE MOVED to ANYπProgram THAT NEEDS THE USE of JOYSTICKS or PADDLES.π*)ππππVarπI: Integer;πTEMP: Byte;πππ Function BUTtoN_PRESSED (WHICH_ONE: Char): Boolean;π (* RETURN True if THE BUTtoN IS PRESSED *)π Constπ JOYPorT = $201; (* LOCATION of THE GAME PorT *)π Varπ MASK: Byte;π beginπ if not (WHICH_ONE in ['A'..'D']) then WHICH_ONE := 'A';π Case WHICH_ONE ofπ 'A': MASK := 16;π 'B': MASK := 32;π 'C': MASK := 64;π 'D': MASK := 128;π end;π BUTtoN_PRESSED := (PorT [JOYPorT] and MASK) = 0;π end; (* BUTtoN_PRESSED *)πππ Function JOYSTICK_POS (WHICH_ONE: Char): Integer;π (*π With A KRAFT JOYSTICK, VALUES RETURNED ARE in THE RANGE 4 to ABOUTπ 140. if YOUR MACHinE RUNS FASTER THAN A STandARD IBM-PC or if YOUπ MODifY YOUR GAME ADAPTER CARD With BIGGER CAPACItoRS, YOU WILLπ GET LARGER COUNTS and YOU MUST MODifY "MAXCOUNT".ππ CALLinG A JOYSTICK THAT IS not in USE or ONE THAT HAS GONEπ OVER-RANGE (COUNT REACHED MAXCOUNT) YIELDS A VALUE of 0.π *)π Constπ MAXCOUNT = 2000; (* MODifY THIS if YOU CAN GET LONGER COUNTS *)π JOYPorT = $201; (* For inForMATION ONLY: LOC of GAME inPUT PorT *)π Varπ COUNTER: Integer;π MASK: Byte;π beginπ if not (WHICH_ONE in ['A'..'D']) then WHICH_ONE := 'A';π Case WHICH_ONE ofπ 'A': MASK := 1;π 'B': MASK := 2;π 'C': MASK := 4;π 'D': MASK := 8;π end;π (*π THIS ASSEMBLY CODE CAUses THE CX REGISTER to COUNT doWN FROM "MAXCOUNT"π toWARD ZERO. WHEN CX REACHES ZERO or WHEN THE ONE-SHOT ON THE GAMEπ ADAPTER TIMES OUT, THE LOOPinG StoPS and "COUNTER" IS ASSIGNED THE NUMBERπ of COUNTS THAT toOK PLACE. MAXCOUNT SHOULD BE CHOSEN SO THAT CX NEVERπ REACHES 0 SO THAT THE USABLE RANGE of THE JOYSTICK WILL not BE LIMITED.π *)π Inline (π $B9/MAXCOUNT/ (* MOV CX,MAXCOUNT inITIALIZE doWN-COUNTER *)π $BA/JOYPorT/ (* MOV DX,JOYPorT PorT ADDR of JOYSTICKS *)π $8A/$A6/MASK/ (* MOV AH,MASK[BP] MASK For DESIRED 1-SHOT *)π $EE/ (* OUT DX,AL START THE ONE-SHOTS *)π $EC/ (* READ: in AL,DX READ THE ONESHOTS *)π $84/$C4/ (* TEST AL,AH CHECK DESIRED ONE-SHOT *)π $E0/$FB/ (* LOOPNZ READ Repeat Until TIMED OUT *)π $89/$8E/COUNTER); (* MOV COUNTER[BP],CX THIS MAKES CX AVAIL-*)π (* ABLE to TURBO *)π if COUNTER = 0π then JOYSTICK_POS := 0 (* OVER-RANGE or not in USE *)π else JOYSTICK_POS := MAXCOUNT - COUNTER;π end; (* JOYSTICK_POS *)πππbegin (***** DEMO Program - MAin CODE *****)πClrScr;πGotoXY (1, 2);πWriteLN ('JOYSTICKS':10, 'BUTtoNS':10);πWrite ('A':5, 'B':5, 'A':5, 'B':5);ππWhile True do (* PRESS CTRL C to StoP THE Program *)π beginπ GotoXY (1, 5);π Write (JOYSTICK_POS ('A'):5, JOYSTICK_POS ('B'):5);π if BUTtoN_PRESSED ('A')π then Write ('PRES':5)π else Write ('UP':5);π if BUTtoN_PRESSED ('B')π then Write ('PRES':5)π else Write ('UP':5);π end;πend.π 3 05-28-9313:49ALL SWAG SUPPORT TEAM JOYSTCK3.PAS IMPORT 42 {πto whomever sent me a message concerning joystick support, I apologize that Iπcannot send this message to you directly (message Pointers were screwed up on mπend, and I lost your message), but here is both my source For a Unit and aπsample Program. First I'd like to say that my Unit may be somewhat inComplete.πhave only a Single joystick port, so reading of two ports is impossible. Forπthis reason, I'd like to ask any and all to make suggestions, and modificationsπso that I, and all Programmers, may have a Complete Unit. Also, remarks haveπnot been added to the Program, if an explanation is needed, please feel free toπask...I'd be more than happy to give explanations For my work. Anyhows, here iπis...π}πUnit Joystick;ππInterfaceππFunction JoystickExists : Boolean;πFunction JoystickPosX : Integer;πFunction JoystickPosY : Integer;πFunction JoystickButtonA : Boolean;πFunction JoystickButtonB : Boolean;ππImplementationππUses Crt, Dos;ππConst GamePortAddr = $200;π MaxCount = 500;ππFunction JoystickStatus (Mask : Byte) : Integer;πVar Counter : Integer;πLabel Read;πbeginπ Asmπ mov cx,MaxCountπ mov dx,GamePortAddrπ mov ah,Maskπ out dx,alπ read:π in al,dxπ test al,ahπ loopnz readπ mov counter,cxπ end;π JoystickStatus := MaxCount - Counter;π Delay (2);πend;ππFunction JoystickPosX : Integer;πbeginπ JoystickPosX := JoystickStatus (1);πend;ππFunction JoystickPosY : Integer;πbeginπ JoystickPosY := JoystickStatus (2);πend;ππFunction JoystickButtonA : Boolean;πbeginπ JoystickButtonA := (Port [GamePortAddr] and 16) = 0;πend;ππFunction JoystickButtonB : Boolean;πbeginπ JoystickButtonB := (Port [GamePortAddr] and 32) = 0;πend;ππFunction JoystickExists : Boolean;πVar Regs : Registers;πbeginπ JoystickExists := not ((JoystickPosX = 0) and (JoystickPosY = 0));πend;ππend.πππProgram JoyTest;ππUses Crt, Dos, AniVGA, Joystick;ππVar XMin, XMax, YMin, YMax,π XRange, YRange,π X, Y,π PosX, PosY,π Bullet1X, Bullet1Y,π Bullet2X, Bullet2Y : Integer;π Shooting1, Shooting2 : Boolean;π ShootNext : Boolean;ππProcedure CalibrateJoystick (Var XMin, XMax, YMin, YMax : Integer);πbeginπ Write ('Press joystick to upper left corner and press button one...');π Repeat Until JoystickButtonA;π XMin := JoystickPosX;π YMin := JoystickPosY;π Writeln ('OK.');π Repeat Until not JoystickButtonA;π Write ('Press joystick to lower right corner and press button two...');π Repeat Until JoystickButtonB;π XMax := JoystickPosX;π YMax := JoystickPosY;π Writeln ('OK.');π Repeat Until not JoystickButtonB;πend;ππProcedure AnimateShip;πbeginπ X := JoystickPosX - XMin;π if (X <= XRange div 3) thenπ Dec (PosX, 3)π else if (X > XRange * 2 div 3) thenπ Inc (PosX, 3);π Y := JoystickPosY - YMin;π if (Y <= YRange div 3) thenπ Dec (PosY, 3)π else if (Y > YRange * 2 div 3) thenπ Inc (PosY, 3);π SpriteX [0] := PosX;π SpriteY [0] := PosY;πend;ππProcedure AnimateBullets;πbeginπ if Shooting1 thenπ if (Bullet1Y < 0) thenπ Shooting1 := Falseπ elseπ Dec (Bullet1Y, 8)π elseπ beginπ Bullet1X := PosX + 3;π Bullet1Y := PosY + 14;π end;π if Shooting2 thenπ if (Bullet2Y < 0) thenπ Shooting2 := Falseπ elseπ Dec (Bullet2Y, 8)π elseπ beginπ Bullet2X := PosX + 30;π Bullet2Y := PosY + 14;π end;π SpriteX [1] := Bullet1X;π SpriteY [1] := Bullet1Y;π SpriteX [2] := Bullet2X;π SpriteY [2] := Bullet2Y;πend;ππbeginπ if JoystickExists and (LoadSprite ('SHIP1.COD', 1) = 1) andπ (LoadSprite ('BULLET.COD', 2) = 1) thenπ beginπ ClrScr;π CalibrateJoystick (XMin, XMax, YMin, YMax);π ClrScr;π InitGraph;π SpriteN [0] := 1;π SpriteN [1] := 2;π SpriteN [2] := 2;π PosX := 160;π PosY := 160;π Shooting1 := False;π XRange := XMax - XMin;π YRange := YMax - YMin;π ShootNext := Boolean (0);π While not (JoystickButtonA and JoystickButtonB) doπ beginπ if JoystickButtonA and not JoystickButtonB thenπ if not Shooting1 and ShootNext thenπ beginπ Bullet1X := PosX + 3;π Bullet1Y := PosY + 14;π Shooting1 := True;π ShootNext := False;π endπ else if not Shooting2 and not ShootNext thenπ beginπ Bullet2X := PosX + 30;π Bullet2Y := PosY + 14;π Shooting2 := True;π ShootNext := True;π end;π While JoystickButtonA doπ beginπ AnimateShip;π AnimateBullets;π Animate;π end;π AnimateShip;π AnimateBullets;π Animate;π end;π CloseRoutines;π endπ elseπ Writeln ('Game card not installed.');πend.π{πI apologize For giving you an example that Uses another Unit. if need be, thisπProgram can be easily modified to provide a successful example. Hope thisπhelps, and I hope my Programming is not toO bad.π} 4 05-28-9313:49ALL SWAG SUPPORT TEAM JOYSTCK4.PAS IMPORT 5 πAnyone know how to read the Joystick....ππif you are using an AT (286 or later), here's the easy way.πUse Intr ($15, Regs), and load AH With the $84, then loadπDX With 1 to get the joystick status' and 0 to get theπbutton status. if you use DX=1, it returns:ππAX x of joystick AπBX y of joystick AπCX x of joystick BπDX y of joystick Bππif you use DX=0:ππAL button status, bit #π 4 joystick A,button 1π 5 joystick A,button 2π 6 joystick B,button 1π 7 joystick B,button 2π 5 05-28-9313:49ALL SWAG SUPPORT TEAM JOYSTCK5.PAS IMPORT 42 {πto whomever sent me a message concerning joystick support, I apologize that Iπcannot send this message to you directly (message Pointers were screwed up on mπend, and I lost your message), but here is both my source For a Unit and aπsample Program. First I'd like to say that my Unit may be somewhat inComplete.πhave only a Single joystick port, so reading of two ports is impossible. Forπthis reason, I'd like to ask any and all to make suggestions, and modificationsπso that I, and all Programmers, may have a Complete Unit. Also, remarks haveπnot been added to the Program, if an explanation is needed, please feel free toπask...I'd be more than happy to give explanations For my work. Anyhows, here iπis...π}ππUnit Joystick;ππInterfaceππFunction JoystickExists : Boolean;πFunction JoystickPosX : Integer;πFunction JoystickPosY : Integer;πFunction JoystickButtonA : Boolean;πFunction JoystickButtonB : Boolean;ππImplementationππUses Crt, Dos;ππConst GamePortAddr = $200;π MaxCount = 500;ππFunction JoystickStatus (Mask : Byte) : Integer;πVar Counter : Integer;πLabel Read;πbeginπ Asmπ mov cx,MaxCountπ mov dx,GamePortAddrπ mov ah,Maskπ out dx,alπ read:π in al,dxπ test al,ahπ loopnz readπ mov counter,cxπ end;π JoystickStatus := MaxCount - Counter;π Delay (2);πend;ππFunction JoystickPosX : Integer;πbeginπ JoystickPosX := JoystickStatus (1);πend;ππFunction JoystickPosY : Integer;πbeginπ JoystickPosY := JoystickStatus (2);πend;ππFunction JoystickButtonA : Boolean;πbeginπ JoystickButtonA := (Port [GamePortAddr] and 16) = 0;πend;ππFunction JoystickButtonB : Boolean;πbeginπ JoystickButtonB := (Port [GamePortAddr] and 32) = 0;πend;ππFunction JoystickExists : Boolean;πVar Regs : Registers;πbeginπ JoystickExists := not ((JoystickPosX = 0) and (JoystickPosY = 0));πend;ππend.πππ{ππProgram JoyTest;ππUses Crt, Dos, AniVGA, Joystick;ππVar XMin, XMax, YMin, YMax,π XRange, YRange,π X, Y,π PosX, PosY,π Bullet1X, Bullet1Y,π Bullet2X, Bullet2Y : Integer;π Shooting1, Shooting2 : Boolean;π ShootNext : Boolean;ππProcedure CalibrateJoystick (Var XMin, XMax, YMin, YMax : Integer);πbeginπ Write ('Press joystick to upper left corner and press button one...');π Repeat Until JoystickButtonA;π XMin := JoystickPosX;π YMin := JoystickPosY;π Writeln ('OK.');π Repeat Until not JoystickButtonA;π Write ('Press joystick to lower right corner and press button two...');π Repeat Until JoystickButtonB;π XMax := JoystickPosX;π YMax := JoystickPosY;π Writeln ('OK.');π Repeat Until not JoystickButtonB;πend;ππProcedure AnimateShip;πbeginπ X := JoystickPosX - XMin;π if (X <= XRange div 3) thenπ Dec (PosX, 3)π else if (X > XRange * 2 div 3) thenπ Inc (PosX, 3);π Y := JoystickPosY - YMin;π if (Y <= YRange div 3) thenπ Dec (PosY, 3)π else if (Y > YRange * 2 div 3) thenπ Inc (PosY, 3);π SpriteX [0] := PosX;π SpriteY [0] := PosY;πend;ππProcedure AnimateBullets;πbeginπ if Shooting1 thenπ if (Bullet1Y < 0) thenπ Shooting1 := Falseπ elseπ Dec (Bullet1Y, 8)π elseπ beginπ Bullet1X := PosX + 3;π Bullet1Y := PosY + 14;π end;π if Shooting2 thenπ if (Bullet2Y < 0) thenπ Shooting2 := Falseπ elseπ Dec (Bullet2Y, 8)π elseπ beginπ Bullet2X := PosX + 30;π Bullet2Y := PosY + 14;π end;π SpriteX [1] := Bullet1X;π SpriteY [1] := Bullet1Y;π SpriteX [2] := Bullet2X;π SpriteY [2] := Bullet2Y;πend;ππbeginπ if JoystickExists and (LoadSprite ('SHIP1.COD', 1) = 1) andπ (LoadSprite ('BULLET.COD', 2) = 1) thenπ beginπ ClrScr;π CalibrateJoystick (XMin, XMax, YMin, YMax);π ClrScr;π InitGraph;π SpriteN [0] := 1;π SpriteN [1] := 2;π SpriteN [2] := 2;π PosX := 160;π PosY := 160;π Shooting1 := False;π XRange := XMax - XMin;π YRange := YMax - YMin;π ShootNext := Boolean (0);π While not (JoystickButtonA and JoystickButtonB) doπ beginπ if JoystickButtonA and not JoystickButtonB thenπ if not Shooting1 and ShootNext thenπ beginπ Bullet1X := PosX + 3;π Bullet1Y := PosY + 14;π Shooting1 := True;π ShootNext := False;π endπ else if not Shooting2 and not ShootNext thenπ beginπ Bullet2X := PosX + 30;π Bullet2Y := PosY + 14;π Shooting2 := True;π ShootNext := True;π end;π While JoystickButtonA doπ beginπ AnimateShip;π AnimateBullets;π Animate;π end;π AnimateShip;π AnimateBullets;π Animate;π end;π CloseRoutines;π endπ elseπ Writeln ('Game card not installed.');πend.ππI apologize For giving you an example that Uses another Unit. if need be, thisπProgram can be easily modified to provide a successful example. Hope thisπhelps, and I hope my Programming is not toO bad.π}