1 | ; Project name : Assembly Library
|
---|
2 | ; Description : Functions for file access.
|
---|
3 |
|
---|
4 | ;
|
---|
5 | ; XTIDE Universal BIOS and Associated Tools
|
---|
6 | ; Copyright (C) 2009-2010 by Tomi Tilli, 2011-2012 by XTIDE Universal BIOS Team.
|
---|
7 | ;
|
---|
8 | ; This program is free software; you can redistribute it and/or modify
|
---|
9 | ; it under the terms of the GNU General Public License as published by
|
---|
10 | ; the Free Software Foundation; either version 2 of the License, or
|
---|
11 | ; (at your option) any later version.
|
---|
12 | ;
|
---|
13 | ; This program is distributed in the hope that it will be useful,
|
---|
14 | ; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
15 | ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
16 | ; GNU General Public License for more details.
|
---|
17 | ; Visit http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
---|
18 | ;
|
---|
19 |
|
---|
20 |
|
---|
21 | ; Section containing code
|
---|
22 | SECTION .text
|
---|
23 |
|
---|
24 | ;--------------------------------------------------------------------
|
---|
25 | ; FileIO_OpenWithPathInDSSIandFileAccessInAL
|
---|
26 | ; Parameters:
|
---|
27 | ; AL: FILE_ACCESS.(mode)
|
---|
28 | ; DS:SI: Ptr to NULL terminated path or file name
|
---|
29 | ; Returns:
|
---|
30 | ; AX: DOS error code if CF set
|
---|
31 | ; BX: File handle if CF cleared
|
---|
32 | ; CF: Clear if file opened successfully
|
---|
33 | ; Set if error
|
---|
34 | ; Corrupts registers:
|
---|
35 | ; AX, BX
|
---|
36 | ;--------------------------------------------------------------------
|
---|
37 | ALIGN JUMP_ALIGN
|
---|
38 | FileIO_OpenWithPathInDSSIandFileAccessInAL:
|
---|
39 | xchg dx, si ; Path now in DS:DX
|
---|
40 | mov ah, OPEN_EXISTING_FILE
|
---|
41 | int DOS_INTERRUPT_21h
|
---|
42 | xchg si, dx
|
---|
43 | mov bx, ax ; Copy file handle to BX
|
---|
44 | ret
|
---|
45 |
|
---|
46 |
|
---|
47 | ;--------------------------------------------------------------------
|
---|
48 | ; FileIO_ReadDXCXbytesToDSSIusingHandleFromBX
|
---|
49 | ; Parameters:
|
---|
50 | ; BX: File handle
|
---|
51 | ; DX:CX: Number of bytes to read
|
---|
52 | ; DS:SI: Ptr to destination buffer
|
---|
53 | ; Returns:
|
---|
54 | ; AX: DOS error code if CF set
|
---|
55 | ; CF: Clear if successful
|
---|
56 | ; Set if error
|
---|
57 | ; Corrupts registers:
|
---|
58 | ; AX
|
---|
59 | ;--------------------------------------------------------------------
|
---|
60 | ALIGN JUMP_ALIGN
|
---|
61 | FileIO_ReadDXCXbytesToDSSIusingHandleFromBX:
|
---|
62 | push bp
|
---|
63 | mov bp, FileIO_ReadCXbytesToDSSIusingHandleFromBX
|
---|
64 | call SplitLargeReadOrWritesToSmallerBlocks
|
---|
65 | pop bp
|
---|
66 | ret
|
---|
67 |
|
---|
68 | ;--------------------------------------------------------------------
|
---|
69 | ; File position is updated so next read will start where
|
---|
70 | ; previous read stopped.
|
---|
71 | ;
|
---|
72 | ; FileIO_ReadCXbytesToDSSIusingHandleFromBX
|
---|
73 | ; Parameters:
|
---|
74 | ; BX: File handle
|
---|
75 | ; CX: Number of bytes to read
|
---|
76 | ; DS:SI: Ptr to destination buffer
|
---|
77 | ; Returns:
|
---|
78 | ; AX: Number of bytes actually read if successful (0 if at EOF before call)
|
---|
79 | ; DOS error code if CF set
|
---|
80 | ; CF: Clear if successful
|
---|
81 | ; Set if error
|
---|
82 | ; Corrupts registers:
|
---|
83 | ; Nothing
|
---|
84 | ;--------------------------------------------------------------------
|
---|
85 | ALIGN JUMP_ALIGN
|
---|
86 | FileIO_ReadCXbytesToDSSIusingHandleFromBX:
|
---|
87 | xchg dx, si ; DS:DX now points to destination buffer
|
---|
88 | mov ah, READ_FROM_FILE_OR_DEVICE
|
---|
89 | int DOS_INTERRUPT_21h
|
---|
90 | xchg si, dx
|
---|
91 | ret
|
---|
92 |
|
---|
93 |
|
---|
94 | ;--------------------------------------------------------------------
|
---|
95 | ; FileIO_WriteDXCXbytesFromDSSIusingHandleFromBX
|
---|
96 | ; Parameters:
|
---|
97 | ; BX: File handle
|
---|
98 | ; DX:CX: Number of bytes to write
|
---|
99 | ; DS:SI: Ptr to source buffer
|
---|
100 | ; Returns:
|
---|
101 | ; AX: DOS error code if CF set
|
---|
102 | ; CF: Clear if successful
|
---|
103 | ; Set if error
|
---|
104 | ; Corrupts registers:
|
---|
105 | ; AX
|
---|
106 | ;--------------------------------------------------------------------
|
---|
107 | ALIGN JUMP_ALIGN
|
---|
108 | FileIO_WriteDXCXbytesFromDSSIusingHandleFromBX:
|
---|
109 | push bp
|
---|
110 | mov bp, FileIO_WriteCXbytesFromDSSIusingHandleFromBX
|
---|
111 | call SplitLargeReadOrWritesToSmallerBlocks
|
---|
112 | pop bp
|
---|
113 | ret
|
---|
114 |
|
---|
115 | ;--------------------------------------------------------------------
|
---|
116 | ; File position is updated so next write will start where
|
---|
117 | ; previous write stopped.
|
---|
118 | ;
|
---|
119 | ; FileIO_WriteCXbytesFromDSSIusingHandleFromBX:
|
---|
120 | ; Parameters:
|
---|
121 | ; BX: File handle
|
---|
122 | ; CX: Number of bytes to write
|
---|
123 | ; DS:SI: Ptr to source buffer
|
---|
124 | ; Returns:
|
---|
125 | ; AX: Number of bytes actually written if successful (EOF check)
|
---|
126 | ; DOS error code if CF set
|
---|
127 | ; CF: Clear if successful
|
---|
128 | ; Set if error
|
---|
129 | ; Corrupts registers:
|
---|
130 | ; Nothing
|
---|
131 | ;--------------------------------------------------------------------
|
---|
132 | ALIGN JUMP_ALIGN
|
---|
133 | FileIO_WriteCXbytesFromDSSIusingHandleFromBX:
|
---|
134 | xchg dx, si ; DS:DX now points to source buffer
|
---|
135 | mov ah, WRITE_TO_FILE_OR_DEVICE
|
---|
136 | int DOS_INTERRUPT_21h
|
---|
137 | xchg si, dx
|
---|
138 | ret
|
---|
139 |
|
---|
140 |
|
---|
141 | ;--------------------------------------------------------------------
|
---|
142 | ; SplitLargeReadOrWritesToSmallerBlocks
|
---|
143 | ; Parameters:
|
---|
144 | ; BX: File handle
|
---|
145 | ; BP: Ptr to transfer function
|
---|
146 | ; DX:CX: Number of bytes to transfer
|
---|
147 | ; DS:SI: Ptr to transfer buffer
|
---|
148 | ; Returns:
|
---|
149 | ; AX: DOS error code if CF set
|
---|
150 | ; CF: Clear if successful
|
---|
151 | ; Set if error
|
---|
152 | ; Corrupts registers:
|
---|
153 | ; AX
|
---|
154 | ;--------------------------------------------------------------------
|
---|
155 | ALIGN JUMP_ALIGN
|
---|
156 | SplitLargeReadOrWritesToSmallerBlocks:
|
---|
157 | push ds
|
---|
158 | push si
|
---|
159 | push dx
|
---|
160 | push cx
|
---|
161 |
|
---|
162 | xchg ax, cx ; DX:AX now holds bytes to transfer
|
---|
163 | mov cx, SPLIT_SIZE_FOR_LARGE_TRANSFERS
|
---|
164 | div cx ; AX = Number of full transfers
|
---|
165 | push dx ; Bytes for last transfer
|
---|
166 | test ax, ax
|
---|
167 | jz SHORT .TransferRemainingBytes
|
---|
168 | xchg dx, ax ; DX = Number of full transfers
|
---|
169 |
|
---|
170 | ALIGN JUMP_ALIGN
|
---|
171 | .TransferNextBytes:
|
---|
172 | call NormalizeDSSI
|
---|
173 | call bp ; Transfer function
|
---|
174 | jc SHORT .ErrorOccurredDuringTransfer
|
---|
175 | add si, SPLIT_SIZE_FOR_LARGE_TRANSFERS
|
---|
176 | dec dx
|
---|
177 | jnz SHORT .TransferNextBytes
|
---|
178 | .TransferRemainingBytes:
|
---|
179 | pop cx ; CX = Bytes for last transfer
|
---|
180 | jcxz .ReturnErrorCodeInAX ; No remaining bytes
|
---|
181 | call NormalizeDSSI
|
---|
182 | call bp
|
---|
183 | .ReturnErrorCodeInAX:
|
---|
184 | pop cx
|
---|
185 | pop dx
|
---|
186 | pop si
|
---|
187 | pop ds
|
---|
188 | ret
|
---|
189 | .ErrorOccurredDuringTransfer:
|
---|
190 | pop cx ; Remove bytes for last transfer
|
---|
191 | jmp SHORT .ReturnErrorCodeInAX
|
---|
192 |
|
---|
193 | ;--------------------------------------------------------------------
|
---|
194 | ; NormalizeDSSI
|
---|
195 | ; Parameters
|
---|
196 | ; DS:SI: Ptr to normalize
|
---|
197 | ; Returns:
|
---|
198 | ; DS:SI: Normalized pointer
|
---|
199 | ; Corrupts registers:
|
---|
200 | ; Nothing
|
---|
201 | ;--------------------------------------------------------------------
|
---|
202 | ALIGN JUMP_ALIGN
|
---|
203 | NormalizeDSSI:
|
---|
204 | push dx
|
---|
205 | push ax
|
---|
206 | NORMALIZE_FAR_POINTER ds, si, ax, dx
|
---|
207 | pop ax
|
---|
208 | pop dx
|
---|
209 | ret
|
---|
210 |
|
---|
211 |
|
---|
212 | ;--------------------------------------------------------------------
|
---|
213 | ; FileIO_GetFileSizeToDXAXusingHandleFromBXandResetFilePosition:
|
---|
214 | ; Parameters:
|
---|
215 | ; BX: File handle
|
---|
216 | ; Returns:
|
---|
217 | ; DX:AX: Signed file size (if CF cleared)
|
---|
218 | ; AX: DOS error code (if CF set)
|
---|
219 | ; CF: Clear if successful
|
---|
220 | ; Set if error
|
---|
221 | ; Corrupts registers:
|
---|
222 | ; Nothing
|
---|
223 | ;--------------------------------------------------------------------
|
---|
224 | ALIGN JUMP_ALIGN
|
---|
225 | FileIO_GetFileSizeToDXAXusingHandleFromBXandResetFilePosition:
|
---|
226 | push cx
|
---|
227 |
|
---|
228 | ; Get file size to DX:AX
|
---|
229 | xor cx, cx
|
---|
230 | xor dx, dx
|
---|
231 | mov al, SEEK_FROM.endOfFile
|
---|
232 | call FileIO_SeekFromOriginInALtoOffsetInDXAXusingHandleFromBX
|
---|
233 | jc SHORT .ReturnFileError
|
---|
234 | push dx
|
---|
235 | push ax
|
---|
236 |
|
---|
237 | ; Reset file position
|
---|
238 | xor dx, dx
|
---|
239 | mov al, SEEK_FROM.startOfFile
|
---|
240 | call FileIO_SeekFromOriginInALtoOffsetInDXAXusingHandleFromBX
|
---|
241 | pop ax
|
---|
242 | pop dx
|
---|
243 |
|
---|
244 | .ReturnFileError:
|
---|
245 | pop cx
|
---|
246 | ret
|
---|
247 |
|
---|
248 |
|
---|
249 | ;--------------------------------------------------------------------
|
---|
250 | ; FileIO_CloseUsingHandleFromBX
|
---|
251 | ; Parameters:
|
---|
252 | ; BX: File handle
|
---|
253 | ; Returns:
|
---|
254 | ; AX: DOS error code if CF set
|
---|
255 | ; CF: Clear if file closed successfully
|
---|
256 | ; Set if error
|
---|
257 | ; Corrupts registers:
|
---|
258 | ; AX
|
---|
259 | ;--------------------------------------------------------------------
|
---|
260 | ALIGN JUMP_ALIGN
|
---|
261 | FileIO_CloseUsingHandleFromBX:
|
---|
262 | mov ah, CLOSE_FILE
|
---|
263 | SKIP2B f ; cmp ax, <next instruction>
|
---|
264 | ; Fall to FileIO_SeekFromOriginInALtoOffsetInDXAXusingHandleFromBX
|
---|
265 |
|
---|
266 |
|
---|
267 | ;--------------------------------------------------------------------
|
---|
268 | ; FileIO_SeekFromOriginInALtoOffsetInDXAXusingHandleFromBX:
|
---|
269 | ; Parameters:
|
---|
270 | ; AL: SEEK_FROM.(origin)
|
---|
271 | ; BX: File handle
|
---|
272 | ; CX:DX: Signed offset to seek starting from AL
|
---|
273 | ; Returns:
|
---|
274 | ; DX:AX: New file position in bytes from start of file (if CF cleared)
|
---|
275 | ; AX: DOS error code (if CF set)
|
---|
276 | ; CF: Clear if successful
|
---|
277 | ; Set if error
|
---|
278 | ; Corrupts registers:
|
---|
279 | ; Nothing
|
---|
280 | ;--------------------------------------------------------------------
|
---|
281 | FileIO_SeekFromOriginInALtoOffsetInDXAXusingHandleFromBX:
|
---|
282 | mov ah, SET_CURRENT_FILE_POSITION
|
---|
283 | int DOS_INTERRUPT_21h
|
---|
284 | ret
|
---|