12
Module 2.E Buffered I/O Tim Rogers 2017

Module 2 - College of Engineering - Purdue University A buffer [2.C]-4 For output devices -want the CPU to put data in a buffer as soon as it’s ready (don’t wait for the device

  • Upload
    lydung

  • View
    212

  • Download
    0

Embed Size (px)

Citation preview

Module2.EBufferedI/O

TimRogers2017

LearningOutcome#2

How?

A. BusTimingAnalysisB. 9S12CMultiplexedBusExpansionC. General-PurposeI/OPortsD. InterruptHandlingE. BufferedI/OF. Buffered,Interrupt-DrivenPrinterDesignExample

“Anabilitytointerfaceamicrocontrollertovariousdevices”

[2.C]-2

Objective“BufferedI/O” Why?

DevicesaremuchslowerthanCPUs(yes– eventhe9S12)

[2.C]-3

Problems:Dataproduction/consumptionratesaredifferent.

CPUslowedbydevice.EX:CPUhasacharactertoprint- printerisbusy,soCPUmustwait.DatamaybecomingintoCPUbeforetheCPUisreadytodealwithit.

Solution:Abuffer

[2.C]-4

Foroutputdevices- wanttheCPUtoputdatainabufferassoonasit’sready(don’twaitforthedevicetonolongerbebusy).

Thesesimplerelationshiparecalledproducer/consumer.

ProduceData

BufferConsumeData

Solution:ABuffer

[2.C]-5

Forinputdevices- wantthedevicetoplacedatainabufferasit’sproduced.ThentheCPUcangrabitwhentheCPUisready.

Thesesimplerelationshiparecalledproducer/consumer.

ProduceData

BufferConsumeData

CircularBuffers

• Spaceisfiniteandwedonothavedynamicmemorymanagement(i.e.malloc/freeornew/delete).

• Allocateafixedsizeinmemory,then“wraparound”• ManagedwithaFirstinFirstOut(FIFO)policy.

[2.C]-6

Circularbuffers• 2pointersmanagebufferallocationanddeallocation

• IN:pointstonextavailablelocation.Producerputsdatahere.• OUT: pointstonextdataiteminFIFOorder.Consumertakesdatafromhere.• Wearegoingtomanagethebufferwithnoothermeta-data.

[2.C]-7

IN OUT

CircularBuffers

[2.C]-8

INOUT

Emptycondition:IN==OUT

0 12

345

6

7

Example:BufferSize =8

CircularBuffers

[2.C]-9IN

OUT

Contentinbuffer

0 12

345

6

7

Example:BufferSize =8

A B

C

D

CircularBuffers

[2.C]-10

IN

OUT

BufferFull:(IN+1)modBufferSize ==OUT

0 12

345

6

7

Example:BufferSize =8

A B

C

D

F

G

Why?

Can the buffer be full when OUT points to index 3(A) Yes(B) No(C) I really can’t tell

E

CircularBufferAlgorithms• Producerprocess

1.Checkstateofbuffer2.IfbufferisFULL,waitforspace3.Else,placecharacterinbufferatlocationpointedtobyIN4.IncrementINpointermoduloBUFSIZE5.Exit

11

Spin-waitwhenbufferisfull

CircularBufferAlgorithms• ConsumerProcess

1.Checkstateofbuffer2.IfbufferisEMPTY,waitfordata3.Else,getdatafromlocationpointedtobyOUTpointer4.IncrementOUTpointermoduloBUFSIZE5.Exit

12

Spin-waitwhenbufferisempty