OSDN Git Service

V205
[fast-forth/master.git] / MSP430-FORTH / UTILITY.f
1 \ ------------------------------------------------------------------------------
2 \ UTILITY.f
3 \ ------------------------------------------------------------------------------
4
5 \ must be preprocessed with yourtarget.pat file because PSTACK,CONTEXT,INI_THREAD:
6
7 \ TARGET SELECTION: selects to highlight target then CTRL+6
8 \ MSP_EXP430FR5739  MSP_EXP430FR5969    MSP_EXP430FR5994    MSP_EXP430FR6989
9 \ MSP_EXP430FR4133  MSP_EXP430FR2433    MSP_EXP430FR2355    CHIPSTICK_FR2433
10
11 \ ------------------------------------------------------------------------------
12
13 \ REGISTERS USAGE
14 \ R4 to R7 must be saved before use and restored after
15 \ scratch registers Y to S are free for use
16 \ under interrupt, IP is free for use
17
18 \ PUSHM order : PSP,TOS, IP,  S,  T,  W,  X,  Y, R7, R6, R5, R4
19 \ example : PUSHM IP,Y
20 \
21 \ POPM  order :  R4, R5, R6, R7,  Y,  X,  W,  T,  S, IP,TOS,PSP
22 \ example : POPM Y,IP
23
24 \ FORTH conditionnals:  unary{ 0= 0< 0> }, binary{ = < > U< }
25
26 \ ASSEMBLER conditionnal usage with IF UNTIL WHILE  S<  S>=  U<   U>=  0=  0<>  0>=
27
28 \ ASSEMBLER conditionnal usage with ?JMP ?GOTO      S<  S>=  U<   U>=  0=  0<>  <0
29
30 \ ------------------------------------------------------------------------------
31
32     \
33 [DEFINED] {TOOLS} [IF] {TOOLS} [THEN]     \ remove {UTILITY} if outside core 
34     \
35 [UNDEFINED] {TOOLS} [IF]  \ don't replicate {UTILITY} if inside core
36     \
37 MARKER {TOOLS} 
38     \
39 [UNDEFINED] ? [IF]    \
40 \ https://forth-standard.org/standard/tools/q
41 \ ?         adr --            display the content of adr
42 CODE ?          
43     MOV @TOS,TOS
44     MOV #U.,PC  \ goto U.
45 ENDCODE
46 [THEN]
47     \
48
49 [UNDEFINED] .S [IF]    \
50 \ https://forth-standard.org/standard/tools/DotS
51 \ .S            --            display <depth> of param Stack and stack contents if not empty
52 CODE .S
53     MOV     TOS,-2(PSP) \ -- TOS ( tos x x )
54     MOV     PSP,TOS
55     SUB     #2,TOS      \ to take count that TOS is first cell
56     MOV     TOS,-6(PSP) \ -- TOS ( tos x  PSP )
57     MOV     #PSTACK,TOS \ -- P0  ( tos x  PSP )
58     SUB     #2,TOS      \ to take count that TOS is first cell
59 BW1 MOV     TOS,-4(PSP) \ -- S0  ( tos S0 SP )
60     SUB     #6,PSP      \ -- S0 SP S0
61     SUB     @PSP,TOS    \ -- S0 SP S0-SP
62     RRA     TOS         \ -- S0 SP #cells
63 COLON
64     $3C EMIT            \ char '<'
65     .                   \ display #cells
66     $08 EMIT            \ backspace
67     $3E EMIT SPACE      \ char '>' SPACE
68     OVER OVER >         \ 
69     0= IF 
70         DROP DROP EXIT
71     THEN
72     DO 
73         I @ U.
74     2 +LOOP
75 ;
76 [THEN]
77     \
78
79 [UNDEFINED] .RS [IF]    \
80 \ .RS            --            display <depth> of Return Stack and stack contents if not empty
81 CODE .RS
82     MOV     TOS,-2(PSP) \ -- TOS ( tos x x ) 
83     MOV     RSP,-6(PSP) \ -- TOS ( tos x  RSP )
84     MOV     #RSTACK,TOS \ -- R0  ( tos x  RSP )
85     GOTO    BW1
86 ENDCODE
87 [THEN]
88     \
89
90 [UNDEFINED] WORDS [IF]
91     \
92 [UNDEFINED] AND [IF]
93     \
94 \ https://forth-standard.org/standard/core/AND
95 \ C AND    x1 x2 -- x3           logical AND
96 CODE AND
97 AND @PSP+,TOS
98 MOV @IP+,PC
99 ENDCODE
100     \
101 [THEN]
102     \
103 [UNDEFINED] PAD [IF]
104     \
105 \ https://forth-standard.org/standard/core/PAD
106 \ C PAD    -- addr
107 PAD_ORG CONSTANT PAD
108     \
109 [THEN]
110     \
111 \ https://forth-standard.org/standard/tools/WORDS
112 \ list all words of vocabulary first in CONTEXT.
113 : WORDS                             \ --            
114
115 \ \ vvvvvvvv   may be skipped    vvvvvvvv
116 \ BASE @                              \ -- BASE
117 \ #10 BASE !
118 \ CR ."    "
119 \ INI_THREAD @ DUP
120 \ 1 = IF DROP ." monothread"
121 \     ELSE . ." threads"
122 \     THEN ."  vocabularies"
123 \ BASE !                              \ --
124 \ \ ^^^^^^^^   may be skipped    ^^^^^^^^
125
126 CR ."    "                          \
127 CONTEXT @                           \ -- VOC_BODY                   MOVE all threads of VOC_BODY in PAD
128     PAD INI_THREAD @ DUP +          \ -- VOC_BODY PAD THREAD*2
129     MOVE                            \
130     BEGIN                           \ -- 
131 \        0 DUP                       \ -- ptr=0 MAX=0                select the MAX of NFAs in all vocabulary threads
132         0.                          \ -- ptr=0 MAX=0                
133         INI_THREAD @ DUP + 0        \ -- ptr=0 MAX=0 THREADS*2 0
134             DO                      \ -- ptr MAX            I =  PAD_ptr = thread*2
135             DUP I PAD + @           \ -- ptr MAX MAX NFAx
136                 U< IF               \ -- ptr MAX            if MAX U< NFAx
137                     DROP DROP       \ --                    drop ptr and MAX
138                     I DUP PAD + @   \ -- new_ptr new_MAX
139                 THEN                \ 
140             2 +LOOP                 \ -- ptr MAX
141         ?DUP                        \ -- ptr MAX MAX | -- ptr 0  
142     WHILE                           \ -- ptr MAX                    replace it by its LFA
143         DUP                         \ -- ptr MAX MAX
144         2 - @                       \ -- ptr MAX [LFA]
145         ROT                         \ -- MAX [LFA] ptr
146         PAD +                       \ -- MAX [LFA] thread
147         !                           \ -- MAX                [LFA]=new_NFA --> PAD+ptr   type it in 16 chars format
148         DUP                         \ -- MAX MAX
149         COUNT $7F AND               \ -- MAX addr count (with suppr. of immediate bit)
150         TYPE                        \ -- MAX
151         C@ $0F AND                  \ -- count_of_chars
152         $10 SWAP - SPACES           \ --                    complete with spaces to 16 chars
153     REPEAT                          \ -- ptr
154     DROP                            \ --
155 ;
156 [THEN]
157     \
158
159 [UNDEFINED] MAX [IF]    \ MAX and MIN are defined in {ANS_COMP}
160     CODE MAX    \    n1 n2 -- n3       signed maximum
161         CMP @PSP,TOS    \ n2-n1
162         S< ?GOTO FW1    \ n2<n1
163     BW1 ADD #2,PSP
164         MOV @IP+,PC
165     ENDCODE
166     \
167
168     CODE MIN    \    n1 n2 -- n3       signed minimum
169         CMP @PSP,TOS    \ n2-n1
170         S< ?GOTO BW1    \ n2<n1
171     FW1 MOV @PSP+,TOS
172         MOV @IP+,PC
173     ENDCODE
174 [THEN]
175     \
176
177 [UNDEFINED] U.R [IF]
178 : U.R                       \ u n --           display u unsigned in n width (n >= 2)
179 >R  <# 0 # #S #>  
180 R> OVER - 0 MAX SPACES TYPE
181 ;
182 [THEN]
183     \
184
185 [UNDEFINED] DUMP [IF]    \
186 \ https://forth-standard.org/standard/tools/DUMP
187 CODE DUMP                   \ adr n  --   dump memory
188 PUSH IP
189 PUSH &BASE                  \ save current base
190 MOV #$10,&BASE              \ HEX base
191 ADD @PSP,TOS                \ -- ORG END
192 LO2HI
193   SWAP OVER OVER            \ -- END ORG END ORG 
194   U. U.                     \ -- END ORG        display org end 
195   $FFF0 AND                 \ -- END ORG_modulo_16
196   DO  CR                    \ generate line
197     I 7 U.R SPACE           \ generate address
198       I $10 + I             \ display 16 bytes
199       DO I C@ 3 U.R LOOP  
200       SPACE SPACE
201       I $10 + I             \ display 16 chars
202       DO I C@ $7E MIN BL MAX EMIT LOOP
203   $10 +LOOP
204   R> BASE !                 \ restore current base
205 ;
206 [THEN]
207     \
208
209 [THEN]
210     \
211 ECHO
212     \
213 PWR_HERE
214     \
215 : BS 8 EMIT ;
216     \
217 : ESC #27 EMIT ;
218     \
219 : specs         \ to see Fast Forth specifications
220 PWR_STATE       \ remove specs definition when running, and before bytes free processing
221 6 0 DO BS LOOP  \ 8 EMIT = BackSpace EMIT
222 ESC ." [7m"     \ set reverse video
223 ." FastForth "
224 INI_THREAD @ U. ." Threads "    \ vocabularies threads
225 FREQ_KHZ @ 0 1000 UM/MOD U. BS ." ," U. ." MHz "    \ MCLK
226 HECTOBAUDS @ 0 10 UM/MOD U. BS ." ," U. ." kBds "   \ Terminal Baudrate 
227 FRAM_FULL HERE - U. ." bytes free"
228 ESC ." [0m"    \ reset reverse video
229 ;
230     \
231
232 specs