1 \ -*- coding: utf-8 -*-
3 ; -----------------------------------------------------------------------
5 ; -----------------------------------------------------------------------
7 \ TARGET SELECTION ( = the name of \INC\target.pat file without the extension)
8 \ MSP_EXP430FR5739 MSP_EXP430FR5969 MSP_EXP430FR5994 MSP_EXP430FR6989
9 \ MSP_EXP430FR4133 MSP_EXP430FR2433 MSP_EXP430FR2355 CHIPSTICK_FR2433
12 \ from scite editor : copy your target selection in (shift+F8) parameter 1:
16 \ drag and drop this file onto SendSourceFileToTarget.bat
17 \ then select your TARGET when asked.
21 \ PUSHM order : PSP,TOS, IP, S, T, W, X, Y, rEXIT,rDOVAR,rDOCON, rDODOES, R3, SR,RSP, PC
22 \ PUSHM order : R15,R14,R13,R12,R11,R10, R9, R8, R7 , R6 , R5 , R4 , R3, R2, R1, R0
24 \ example : PUSHM #6,IP pushes IP,S,T,W,X,Y registers to return stack
26 \ POPM order : PC,RSP, SR, R3, rDODOES,rDOCON,rDOVAR,rEXIT, Y, X, W, T, S, IP,TOS,PSP
27 \ POPM order : R0, R1, R2, R3, R4 , R5 , R6 , R7 , R8, R9,R10,R11,R12,R13,R14,R15
29 \ example : POPM #6,IP pop Y,X,W,T,S,IP registers from return stack
31 \ ASSEMBLER conditionnal usage after IF UNTIL WHILE : S< S>= U< U>= 0= 0<> 0>=
32 \ ASSEMBLER conditionnal usage before ?JMP ?GOTO : S< S>= U< U>= 0= 0<> 0<
34 \ FORTH conditionnal : 0= 0< = < > U<
36 \ first, we test for downloading driver only if UART TERMINAL target
41 SUB #309,TOS \ FastForth V3.9
43 'CR' EMIT \ return to column 1 without 'LF'
44 ABORT" FastForth V3.9 please!"
45 RST_RET \ remove ABORT_TEST_ASM definition before resuming
48 ABORT_TEST_ASM \ abort test
54 \ https://forth-standard.org/standard/core/toR
55 \ >R x -- R: -- x push to return stack
65 \ https://forth-standard.org/standard/core/Rfrom
66 \ R> -- x R: x -- pop from return stack ; CALL #RFROM performs DOVAR
74 \ https://forth-standard.org/standard/core/Plus
75 \ + n1/u1 n2/u2 -- n3/u3 add n1+n2
84 \ https://forth-standard.org/standard/core/Minus
85 \ - n1/u1 n2/u2 -- n3/u3 n3 = n1-n2
87 SUB @PSP+,TOS \ 2 -- n2-n1 ( = -n3)
89 ADD #1,TOS \ 1 -- n3 = -(n2-n1) = n1-n2
96 \ https://forth-standard.org/standard/core/SWAP
97 \ SWAP x1 x2 -- x2 x1 swap top two items
107 [IF] \ MAX and MIN are defined in {UTILITY}
109 CODE MAX \ n1 n2 -- n3 signed maximum
116 CODE MIN \ n1 n2 -- n3 signed minimum
127 \ https://forth-standard.org/standard/core/CFetch
128 \ C@ c-addr -- char fetch char from memory
137 \ https://forth-standard.org/standard/core/VARIABLE
138 \ VARIABLE <name> -- define a Forth VARIABLE
142 MOV #$1287,-4(W) \ CFA = CALL rDOVAR
150 \ https://forth-standard.org/standard/core/CONSTANT
151 \ CONSTANT <name> n -- define a Forth CONSTANT
155 MOV TOS,-2(W) \ PFA = n
164 \ https://forth-standard.org/standard/core/DEFER
165 \ DEFER "<spaces>name" --
166 \Skip leading space delimiters. Parse name delimited by a space.
167 \Create a definition for name with the execution semantics defined below.
170 \Execute the xt that name is set to execute, i.e. NEXT (nothing),
171 \until the phrase ' word IS name is executed, causing a new value of xt to be assigned to name.
175 MOV #$4030,-4(W) \ CFA = MOV @PC+,PC = BR MOV @IP+,PC
176 MOV #NEXT_ADR,-2(W) \ PFA = address of MOV @IP+,PC to do nothing.
184 \ https://forth-standard.org/standard/core/toBODY
185 \ >BODY -- addr leave BODY of a CREATEd word\ also leave default ACTION-OF primary DEFERred word
194 \ https://forth-standard.org/standard/core/SPACE
195 \ SPACE -- output a space
202 \ https://forth-standard.org/standard/core/SPACES
203 \ SPACES n -- output n spaces
217 MOV @PSP+,TOS \ -- drop n
223 [IF] \ define DUP and ?DUP
224 \ https://forth-standard.org/standard/core/DUP
225 \ DUP x -- x x duplicate top of stack
227 BW1 SUB #2,PSP \ 2 push old TOS..
228 MOV TOS,0(PSP) \ 3 ..onto stack
232 \ https://forth-standard.org/standard/core/qDUP
233 \ ?DUP x -- 0 | x x DUP if nonzero
235 CMP #0,TOS \ 2 test for TOS nonzero
243 \ https://forth-standard.org/standard/core/OVER
244 \ OVER x1 x2 -- x1 x2 x1
246 MOV TOS,-2(PSP) \ 3 -- x1 (x2) x2
247 MOV @PSP,TOS \ 2 -- x1 (x2) x1
248 SUB #2,PSP \ 1 -- x1 x2 x1
254 [IF] \ defined in {UTILITY}
255 : U.R \ u n -- display u unsigned in n width (n >= 2)
257 R> OVER - 0 MAX SPACES TYPE
261 \ https://forth-standard.org/standard/core/IF
262 \ IF -- IFadr initialize conditional forward branch
264 [IF] \ define IF THEN
269 MOV &DP,TOS \ -- HERE
270 ADD #4,&DP \ compile one word, reserve one word
271 MOV #QFBRAN,0(TOS) \ -- HERE compile QFBRAN
272 ADD #2,TOS \ -- HERE+2=IFadr
276 \ https://forth-standard.org/standard/core/THEN
277 \ THEN IFadr -- resolve forward branch
279 MOV &DP,0(TOS) \ -- IFadr
285 \ https://forth-standard.org/standard/core/SWAP
286 \ SWAP x1 x2 -- x2 x1 swap top two items
297 \ https://forth-standard.org/standard/core/BEGIN
298 \ BEGIN -- BEGINadr initialize backward branch
300 [IF] \ define BEGIN UNTIL AGAIN WHILE REPEAT
306 \ https://forth-standard.org/standard/core/UNTIL
307 \ UNTIL BEGINadr -- resolve conditional backward branch
310 BW1 ADD #4,&DP \ compile two words
312 MOV X,-4(W) \ compile Bran or QFBRAN at HERE
313 MOV TOS,-2(W) \ compile bakcward adr at HERE+2
318 \ https://forth-standard.org/standard/core/AGAIN
319 \ AGAIN BEGINadr -- resolve uncondionnal backward branch
325 \ https://forth-standard.org/standard/core/WHILE
326 \ WHILE BEGINadr -- WHILEadr BEGINadr
331 \ https://forth-standard.org/standard/core/REPEAT
332 \ REPEAT WHILEadr BEGINadr -- resolve WHILE loop
334 POSTPONE AGAIN POSTPONE THEN
339 [IF] \ define DO LOOP +LOOP
341 \ https://forth-standard.org/standard/core/DO
342 \ DO -- DOadr L: -- 0
343 HDNCODE XDO \ DO run time
344 MOV #$8000,X \ 2 compute 8000h-limit = "fudge factor"
346 MOV TOS,Y \ 1 loop ctr = index+fudge
347 ADD X,Y \ 1 Y = INDEX
348 PUSHM #2,X \ 4 PUSHM X,Y, i.e. PUSHM LIMIT, INDEX
356 ADD #2,&DP \ make room to compile xdo
357 MOV &DP,TOS \ -- HERE+2
358 MOV #XDO,-2(TOS) \ compile xdo
359 ADD #2,&LEAVEPTR \ -- HERE+2 LEAVEPTR+2
361 MOV #0,0(W) \ -- HERE+2 L-- 0, init
365 \ https://forth-standard.org/standard/core/LOOP
366 \ LOOP DOadr -- L-- an an-1 .. a1 0
367 HDNCODE XLOOP \ LOOP run time
368 ADD #1,0(RSP) \ 4 increment INDEX
369 BW1 BIT #$100,SR \ 2 is overflow bit set?
370 0= IF \ branch if no overflow
374 ADD #4,RSP \ 1 empties RSP
375 ADD #2,IP \ 1 overflow = loop done, skip branch ofs
376 MOV @IP+,PC \ 4 14~ taken or not taken xloop/loop
381 BW2 ADD #4,&DP \ make room to compile two words
383 MOV X,-4(W) \ xloop --> HERE
384 MOV TOS,-2(W) \ DOadr --> HERE+2
385 BEGIN \ resolve all "leave" adr
386 MOV &LEAVEPTR,TOS \ -- Adr of top LeaveStack cell
387 SUB #2,&LEAVEPTR \ --
388 MOV @TOS,TOS \ -- first LeaveStack value
389 CMP #0,TOS \ -- = value left by DO ?
391 MOV W,0(TOS) \ move adr after loop as UNLOOP adr
397 \ https://forth-standard.org/standard/core/PlusLOOP
398 \ +LOOP adrs -- L-- an an-1 .. a1 0
399 HDNCODE XPLOO \ +LOOP run time
400 ADD TOS,0(RSP) \ 4 increment INDEX by TOS value
401 MOV @PSP+,TOS \ 2 get new TOS, doesn't change flags
413 \ https://forth-standard.org/standard/core/I
414 \ I -- n R: sys1 sys2 -- sys1 sys2
415 \ get the innermost loop index
417 SUB #2,PSP \ 1 make room in TOS
419 MOV @RSP,TOS \ 2 index = loopctr - fudge
425 \ https://forth-standard.org/standard/core/BASE
426 \ BASE -- a-addr holds conversion radix
429 BASEADR CONSTANT BASE
432 \ https://forth-standard.org/standard/core/CR
433 \ CR -- send CR+LF to the output device
436 DEFER CR \ DEFERed definition, by default executes that of :NONAME
444 [IF] \ defined in {UTILITY}
445 \ https://forth-standard.org/standard/tools/DUMP
446 CODE DUMP \ adr n -- dump memory
448 PUSH &BASE \ save current base
449 MOV #$10,&BASEADR \ HEX base
450 ADD @PSP,TOS \ -- ORG END
453 DO CR \ generate line
454 I 4 U.R SPACE \ generate address
461 I $10 + I \ display 16 chars
462 DO I C@ $7E MIN $20 MAX EMIT LOOP
464 R> BASE ! \ restore current base
468 \ -----------------------------------------------------------------------
469 \ test CPUx instructions PUSHM, POPM, RLAM, RRAM, RRCM, RRUM
470 \ -----------------------------------------------------------------------
473 \ PUSHM #16,R14 \ uncomment to test error "out of bounds"
474 \ PUSHM #2,R0 \ uncomment to test error "out of bounds"
475 \ PUSHM #0,IP \ uncomment to test error "out of bounds"
476 \ POPM #17,R15 \ uncomment to test error "out of bounds"
477 \ POPM #2,R0 \ uncomment to test error "out of bounds"
478 \ POPM #0,IP \ uncomment to test error "out of bounds"
485 PUSHM #4,IP \ PUSHM IP,S,T,W
486 POPM #4,IP \ POPM W,T,S,IP
488 MOV TOS,8(PSP) \ save old TOS
494 \ RLAM #0,TOS \ uncomment to test error "out of bounds"
495 \ RLAM #5,TOS \ uncomment to test error "out of bounds"
496 RRAM #1,TOS \ 0 < shift value < 5
500 COLON \ high level part of the word starts here...
502 ; \ and finishes here.
504 TESTPUSHM ; you should see 11111 3 2 1 0 -->
507 GOTO BW1 \ JMP TESTPUSHM
510 TESTPOPM ; you should see 11111 3 2 1 0 -->
514 \ -----------------------------------------------------------------------
515 \ test symbolic branch in assembler
516 \ test a FORTH section encapsulated in an assembly word
517 \ -----------------------------------------------------------------------
518 CODE TEST1 \ the word "CODE" add ASSEMBLER as CONTEXT vocabulary...
520 MOV &BASE,&BASE \ to test &xxxx src operand
522 0<> IF MOV #2,&BASE \ if base <> 2
523 ELSE MOV #$0A,&BASE \ else base = 2
525 COLON \ tips : no "ok" displayed in start of line <==> compilation mode
526 BASE @ U. \ always display 10 !
530 \ -----------------------------------------------------------------------
531 \ test a word that starts as word FORTH and ends as assembly word
532 \ -----------------------------------------------------------------------
533 : TEST2 \ ":" starts compilation
534 BASE @ U. \ always display 10 !
535 HI2LO \ switch FORTH to ASM : compile one word (next address)
536 \ add vocabulary ASSEMBLER as CONTEXT vocabulary
537 \ switch in interpret mode
539 0<> IF MOV #2, &BASE \ if variable system BASE <> 2
540 ELSE MOV #10,&BASE \ else (BASE = 2)
542 \ MOV #EXIT,PC \ to pair with ":" i.e. to restore IP saved by : then execute NEXT.
543 \ but even compile two words, it's better to compile an inline EXIT :
544 MOV @RSP+,IP \ restore IP
546 ENDCODE \ ends assembler : remove vocabulary ASSEMBLER from CONTEXT
549 \ -----------------------------------------------------------------------
550 \ test a word that starts as assembly word and ends as FORTH word
551 \ -----------------------------------------------------------------------
552 CODE TEST3 \ "CODE" starts assembler, i.e. add ASSEMBLER as CONTEXT vocabulary
554 0<> IF MOV #2, &BASE \ if variable system BASE <> 2
555 ELSE MOV #10,&BASE \ else (BASE = 2)
557 BASE @ U. \ always display 10 !
562 \ -----------------------------------------------------------------------
563 \ test an assembly jump spanning a section written in FORTH
564 \ -----------------------------------------------------------------------
570 MOV #%1010,TOS \ init count = 10
573 \ IP is already saved by word ":"
574 DUP U. \ display count
577 0= UNTIL MOV @PSP+,TOS
578 \ MOV #EXIT,PC \ to pair with ":" i.e. to restore IP saved by : then execute NEXT.
579 MOV @RSP+,IP \ restore IP
583 TEST5 ; you should see : 9 8 7 6 5 4 3 2 1 0 -->
586 \ -----------------------------------------------------------------------
587 \ tests indexing address
588 \ -----------------------------------------------------------------------
591 \ https://forth-standard.org/standard/core/CComma
592 \ C, char -- append char
603 \ https://forth-standard.org/standard/core/CFetch
604 \ C@ c-addr -- char fetch char from memory
619 8 BYTES_TABLE_IDX BYTES_TABLE \ create table "BYTES_TABLE" with bytes content = 0,1,2,3,4,5,6,7
621 2 BYTES_TABLE C@ . ; you should see 2 -->
625 VARIABLE BYTES_TABLE1
627 $0201 BYTES_TABLE1 ! \ words written in memory are little endian !
629 CODE IDX_TEST1 \ index -- value
630 MOV.B BYTES_TABLE1(TOS),TOS \ -- value
635 0 IDX_TEST1 ; you should see 1 -->
643 1 TEST6 . ; you should see 1 -->
646 \ -----------------------------------------------------------------------
647 \ tests access to a CREATED word with assembler
648 \ -----------------------------------------------------------------------
677 TABLE 2 - CONSTANT PFA_TABLE \ PFA_TABLE leave the PFA of TABLE
680 CODE REDIRECT ; <table> -- redirects TABLE to argument <table>
687 CODE REDIRECT0 ; -- redirects TABLE to TABLE0
688 MOV #TABLE0,&PFA_TABLE
693 CODE REDIRECT10 ; -- redirects TABLE to TABLE10
694 MOV #TABLE10,&PFA_TABLE
699 CODE REDIRECT20 ; -- redirects TABLE to TABLE20
700 MOV #TABLE20,&PFA_TABLE
712 TABLE0 REDIRECT TABLE 10 DUMP
714 TABLE10 REDIRECT TABLE 10 DUMP
716 TABLE20 REDIRECT TABLE 10 DUMP
719 REDIRECT0 TABLE 10 DUMP
721 REDIRECT10 TABLE 10 DUMP
723 REDIRECT20 TABLE 10 DUMP
726 TABLE0 PFA_TABLE ! TABLE 10 DUMP
728 TABLE10 PFA_TABLE ! TABLE 10 DUMP
730 TABLE20 PFA_TABLE ! TABLE 10 DUMP
733 \ -----------------------------------------------------------------------
734 \ tests behaviour of assembly error
735 \ -----------------------------------------------------------------------
736 \ R16 causes an error, assembler context is aborted and the word TEST7 is "hidden".
739 \ MOV 0(truc),0(R16) ; display an error "out of bounds" -->
741 ; -----------------------------------------------------------------------
742 ; create a primary DEFERred assembly word
743 ; -----------------------------------------------------------------------
746 DEFER TRUC ; here, TRUC is a secondary DEFERred word (i.e. without BODY)
753 ENDCODE ; leaves its execution address (CFA) on stack
757 IS TRUC ; TRUC becomes a primary DEFERred word
758 ; with its default action (DUP) located at its BODY addresse.
760 TRUC . ; display TOS value -->
763 \ ' DROP IS TRUC ; TRUC is redirected to DROP
765 \ TRUC ; The generated error displays stack empty! in reverse video, removes the TRUC definition and restarts the interpretation after the end of the file. And as you see, FastForth is able to display long lines, interesting, doesn't it? -->
768 ' TRUC >BODY IS TRUC ; TRUC is reinitialized with its default action
771 TRUC . ; display TOS value -->