An example of using an array to create a memory structure holding 3 distinct items, all pertaining to the use of a First In First Out (FIFO) circular memory buffer.
The supporting Procedures/Functions work with multiple instances of FIFO's
#chip tiny85, 16
#DEFINE FIFO1_Length = 4 ; Must be 2,4,8,16,32,64,128,256,...
; Only (length-1) bytes used for storage.
; FIFO - First In First Out circular memory buffer
; FIFO Structure: Offset +0 (GCB created, but not used)
; FIFO Head.....: Offset +1
; FIFO Tail.....: Offset +2
; FIFO Buffer(1): Offset +3
; FIFO Buffer(n): Offset n+3
#DEFINE FIFO_Head = 1 ; Holds pointer to where data is written
#DEFINE FIFO_Tail = 2 ; Holds pointer from where data is read
#DEFINE FIFO_Buffer = 3 ; Start of the physical FIFO buffer
DIM FIFO1(FIFO1_Length+3) ; Create FIFO structure
DIM FIFO_Pointer AS WORD ; Pointer to FIFO structure
DIM FIFO_Value AS BYTE ; Used by FIFO_Read and FIFO_Write
DIM FIFO_Data AS BYTE ; Data read from FIFO buffer
FIFO_Pointer = @FIFO1 ; Point to memory address of FIFO structure
FIFO_Init ; Logically clear FIFO buffer
; FIFO_Head = FIFO_Tail
FIFO_Write_Buffer( 1 ) ; 1 written to FIFO
FIFO_Write_Buffer( 2 ) ; 2 written to FIFO
FIFO_Write_Buffer( 3 ) ; 3 written to FIFO
FIFO_Write_Buffer( 4 ) ; FIFO full, 4 NOT written to FIFO
FIFO_Data = FIFO_Read_Buffer() ; 1 read from FIFO
FIFO_Write_Buffer( 5 ) ; 5 written to FIFO
FIFO_Write_Buffer( 6 ) ; FIFO full, 6 NOT written to FIFO
FIFO_Data = FIFO_Read_Buffer() ; 2 read from FIFO
FIFO_Data = FIFO_Read_Buffer() ; 3 read from FIFO
FIFO_Data = FIFO_Read_Buffer() ; 5 read from FIFO
FIFO_Data = FIFO_Read_Buffer() ; FIFO empty, 0 returned
END
SUB FIFO_Init
FIFO_Write(FIFO_Head, 0)
FIFO_Write(FIFO_Tail, 0)
END SUB
FUNCTION FIFO_Read( IN FIFO_Offset AS WORD )
FIFO_Read = PEEK(FIFO_pointer + FIFO_Offset)
END FUNCTION
SUB FIFO_Write( IN FIFO_Offset AS WORD, IN FIFO_Value AS BYTE)
POKE((FIFO_pointer + FIFO_Offset), FIFO_Value )
END SUB
SUB FIFO_Write_Buffer( IN FIFO_Data AS BYTE )
; Wrap around from end of physical buffer to start of physical
; buffer as required. E.g. FIFO Buffer = 4 Bytes
;
; b00000010 2 b00000100 4 FIFO_Head
; & b00000011 3 & b00000011 3 ( FIFO1_Length - 1 )
; ---------------- ---------------
; b00000010 2 b00000000 0
FIFO_Value = ( FIFO_Read( FIFO_Head ) + 1 ) & ( FIFO1_Length - 1 )
IF FIFO_Value <> FIFO_Read( FIFO_Tail ) THEN ; Is FIFO full ?
FIFO_Write( FIFO_Head, FIFO_Value ) ; No
FIFO_Write( (FIFO_Buffer + FIFO_Value ), FIFO_Data )
END IF
END SUB
FUNCTION FIFO_Read_Buffer
FIFO_Value = FIFO_Read( FIFO_Tail )
IF FIFO_Value <> FIFO_Read( FIFO_Head ) THEN ; Is FIFO empty ?
; Wrap around from end of physical buffer to start of physical
; buffer as required. E.g. FIFO Buffer = 4 Bytes
;
; b00000010 2 b00000100 4 FIFO_Tail
; & b00000011 3 & b00000011 3 ( FIFO1_Length - 1 )
; ---------------- ---------------
; b00000010 2 b00000000 0
FIFO_Value = ( FIFO_Value + 1 ) & ( FIFO1_Length - 1 ) ; No
FIFO_WRITE( FIFO_Tail, FIFO_Value )
FIFO_Read_Buffer = FIFO_Read( FIFO_Buffer + FIFO_Value )
ELSE
FIFO_Read_Buffer = 0
END IF
END FUNCTION
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
An example of using an array to create a memory structure holding 3 distinct items, all pertaining to the use of a First In First Out (FIFO) circular memory buffer.
The supporting Procedures/Functions work with multiple instances of FIFO's
Very nice. Thank you for sharing.