I/O devices. I/O devices 2/32 Addressing Service Data buffering.

Post on 03-Jan-2016

225 views 0 download

Tags:

transcript

I/O devices

I/O devices 2/32

Addressing

Service

Data buffering

I/O devices - addressing 3/32

I/O device addressing methods:

memory-mapped:

• in small systems or where memory address space usage is less then 100%:

- so full utilization of memory space gets impossible;

• access to I/O devices by the same machine instructions as to memory, so:

- more types of instructions;

- instructions are executed little faster (general-purpose processors);

• during data exchange are used signals like during memory access:

/MREQ, /RD or /WR (/MEMR or /MEMW).

Example of memory-mapped addressing:

VCC

A15

A14A13A12

A[0..15]

/MREQ

/CSROM/CSRAM

/CSIO1/CSIO2/CSIO3

I/O1

I/O2

I/O3

RAM

EPROM0000h

8000h

FFFFh

7xxxh

6xxxh

5xxxh4FFFh

2000h

1000h

A1

B2

C3

E14

E25

E36

Y015

Y114

Y213

Y312

Y411

Y510

Y69

Y77

74LS138

I/O devices - addressing 4/32

isolated I/O:

• used in general-purpose, huge, modular systems

or where memory address space is (or will be) utilized in almost 100%, so:

- no any memory address sub-space is lost;

• access to I/O devices realized by special group of machine instructions, so:

- only few instructions to use (at least 2);

- instructions are executed slower,

because of additional clock cycles inserted in machine cycles;

• during data exchange are used dedicated to I/O devices signals:

/IORQ, /RD /WR (/IOR or /IOW).or

I/O devices - addressing 5/32

Example of isolated I/O:

VCC

A15

A14A13A12

A[0..15]

/MREQ

/CSROM

/CSRAM

/IORQ

/CSIO0/CSIO1

/CSIO7/CSIO6

/CSIO2

A7A6

A5A4

RAM

EPROM0000h

8000h

FFFFh

7000h

1000h

A3

00h

80h

FFh

40h48h50h58h60h68h70h78h

3FhIO0IO1IO2

IO6IO7

01000xxxB01001xxxB01010xxxB

01110xxxB01111xxxB

A1

B2

C3

E14

E25

E36

Y0 15

Y1 14

Y213

Y312

Y411

Y510

Y69

Y7 7

74LS138

A1

B2

C3

E14

E25

E36

Y0 15

Y114

Y213

Y312

Y411

Y5 10

Y69

Y7 7

74LS138

I/O devices - addressing 6/32

1. Programme service

new inp. data?

Y

checkingdevice state

read data

N

data input data output

ready?

Y

checking device state

write data

N

I/O devices – service 7/32

I/O nDATAPORT

STATUSREGISTER

I/O m

DATAPORT

READY

Features:

• simple implementation, but:

• allowed when:

- device work fast;

- microprocessor has free time to wait;

• Attention: emergency of dead-loop,

if device is switched-off, failed, broken-down, etc.

I/O devices – service 8/32

2. Programme service in clock interrupts

data input data output

MAIN PROGRAMM: in_flag:=0

MAIN PROGRAMM :read from buffer; in_flag :=0

(optional)

MAIN PROGRAMM: write to buffer; out_flag:=1

MAIN PROGRAMM (optional):

write to buffer; out_flag:=1

CLOCK INTERRUPT SERVICE ROUTINE:

N

N

Y

Ynew data ?

in_flag =0 ?

data read in_flag :=1

CLOCK INTERRUPT SERVICE ROUTINE :

N

Yout_flag =1 ?

N

Ydevice ready ?

data writeout_flag:=0

I/O devices – service 9/32

Features:

• devices are served in discrete moments;

• constant frequency of service;

• processor can realize other tasks between interrupt service routines;

• allowed, when device isn’t block-data device.

I/O devices – service 10/32

MAIN PROGRAMM: inK_flag:=0; inK_fail:=0;

counter_K:=MAX_CHECK;

CLOCK INTERRUPT SERVICE ROUTINE :

read data inK_flag:=1

N

N

Y

MAIN PROGRAMM: read from buffer; inK_flag:=0 (optional)

dec(counter_K)

N

YinK_fail:=1

Y

MAIN PROGRAMM: write to buffer;

outL_flag:=1; outL_fail:=0;counter_L:= MAX_CHECK;

CLOCK INTERRUPT SERVICE ROUTINE :

write dataoutL_flag:=0

N

N

Y

MAIN PROGRAMM (optional): write to buffer; outL_flag :=1

