1 \ @(#) misc2.fth 98/01/26 1.2
2 \ Utilities for PForth extracted from HMSL
5 \ Copyright 1994 3DO, Phil Burk, Larry Polansky, David Rosenboom
7 \ Permission to use, copy, modify, and/or distribute this
8 \ software for any purpose with or without fee is hereby granted.
10 \ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11 \ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12 \ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
13 \ THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
14 \ CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
15 \ FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
16 \ CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 \ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 \ 00001 9/14/92 Added call, 'c w->s
20 \ 00002 11/23/92 Moved redef of : to loadcom.fth
24 : 'N ( <name> -- , make 'n state smart )
28 IF namebase - ( make nfa relocatable )
29 [compile] literal ( store nfa of word to be compiled )
35 : ?LITERAL ( n -- , do literal if compiling )
41 : 'c ( <name> -- xt , state sensitive ' )
47 : ? ( address -- , fatch from address and print value )
52 create MSEC-DELAY 100000 , \ calibrate this for your system
53 : (MSEC.SPIN) ( #msecs -- , busy wait, not accurate )
54 0 max \ avoid endless loop
61 : (MSEC) ( millis -- )
62 dup (sleep) \ call system sleep in kernel
64 ." (SLEEP) failed or not implemented! Using (MSEC.SPIN)" CR
73 \ (SLEEP) uses system sleep functions to actually sleep.
74 \ Use (MSEC.SPIN) on embedded systems that do not support Win32 Sleep() posix usleep().
76 ." (SLEEP) failed or not implemented! Use (MSEC.SPIN) for MSEC" CR
82 : MS ( msec -- , sleep, ANS standard )
86 : SHIFT ( val n -- val<<n )
93 variable rand-seed here rand-seed !
94 : random ( -- random_number )
97 65535 and dup rand-seed !
99 : choose ( range -- random_number , in range )
103 : wchoose ( hi lo -- random_number )
108 \ sort top two items on stack.
109 : 2sort ( a b -- a<b | b<a , largest on top of stack)
115 \ sort top two items on stack.
116 : -2sort ( a b -- a>b | b>a , smallest on top of stack)
122 : barray ( #bytes -- ) ( index -- addr )
127 : warray ( #words -- ) ( index -- addr )
132 : array ( #cells -- ) ( index -- addr )
137 : .bin ( n -- , print in binary )
138 base @ binary swap . base !
141 base @ decimal swap . base !
144 base @ hex swap . base !
147 : B->S ( c -- c' , sign extend byte )
150 [ $ 0FF invert ] literal or
155 : W->S ( 16bit-signed -- cell-signed )
158 [ $ 0FFFF invert ] literal or
164 : WITHIN { n1 n2 n3 -- flag }
175 : MOVE ( src dst num -- )
184 : ERASE ( caddr num -- )
193 : BLANK ( addr u -- , set memory to blank )
202 \ Obsolete but included for CORE EXT word set.
203 : QUERY REFILL DROP ;
205 : EXPECT accept span ! ;
209 : UNUSED ( -- unused , dictionary space )
213 : MAP ( -- , dump interesting dictionary info )
215 ." CODEBASE = " codebase .hex cr
216 ." HERE = " here .hex cr
217 ." CODELIMIT = " codelimit .hex cr
218 ." Compiled Code Size = " here codebase - . cr
219 ." CODE-SIZE = " code-size @ . cr
220 ." Code Room UNUSED = " UNUSED . cr
222 ." NAMEBASE = " namebase .hex cr
223 ." HEADERS-PTR @ = " headers-ptr @ .hex cr
224 ." NAMELIMIT = " namelimit .hex cr
225 ." CONTEXT @ = " context @ .hex cr
226 ." LATEST = " latest .hex ." = " latest id. cr
227 ." Compiled Name size = " headers-ptr @ namebase - . cr
228 ." HEADERS-SIZE = " headers-size @ . cr
229 ." Name Room Left = " namelimit headers-ptr @ - . cr
233 \ Search for substring S2 in S1
234 : SEARCH { addr1 cnt1 addr2 cnt2 | addr3 cnt3 flag -- addr3 cnt3 flag }
235 \ ." Search for " addr2 cnt2 type ." in " addr1 cnt1 type cr
236 \ if true, s1 contains s2 at addr3 with cnt3 chars remaining
248 addr1 i j + chars + c@ <> \ mismatch?
256 addr1 i chars + -> addr3
267 : env= ( c-addr u c-addr1 u1 x -- x true true | c-addr u false )
268 { x } 2over compare 0= if 2drop x true true else false then
271 : 2env= ( c-addr u c-addr1 u1 x y -- x y true true | c-addr u false )
272 { x y } 2over compare 0= if 2drop x y true true else false then
275 0 invert constant max-u
276 0 invert 1 rshift constant max-n
280 : ENVIRONMENT? ( c-addr u -- false | i*x true )
281 s" /COUNTED-STRING" 255 env= if exit then
282 s" /HOLD" 128 env= if exit then \ same as PAD
283 s" /PAD" 128 env= if exit then
284 s" ADDRESS-UNITS-BITS" 8 env= if exit then
285 s" FLOORED" false env= if exit then
286 s" MAX-CHAR" 255 env= if exit then
287 s" MAX-D" max-n max-u 2env= if exit then
288 s" MAX-N" max-n env= if exit then
289 s" MAX-U" max-u env= if exit then
290 s" MAX-UD" max-u max-u 2env= if exit then
291 s" RETURN-STACK-CELLS" 512 env= if exit then \ DEFAULT_RETURN_DEPTH
292 s" STACK-CELLS" 512 env= if exit then \ DEFAULT_USER_DEPTH
293 \ FIXME: maybe define those: