OSDN Git Service

V309 Modified APPEND" in the long run, clusters can become non-contiguous
[fast-forth/master.git] / MSP430-FORTH / CORE_ANS.f
1 \ -*- coding: utf-8 -*-
2 \
3 \ TARGET SELECTION ( = the name of \INC\target.pat file without the extension)
4 \ (used by preprocessor GEMA to load the pattern: \inc\TARGET.pat)
5 \ MSP_EXP430FR5739  MSP_EXP430FR5969    MSP_EXP430FR5994    MSP_EXP430FR6989
6 \ MSP_EXP430FR4133  CHIPSTICK_FR2433    MSP_EXP430FR2433    MSP_EXP430FR2355
7 \
8 \ from scite editor : copy your target selection in (shift+F8) parameter 1:
9 \
10 \ OR
11 \
12 \ drag and drop this file onto SendSourceFileToTarget.bat
13 \ then select your TARGET when asked.
14 \
15 \
16 \ REGISTERS USAGE
17 \ rDODOES to rEXIT must be saved before use and restored after
18 \ scratch registers Y to S are free for use
19 \ under interrupt, IP is free for use
20 \
21 \ PUSHM order : PSP,TOS, IP,  S,  T,  W,  X,  Y, rEXIT, rDOVAR, rDOCON, rDODOES
22 \ example : PUSHM #6,IP pushes IP,S,T,W,X,Y registers to return stack
23 \
24 \ POPM  order :  rDODOES, rDOCON, rDOVAR, rEXIT,  Y,  X,  W,  T,  S, IP,TOS,PSP
25 \ example : POPM #6,IP   pulls Y,X,W,T,S,IP registers from return stack
26 \
27 \ FORTH conditionnals:  unary{ 0= 0< 0> }, binary{ = < > U< }
28 \
29 \ ASSEMBLER conditionnal usage with IF UNTIL WHILE  S<  S>=  U<   U>=  0=  0<>  0>=
30 \ ASSEMBLER conditionnal usage with ?GOTO           S<  S>=  U<   U>=  0=  0<>  0<
31
32     CODE ABORT_CORE_ANS
33     SUB #2,PSP
34     MOV TOS,0(PSP)
35     MOV &VERSION,TOS
36     SUB #309,TOS        \ FastForth V3.9
37     COLON
38     $0D EMIT            \ return to column 1 without CR
39     ABORT" FastForth V3.9 please!"
40     ;
41
42     ABORT_CORE_ANS
43
44 \  BC!     pattern @ --            Bits Clear in @
45     [UNDEFINED] BC!
46     [IF]
47     CODE BC!
48     BIC @PSP+,0(TOS)
49     MOV @PSP+,TOS
50     MOV @IP+,PC
51     ENDCODE
52     [THEN]
53
54 \  BS!     pattern @ --            Bits Set in @
55     [UNDEFINED] BS!
56     [IF]
57     CODE BS!
58     BIS @PSP+,0(TOS)
59     MOV @PSP+,TOS
60     MOV @IP+,PC
61     ENDCODE
62     [THEN]
63
64 \ =============================================================================
65 \    $8000 KERNEL_ADDON BS! \ uncomment to select FLOORED division
66     $8000 KERNEL_ADDON BC! \ uncomment to select SYMMETRIC division
67 \ =============================================================================
68
69     RST_RET           \ remove all above before CORE_ANS downloading
70
71 ; ----------------------------------
72 ; CORE_ANS.f
73 ; ----------------------------------
74 \
75 \ words complement to pass CORETEST.4TH
76
77     MARKER {CORE_ANS}   \ if already defined removes it before.
78
79     [UNDEFINED] HERE
80     [IF]
81     CODE HERE
82     MOV #HEREXEC,PC
83     ENDCODE
84     [THEN]
85
86 \ https://forth-standard.org/standard/core/Plus
87 \ +       n1/u1 n2/u2 -- n3/u3     add n1+n2
88     [UNDEFINED] +
89     [IF]
90     CODE +
91     ADD @PSP+,TOS
92     MOV @IP+,PC
93     ENDCODE
94     [THEN]
95
96 \ https://forth-standard.org/standard/core/Minus
97 \ -      n1/u1 n2/u2 -- n3/u3     n3 = n1-n2
98     [UNDEFINED] -
99     [IF]
100     CODE -
101     SUB @PSP+,TOS   \ 2  -- n2-n1 ( = -n3)
102     XOR #-1,TOS     \ 1
103     ADD #1,TOS      \ 1  -- n3 = -(n2-n1) = n1-n2
104     MOV @IP+,PC
105     ENDCODE
106     [THEN]
107
108 \ https://forth-standard.org/standard/core/DUP
109 \ DUP      x -- x x      duplicate top of stack
110     [UNDEFINED] DUP
111     [IF]
112     CODE DUP
113 BW1 SUB #2,PSP      \ 2  push old TOS..
114     MOV TOS,0(PSP)  \ 3  ..onto stack
115     MOV @IP+,PC     \ 4
116     ENDCODE
117
118 \ https://forth-standard.org/standard/core/qDUP
119 \ ?DUP     x -- 0 | x x    DUP if nonzero
120     CODE ?DUP
121     CMP #0,TOS      \ 2  test for TOS nonzero
122     0<> ?GOTO BW1    \ 2
123     MOV @IP+,PC     \ 4
124     ENDCODE
125     [THEN]
126
127 \ https://forth-standard.org/standard/core/EXIT
128 \ EXIT     --      exit a colon definition
129     [UNDEFINED] EXIT
130     [IF]
131     CODE EXIT
132     MOV @RSP+,IP    \ 2 pop previous IP (or next PC) from return stack
133     MOV @IP+,PC     \ 4 = NEXT
134     ENDCODE
135     [THEN]
136
137 \ https://forth-standard.org/standard/core/DEPTH
138 \ DEPTH    -- +n        number of items on stack, must leave 0 if stack empty
139     [UNDEFINED] DEPTH
140     [IF]
141     CODE DEPTH
142     MOV TOS,-2(PSP)
143     MOV #PSTACK,TOS
144     SUB PSP,TOS     \ PSP-S0--> TOS
145     RRA TOS         \ TOS/2   --> TOS
146     SUB #2,PSP      \ post decrement stack...
147     MOV @IP+,PC
148     ENDCODE
149     [THEN]
150
151 \ https://forth-standard.org/standard/core/SWAP
152 \ SWAP     x1 x2 -- x2 x1    swap top two items
153     [UNDEFINED] SWAP
154     [IF]
155     CODE SWAP
156     PUSH TOS            \ 3
157     MOV @PSP,TOS        \ 2
158     MOV @RSP+,0(PSP)    \ 4
159     MOV @IP+,PC         \ 4
160     ENDCODE
161     [THEN]
162
163 \ https://forth-standard.org/standard/core/DROP
164 \ DROP     x --          drop top of stack
165     [UNDEFINED] DROP
166     [IF]
167     CODE DROP
168     MOV @PSP+,TOS   \ 2
169     MOV @IP+,PC     \ 4
170     ENDCODE
171     [THEN]
172
173 \ https://forth-standard.org/standard/core/OVER
174 \ OVER    x1 x2 -- x1 x2 x1
175     [UNDEFINED] OVER
176     [IF]
177     CODE OVER
178     MOV TOS,-2(PSP)     \ 3 -- x1 (x2) x2
179     MOV @PSP,TOS        \ 2 -- x1 (x2) x1
180     SUB #2,PSP          \ 1 -- x1 x2 x1
181     MOV @IP+,PC
182     ENDCODE
183     [THEN]
184
185 \ https://forth-standard.org/standard/core/NIP
186 \ NIP      x1 x2 -- x2         Drop the first item below the top of stack
187     [UNDEFINED] NIP
188     [IF]
189     CODE NIP
190     ADD #2,PSP
191     MOV @IP+,PC
192     ENDCODE
193     [THEN]
194
195 \ https://forth-standard.org/standard/core/toR
196 \ >R    x --   R: -- x   push to return stack
197     [UNDEFINED] >R
198     [IF]
199     CODE >R
200     PUSH TOS
201     MOV @PSP+,TOS
202     MOV @IP+,PC
203     ENDCODE
204     [THEN]
205
206 \ https://forth-standard.org/standard/core/Rfrom
207 \ R>    -- x    R: x --   pop from return stack
208     [UNDEFINED] R>
209     [IF]
210     CODE R>
211     SUB #2,PSP      \ 1
212     MOV TOS,0(PSP)  \ 3
213     MOV @RSP+,TOS   \ 2
214     MOV @IP+,PC     \ 4
215     ENDCODE
216     [THEN]
217
218 \ https://forth-standard.org/standard/core/Fetch
219 \ @     c-addr -- word   fetch word from memory
220     [UNDEFINED] @
221     [IF]
222     CODE @
223     MOV @TOS,TOS
224     MOV @IP+,PC
225     ENDCODE
226     [THEN]
227
228 \ https://forth-standard.org/standard/core/Store
229 \ !      word c-addr --    store word in memory
230     [UNDEFINED] !
231     [IF]
232     CODE !
233     MOV @PSP+,0(TOS)    \ 4
234     MOV @PSP+,TOS       \ 2
235     MOV @IP+,PC
236     ENDCODE
237     [THEN]
238
239 \ https://forth-standard.org/standard/core/Fetch
240 \ C@     c-addr -- char   fetch char from memory
241     [UNDEFINED] C@
242     [IF]
243     CODE C@
244     MOV.B @TOS,TOS
245     MOV @IP+,PC
246     ENDCODE
247     [THEN]
248
249 \ https://forth-standard.org/standard/core/CStore
250 \ C!      char c-addr --    store char in memory
251     [UNDEFINED] C!
252     [IF]
253     CODE C!
254     MOV.B @PSP+,0(TOS)  \ 4
255     ADD #1,PSP          \ 1
256     MOV @PSP+,TOS       \ 2
257     MOV @IP+,PC
258     ENDCODE
259     [THEN]
260
261 \ https://forth-standard.org/standard/core/CComma
262 \ C,   char --        append char
263     [UNDEFINED] C,
264     [IF]
265     CODE C,
266     MOV &DP,W
267     MOV.B TOS,0(W)
268     ADD #1,&DP
269     MOV @PSP+,TOS
270     MOV @IP+,PC
271     ENDCODE
272     [THEN]
273
274 \ https://forth-standard.org/standard/core/ZeroEqual
275 \ 0=     n/u -- flag    return true if TOS=0
276     [UNDEFINED] 0=
277     [IF]
278     CODE 0=
279     SUB #1,TOS      \ 1 borrow (clear cy) if TOS was 0
280     SUBC TOS,TOS    \ 1 TOS=-1 if borrow was set
281     MOV @IP+,PC
282     ENDCODE
283     [THEN]
284
285 \ https://forth-standard.org/standard/core/Zerone
286 \ 0<>     n/u -- flag    return true if TOS<>0
287     [UNDEFINED] 0<>
288     [IF]
289     CODE 0<>
290     SUB #1,TOS      \ 1 borrow (clear cy) if TOS was 0
291     SUBC TOS,TOS    \ 1 TOS=-1 if borrow was set
292     XOR #-1,TOS     \ 1
293     MOV @IP+,PC
294     ENDCODE
295     [THEN]
296
297 \ https://forth-standard.org/standard/core/Zeroless
298 \ 0<     n -- flag      true if TOS negative
299     [UNDEFINED] 0<
300     [IF]
301     CODE 0<
302 BW1 ADD TOS,TOS     \ 1 set carry if TOS negative
303     SUBC TOS,TOS    \ 1 TOS=-1 if carry was clear
304     XOR #-1,TOS     \ 1 TOS=-1 if carry was set
305     MOV @IP+,PC     \
306     ENDCODE
307     [THEN]
308
309 \ HERE
310
311 \ https://forth-standard.org/standard/core/StoD
312 \ S>D    n -- d          single -> double prec.
313     [UNDEFINED] S>D
314     [IF]
315     : S>D
316     DUP 0<
317     ;
318     [THEN]
319
320 \ https://forth-standard.org/standard/core/Equal
321 \ =      x1 x2 -- flag         test x1=x2
322     [UNDEFINED] =
323     [IF]
324     CODE =
325     SUB @PSP+,TOS   \ 2
326     SUB #1,TOS      \ 1 borrow (clear cy) if TOS was 0
327     SUBC TOS,TOS    \ 1 TOS=-1 if borrow was set
328     MOV @IP+,PC
329     ENDCODE
330     [THEN]
331
332 \ https://forth-standard.org/standard/core/Uless
333 \ U<    u1 u2 -- flag       test u1<u2, unsigned
334     [UNDEFINED] U<
335     [IF]
336
337     CODE U<
338     SUB @PSP+,TOS   \ 2 u2-u1
339     U< ?GOTO FW1
340     0<> IF
341 BW1 MOV #-1,TOS     \ 1
342     THEN
343     MOV @IP+,PC     \ 4
344     ENDCODE
345
346 \ https://forth-standard.org/standard/core/Umore
347 \ U>     n1 n2 -- flag
348     CODE U>
349     SUB @PSP+,TOS   \ 2
350     U< ?GOTO BW1    \ 2 flag = true, Z = 0
351 FW1 AND #0,TOS      \ 1 Z = 1
352     MOV @IP+,PC     \ 4
353     ENDCODE
354     [THEN]
355
356 \ https://forth-standard.org/standard/core/less
357 \ <      n1 n2 -- flag        test n1<n2, signed
358     [UNDEFINED] <
359     [IF]  \ define < and >
360
361     CODE <
362     SUB @PSP+,TOS   \ 1 TOS=n2-n1
363     S< ?GOTO FW1    \ 2 signed
364     0<> IF          \ 2
365 BW1 MOV #-1,TOS \ 1 flag Z = 0
366     THEN
367     MOV @IP+,PC
368     ENDCODE
369
370 \ https://forth-standard.org/standard/core/more
371 \ >     n1 n2 -- flag         test n1>n2, signed
372     CODE >
373     SUB @PSP+,TOS   \ 2 TOS=n2-n1
374     S< ?GOTO BW1    \ 2 --> +5
375 FW1 AND #0,TOS      \ 1 flag Z = 1
376     MOV @IP+,PC
377     ENDCODE
378     [THEN]
379
380 \ ------------------------------------------------------------------------------
381 \ CONTROL STRUCTURES
382 \ ------------------------------------------------------------------------------
383 \ THEN and BEGIN compile nothing
384 \ DO compile one word
385 \ IF, ELSE, AGAIN, UNTIL, WHILE, REPEAT, LOOP & +LOOP compile two words
386 \ LEAVE compile three words
387 \
388 \ https://forth-standard.org/standard/core/IF
389 \ IF       -- IFadr    initialize conditional forward branch
390     [UNDEFINED] IF
391     [IF]     \ define IF THEN
392
393     CODE IF
394     SUB #2,PSP              \
395     MOV TOS,0(PSP)          \
396     MOV &DP,TOS             \ -- HERE
397     ADD #4,&DP            \           compile one word, reserve one word
398     MOV #QFBRAN,0(TOS)      \ -- HERE   compile QFBRAN
399     ADD #2,TOS              \ -- HERE+2=IFadr
400     MOV @IP+,PC
401     ENDCODE IMMEDIATE
402
403 \ https://forth-standard.org/standard/core/THEN
404 \ THEN     IFadr --                resolve forward branch
405     CODE THEN
406     MOV &DP,0(TOS)          \ -- IFadr
407     MOV @PSP+,TOS           \ --
408     MOV @IP+,PC
409     ENDCODE IMMEDIATE
410     [THEN]
411
412 \ https://forth-standard.org/standard/core/ELSE
413 \ ELSE     IFadr -- ELSEadr        resolve forward IF branch, leave ELSEadr on stack
414     [UNDEFINED] ELSE
415     [IF]
416     CODE ELSE
417     ADD #4,&DP              \ make room to compile two words
418     MOV &DP,W               \ W=HERE+4
419     MOV #BRAN,-4(W)
420     MOV W,0(TOS)            \ HERE+4 ==> [IFadr]
421     SUB #2,W                \ HERE+2
422     MOV W,TOS               \ -- ELSEadr
423     MOV @IP+,PC
424     ENDCODE IMMEDIATE
425     [THEN]
426
427 \ https://forth-standard.org/standard/core/BEGIN
428 \ BEGIN    -- BEGINadr             initialize backward branch
429     [UNDEFINED] BEGIN
430     [IF]  \ define BEGIN UNTIL AGAIN WHILE REPEAT
431
432     CODE BEGIN
433     MOV #HEREXEC,PC
434     ENDCODE IMMEDIATE
435
436 \ https://forth-standard.org/standard/core/UNTIL
437 \ UNTIL    BEGINadr --             resolve conditional backward branch
438     CODE UNTIL
439     MOV #QFBRAN,X
440 BW1 ADD #4,&DP          \ compile two words
441     MOV &DP,W           \ W = HERE
442     MOV X,-4(W)         \ compile Bran or QFBRAN at HERE
443     MOV TOS,-2(W)       \ compile bakcward adr at HERE+2
444     MOV @PSP+,TOS
445     MOV @IP+,PC
446     ENDCODE IMMEDIATE
447
448 \ https://forth-standard.org/standard/core/AGAIN
449 \ AGAIN    BEGINadr --             resolve uncondionnal backward branch
450     CODE AGAIN
451     MOV #BRAN,X
452     GOTO BW1
453     ENDCODE IMMEDIATE
454
455 \ https://forth-standard.org/standard/core/WHILE
456 \ WHILE    BEGINadr -- WHILEadr BEGINadr
457     : WHILE
458     POSTPONE IF SWAP
459     ; IMMEDIATE
460
461 \ https://forth-standard.org/standard/core/REPEAT
462 \ REPEAT   WHILEadr BEGINadr --     resolve WHILE loop
463     : REPEAT
464     POSTPONE AGAIN POSTPONE THEN
465     ; IMMEDIATE
466     [THEN]
467
468     [UNDEFINED] DO
469     [IF]     \ define DO LOOP +LOOP
470
471 \ https://forth-standard.org/standard/core/DO
472 \ DO       -- DOadr   L: -- 0
473     HDNCODE XDO         \ DO run time
474     MOV #$8000,X        \ 2 compute 8000h-limit = "fudge factor"
475     SUB @PSP+,X         \ 2
476     MOV TOS,Y           \ 1 loop ctr = index+fudge
477     ADD X,Y             \ 1 Y = INDEX
478     PUSHM #2,X          \ 4 PUSHM X,Y, i.e. PUSHM LIMIT, INDEX
479     MOV @PSP+,TOS       \ 2
480     MOV @IP+,PC         \ 4
481     ENDCODE
482
483     CODE DO
484     SUB #2,PSP          \
485     MOV TOS,0(PSP)      \
486     ADD #2,&DP          \   make room to compile xdo
487     MOV &DP,TOS         \ -- HERE+2
488     MOV #XDO,-2(TOS)    \   compile xdo
489     ADD #2,&LEAVEPTR    \ -- HERE+2     LEAVEPTR+2
490     MOV &LEAVEPTR,W     \
491     MOV #0,0(W)         \ -- HERE+2     L-- 0, init
492     MOV @IP+,PC
493     ENDCODE IMMEDIATE
494
495 \ https://forth-standard.org/standard/core/LOOP
496 \ LOOP    DOadr --         L-- an an-1 .. a1 0
497     HDNCODE XLOOP       \   LOOP run time
498     ADD #1,0(RSP)       \ 4 increment INDEX
499 BW1 BIT #$100,SR        \ 2 is overflow bit set?
500     0= IF               \   branch if no overflow
501         MOV @IP,IP
502         MOV @IP+,PC
503     THEN
504     ADD #4,RSP          \ 1 empties RSP
505     ADD #2,IP           \ 1 overflow = loop done, skip branch ofs
506     MOV @IP+,PC         \ 4 14~ taken or not taken xloop/loop
507     ENDCODE             \
508
509     CODE LOOP
510     MOV #XLOOP,X
511 BW2 ADD #4,&DP          \ make room to compile two words
512     MOV &DP,W
513     MOV X,-4(W)         \ xloop --> HERE
514     MOV TOS,-2(W)       \ DOadr --> HERE+2
515     BEGIN                   \ resolve all "leave" adr
516         MOV &LEAVEPTR,TOS   \ -- Adr of top LeaveStack cell
517         SUB #2,&LEAVEPTR    \ --
518         MOV @TOS,TOS        \ -- first LeaveStack value
519         CMP #0,TOS          \ -- = value left by DO ?
520     0<> WHILE
521         MOV W,0(TOS)        \ move adr after loop as UNLOOP adr
522     REPEAT
523     MOV @PSP+,TOS
524     MOV @IP+,PC
525     ENDCODE IMMEDIATE
526
527 \ https://forth-standard.org/standard/core/PlusLOOP
528 \ +LOOP   adrs --   L-- an an-1 .. a1 0
529     HDNCODE XPLOO   \   +LOOP run time
530     ADD TOS,0(RSP)  \ 4 increment INDEX by TOS value
531     MOV @PSP+,TOS   \ 2 get new TOS, doesn't change flags
532     GOTO BW1        \ 2
533     ENDCODE         \
534
535     CODE +LOOP
536     MOV #XPLOO,X
537     GOTO BW2
538     ENDCODE IMMEDIATE
539     [THEN]
540
541 \ https://forth-standard.org/standard/core/I
542 \ I        -- n   R: sys1 sys2 -- sys1 sys2
543 \                  get the innermost loop index
544     [UNDEFINED] I
545     [IF]
546     CODE I
547     SUB #2,PSP              \ 1 make room in TOS
548     MOV TOS,0(PSP)          \ 3
549     MOV @RSP,TOS            \ 2 index = loopctr - fudge
550     SUB 2(RSP),TOS          \ 3
551     MOV @IP+,PC             \ 4 13~
552     ENDCODE
553     [THEN]
554
555 \ https://forth-standard.org/standard/core/J
556 \ J        -- n   R: 4*sys -- 4*sys
557 \ C                  get the second loop index
558     [UNDEFINED] J
559     [IF]
560     CODE J
561     SUB #2,PSP
562     MOV TOS,0(PSP)
563     MOV 4(RSP),TOS
564     SUB 6(RSP),TOS
565     MOV @IP+,PC
566     ENDCODE
567     [THEN]
568
569 \ https://forth-standard.org/standard/core/UNLOOP
570 \ UNLOOP   --   R: sys1 sys2 --  drop loop parms
571     [UNDEFINED] UNLOOP
572     [IF]
573     CODE UNLOOP
574     ADD #4,RSP
575     MOV @IP+,PC
576     ENDCODE
577     [THEN]
578
579 \ https://forth-standard.org/standard/core/LEAVE
580 \ LEAVE    --    L: -- adrs
581     [UNDEFINED] LEAVE
582     [IF]
583     CODE LEAVE
584     MOV &DP,W               \ compile three words
585     MOV #UNLOOP,0(W)        \ [HERE] = UNLOOP
586     MOV #BRAN,2(W)          \ [HERE+2] = BRAN
587     ADD #6,&DP              \ [HERE+4] = at adr After LOOP
588     ADD #2,&LEAVEPTR
589     ADD #4,W
590     MOV &LEAVEPTR,X
591     MOV W,0(X)              \ leave HERE+4 on LEAVEPTR stack
592     MOV @IP+,PC
593     ENDCODE IMMEDIATE
594     [THEN]
595
596 \ https://forth-standard.org/standard/core/AND
597 \ C AND    x1 x2 -- x3           logical AND
598     [UNDEFINED] AND
599     [IF]
600     CODE AND
601     AND @PSP+,TOS
602     MOV @IP+,PC
603     ENDCODE
604     [THEN]
605
606 \ https://forth-standard.org/standard/core/OR
607 \ C OR     x1 x2 -- x3           logical OR (BIS, BIts Set)
608     [UNDEFINED] OR
609     [IF]
610     CODE OR
611     BIS @PSP+,TOS
612     AND #-1,TOS \ to set flags
613     MOV @IP+,PC
614     ENDCODE
615     [THEN]
616
617 \ https://forth-standard.org/standard/core/XOR
618 \ C XOR    x1 x2 -- x3           logical XOR
619     [UNDEFINED] XOR
620     [IF]
621     CODE XOR
622     XOR @PSP+,TOS
623     MOV @IP+,PC
624     ENDCODE
625     [THEN]
626
627 \ https://forth-standard.org/standard/core/OnePlus
628 \ 1+      n1/u1 -- n2/u2       add 1 to TOS
629     [UNDEFINED] 1+
630     [IF]
631     CODE 1+
632     ADD #1,TOS
633     MOV @IP+,PC
634     ENDCODE
635     [THEN]
636
637 \ https://forth-standard.org/standard/core/OneMinus
638 \ 1-      n1/u1 -- n2/u2     subtract 1 from TOS
639     [UNDEFINED] 1-
640     [IF]
641     CODE 1-
642     SUB #1,TOS
643     MOV @IP+,PC
644     ENDCODE
645     [THEN]
646
647 \ https://forth-standard.org/standard/core/INVERT
648 \ INVERT   x1 -- x2            bitwise inversion
649     [UNDEFINED] INVERT
650     [IF]
651     CODE INVERT
652     XOR #-1,TOS
653     MOV @IP+,PC
654     ENDCODE
655     [THEN]
656
657 \ https://forth-standard.org/standard/core/NEGATE
658 \ C NEGATE   x1 -- x2            two's complement
659     [UNDEFINED] NEGATE
660     [IF]
661     CODE NEGATE
662     XOR #-1,TOS
663     ADD #1,TOS
664     MOV @IP+,PC
665     ENDCODE
666     [THEN]
667
668 \ https://forth-standard.org/standard/core/ABS
669 \ C ABS     n1 -- +n2     absolute value
670     [UNDEFINED] ABS
671     [IF]
672     CODE ABS
673     CMP #0,TOS       \  1
674     0>= IF
675         MOV @IP+,PC
676     THEN
677     MOV #NEGATE,PC
678     ENDCODE
679     [THEN]
680
681 \ https://forth-standard.org/standard/core/LSHIFT
682 \ LSHIFT  x1 u -- x2    logical L shift u places
683     [UNDEFINED] LSHIFT
684     [IF]
685     CODE LSHIFT
686     MOV @PSP+,W
687     AND #$1F,TOS        \ no need to shift more than 16
688     0<> IF
689         BEGIN
690             ADD W,W
691             SUB #1,TOS
692         0= UNTIL
693     THEN
694     MOV W,TOS
695     MOV @IP+,PC
696     ENDCODE
697     [THEN]
698
699 \ https://forth-standard.org/standard/core/RSHIFT
700 \ RSHIFT  x1 u -- x2    logical R7 shift u places
701     [UNDEFINED] RSHIFT
702     [IF]
703     CODE RSHIFT
704     MOV @PSP+,W
705     AND #$1F,TOS       \ no need to shift more than 16
706     0<> IF
707         BEGIN
708             BIC #C,SR           \ Clr Carry
709             RRC W
710             SUB #1,TOS
711         0= UNTIL
712     THEN
713     MOV W,TOS
714     MOV @IP+,PC
715     ENDCODE
716     [THEN]
717
718 \ https://forth-standard.org/standard/core/MAX
719 \ MAX    n1 n2 -- n3       signed maximum
720     [UNDEFINED] MAX
721     [IF]
722
723     CODE MAX
724     CMP @PSP,TOS    \ n2-n1
725     S<  ?GOTO FW1   \ n2<n1
726 BW1 ADD #2,PSP
727     MOV @IP+,PC
728     ENDCODE
729
730 \ https://forth-standard.org/standard/core/MIN
731 \ MIN    n1 n2 -- n3       signed minimum
732     CODE MIN
733     CMP @PSP,TOS    \ n2-n1
734     S< ?GOTO BW1    \ n2<n1
735 FW1 MOV @PSP+,TOS
736     MOV @IP+,PC
737     ENDCODE
738     [THEN]
739
740 \ https://forth-standard.org/standard/core/TwoTimes
741 \ 2*      x1 -- x2         arithmetic left shift
742     [UNDEFINED] 2*
743     [IF]
744     CODE 2*
745     ADD TOS,TOS
746     MOV @IP+,PC
747     ENDCODE
748     [THEN]
749
750 \ https://forth-standard.org/standard/core/TwoDiv
751 \ 2/      x1 -- x2        arithmetic right shift
752     [UNDEFINED] 2/
753     [IF]
754     CODE 2/
755     RRA TOS
756     MOV @IP+,PC
757     ENDCODE
758     [THEN]
759
760 \ --------------------
761 \ ARITHMETIC OPERATORS
762 \ --------------------
763     RST_SET
764
765     CODE TSTBIT         \ addr bit_mask -- true/flase flag
766     MOV @PSP+,X
767     AND @X,TOS
768     MOV @IP+,PC
769     ENDCODE
770
771 \    $81EF DEVICEID @ U<
772 \    DEVICEID @ $81F3 U<
773 \    = [IF]   ; MSP430FR413x subfamily without hardware_MPY
774     KERNEL_ADDON HMPY TSTBIT    \   KERNEL_ADDON(BIT0) = hardware MPY flag
775     RST_RET
776
777     [IF]
778 \ https://forth-standard.org/standard/core/MTimes
779 \ M*     n1 n2 -- dlo dhi  signed 16*16->32 multiply
780     CODE UM*
781     MOV @PSP,&MPY       \ Load 1st operand for unsigned multiplication
782 BW1 MOV TOS,&OP2        \ Load 2nd operand
783     MOV &RES0,0(PSP)    \ low result on stack
784     MOV &RES1,TOS       \ high result in TOS
785     MOV @IP+,PC
786     ENDCODE
787
788 \ https://forth-standard.org/standard/core/MTimes
789 \ M*     n1 n2 -- dlo dhi  signed 16*16->32 multiply
790     CODE M*
791     MOV @PSP,&MPYS      \ Load 1st operand for signed multiplication
792     GOTO BW1
793     ENDCODE
794
795     [ELSE]  ; MSP430FRxxxx with hardware_MPY
796 \ https://forth-standard.org/standard/core/UMTimes
797 \ UM*     u1 u2 -- udlo udhi   unsigned 16x16->32 mult.
798         [UNDEFINED] M* [IF]
799     CODE M*
800     MOV @PSP,S          \ S= n1
801     CMP #0,S            \ n1 > -1 ?
802     S< IF
803         XOR #-1,0(PSP)  \ n1 --> u1
804         ADD #1,0(PSP)   \
805     THEN
806     XOR TOS,S           \ S contains sign of result
807     CMP #0,TOS          \ n2 > -1 ?
808     S< IF
809         XOR #-1,TOS     \ n2 --> u2
810         ADD #1,TOS      \
811     THEN
812     PUSHM #2,IP         \ UMSTAR use S,T,W,X,Y
813     LO2HI               \ -- ud1 u2
814     UM*
815     HI2LO
816     POPM #2,IP           \ pop S,IP
817     CMP #0,S            \ sign of result > -1 ?
818     S< IF
819         XOR #-1,0(PSP)  \ ud --> d
820         XOR #-1,TOS
821         ADD #1,0(PSP)
822         ADDC #0,TOS
823     THEN
824     MOV @IP+,PC
825     ENDCODE
826         [THEN]
827     [THEN]  ;  endof hardware_MPY
828
829 \ HERE
830
831 \ https://forth-standard.org/standard/core/UMDivMOD
832 \ UM/MOD   udlo|udhi u1 -- r q   unsigned 32/16->r16 q16
833     [UNDEFINED] UM/MOD
834     [IF]
835     CODE UM/MOD
836     PUSH #DROP      \
837     MOV #MUSMOD,PC  \ execute MUSMOD then return to DROP
838     ENDCODE
839     [THEN]
840
841 \ HERE OVER - DUMP
842
843
844     KERNEL_ADDON @ 0<  ; test the switch: FLOORED / SYMETRIC DIVISION
845     [IF]
846 \ https://forth-standard.org/standard/core/FMDivMOD
847 \ FM/MOD   d1 n1 -- r q   floored signed div'n
848         [UNDEFINED] FM/MOD
849         [IF]
850         CODE FM/MOD
851         MOV TOS,S           \           S=DIV
852         MOV @PSP,T          \           T=DVDhi
853         CMP #0,TOS          \           n2 >= 0 ?
854         S< IF               \
855             XOR #-1,TOS
856             ADD #1,TOS      \ -- d1 u2
857         THEN
858         CMP #0,0(PSP)       \           d1hi >= 0 ?
859         S< IF               \
860             XOR #-1,2(PSP)  \           d1lo
861             XOR #-1,0(PSP)  \           d1hi
862             ADD #1,2(PSP)   \           d1lo+1
863             ADDC #0,0(PSP)  \           d1hi+C
864         THEN                \ -- uDVDlo uDVDhi uDIVlo
865         PUSHM  #2,S         \ 4         PUSHM S,T
866         CALL #MUSMOD
867         MOV @PSP+,TOS
868         POPM  #2,S          \ 4         POPM T,S
869         CMP #0,T            \           T=DVDhi --> REM_sign
870         S< IF
871             XOR #-1,0(PSP)
872             ADD #1,0(PSP)
873         THEN
874         XOR S,T             \           S=DIV XOR T=DVDhi = Quot_sign
875         CMP #0,T            \ -- n3 u4  T=quot_sign
876         S< IF
877             XOR #-1,TOS
878             ADD #1,TOS
879         THEN                \ -- n3 n4  S=divisor
880
881         CMP #0,0(PSP)       \ remainder <> 0 ?
882         0<> IF
883             CMP #1,TOS      \ quotient < 1 ?
884             S< IF
885             ADD S,0(PSP)  \ add divisor to remainder
886             SUB #1,TOS    \ decrement quotient
887             THEN
888         THEN
889         MOV @IP+,PC
890         ENDCODE
891         [THEN]
892     [ELSE]
893 \ https://forth-standard.org/standard/core/SMDivREM
894 \ SM/REM   DVDlo DVDhi DIV -- r3 q4  symmetric signed div
895         [UNDEFINED] SM/REM
896         [IF]
897         CODE SM/REM
898         MOV TOS,S           \           S=DIV
899         MOV @PSP,T          \           T=DVDhi
900         CMP #0,TOS          \           n2 >= 0 ?
901         S< IF               \
902             XOR #-1,TOS
903             ADD #1,TOS      \ -- d1 u2
904         THEN
905         CMP #0,0(PSP)       \           d1hi >= 0 ?
906         S< IF               \
907             XOR #-1,2(PSP)  \           d1lo
908             XOR #-1,0(PSP)  \           d1hi
909             ADD #1,2(PSP)   \           d1lo+1
910             ADDC #0,0(PSP)  \           d1hi+C
911         THEN                \ -- uDVDlo uDVDhi uDIVlo
912         PUSHM  #2,S         \ 4         PUSHM S,T
913         CALL #MUSMOD
914         MOV @PSP+,TOS
915         POPM  #2,S          \ 4         POPM T,S
916         CMP #0,T            \           T=DVDhi --> REM_sign
917         S< IF
918             XOR #-1,0(PSP)
919             ADD #1,0(PSP)
920         THEN
921         XOR S,T             \           S=DIV XOR T=DVDhi = Quot_sign
922         CMP #0,T            \ -- n3 u4  T=quot_sign
923         S< IF
924             XOR #-1,TOS
925             ADD #1,TOS
926         THEN                \ -- n3 n4  S=divisor
927         MOV @IP+,PC
928         ENDCODE
929         [THEN]
930     [THEN]
931
932 \ https://forth-standard.org/standard/core/Times
933 \ *      n1 n2 -- n3       signed multiply
934     [UNDEFINED] *
935     [IF]
936     : *
937     M* DROP
938     ;
939     [THEN]
940
941 \ https://forth-standard.org/standard/core/DivMOD
942 \ /MOD   n1 n2 -- r3 q4     signed division
943     [UNDEFINED] /MOD
944     [IF]
945     : /MOD
946     >R DUP 0< R>
947         [ KERNEL_ADDON @ 0< ]   \ test the switch: FLOORED / SYMETRIC DIVISION
948         [IF]    FM/MOD
949         [ELSE]  SM/REM
950         [THEN]
951     ;
952     [THEN]
953
954 \ https://forth-standard.org/standard/core/Div
955 \ /      n1 n2 -- n3       signed quotient
956     [UNDEFINED] /
957     [IF]
958     : /
959     >R DUP 0< R>
960         [ KERNEL_ADDON @ 0< ]   \ test the switch: FLOORED / SYMETRIC DIVISION
961         [IF]    FM/MOD
962         [ELSE]  SM/REM
963         [THEN]
964     NIP
965     ;
966     [THEN]
967
968 \ https://forth-standard.org/standard/core/MOD
969 \ MOD    n1 n2 -- n3       signed remainder
970     [UNDEFINED] MOD
971     [IF]
972     : MOD
973     >R DUP 0< R>
974         [ KERNEL_ADDON @ 0< ]   \ test the switch: FLOORED / SYMETRIC DIVISION
975         [IF]    FM/MOD
976         [ELSE]  SM/REM
977         [THEN]
978     DROP
979     ;
980     [THEN]
981
982 \ https://forth-standard.org/standard/core/TimesDivMOD
983 \ */MOD  n1 n2 n3 -- r4 q5    signed mult/div
984     [UNDEFINED] */MOD
985     [IF]
986     : */MOD
987     >R M* R>
988         [ KERNEL_ADDON @ 0< ]   \ test the switch: FLOORED / SYMETRIC DIVISION
989         [IF]    FM/MOD
990         [ELSE]  SM/REM
991         [THEN]
992     ;
993     [THEN]
994
995 \ https://forth-standard.org/standard/core/TimesDiv
996 \ */     n1 n2 n3 -- n4        n1*n2/q3
997     [UNDEFINED] */
998     [IF]
999     : */
1000     >R M* R>
1001         [ KERNEL_ADDON @ 0< ]   \ test the switch: FLOORED / SYMETRIC DIVISION
1002         [IF]    FM/MOD
1003         [ELSE]  SM/REM
1004         [THEN]
1005     NIP
1006     ;
1007     [THEN]
1008
1009 \ -------------------------------------------------------------------------------
1010 \  STACK OPERATIONS
1011 \ -------------------------------------------------------------------------------
1012 \ https://forth-standard.org/standard/core/ROT
1013 \ ROT    x1 x2 x3 -- x2 x3 x1
1014     [UNDEFINED] ROT
1015     [IF]
1016     CODE ROT
1017     MOV @PSP,W          \ 2 fetch x2
1018     MOV TOS,0(PSP)      \ 3 store x3
1019     MOV 2(PSP),TOS      \ 3 fetch x1
1020     MOV W,2(PSP)        \ 3 store x2
1021     MOV @IP+,PC
1022     ENDCODE
1023     [THEN]
1024
1025 \ https://forth-standard.org/standard/core/RFetch
1026 \ R@    -- x     R: x -- x   fetch from return stack
1027     [UNDEFINED] R@
1028     [IF]
1029     CODE R@
1030     SUB #2,PSP
1031     MOV TOS,0(PSP)
1032     MOV @RSP,TOS
1033     MOV @IP+,PC
1034     ENDCODE
1035     [THEN]
1036
1037 \ https://forth-standard.org/standard/core/TUCK
1038 \ TUCK  ( x1 x2 -- x2 x1 x2 )
1039     [UNDEFINED] TUCK
1040     [IF]
1041     : TUCK SWAP OVER ;
1042     [THEN]
1043
1044 \ ----------------------------------------------------------------------
1045 \ DOUBLE OPERATORS
1046 \ ----------------------------------------------------------------------
1047 \ https://forth-standard.org/standard/core/TwoFetch
1048 \ 2@    a-addr -- x1 x2    fetch 2 cells ; the lower address will appear on top of stack
1049     [UNDEFINED] 2@
1050     [IF]
1051     CODE 2@
1052     SUB #2,PSP
1053     MOV 2(TOS),0(PSP)
1054     MOV @TOS,TOS
1055     MOV @IP+,PC
1056     ENDCODE
1057     [THEN]
1058
1059 \ https://forth-standard.org/standard/core/TwoStore
1060 \ 2!    x1 x2 a-addr --    store 2 cells ; the top of stack is stored at the lower adr
1061     [UNDEFINED] 2!
1062     [IF]
1063     CODE 2!
1064     MOV @PSP+,0(TOS)
1065     MOV @PSP+,2(TOS)
1066     MOV @PSP+,TOS
1067     MOV @IP+,PC
1068     ENDCODE
1069     [THEN]
1070
1071 \ https://forth-standard.org/standard/core/TwoDUP
1072 \ 2DUP   x1 x2 -- x1 x2 x1 x2   dup top 2 cells
1073     [UNDEFINED] 2DUP
1074     [IF]
1075     CODE 2DUP
1076     MOV TOS,-2(PSP)     \ 3
1077     MOV @PSP,-4(PSP)    \ 4
1078     SUB #4,PSP          \ 1
1079     MOV @IP+,PC         \ 4
1080     ENDCODE
1081     [THEN]
1082
1083 \ https://forth-standard.org/standard/core/TwoDROP
1084 \ 2DROP  x1 x2 --          drop 2 cells
1085     [UNDEFINED] 2DROP
1086     [IF]
1087     CODE 2DROP
1088     ADD #2,PSP
1089     MOV @PSP+,TOS
1090     MOV @IP+,PC
1091     ENDCODE
1092     [THEN]
1093
1094 \ https://forth-standard.org/standard/core/TwoSWAP
1095 \ 2SWAP  x1 x2 x3 x4 -- x3 x4 x1 x2
1096     [UNDEFINED] 2SWAP
1097     [IF]
1098     CODE 2SWAP
1099     MOV @PSP,W          \ -- x1 x2 x3 x4    W=x3
1100     MOV 4(PSP),0(PSP)   \ -- x1 x2 x1 x4
1101     MOV W,4(PSP)        \ -- x3 x2 x1 x4
1102     MOV TOS,W           \ -- x3 x2 x1 x4    W=x4
1103     MOV 2(PSP),TOS      \ -- x3 x2 x1 x2    W=x4
1104     MOV W,2(PSP)        \ -- x3 x4 x1 x2
1105     MOV @IP+,PC
1106     ENDCODE
1107     [THEN]
1108
1109 \ https://forth-standard.org/standard/core/TwoOVER
1110 \ 2OVER  x1 x2 x3 x4 -- x1 x2 x3 x4 x1 x2
1111     [UNDEFINED] 2OVER
1112     [IF]
1113     CODE 2OVER
1114     SUB #4,PSP          \ -- x1 x2 x3 x x x4
1115     MOV TOS,2(PSP)      \ -- x1 x2 x3 x4 x x4
1116     MOV 8(PSP),0(PSP)   \ -- x1 x2 x3 x4 x1 x4
1117     MOV 6(PSP),TOS      \ -- x1 x2 x3 x4 x1 x2
1118     MOV @IP+,PC
1119     ENDCODE
1120     [THEN]
1121
1122 \ ----------------------------------------------------------------------
1123 \ ALIGNMENT OPERATORS
1124 \ ----------------------------------------------------------------------
1125 \ https://forth-standard.org/standard/core/ALIGNED
1126 \ ALIGNED  addr -- a-addr       align given addr
1127     [UNDEFINED] ALIGNED
1128     [IF]
1129     CODE ALIGNED
1130     BIT #1,TOS
1131     ADDC #0,TOS
1132     MOV @IP+,PC
1133     ENDCODE
1134     [THEN]
1135
1136 \ https://forth-standard.org/standard/core/ALIGN
1137 \ ALIGN    --                         align HERE
1138     [UNDEFINED] ALIGN
1139     [IF]
1140     CODE ALIGN
1141     BIT #1,&DP  \ 3
1142     ADDC #0,&DP \ 4
1143     MOV @IP+,PC
1144     ENDCODE
1145     [THEN]
1146
1147 \ ---------------------
1148 \ PORTABILITY OPERATORS
1149 \ ---------------------
1150 \ https://forth-standard.org/standard/core/CHARS
1151 \ CHARS    n1 -- n2            chars->adrs units
1152     [UNDEFINED] CHARS
1153     [IF]
1154     CODE CHARS
1155     MOV @IP+,PC
1156     ENDCODE
1157     [THEN]
1158
1159 \ https://forth-standard.org/standard/core/CHARPlus
1160 \ CHAR+    c-addr1 -- c-addr2   add char size
1161     [UNDEFINED] CHAR+
1162     [IF]
1163     CODE CHAR+
1164     ADD #1,TOS
1165     MOV @IP+,PC
1166     ENDCODE
1167     [THEN]
1168
1169 \ https://forth-standard.org/standard/core/CELLS
1170 \ CELLS    n1 -- n2            cells->adrs units
1171     [UNDEFINED] CELLS
1172     [IF]
1173     CODE CELLS
1174     ADD TOS,TOS
1175     MOV @IP+,PC
1176     ENDCODE
1177     [THEN]
1178
1179 \ https://forth-standard.org/standard/core/CELLPlus
1180 \ CELL+    a-addr1 -- a-addr2      add cell size
1181     [UNDEFINED] CELL+
1182     [IF]
1183     CODE CELL+
1184     ADD #2,TOS
1185     MOV @IP+,PC
1186     ENDCODE
1187     [THEN]
1188
1189 \ ---------------------------
1190 \ BLOCK AND STRING COMPLEMENT
1191 \ ---------------------------
1192 \ https://forth-standard.org/standard/core/CHAR
1193 \ CHAR   -- char           parse ASCII character
1194     [UNDEFINED] CHAR
1195     [IF]
1196     : CHAR
1197     $20 WORD 1+ C@
1198     ;
1199     [THEN]
1200
1201 \ https://forth-standard.org/standard/core/BracketCHAR
1202 \ [CHAR]   --          compile character literal
1203     [UNDEFINED] [CHAR]
1204     [IF]
1205     : [CHAR]
1206     CHAR POSTPONE LITERAL
1207     ; IMMEDIATE
1208     [THEN]
1209
1210 \ https://forth-standard.org/standard/core/PlusStore
1211 \ +!     n/u a-addr --       add n/u to memory
1212     [UNDEFINED] +!
1213     [IF]
1214     CODE +!
1215     ADD @PSP+,0(TOS)
1216     MOV @PSP+,TOS
1217     MOV @IP+,PC
1218     ENDCODE
1219     [THEN]
1220
1221 \ https://forth-standard.org/standard/core/MOVE
1222 \ MOVE    addr1 addr2 u --     smart move
1223 \             VERSION FOR 1 ADDRESS UNIT = 1 CHAR
1224     [UNDEFINED] MOVE
1225     [IF]
1226     CODE MOVE
1227     MOV TOS,W           \ W = cnt
1228     MOV @PSP+,Y         \ Y = addr2 = dst
1229     MOV @PSP+,X         \ X = addr1 = src
1230     MOV @PSP+,TOS       \ pop new TOS
1231     CMP #0,W            \ count = 0 ?
1232     0<> IF              \ if 0, already done !
1233         CMP X,Y         \ dst = src ?
1234         0<> IF          \ if 0, already done !
1235             U< IF       \ U< if src > dst
1236                 BEGIN   \ copy W bytes
1237                     MOV.B @X+,0(Y)
1238                     ADD #1,Y
1239                     SUB #1,W
1240                 0= UNTIL
1241                 MOV @IP+,PC \ out 1 of MOVE ====>
1242             THEN        \ U>= if dst > src
1243             ADD W,Y     \ copy W bytes beginning with the end
1244             ADD W,X
1245             BEGIN
1246                 SUB #1,X
1247                 SUB #1,Y
1248                 MOV.B @X,0(Y)
1249                 SUB #1,W
1250             0= UNTIL
1251         THEN
1252     THEN
1253     MOV @IP+,PC \ out 2 of MOVE ====>
1254     ENDCODE
1255     [THEN]
1256
1257 \ https://forth-standard.org/standard/core/FILL
1258 \ FILL   c-addr u char --  fill memory with char
1259     [UNDEFINED] FILL
1260     [IF]
1261     CODE FILL
1262     MOV @PSP+,X     \ count
1263     MOV @PSP+,W     \ address
1264     CMP #0,X
1265     0<> IF
1266         BEGIN
1267             MOV.B TOS,0(W)    \ store char in memory
1268             ADD #1,W
1269             SUB #1,X
1270         0= UNTIL
1271     THEN
1272     MOV @PSP+,TOS     \ empties stack
1273     MOV @IP+,PC
1274     ENDCODE
1275     [THEN]
1276
1277 \ --------------------
1278 \ INTERPRET COMPLEMENT
1279 \ --------------------
1280 \ https://forth-standard.org/standard/core/HEX
1281     [UNDEFINED] HEX
1282     [IF]
1283     CODE HEX
1284     MOV #$10,&BASEADR
1285     MOV @IP+,PC
1286     ENDCODE
1287     [THEN]
1288
1289     \ https://forth-standard.org/standard/core/DECIMAL
1290     [UNDEFINED] DECIMAL
1291     [IF]
1292     CODE DECIMAL
1293     MOV #$0A,&BASEADR
1294     MOV @IP+,PC
1295     ENDCODE
1296     [THEN]
1297
1298 \ https://forth-standard.org/standard/core/p
1299 \ (         --          skip input until char ) or EOL
1300     [UNDEFINED] (   ; )
1301     [IF]
1302     : (
1303     ')' WORD DROP
1304     ; IMMEDIATE
1305     [THEN]
1306
1307 \ https://forth-standard.org/standard/core/Dotp
1308 \ .(        --          type comment immediatly.
1309     [UNDEFINED] .(  ; "
1310     [IF]
1311     CODE .(         ; "
1312     MOV #0,&CAPS    \ CAPS OFF
1313     COLON
1314     ')' WORD
1315     COUNT TYPE
1316     HI2LO
1317     MOV #$20,&CAPS   \ CAPS ON
1318     MOV @RSP+,IP
1319     MOV @IP+,PC
1320     ENDCODE IMMEDIATE
1321     [THEN]
1322
1323 \ https://forth-standard.org/standard/core/toBODY
1324 \ >BODY     -- addr      leave BODY of a CREATEd word\ also leave default ACTION-OF primary DEFERred word
1325     [UNDEFINED] >BODY
1326     [IF]
1327     CODE >BODY
1328     ADD #4,TOS
1329     MOV @IP+,PC
1330     ENDCODE
1331     [THEN]
1332
1333 \ https://forth-standard.org/standard/core/EXECUTE
1334 \ EXECUTE   i*x xt -- j*x   execute Forth word at 'xt'
1335     [UNDEFINED] EXECUTE
1336     [IF]
1337     CODE EXECUTE
1338     PUSH TOS                \ 3 push xt
1339     MOV @PSP+,TOS           \ 2
1340     MOV @RSP+,PC            \ 4 xt --> PC
1341     ENDCODE
1342     [THEN]
1343
1344 \ https://forth-standard.org/standard/core/EVALUATE
1345 \ EVALUATE          \ i*x c-addr u -- j*x  interpret string
1346     [UNDEFINED] EVALUATE
1347     [IF]
1348     CODE EVALUATE
1349     MOV #SOURCE_LEN,X       \ 2
1350     MOV @X+,S               \ 2 S = SOURCE_LEN
1351     MOV @X+,T               \ 2 T = SOURCE_ORG
1352     MOV @X+,W               \ 2 W = TOIN
1353     PUSHM #4,IP             \ 6 PUSHM IP,S,T,W
1354     LO2HI
1355     [ ' \ 8 + , ]           \ compile INTERPRET
1356     HI2LO
1357     MOV @RSP+,&TOIN         \ 4
1358     MOV @RSP+,&SOURCE_ORG   \ 4
1359     MOV @RSP+,&SOURCE_LEN   \ 4
1360     MOV @RSP+,IP
1361     MOV @IP+,PC
1362     ENDCODE
1363     [THEN]
1364
1365 \ https://forth-standard.org/standard/core/RECURSE
1366 \ C RECURSE  --      recurse to current definition
1367     [UNDEFINED] RECURSE
1368     [IF]
1369     CODE RECURSE
1370     MOV &DP,X
1371     MOV &LAST_CFA,0(X)
1372     ADD #2,&DP
1373     MOV @IP+,PC
1374     ENDCODE IMMEDIATE
1375     [THEN]
1376
1377 \ https://forth-standard.org/standard/core/SOURCE
1378 \ SOURCE    -- adr u    of current input buffer
1379     [UNDEFINED] SOURCE
1380     [IF]
1381     CODE SOURCE
1382     SUB #4,PSP
1383     MOV TOS,2(PSP)
1384     MOV &SOURCE_LEN,TOS
1385     MOV &SOURCE_ORG,0(PSP)
1386     MOV @IP+,PC
1387     ENDCODE
1388     [THEN]
1389
1390 \ https://forth-standard.org/standard/core/VARIABLE
1391 \ VARIABLE <name>       --                      define a Forth VARIABLE
1392     [UNDEFINED] VARIABLE
1393     [IF]
1394     : VARIABLE
1395     CREATE
1396     HI2LO
1397     MOV #DOVAR,-4(W)        \   CFA = CALL rDOVAR
1398     MOV @RSP+,IP
1399     MOV @IP+,PC
1400     ENDCODE
1401     [THEN]
1402
1403 \ https://forth-standard.org/standard/core/CONSTANT
1404 \ CONSTANT <name>     n --                      define a Forth CONSTANT
1405     [UNDEFINED] CONSTANT
1406     [IF]
1407     : CONSTANT
1408     CREATE
1409     HI2LO
1410     MOV TOS,-2(W)           \   PFA = n
1411     MOV @PSP+,TOS
1412     MOV @RSP+,IP
1413     MOV @IP+,PC
1414     ENDCODE
1415     [THEN]
1416
1417 \ https://forth-standard.org/standard/core/STATE
1418 \ STATE   -- a-addr       holds compiler state
1419     [UNDEFINED] STATE
1420     [IF]
1421     STATEADR CONSTANT STATE
1422     [THEN]
1423
1424 \ https://forth-standard.org/standard/core/BASE
1425 \ BASE    -- a-addr       holds conversion radix
1426     [UNDEFINED] BASE
1427     [IF]
1428     BASEADR  CONSTANT BASE
1429     [THEN]
1430
1431 \ https://forth-standard.org/standard/core/toIN
1432 \ C >IN     -- a-addr       holds offset in input stream
1433     [UNDEFINED] >IN
1434     [IF]
1435     TOIN CONSTANT >IN
1436     [THEN]
1437
1438 \ https://forth-standard.org/standard/core/PAD
1439 \  PAD           --  addr
1440     [UNDEFINED] PAD
1441     [IF]
1442     PAD_ORG CONSTANT PAD
1443     [THEN]
1444
1445 \ https://forth-standard.org/standard/core/BL
1446 \ BL      -- char            an ASCII space
1447     [UNDEFINED] BL
1448     [IF]
1449     'SP' CONSTANT BL
1450     [THEN]
1451
1452 \ https://forth-standard.org/standard/core/SPACE
1453 \ SPACE   --               output a space
1454     [UNDEFINED] SPACE
1455     [IF]
1456     : SPACE
1457     'SP' EMIT ;
1458     [THEN]
1459
1460 \ https://forth-standard.org/standard/core/SPACES
1461 \ SPACES   n --            output n spaces
1462     [UNDEFINED] SPACES
1463     [IF]
1464     : SPACES
1465     BEGIN
1466         ?DUP
1467     WHILE
1468         'SP' EMIT
1469         1-
1470     REPEAT
1471     ;
1472     [THEN]
1473
1474     [UNDEFINED] DEFER
1475     [IF]
1476 \ https://forth-standard.org/standard/core/DEFER
1477 \ Skip leading space delimiters. Parse name delimited by a space.
1478 \ Create a definition for name with the execution semantics defined below.
1479 \
1480 \ name Execution:   --
1481 \ Execute the xt that name is set to execute, i.e. NEXT (nothing),
1482 \ until the phrase ' word IS name is executed, causing a new value of xt to be assigned to name.
1483     : DEFER
1484     CREATE
1485     HI2LO
1486     MOV #$4030,-4(W)        \4 first CELL = MOV @PC+,PC = BR #addr
1487     MOV #NEXT_ADR,-2(W)     \3 second CELL              =   ...mNEXT : do nothing by default
1488     MOV @RSP+,IP
1489     MOV @IP+,PC
1490     ENDCODE
1491     [THEN]
1492
1493 \ https://forth-standard.org/standard/core/CR
1494 \ CR      --               send CR+LF to the output device
1495     [UNDEFINED] CR
1496     [IF]
1497 \    DEFER CR       \ DEFERed definition, by default executes :NONAME part
1498     CODE CR         \ replaced by this CODE definition
1499     MOV #NEXT_ADR,PC
1500     ENDCODE
1501
1502     :NONAME
1503     'CR' EMIT 'LF' EMIT
1504     ; IS CR
1505     [THEN]
1506
1507 \ https://forth-standard.org/standard/core/TO
1508 \ TO name Run-time: ( x -- )
1509 \ Assign the value x to named VALUE.
1510     [UNDEFINED] TO
1511     [IF]
1512     CODE TO
1513     BIS #UF9,SR
1514     MOV @IP+,PC
1515     ENDCODE
1516     [THEN]
1517
1518 \ https://forth-standard.org/standard/core/VALUE
1519 \ ( x "<spaces>name" -- )                      define a Forth VALUE
1520 \ Skip leading space delimiters. Parse name delimited by a space.
1521 \ Create a definition for name with the execution semantics defined below,
1522 \ with an initial value equal to x.
1523 \
1524 \ name Execution: ( -- x )
1525 \ Place x on the stack. The value of x is that given when name was created,
1526 \ until the phrase x TO name is executed, causing a new value of x to be assigned to name.
1527     [UNDEFINED] VALUE
1528     [IF]
1529     : VALUE                 \ x "<spaces>name" --
1530     CREATE ,
1531     DOES>
1532     HI2LO
1533     MOV @RSP+,IP
1534     BIT #UF9,SR         \ 2 see TO
1535     0= IF               \ 2 if UF9 is not set
1536         MOV @TOS,TOS    \ 2     execute FETCH
1537         MOV @IP+,PC     \ 4
1538     THEN                \   else
1539     BIC #UF9,SR         \ 2     clear UF9 flag
1540     MOV #!,PC           \ 4     execute STORE
1541     ENDCODE
1542     [THEN]
1543
1544 \ https://forth-standard.org/standard/core/CASE
1545     [UNDEFINED] CASE
1546     [IF]
1547
1548     : CASE 0
1549     ; IMMEDIATE \ -- #of-1
1550
1551 \ https://forth-standard.org/standard/core/OF
1552     : OF \ #of-1 -- orgOF #of
1553     1+                      \ count OFs
1554     >R                      \ move off the stack in case the control-flow stack is the data stack.
1555     POSTPONE OVER POSTPONE = \ copy and test case value
1556     POSTPONE IF             \ add orig to control flow stack
1557     POSTPONE DROP               \ discards case value if =
1558     R>                      \ we can bring count back now
1559     ; IMMEDIATE
1560
1561 \ https://forth-standard.org/standard/core/ENDOF
1562     : ENDOF \ orgOF #of -- orgENDOF #of
1563     >R                      \ move off the stack in case the control-flow stack is the data stack.
1564     POSTPONE ELSE
1565     R>                      \ we can bring count back now
1566     ; IMMEDIATE
1567
1568 \ https://forth-standard.org/standard/core/ENDCASE
1569     : ENDCASE \ orgENDOF1..orgENDOFn #of --
1570     POSTPONE DROP
1571     0 DO POSTPONE THEN
1572     LOOP
1573     ; IMMEDIATE
1574     [THEN]
1575
1576     RST_SET
1577
1578     ECHO
1579 ; CORE_ANS.f is loaded
1580