OSDN Git Service

fc530edffceeba42d84ea370e84bb1bb1b2109a1
[fast-forth/master.git] / MSP430-FORTH / UTILITY.f
1
2 ; ------------------------------------------------------------------------------
3 ; UTILITY.f
4 ; ------------------------------------------------------------------------------
5 \
6 \ to see kernel options, download FastForthSpecs.f
7 \ FastForth kernel options: MSP430ASSEMBLER, CONDCOMP
8 \
9 \ TARGET SELECTION
10 \ MSP_EXP430FR5739  MSP_EXP430FR5969    MSP_EXP430FR5994    MSP_EXP430FR6989
11 \ MSP_EXP430FR4133  MSP_EXP430FR2433    MSP_EXP430FR2355    CHIPSTICK_FR2433
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, rEXIT,rDOVAR,rDOCON, rDODOES, R3, SR,RSP, PC
19 \ PUSHM order : R15,R14,R13,R12,R11,R10, R9, R8,  R7  ,  R6  ,  R5  ,   R4   , R3, R2, R1, R0
20 \
21 \ example : PUSHM #6,IP pushes IP,S,T,W,X,Y registers to return stack
22 \
23 \ POPM  order :  PC,RSP, SR, R3, rDODOES,rDOCON,rDOVAR,rEXIT,  Y,  X,  W,  T,  S, IP,TOS,PSP
24 \ POPM  order :  R0, R1, R2, R3,   R4   ,  R5  ,  R6  ,  R7 , R8, R9,R10,R11,R12,R13,R14,R15
25 \
26 \ example : POPM #6,IP   pop Y,X,W,T,S,IP registers from return stack
27 \
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 ?JMP ?GOTO      S<  S>=  U<   U>=  0=  0<>  0<
33
34 PWR_STATE
35
36 [DEFINED] {TOOLS} [IF] {TOOLS} [THEN]     \ remove {UTILITY} if outside core 
37
38 [UNDEFINED] {TOOLS} [IF]  \ don't replicate {UTILITY} if inside core
39
40 MARKER {TOOLS} 
41
42 [UNDEFINED] ? [IF]    \
43 \ https://forth-standard.org/standard/tools/q
44 \ ?         adr --            display the content of adr
45 CODE ?          
46     MOV @TOS,TOS
47     MOV #U.,PC  \ goto U.
48 ENDCODE
49 [THEN]
50
51 [UNDEFINED] .S [IF]    \
52 \ https://forth-standard.org/standard/tools/DotS
53 \ .S            --            display <depth> of param Stack and stack contents if not empty
54 CODE .S
55     MOV     TOS,-2(PSP) \ -- TOS ( tos x x )
56     MOV     PSP,TOS
57     SUB     #2,TOS      \ to take count that TOS is first cell
58     MOV     TOS,-6(PSP) \ -- TOS ( tos x  PSP )
59     MOV     #PSTACK,TOS \ -- P0  ( tos x  PSP )
60     SUB     #2,TOS      \ to take count that TOS is first cell
61 BW1 MOV     TOS,-4(PSP) \ -- S0  ( tos S0 SP )
62     SUB     #6,PSP      \ -- S0 SP S0
63     SUB     @PSP,TOS    \ -- S0 SP S0-SP
64     RRA     TOS         \ -- S0 SP #cells
65 COLON
66     $3C EMIT            \ char '<'
67     .                   \ display #cells
68     $08 EMIT            \ backspace
69     $3E EMIT SPACE      \ char '>' SPACE
70     OVER OVER >         \ 
71     0= IF 
72         DROP DROP EXIT
73     THEN
74     DO 
75         I @ U.
76     2 +LOOP
77 ;
78 [THEN]
79
80 [UNDEFINED] .RS [IF]    \
81 \ .RS            --            display <depth> of Return Stack and stack contents if not empty
82 CODE .RS
83     MOV     TOS,-2(PSP) \ -- TOS ( tos x x ) 
84     MOV     RSP,-6(PSP) \ -- TOS ( tos x  RSP )
85     MOV     #RSTACK,TOS \ -- R0  ( tos x  RSP )
86     GOTO    BW1
87 ENDCODE
88 [THEN]
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 CR ."    "                          \
115 CONTEXT @                           \ -- VOC_BODY                   MOVE all threads of VOC_BODY in PAD
116     PAD INI_THREAD @ DUP +          \ -- VOC_BODY PAD THREAD*2
117     MOVE                            \
118     BEGIN                           \ -- 
119         0.                          \ -- ptr=0 MAX=0                
120         INI_THREAD @ DUP + 0        \ -- ptr=0 MAX=0 THREADS*2 0
121             DO                      \ -- ptr MAX            I =  PAD_ptr = thread*2
122             DUP I PAD + @           \ -- ptr MAX MAX NFAx
123                 U< IF               \ -- ptr MAX            if MAX U< NFAx
124                     DROP DROP       \ --                    drop ptr and MAX
125                     I DUP PAD + @   \ -- new_ptr new_MAX
126                 THEN                \ 
127             2 +LOOP                 \ -- ptr MAX
128         ?DUP                        \ -- ptr MAX MAX | -- ptr 0  
129     WHILE                           \ -- ptr MAX                    replace it by its LFA
130         DUP                         \ -- ptr MAX MAX
131         2 - @                       \ -- ptr MAX [LFA]
132         ROT                         \ -- MAX [LFA] ptr
133         PAD +                       \ -- MAX [LFA] thread
134         !                           \ -- MAX                [LFA]=new_NFA --> PAD+ptr   type it in 16 chars format
135         DUP                         \ -- MAX MAX
136         COUNT $7F AND               \ -- MAX addr count (with suppr. of immediate bit)
137         TYPE                        \ -- MAX
138         C@ $0F AND                  \ -- count_of_chars
139         $10 SWAP - SPACES           \ --                    complete with spaces modulo 16 chars
140     REPEAT                          \ -- ptr
141     DROP                            \ --
142 ;
143 [THEN]
144
145 [UNDEFINED] MAX [IF]    \ MAX and MIN are defined in {ANS_COMP}
146     CODE MAX    \    n1 n2 -- n3       signed maximum
147         CMP @PSP,TOS    \ n2-n1
148         S< ?GOTO FW1    \ n2<n1
149     BW1 ADD #2,PSP
150         MOV @IP+,PC
151     ENDCODE
152     \
153
154     CODE MIN    \    n1 n2 -- n3       signed minimum
155         CMP @PSP,TOS    \ n2-n1
156         S< ?GOTO BW1    \ n2<n1
157     FW1 MOV @PSP+,TOS
158         MOV @IP+,PC
159     ENDCODE
160 [THEN]
161
162 [UNDEFINED] U.R [IF]
163 : U.R                       \ u n --           display u unsigned in n width (n >= 2)
164 >R  <# 0 # #S #>  
165 R> OVER - 0 MAX SPACES TYPE
166 ;
167 [THEN]
168
169 [UNDEFINED] DUMP [IF]    \
170 \ https://forth-standard.org/standard/tools/DUMP
171 CODE DUMP                   \ adr n  --   dump memory
172 PUSH IP
173 PUSH &BASE                  \ save current base
174 MOV #$10,&BASE              \ HEX base
175 ADD @PSP,TOS                \ -- ORG END
176 LO2HI
177   SWAP OVER OVER            \ -- END ORG END ORG 
178   U. U.                     \ -- END ORG        display org end 
179   $FFF0 AND                 \ -- END ORG_modulo_16
180   DO  CR                    \ generate line
181     I 7 U.R SPACE           \ generate address
182       I $10 + I             \ display 16 bytes
183       DO I C@ 3 U.R LOOP  
184       SPACE SPACE
185       I $10 + I             \ display 16 chars
186       DO I C@ $7E MIN BL MAX EMIT LOOP
187   $10 +LOOP
188   R> BASE !                 \ restore current base
189 ;
190 [THEN]  \ of [UNDEFINED] DUMP
191
192 PWR_HERE
193 ECHO
194 [ELSE]
195 ECHO
196 ; already exists
197 [THEN]  \ of [UNDEFINED] {TOOLS}
198