OSDN Git Service

V4.0
[fast-forth/master.git] / MSP430-FORTH / SD_TOOLS.f
index c234fdb..fe2f5e2 100644 (file)
-; ------------------------------------------------------------------------
-; BASIC TOOLS for SD Card : {DIR FAT SECTOR CLUSER} DUMP ; include UTILITY
-; ------------------------------------------------------------------------
+\ -*- coding: utf-8 -*-
 
-\ Fast Forth For Texas Instrument MSP430FRxxxx FRAM devices
-\ Copyright (C) <2017>  <J.M. THOORENS>
+\ to see kernel options, download FastForthSpecs.f
+\ FastForth kernel options: MSP430ASSEMBLER, CONDCOMP, DOUBLE_INPUT, SD_CARD_LOADER
 \
-\ This program is free software: you can redistribute it and/or modify
-\ it under the terms of the GNU General Public License as published by
-\ the Free Software Foundation, either version 3 of the License, or
-\ (at your option) any later version.
+\ TARGET SELECTION ( = the name of \INC\target.pat file without the extension)
+\ MSP_EXP430FR5739  MSP_EXP430FR5969    MSP_EXP430FR5994    MSP_EXP430FR6989
+\ MSP_EXP430FR4133  CHIPSTICK_FR2433    MSP_EXP430FR2433    MSP_EXP430FR2355
+\ LP_MSP430FR2476
+\
+\ from scite editor : copy your target selection in (shift+F8) parameter 1:
+\
+\ OR
+\
+\ drag and drop this file onto SendSourceFileToTarget.bat
+\ then select your TARGET when asked.
 \
-\ This program is distributed in the hope that it will be useful,
-\ but WITHOUT ANY WARRANTY; without even the implied warranty of
-\ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-\ GNU General Public License for more details.
 \
-\ You should have received a copy of the GNU General Public License
-\ along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
 \ REGISTERS USAGE
 \ R4 to R7 must be saved before use and restored after
 \ scratch registers Y to S are free for use
 \ under interrupt, IP is free for use
-
-\ PUSHM order : PSP,TOS, IP,  S,  T,  W,  X,  Y, R7, R6, R5, R4
-\ example : PUSHM IP,Y
 \
-\ POPM  order :  R4, R5, R6, R7,  Y,  X,  W,  T,  S, IP,TOS,PSP
-\ example : POPM Y,IP
-
-\ ASSEMBLER conditionnal usage after IF UNTIL WHILE : S< S>= U< U>= 0= 0<> 0>=
-\ ASSEMBLER conditionnal usage before GOTO ?GOTO     : S< S>= U< U>= 0= 0<> <0 
-
-\ FORTH conditionnal usage after IF UNTIL WHILE : 0= 0< = < > U<
+\ PUSHM order : PSP,TOS, IP,  S,  T,  W,  X,  Y, rEXIT,rDOVAR,rDOCON, rDODOES, R3, SR,RSP, PC
+\ PUSHM order : R15,R14,R13,R12,R11,R10, R9, R8,  R7  ,  R6  ,  R5  ,   R4   , R3, R2, R1, R0
+\
+\ example : PUSHM #6,IP pushes IP,S,T,W,X,Y registers to return stack
+\
+\ POPM  order :  PC,RSP, SR, R3, rDODOES,rDOCON,rDOVAR,rEXIT,  Y,  X,  W,  T,  S, IP,TOS,PSP
+\ POPM  order :  R0, R1, R2, R3,   R4   ,  R5  ,  R6  ,  R7 , R8, R9,R10,R11,R12,R13,R14,R15
+\
+\ example : POPM #6,IP   pop Y,X,W,T,S,IP registers from return stack
+\
+\
+\ FORTH conditionnals:  unary{ 0= 0< 0> }, binary{ = < > U< }
+\
+\ ASSEMBLER conditionnal usage with IF UNTIL WHILE  S<  S>=  U<   U>=  0=  0<>  0>=
+\ ASSEMBLER conditionnal usage with ?JMP ?GOTO      S<  S>=  U<   U>=  0=  0<>  0<
 
