OSDN Git Service

V161
[fast-forth/master.git] / MSP430-FORTH / SD_TOOLS.f
1 ; ------------------------------------------------------------------------
2 ; BASIC TOOLS for SD Card : {DIR FAT SECTOR CLUSER} DUMP ; include UTILITY
3 ; ------------------------------------------------------------------------
4
5 \ Fast Forth For Texas Instrument MSP430FRxxxx FRAM devices
6 \ Copyright (C) <2017>  <J.M. THOORENS>
7 \
8 \ This program is free software: you can redistribute it and/or modify
9 \ it under the terms of the GNU General Public License as published by
10 \ the Free Software Foundation, either version 3 of the License, or
11 \ (at your option) any later version.
12 \
13 \ This program is distributed in the hope that it will be useful,
14 \ but WITHOUT ANY WARRANTY; without even the implied warranty of
15 \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 \ GNU General Public License for more details.
17 \
18 \ You should have received a copy of the GNU General Public License
19 \ along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 \ REGISTERS USAGE
22 \ R4 to R7 must be saved before use and restored after
23 \ scratch registers Y to S are free for use
24 \ under interrupt, IP is free for use
25
26 \ PUSHM order : PSP,TOS, IP,  S,  T,  W,  X,  Y, R7, R6, R5, R4
27 \ example : PUSHM IP,Y
28 \
29 \ POPM  order :  R4, R5, R6, R7,  Y,  X,  W,  T,  S, IP,TOS,PSP
30 \ example : POPM Y,IP
31
32 \ ASSEMBLER conditionnal usage after IF UNTIL WHILE : S< S>= U< U>= 0= 0<> 0>=
33 \ ASSEMBLER conditionnal usage before GOTO ?GOTO     : S< S>= U< U>= 0= 0<> <0 
34
35 \ FORTH conditionnal usage after IF UNTIL WHILE : 0= 0< = < > U<
36
37
38
39
40 \ ECHO      ; if an error occurs, uncomment this line before new download to find it.
41
42     \
43
44 CODE ?          \ adr --            display the content of adr
45     MOV @TOS,TOS
46     MOV #U.,PC  \ goto U.
47 ENDCODE
48     \
49
50 CODE SP@        \ -- SP
51     SUB #2,PSP
52     MOV TOS,0(PSP)
53     MOV PSP,TOS
54     MOV @IP+,PC
55 ENDCODE
56
57 : .S                \ --            print <number> of cells and stack contents if not empty
58 $3C EMIT           \ --            char "<"
59 DEPTH .
60 8 EMIT              \               backspace
61 $3E EMIT SPACE     \               char ">"
62 SP@  PSTACK OVER OVER U<    \
63 IF  2 -
64     DO I @ U.
65     -2 +LOOP
66 ELSE
67     DROP DROP
68 THEN
69 ;
70     \
71
72 : WORDS                             \ --            list all words in all dicts in CONTEXT.
73
74 \ vvvvvvvv   may be skipped    vvvvvvvv
75 BASE @                              \ -- BASE
76 #10 BASE !
77 CR ."    "
78 INI_THREAD @ DUP
79 1 = IF DROP ." monothread"
80     ELSE . ." threads"
81     THEN ."  vocabularies"
82 BASE !                              \ --
83 \ ^^^^^^^^   may be skipped    ^^^^^^^^
84
85 CONTEXT                             \ -- CONTEXT
86 BEGIN                               \                                       search dictionnary
87     DUP 
88     2 + SWAP                        \ -- CONTEXT+2 CONTEXT
89     @ ?DUP                          \ -- CONTEXT+2 (VOC_BODY VOC_BODY or 0)
90 WHILE                               \ -- CONTEXT+2 VOC_BODY                  dictionnary found
91 CR ."    "                          \
92 \   MOVE all threads of VOC_BODY in PAD
93     DUP PAD INI_THREAD @ DUP +      \ -- CONTEXT+2 VOC_BODY  VOC_BODY PAD THREAD*2
94     MOVE                            \         char MOVE
95
96     BEGIN                           \ -- CONTEXT+2 VOC_BODY
97         0 DUP                       \ -- CONTEXT+2 VOC_BODY ptr MAX
98 \   select the MAX of NFA in threads
99         INI_THREAD @ DUP + 0 DO     \         ptr = threads*2
100         DUP I PAD + @               \ -- CONTEXT+2 VOC_BODY ptr MAX MAX NFAx
101         U< IF 
102             DROP DROP I DUP PAD + @ \ -- CONTEXT+2 VOC_BODY ptr MAX          if MAX U< NFAx replace adr and MAX
103         THEN                        \ 
104         2 +LOOP                     \ -- CONTEXT+2 VOC_BODY ptr MAX
105         ?DUP                        \ -- CONTEXT+2 VOC_BODY ptr MAX          max NFA = 0 ? end of vocabulary ?
106     WHILE                           \ -- CONTEXT+2 VOC_BODY ptr MAX
107 \   replace it by its LFA
108         DUP                         \ -- CONTEXT+2 VOC_BODY ptr MAX MAX
109         2 - @                       \ -- CONTEXT+2 VOC_BODY ptr MAX [LFA]
110         ROT                         \ -- CONTEXT+2 VOC_BODY MAX [LFA] ptr
111         PAD +                       \ -- CONTEXT+2 VOC_BODY MAX [LFA] thread
112         !                           \ -- CONTEXT+2 VOC_BODY MAX
113 \   type it in 16 chars format
114                 DUP                 \ -- CONTEXT+2 VOC_BODY MAX MAX
115             COUNT $7F AND TYPE      \ -- CONTEXT+2 VOC_BODY MAX
116                 C@ $0F AND          \ -- 
117                 $10 SWAP - SPACES   \ -- CONTEXT+2 VOC_BODY 
118 \   search next MAX of NFA 
119     REPEAT
120                                     \ -- CONTEXT+2 VOC_BODY 0
121     DROP DROP                       \ -- CONTEXT+2
122     CR         
123 \   repeat for each CONTEXT vocabulary
124
125 REPEAT                              \ -- 0
126 DROP                                \ --
127 ;
128     \
129
130 CODE MAX    \    n1 n2 -- n3       signed maximum
131     CMP @PSP,TOS    \ n2-n1
132     S<  ?GOTO FW1   \ n2<n1
133 BW1 ADD #2,PSP
134     MOV @IP+,PC
135 ENDCODE
136     \
137
138 CODE MIN    \    n1 n2 -- n3       signed minimum
139     CMP @PSP,TOS     \ n2-n1
140     S<  ?GOTO BW1    \ n2<n1
141 FW1 MOV @PSP+,TOS
142     MOV @IP+,PC
143 ENDCODE
144     \
145
146 : U.R                       \ u n --           display u unsigned in n width (n >= 2)
147   >R  <# 0 # #S #>  
148   R> OVER - 0 MAX SPACES TYPE
149 ;
150     \
151
152 : DUMP                      \ adr n  --   dump memory
153   BASE @ >R $10 BASE !
154   SWAP $FFF0 AND SWAP
155   OVER + SWAP
156   DO  CR                    \ generate line
157     I 7 U.R SPACE           \ generate address
158       I $10 + I             \ display 16 bytes
159       DO I C@ 3 U.R LOOP  
160       SPACE SPACE
161       I $10 + I             \ display 16 chars
162       DO I C@ $7E MIN BL MAX EMIT LOOP
163   $10 +LOOP
164   R> BASE !
165 ;
166     \
167
168 \ display content of a sector
169 \ ----------------------------------\
170 CODE SECT_D                         \ sector. --     don't forget to add decimal point to your sector number
171 \ ----------------------------------\
172     MOV     TOS,X                   \ X = SectorH
173     MOV     @PSP,W                  \ W = sectorL
174     CALL    &ReadSectorWX           \ W = SectorLO  X = SectorHI
175 COLON                               \
176     UD.                             \ display the sector number
177     BUFFER $200 DUMP CR ;           \ then dump the sector
178 \ ----------------------------------\
179     \
180
181 \ ----------------------------------\
182 CODE FAT_D                          \ Display CurFATsector
183 \ ----------------------------------\
184     SUB     #4,PSP                  \
185     MOV     TOS,2(PSP)              \
186     MOV     &CurFATsector,0(PSP)    \ FATsectorLO
187     ADD     &OrgFAT1,0(PSP)         \
188     MOV     #0,TOS                  \ FATsectorHI = 0
189     JMP     SECT_D                  \ jump to a defined word
190 ENDCODE
191 \ ----------------------------------\
192     \
193
194 \ display first sector of a Cluster
195 \ ----------------------------------\
196 CODE CLUST_D                        \ cluster.  --        don't forget to add decimal point to your cluster number
197 \ ----------------------------------\
198     MOV.B &SecPerClus,W             \ 3 SecPerClus(5-1) = multiplicator
199     MOV @PSP,X
200     RRA W                           \ 1
201     U< IF                           \ case of SecPerClus>1
202         BEGIN
203             ADD X,X                 \ 5 (RLA) shift one left MULTIPLICANDlo16
204             ADDC TOS,TOS            \ 1 (RLC) shift one left MULTIPLICANDhi8
205             RRA W                   \ 1 shift one right multiplicator
206         U>= UNTIL
207     THEN                            \
208     ADD     &OrgClusters,X          \ add OrgClusters = sector of virtual cluster 0 (word size)
209     MOV     X,0(PSP)      
210     ADDC    #0,TOS                  \ don't forget carry
211     JMP     SECT_D                  \ jump to a defined word
212 ENDCODE
213 \ ----------------------------------\
214     \
215
216 \ ----------------------------------\
217 CODE DIR_D                          \ Display CurrentDir first sector
218 \ ----------------------------------\
219     SUB     #4,PSP                  \
220     MOV     TOS,2(PSP)              \           save TOS
221     MOV     &DIRclusterL,0(PSP)     \
222     MOV     &DIRclusterH,TOS        \
223     JMP     CLUST_D                 \
224 ENDCODE
225 \ ----------------------------------\
226     \
227
228 ECHO
229             ; added : UTILITY : ? SP@ .S WORDS MAX MIN U.R DUMP 
230             ; added : FAT_D to DUMP first sector of FAT1 and DIR_D for that of current DIRectory.
231             ; added : SECT_D to DUMP a sector and CLUST_D for first sector of a cluster
232             ;         include a decimal point to force 32 bits number, example : 2. CLUST_D
233     \
234 PWR_HERE    ; to protect this app against a RESET, type: RST_HERE
235