• Aucun résultat trouvé

Figure 2. Keyboard Location

Location 1 Location 2

0 22

0. .J 0 K. .1 1..J 0 K. .0

M M

22 0

159

Christopher J.FIynn

77ns discussion explains how the VIC reads the joystick port. Also in cluded is a game called "Doodle."

What is the most inexpensive peripheral that you can buy for your VIC? A color television? Certainly not. Memory expansion?

Probably not. No, a joystick. What? You mean one of those gadgets for playing games? That's right!

Perhaps you didn't realize it, but your VIC can use the very same joysticks that are found on the Atari and Sears video games.

Absolutely no hardware modifications are needed at all.

To give you an idea of the capabilities of the joystick, we've in cluded a demonstration program called "Doodle." It's a fast-paced game in full sound and color designed for drawing patterns with

the joystick. Your kids will love it — if they can get it away from

you.

Before we get into the details, an acknowledgment is due.

Creative Software of California deserves credit for pointing out to me that Atari joysticks are usable on the VIC.

How We Do It

The figure compares the VIC joystick socket with the Atari's. The similarities are striking.

We need to do a little exploratory surgery first. Since I've already done this, please just follow my description. You don't need to do this to your VIC. First we gently open up VICs case.

Armed with our trusty ohmmeter, we trace the joystick connec tions. We assume that they must reach the 6522 VIA I/O chips. So that's where we start looking. VoilS! Tracing all the connections, we find that the joystick switches do indeed go to the 6522s.

160

Finally, we determine that the joystick is connected as follows:

6522#1 E???????

PB7 PB6 PB5 PB4 PB3 PB2 PB1 PBO

6522#2 ??FWSN??

PA7 PA6 PA5 PA4 PA3 PA2 PA1 PAD E, W, S, and N represent the four compass directions. F repre sents the fire button. We won't be concerned with the fire button in this article.

How do we use this information in a program? What we gen erally have to do is read each I/O port and test the appropriate bits. Then our program can take any action needed. And there are some complications. Don't forget that the 6522 has data direction registers which program each bit for an input or output opera tion. Also, the signals from the joystick are in what is called an

"active low" state. That is, if the joystick is pointing, say, north, the north bit will be low or zero. The other three directions will be high or ones.

That probably sounds a lot harder than it is. We can actually use BASIC to obtain the joystick readings pretty easily. The BASIC statements shown here are the key to using joysticks on the VIC.

POKE 37154,127

VI =PEEK(37152) AND 128 V2 =PEEK(37151) AND 28 POKE37154>255

JS=Vl/16+V2/4 JS=(NOTJS)AND15

These statements read the I/O ports and manipulate the bits. We end up with a bit configuration like this:

OOOOEWSN

The least significant four bits in the variable JS thus corre spond to the four joystick switches. Normally, this would mean that JS could range in value from 0 to 15. In practice, JS will take on values from 0 to 10. This is because some bit patterns just aren't possible. With a properly functioning joystick, you can't press the north and south switches at the same time, for example.

The following table shows the values that JS will assume for each of the valid joystick positions.

161

Direction

Note that JS is 0 in the neutral position. This gives us a handy way to test for joystick movement.

Delta X and Delta Y are variables which will help us if we're trying to move an object around the screen. Suppose we're using an X and Y coordinate system like this:

X

Y represents a row number, and X represents a position with in a row. When the joystick moves, we want to update the values of X and Y so they indicate the new position. We can do this again easily in BASIC:

X=X+DX(JS) Y=Y+DY(JS)

DX and DY are arrays where we've saved the list of values for Delta X and Delta Y.

An example will show how this works. Let us assume that we have an object at X =7 and Y =5. We test the joystick and deter mine that it has moved. Let's assume that it's pointing north.

From our table, we know that JS will contain 1. So, the new posi tions of X and Y will be:

162

X=7+DX(1) Y =5 +DY(1) or X=7

Y=4

Thus, our object is moved up one line closer to the top of the screen. There was no left or right horizontal change.

One last detail we need to think about is how to convert X and Y into something VIC understands. As you know, we can POKE things into VIC's screen memory. But we need a memory location for that. Again, BASIC helps us out:

P=22*Y+X

That little formula will convert valid X and Y values into a number ranging from 0 to 505. Next, we must add P to the screen and color memory starting locations:

POKE 7680 +P, code POKE 38400 +P, color

Use any screen code and color that you wish.

Doodling

We've covered joysticks pretty quickly; we've only discussed the highlights. There are many other details involved. The best way to pick these up is to study Program 1 and to enjoy the Doodle

game.

Doodle is a lot of fun to play. The object is just to enjoy your self. When you start Doodle, it will display instructions on how to use the special function keys.

Description Ends the game.

The cursor moves, but does not draw a line. Erases any objects that it crosses.

The cursor moves and draws a line.

The screen is cleared and the cursor is centered. VIC is ready to doodle again.

You may press any key at any time while doodling. For inter esting effects, alternate the f3 and f5 keys. By doing this properly, you can enclose a figure within another figure without any inter secting lines.

Comparison of VIC and Atari Joystick Sockets (as viewed from the outside)

VIC Joystick Socket

1JOY0 2JOY1 3JOY2 4 JOY 3 5POTY

6 LIGHT PEN 7+5V 8 GROUND 9POTX