+; ---------------------------------------------------------------
+; SD_TOOLS.f
+; BASIC TOOLS for SD Card : DIR FAT SECTOR. CLUSTER.
+; ---------------------------------------------------------------
 
+\ first, we do some tests allowing the download
+    CODE ABORT_SD_TOOLS
+    SUB #4,PSP
+    MOV TOS,2(PSP)
+    [UNDEFINED] LOAD"   \ "
+    [IF]
+        MOV #-1,0(PSP)
+    [ELSE]
+        MOV #0,0(PSP)
+    [THEN]
+    MOV &VERSION,TOS
+    SUB #400,TOS        \ FastForth V4.0
+    COLON
+    'CR' EMIT           \ return to column 1 without 'LF'
+    ABORT" FastForth V4.0 please!"
+    ABORT" Build FastForth with SD_CARD_LOADER addon!"
+    RST_RET             \ remove ABORT_UARTI2CS definition before resuming
+    ;
 
+    ABORT_SD_TOOLS
 
-\ ECHO      ; if an error occurs, uncomment this line before new download to find it.
+    [DEFINED] {SD_TOOLS} 
+    [IF] {SD_TOOLS}
+    [THEN]
+    [UNDEFINED] {SD_TOOLS}
+    [IF]
+    MARKER {SD_TOOLS}
 
-    \
+; ------------------------------------------------------------------
+; first we download the set of definitions we need (from CORE_ANS.f)
+; ------------------------------------------------------------------
 
-CODE ?          \ adr --            display the content of adr
-    MOV @TOS,TOS
-    MOV #U.,PC  \ goto U.
-ENDCODE
-    \
+    [UNDEFINED] HERE [IF]
+    CODE HERE
+    MOV #BEGIN,PC
+    ENDCODE
+    [THEN]
 
-CODE SP@        \ -- SP
-    SUB #2,PSP
-    MOV TOS,0(PSP)
-    MOV PSP,TOS
+\ https://forth-standard.org/standard/core/Plus
+\ +       n1/u1 n2/u2 -- n3/u3     add n1+n2
+    [UNDEFINED] + [IF]
+    CODE +
+    ADD @PSP+,TOS
     MOV @IP+,PC
