OSDN Git Service

V 3.2
[fast-forth/master.git] / MSP430-FORTH / SD_TOOLS.f
1 \ -*- coding: utf-8 -*-
2
3 ; ---------------------------------------------------------------
4 ; SD_TOOLS.f : BASIC TOOLS for SD Card : DIR FAT SECTOR CLUSTER
5 ; ---------------------------------------------------------------
6 \
7 \ to see kernel options, download FastForthSpecs.f
8 \ FastForth kernel options: MSP430ASSEMBLER, CONDCOMP, DOUBLE_INPUT, SD_CARD_LOADER
9 \
10 \ TARGET SELECTION
11 \ MSP_EXP430FR5739  MSP_EXP430FR5969    MSP_EXP430FR5994    MSP_EXP430FR6989
12 \ MSP_EXP430FR4133  CHIPSTICK_FR2433    MSP_EXP430FR2433    MSP_EXP430FR2355
13 \
14 \ REGISTERS USAGE
15 \ R4 to R7 must be saved before use and restored after
16 \ scratch registers Y to S are free for use
17 \ under interrupt, IP is free for use
18 \
19 \ PUSHM order : PSP,TOS, IP,  S,  T,  W,  X,  Y, rEXIT,rDOVAR,rDOCON, rDODOES, R3, SR,RSP, PC
20 \ PUSHM order : R15,R14,R13,R12,R11,R10, R9, R8,  R7  ,  R6  ,  R5  ,   R4   , R3, R2, R1, R0
21 \
22 \ example : PUSHM #6,IP pushes IP,S,T,W,X,Y registers to return stack
23 \
24 \ POPM  order :  PC,RSP, SR, R3, rDODOES,rDOCON,rDOVAR,rEXIT,  Y,  X,  W,  T,  S, IP,TOS,PSP
25 \ POPM  order :  R0, R1, R2, R3,   R4   ,  R5  ,  R6  ,  R7 , R8, R9,R10,R11,R12,R13,R14,R15
26 \
27 \ example : POPM #6,IP   pop Y,X,W,T,S,IP registers from return stack
28 \
29 \
30 \ FORTH conditionnals:  unary{ 0= 0< 0> }, binary{ = < > U< }
31 \
32 \ ASSEMBLER conditionnal usage with IF UNTIL WHILE  S<  S>=  U<   U>=  0=  0<>  0>=
33 \ ASSEMBLER conditionnal usage with ?JMP ?GOTO      S<  S>=  U<   U>=  0=  0<>  0<
34
35 [DEFINED] {SD_TOOLS} [IF]  {SD_TOOLS} [THEN]
36
37 [UNDEFINED] {SD_TOOLS} [IF]
38
39 PWR_STATE
40
41 [UNDEFINED] MARKER [IF]
42 \  https://forth-standard.org/standard/core/MARKER
43 \  MARKER
44 \ ( "<spaces>name" -- )
45 \ Skip leading space delimiters. Parse name delimited by a space. Create a definition for name
46 \ with the execution semantics defined below.
47
48 \ name Execution: ( -- )
49 \ Restore all dictionary allocation and search order pointers to the state they had just prior to the
50 \ definition of name. Remove the definition of name and all subsequent definitions. Restoration
51 \ of any structures still existing that could refer to deleted definitions or deallocated data space is
52 \ not necessarily provided. No other contextual information such as numeric base is affected
53 \
54 : MARKER
55 CREATE
56 HI2LO
57 MOV &LASTVOC,0(W)   \ [BODY] = LASTVOC
58 SUB #2,Y            \ 1 Y = LFA
59 MOV Y,2(W)          \ 3 [BODY+2] = LFA = DP to be restored
60 ADD #4,&DP          \ 3 add 2 cells
61 LO2HI
62 DOES>
63 HI2LO
64 MOV @RSP+,IP        \ -- PFA
65 MOV @TOS+,&INIVOC   \       set VOC_LINK value for RST_STATE
66 MOV @TOS,&INIDP     \       set DP value for RST_STATE
67 MOV @PSP+,TOS       \ --
68 MOV #RST_STATE,PC   \       execute RST_STATE, PWR_STATE then STATE_DOES
69 ENDCODE
70 [THEN]
71
72 MARKER {SD_TOOLS}
73
74 [UNDEFINED] + [IF]
75 \ https://forth-standard.org/standard/core/Plus
76 \ +       n1/u1 n2/u2 -- n3/u3     add n1+n2
77 CODE +
78 ADD @PSP+,TOS
79 MOV @IP+,PC
80 ENDCODE
81 [THEN]
82
83 [UNDEFINED] MAX [IF]    \ MAX and MIN are defined in {UTILITY}
84
85 CODE MAX    \    n1 n2 -- n3       signed maximum
86     CMP @PSP,TOS    \ n2-n1
87     S<  ?GOTO FW1   \ n2<n1
88 BW1 ADD #2,PSP
89     MOV @IP+,PC
90 ENDCODE
91
92 CODE MIN    \    n1 n2 -- n3       signed minimum
93     CMP @PSP,TOS     \ n2-n1
94     S<  ?GOTO BW1    \ n2<n1
95 FW1 MOV @PSP+,TOS
96     MOV @IP+,PC
97 ENDCODE
98
99 [THEN]
100
101 [UNDEFINED] C@ [IF]
102 \ https://forth-standard.org/standard/core/CFetch
103 \ C@     c-addr -- char   fetch char from memory
104 CODE C@
105 MOV.B @TOS,TOS
106 MOV @IP+,PC
107 ENDCODE
108 [THEN]
109
110 [UNDEFINED] ! [IF]
111 \ https://forth-standard.org/standard/core/Store
112 \ !        x a-addr --   store cell in memory
113 CODE !
114 MOV @PSP+,0(TOS)    \ 4
115 MOV @PSP+,TOS       \ 2
116 MOV @IP+,PC         \ 4
117 ENDCODE
118 [THEN]
119
120 [UNDEFINED] SPACE [IF]
121 \ https://forth-standard.org/standard/core/SPACE
122 \ SPACE   --               output a space
123 : SPACE
124 $20 EMIT ;
125 [THEN]
126
127 [UNDEFINED] SPACES [IF]
128 \ https://forth-standard.org/standard/core/SPACES
129 \ SPACES   n --            output n spaces
130 CODE SPACES
131 CMP #0,TOS
132 0<> IF
133     PUSH IP
134     BEGIN
135         LO2HI
136         $20 EMIT
137         HI2LO
138         SUB #2,IP 
139         SUB #1,TOS
140     0= UNTIL
141     MOV @RSP+,IP
142 THEN
143 MOV @PSP+,TOS           \ --         drop n
144 NEXT              
145 ENDCODE
146 [THEN]
147
148 [UNDEFINED] SWAP [IF]
149 \ https://forth-standard.org/standard/core/SWAP
150 \ SWAP     x1 x2 -- x2 x1    swap top two items
151 CODE SWAP
152 MOV @PSP,W      \ 2
153 MOV TOS,0(PSP)  \ 3
154 MOV W,TOS       \ 1
155 MOV @IP+,PC     \ 4
156 ENDCODE
157 [THEN]
158
159 [UNDEFINED] OVER [IF]
160 \ https://forth-standard.org/standard/core/OVER
161 \ OVER    x1 x2 -- x1 x2 x1
162 CODE OVER
163 MOV TOS,-2(PSP)     \ 3 -- x1 (x2) x2
164 MOV @PSP,TOS        \ 2 -- x1 (x2) x1
165 SUB #2,PSP          \ 1 -- x1 x2 x1
166 MOV @IP+,PC
167 ENDCODE
168 [THEN]
169
170 [UNDEFINED] >R [IF]
171 \ https://forth-standard.org/standard/core/toR
172 \ >R    x --   R: -- x   push to return stack
173 CODE >R
174 PUSH TOS
175 MOV @PSP+,TOS
176 MOV @IP+,PC
177 ENDCODE
178 [THEN]
179
180 [UNDEFINED] R> [IF]
181 \ https://forth-standard.org/standard/core/Rfrom
182 \ R>    -- x    R: x --   pop from return stack ; CALL #RFROM performs DOVAR
183 CODE R>
184 MOV rDOVAR,PC
185 ENDCODE
186 [THEN]
187
188 [UNDEFINED] - [IF]
189 \ https://forth-standard.org/standard/core/Minus
190 \ -      n1/u1 n2/u2 -- n3/u3     n3 = n1-n2
191 CODE -
192 SUB @PSP+,TOS   \ 2  -- n2-n1 ( = -n3)
193 XOR #-1,TOS     \ 1
194 ADD #1,TOS      \ 1  -- n3 = -(n2-n1) = n1-n2
195 MOV @IP+,PC
196 ENDCODE
197 [THEN]
198
199 [UNDEFINED] U.R [IF]        \ defined in {UTILITY}
200 : U.R                       \ u n --           display u unsigned in n width (n >= 2)
201   >R  <# 0 # #S #>  
202   R> OVER - 0 MAX SPACES TYPE
203 ;
204 [THEN]
205
206 [UNDEFINED] DO [IF]
207 \ https://forth-standard.org/standard/core/DO
208 \ DO       -- DOadr   L: -- 0
209 CODE DO                 \ immediate
210 SUB #2,PSP              \
211 MOV TOS,0(PSP)          \
212 ADD #2,&DP              \   make room to compile xdo
213 MOV &DP,TOS             \ -- HERE+2
214 MOV #XDO,-2(TOS)        \   compile xdo
215 ADD #2,&LEAVEPTR        \ -- HERE+2     LEAVEPTR+2
216 MOV &LEAVEPTR,W         \
217 MOV #0,0(W)             \ -- HERE+2     L-- 0
218 MOV @IP+,PC
219 ENDCODE IMMEDIATE
220 [THEN]
221
222 [UNDEFINED] LOOP [IF]
223 \ https://forth-standard.org/standard/core/LOOP
224 \ LOOP    DOadr --         L-- an an-1 .. a1 0
225 CODE LOOP               \ immediate
226     MOV #XLOOP,X
227 BW1 ADD #4,&DP          \ make room to compile two words
228     MOV &DP,W
229     MOV X,-4(W)         \ xloop --> HERE
230     MOV TOS,-2(W)       \ DOadr --> HERE+2
231 BEGIN                   \ resolve all "leave" adr
232     MOV &LEAVEPTR,TOS   \ -- Adr of top LeaveStack cell
233     SUB #2,&LEAVEPTR    \ --
234     MOV @TOS,TOS        \ -- first LeaveStack value
235     CMP #0,TOS          \ -- = value left by DO ?
236 0<> WHILE
237     MOV W,0(TOS)        \ move adr after loop as UNLOOP adr
238 REPEAT
239     MOV @PSP+,TOS
240     MOV @IP+,PC
241 ENDCODE IMMEDIATE
242 [THEN]
243
244 [UNDEFINED] +LOOP [IF]
245 \ https://forth-standard.org/standard/core/PlusLOOP
246 \ +LOOP   adrs --   L-- an an-1 .. a1 0
247 CODE +LOOP              \ immediate
248 MOV #XPLOOP,X
249 GOTO BW1
250 ENDCODE IMMEDIATE
251 [THEN]
252
253 [UNDEFINED] I [IF]
254 \ https://forth-standard.org/standard/core/I
255 \ I        -- n   R: sys1 sys2 -- sys1 sys2
256 \                  get the innermost loop index
257 CODE I
258 SUB #2,PSP              \ 1 make room in TOS
259 MOV TOS,0(PSP)          \ 3
260 MOV @RSP,TOS            \ 2 index = loopctr - fudge
261 SUB 2(RSP),TOS          \ 3
262 MOV @IP+,PC             \ 4 13~
263 ENDCODE
264 [THEN]
265
266 [UNDEFINED] DUMP [IF]       \ defined in {UTILITY}
267 \ https://forth-standard.org/standard/tools/DUMP
268 CODE DUMP                   \ adr n  --   dump memory
269 PUSH IP
270 PUSH &BASEADR               \ save current base
271 MOV #$10,&BASEADR           \ HEX base
272 ADD @PSP,TOS                \ -- ORG END
273 LO2HI
274   SWAP                      \ -- END ORG
275   DO  CR                    \ generate line
276     I 4 U.R SPACE           \ generate address
277       I 8 + I
278       DO I C@ 3 U.R LOOP
279       SPACE
280       I $10 + I 8 +
281       DO I C@ 3 U.R LOOP  
282       SPACE SPACE
283       I $10 + I             \ display 16 chars
284       DO I C@ $7E MIN $20 MAX EMIT LOOP
285   $10 +LOOP
286   R> BASEADR !              \ restore current base
287 ;
288 [THEN]
289
290 \ display content of a sector
291 \ ----------------------------------\
292 CODE SECTOR                         \ sector. --     don't forget to add decimal point to your sector number
293 \ ----------------------------------\
294 BW1 MOV     TOS,X                   \ X = SectorH
295     MOV     @PSP,W                  \ W = sectorL
296     CALL    &ReadSectorWX           \ W = SectorLO  X = SectorHI
297 COLON                               \
298     <# #S #> TYPE SPACE             \ ud --            display the double number
299     SD_BUF $200 DUMP CR ;           \ then dump the sector
300 \ ----------------------------------\
301
302 \ display first sector of a Cluster
303 \ ----------------------------------\
304 CODE CLUSTER                        \ cluster.  --        don't forget to add decimal point to your cluster number
305 \ ----------------------------------\
306 BW2 MOV.B &SecPerClus,W             \ SecPerClus(54321) = multiplicator
307     MOV @PSP,X                      \ X = ClusterL
308     GOTO FW1                        \
309     BEGIN
310         ADD X,X                     \ (RLA) shift one left MULTIPLICANDlo16
311         ADDC TOS,TOS                \ (RLC) shift one left MULTIPLICANDhi8
312 FW1     RRA W                       \ shift one right multiplicator
313     U>= UNTIL                       \ carry set
314     ADD     &OrgClusters,X          \ add OrgClusters = sector of virtual cluster 0 (word size)
315     MOV     X,0(PSP)      
316     ADDC    #0,TOS                  \ don't forget carry
317     GOTO    BW1                     \ jump to SECTOR
318 ENDCODE
319 \ ----------------------------------\
320
321 \ ----------------------------------\
322 CODE FAT                            \ Display CurFATsector
323 \ ----------------------------------\
324     SUB     #4,PSP                  \
325     MOV     TOS,2(PSP)              \
326     MOV     &OrgFAT1,0(PSP)         \
327     MOV     #0,TOS                  \ FATsectorHI = 0
328     GOTO    BW1                     \ jump to SECTOR
329 ENDCODE
330 \ ----------------------------------\
331
332 \ ----------------------------------\
333 CODE DIR                            \ Display CurrentDir first sector
334 \ ----------------------------------\
335     SUB     #4,PSP                  \
336     MOV     TOS,2(PSP)              \           save TOS
337     MOV     &DIRclusterL,0(PSP)     \
338     MOV     &DIRclusterH,TOS        \
339     CMP     #0,TOS
340     0<>     ?GOTO BW2               \ jump to CLUSTER
341     CMP     #1,0(PSP)               \ cluster 1 ?
342     0<>     ?GOTO BW2               \ jump to CLUSTER
343     MOV     &OrgRootDir,0(PSP)      \ if yes, special case of FAT16 OrgRootDir
344     GOTO    BW1                     \ jump to SECTOR
345 ENDCODE
346 \ ----------------------------------\
347
348
349 RST_HERE
350
351 [THEN]
352 ECHO
353