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