
The following is a compact machine code routine that provides a 2068 user with digital joystick position information and button status at machine code speeds. It was originally designed to be of use in upgrading game programs from keyboard control to joystick control.
Basically, the two joysticks are controlled via Register 14 of the Programmable Sound Generator Chip. Addresses are transfered by port #245 and data by port #246. To read a joystick, 14 is written to port #245, the B register is loaded with stick no., and the data is read from port #246 as a single byte with direction/button status bits located as shown in fig. 1.
FIG. 1 – JOYSTICK DATA BYTE
Bit 7 6 5 4 3 2 1 0
| not used | \ \ \
button stick stick stick stick
depressed right left down up
FIG. 2 – JOYSTICK DATA LOCATIONS
ADDRESS DATA 26715 STICK RIGHT 26716 STICK LEFT 26717 STICK DOWN 26718 STICK UP 26719 "FIRE" BUTTON
The routine, as presented in this article, resides in a 1 REM statement, although it can easily be relocated.
Providing the second display file is not used, the first available address in a 1 REM statement is 26715. This and the next four addresses are where the results of reading a joystick will be stored (see fig. 2). After the routine is called, a 1 will be found in the addresses corresponding to positive switch closures and 0’s in those addresses corresponding to negative or “open” switch conditions.
The program in fig. 3 is a machine code loader which puts the machine code into the 1 REM statement.
As the resulting 1 REM machine code lint is partially unlistable, any subsequent program lines will be unlistable unless…the [>] cursor is set to a line no. equal to or larger than the next line no. after the 1 REM and the following two direct commands are entered: POKE 23660, (your next program line above the 1 REM line): POKE 23661,0. This will cause the automatic listing to start at your first program line.
Enter and RUN the following machine code loader:
FIG. 3 – MACHINE CODE LOADER
1 REM xxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxx
10 FOR n=26715 TO 26773
20 READ d: POKE n,d
30 NEXT n
40 DATA 0,0,0,0,0,33,91,104,1,
0,5,113,35,16,252,62,14,211,245,
1,246,2,237,120,43,203,127,32,2,
54,1,43,203,71,32,2,54,1,43,203,
79,32,2,54,1,43,203,87,32,2,54,1
,43,203,95,192,54,1,201
Lines 10-40 of the loader can be DELETED and the remaining 1 REM machine code line can be SAVED to be later MERGED with any program that is being up-graded to joystick control.
At the point in a program where joystick information is needed, address #26736 is POKED with the desired joystick’s no. (1-LEFT; 2-RIGHT) and the routine is called at 26720.
By PEEKING addresses 26715-26719, the sticks’ position and button status are obtained.
The following program is a typical example of how the routine may be used to move a single character around the screen with a joystick. The program will stop when the “fire” button is pressed.
FIG. 4 – EXAMPLE PROGRAM
1 REM ?????![h???q#
(Handwritten note beside line 1: “the remainder is unlistable”)
10>LET x=26715: LET vert=10: L
ET horiz=15
20 PRINT AT vert,horiz;" "
30 RANDOMIZE USR 26720: REM **
the right stick is selected**
40 LET verticle=vert+PEEK (x+2
)-PEEK (x+3)
50 LET horizontal=horiz+PEEK x
-PEEK (x+1)
60 IF verticle<0 OR verticle>2
1 THEN GO TO 30
70 IF horizontal<0 OR horizont
al>31 THEN GO TO 30
80 PRINT AT verticle,horizonta
l;"#"
90 LET vert=verticle: LET hori
z=horizontal
100 IF PEEK (x+4)=1 THEN STOP
110 GO TO 20
Fig. 5 is the OP CODE listing for the 1 REM routine.
FIG. 5 – OP CODE LISTING
26715 00 NOP
26716 00 NOP
26717 00 NOP
26718 00 NOP
26719 00 NOP
26720 215B68 LD HL,26715
26723 010005 LD BC,1280
26726 71 LD (HL),C
26727 23 INC HL
26728 10FC DJNZ 26726
26730 3E0E LD A,14
26732 D3F5 OUT (245),A
26734 01F602 LD BC,758
26737 ED78 IN A,(C)
26739 2B DEC HL
26740 CB7F BIT 7,A
26742 2002 JR NZ,26746
26744 3601 LD (HL),1
26746 2B DEC HL
26747 CB47 BIT 0,A
26749 2002 JR NZ,26753
26751 3601 LD (HL),1
26753 2B DEC HL
26754 CB4F BIT 1,A
26756 2002 JR NZ,26760
26758 3601 LD (HL),1
26760 2B DEC HL
26761 CB57 BIT 2,A
26763 2002 JR NZ,26767
26765 3601 LD (HL),1
26767 2B DEC HL
26768 CB5F BIT 3,A
26770 C0 RET NZ
26771 3601 LD (HL),1
26773 C9 RET
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.