1 \ -*- coding: utf-8 -*-
3 ; ---------------------------------------------------------------
4 ; SD_TOOLS.f : BASIC TOOLS for SD Card : DIR FAT SECTOR CLUSTER
5 ; ---------------------------------------------------------------
7 \ to see kernel options, download FastForthSpecs.f
8 \ FastForth kernel options: MSP430ASSEMBLER, CONDCOMP, DOUBLE_INPUT, SD_CARD_LOADER
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
15 \ from scite editor : copy your target selection in (shift+F8) parameter 1:
19 \ drag and drop this file onto SendSourceFileToTarget.bat
20 \ then select your TARGET when asked.
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
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
31 \ example : PUSHM #6,IP pushes IP,S,T,W,X,Y registers to return stack
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
36 \ example : POPM #6,IP pop Y,X,W,T,S,IP registers from return stack
39 \ FORTH conditionnals: unary{ 0= 0< 0> }, binary{ = < > U< }
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<
46 [DEFINED] {SD_TOOLS} [IF] {SD_TOOLS} [THEN]
48 [UNDEFINED] {SD_TOOLS} [IF]
53 \ https://forth-standard.org/standard/core/Plus
54 \ + n1/u1 n2/u2 -- n3/u3 add n1+n2
61 [UNDEFINED] MAX [IF] \ define MAX and MIN
63 CODE MAX \ n1 n2 -- n3 signed maximum
70 CODE MIN \ n1 n2 -- n3 signed minimum
80 \ https://forth-standard.org/standard/core/CFetch
81 \ C@ c-addr -- char fetch char from memory
89 \ https://forth-standard.org/standard/core/Store
90 \ ! x a-addr -- store cell in memory
98 [UNDEFINED] SPACE [IF]
99 \ https://forth-standard.org/standard/core/SPACE
100 \ SPACE -- output a space
105 [UNDEFINED] SPACES [IF]
106 \ https://forth-standard.org/standard/core/SPACES
107 \ SPACES n -- output n spaces
121 MOV @PSP+,TOS \ -- drop n
126 [UNDEFINED] SWAP [IF]
127 \ https://forth-standard.org/standard/core/SWAP
128 \ SWAP x1 x2 -- x2 x1 swap top two items
137 [UNDEFINED] OVER [IF]
138 \ https://forth-standard.org/standard/core/OVER
139 \ OVER x1 x2 -- x1 x2 x1
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
149 \ https://forth-standard.org/standard/core/toR
150 \ >R x -- R: -- x push to return stack
159 \ https://forth-standard.org/standard/core/Rfrom
160 \ R> -- x R: x -- pop from return stack ; CALL #RFROM performs DOVAR
170 \ https://forth-standard.org/standard/core/Minus
171 \ - n1/u1 n2/u2 -- n3/u3 n3 = n1-n2
173 SUB @PSP+,TOS \ 2 -- n2-n1 ( = -n3)
175 ADD #1,TOS \ 1 -- n3 = -(n2-n1) = n1-n2
180 [UNDEFINED] U.R [IF] \ defined in {UTILITY}
181 : U.R \ u n -- display u unsigned in n width (n >= 2)
183 R> OVER - 0 MAX SPACES TYPE
187 [UNDEFINED] DO [IF] \ define DO LOOP +LOOP
188 \ https://forth-standard.org/standard/core/DO
189 \ DO -- DOadr L: -- 0
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
198 MOV #0,0(W) \ -- HERE+2 L-- 0
202 \ https://forth-standard.org/standard/core/LOOP
203 \ LOOP DOadr -- L-- an an-1 .. a1 0
206 BW1 ADD #4,&DP \ make room to compile two words
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 ?
216 MOV W,0(TOS) \ move adr after loop as UNLOOP adr
222 \ https://forth-standard.org/standard/core/PlusLOOP
223 \ +LOOP adrs -- L-- an an-1 .. a1 0
226 GOTO BW1 \ goto BW1 LOOP
231 \ https://forth-standard.org/standard/core/I
232 \ I -- n R: sys1 sys2 -- sys1 sys2
233 \ get the innermost loop index
235 SUB #2,PSP \ 1 make room in TOS
237 MOV @RSP,TOS \ 2 index = loopctr - fudge
243 [UNDEFINED] DUMP [IF] \ defined in {UTILITY}
244 \ https://forth-standard.org/standard/tools/DUMP
245 CODE DUMP \ adr n -- dump memory
247 PUSH &BASEADR \ save current base
248 MOV #$10,&BASEADR \ HEX base
249 ADD @PSP,TOS \ -- ORG END
252 DO CR \ generate line
253 I 4 U.R SPACE \ generate address
260 I $10 + I \ display 16 chars
261 DO I C@ $7E MIN $20 MAX EMIT LOOP
263 R> BASEADR ! \ restore current base
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
275 <# #S #> TYPE SPACE \ ud -- display the double number
276 SD_BUF $200 DUMP CR ; \ then dump the sector
277 \ ----------------------------------\
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 ?
285 MOV #COLD,PC \ no: force COLD
287 MOV.B &SecPerClus,W \ SecPerClus(54321) = multiplicator
288 MOV @PSP,X \ X = ClusterL
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)
297 ADDC #0,TOS \ don't forget carry
298 GOTO BW1 \ jump to SECTOR
300 \ ----------------------------------\
302 \ ----------------------------------\
303 CODE FAT \ Display CurFATsector
304 \ ----------------------------------\
307 MOV &OrgFAT1,0(PSP) \
308 MOV #0,TOS \ FATsectorHI = 0
309 GOTO BW1 \ jump to SECTOR
311 \ ----------------------------------\
313 \ ----------------------------------\
314 CODE DIR \ Display CurrentDir first sector
315 \ ----------------------------------\
317 MOV TOS,2(PSP) \ save TOS
318 MOV &DIRclusterL,0(PSP) \
319 MOV &DIRclusterH,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
327 \ ----------------------------------\