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