OSDN Git Service

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