OSDN Git Service

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