-ENDCODE
-
-: .S                \ --            print <number> of cells and stack contents if not empty
-$3C EMIT           \ --            char "<"
-DEPTH .
-8 EMIT              \               backspace
-$3E EMIT SPACE     \               char ">"
-SP@  PSTACK OVER OVER U<    \
-IF  2 -
-    DO I @ U.
-    -2 +LOOP
-ELSE
-    DROP DROP
-THEN
-;
-    \
-
-: WORDS                             \ --            list all words in all dicts in CONTEXT.
-
-\ vvvvvvvv   may be skipped    vvvvvvvv
-BASE @                              \ -- BASE
-#10 BASE !
-CR ."    "
-INI_THREAD @ DUP
-1 = IF DROP ." monothread"
-    ELSE . ." threads"
-    THEN ."  vocabularies"
-BASE !                              \ --
-\ ^^^^^^^^   may be skipped    ^^^^^^^^
-
-CONTEXT                             \ -- CONTEXT
-BEGIN                               \                                       search dictionnary
-    DUP 
-    2 + SWAP                        \ -- CONTEXT+2 CONTEXT
-    @ ?DUP                          \ -- CONTEXT+2 (VOC_BODY VOC_BODY or 0)
-WHILE                               \ -- CONTEXT+2 VOC_BODY                  dictionnary found
-CR ."    "                          \
-\   MOVE all threads of VOC_BODY in PAD
-    DUP PAD INI_THREAD @ DUP +      \ -- CONTEXT+2 VOC_BODY  VOC_BODY PAD THREAD*2
-    MOVE                            \         char MOVE
-
-    BEGIN                           \ -- CONTEXT+2 VOC_BODY
-        0 DUP                       \ -- CONTEXT+2 VOC_BODY ptr MAX
-\   select the MAX of NFA in threads
-        INI_THREAD @ DUP + 0 DO     \         ptr = threads*2
-        DUP I PAD + @               \ -- CONTEXT+2 VOC_BODY ptr MAX MAX NFAx
-        U< IF 
-            DROP DROP I DUP PAD + @ \ -- CONTEXT+2 VOC_BODY ptr MAX          if MAX U< NFAx replace adr and MAX
-        THEN                        \ 
-        2 +LOOP                     \ -- CONTEXT+2 VOC_BODY ptr MAX
-        ?DUP                        \ -- CONTEXT+2 VOC_BODY ptr MAX          max NFA = 0 ? end of vocabulary ?
-    WHILE                           \ -- CONTEXT+2 VOC_BODY ptr MAX
-\   replace it by its LFA
-        DUP                         \ -- CONTEXT+2 VOC_BODY ptr MAX MAX
-        2 - @                       \ -- CONTEXT+2 VOC_BODY ptr MAX [LFA]
-        ROT                         \ -- CONTEXT+2 VOC_BODY MAX [LFA] ptr
-        PAD +                       \ -- CONTEXT+2 VOC_BODY MAX [LFA] thread
-        !                           \ -- CONTEXT+2 VOC_BODY MAX
-\   type it in 16 chars format
-                DUP                 \ -- CONTEXT+2 VOC_BODY MAX MAX
-            COUNT $7F AND TYPE      \ -- CONTEXT+2 VOC_BODY MAX
-                C@ $0F AND          \ -- 
-                $10 SWAP - SPACES   \ -- CONTEXT+2 VOC_BODY 
-\   search next MAX of NFA 
-    REPEAT
-                                    \ -- CONTEXT+2 VOC_BODY 0
-    DROP DROP                       \ -- CONTEXT+2
-    CR         
-\   repeat for each CONTEXT vocabulary
-
-REPEAT                              \ -- 0
-DROP                                \ --
-;
-    \
+    ENDCODE
+    [THEN]
 
-CODE MAX    \    n1 n2 -- n3       signed maximum
+    [UNDEFINED] MAX
+    [IF]    \ define MAX and MIN
+    CODE MAX    \    n1 n2 -- n3       signed maximum
     CMP @PSP,TOS    \ n2-n1
     S<  ?GOTO FW1   \ n2<n1
 BW1 ADD #2,PSP
     MOV @IP+,PC
-ENDCODE
-    \
+    ENDCODE
 
-CODE MIN    \    n1 n2 -- n3       signed minimum
+    CODE MIN    \    n1 n2 -- n3       signed minimum
     CMP @PSP,TOS     \ n2-n1
     S<  ?GOTO BW1    \ n2<n1
 FW1 MOV @PSP+,TOS
     MOV @IP+,PC
