OSDN Git Service

39d17f7a5fce6bf65b9a69a215a41b9cff973dc5
[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             MOV @IP+,PC
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             MOV @IP+,PC
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  .word   $+2
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             MOV @IP+,PC             ;
65
66     .ENDIF
67
68     .IFNDEF II
69 ; https://forth-standard.org/standard/core/I
70 ; I        -- n   R: sys1 sys2 -- sys1 sys2
71 ;                  get the innermost loop index
72             FORTHWORD "I"
73 II          SUB #2,PSP              ;1 make room in TOS
74             MOV TOS,0(PSP)          ;3
75             MOV @RSP,TOS            ;2 index = loopctr - fudge
76             SUB 2(RSP),TOS          ;3
77             MOV @IP+,PC             ;4 13~
78     .ENDIF
79
80         .IFNDEF OVER
81 ;https://forth-standard.org/standard/core/OVER
82 ;C OVER    x1 x2 -- x1 x2 x1
83             FORTHWORD "OVER"
84 OVER        MOV TOS,-2(PSP)     ; 3 -- x1 (x2) x2
85             MOV @PSP,TOS        ; 2 -- x1 (x2) x1
86             SUB #2,PSP          ; 1 -- x1 x2 x1
87             MOV @IP+,PC         ; 4
88         .ENDIF
89
90     .IFNDEF TOR
91 ; https://forth-standard.org/standard/core/toR
92 ; >R    x --   R: -- x   push to return stack
93             FORTHWORD ">R"
94 TOR         PUSH TOS
95             MOV @PSP+,TOS
96             MOV @IP+,PC
97     .ENDIF
98
99     .IFNDEF UDOTR
100 ;https://forth-standard.org/standard/core/UDotR
101 ;X U.R      u n --      display u unsigned in n width
102             FORTHWORD "U.R"
103 UDOTR       mDOCOL
104             .word   TOR,LESSNUM,lit,0,NUM,NUMS,NUMGREATER
105             .word   RFROM,OVER,MINUS,lit,0,MAX,SPACES,TYPE
106             .word   EXIT
107     .ENDIF
108
109         .IFNDEF CFETCH
110 ;https://forth-standard.org/standard/core/CFetch
111 ;C C@     c-addr -- char   fetch char from memory
112             FORTHWORD "C@"
113 CFETCH      MOV.B @TOS,TOS      ;2
114             MOV @IP+,PC         ;4
115         .ENDIF
116
117     .IFNDEF PLUS
118 ;https://forth-standard.org/standard/core/Plus
119 ;C +       n1/u1 n2/u2 -- n3/u3     add n1+n2
120             FORTHWORD "+"
121 PLUS        ADD @PSP+,TOS
122             MOV @IP+,PC
123     .ENDIF
124
125     .IFNDEF DUMP
126 ;https://forth-standard.org/standard/tools/DUMP
127             FORTHWORD "DUMP"
128 DUMP        PUSH    IP
129             PUSH    &BASE                   ; save current base
130             MOV     #10h,&BASE              ; HEX base
131             ADD     @PSP,TOS                ; -- ORG END
132             ASMtoFORTH
133             .word   SWAP                    ; -- END ORG
134             .word   xdo                     ; --
135 DUMP1       .word   CR
136             .word   II,lit,4,UDOTR,SPACE    ; generate address
137
138             .word   II,lit,8,PLUS,II,xdo    ; display first 8 bytes
139 DUMP2       .word   II,CFETCH,lit,3,UDOTR
140             .word   xloop,DUMP2             ; bytes display loop
141             .word   SPACE
142             .word   II,lit,10h,PLUS,II,lit,8,PLUS,xdo    ; display last 8 bytes
143 DUMP3       .word   II,CFETCH,lit,3,UDOTR
144             .word   xloop,DUMP3             ; bytes display loop
145             .word   SPACE,SPACE
146             .word   II,lit,10h,PLUS,II,xdo  ; display 16 chars
147 DUMP4       .word   II,CFETCH
148             .word   lit,7Eh,MIN,FBLANK,MAX,EMIT
149             .word   xloop,DUMP4             ; chars display loop
150             .word   lit,10h,xploop,DUMP1    ; line loop
151             .word   RFROM,lit,BASE,STORE    ; restore current base
152             .word   EXIT
153
154     .ENDIF
155
156     FORTHWORD "{SD_TOOLS}"
157     MOV @IP+,PC
158
159 ; read logical sector and dump it 
160 ; ----------------------------------;
161     FORTHWORD "SECTOR"              ; sector. --            don't forget to add decimal point to your sector number (if < 65536)
162 ; ----------------------------------;
163 SECTOR
164     MOV     TOS,X                   ; X = SectorH
165     MOV     @PSP,W                  ; W = sectorL
166     CALL    #readSectorWX           ; W = SectorLO  X = SectorHI
167 DisplaySector
168     mDOCOL                          ;
169     .word   LESSNUM,NUMS,NUMGREATER ; ud --            display the double number
170     .word   TYPE,SPACE              ;
171     .word   lit,SD_BUF,lit,200h,DUMP;    
172     .word   EXIT                    ;
173 ; ----------------------------------;
174
175 ; ----------------------------------;
176 ; read first sector of Cluster and dump it
177 ; ----------------------------------;
178             FORTHWORD "CLUSTER"     ; cluster.  --         don't forget to add decimal point to your sector number (if < 65536)
179 ; ----------------------------------;
180 CLUSTER
181     MOV.B   &SecPerClus,W           ; SecPerClus(54321) = multiplicator
182     MOV     @PSP,X                  ; X = ClusterL
183     JMP     CLUSTER1                ;
184 CLUSTERLOOP
185     ADD     X,X                     ; (RLA) shift one left MULTIPLICANDlo16
186     ADDC    TOS,TOS                 ; (RLC) shift one left MULTIPLICANDhi8
187 CLUSTER1
188     RRA     W                       ; shift one right multiplicator
189     JNC     CLUSTERLOOP             ; if not carry
190     ADD     &OrgClusters,X          ; add OrgClusters = sector of virtual cluster 0 (word size)
191     MOV     X,0(PSP)      
192     ADDC    #0,TOS                  ; don't forget carry
193     JMP     SECTOR                  ; jump to a defined word
194 ; ----------------------------------;
195
196 ; dump FAT1 first sector
197 ; ----------------------------------;
198             FORTHWORD "FAT"         ;VWXY Display first FATsector
199 ; ----------------------------------;
200     SUB     #4,PSP                  ;
201     MOV     TOS,2(PSP)              ;
202     MOV     &OrgFAT1,0(PSP)         ;
203     MOV     #0,TOS                  ; FATsectorHI = 0
204     JMP     SECTOR                  ;
205 ; ----------------------------------;
206
207
208 ; dump current DIR first sector
209 ; ----------------------------------;
210             FORTHWORD "DIR"         ;
211 ; ----------------------------------;
212     SUB     #4,PSP                  ;
213     MOV     TOS,2(PSP)              ;           save TOS
214     MOV     &DIRclusterL,0(PSP)     ;
215     MOV     &DIRclusterH,TOS        ;
216     CMP     #0,TOS
217     JNZ     CLUSTER
218     CMP     #1,0(PSP)               ; cluster 1 ?
219     JNZ     CLUSTER       
220     MOV     &OrgRootDir,0(PSP)      ; if yes, special case of FAT16 OrgRootDir        
221     JMP     SECTOR
222 ; ----------------------------------;
223