




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





Slide 1
& '
% $
Chris J. Myers
Lecture 8: FIFOs
Slide 2
& '
% $
Introduction to FIFOs
(^) FIFO circular queue is useful for a buffered I/O interface.
data created by producer before processed by consumer.^ This order-preserving data structure temporarily saves
(^) Decouples the producer from the consumer.
shared by threads, but must be accessed carefully.^ Use statically allocated global memory, so they can be
1
Slide 3
& '
Produce-Consumer Examples
Source/producer
Sink/consumer
Keyboard input
Program that interprets
Program with data
Printer output
Program sends message
Program receives message
Microphone and ADC
Program that saves sound data
Program that has sound data
DAC and speaker
Slide 4
& '
Two-Pointer FIFO
2
Slide 5
& '
% $
Basic Idea of FIFO
GetPt
rmb 2
Ptr to oldest data, next to be removed
PutPt
rmb 2
Ptr to free memory, place to put next
PutFifo* Reg A contains byte to store into the FIFO
ldx
PutPt
Reg X points to free place
staa ,X
Store data into FIFO
inx
Update pointer
stx
PutPt
rts
GetFifo* Reg A returned with byte from FIFO
ldx
GetPt
Reg X points to oldest data
ldaa ,X
Read data from FIFO
inx
Update pointer
stx
GetPt
rts
Slide 6
& '
% $
Initialization of a Two-Pointer FIFO in Assembly
FifoSize
equ
Size of Fifo
PutPt
rmb
Pointer where to put next */
GetPt
rmb
Pointer where to get next
empty if PutPt=GetPt
if PutPt+1=GetPt (with wrap)
Fifo
rmb
FifoSize
The fifo data
InitFifo
seitpaldx #Fifo
entering critical section
stx GetPtstx PutPt
Empty when PutPt=GetPt
tap
end critical section
rts
3
Slide 7
& '
Initialization of a Two-Pointer FIFO in C
#define FifoSize 10
/* Size of Fifo */
char *PutPt;
/* Ptr of where to put next */
char *GetPt;
/* Ptr of where to get next
/* FIFO is full/* FIFO is empty if PutPt=GetPt */
if PutPt+1=GetPt (with wrap) */
char Fifo[FifoSize];
/* The fifo data */
void InitFifo(void) { char SaveSP; asm(" ldaa %SaveSP\n tap"); /* end critical /PutPt=GetPt=&Fifo[0]; / Empty when PutPt=GetPt /asm(" tpa\n staa %SaveSP\n sei"); / critical */
Slide 8
& '
PutFifo for a Two-Pointer FIFO in Assembly
RegA contains 8 bit data to put
PutFifo* Output RegA is -1 if successful, 0 otherwise
pshbpulatabtpapsha
save old CCR
sei
critical section
ldx
PutPt
Temporary put pointer
staa 0,x
Try to put data into fifo
cpxinx
#Fifo+FifoSize
4
Slide 13
& '
% $
GetFifo for a Two-Pointer FIFO in C
int GetFifo (char *datapt) { char SaveSP; if (PutPt == GetPt ) { return(0);}
/* Empty if PutPt=GetPt */
else{ return(-1);asm(" ldaa %SaveSP\n tap"); /* end critical /if (GetPt == &Fifo[FifoSize]) GetPt = &Fifo[0];datapt=(GetPt++);asm(" tpa\n staa %SaveSP\n sei"); / critical*/
Slide 14
& '
% $
Initialization of a Two-Ptr/Cnt FIFO in Assembly
FifoSize
equ
Size of Fifo
PutPt
rmb
Pointer of where to put next
GetPt
rmb
Pointer of where to get next
Size
rmb
Fifo
rmb
FifoSize
fifo data
InitFifo
seitpa
critical section
ldx
#Fifo
stx
PutPt
stx
GetPt
Empty when Size == 0
clr
Size
tap
end critical section
rts
7
Slide 15
& '
Initialization of a Two-Pointer/Counter FIFO in C
#define FifoSize 10
/* Size of Fifo */
char *PutPt;
/* Ptr of where to put next */
char *GetPt;
/* Ptr of where to get next */
unsigned char Size;
/* # of elements in FIFO */
/* FIFO is full/* FIFO is empty if Size=0 */
if Size=FifoSize */
char Fifo[FifoSize];
/* fifo data */
void InitFifo(void) { char SaveSP; PutPt=GetPt=&Fifo[0];asm(" tpa\n staa %SaveSP\n sei"); /* critical*/
/* Empty when Size==0 */
asm(" ldaa %SaveSP\n tap"); /* end critical */Size=0;
Slide 16
& '
PutFifo for a Two-Ptr/Cnt FIFO in Assembly
RegA contains 8 bit data to put
PutFifo* Output RegA is -1 if successful, 0 otherwise
pshbpulatabtpapsha
save old CCR
sei
critical section
cmpb #FifoSizeldab Size
Full if Size==FifoSize
bne
PutNotFull
braclra
PutDone
8
Slide 17
& '
% $
PutNotFull incb PutFifo for a Two-Ptr/Cnt FIFO in Assembly (cont)
stab Size
Size++
ldx
PutPt
staa 0,x
Put data into fifo
cpxinx
#Fifo+FifoSize
bne
PutNoWrap
skip if no wrap needed
ldx
#Fifo
Wrap
PutNoWrap
ldaa #-
success means OK
stx
PutPt
PutDone
tab
end critical section
tpapula
restore CCR to previous value
rtstba
Slide 18
& '
% $
PutFifo for a Two-Pointer/Counter FIFO in C
int PutFifo (char data) { char SaveSP; if (Size == FifoSize ) { return(0);}
/* Failed, fifo was full */
else{ (PutPt++)=data;Size++;asm(" tpa\n staa %SaveSP\n sei"); / critical*/
/* put data into fifo */
return(-1);asm(" ldaa %SaveSP\n tap"); /* end critical */if (PutPt == &Fifo[FifoSize]) PutPt = &Fifo[0];
/* Successful */
9
Slide 19
& '
GetFifo for a Two-Ptr/Cnt FIFO in Assembly
RegX points to place for 8 bit data
GetFifo tpa* Output RegA is -1 if successful, 0 if Fifo empty
psha
save old CCR
sei
critical section
clra
assume it will fail
tst
Size
beq
GetDone
dec
Size
ldy
GetPt
coma
RegA=-1 means OK
ldab 0,y
Data from FIFO
stab 0,x
Return by reference
Slide 20
& ' GetFifo for a Two-Ptr/Cnt FIFO in Assembly (cont)
cpy iny
#Fifo+FifoSize
bne
GetNoWrap
skip if no wrapping needed
ldy
#Fifo
Wrap
GetDone tabGetNoWrap sty GetPt
end critical section
tpapula
restore CCR to previous value
rtstba
10
Slide 25
& '
% $
PutFifo for a Index FIFO in Assembly (cont)
staa 0,xabx ldab PutI
Put data into fifo
bnecmpb #FifoSizeincb
PutNoWrap skip if no wrap needed
clrb
Wrap
PutNoWrap
clra
sucess
coma
RegA=-1 means OK
stab PutI
PutDone
tab
end critical section
tpapula
restore CCR to previous value
rtstba
Slide 26
& '
% $
PutFifo for a Index FIFO in C
int PutFifo (char data) { char SaveSP; if (Size == FifoSize ) return(0);
/* Failed, fifo was full */
else{ Fifo[PutI++]=data; /Size++;asm(" tpa\n staa %SaveSP\n sei"); / critical*/
put data into fifo */
if (PutI == FifoSize) PutI = 0;
/* Wrap */
asm(" ldaa %SaveSP\n tap");
/* end critical */
return(-1);
/* Successful */
13
Slide 27
& '
GetFifo for a Index FIFO in Assembly
RegX points to place for 8 bit data
GetFifo* Output RegA is -1 if successful, 0 if Fifo empty
pshatpa
save old CCR
sei
entering critical section
clra
assume it will fail
tst
Size
beq
GetDone
dec
Size
ldy
#Fifo
comaabyldab GetI
RegA=-1 means OK
ldab 0,y
Data from FIFO
Slide 28
& '
GetFifo for a Index FIFO in Assembly (cont)
stab 0,x
Return by reference
bnecmpb #FifoSizeincbldab GetI
GetNoWrap skip if no wrap needed
clrb
Wrap
GetNoWrap
stab GetI
GetDone
tab
end critical section
tpapula
restore CCR to previous value
rtstba
14
Slide 29
& '
GetFifo for a Index FIFO in C
int GetFifo (char *datapt) { char SaveSP; if (Size == 0 ) return(0);
/* Empty if Size=0 */
else{ return(-1);asm(" ldaa %SaveSP\n tap"); /* end critical /if (GetI == FifoSize) GetI = 0;datapt=Fifo[GetI++]; Size--;asm(" tpa\n staa %SaveSP\n sei"); /* critical*/
Slide 30
& '
FIFO Dynamics
(^) Rates of production/consumption vary dynamically.
(^) t p is time b/w
(^) PutFifo
(^) calls,
(^) r p is arrival rate (
pr (^) = t^1 p (^) ).
(^) t g is time b/w
(^) GetFifo
(^) calls,
(^) r g is service rate (
gr (^) =
g t^1 ).
(^) If min
(^) t p ≥ (^) max
(^) t g, FIFO is not necessary.
temporarily descrease, then a FIFO is necessary.^ If arrival rate can temporarily increase or service rate
rate (i.e.,^ If average production rate exceeds average consumption
(^) r p
(^) r g), then FIFO will overflow.
(^) A full error is serious because if ignored data is lost.
(^) An empty error is usually not serious.
15