-ENDCODE
-    \
-
-: U.R                       \ u n --           display u unsigned in n width (n >= 2)
-  >R  <# 0 # #S #>  
-  R> OVER - 0 MAX SPACES TYPE
-;
-    \
-
-: DUMP                      \ adr n  --   dump memory
-  BASE @ >R $10 BASE !
-  SWAP $FFF0 AND SWAP
-  OVER + SWAP
-  DO  CR                    \ generate line
-    I 7 U.R SPACE           \ generate address
-      I $10 + I             \ display 16 bytes
-      DO I C@ 3 U.R LOOP  
-      SPACE SPACE
-      I $10 + I             \ display 16 chars
-      DO I C@ $7E MIN BL MAX EMIT LOOP
-  $10 +LOOP
-  R> BASE !
-;
-    \
-
-\ ----------------------------------\
-\ read sector and dump it           \ sector. --            don't forget to add decimal point to your sector number (if < 65536)
-\ ----------------------------------\
-CODE SECT_D                         \
-    MOV     TOS,X                   \ X = SectorH
+    ENDCODE
+    [THEN]
+
+\ https://forth-standard.org/standard/core/CFetch
+\ C@     c-addr -- char   fetch char from memory
+    [UNDEFINED] C@
+    [IF]
+    CODE C@
+    MOV.B @TOS,TOS
+    MOV @IP+,PC
+    ENDCODE
+    [THEN]
+
+\ https://forth-standard.org/standard/core/SPACE
+\ SPACE   --               output a space
+    [UNDEFINED] SPACE
+    [IF]
+    : SPACE
+    $20 EMIT ;
+    [THEN]
+
+\ https://forth-standard.org/standard/core/SPACES
+\ SPACES   n --            output n spaces
+    [UNDEFINED] SPACES
+    [IF]
+    CODE SPACES
+    CMP #0,TOS
+    0<> IF
+        PUSH IP
+        BEGIN
+            LO2HI
+            $20 EMIT
+            HI2LO
+            SUB #2,IP
+            SUB #1,TOS
+        0= UNTIL
+        MOV @RSP+,IP
+    THEN
+    MOV @PSP+,TOS           \ --         drop n
+    NEXT
+    ENDCODE
+    [THEN]
+
+\ https://forth-standard.org/standard/core/SWAP
+\ SWAP     x1 x2 -- x2 x1    swap top two items
+    [UNDEFINED] SWAP
+    [IF]
+    CODE SWAP
+    MOV @PSP,W      \ 2
+    MOV TOS,0(PSP)  \ 3
+    MOV W,TOS       \ 1
+    MOV @IP+,PC     \ 4
+    ENDCODE
+    [THEN]
+
+\ https://forth-standard.org/standard/core/OVER
+\ OVER    x1 x2 -- x1 x2 x1
+    [UNDEFINED] OVER
+    [IF]
+    CODE OVER
+    MOV TOS,-2(PSP)     \ 3 -- x1 (x2) x2
+    MOV @PSP,TOS        \ 2 -- x1 (x2) x1
+    SUB #2,PSP          \ 1 -- x1 x2 x1
+    MOV @IP+,PC
+    ENDCODE
+    [THEN]
+
+\ https://forth-standard.org/standard/core/toR
+\ >R    x --   R: -- x   push to return stack
+    [UNDEFINED] >R
+    [IF]
+    CODE >R
+    PUSH TOS
+    MOV @PSP+,TOS
+    MOV @IP+,PC
+    ENDCODE
+    [THEN]
+
+\ https://forth-standard.org/standard/core/Rfrom
+\ R>    -- x    R: x --   pop from return stack ; CALL #RFROM performs DOVAR
+    [UNDEFINED] R>
+    [IF]
+    CODE R>
+    SUB #2,PSP      \ 1
+    MOV TOS,0(PSP)  \ 3
+    MOV @RSP+,TOS   \ 2
+    MOV @IP+,PC     \ 4
+    ENDCODE
+    [THEN]
+
+\ https://forth-standard.org/standard/core/Minus
+\ -      n1/u1 n2/u2 -- n3/u3     n3 = n1-n2
+    [UNDEFINED] -
+    [IF]
+    CODE -
+    SUB @PSP+,TOS   \ 2  -- n2-n1 ( = -n3)
+    XOR #-1,TOS     \ 1
+    ADD #1,TOS      \ 1  -- n3 = -(n2-n1) = n1-n2
+    MOV @IP+,PC
+    ENDCODE
+    [THEN]
+
+\ https://forth-standard.org/standard/core/DO
+\ DO       -- DOadr   L: -- 0
+    [UNDEFINED] DO
+    [IF]                \ define DO LOOP +LOOP
+    HDNCODE XDO         \ DO run time
+    MOV #$8000,X        \ 2 compute 8000h-limit = "fudge factor"
+    SUB @PSP+,X         \ 2
+    MOV TOS,Y           \ 1 loop ctr = index+fudge
+    ADD X,Y             \ 1 Y = INDEX
+    PUSHM #2,X          \ 4 PUSHM X,Y, i.e. PUSHM LIMIT, INDEX
+    MOV @PSP+,TOS       \ 2
+    MOV @IP+,PC         \ 4
+    ENDCODE
+
+    CODE DO
+    SUB #2,PSP              \
+    MOV TOS,0(PSP)          \
+    ADD #2,&DP              \   make room to compile xdo
+    MOV &DP,TOS             \ -- HERE+2
+    MOV #XDO,-2(TOS)        \   compile xdo
+    ADD #2,&LEAVEPTR        \ -- HERE+2     LEAVEPTR+2
+    MOV &LEAVEPTR,W         \
+    MOV #0,0(W)             \ -- HERE+2     L-- 0
+    MOV @IP+,PC
+    ENDCODE IMMEDIATE
+
+\ https://forth-standard.org/standard/core/LOOP
+\ LOOP    DOadr --         L-- an an-1 .. a1 0
+    HDNCODE XLOOP       \   LOOP run time
+    ADD #1,0(RSP)       \ 4 increment INDEX
+BW1 BIT #$100,SR        \ 2 is overflow bit set?
+    0= IF               \   branch if no overflow
+        MOV @IP,IP
+        MOV @IP+,PC
+    THEN
+    ADD #4,RSP          \ 1 empties RSP
+    ADD #2,IP           \ 1 overflow = loop done, skip branch ofs
+    MOV @IP+,PC         \ 4 14~ taken or not taken xloop/loop
+    ENDCODE             \
+
+    CODE LOOP
+    MOV #XLOOP,X
+BW2 ADD #4,&DP              \ make room to compile two words
+    MOV &DP,W
+    MOV X,-4(W)             \ xloop --> HERE
+    MOV TOS,-2(W)           \ DOadr --> HERE+2
+    BEGIN                   \ resolve all "leave" adr
+        MOV &LEAVEPTR,TOS   \ -- Adr of top LeaveStack cell
+        SUB #2,&LEAVEPTR    \ --
+        MOV @TOS,TOS        \ -- first LeaveStack value
+        CMP #0,TOS          \ -- = value left by DO ?
+    0<> WHILE
+        MOV W,0(TOS)        \ move adr after loop as UNLOOP adr
+    REPEAT
+    MOV @PSP+,TOS
+    MOV @IP+,PC
+    ENDCODE IMMEDIATE
+
+\ https://forth-standard.org/standard/core/PlusLOOP
+\ +LOOP   adrs --   L-- an an-1 .. a1 0
+    HDNCODE XPLOO   \   +LOOP run time
+    ADD TOS,0(RSP)  \ 4 increment INDEX by TOS value
+    MOV @PSP+,TOS   \ 2 get new TOS, doesn't change flags
+    GOTO BW1        \ 2
+    ENDCODE         \
+
+    CODE +LOOP
+    MOV #XPLOO,X
+    GOTO BW2        \ goto BW1 LOOP
+    ENDCODE IMMEDIATE
+    [THEN]
+
+\ https://forth-standard.org/standard/core/I
+\ I        -- n   R: sys1 sys2 -- sys1 sys2
+\                  get the innermost loop index
+    [UNDEFINED] I
+    [IF]
+    CODE I
+    SUB #2,PSP              \ 1 make room in TOS
+    MOV TOS,0(PSP)          \ 3
+    MOV @RSP,TOS            \ 2 index = loopctr - fudge
+    SUB 2(RSP),TOS          \ 3
+    MOV @IP+,PC             \ 4 13~
+    ENDCODE
+    [THEN]
+
+\ https://forth-standard.org/standard/core/CR
+\ CR      --               send CR+LF to the output device
+    [UNDEFINED] CR
+    [IF]
+\ create a primary defered word, i.e. with its default runtime beginning at the >BODY of the definition
+    CODE CR     \ part I : DEFERed definition of CR
+    MOV #NEXT_ADR,PC                \ [PFA] = NEXT_ADR
+    ENDCODE
+
+    :NONAME
+    'CR' EMIT 'LF' EMIT
+    ; IS CR
+    [THEN]
+
+; ------------------------------------------------------------------
+; then we download the set of definitions we need (from UTILITY.f)
+; ------------------------------------------------------------------
+
+    [UNDEFINED] U.R
+    [IF]        \ defined in {UTILITY}
+    : U.R                       \ u n --           display u unsigned in n width (n >= 2)
+    >R  <# 0 # #S #>
+    R> OVER - 0 MAX SPACES TYPE
+    ;
+    [THEN]
+
+\ https://forth-standard.org/standard/tools/DUMP
+    [UNDEFINED] DUMP
+    [IF]       \ defined in {UTILITY}
+    CODE DUMP                   \ adr n  --   dump memory
+    PUSH IP
+    PUSH &BASEADR               \ save current base
+    MOV #$10,&BASEADR           \ HEX base
+    ADD @PSP,TOS                \ -- ORG END
+    LO2HI
+    SWAP                        \ -- END ORG
+    CR
+    4 SPACES $10 0
+    DO I 3 U.R  LOOP            \ -- END ORG
+    DO  CR                      \ generate line
+        I 4 U.R                 \ generate address
+        I $10 + I
+        DO I C@ 3 U.R LOOP
+        SPACE SPACE
+        I $10 + I             \ display 16 chars
+        DO I C@ $7E MIN $20 MAX EMIT LOOP
+    $10 +LOOP
+    R> BASEADR !              \ restore current base
+    ;
+    [THEN]
+
+; --------------------------
+; end of definitions we need
+; --------------------------
+
+\ display content of a sector
+\ to Display MBR_FirstSector, type : 0. SECTOR.
+\   --------------------------------\
+    CODE SECTOR.                    \ sector. --     don't forget to add decimal point to your sector number
+\   --------------------------------\
+BW1 MOV     TOS,X                   \ X = SectorH
     MOV     @PSP,W                  \ W = sectorL
-    CALL    &ReadSectorWX           \ W = SectorLO  X = SectorHI
-COLON                               \
-    UD.                             \ display the sector number
-    BUFFER $200 DUMP CR ;           \ then dump the sector
-\ ----------------------------------\
-    \
-
-\ TIP : How to identify FAT16 or FAT32 SD_Card ?
-\ 1 CLUSTER <==> FAT16 RootDIR
-\ 2 CLUSTER <==> FAT32 RootDIR
-\ ----------------------------------\
-\ read first sector of Cluster and dump it
-\ ----------------------------------\
-CODE CLUST_D                        \ cluster.  --         don't forget to add decimal point to your cluster number (if < 65536)
-\ ----------------------------------\
-    MOV     TOS,&ClusterH           \
-    MOV     @PSP,&ClusterL          \
-BW1 MOV     &OrgClusters,&RES0      \ OrgClusters = sector of virtual cluster 0, word size
-    MOV     #0,&RES1
-    MOV     &ClusterL,&MAC32L
-    MOV     &ClusterH,&MAC32H
-    MOV     &SecPerClus,&OP2
-    MOV     &RES0,0(PSP)            \ cluster sectorL
-    MOV     &RES1,TOS               \ cluster sectorH
-    JMP     SECT_D                  \ jump to a defined word
-ENDCODE
-\ ----------------------------------\
-    \
-
-\ dump FAT1 sector of last entry
-\ ----------------------------------\
-CODE FAT_D                          \ Display CurFATsector
-\ ----------------------------------\
+    CALL    #RD_SECT                \ W = SectorLO  X = SectorHI
+    COLON                           \
+    SPACE <# #S #> TYPE             \ ud --            display the double number
+    SD_BUF $200 DUMP CR ;           \ then dump the sector
+\   --------------------------------\
+
+\ display first sector of a Cluster
+\   --------------------------------\
+    CODE CLUSTER.                   \ cluster.  --        don't forget to add decimal point to your cluster number
+\   --------------------------------\
+BW2 BIT.B   #CD_SD,&SD_CDIN         \ test Card Detect: memory card present ?
+    0<> IF                          \ no: force COLD
+        MOV #COLD,PC                \ no
+    THEN
+    MOV.B &SecPerClus,W             \ SecPerClus(54321) = multiplicator
+    MOV @PSP,X                      \ X = ClusterL
+    BEGIN
+        RRA W                       \ shift one right multiplicator
+    U< WHILE                        \ carry clear
+        ADD X,X                     \ (RLA) shift one left MULTIPLICANDlo16
+        ADDC TOS,TOS                \ (RLC) shift one left MULTIPLICANDhi8
+    REPEAT
+    ADD     &OrgClusters,X          \ add OrgClusters = sector of virtual cluster 0 (word size)
+    MOV     X,0(PSP)
+    ADDC    #0,TOS                  \ don't forget carry
+    GOTO    BW1                     \ jump to SECTOR
+    ENDCODE
+\   --------------------------------\
+
+\   --------------------------------\
+    CODE FAT                        \ Display FATsector
+\   --------------------------------\
     SUB     #4,PSP                  \
     MOV     TOS,2(PSP)              \