dec(counter_L)

Y

outL_fail:=1

N

Y

counter_K =0 ?

counter_L=0 ?

new data ?

inK_flag=0 ?

device ready ?

outL_flag =1 ?

I/O devices – service 11/32

3. Service by DMA

accept of data transfer end in service routine

main programme - cont.

programming of DMA

main programme DMA transfer

interrupt

I/O devices – service 12/32

4. Service using device interrupts

MAIN PROGRAMM: in_flag:=0

DEVICE INTERRUPT SERVICE ROUTINE:

MAIN PROGRAMM :read from buffer; in_flag :=0

(optional)

MAIN PROGRAMM: write to buffer; out_flag:=1

DEVICE INTERRUPT SERVICE ROUTINE:

MAIN PROGRAMM (optional):

write to buffer; out_flag:=1

data input data output

N

Yout_flag =1 ?

N

Ydevice ready ?

write dataout_flag:=0

N

N

Y

Ynew data ?

in_flag=0 ?

read datain_flag:=1

I/O devices – service 13/32

Features:

• very effective method;

• vector interrupts are the most effective;

• service routine executed only if device is ready for transfer.

I/O devices – service 14/32

5. Service without previous check

some devices are always ready for transfer or don’t have any “ready” signal or

status register,

they allow direct data read and write

for example: binary I/O, simple displays, D/A converters.

I/O devices – service 15/32

Recommended I/O devices service rules:

• not dissipate I/O instructions by whole programme;

• usage I/O routines, modules of device service;

• usage buffering variables input and output data;

I/O devices – data buffering 16/32

Example of lack of input data buffering:

read01110101b

port in X

port in X

port in X

PRO

G

RAMM

SEQ.

t1

t2

read01110101b

read11110101b

NO, because of:• error during calculations;• error of control decisions;

I/O devices – data buffering 17/32

Correct solution (with buffer):

YES, because:• correct calculations;• faster (sometimes).

port in X

buffer X

PROGRAMM

SEQUENCE

t1

t2

read01110101b

copy to bufferread

01110101b

read01110101b

read01110101b

I/O devices – data buffering 18/32

Example of some input ports:

readings from buffers to procesing

read all input dataand copy to buffering

variables

port in X1

buffer in X1

PROGRAMM

SEQUENCE

. . .

port in Xk

. . .

buffer in Xk

solution used in controllers

I/O devices – data buffering 19/32

Example of lack of output data buffering :

writeport out Y1

port out Y2

port out Y3

PROGRAMM

SEQUENCE

t1

t2

write

write

Faults:

• dissipation of transfer instructions;

• time intervals t1, t2

between writings to ports quite large

and can have negative effect on

behaviour of controlled device

I/O devices – data buffering 20/32

Recommended solution for many output ports:

write

buffer out Y1

buffer out Y2

buffer out Y3

PROGRAMM

SEQUENCE

t1

t2

write

write

port out Y1

port out Y2

port out Y3

write to ports

Advantages:

• output instructions grouped

in one place;

• time intervals t1, t2

between writings to ports minimized

I/O devices – data buffering 21/32

Output word determined bit by bit - invalid solution:

write of bits 3..0port out Y

port out Y

PROGRAMM

SEQUENCE

t1

write of bits 7..4

Fault:

binary output error

I/O devices – data buffering 22/32

Output word determined bit by bit - valid solution :

Advantege:

logical validchange of bit 7..4

change of bit 3..0

buffer out Y

PROGRAMM

SEQUENCE

t1

port out Y1 write to port

read from buffer

I/O devices – data buffering 23/32

Simple buffering (as above) is used when read and write are regular

(for example: in clock interrupt service routine or in main programme loop)

Buffering variables placed in RAM stores the copies of input and output data.

It’s used for so called process devices.

Devices, which work irregular, with various speed need complex buffering

structures.

Cycle buffers are used.

Examples of such “irregular” devices: serial ports, printers, etc.

I/O devices – data buffering 24/32

one-dimension matrix of recommended length

B=2k

valid data

FU

FF

Cyclic buffer

ver.1.

FU - index (variable) first used byte (with oldest valid data), FU=0..B-1;

FF - index (variable) of first byte free to write, FF=0..B-1.

I/O devices – data buffering 25/32

writing to buffer reading from buffer

input data in interrupt service routine:

if (FU+B-1) mod B FF then: A:=IN[in_port]

M[BUF+FF]:=A FF:=(FF+1) mod B

in main programme: if PZ FF then:

A:=M[BUF+ FU] FU:=( FU +1) mod B

