OSDN Git Service

V308
[fast-forth/master.git] / ADDON / SD_TOOLS.asm
1 ; -*- coding: utf-8 -*-
2
3     .IFNDEF MAX
4
5 ;https://forth-standard.org/standard/core/MAX
6 ;C MAX    n1 n2 -- n3       signed maximum
7             FORTHWORD "MAX"
8 MAX:        CMP @PSP,TOS        ; n2-n1
9             JL SELn1            ; n2<n1
10 SELn2:      ADD #2,PSP
11             MOV @IP+,PC
12
13 ;https://forth-standard.org/standard/core/MIN
14 ;C MIN    n1 n2 -- n3       signed minimum
15             FORTHWORD "MIN"
16 MIN:        CMP @PSP,TOS        ; n2-n1
17             JL SELn2            ; n2<n1
18 SELn1:      MOV @PSP+,TOS
19             MOV @IP+,PC
20
21     .ENDIF
22
23     .IFNDEF SPACE
24 ;https://forth-standard.org/standard/core/SPACE
25 ;C SPACE   --               output a space
26             FORTHWORD "SPACE"
27 SPACE       SUB #2,PSP              ;1
28             MOV TOS,0(PSP)          ;3
29             MOV #20h,TOS            ;2
30             MOV #EMIT,PC            ;17~  23~
31
32 ;https://forth-standard.org/standard/core/SPACES
33 ;C SPACES   n --            output n spaces
34             FORTHWORD "SPACES"
35 SPACES      CMP #0,TOS
36             JZ SPACESNEXT2
37             PUSH IP
38             MOV #SPACESNEXT,IP
39             JMP SPACE               ;25~
40 SPACESNEXT  .word   $+2
41             SUB #2,IP               ;1
42             SUB #1,TOS              ;1
43             JNZ SPACE               ;25~ ==> 27~ by space ==> 2.963 MBds @ 8 MHz
44             MOV @RSP+,IP            ;
45 SPACESNEXT2 MOV @PSP+,TOS           ; --         drop n
46             MOV @IP+,PC             ;
47
48     .ENDIF
49
50     .IFNDEF II
51 ; https://forth-standard.org/standard/core/I
52 ; I        -- n   R: sys1 sys2 -- sys1 sys2
53 ;                  get the innermost loop index
54             FORTHWORD "I"
55 II          SUB #2,PSP              ;1 make room in TOS
56             MOV TOS,0(PSP)          ;3
57             MOV @RSP,TOS            ;2 index = loopctr - fudge
58             SUB 2(RSP),TOS          ;3
59             MOV @IP+,PC             ;4 13~
60     .ENDIF
61
62         .IFNDEF OVER
63 ;https://forth-standard.org/standard/core/OVER
64 ;C OVER    x1 x2 -- x1 x2 x1
65             FORTHWORD "OVER"
66 OVER        MOV TOS,-2(PSP)         ; 3 -- x1 (x2) x2
67             MOV @PSP,TOS            ; 2 -- x1 (x2) x1
68             SUB #2,PSP              ; 1 -- x1 x2 x1
69             MOV @IP+,PC             ; 4
70         .ENDIF
71
72     .IFNDEF TOR
73 ; https://forth-standard.org/standard/core/toR
74 ; >R    x --   R: -- x   push to return stack
75             FORTHWORD ">R"
76 TOR         PUSH TOS
77             MOV @PSP+,TOS
78             MOV @IP+,PC
79     .ENDIF
80
81     .IFNDEF UDOTR
82 ;https://forth-standard.org/standard/core/UDotR
83 ;X U.R      u n --      display u unsigned in n width
84             FORTHWORD "U.R"
85 UDOTR       mDOCOL
86             .word   TOR,LESSNUM,lit,0,NUM,NUMS,NUMGREATER
87             .word   RFROM,OVER,MINUS,lit,0,MAX,SPACES,TYPE
88             .word   EXIT
89     .ENDIF
90
91         .IFNDEF CFETCH
92 ;https://forth-standard.org/standard/core/CFetch
93 ;C C@     c-addr -- char   fetch char from memory
94             FORTHWORD "C@"
95 CFETCH      MOV.B @TOS,TOS          ;2
96             MOV @IP+,PC             ;4
97         .ENDIF
98
99     .IFNDEF PLUS
100 ;https://forth-standard.org/standard/core/Plus
101 ;C +       n1/u1 n2/u2 -- n3/u3     add n1+n2
102             FORTHWORD "+"
103 PLUS        ADD @PSP+,TOS
104             MOV @IP+,PC
105     .ENDIF
106
107     .IFNDEF DUMP
108 ;https://forth-standard.org/standard/tools/DUMP
109             FORTHWORD "DUMP"
110 DUMP        PUSH IP
111             PUSH &BASE                      ; save current base
112             MOV #10h,&BASE                  ; HEX base
113             ADD @PSP,TOS                    ; -- ORG END
114             ASMtoFORTH
115             .word   SWAP                    ; -- END ORG
116             .word   xdo                     ; --
117 DUMP1       .word   CR
118             .word   II,lit,4,UDOTR,SPACE    ; generate address
119
120             .word   II,lit,8,PLUS,II,xdo    ; display first 8 bytes
121 DUMP2       .word   II,CFETCH,lit,3,UDOTR
122             .word   xloop,DUMP2             ; bytes display loop
123             .word   SPACE
124             .word   II,lit,10h,PLUS,II,lit,8,PLUS,xdo    ; display last 8 bytes
125 DUMP3       .word   II,CFETCH,lit,3,UDOTR
126             .word   xloop,DUMP3             ; bytes display loop
127             .word   SPACE,SPACE
128             .word   II,lit,10h,PLUS,II,xdo  ; display 16 chars
129 DUMP4       .word   II,CFETCH
130             .word   lit,7Eh,MIN,FBLANK,MAX,EMIT
131             .word   xloop,DUMP4             ; chars display loop
132             .word   lit,10h,xploop,DUMP1    ; line loop
133             .word   RFROM,lit,BASE,STORE    ; restore current base
134             .word   EXIT
135
136     .ENDIF
137
138     FORTHWORD "{SD_TOOLS}"
139     MOV @IP+,PC
140
141 ; read logical sector and dump it 
142 ; ----------------------------------;
143             FORTHWORD "SECTOR."     ; sector. --            don't forget to add decimal point to your sector number (if < 65536)
144 ; ----------------------------------;
145 SECTOR      MOV TOS,X               ; X = SectorH
146             MOV @PSP,W              ; W = sectorL
147             CALL #readSectorWX      ; W = SectorLO  X = SectorHI
148 DisplaySector
149             mDOCOL                  ;
150             .word   LESSNUM,NUMS
151             .word   NUMGREATER      ; ud --            display the double number
152             .word   TYPE,SPACE      ;
153             .word   lit,SD_BUF
154             .word   lit,200h,DUMP   ;    
155             .word   EXIT            ;
156 ; ----------------------------------;
157
158 ; ----------------------------------;
159 ; read first sector of Cluster and dump it
160 ; ----------------------------------;
161             FORTHWORD "CLUSTR."     ; cluster.  --         don't forget to add decimal point to your sector number (if < 65536)
162 ; ----------------------------------;
163 CLUSTER     BIT.B #CD_SD,&SD_CDIN   ; test Card Detect: memory card present ?
164             JZ CD_CLUST_OK          ;
165             MOV #COLD,PC            ; no: force COLD
166 CD_CLUST_OK MOV.B &SecPerClus,W     ; SecPerClus(54321) = multiplicator
167             MOV @PSP,X              ; X = ClusterL
168             JMP CLUSTER1            ;
169 CLUSTERLOOP ADD X,X                 ; (RLA) shift one left MULTIPLICANDlo16
170             ADDC TOS,TOS            ; (RLC) shift one left MULTIPLICANDhi8
171 CLUSTER1    RRA W                   ; shift one right multiplicator
172             JNC CLUSTERLOOP         ; if not carry
173             ADD &OrgClusters,X      ; add OrgClusters = sector of virtual cluster 0 (word size)
174             MOV X,0(PSP)            
175             ADDC #0,TOS             ; don't forget carry
176             JMP SECTOR              ; jump to a defined word
177 ; ----------------------------------;
178
179 ; dump FAT1 first sector
180 ; ----------------------------------;
181             FORTHWORD "FAT"         ;VWXY Display first FATsector
182 ; ----------------------------------;
183             SUB #4,PSP              ;
184             MOV TOS,2(PSP)          ;
185             MOV &OrgFAT1,0(PSP)     ;
186             MOV #0,TOS              ; FATsectorHI = 0
187             JMP SECTOR              ;
188 ; ----------------------------------;
189
190
191 ; dump current DIR first sector
192 ; ----------------------------------;
193             FORTHWORD "DIR"         ;
194 ; ----------------------------------;
195             SUB #4,PSP              ;
196             MOV TOS,2(PSP)          ;           save TOS
197             MOV &DIRclusterL,0(PSP) ;
198             MOV &DIRclusterH,TOS    ;
199             CMP #0,TOS
200             JNZ CLUSTER
201             CMP #1,0(PSP)           ; cluster 1 ?
202             JNZ CLUSTER       
203             MOV &OrgRootDir,0(PSP)  ; if yes, special case of FAT16 OrgRootDir        
204             JMP SECTOR
205 ; ----------------------------------;
206