CH24.PDF

(195 KB) Pobierz
1135156 UNPDF
The PC Game Adapter
Chapter 24
, the most popular computer available at the time the
PC was developed. Although IBM provided for twice the analog inputs of the Apple II, thinking that
would give them an edge, their decision to support only four switches and four potentiometers (or “pots”)
seems confining to game designers today – in much the same way that IBM’s decision to support 256K
RAM seems so limiting today. Nevertheless, game designers have managed to create some really marvel-
ous products, even living with the limitations of IBM’s 1981 design.
IBM’s analog input design, like Apple’s, was designed to be dirt cheap. Accuracy and performance
were not a concern at all. In fact, you can purchase the electronic parts to build your own version of the
game adapter, at retail, for under three dollars. Indeed, today you can purchase a game adapter card from
various discount merchants for under eight dollars. Unfortunately, IBM’s low-cost design in 1981 produces
some major performance problems for high-speed machines and high-performance game software in the
1990’s. However, there is no use crying over spilled milk – we’re stuck with the original game adapter
design, we need to make the most of it. The following sections will describe how to do exactly that.
1
24.1 Typical Game Devices
The game adapter is nothing more than a computer interface to various game input devices. The
game adapter card typically contains a DB15 connector into which you plug an external device. Typical
devices you can obtain for the game adapter include
paddles, joysticks, flight yokes, digital joysticks, rud-
Undoubtedly, this is but a short list of the types of devices
you can connect to the game adapter. Most of these devices are far more expensive that the game adapter
card itself. Indeed, certain high performance flight simulator consoles for the game adapter cost several
hundred dollars.
The digital joystick is probably the least complex device you can connect to the PC’s game port. This
device consists of four switches and a stick. Pushing the stick forward, left, right, or pulling it backward
closes one of the switches. The game adapter card provides four switch inputs, so you can sense which
direction (including the rest position) the user is pressing the digital joystick. Most digital joysticks also
allow you to sense the in-between positions by closing two contacts at once. for example, pushing the
control stick at a 45 degree angle between forward and right closes both the forward and right switches.
The application software can sense this and take appropriate action. The original allure of these devices is
that they were very cheap to manufacture (these were the original joysticks found on most home game
machines). However, as manufacturers increased production of analog joysticks, the price fell to the point
that digital joysticks failed to offer a substantial price difference. So today, you will rarely encounter such
devices in the hands of a typical user.
The game paddle is another device whose use has declined over the years. A game paddle is a single
pot in a case with a single knob (and, typically, a single push button). Apple used to ship a pair of game
paddles with every Apple II they sold. As a result, games that used game paddles were still quite popular
when IBM released the PC in 1981. Indeed, a couple manufacturers produced game paddles for the PC
when it was first introduced. However, once again the cost of manufacturing analog joysticks fell to the
point that paddles couldn’t compete. Although paddles are the appropriate input device for many games,
joysticks could do just about everything a game paddle could, and more. So the use of game paddles
quickly died out. There is one thing you can do with game paddles that you cannot do with joysticks – you
and
steering wheels.
1. In fact, the PC’s game adapter design was obviously stolen directly from the Apple II.
Page 1255
Thi d
t
t d ith F
M k 4 0 2
One need look no farther than the internals of several popular games on the PC to discover than
many programmers do not fully understand one of the least complex devices attached to the PC today –
the analog game adapter. This device allows a user to connect up to four resistive potentiometers and four
digital switch connections to the PC. The design of the PC’s game adapter was obviously influenced by the
analog input capabilities of the Apple II computer
der pedals, RC simulators,
1135156.004.png 1135156.005.png
Chapter 24
can place four of them on a system and produce a four player game. However, this (obviously) isn’t
important to most game designers who generally design their games for only one player.
A game paddle or set of rudder pedals
g ener ally pr ovi de a sing le nu mb er in the
r ang e z er o th ro ugh som e system dep end ent
maximum value.
0
Maximum
Reading
Game Paddle or Rudder Pedal Game Input Device
Rudder pedals are really nothing more than a specially designed game paddle designed so you can
activate them with your feet. Many flight simulator games take advantage of this input device to provide a
more realistic experience. Generally, you would use rudder pedals in addition to a joystick device.
A joystick contains two pots connected with a stick. Moving the joystick along the x-axis actuates one
of the pots, moving the joystick along the y-axis actuates the other pot. By reading both pots, you can
roughly determine the absolute position of the pot within its working range.
Y
A joystick uses two independent pots
to provide an (X,Y) input value. Hori-
zontal movements on the joystick af-
fect the x-axis pot independently of
the y-axis pot. Likewise, vertical move-
ments affect the y-axis pot independ-
ent of the x-axis pot. By reading both
pots you can determine the position
of the joystick in the (X,Y) coordinate
system.
X
Joystick Game Input Device
.
The steering wheel is connected to a pot that corresponds to the x-axis on the joystick. Pulling back (or
pushing forward) on the wheel activates a second pot that corresponds to the y-axis on the joystick.
Certain joystick devices, generically known as
2
, contain three pots. Two pots are con-
nected in a standard joystick fashion, the third is connected to a knob which many games use for the throt-
tle control. Other joysticks, like the Thrustmaster
flight sticks
or CH Products’ FlightStick Pro, include extra switches
including a special “cooley switch” that provide additional inputs to the game. The cooley switch is, essen-
tially, a digital pot mounted on the top of a joystick. Users can select one of four positions on the cooley
switch using their thumb. Most flight simulator programs compatible with such devices use the cooley
switch to select different views from the aircraft.
Ô
2. In fact, many such devices are switchable between the two.
Page 1256
An RC simulator is really nothing more than a box containing two joysticks. The yoke and steering
wheel devices are essentially the same device, sold specifically for flight simulators or automotive games
1135156.006.png
The Game Adapter
The cooley switch (shown here on a device layout similar
to the CH Products' FlightStick Pro) is a thumb actuated
digitial joystick. You can move the switch up, down, left
or right, activating individual switches inside the game
input device.
Cooley Switch (found on CH Products and Thrustmaster Joysticks)
24.2 The Game Adapter Hardware
The game adapter hardware is simplicity itself. There is a single input port and a single output port.
The input port bit layout is
I/O Address 201h
7 6 5 4 3 2 1 0
Pot #0 input
Pot #1 input
Pot #2 input
Pot #3 input
Switch #0 input
Switch #1 in put
Switch #2 input
Switch #3 input
Game Adapter Input Port
The four switches come in on the H.O. four bits of I/O port 201h. If the user is currently pressing a
button, the corresponding bit position will contain a zero. If the button is up, the corresponding bit will
contain a one.
The pot inputs might seem strange at first glance. After all, how can we represent one of a large num-
ber of potential pot positions (say, at least 256) with a single bit? Obviously we can’t. However, the input
bit on this port does not return any type of numeric value specifying the pot position. Instead, each of the
Page 1257
1135156.007.png
Chapter 24
four pot bits is connected to an input of a resistive sensitive 558 quad timer chip. When you trigger the
timer chip, it produces an output pulse whose duration is proportional to the resistive input to the timer.
The output of this timer chip appears as the input bit for a given pot. The schematic for this circuit is
Trigger (Write to I/O Address 201h)
558
Timer
D0
D1
D2
D3
L.O. Four Bits
on Input Port 201h
External Potentiometers
Joystick Schematic
Normally, the pot input bits contain zero. When you trigger the timer chip, the pot input lines go high
for some period of time determined by the current resistance of the potentiometer. By measuring how
long this bit stays set, you can get a rough estimate of the resistance. To trigger the pots, simply write any
value to I/O port 201h. The actual value you write is unimportant. The following timing diagram shows
how the signal varies on each pot’s input bit:
Input on D0..D3 goes high for some
period of time depending on the pot
setting.
1
0
Trigger
Occurs
Here
Analog Input Timing Signal
Page 1258
1135156.001.png 1135156.002.png
The Game Adapter
The only remaining question is “how do we determine the length of the pulse?” The following short loop
demonstrates one way to determine the width of this timing pulse:
mov
cx, -1
;We’re going to count backwards
mov
dx, 201h
;Point at joystick port.
out
dx, al
;Trigger the timer chip.
CntLp:
in
al, dx
;Read joystick port.
test
al, 1
;Check pot #0 input.
loopne CntLp
;Repeat while high.
neg
cx
;Convert CX to a positive value.
register will contain the number of passes made through this loop
while the timer output signal was a logic one. The larger the value in
cx
, the longer the pulse and, there-
cx
fore, the greater the resistance of pot #0.
There are several minor problems with this code. First of all, the code will obviously produce differ-
ent results on different machines running at different clock rates. For example, a 150 MHz Pentium system
will execute this code much faster than a 5 MHz 8088 system
. The second problem is that different joy-
sticks and different game adapter cards produce radically different timing results. Even on the same system
with the same adapter card and joystick, you may not always get consistent readings on different days. It
turns out that the 558 is somewhat temperature sensitive and will produce slightly different readings as the
temperature changes.
Unfortunately, there is no way to design a loop like the above so that it returns consistent readings
across a wide variety of machines, potentiometers, and game adapter cards. Therefore, you have to write
your application software so that it is insensitive to wide variances in the input values from the analog
inputs. Fortunately, this is very easy to do, but more on that later.
3
24.3 Using BIOS’ Game I/O Functions
The BIOS provides two functions for reading game adapter inputs. Both are subfunctions of the
int 15h handler.
To read the switches, load ah with 84h and dx with zero then execute an int 15h instruction. On
return, al will contain the switch readings in the H.O. four bits (see the diagram in the previous section).
This function is roughly equivalent to reading port 201h directly.
To read the analog inputs, load ah with 84h and dx with one then execute an int 15h instruction. On
return, AX, BX, CX, and DX will contain the values for pots zero, one, two, and three, respectively. In prac-
tice, this call should return values in the range 0-400h, though you cannot count on this for reasons
described in the previous section.
Very few programs use the BIOS joystick support. It’s easier to read the switches directly and reading
the pots is not that much more work that calling the BIOS routine. The BIOS code is
very
3. Actually, the speed difference is not as great as you would first think. Joystick adapter cards almost always interface to the computer system via
the ISA bus. The ISA bus runs at only 8 Mhz and requires four clock cycles per data transfer (i.e., 500 ns to read the joystick input port). This is equiv-
alent to a small number of wait states on a slow machine and a gigantic number of wait states on a fast machine. Tests run on a 5 MHz 8088 system
vs. a 50 MHz 486DX system produces only a 2:1 to 3:1 speed difference between the two machines even though the 486 machine was over 50 times
faster for most other computations.
Page 1259
When this loop finish execution, the
slow. Most BIO-
Ses read the four pots sequentially, taking up to four times longer than a program that reads all four pots
concurrently (see the next section). Because reading the pots can take several hundred microseconds up
to several milliseconds, most programmers writing high performance games do not use the BIOS calls,
they write their own high performance routines instead.
This is a real shame. By writing drivers specific to the PC’s original game adapter design, these devel-
opers force the user to purchase and use a standard game adapter card and game input device. Were the
game to make the BIOS call, third party developers could create different and unique game controllers and
then simply supply a driver that replaces the int 15h routine and provides the same programming inter-
face. For example, Ge novation made a device that lets you plug a joystick into the parallel port of a PC.
1135156.003.png
Zgłoś jeśli naruszono regulamin