output data

in main programme: if (FU +B-1) mod B FF then:

M[BUF+ FF]:=A FF:=( FF +1) mod B

in interrupt service routine: if PZ FF then:

A:= M[BUF+ FU] OUT[out_port]:=A

FU:=( FU +1) mod B BUF = basic address of cyclic buffer

Service rules

Buffer with indexes FU & FF of B length is:

- empty if FU = FF;

- full (contains B-1 valid bytes) if (FU+B-1) mod B = FF

I/O devices – data buffering 26/32

Cyclic buffer

ver.2.

one-dimension matrix of recommended length

B=2k

valid data

FU CNT

FU - index (variable) first used byte (with oldest valid data), FU=0..B-1;

CNT - variable contains the number of valid data bytes, CNT=0..B.

I/O devices – data buffering 27/32

Service rules

Buffer with index FU & variable CNT of B length is:

- empty if CNT = 0;

- full (contains B valid bytes) if CNT = B.

writing to buffer reading from buffer

input datain interrupt service routine:

if CNT < B then:A:=IN[in_port]

M[BUF+(FU+CNT) mod B]:=ACNT:=CNT+1

in main programme:if CNT > 0 then:A:=M[BUF+FU]

FU:=(FU+1) mod BCNT:=CNT-1

output datain main programme:

if CNT < B then:M[BUF+(FU+CNT) mod B]:=A

CNT:=CNT+1

in interrupt service routine:if CNT > 0 then:A:= M[BUF+FU]

OUT[out_port]:=AFU:=(FU+1) mod B

CNT:=CNT-1 BUF = basic address of cyclic buffer

I/O devices – data buffering 28/32

I/O devices - data buffering 29/32

Implementation 16B length cyclic buffer ver.1 in MCS51

buf equ 0x30 ;location of buffer tablefu equ 0x40 ;variable FUff equ 0x41 ;variable FFlng equ 16 ;buffer length B=16;writing to buffer from in_port (SBUF) in interrupt service routine mov a,#lng-1 add a,fu ;A:=FU+LNG-1 cjne a,#lng-1,mod1 subb a,#lng ;mod lng mod1: cjne a,ff,notfull;checking buff_full condition sjmp bufffull ;jump if condition is metnotfull: mov a,#buf add a,ff mov r0,a ;R0 points destination in buffer mov @r0,SBUF ;input data transfered to buffer inc ff ;index of next free location in buffer mov a,#lng xrl a,ff ;if new FF = B then A:=0 jnz ffok clr ff ;index FF:=0 because it became equal Bffok: sjmp transferokbuffull:

I/O devices - data buffering 30/32

; buf,ff,fu,lng as above; writing from buffer to out_port (SBUF) in interrupt service routine mov a,fu ;A:=FU xrl a,ff ;if FF=FU then A:=0 jz bufempty ;A=0 means buffer empty mov a,fu add a,#buf mov r0,a ;R0 points the oldest valid data in buffer mov SBUF,@r0 ;data transfered to output port inc fu ;index of next data location in buffer mov a,#lng xrl a,fu ;if new FU = B then A:=0 jnz fuok clr fu ;index FU:=0 because it became equal Bfuok: sjmp transferokbufempty:

I/O devices - data buffering 31/32

Implementation 16B length cyclic buffer ver.2 in MCS51

buf equ 0x30 ;location of buffer tablefu equ 0x40 ;variable FUcnt equ 0x41 ;variable CNTlng equ 16 ;buffer length B=16;writing to buffer from in_port (SBUF) in interrupt service routine mov a,cnt ;A:=cnt xrl a,#lng ;if CNT = B then A:=0 jz buffull ;jump if yes mov a,fu ;determining new destination index add a,cnt cjne a,#lng,mod1mod1: jc notmod subb a,#lng ;index:=index mod Bnotmod: add a,#buf mov r0,a ;R0 points new destination in buffer mov @r0,SBUF ;input data transfered to buffer inc cnt ;incrementing valid data counter sjmp transferokbuffull:

I/O devices - data buffering 32/32

; buf,fu,cnt,lng as above; writing from buffer to out_port (SBUF) in interrupt service routine mov a,cnt ;A:=CNT jz bufempty ;A=0 means buffer empty mov a,fu add a,buf mov r0,a ;R0 points the oldest valid data in buffer mov SBUF,@r0 ;transfer data to SBUF dec cnt inc fu ;modification of FU mov a,fu ;checking if FU=B cjne a,#lng,transferok clr fu ;FU:=0 sjmp transferokbufempty: