VFX Forth harness





The harness files permit you to optimise the FAT file system for speed or code size.




Sector handling

How you code these will depend on the host CPU and Forth implementation.

: secs>bytes    \ u -- #byes
Multiply a sector number by the number of bytes per sector. Optimise this for performance.

: bytes>secs    \ #bytes -- #secs
Divide a number of bytes by the number of bytes per sector, returning the number of complete sectors. Optimise this for performance.

: bytes>offsecs \ #bytes -- offset #secs
Divide a number of bytes by the number of bytes per sector, returning the offset in a sector and the number of complete sectors. Optimise this for performance.




Tools

These tools should be compiled as required.

: buffer:       \ u -- ; -- addr
Reserve u bytes of uninitialised read/write data.

: scan          \ addr len char -- addr' len'
Find char in string.

: upc           \ char -- char'
Convert char to upper case.

: FOR           \ -- ; n -- ; R: -- n'
Starts a loop that is executed one or more times. At runtime, the loop is entered with the loop count on the stack. In the body of the loop the loop index is on the return stack and counts down from n-1 to zero. Use in the form:

 ( -- n ) FOR ... NEXT ( -- )

: NEXT          \ -- ; R: n -- ; -- [n]
Terminates a [m ... m] structure, discarding the loop index.

: ucompare      \ a1 u1 a2 u2 -- notsame?
Case insensitive string compare. Returns zero for a match.

: ucmove        \ src dest len --
As CMOVE but characters are converted to upper case.

: N++           \ n1 x -- n1+1 x
Increment n1.

: byte-split    \ w -- lo hi
Split a 16 bit item into its low and high bytes.

: byte-join     \ lo hi -- w
Convert two bytes into a 16 bit item.

: word-split    \ x -- wlo whi
Split a 32 bit item into its low and high 16 bit items.

: word-join     \ wlo whi -- x
Convert two 16 bit items into a 32 bit item.

: qlog2         \ n -- log2[n]
Given n, a power of two, return its power.

: @C @ ;
Fetch cell from code space (e.g. for tables).




Alignment checking

Some CPUs require aligned memory accesses. The code here checks alignment at run-time.




Embedded systems compatibility

Embedded systems may have words TARGET and HOST that may be ignored.

[undefined] target [if]
: target ;
: host ;
[then]

: cvariable  variable  ;        \ -- ; -- addr
In embedded systems, CVARIABLE can be used to reduce RAM use.




Unaligned memory access

Systems that require aligned accesses for 16 and 32 bit values must provide unaligned access words. Similarly, big-endian CPUs must provide little-endian access as all the FAT data structure items are little-endian items.