1 \ -*- coding: utf-8 -*-
4 \ to see kernel options, download FastForthSpecs.f
5 \ FastForth kernel options: MSP430ASSEMBLER, CONDCOMP
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 CHIPSTICK_FR2433 MSP_EXP430FR2355
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 \ R4 to R7 must be saved before use and restored after
22 \ scratch registers Y to S are free for use
23 \ under interrupt, IP is free for use
25 \ PUSHM order : PSP,TOS, IP, S, T, W, X, Y, rEXIT,rDOVAR,rDOCON, rDODOES, R3, SR,RSP, PC
26 \ PUSHM order : R15,R14,R13,R12,R11,R10, R9, R8, R7 , R6 , R5 , R4 , R3, R2, R1, R0
28 \ example : PUSHM #6,IP pushes IP,S,T,W,X,Y registers to return stack
30 \ POPM order : PC,RSP, SR, R3, rDODOES,rDOCON,rDOVAR,rEXIT, Y, X, W, T, S, IP,TOS,PSP
31 \ POPM order : R0, R1, R2, R3, R4 , R5 , R6 , R7 , R8, R9,R10,R11,R12,R13,R14,R15
33 \ example : POPM #6,IP pop Y,X,W,T,S,IP registers from return stack
36 \ FORTH conditionnals: unary{ 0= 0< 0> }, binary{ = < > U< }
38 \ ASSEMBLER conditionnal usage with IF UNTIL WHILE S< S>= U< U>= 0= 0<> 0>=
39 \ ASSEMBLER conditionnal usage with ?JMP ?GOTO S< S>= U< U>= 0= 0<> 0<
41 ; ------------------------------------------------------------------------------
43 ; ------------------------------------------------------------------------------
45 \ first, we test for downloading driver only if UART TERMINAL target
50 SUB #308,TOS \ FastForth V3.8
52 'CR' EMIT \ return to column 1 without 'LF'
53 ABORT" FastForth V3.8 please!"
54 PWR_STATE \ remove ABORT_UTILITY definition before resuming
61 [DEFINED] {TOOLS} [IF] {TOOLS} [THEN]
63 [UNDEFINED] {TOOLS} [IF]
68 \ https://forth-standard.org/standard/core/EXIT
69 \ EXIT -- exit a colon definition; CALL #EXIT performs ASMtoFORTH (10 cycles)
70 \ JMP #EXIT performs EXIT
72 MOV @RSP+,IP \ 2 pop previous IP (or next PC) from return stack
73 MOV @IP+,PC \ 4 = NEXT
79 \ https://forth-standard.org/standard/core/SWAP
80 \ SWAP x1 x2 -- x2 x1 swap top two items
89 \ https://forth-standard.org/standard/core/Uless
90 \ U< u1 u2 -- flag test u1<u2, unsigned
93 SUB @PSP+,TOS \ 2 u2-u1
97 AND #0,TOS \ 1 flag Z = 1
104 [UNDEFINED] IF [IF] \ define IF and THEN
105 \ https://forth-standard.org/standard/core/IF
106 \ IF -- IFadr initialize conditional forward branch
110 MOV &DP,TOS \ -- HERE
111 ADD #4,&DP \ compile one word, reserve one word
112 MOV #QFBRAN,0(TOS) \ -- HERE compile QFBRAN
113 ADD #2,TOS \ -- HERE+2=IFadr
117 \ https://forth-standard.org/standard/core/THEN
118 \ THEN IFadr -- resolve forward branch
119 CODE THEN \ immediate
120 MOV &DP,0(TOS) \ -- IFadr
126 [UNDEFINED] BEGIN [IF] \ define BEGIN UNTIL AGAIN WHILE REPEAT
127 \ https://forth-standard.org/standard/core/BEGIN
128 \ BEGIN -- BEGINadr initialize backward branch
133 \ https://forth-standard.org/standard/core/UNTIL
134 \ UNTIL BEGINadr -- resolve conditional backward branch
137 BW1 ADD #4,&DP \ compile two words
139 MOV X,-4(W) \ compile Bran or QFBRAN at HERE
140 MOV TOS,-2(W) \ compile bakcward adr at HERE+2
145 \ https://forth-standard.org/standard/core/AGAIN
146 \ AGAIN BEGINadr -- resolve uncondionnal backward branch
152 \ https://forth-standard.org/standard/core/WHILE
153 \ WHILE BEGINadr -- WHILEadr BEGINadr
158 \ https://forth-standard.org/standard/core/REPEAT
159 \ REPEAT WHILEadr BEGINadr -- resolve WHILE loop
161 POSTPONE AGAIN POSTPONE THEN
165 [UNDEFINED] DO [IF] \ define DO LOOP +LOOP
166 \ https://forth-standard.org/standard/core/DO
167 \ DO -- DOadr L: -- 0
171 ADD #2,&DP \ make room to compile xdo
172 MOV &DP,TOS \ -- HERE+2
173 MOV #XDO,-2(TOS) \ compile xdo
174 ADD #2,&LEAVEPTR \ -- HERE+2 LEAVEPTR+2
176 MOV #0,0(W) \ -- HERE+2 L-- 0
180 \ https://forth-standard.org/standard/core/LOOP
181 \ LOOP DOadr -- L-- an an-1 .. a1 0
182 CODE LOOP \ immediate
184 BW1 ADD #4,&DP \ make room to compile two words
186 MOV X,-4(W) \ xloop --> HERE
187 MOV TOS,-2(W) \ DOadr --> HERE+2
188 BEGIN \ resolve all "leave" adr
189 MOV &LEAVEPTR,TOS \ -- Adr of top LeaveStack cell
190 SUB #2,&LEAVEPTR \ --
191 MOV @TOS,TOS \ -- first LeaveStack value
192 CMP #0,TOS \ -- = value left by DO ?
194 MOV W,0(TOS) \ move adr after loop as UNLOOP adr
200 \ https://forth-standard.org/standard/core/PlusLOOP
201 \ +LOOP adrs -- L-- an an-1 .. a1 0
204 GOTO BW1 \ goto BW1 LOOP
209 \ https://forth-standard.org/standard/core/I
210 \ I -- n R: sys1 sys2 -- sys1 sys2
211 \ get the innermost loop index
213 SUB #2,PSP \ 1 make room in TOS
215 MOV @RSP,TOS \ 2 index = loopctr - fudge
221 [UNDEFINED] DUP [IF] \ define DUP and ?DUP
222 \ https://forth-standard.org/standard/core/DUP
223 \ DUP x -- x x duplicate top of stack
225 BW1 SUB #2,PSP \ 2 push old TOS..
226 MOV TOS,0(PSP) \ 3 ..onto stack
230 \ https://forth-standard.org/standard/core/qDUP
231 \ ?DUP x -- 0 | x x DUP if nonzero
233 CMP #0,TOS \ 2 test for TOS nonzero
239 [UNDEFINED] SWAP [IF]
240 \ https://forth-standard.org/standard/core/SWAP
241 \ SWAP x1 x2 -- x2 x1 swap top two items
251 [UNDEFINED] DROP [IF]
252 \ https://forth-standard.org/standard/core/DROP
253 \ DROP x -- drop top of stack
261 \ https://forth-standard.org/standard/core/toR
262 \ >R x -- R: -- x push to return stack
271 \ https://forth-standard.org/standard/core/Rfrom
272 \ R> -- x R: x -- pop from return stack ; CALL #RFROM performs DOVAR
281 [UNDEFINED] SPACE [IF]
282 \ https://forth-standard.org/standard/core/SPACE
283 \ SPACE -- output a space
288 [UNDEFINED] SPACES [IF]
289 \ https://forth-standard.org/standard/core/SPACES
290 \ SPACES n -- output n spaces
304 MOV @PSP+,TOS \ -- drop n
309 [UNDEFINED] 2DUP [IF] \
310 \ https://forth-standard.org/standard/core/TwoDUP
311 \ 2DUP x1 x2 -- x1 x2 x1 x2 dup top 2 cells
321 \ https://forth-standard.org/standard/core/OnePlus
322 \ 1+ n1/u1 -- n2/u2 add 1 to TOS
330 \ https://forth-standard.org/standard/core/Plus
331 \ + n1/u1 n2/u2 -- n3/u3 add n1+n2
339 \ https://forth-standard.org/standard/core/Minus
340 \ - n1/u1 n2/u2 -- n3/u3 n3 = n1-n2
342 SUB @PSP+,TOS \ 2 -- n2-n1
344 ADD #1,TOS \ 1 -- n3 = -(n2-n1) = n1-n2
350 \ https://forth-standard.org/standard/core/CFetch
351 \ C@ c-addr -- char fetch char from memory
359 \ https://forth-standard.org/standard/core/AND
360 \ C AND x1 x2 -- x3 logical AND
368 \ https://forth-standard.org/standard/core/ROT
369 \ ROT x1 x2 x3 -- x2 x3 x1
371 MOV @PSP,W \ 2 fetch x2
372 MOV TOS,0(PSP) \ 3 store x3
373 MOV 2(PSP),TOS \ 3 fetch x1
374 MOV W,2(PSP) \ 3 store x2
379 [UNDEFINED] MAX [IF] \ define MAX and MIN
380 CODE MAX \ n1 n2 -- n3 signed maximum
387 CODE MIN \ n1 n2 -- n3 signed minimum
395 [UNDEFINED] OVER [IF]
396 \ https://forth-standard.org/standard/core/OVER
397 \ OVER x1 x2 -- x1 x2 x1
399 MOV TOS,-2(PSP) \ 3 -- x1 (x2) x2
400 MOV @PSP,TOS \ 2 -- x1 (x2) x1
401 SUB #2,PSP \ 1 -- x1 x2 x1
406 [UNDEFINED] MOVE [IF]
407 \ https://forth-standard.org/standard/core/MOVE
408 \ MOVE addr1 addr2 u -- smart move
409 \ VERSION FOR 1 ADDRESS UNIT = 1 CHAR
412 MOV @PSP+,Y \ Y = addr2 = dst
413 MOV @PSP+,X \ X = addr1 = src
414 MOV @PSP+,TOS \ pop new TOS
415 CMP #0,W \ count = 0 ?
416 0<> IF \ if 0, already done !
417 CMP X,Y \ Y-X \ dst - src
418 0= ?GOTO FW1 \ already done !
419 U< IF \ U< if src > dst
426 ELSE \ U>= if dst > src
427 ADD W,Y \ copy W bytes beginning with the end
441 [UNDEFINED] .S [IF] \
442 \ https://forth-standard.org/standard/tools/DotS
443 \ .S TOS -- TOS display <depth> of param Stack and stack contents in hedadecimal if not empty
445 MOV TOS,-2(PSP) \ -- TOS ( TOS x x )
446 MOV PSP,TOS \ -- PSP ( TOS x x )
447 SUB #2,TOS \ -- PSP ( TOS x x ) to take count that TOS is first cell
448 MOV TOS,-6(PSP) \ -- TOS ( TOS x PSP )
449 MOV #PSTACK,TOS \ -- P0 ( TOS x PSP )
450 SUB #2,TOS \ -- P0 ( TOS x PSP ) to take count that TOS is first cell
451 BW1 MOV TOS,-4(PSP) \ -- S0 ( TOS S0 PSP ) | -- TOS ( TOS R0 RSP )
452 SUB #6,PSP \ -- TOS S0 PSP S0 | -- TOS R0 RSP R0
453 SUB @PSP,TOS \ -- TOS S0 PSP S0-SP | -- TOS R0 RSP R0-RSP
454 RRA TOS \ -- TOS S0 PSP #cells | -- TOS R0 RSP #cells
459 $3E EMIT SPACE \ char '>' SPACE
463 THEN \ display content of stack in hexadecimal
473 [UNDEFINED] .RS [IF] \
474 \ .RS TOS -- TOS display <depth> of Return Stack and stack contents if not empty
476 MOV TOS,-2(PSP) \ -- TOS ( TOS x x )
477 MOV RSP,-6(PSP) \ -- TOS ( TOS x RSP )
478 MOV #RSTACK,TOS \ -- R0 ( TOS x RSP )
484 \ https://forth-standard.org/standard/tools/q
485 \ ? adr -- display the content of adr
492 [UNDEFINED] WORDS [IF]
493 \ https://forth-standard.org/standard/tools/WORDS
494 \ list all words of vocabulary first in CONTEXT.
497 CONTEXT @ PAD_ORG \ -- VOC_BODY PAD_ORG MOVE all threads of VOC_BODY in PAD_ORG
498 THREADS @ DUP + \ -- VOC_BODY PAD_ORG THREAD*2
499 MOVE \ -- vocabumary entries are copied in PAD_ORG
501 0 DUP \ -- ptr=0 MAX=0
502 THREADS @ DUP + 0 \ -- ptr=0 MAX=0 THREADS*2 0
503 DO \ -- ptr MAX I = PAD_ptr = thread*2
504 DUP I PAD_ORG + @ \ -- ptr MAX MAX NFAx
505 U< IF \ -- ptr MAX if MAX U< NFAx
506 DROP DROP \ -- drop ptr and MAX
507 I DUP PAD_ORG + @ \ -- new_ptr new_MAX
510 ?DUP \ -- ptr MAX MAX | -- ptr 0 (all threads in PAD_ORG = 0)
511 WHILE \ -- ptr MAX replace it by its LFA
513 2 - @ \ -- ptr MAX [LFA]
514 ROT \ -- MAX [LFA] ptr
515 PAD_ORG + \ -- MAX [LFA] thread
516 ! \ -- MAX [LFA]=new_NFA updates PAD_ORG+ptr
518 COUNT $7F AND \ -- MAX addr count (with suppr. of immediate bit)
520 C@ $0F AND \ -- count_of_chars
521 $10 SWAP - SPACES \ -- complete with spaces modulo 16 chars
524 ; \ all threads in PAD_ORG are filled with 0
528 : U.R \ u n -- display u unsigned in n width (n >= 2)
530 R> OVER - 0 MAX SPACES TYPE
534 [UNDEFINED] DUMP [IF] \
535 \ https://forth-standard.org/standard/tools/DUMP
536 CODE DUMP \ adr n -- dump memory
538 PUSH &BASEADR \ save current base
539 MOV #$10,&BASEADR \ HEX base
540 ADD @PSP,TOS \ -- ORG END
542 SWAP 2DUP \ -- END ORG END ORG
543 U. U. \ -- END ORG display org end
544 $FFF0 AND \ -- END ORG_modulo_16
545 DO CR \ generate line
546 I 4 U.R SPACE \ generate address
547 I 8 + I \ display first 8 bytes
550 I $10 + I 8 + \ display last 8 bytes
553 I $10 + I \ display 16 chars
554 DO I C@ $7E MIN $20 MAX EMIT LOOP
556 R> BASEADR ! \ restore current base
558 [THEN] \ endof [UNDEFINED] DUMP
562 [THEN] \ endof [UNDEFINED] {TOOLS}