2020-03-27 08:23:45 -04:00
|
|
|
( Inner core. This unit represents core definitions that
|
|
|
|
happen right after native definitions. Before core.fs.
|
|
|
|
|
|
|
|
Unlike core.fs and its followers, this unit isn't self-
|
|
|
|
sustained. Like native defs it uses the machinery of a
|
|
|
|
full Forth interpreter, notably for flow structures.
|
|
|
|
|
|
|
|
Because of that, it has to obey specific rules:
|
|
|
|
|
|
|
|
1. It cannot compile a word from higher layers. Using
|
|
|
|
immediates is fine though.
|
|
|
|
2. If it references a word from this unit or from native
|
|
|
|
definitions, these need to be properly offsetted
|
|
|
|
because their offset at compile time are not the same
|
|
|
|
as their runtime offsets.
|
|
|
|
3. Anything they refer to in the boot binary has to be
|
|
|
|
properly stabilized.
|
|
|
|
4. Make sure that the words you compile are not overridden
|
|
|
|
by the full interpreter.
|
2020-03-30 13:50:53 -04:00
|
|
|
5. When using words as immediates, make sure that they're
|
|
|
|
not defined in icore or, if they are, make sure that
|
2020-04-09 08:23:53 -04:00
|
|
|
they are *not* offsetted
|
2020-03-30 13:50:53 -04:00
|
|
|
|
2020-04-09 08:23:53 -04:00
|
|
|
Those rules are mostly met by the "xcomp" unit, which is
|
|
|
|
expected to have been loaded prior to icore and redefines
|
|
|
|
":" and other defining words. So, in other words, when
|
|
|
|
compiling icore, ":" doesn't means what you think it means,
|
|
|
|
go look in xcomp.
|
2020-03-27 08:23:45 -04:00
|
|
|
)
|
|
|
|
|
2020-04-02 09:58:02 -04:00
|
|
|
: RAM+
|
2020-04-09 08:23:53 -04:00
|
|
|
[ RAMSTART LITN ] +
|
2020-03-30 14:29:21 -04:00
|
|
|
;
|
|
|
|
|
2020-04-09 08:23:53 -04:00
|
|
|
: FLAGS 0x08 RAM+ ;
|
|
|
|
: (parse*) 0x0a RAM+ ;
|
|
|
|
: HERE 0x04 RAM+ ;
|
2020-04-09 12:01:08 -04:00
|
|
|
: CURRENT* 0x51 RAM+ ;
|
|
|
|
: CURRENT CURRENT* @ ;
|
2020-03-30 15:11:23 -04:00
|
|
|
|
2020-04-09 11:20:54 -04:00
|
|
|
( w -- a f )
|
|
|
|
: (find) CURRENT @ SWAP _find ;
|
|
|
|
|
2020-03-30 13:50:53 -04:00
|
|
|
: QUIT
|
2020-04-09 08:23:53 -04:00
|
|
|
0 FLAGS ! (resRS)
|
|
|
|
LIT< INTERPRET (find) DROP EXECUTE
|
2020-03-30 13:50:53 -04:00
|
|
|
;
|
2020-03-30 08:25:22 -04:00
|
|
|
|
2020-04-09 08:23:53 -04:00
|
|
|
: ABORT (resSP) QUIT ;
|
2020-03-28 10:11:52 -04:00
|
|
|
|
2020-04-09 08:23:53 -04:00
|
|
|
: = CMP NOT ;
|
|
|
|
: < CMP -1 = ;
|
|
|
|
: > CMP 1 = ;
|
2020-03-31 15:04:28 -04:00
|
|
|
|
2020-04-15 10:41:27 -04:00
|
|
|
( r c -- r f )
|
|
|
|
( Parse digit c and accumulate into result r.
|
|
|
|
Flag f is 0 when c was a valid digit, 1 when c was WS,
|
|
|
|
-1 when c was an invalid digit. )
|
|
|
|
: _pdacc
|
|
|
|
DUP 0x21 < IF DROP 1 EXIT THEN
|
|
|
|
( parse char )
|
|
|
|
'0' -
|
|
|
|
( if bad, return "r -1" )
|
|
|
|
DUP 0 < IF DROP -1 EXIT THEN ( bad )
|
|
|
|
DUP 9 > IF DROP -1 EXIT THEN ( bad )
|
|
|
|
( good, add to running result )
|
|
|
|
SWAP 10 * + ( r*10+n )
|
|
|
|
0 ( good )
|
|
|
|
;
|
|
|
|
|
2020-03-31 15:04:28 -04:00
|
|
|
: (parsed) ( a -- n f )
|
|
|
|
( read first char outside of the loop. it *has* to be
|
|
|
|
nonzero. )
|
2020-04-15 10:41:27 -04:00
|
|
|
DUP C@ ( a c )
|
2020-03-31 15:04:28 -04:00
|
|
|
( special case: do we have a negative? )
|
2020-04-09 08:23:53 -04:00
|
|
|
DUP '-' = IF
|
2020-03-31 15:04:28 -04:00
|
|
|
( Oh, a negative, let's recurse and reverse )
|
2020-04-09 08:23:53 -04:00
|
|
|
DROP 1 + ( a+1 )
|
2020-04-15 10:41:27 -04:00
|
|
|
(parsed) ( n f )
|
|
|
|
0 ROT ( f 0 n )
|
2020-04-09 08:23:53 -04:00
|
|
|
- SWAP EXIT ( 0-n f )
|
2020-03-31 15:04:28 -04:00
|
|
|
THEN
|
2020-04-15 10:41:27 -04:00
|
|
|
( running result from first char )
|
2020-04-09 08:23:53 -04:00
|
|
|
0 SWAP ( a r c )
|
2020-04-15 10:41:27 -04:00
|
|
|
_pdacc ( a r f )
|
|
|
|
DUP IF
|
|
|
|
( first char was not a valid digit )
|
|
|
|
2DROP 0 EXIT ( a 0 )
|
|
|
|
THEN
|
|
|
|
BEGIN ( a r 0 )
|
|
|
|
DROP SWAP 1 + ( r a+1 )
|
|
|
|
DUP C@ ( r a c )
|
|
|
|
ROT SWAP ( a r c )
|
|
|
|
_pdacc ( a r f )
|
|
|
|
DUP UNTIL
|
|
|
|
( a r f -- f is 1 on success, -1 on error, normalize
|
|
|
|
to bool. )
|
|
|
|
1 = ( a r f )
|
|
|
|
( we want "r f" )
|
|
|
|
ROT DROP
|
2020-03-31 15:04:28 -04:00
|
|
|
;
|
|
|
|
|
2020-03-27 11:49:50 -04:00
|
|
|
( This is only the "early parser" in earlier stages. No need
|
|
|
|
for an abort message )
|
|
|
|
: (parse)
|
2020-04-09 08:23:53 -04:00
|
|
|
(parsed) NOT IF ABORT THEN
|
2020-03-27 11:49:50 -04:00
|
|
|
;
|
|
|
|
|
2020-03-30 08:37:33 -04:00
|
|
|
: C<
|
2020-04-02 09:58:02 -04:00
|
|
|
( 0c == CINPTR )
|
2020-04-09 08:23:53 -04:00
|
|
|
0x0c RAM+ @ EXECUTE
|
2020-03-30 08:37:33 -04:00
|
|
|
;
|
|
|
|
|
2020-03-31 15:26:43 -04:00
|
|
|
: ,
|
2020-04-09 08:23:53 -04:00
|
|
|
HERE @ !
|
|
|
|
HERE @ 2 + HERE !
|
2020-03-31 15:26:43 -04:00
|
|
|
;
|
|
|
|
|
2020-03-27 19:12:46 -04:00
|
|
|
: C,
|
2020-04-09 08:23:53 -04:00
|
|
|
HERE @ C!
|
|
|
|
HERE @ 1 + HERE !
|
2020-03-27 19:12:46 -04:00
|
|
|
;
|
|
|
|
|
2020-03-28 13:18:43 -04:00
|
|
|
( The NOT is to normalize the negative/positive numbers to 1
|
|
|
|
or 0. Hadn't we wanted to normalize, we'd have written:
|
|
|
|
32 CMP 1 - )
|
2020-04-09 08:23:53 -04:00
|
|
|
: WS? 33 CMP 1 + NOT ;
|
2020-03-28 13:18:43 -04:00
|
|
|
|
2020-03-28 13:02:04 -04:00
|
|
|
: TOWORD
|
|
|
|
BEGIN
|
2020-04-09 08:23:53 -04:00
|
|
|
C< DUP WS? NOT IF EXIT THEN DROP
|
2020-03-28 13:02:04 -04:00
|
|
|
AGAIN
|
|
|
|
;
|
|
|
|
|
2020-03-28 12:55:22 -04:00
|
|
|
( Read word from C<, copy to WORDBUF, null-terminate, and
|
|
|
|
return, make HL point to WORDBUF. )
|
|
|
|
: WORD
|
2020-04-02 09:58:02 -04:00
|
|
|
( 0e == WORDBUF )
|
2020-04-09 08:23:53 -04:00
|
|
|
0x0e RAM+ ( a )
|
|
|
|
TOWORD ( a c )
|
2020-03-28 12:55:22 -04:00
|
|
|
BEGIN
|
|
|
|
( We take advantage of the fact that char MSB is
|
|
|
|
always zero to pre-write our null-termination )
|
2020-04-09 08:23:53 -04:00
|
|
|
OVER ! ( a )
|
|
|
|
1 + ( a+1 )
|
|
|
|
C< ( a c )
|
|
|
|
DUP WS?
|
2020-03-28 12:55:22 -04:00
|
|
|
UNTIL
|
|
|
|
( a this point, PS is: a WS )
|
|
|
|
( null-termination is already written )
|
2020-04-09 08:23:53 -04:00
|
|
|
2DROP
|
|
|
|
0x0e RAM+
|
2020-03-28 12:55:22 -04:00
|
|
|
;
|
|
|
|
|
2020-04-08 16:12:34 -04:00
|
|
|
: SCPY
|
|
|
|
BEGIN ( a )
|
2020-04-09 08:23:53 -04:00
|
|
|
DUP C@ ( a c )
|
|
|
|
DUP C, ( a c )
|
|
|
|
NOT IF DROP EXIT THEN
|
|
|
|
1 + ( a+1 )
|
2020-04-08 16:12:34 -04:00
|
|
|
AGAIN
|
|
|
|
;
|
|
|
|
|
2020-04-14 16:07:07 -04:00
|
|
|
: [entry]
|
|
|
|
HERE @ ( w h )
|
|
|
|
SWAP SCPY ( h )
|
2020-03-27 16:52:42 -04:00
|
|
|
( Adjust HERE -1 because SCPY copies the null )
|
2020-04-09 08:23:53 -04:00
|
|
|
HERE @ 1 - ( h h' )
|
|
|
|
DUP HERE ! ( h h' )
|
|
|
|
SWAP - ( sz )
|
2020-03-27 16:52:42 -04:00
|
|
|
( write prev value )
|
2020-04-09 08:23:53 -04:00
|
|
|
HERE @ CURRENT @ - ,
|
2020-03-27 16:52:42 -04:00
|
|
|
( write size )
|
2020-04-09 08:23:53 -04:00
|
|
|
C,
|
|
|
|
HERE @ CURRENT !
|
2020-03-27 16:52:42 -04:00
|
|
|
;
|
|
|
|
|
2020-04-14 16:07:07 -04:00
|
|
|
: (entry) WORD [entry] ;
|
|
|
|
|
2020-03-28 12:55:22 -04:00
|
|
|
: INTERPRET
|
|
|
|
BEGIN
|
2020-04-09 08:23:53 -04:00
|
|
|
WORD
|
|
|
|
(find)
|
2020-03-28 12:55:22 -04:00
|
|
|
IF
|
2020-04-09 08:23:53 -04:00
|
|
|
1 FLAGS !
|
2020-03-28 12:55:22 -04:00
|
|
|
EXECUTE
|
2020-04-09 08:23:53 -04:00
|
|
|
0 FLAGS !
|
2020-03-28 12:55:22 -04:00
|
|
|
ELSE
|
2020-04-09 08:23:53 -04:00
|
|
|
(parse*) @ EXECUTE
|
2020-03-28 12:55:22 -04:00
|
|
|
THEN
|
|
|
|
AGAIN
|
|
|
|
;
|
|
|
|
|
2020-04-03 08:31:30 -04:00
|
|
|
( system c< simply reads source from binary, starting at
|
|
|
|
LATEST. Convenient way to bootstrap a new system. )
|
2020-04-14 18:15:07 -04:00
|
|
|
: (boot<)
|
2020-04-10 14:57:00 -04:00
|
|
|
( 2e == BOOT C< PTR )
|
|
|
|
0x2e RAM+ @ ( a )
|
2020-04-09 08:23:53 -04:00
|
|
|
DUP C@ ( a c )
|
|
|
|
SWAP 1 + ( c a+1 )
|
2020-04-10 14:57:00 -04:00
|
|
|
0x2e RAM+ ! ( c )
|
2020-04-03 08:31:30 -04:00
|
|
|
;
|
|
|
|
|
2020-03-28 12:55:22 -04:00
|
|
|
: BOOT
|
2020-04-09 12:01:08 -04:00
|
|
|
0x02 RAM+ CURRENT* !
|
2020-04-09 08:23:53 -04:00
|
|
|
LIT< (parse) (find) DROP (parse*) !
|
2020-04-10 14:57:00 -04:00
|
|
|
( 2e == SYSTEM SCRATCHPAD )
|
|
|
|
CURRENT @ 0x2e RAM+ !
|
2020-04-02 09:58:02 -04:00
|
|
|
( 0c == CINPTR )
|
2020-04-14 18:15:07 -04:00
|
|
|
LIT< (boot<) (find) DROP 0x0c RAM+ !
|
2020-04-09 08:23:53 -04:00
|
|
|
LIT< INIT (find)
|
2020-04-02 23:21:53 -04:00
|
|
|
IF EXECUTE
|
2020-04-09 08:23:53 -04:00
|
|
|
ELSE DROP INTERPRET THEN
|
2020-03-28 12:55:22 -04:00
|
|
|
;
|
|
|
|
|
2020-03-30 13:50:53 -04:00
|
|
|
( LITN has to be defined after the last immediate usage of
|
|
|
|
it to avoid bootstrapping issues )
|
|
|
|
: LITN
|
2020-03-30 17:05:00 -04:00
|
|
|
( 32 == NUMBER )
|
2020-04-09 08:23:53 -04:00
|
|
|
32 , ,
|
2020-03-30 13:50:53 -04:00
|
|
|
;
|
|
|
|
|
2020-04-09 08:23:53 -04:00
|
|
|
: IMMED? 1 - C@ 0x80 AND ;
|
2020-04-08 20:09:32 -04:00
|
|
|
|
2020-04-09 09:38:59 -04:00
|
|
|
( ';' can't have its name right away because, when created, it
|
|
|
|
is not an IMMEDIATE yet and will not be treated properly by
|
|
|
|
xcomp. )
|
|
|
|
: _
|
|
|
|
['] EXIT ,
|
|
|
|
R> DROP ( exit : )
|
|
|
|
; IMMEDIATE
|
2020-03-27 16:16:57 -04:00
|
|
|
|
2020-04-11 11:38:45 -04:00
|
|
|
XCURRENT @ ( to PSP )
|
2020-04-09 09:38:59 -04:00
|
|
|
|
|
|
|
: :
|
2020-04-09 08:23:53 -04:00
|
|
|
(entry)
|
2020-03-28 10:38:05 -04:00
|
|
|
( We cannot use LITN as IMMEDIATE because of bootstrapping
|
2020-03-31 21:46:52 -04:00
|
|
|
issues. Same thing for ",".
|
|
|
|
32 == NUMBER 14 == compiledWord )
|
2020-04-09 09:21:55 -04:00
|
|
|
[ 32 H@ ! 2 ALLOT 14 H@ ! 2 ALLOT ] ,
|
2020-03-27 16:16:57 -04:00
|
|
|
BEGIN
|
2020-04-09 08:23:53 -04:00
|
|
|
WORD
|
|
|
|
(find)
|
2020-03-27 16:16:57 -04:00
|
|
|
( is word )
|
2020-04-09 08:23:53 -04:00
|
|
|
IF DUP IMMED? IF EXECUTE ELSE , THEN
|
2020-03-27 16:16:57 -04:00
|
|
|
( maybe number )
|
2020-04-09 08:23:53 -04:00
|
|
|
ELSE (parse*) @ EXECUTE LITN THEN
|
2020-03-27 16:16:57 -04:00
|
|
|
AGAIN
|
2020-04-09 09:27:51 -04:00
|
|
|
;
|
2020-04-11 11:38:45 -04:00
|
|
|
|
|
|
|
( from PSP ) ';' SWAP 4 - C!
|