-    MOV     &FATsector,0(PSP)       \ FATsectorLO
-    ADD     &OrgFAT1,0(PSP)         \
+    MOV     &OrgFAT1,0(PSP)         \
     MOV     #0,TOS                  \ FATsectorHI = 0
-    JMP     SECT_D                  \ jump to a defined word
-ENDCODE
-\ ----------------------------------\
-    \
-
-\ dump DIR sector of opened file or first sector of current DIR by default
-\ ----------------------------------\
-CODE DIR_D                          \ Display DIR sector of CurrentHdl or CurrentDir sector by default 
-\ ----------------------------------\
+    GOTO    BW1                     \ jump to SECTOR
+    ENDCODE
+\   --------------------------------\
+
+\   --------------------------------\
+    CODE DIR                        \ Display CurrentDir first sector
+\   --------------------------------\
     SUB     #4,PSP                  \
     MOV     TOS,2(PSP)              \           save TOS
-\ ComputeClusFrstSect               \ If Cluster = 1 ==> RootDirectory ==> SectorL = OrgRootDir
-    CMP     #1,&DIRclusterL         \ clusterL = 1 ? (FAT16 specificity)
-    0= IF
-        CMP.B   #0,&DIRclusterH     \     clusterT = 0 ?
-        0=  IF
-            MOV #0,TOS              \
-            MOV &OrgRootDir,0(PSP)  \ sectorL for FAT16 OrgRootDIR is done
-            JMP SECT_D                
-        THEN
-    THEN                            \
-    MOV     &DIRclusterL,&ClusterL  \
-    MOV     &DIRclusterH,&ClusterH  \
-    GOTO    BW1                     \ jump to the backward LABEL BW1
-ENDCODE
-\ ----------------------------------\
-    \
-
-ECHO
-            ; added : UTILITY : ? SP@ .S WORDS MAX MIN U.R DUMP 
-            ; added : FAT_D to DUMP first sector of FAT1 and DIR_D for that of current DIRectory.
-            ; added : SECT_D to DUMP a sector and CLUST_D for first sector of a cluster
-            ;         include a decimal point to force 32 bits number, example : 2. CLUST_D
-    \
-PWR_HERE    ; to protect this app against a RESET, type: RST_HERE
+    MOV     &DIRclusterL,0(PSP)     \
+    MOV     &DIRclusterH,TOS        \
+    CMP     #0,TOS
+    0<>     ?GOTO BW2               \ jump to CLUSTER
+    CMP     #1,0(PSP)               \ cluster 1 ?
+    0<>     ?GOTO BW2               \ jump to CLUSTER
+    MOV     &OrgRootDir,0(PSP)      \ if yes, special case of FAT16 OrgRootDir
+    GOTO    BW1                     \ jump to SECTOR
+    ENDCODE
+\   --------------------------------\
+
+    RST_SET 
+
+    [THEN] \ endof [UNDEFINED] {SD_TOOLS} 
 
+    ECHO