\ This is a ring-buffer defined statically --- written as a prototype to the WBUF definer in novice.4th

marker wbuf.4th

1 cells constant w

10 constant records                 \ how many records are in the ring buffer

create base  w records *  allot     \ the buffer
here constant limit                 \ the adr past the buffer

variable data                       \ pointer to data
variable past                       \ pointer to free space past data
variable used                       \ number of records of data

: init-buf ( -- )
     base data !
     base past !
     0    used ! ;
     
init-buf

: next-record { size ptr -- }     
    size ptr +!
    ptr @  limit = if  base ptr !  then ;
    
: room-wbuf ( -- empty-slots )
    records  used @  - ;
    
: data-wbuf ( -- occupied-slots )        
    used @ ;
        
: wbuf! ( record -- )
    used @  records = abort" *** wbuf overflowed ***"
    past @ !  1 used +!  w past next-record ;

: wbuf@ ( -- record )
    used @  0= abort" *** wbuf underflowed ***"
    data @ @  -1 used +!  w data next-record ;