File Control Blocks








Variables and buffers

SYS_NFCB FLD-REC * equ FCB-SIZE \ -- len
Size of FCB table.

FCB-SIZE buffer: FCB-TABLE      \ -- addr
RAM table containing FCBs loaded from disk FLDs.

TASK_NCFILE open-table-struct * equ OPENT-SIZE  \ -- len
Size of open file table.

OPENT-SIZE buffer: OPEN-TABLE   \ -- addr
RAM table of open file structures.

FLD-REC cell - equ FCB_DATA_REC \ -- len
Size of data in an FCB/FLD. The first element in the structure must be preserved as it is the link field.

FLD-REC buffer: cdir-buffer     \ -- addr
Scratch FCB/FLD buffer.

variable active-fcbs    \ -- addr
Anchors chain of FCBs in use.

variable free-fcbs      \ -- addr
Anchors chain of unused FCBs.

variable active-otbs    \ -- addr
Anchors chain of open file structures in use.

variable free-otbs      \ -- addr
Anchors chain of unused open file structures.




Read/Write FLDs

: fld-d-block   \ fld -- blk
Returns disk block of required FLD.

: fld-m-block   \ fld -- offset
Returns block offest of required FLD

: rewrite-fld   { l_fld l_buff -- t/f }
Update specific FLD to disk via cache

: read-in-fld { l_fld l_buff | -- t/f }
Gead in specific FLD from disk via cache




FCB primitives

: existing-fcb? \ fld# -- struct|0
If the FLD number is already in RAM, return its address, otherwise return zero.

: get-fcb       \ -- struct|0
Get a new FCB structure and return its address or 0 if no free FCBs are available.

: create-fcb    { l_fld | l_fcb -- FCB|0 }
Return file control block address if successful, otherwise return 0.

: release-fcb   \ struct --
Put an FCB structure back on the free chain; exit quietly if the FCB is not on the active chain.

: dec-fcb-open  \ fcb -- t/f
Decrement the number of open instances of this FCB. Returns true if all access closed, i.e. no instance is still open.

: init-fcbs     \ --
Initialise the table of FCBs.




Open Table Blocks (OTBs)

Because a file may have been opened more than once, some data must be held for each open instance. An Open Table Block holds the instance data and the address of the FCB for the file. The OTB address is used as handle (FID) of the file.

: init-otbs     \ --
Initialise the OTB structures.

: create-otb     \ -- fid|0
Get a free OTB and return its address or 0 if one cannot be found.

: release-otb   \ fid --
Put the OTB back on the free chain if it is active, otherwise do nothing.

: fcb-addr      \ fid -- fcb
COMPILER: Returns the FCB address from the FID.

: fid-file-size \ fid -- size
COMPILER: Returns the file size given the FID.

: set-f-r/w-ptr \ value fid --
COMPILER: Set the file read/write pointer.

: get-f-r/w-ptr \ fid -- value
Get the file read/write pointer.

: get-r/w-b-ptr \ fid -- block
Get the n-th block in the file which includes the data at the read/write pointer.

: get-r/w-pb-ptr        \ fid -- offset
Get the offset in the block in the file which includes the data at the read/write pointer.

: inc-f-r/w-ptr \ step fid --
Add step to the file's read/write pointer.

: r/w-ptr-inrange?      \ fid -- t/f
Return true if the file read/write pointer is within the number of allocated blocks.

: r-ptr-inrange?        \ fid -- t/f
Return true if the file read/write pointer is within the current file size.