/* ****************SFIFO.C******** Jonathan W. Valvano Feb 26, 1997 FIFO using semaphores */ unsigned int *PutPt; /* Pointer of where to put next */ unsigned int *GetPt; /* Pointer of where to get next /* FIFO is empty when Available is less than or equal to zero */ /* FIFO is full when RoomLeft is less than or equal to zero */ int RoomLeft; /* Semaphore counting empty spaces */ int Available; /* Semaphore counting data in fifo */ unsigned int Fifo[FifoSize]; /* The statically allocated fifo data */ void InitFifo(void) { RoomLeft=FifoSize-1; /* Maximum storage capability */ Available=0; /* Initially empty */ PutPt=GetPt=&Fifo[0]; } void Put(unsigned int data) { Wait(&RoomLeft); asm(" sei"); // read modify write nonreentrant code PORTC|=1; *(PutPt++)=data; // Put data into fifo if (PutPt==&Fifo[FifoSize]) PutPt = &Fifo[0]; PORTC&=0xFE; asm(" cli"); Signal(&Available); } unsigned int Get(void) { unsigned int data; Wait(&Available); asm(" sei"); // read modify write nonreentrant code PORTC|=2; data=*(GetPt++); if (GetPt==&Fifo[FifoSize]) GetPt = &Fifo[0]; PORTC&=0xFD; asm(" cli"); Signal(&RoomLeft); return(data);}