home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
forth
/
compiler
/
love
/
chap19.doc
< prev
next >
Wrap
Text File
|
1993-04-11
|
9KB
|
218 lines
Self-fetching Array Word Set with Bounds Checking
This word set offers a set of self fetching arrays, both one
and two dimensional (VECTOR and MATRIX). Each use (fetch, store,
or plus-store) takes only two bytes in the thread segment.
Organizations of byte, cell, and double are available. A fixed length
string vector is also available. All array indexes are zero based.
These words require the multiple-cfa word set (MCFA.TXT) to be loaded
in advance.
Operation
These arrays are declared just as any variable, with the size
passed in. These arrays may have up to 65535 elements. VECTORs are
one-dimensional and have a single number of elements passed in,
MATRIXs have two dimensions and two sizes (rows,columns) are passed in.
For example:
365 VECTOR DayFlags ( one flag for each day)
7 VECTOR Visits/Day ( visits per day)
10 10 DMATRIX HundredElementSquare
3 3 MATRIX TicTacToeBoard
Data can subsequently be stored into the arrays with prefixes
such as )-> )C-> etc. Data are fetched simply by naming the array.
In each case the array index(es) are passed in. The data type *VECTOR,
is for storing a vector of fixed length strings and is not
self-fetching. It returns the segment and offset of the string
on the stack. The storage word )*MOVEL> is available to store a string
into the reference array.
CVECTOR,VECTOR,DVECTOR declaration:
( elements-- )
array declared with: fetch stack:
CVECTOR arrayname arrayname ( index--c )
(plus) store stack:
)C-> arrayname ( c,index-- )
)C+> arrayname ( c,index-- )
array declared with: fetch stack:
VECTOR arrayname arrayname ( index--n )
(plus) store stack:
)-> arrayname ( n,index-- )
)+> arrayname ( n,index-- )
array declared with: fetch stack:
DVECTOR arrayname arrayname ( index--d )
(plus) store stack:
)D-> arrayname ( d,index-- )
)D+> arrayname ( d,index-- )
*VECTOR declaration:
( elements, bytes -- )
array declared with: fetch stack:
*VECTOR stringarrayname arrayname ( index -- seg,ofs )
store stack:
)*MOVEL> stringarrayname ( seg,ofs,index -- )
CMATRIX,MATRIX,DMATRIX declaration:
( rows,columns-- )
array declared with: fetch stack:
CMATRIX arrayname arrayname ( row,col--c )
(plus) store stack:
]C-> arrayname ( c,row,col-- )
]C+> arrayname ( c,row,col-- )
array declared with: fetch stack:
MATRIX arrayname arrayname ( row,col--n )
(plus) store stack:
]-> arrayname ( n,row,col-- )
]+> arrayname ( n,row,col-- )
array declared with: fetch stack:
DMATRIX arrayname arrayname ( row,col--d )
(plus) store stack:
]D-> arrayname ( d,row,col--)
]D+> arrayname ( d,row,col--)
Examples:
365 VECTOR DayFlags ( one flag for each day)
: SET-DAY-FLAG ( day-- )
-1 SWAP )-> DayFlags ;
: CHECK-DAY-FLAG ( day -- )
DayFlags
IF ." Set" THEN ;
30 VECTOR jail ( 30 cells in jail)
: FILL10CELLS ( clear ten cells in jail ( -- )
10 0
DO -1 I )-> jail ( store true flag into cell) LOOP ;
25 80 *VECTOR instruction-screen ( one screen-full of text)
: ENTER-INSTRUCTIONS ( -- )
25 0
DO TIB 80 BL FILL ( pre-fill to blanks )
TIB 80 EXPECT ( get a line of text)
SPAN @ 0= ( blank line exits)
IF LEAVE THEN
GET:VS TIB I )*MOVEL> instruction-screen
LOOP ;
Declaration
The data space of these arrays is allocated outside the
L.O.V.E. Forth memory map. When allocated during array declaration,
the actual starting memory is relative to the variable HEAPSEG. This
is usually set from the L.O.V.E. Forth segment: GET:HEAP prior to
using the arrays, allowing the arrays to exist above the usual memory
map (in the heap). In order to use memory above the usual L.O.V.E.
Forth memory map, variables MINHEAP and MAXHEAP must be set
appropriately. See instructions for saving the system.
During array declaration only, VARIABLEs alloc.addr and
alloc.seg are used. They contain the "dictionary pointer" for the
allocation. They are initialized to 0 and then are updated after each
array declaration.
alloc.seg contains the relative segment of the
start for the next array
(ie. relative to HEAPSEG)
alloc.ofs contains the address relative to alloc.seg
Automatic Fill Words
A common operation for any array is to fill it with a
particular number, usually zero. A set of words performs this
function. No sizes need be specified, the array is filled based on its
declared size.
To fill a CVECTOR VECTOR DVECTOR or *VECTOR with zero use:
(0)C-> (0)-> (0)D-> and (0)*-> respectively.
For CMATRIX MATRIX and DMATRIX
[0]C-> [0]-> and [0]D-> are used. All have stack action
( -- ).
To fill an array with a particular number the words
(N)C-> (N)-> (N)D-> and (N)*->
are used for the VECTORs and
[N]C-> [N]-> [N]D->
are used with the MATRIXes. A byte is passed into
(N)C-> (N)*-> and (N)C-> , a single number is passed into
(N)-> and [N]-> , and a double number into (N)D-> and [N]D->.
Examples (with declarations from above):
(0)-> jail ( clear jail)
BL (N)*-> instruction-screen ( blank screen contents)
1. [N]D-> HundredElementSquare ( unit matrix )
Other words
SEG:OFS> returns the address of the array following, For
example to display the actual location in RAM of an array
above:
HEX SEG:OFS> jail U. U. DECIMAL
MAT-EVENPARA operates during declaration to ensure that the
next array starts on an even paragraph, by adjusting alloc.seg
and alloc.addr . Several bytes may be wasted.
Bounds Checking
A bounds error occurs when an invalid index is used. If for
example, the program tries to store element 100 in a 50 element array,
a bounds error occurs, as a 50 element array only supports indexes 0 to
49. On a boundary error during a store or plus-store operation,
nothing is actually stored or changed in the array. Error flags are
set, as described above. On a boundary error during a fetch, 0 is
returned (or double 0. in the case of a double array). For string
vectors, the address of the first element is returned. This action is
intended for real time and embedded programs, where execution cannot
simply "ABORT".
When such errors occur, bit flags are set in the VARIABLE
bound.err which is a 3 cell array. The contents of this can be printed
with BOUND. in the current base.
first number VECTOR errors
bit description
--- -----------
1 on CVECTOR fetch
2 on CVECTOR store )C->
4 on CVECTOR plus-store )C+>
10 on VECTOR fetch
20 on VECTOR store )->
40 on VECTOR plus-store )+>
100 on DVECTOR fetch
200 on DVECTOR store )D->
400 on DVECTOR plus-store )D+>
8 on *VECTOR
second number MATRIX row errors
bit description
--- -----------
1 on CMATRIX row fetch
2 on CMATRIX row store ]C->
4 on CMATRIX row plus-store ]C+>
10 on MATRIX row fetch
20 on MATRIX row store ]->
40 on MATRIX row plus-store ]+>
100 on DMATRIX row fetch
200 on DMATRIX row store ]D->
400 on DMATRIX row plus-store ]D+>
third number MATRIX column errors
(as the above for row errors)