Sie sind auf Seite 1von 23

Parallel Input/Output Parallel Input/Output

Thorne : Section 20.1, 20.2


(Irvine Edition IV : Section 17.1)
SYSC3006 1
Basic Concepts of I/O
Input/Output isthe information exchange between CPU and Input/Output isthe information exchange between CPU and
(external) connected devices
Block Diagram of a Simple Computer System
Processor
connected devices
k b d
Memory
I/O
Bus
keyboard
mouse
display
printer
I/O
printer
disk drives
comm
n
links, etc.
SYSC3006 2
Basic Concepts of I/O
InterfacingandprogrammingI/Odevicesaredifferent from Interfacing and programming I/O devices are different from
memory programming :
1. Electrical characteristics may be different from CPUs
A l d i d i Analog devices, power, current drive
2. I/O devices operate asynchronouslyfrom the CPU (and the
program being run) p g g )
To transfer any data, the processor and I/O device must
synchronizeor handshake to exchange information
SYSC3006 3
Basic Concepts of I/O
The I/O box in our simple computer represents the set of
independent I/Ocomponents associatedwitheachconnected independent I/O components associated with each connected
device.

keyboardcomponent(s)
connected devices
keyboard keyboard component(s)
bus
keyboard
mouse
display
printer
I/O
comm
n
component(s)
disk drives
comm
n
links, etc.
I/O componentsare interfacesthat electrically connect an
external device to computers internal bus.
ThebusconnectionallowstheCPU toreadand/or writethe
SYSC3006 4
The bus connection allows the CPU to read and/or writethe
device
I/O Programming on a PC g g
Access to the Hardware is routed through several layers
User Programs (Applications)
Operating System
BIOS
Hardware
Operating System : General device-independent access to hardware
BIOS: lowlevel routinesinROM for devicesupport (I/O,
SYSC3006 5
BIOS : low level routines in ROM for device support (I/O,
configuration, diagnostics)
I/O Ports
A ll h f i f i b b ( d A port allows exchange of information between bus (connected
to CPU and memory) and I/O components (that in turn are
connected to devices)
An I/O component typically has 3 kinds of Ports:
Control ports: writevalues to these control behaviour of
component/device component/device
Status ports: readvalues from these find out about current
state of component/device
Data port: read and/or writevalues of these exchange
application information
Some ports are read-only, write-only or read&write.
SYSC3006 6
p y, y
I/O Addresses
When connected to a computer system, each device port is
assigned an I/O address
A device(port) isidentifiedbyitsI/Oaddresses A device (port) is identified by its I/O addresses
Computer will read/write from/to this I/O address to
receive/send data from/to device
In microprocessor architectures, there are two kinds of I/O
addressconnections address connections
1. Isolated I/O
2. Memory-Mapped I/O
Concept !
SYSC3006 7
I/O Addressing Schemes : Isolated I/O (Intel)
- The microprocessor has dedicated instructions for I/O operations. p p
- The microprocessor has a separate address space for I/O devices.
MemoryMap
Control (RD/WR)
Processor
Memory Map
FFFFF
Data
I/O Map
Memory
Address
0000
FFFFh
I/O Device
0000
SYSC3006 8
I/O Control (IOR/IOW)
I/O Addressing Scheme : Memory Mapped I/O (Motorola)
The microprocessor uses the same instruction set to perform memory p p y
accesses and I/O operations.
The I/O devices and memory components are resident in the same
memory space.
M M
y p
Processor
Memory Map
FFFFF
Control (RD/WR)
Data
Memory I/O Map
Address
I/ODevice
SYSC3006 9
I/O Device
0000
Intel Uses Isolated I/O Implementation
of a Concept
Within 80x86 family, I/O addresses range 0-FFFFh
For thePC devicesareassignedstandardI/Oaddresses(used
of a Concept
For the PC, devices are assigned standard I/O addresses (used
by all manufacturers of PCs)
Keyboard 60h (dataport)
Speaker 61h(dataport) Speaker 61h (dataport)
Parallel Printer (LPT1) 3BCh (dataport)
3BDh (status port)
3BEh (control port)
I/O ports memory cells p y
J ust because a port might allow a value to be written to it
DOES NOT meanthat theport canhaveavaluereadfromit
SYSC3006 10
DOES NOT mean that the port can have a value read from it
(or vice versa!)
I/O Instructions
Intel uses the Isolated I/Oaddress scheme
I/O ports and memory address can have same value but
represent locations in two separate address spaces ep ese oc o s wosep e dd esssp ces
There are separateinstructions that transfer data to/from I/O
ports
For memory transfers : MOV AL, [61h]
Offset address of memory
For I/Otransfers : INAL, 61h
OUT 61h, AL
I/O port address
SYSC3006 11
Intel 8086 IN Instruction
Mnemonic : IN
Semantics : Readfrom I/O port
S t
Legacy of 8085
A l t ! Syntax :
IN AL, imm8 8-bit read
IN AX, imm8 16-bit read
g y
which had an 8-bit
I/O space
Data
Accumulator reg!
,
imm8specifies an 8-bit I/O addressin the range 00h-FFh
16-bit data reg!
src des
IN AL, DX
IN AX, DX
DX specifiesa16-bit I/Oaddressintherange0000h-FFFFh
Addressing Modes are different!
SYSC3006 12
DX specifies a 16 bit I/O addressin the range 0000h FFFFh
Intel 8086 OUT Instruction
Mnemonic OUT
Semantics : Writeto I/O port
S t
Destination looks like immediate!
Syntax :
OUT imm8, AL 8-bit write
OUT imm8, AX 16-bit write
src des
,
src des
OUT DX, AL
OUT DX, AX
SYSC3006 13
I/O Example
Supposewehaveadisplaydevicefor ASCII characters Suppose we have a display device for ASCII characters
The programmers model is a single write-only data port at I/O
Address =04E9H
Th di l i d i Th ASCII h i The display is cursor driven : The ASCII character written
to the data port is display at the current cursor position
Cursor position is maintained by the display device p y p y
When a character is written, cursor position is advanced
Advancement handles new lines and scrolling too.
Write a code fragment showing the display of the character A
SYSC3006 14
I/O Example
Solution : Write a code fragment showing the display of the
character A
MOV DX, 04E9H
MOV AL, 41h
OUT DX AL
This port address is 16 bits, so
must load it into DX first
OUT DX, AL
.
Question :
(Immediate is only for 8-bit
port addresses)
A character is a byte

IN AL, DX
Will AL contain 41H ?
SYSC3006 15
Lab PCs LED/Switch Box
Our labs have attached an I/O Box to the PCs
5 LEDs (Light Emitting Diodes) each may be either ON or
OFF
5 switches each may be either ON or OFF y
LEDs are connected to bits of an 8-bit output parallel port
Each LED is driven by a particular bit in the port
LE
D
l f bit
D
value of bit
i
determines whether
LED is ON or OFF
8-bit
SYSC3006 16
bit
i
port
Programmers Model for the Lab LEDs
LEDdataport address: E010H LED data port address: E010 H
Bit configuration: LEDS are labelled 1 .. 5
[ bit 7 =most significant ; bit 0 =least significant ]

bit 7 6 5 4 3 2 1 0
LEDx x x 5 4 3 2 1
1through5indicatebitsfor LEDs1through5 1 through 5 indicate bits for LEDs 1 through 5
x indicates unused (dont care what value is written)
T t LEDON t bit i t d ithth LED To turn LED ON: set bit associated with the LED
ie. write an 8-bit value to the port in which the bit associated with
the LED is a "1"
ToturnLEDOFF: clear bit associatedwithLED
SYSC3006 17
To turn LED OFF: clear bit associated with LED
Programming the LEDs
Challenges :
The LEDs interface is a 8-bit port.
Alth h t t t/ l ti l bit t Although we want to set/clear a particular bit, we must
writean entire byteto the LED port.
Writing any value to the LED port affects all LEDs !
To modify the state of one LED, we must know the state
of all LEDS . but
Readingport ismeaningless(writeonlyport) Reading port is meaningless (write-only port)
In particular, we cannot read the LED port to get the
current state of all LEDs.
SYSC3006 18
Programming the LEDs
To manipulate the LEDs individually, the program must
maintain the stateof the LEDs as a variable
Updatethevariableeachtimeanewvalueiswrittentothe Update the variable each time a new value is written to the
LED port so that it always represents the current LED state.
LED St t DB ? t t t f LED LED_State DB ? ; current state of LEDs
; To turn on LED x
; set appropriate bit in LED_State
; write LED_State to LED port
; To turn off LED x
; Clear theappropriatebit inLED State ; Clear the appropriate bit in LED_State
; Write LED_State to the LED port
Q ti H d i iti li thi ?
SYSC3006 19
Question : How do you initialise this program ?
Lab Switches Lab Switches
The 5 switches on the I/O box are connected to 5 bits of an 8-bit input
parallel port parallel port
There is one bit (in port) per switch
It is a read-onlyport used to get current setting of all switches
A writetotheport hasnoeffect A write to the port has no effect
If the switch is ON, its bit is set (i.e. "1")
If the switch is OFF, its bit is clear (i.e. "0")
Theswitcheslabelled"A" through"E" The switches labelled A through E
Switch data port address: E011H
Bit config: [ bit 7 most signif ; bit 0 least signif ] Bit config: [ bit 7 =most signif ; bit 0 =least signif ]
bit 7 6 5 4 3 2 1 0
Switch E D C B A x x x
i di t d( d fi d)
SYSC3006 20
x indicates unused (undefined)
Switch De-bouncing
A switch is a mechanical device
Moving the switch position, opens or closes a circuit
Switcheshavemetal contactsthat will completethecircuit Switches have metal contacts that will complete the circuit
when joined
Theswitchesinthelabarespring loadedtoholdopen/closed The switches in the lab are spring-loadedto hold open/closed
position
When the position is changed, the contacts can bounce
A l Di i b d Analogy : Diving board
For a program reading the switch port, the value of switch
output will appear to oscillate (open/closed) until bouncing
t stops
The program must filter out the oscillations so that the
program only sees one switch state change per position
h Thi i ll dd b i
SYSC3006 21
change. This is called de-bouncing.
Simple De-Bouncing
Write a loop that polls the switch until first change is seen
Waste enough time (do-nothing-loop) until sure switch
stoppedbouncing stopped bouncing
Questions :
How much is enough time?
What if the program waits longer than necessary?
What if the program does not wait long enough ?
SYSC3006 22
Adaptive De-Bouncing
In a loop, poll the switch until first change is seen p, p g
Set a loop counter to an init_value
Repeat {
Poll switch
E l i h hi h
Poll switch
If the switch has change state again
{
Explain why this approach
is adaptive ?
loop counter =init_value
} else
{
Remaining Issue : Decide
on the init_value
decrement loop counter
}
}until loopcounter ==0
SYSC3006 23
} until loop counter ==0

Das könnte Ihnen auch gefallen