1 \ -*- coding: utf-8 -*-
3 ; -----------------------------------------------------------------------
5 ; -----------------------------------------------------------------------
8 \ MSP_EXP430FR5739 MSP_EXP430FR5969 MSP_EXP430FR5994 MSP_EXP430FR6989
9 \ MSP_EXP430FR4133 MSP_EXP430FR2433 MSP_EXP430FR2355 CHIPSTICK_FR2433
11 \ PUSHM order : PSP,TOS, IP, S, T, W, X, Y, rEXIT,rDOVAR,rDOCON, rDODOES, R3, SR,RSP, PC
12 \ PUSHM order : R15,R14,R13,R12,R11,R10, R9, R8, R7 , R6 , R5 , R4 , R3, R2, R1, R0
14 \ example : PUSHM #6,IP pushes IP,S,T,W,X,Y registers to return stack
16 \ POPM order : PC,RSP, SR, R3, rDODOES,rDOCON,rDOVAR,rEXIT, Y, X, W, T, S, IP,TOS,PSP
17 \ POPM order : R0, R1, R2, R3, R4 , R5 , R6 , R7 , R8, R9,R10,R11,R12,R13,R14,R15
19 \ example : POPM #6,IP pop Y,X,W,T,S,IP registers from return stack
21 \ ASSEMBLER conditionnal usage after IF UNTIL WHILE : S< S>= U< U>= 0= 0<> 0>=
22 \ ASSEMBLER conditionnal usage before ?JMP ?GOTO : S< S>= U< U>= 0= 0<> 0<
24 \ FORTH conditionnal : 0= 0< = < > U<
27 \ https://forth-standard.org/standard/core/Plus
28 \ + n1/u1 n2/u2 -- n3/u3 add n1+n2
35 [UNDEFINED] MAX [IF] \ MAX and MIN are defined in {UTILITY}
37 CODE MAX \ n1 n2 -- n3 signed maximum
44 CODE MIN \ n1 n2 -- n3 signed minimum
54 \ https://forth-standard.org/standard/core/CFetch
55 \ C@ c-addr -- char fetch char from memory
62 [UNDEFINED] VARIABLE [IF]
63 \ https://forth-standard.org/standard/core/VARIABLE
64 \ VARIABLE <name> -- define a Forth VARIABLE
69 MOV #DOVAR,-4(W) \ CFA = DOVAR
74 [UNDEFINED] CONSTANT [IF]
75 \ https://forth-standard.org/standard/core/CONSTANT
76 \ CONSTANT <name> n -- define a Forth CONSTANT
81 MOV #DOCON,-4(W) \ CFA = DOCON
82 MOV TOS,-2(W) \ PFA = n
88 [UNDEFINED] SPACE [IF]
89 \ https://forth-standard.org/standard/core/SPACE
90 \ SPACE -- output a space
95 [UNDEFINED] SPACES [IF]
96 \ https://forth-standard.org/standard/core/SPACES
97 \ SPACES n -- output n spaces
111 MOV @PSP+,TOS \ -- drop n
116 [UNDEFINED] OVER [IF]
117 \ https://forth-standard.org/standard/core/OVER
118 \ OVER x1 x2 -- x1 x2 x1
120 MOV TOS,-2(PSP) \ 3 -- x1 (x2) x2
121 MOV @PSP,TOS \ 2 -- x1 (x2) x1
122 SUB #2,PSP \ 1 -- x1 x2 x1
127 [UNDEFINED] U.R [IF] \ defined in {UTILITY}
128 : U.R \ u n -- display u unsigned in n width (n >= 2)
130 R> OVER - 0 MAX SPACES TYPE
134 [UNDEFINED] DUMP [IF] \ defined in {UTILITY}
135 \ https://forth-standard.org/standard/tools/DUMP
136 CODE DUMP \ adr n -- dump memory
138 PUSH &BASEADR \ save current base
139 MOV #$10,&BASEADR \ HEX base
140 ADD @PSP,TOS \ -- ORG END
143 DO CR \ generate line
144 I 4 U.R SPACE \ generate address
151 I $10 + I \ display 16 chars
152 DO I C@ $7E MIN $20 MAX EMIT LOOP
154 R> BASEADR ! \ restore current base
158 \ -----------------------------------------------------------------------
159 \ test CPUx instructions PUSHM, POPM, RLAM, RRAM, RRCM, RRUM
160 \ -----------------------------------------------------------------------
163 \ PUSHM #16,R14 \ uncomment to test error "out of bounds"
164 \ PUSHM #2,R0 \ uncomment to test error "out of bounds"
165 \ PUSHM #0,IP \ uncomment to test error "out of bounds"
166 \ POPM #17,R15 \ uncomment to test error "out of bounds"
167 \ POPM #2,R0 \ uncomment to test error "out of bounds"
168 \ POPM #0,IP \ uncomment to test error "out of bounds"
175 PUSHM #4,IP \ PUSHM IP,S,T,W
176 POPM #4,IP \ POPM W,T,S,IP
178 MOV TOS,8(PSP) \ save old TOS
184 \ RLAM #0,TOS \ uncomment to test error "out of bounds"
185 \ RLAM #5,TOS \ uncomment to test error "out of bounds"
186 RRAM #1,TOS \ 0 < shift value < 5
190 COLON \ high level part of the word starts here...
192 ; \ and finishes here.
194 TESTPUSHM ; you should see 11111 3 2 1 0 -->
197 GOTO BW1 \ JMP TESTPUSHM
201 TESTPOPM ; you should see 11111 3 2 1 0 -->
205 \ -----------------------------------------------------------------------
206 \ test symbolic branch in assembler
207 \ test a FORTH section encapsulated in an assembly word
208 \ -----------------------------------------------------------------------
209 CODE TEST1 \ the word "CODE" add ASSEMBLER as CONTEXT vocabulary...
211 MOV &BASEADR,&BASEADR \ to test &xxxx src operand
213 0<> IF MOV #2,&BASEADR \ if base <> 2
214 ELSE MOV #$0A,&BASEADR \ else base = 2
216 COLON \ tips : no "ok" displayed in start of line <==> compilation mode
217 BASEADR @ U. \ always display 10 !
221 \ -----------------------------------------------------------------------
222 \ test a word that starts as word FORTH and ends as assembly word
223 \ -----------------------------------------------------------------------
224 : TEST2 \ ":" starts compilation
225 BASEADR @ U. \ always display 10 !
226 HI2LO \ switch FORTH to ASM : compile one word (next address)
227 \ add vocabulary ASSEMBLER as CONTEXT vocabulary
228 \ switch in interpret mode
230 0<> IF MOV #2, &BASEADR \ if variable system BASE <> 2
231 ELSE MOV #10,&BASEADR \ else (BASE = 2)
233 \ MOV #EXIT,PC \ to pair with ":" i.e. to restore IP saved by : then execute NEXT.
234 \ but even compile two words, it's better to compile an inline EXIT :
235 MOV @RSP+,IP \ restore IP
237 ENDCODE \ ends assembler : remove vocabulary ASSEMBLER from CONTEXT
240 \ -----------------------------------------------------------------------
241 \ test a word that starts as assembly word and ends as FORTH word
242 \ -----------------------------------------------------------------------
243 CODE TEST3 \ "CODE" starts assembler, i.e. add ASSEMBLER as CONTEXT vocabulary
245 0<> IF MOV #2, &BASEADR \ if variable system BASE <> 2
246 ELSE MOV #10,&BASEADR \ else (BASE = 2)
248 BASEADR @ U. \ always display 10 !
253 \ -----------------------------------------------------------------------
254 \ test an assembly jump spanning a section written in FORTH
255 \ -----------------------------------------------------------------------
261 MOV #%1010,TOS \ init count = 10
264 \ IP is already saved by word ":"
265 DUP U. \ display count
268 0= UNTIL MOV @PSP+,TOS
269 \ MOV #EXIT,PC \ to pair with ":" i.e. to restore IP saved by : then execute NEXT.
270 MOV @RSP+,IP \ restore IP
274 TEST5 ; you should see : 9 8 7 6 5 4 3 2 1 0 -->
277 \ -----------------------------------------------------------------------
278 \ tests indexing address
279 \ -----------------------------------------------------------------------
282 \ https://forth-standard.org/standard/core/CComma
283 \ C, char -- append char
294 \ https://forth-standard.org/standard/core/CFetch
295 \ C@ c-addr -- char fetch char from memory
310 8 BYTES_TABLE_IDX BYTES_TABLE \ create table "BYTES_TABLE" with bytes content = 0,1,2,3,4,5,6,7
312 2 BYTES_TABLE C@ . ; you should see 2 -->
316 VARIABLE BYTES_TABLE1
318 $0201 BYTES_TABLE1 ! \ words written in memory are little endian !
320 CODE IDX_TEST1 \ index -- value
321 MOV.B BYTES_TABLE1(TOS),TOS \ -- value
326 0 IDX_TEST1 ; you should see 1 -->
334 1 TEST6 . ; you should see 1 -->
337 \ -----------------------------------------------------------------------
338 \ tests access to a CREATED word with assembler
339 \ -----------------------------------------------------------------------
368 TABLE 2 - CONSTANT PFA_TABLE \ PFA_TABLE leave the PFA of TABLE
371 CODE REDIRECT ; <table> -- redirects TABLE to argument <table>
378 CODE REDIRECT0 ; -- redirects TABLE to TABLE0
379 MOV #TABLE0,&PFA_TABLE
384 CODE REDIRECT10 ; -- redirects TABLE to TABLE10
385 MOV #TABLE10,&PFA_TABLE
390 CODE REDIRECT20 ; -- redirects TABLE to TABLE20
391 MOV #TABLE20,&PFA_TABLE
403 TABLE0 REDIRECT TABLE 10 DUMP
405 TABLE10 REDIRECT TABLE 10 DUMP
407 TABLE20 REDIRECT TABLE 10 DUMP
410 REDIRECT0 TABLE 10 DUMP
412 REDIRECT10 TABLE 10 DUMP
414 REDIRECT20 TABLE 10 DUMP
417 TABLE0 PFA_TABLE ! TABLE 10 DUMP
419 TABLE10 PFA_TABLE ! TABLE 10 DUMP
421 TABLE20 PFA_TABLE ! TABLE 10 DUMP
424 \ -----------------------------------------------------------------------
425 \ tests behaviour of assembly error
426 \ -----------------------------------------------------------------------
427 \ R16 causes an error, assembler context is aborted and the word TEST7 is "hidden".
430 \ MOV 0(truc),0(R16) ; display an error "out of bounds" -->
432 ; -----------------------------------------------------------------------
433 ; create a primary DEFERred assembly word
434 ; -----------------------------------------------------------------------
437 DEFER TRUC ; here, TRUC is a secondary DEFERred word (i.e. without BODY)
441 CODENNM ; leaves its execution address (CFA) on stack
449 IS TRUC ; TRUC becomes a primary DEFERred word
450 ; with its default action (DUP) located at its BODY addresse.
452 TRUC . ; display TOS value -->
455 ' TRUC >BODY IS TRUC ; TRUC is reinitialzed with its default action
458 TRUC . ; display TOS value -->
460 \ ' DROP IS TRUC ; TRUC is redirected to DROP
462 \ 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? -->