OSDN Git Service

Ver 301
[fast-forth/master.git] / ADDON / SD_TOOLS.asm
1 ; -*- coding: utf-8 -*-
2 ; http://patorjk.com/software/taag/#p=display&f=Banner&t=Fast Forth
3
4 ; Fast Forth For Texas Instrument MSP430FRxxxx FRAM devices
5 ; Copyright (C) <2015>  <J.M. THOORENS>
6 ;
7 ; This program is free software: you can redistribute it and/or modify
8 ; it under the terms of the GNU General Public License as published by
9 ; the Free Software Foundation, either version 3 of the License, or
10 ; (at your option) any later version.
11 ;
12 ; This program is distributed in the hope that it will be useful,
13 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 ; GNU General Public License for more details.
16 ;
17 ; You should have received a copy of the GNU General Public License
18 ; along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20
21     .IFNDEF MAX
22
23 ;https://forth-standard.org/standard/core/MAX
24 ;C MAX    n1 n2 -- n3       signed maximum
25             FORTHWORD "MAX"
26 MAX:        CMP     @PSP,TOS    ; n2-n1
27             JL      SELn1       ; n2<n1
28 SELn2:      ADD     #2,PSP
29             mNEXT
30
31 ;https://forth-standard.org/standard/core/MIN
32 ;C MIN    n1 n2 -- n3       signed minimum
33             FORTHWORD "MIN"
34 MIN:        CMP     @PSP,TOS    ; n2-n1
35             JL      SELn2       ; n2<n1
36 SELn1:      MOV     @PSP+,TOS
37             mNEXT
38
39     .ENDIF
40
41     .IFNDEF SPACE
42 ;https://forth-standard.org/standard/core/SPACE
43 ;C SPACE   --               output a space
44             FORTHWORD "SPACE"
45 SPACE       SUB #2,PSP              ;1
46             MOV TOS,0(PSP)          ;3
47             MOV #20h,TOS            ;2
48             MOV #EMIT,PC            ;17~  23~
49
50 ;https://forth-standard.org/standard/core/SPACES
51 ;C SPACES   n --            output n spaces
52             FORTHWORD "SPACES"
53 SPACES      CMP #0,TOS
54             JZ SPACESNEXT2
55             PUSH IP
56             MOV #SPACESNEXT,IP
57             JMP SPACE               ;25~
58 SPACESNEXT  FORTHtoASM
59             SUB #2,IP               ;1
60             SUB #1,TOS              ;1
61             JNZ SPACE               ;25~ ==> 27~ by space ==> 2.963 MBds @ 8 MHz
62             MOV @RSP+,IP            ;
63 SPACESNEXT2 MOV @PSP+,TOS           ; --         drop n
64             mNEXT                   ;
65
66     .ENDIF
67
68         .IFNDEF OVER
69 ;https://forth-standard.org/standard/core/OVER
70 ;C OVER    x1 x2 -- x1 x2 x1
71             FORTHWORD "OVER"
72 OVER        MOV TOS,-2(PSP)     ; 3 -- x1 (x2) x2
73             MOV @PSP,TOS        ; 2 -- x1 (x2) x1
74             SUB #2,PSP          ; 1 -- x1 x2 x1
75             mNEXT               ; 4
76         .ENDIF
77
78     .IFNDEF UDOTR
79 ;https://forth-standard.org/standard/core/UDotR
80 ;X U.R      u n --      display u unsigned in n width
81             FORTHWORD "U.R"
82 UDOTR       mDOCOL
83             .word   TOR,LESSNUM,lit,0,NUM,NUMS,NUMGREATER
84             .word   RFROM,OVER,MINUS,lit,0,MAX,SPACES,TYPE
85             .word   EXIT
86     .ENDIF
87
88         .IFNDEF CFETCH
89 ;https://forth-standard.org/standard/core/CFetch
90 ;C C@     c-addr -- char   fetch char from memory
91             FORTHWORD "C@"
92 CFETCH      MOV.B @TOS,TOS      ;2
93             mNEXT               ;4
94         .ENDIF
95
96     .IFNDEF PLUS
97 ;https://forth-standard.org/standard/core/Plus
98 ;C +       n1/u1 n2/u2 -- n3/u3     add n1+n2
99             FORTHWORD "+"
100 PLUS        ADD @PSP+,TOS
101             mNEXT
102     .ENDIF
103
104     .IFNDEF DUMP
105 ;https://forth-standard.org/standard/tools/DUMP
106             FORTHWORD "DUMP"
107 DUMP        PUSH    IP
108             PUSH    &BASE                   ; save current base
109             MOV     #10h,&BASE              ; HEX base
110             ADD     @PSP,TOS                ; -- ORG END
111             ASMtoFORTH
112             .word   SWAP                    ; -- END ORG
113             .word   xdo                     ; --
114 DUMP1       .word   CR
115             .word   II,lit,4,UDOTR,SPACE    ; generate address
116
117             .word   II,lit,8,PLUS,II,xdo    ; display first 8 bytes
118 DUMP2       .word   II,CFETCH,lit,3,UDOTR
119             .word   xloop,DUMP2             ; bytes display loop
120             .word   SPACE
121             .word   II,lit,10h,PLUS,II,lit,8,PLUS,xdo    ; display last 8 bytes
122 DUMP3       .word   II,CFETCH,lit,3,UDOTR
123             .word   xloop,DUMP3             ; bytes display loop
124             .word   SPACE,SPACE
125             .word   II,lit,10h,PLUS,II,xdo  ; display 16 chars
126 DUMP4       .word   II,CFETCH
127             .word   lit,7Eh,MIN,FBLANK,MAX,EMIT
128             .word   xloop,DUMP4             ; chars display loop
129             .word   lit,10h,xploop,DUMP1    ; line loop
130             .word   RFROM,lit,BASE,STORE       ; restore current base
131             .word   EXIT
132
133     .ENDIF
134
135     FORTHWORD "{SD_TOOLS}"
136     mNEXT
137
138 ; read logical sector and dump it 
139 ; ----------------------------------;
140     FORTHWORD "SECTOR"              ; sector. --            don't forget to add decimal point to your sector number (if < 65536)
141 ; ----------------------------------;
142 SECTOR
143     MOV     TOS,X                   ; X = SectorH
144     MOV     @PSP,W                  ; W = sectorL
145     CALL    #readSectorWX           ; W = SectorLO  X = SectorHI
146 DisplaySector
147     mDOCOL                          ;
148     .word   LESSNUM,NUMS,NUMGREATER ; ud --            display the double number
149     .word   TYPE,SPACE              ;
150     .word   lit,SD_BUF,lit,200h,DUMP;    
151     .word   EXIT                    ;
152 ; ----------------------------------;
153
154 ; ----------------------------------;
155 ; read first sector of Cluster and dump it
156 ; ----------------------------------;
157             FORTHWORD "CLUSTER"     ; cluster.  --         don't forget to add decimal point to your sector number (if < 65536)
158 ; ----------------------------------;
159 CLUSTER
160     MOV.B   &SecPerClus,W           ; SecPerClus(54321) = multiplicator
161     MOV     @PSP,X                  ; X = ClusterL
162     JMP     CLUSTER1                ;
163 CLUSTERLOOP
164     ADD     X,X                     ; (RLA) shift one left MULTIPLICANDlo16
165     ADDC    TOS,TOS                 ; (RLC) shift one left MULTIPLICANDhi8
166 CLUSTER1
167     RRA     W                       ; shift one right multiplicator
168     JNC     CLUSTERLOOP             ; if not carry
169     ADD     &OrgClusters,X          ; add OrgClusters = sector of virtual cluster 0 (word size)
170     MOV     X,0(PSP)      
171     ADDC    #0,TOS                  ; don't forget carry
172     JMP     SECTOR                  ; jump to a defined word
173 ; ----------------------------------;
174
175 ; dump FAT1 first sector
176 ; ----------------------------------;
177             FORTHWORD "FAT"         ;VWXY Display first FATsector
178 ; ----------------------------------;
179     SUB     #4,PSP                  ;
180     MOV     TOS,2(PSP)              ;
181     MOV     &OrgFAT1,0(PSP)         ;
182     MOV     #0,TOS                  ; FATsectorHI = 0
183     JMP     SECTOR                  ;
184 ; ----------------------------------;
185
186
187 ; dump current DIR first sector
188 ; ----------------------------------;
189             FORTHWORD "DIR"         ;
190 ; ----------------------------------;
191     SUB     #4,PSP                  ;
192     MOV     TOS,2(PSP)              ;           save TOS
193     MOV     &DIRclusterL,0(PSP)     ;
194     MOV     &DIRclusterH,TOS        ;
195     CMP     #0,TOS
196     JNZ     CLUSTER
197     CMP     #1,0(PSP)               ; cluster 1 ?
198     JNZ     CLUSTER       
199     MOV     &OrgRootDir,0(PSP)      ; if yes, special case of FAT16 OrgRootDir        
200     JMP     SECTOR
201 ; ----------------------------------;
202