0299m(2).pdf

(122 KB) Pobierz
3KL]]\% &20387(56
Part 4: Polling vs Interrupt
E\ $OYLQ %URZQ DQG &OLYH ¶0D[· 0D[ILHOG
So, here we are already at the fourth installment of our really unique and exciting electronics and
computing project. And, from what we hear, you're getting on well with it Fun, isn't it
a really unique mix
of hardware and software to show how computers work.
Now, buzzing with excitement, we enter the second phase of the tale about our phantasmagorical little
rascal, as we start to prepare to drive the world (or at least, our buggy)
yes, folks the
PhizzyBot
robotic
wheelie is fast approaching. You too can drive it, without needing to know too much about electronics
(although you do know a bit about PCs, don't you?).
Hi there
Phizzyphiles.
As you
will recall, last month we revealed
our master plan, which is to use
the
PhizzyB
as the “brain” of a
simple robot called the
PhizzyBot.
(We'll call this Plan A so that no
one gets confused).
Well, in this month's
PhizzyB
construction article, Alan Win-
stanley describes how to build
two new input devices. These de-
vices can be used for a variety of
purposes, including (as we shall
see) collision detection for the
forthcoming
PhizzyBot.
One interesting point before
we start is that the earlier articles
in this series were focused more
towards the
PhizzyB Simulator
running on your PC, as opposed
to the real
PhizzyB
computer it-
self. This was intended to build
your familiarity with the simulator,
but things are about to change.
Although our forthcoming ex-
periments can (and should) be
performed on the simulator, you'll
discover that having access to a
real
PhizzyB
is going to be ex-
tremely advantageous from this
point onwards.
EXPERIMENT 1
The polled device
First of all, we're assuming
that you've already built the two
input devices described in this
month's
PhizzyB
construction
article. We'll commence with
the simpler of the two boards
the 8-bit Switch module
which
we will call the “polled” device
(we'll explain the significance of
the term
polled
later).
Connect this board to the
PhizzyB's
input port at address
$F011 using a standard
PhizzyB
ribbon cable. (As you know, we
can emulate this device in the
simulation world using the cor-
responding generic input device
as shown in Fig.1.)
Our polled device essen-
tially consists of eight push-
button switches, each of which
drives one of the
PhizzyB's
in-
put port bits. Each switch has an
Fig.1. The
PhizzyB
Simulator interface, which simulates a real
PhizzyB
interconnected by ribbon cables to a variety of
expansion boards.
EPE Online, February 1999 - www.epemag.com - 284
Copyright © 1999 Wimborne Publishing Ltd and
Maxfield & Montrose Interactive Inc
&RQVWUXFWLRQDO 3URMHFW
Listing 1
INPORT1:
OUTPORT0:
LOOP:
.EQU
.EQU
#
#
#
.ORG $4000
#
LDA [INPORT1] #
STA [OUTPORT0] #
JMP [LOOP]
#
.END
$F011
$F030
Input port $F011
Output port $F030
Set start address
Load ACC from I/P
Store ACC to O/P
Do it all again
and pressing a switch connects
that input to a logic 0.
We could have wired our in-
put board the opposite way
around had we wished
but we
didn't! Why not? Well one very
good reason was that wiring the
board the way we did allowed us
to introduce a very important
point, which is that logic 0s and
1s mean whatever we want them
to mean at any particular time.
Before we proceed, reset
your
PhizzyB
and dismiss the
PBLink
utility (as we noted in Part
3, whilst the
PBLink
utility is ac-
tive it may slow the simulator on
your PC, because this utility is
constantly bouncing packets of
information back and forth to
your
PhizzyB).
If you don't have a real
PhizzyB,
you can replicate this
experiment on your simulator.
Load RAM
Use the
Memory
command to load
ccexp1.ram
into
the simulator's memory and then
set this program running.
associated pull-up resistor and
capacitor connected to it. The
pull-up resistors serve to con-
nect the input bits to “weak”
logic 1 (+5V) values. When a
switch is closed, it connects
that input bit to a “strong”
logic 0 (0V).
When we activate a switch,
we tend to think of it transition-
ing smoothly from an Off to an
On condition (or vice versa). In
reality, we can expect to see a
condition called
“switch
bounce,”
in which the contacts
inside the switch bounce to-
gether making several momen-
tary contacts before providing a
good connection.
Switch bounce isn't particu-
larly troublesome in the case of
a light switch in your house, but
it can cause problems when a
switch is connected to a com-
puter, because a computer is so
fast that it may well assume
that the switch has been
pressed multiple times and
respond accordingly.
This is why we associated a
capacitor with each switch. The
RC
time constant of the
resistor-capacitor combination
serves in a simple way to
smooth transitions between
logic states and to remove the
effects of switch bounce, al-
though other more sophisticated
digital methods can be used.
Invoke the
PhizzyB Simula-
tor,
activate the assembler, and
enter the program shown in List-
ing 1 (remember that anything
to the right of a hash “#” charac-
Copyright © 1999 Wimborne Publishing Ltd and
Maxfield & Montrose Interactive Inc
ter is a comment, which will be
ignored by the assembler). As
we see, this program simply
loops around reading from the
switch board we just connected
to the input port at address
$F011, and writing any values it
finds there to the 8-bit LED bar-
graph display connected to the
output port at address $F030.
Save
Now use the
File
As
command to save this pro-
gram as
ccexp1.asm,
and as-
semble it to generate the corre-
sponding
ccexp1.ram
file.
Activate the
PBLink
utility,
open the
ccexp1.ram
file you
just created, download this file
to your real
PhizzyB,
and use
the
PhizzyB's
Run switch (or the
appropriate
PBLink
toolbar icon
or function key) to set the pro-
gram running.
The first thing we note is
that all the LEDs on the bar-
graph display light up. Now try
pressing (and holding) one of
the buttons on the new input
module, and observe that the
corresponding LED on the bar-
graph display turns off. Simi-
larly, as soon as you release
this button, the same LED turns
on again. (Verify that all of the
buttons work by pressing and
releasing them in turn.)
Initially, this may seem to
be counter-intuitive, because we
typically expect that pressing a
switch will cause a light to turn
on. But our input board is be-
having exactly as it was de-
signed, because its pull-up re-
sistors hold the inputs at logic 1,
S
S
Enter a value of 11111111
into the binary field on the
generic input board at address
$F011 (the binary field is the one
indicated by a percent “%” char-
acter). Don't forget to click that
board's
Set
button to present this
value to the input port, at which
time all of the LEDs on the bar-
graph display will light up.
Now enter the following val-
ues to represent the effects of
pressing the switches in the real
world:
Value
Switch
11111110
0
11111101
1
11111011
2
11110111
3
11101111
4
11011111
5
10111111
6
01111111
7
11111111
None
EPE Online, February 1999 - www.epemag.com - 285
&RQVWUXFWLRQDO 3URMHFW
When you've finished, reset
the simulator, and proceed to
the second experiment.
program to the
PhizzyB
and set
the little rascal running. As you
see, the LEDs on the bargraph
display now go out, and it's only
when you press one of the
switches that the related LED
turns on. (Verify that all of the
buttons work by pressing and
releasing them in turn.)
When you've finished play-
ing, reset your
PhizzyB
and dis-
miss the
PBLink
utility. Note that
you can test this new program on
the
PhizzyB
Simulator using
exactly the same input values
that you used before.
mence with an
LDA
to load a
value from the switches into the
accumulator, and we follow this
with an
XOR
to invert the con-
tents of the accumulator. Now
we use a
JZ
(“jump
if zero”)
to
test whether the accumulator
contains zero
if it does we
jump back to the
LOOP
label.
Thus, this portion of the pro-
gram simply loops around
waiting for us to press one of
the switches.
As soon as we do press a
switch, the
JZ
instruction fails
and we “drop through” to the
CMPA
instruction. One thing you
need to know is that the
CMPA
instruction assumes that both
values being compared are un-
signed binary numbers. (The
concepts of signed and un-
signed binary numbers were
discussed in last month's
PhizzyB Bonus Article.)
The way the
CMPA
instruc-
tion works is that it compares
the current value in the accu-
mulator with another value that
we specify. If the value in the
accumulator is the bigger, the
Carry flag is set to 1 and the
Zero flag is cleared to 0. If the
two values are equal, the Zero
EXPERIMENT 2
Inverting the inputs
As we just discussed, we
typically expect that pressing a
switch will cause a light to turn
on rather than turn off. How-
ever, one of the great things
about computers is that we can
use software (programs) to
modify the way things work in
hardware (the physical bits and
pieces in the real world). Let's
examine an example of this.
Use the assembler to load
your
ccexp1.asm
program and
save it out as
ccexp2.asm.
Now
add an
XOR %11111111
in-
struction between the existing
LDA
(“load
accumulator”)
and
STA
(“store
accumulator”)
in-
structions as follows:
LDA [INPORT1]
XOR %11111111
STA [OUTPORT0]
From earlier articles in this
series, we know that
XORing
a
bit with a logic 1 has the same
effect as inverting that bit. In
this case we're
XORing
the con-
tents of our 8-bit accumulator
with eight 1s, which will invert
each bit in the accumulator.
(We could have achieved
exactly the same effect using
XOR $FF
in hexadecimal or
XOR 255
in decimal.)
EXPERIMENT 3
The CMPA Instruction
One instruction we haven't
looked at before is
CMPA
(“compare
accumulator”),
which
allows us to compare the numeri-
cal value of the current contents
of the accumulator to some other
numerical value. In order to in-
vestigate this instruction, use
your assembler to save the
ccexp2.asm
program as
ccexp3.asm,
and then modify this
program as shown in Listing 2.
Don't Panic! This isn't as
complicated as it looks. We com-
Listing 2
INPORT1:
OUTPORT0:
LOOP:
.EQU
.EQU
.ORG
LDA
XOR
JZ
$F011
$F030
$4000
[INPORT1]
%11111111
[LOOP]
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
Input port $F011
Output port $F030
Set start address
Load ACC from I/P port
Invert ACC
Jump if ACC = 0
Else compare
Jump if ACC is bigger
Load ACC with 00001111
Store ACC to O/P port
Do it all again
Load ACC with 11110000
Store ACC to O/P port
Do it all again
So this new program will
loop around loading a value
from the input port into the
accumulator, inverting all of
the bits in the accumulator, and
IS3TO0:
writing this new value to the
output port.
Assemble this program to
generate the corresponding
ccexp2.ram
file, then use your
PBLink
utility to download this
IS7TO4:
CMPA %00001000
JC
[IS7TO4]]
LDA
%00001111
STA [OUTPORT0]
JMP [LOOP]
LDA
STA
JMP
.END
%11110000
[OUTPORT0]
[LOOP]
Copyright © 1999 Wimborne Publishing Ltd and
Maxfield & Montrose Interactive Inc
EPE Online, February 1999 - www.epemag.com - 286
&RQVWUXFWLRQDO 3URMHFW
flag is set to 1 and the Carry
flag is cleared to 0. And if the
value in the accumulator is the
smaller, then both the Carry and
Zero flags are cleared to zero.
In this particular program,
we're comparing the value in
the accumulator (which reflects
the switch we just pressed) with
a binary value of 00001000. So
if the value in the accumulator
is the bigger, this means that we
must have pressed one of the
switches numbered 4, 5, 6 or 7.
Alternatively, if the value in
the accumulator is not the big-
ger, then we must have pressed
one of switches numbered 0, 1,
2 or 3. (With regard to the previ-
ous sentence, note that we
specifically didn't say
“... if the
value in the accumulator is the
smaller...”
This is because if we
press switch 3 the two values
will be equal
you have to be
very precise when you're talking
about the actions of programs.)
Thus, following the
CMPA,
a
JC
(“jump
if carry”)
instruction is
used to decide what we do. If
the carry flag is set, we know
that the value in the accumula-
tor was the bigger, and we jump
to label
IS7TO4
(meaning,
“this
switch is one of those numbered
7 to 4”).
At this point we load the ac-
cumulator with a binary value of
11110000, store it to the output
port, and jump back to
LOOP
to
wait for another switch to be
pressed. Note that the
11110000 value we load into
the accumulator has no particu-
lar significance
it's just some-
thing we'll recognize when it ap-
pears on the LEDs.
Of course, if the value in
the accumulator isn't the bigger
when we perform the
CMPA,
then the Carry flag will be
cleared to zero, the
JC
instruc-
tion will fail, and the program
Copyright © 1999 Wimborne Publishing Ltd and
Maxfield & Montrose Interactive Inc
will “drop through” to the
IS3TO0
label (meaning
“this
switch is one of those numbered
3 to 0”)
be careful not to get
the letter “O” of “TO” confused
with the number “0” at the end
of this label.
In this case, we load the ac-
cumulator with a binary value of
00001111, store it to the output
port, and jump back to
LOOP
to
wait for another switch to be
pressed (again, this 00001111
value has no particular signifi-
cance beyond the scope of this
example program).
So let's assemble our pro-
gram to generate the corre-
sponding
ccexp3.ram
file, use
the
PBLink
utility to download
this file to your
PhizzyB,
and run
the program. Try pressing one
of the 0 to 3 switches and ob-
serve the right-most four LEDs
light up, then try pressing one of
the 4 to 7 switches and note the
left-most four LEDs light up.
Continue to experiment with
this program until your heart
stops pounding, then reset the
PhizzyB,
dismiss the
PBLink
utility, and proceed to the next
section. (You can also replicate
this experiment on your
simulator if you wish
remem-
ber to use the binary patterns
11111110 through 01111111
to represent the action of
pressing switches 0 through
7, respectively.)
using the CPU to do anything
else anyway, but what if we
did want to use the CPU for
another purpose?
As an example, here's an
experiment you can perform for
yourself. Take the simple LED
output board you created in Part
2 of this series and plug it into
the external output port at ad-
dress $F031. Now write a pro-
gram that performs a simple bi-
nary count and displays it on
this output board. In fact you
can use the
test1.asm
file that
was supplied with your
PhizzyB
Simulator
as a basis for this pro-
gram, but remember to save it
out under another name.
Also, you'll have to change
the address of the output port to
$F031 and the label associated
with this port to
OUTPORT1
(remember to change this label
wherever it is used throughout
the program).
You'll also note that this
program contains a simple sub-
routine called
WAIT,
which is
used to slow the count se-
quence down so that you can
see what's happening
(subroutines were introduced
in Part 3).
Once you have this pro-
gram running, modify it such
that every time it performs a
count, it also checks our new
polling switch device connected
to input port $F011. If none of
the switches are pressed, then
make the program return to per-
forming its binary count. But if
one of the switches is pressed,
then make the program perform
the same comparison and dis-
play the same values to the out-
put port at $F030 as we did in
Experiment 3 above (and then
let it return to performing its bi-
nary count).
As you'll discover, one big
problem we find when we try to
combine this polling technique
POLLING PROBLEMS
The previous experiment
relied on the program looping
around reading from the input
port and waiting for something
to happen. This technique is re-
ferred to as
polling,
and it tends
to occupy a lot of the CPU's
time and resources. Using the
polling technique was perfectly
acceptable in the case of Exper-
iment 3, because we weren't
EPE Online, February 1999 - www.epemag.com - 287
&RQVWUXFWLRQDO 3URMHFW
M
RA
M
RO
tp
Ou
ort
tP
u
Inp
From the
outside world
Control bus
Data bus
Address bus
tells the CPU that something
interesting is happening (this
request is stored inside the
CPU in a register called the
interrupt latch).
Similarly, when the CPU
starts to respond to an interrupt
(as discussed in a moment), it
places its IACK output into an
active-low state to tell devices
in the outside world that it's do-
ing something. (Note that we
won't be using the IACK output
at this time.)
u
o
tP
rt
t
se
Re
ck
Clo
IRQ
IA
C
CK
PU
To the
outside world
Fig.2. The CPU has a special Interrupt (IRQ)
input
and Interrupt
Acknowledge (IACK) output.
with the binary count is that
we're trying to do two very dis-
tinct things, but that the code for
both these things becomes
somewhat intermeshed. This
makes it difficult to write the
program, and also to under-
stand it and modify it later.
Even worse, suppose that
you press and release a switch
whilst the program is executing
the
WAIT
subroutine. In this
case your program might not
even notice that you'd pressed
the switch at all! All of these
points mean that the polling
technique has to be used with
discretion. One alternative is
to use a technique caller
interrupt-driven I/O
as
discussed presently.
Note that if you find creating
the program discussed in this
section to be a little too taxing
and/or time-consuming, you'll
be happy to know that we've
created one for you to peruse
and ponder. All you have to do
is to bounce over to the
EPE
Online
web site at
www.epemag.com,
wander
into the Library, and download
the
ccextra1.asm
program and
associated text file.
INTERRUPT MASK
Thus far we've introduced
four status flags: Z (Zero), C
(Carry), O (Overflow), and N
(Negative). In fact there is a fifth
flag called I (Interrupt
Mask).
The interrupt mask is somewhat
different to the other status
flags, in that it is not set as the
result of a logical or arithmetic
instruction or condition. Instead,
the interrupt mask is used to tell
the CPU whether of not it is al-
lowed to respond to interrupts.
Following a power-up or re-
set condition, the interrupt mask
flag is initialized to its inactive
state (a logic 0 in the case of
the
PhizzyB).
This means that,
by default, the
PhizzyB
will NOT
respond to interrupts. Thus, in
order for the
PhizzyB
to accept
an interrupt request, we first
have to use a
SETIM
(“set
inter-
rupt mask”)
instruction, which
loads the I flag with a logic 1.
The way this works is that
the CPU checks the state of the
interrupt mask every time it
completes an instruction (Fig.3).
If the mask is inactive, the CPU
proceeds to the next instruction;
otherwise it checks the interrupt
latch to see if an interrupt has
been requested. If no interrupt
was requested the CPU again
IRQ AND IACK
As discussed in the previ-
ous section, there are a number
of problems associated with a
polling strategy. What we would
like to do is to create a program
that can concentrate on the task
for which it was intended
(performing a binary count in
this case), without being obliged
to constantly keep on checking
to see if we've pressed a switch
on our input device.
However, when we do press
a switch, we want the CPU to
respond as quickly as it can by
performing the comparison, dis-
playing the result on output port
$F030, and returning to its bi-
nary count.
To facilitate this sort of
thing, the CPU has a special
input called the IRQ (“interrupt
request”)
and a special output
called the IACK (“interrupt
ac-
knowledge”),
as shown in Fig.2.
For a number of reasons
(mostly historical), control sig-
nals are usually active-low (that
is, their active state is a logic 0),
and the IRQ is no exception.
What this means is that we usu-
ally maintain the IRQ signal at a
logic 1, so pulling it to a logic 0
Copyright © 1999 Wimborne Publishing Ltd and
Maxfield & Montrose Interactive Inc
EPE Online, February 1999 - www.epemag.com - 288
Zgłoś jeśli naruszono regulamin