[41] | 1 | ; Project name : Assembly Library |
---|
| 2 | ; Description : Functions for file access. |
---|
| 3 | |
---|
[376] | 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 | |
---|
[41] | 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 |
---|
[51] | 43 | mov bx, ax ; Copy file handle to BX |
---|
[41] | 44 | ret |
---|
| 45 | |
---|
| 46 | |
---|
| 47 | ;-------------------------------------------------------------------- |
---|
[66] | 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 |
---|
[293] | 55 | ; CF: Clear if successful |
---|
[66] | 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 | ;-------------------------------------------------------------------- |
---|
[41] | 69 | ; File position is updated so next read will start where |
---|
| 70 | ; previous read stopped. |
---|
[133] | 71 | ; |
---|
[41] | 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: |
---|
[293] | 78 | ; AX: Number of bytes actually read if successful (0 if at EOF before call) |
---|
[41] | 79 | ; DOS error code if CF set |
---|
[293] | 80 | ; CF: Clear if successful |
---|
[41] | 81 | ; Set if error |
---|
| 82 | ; Corrupts registers: |
---|
| 83 | ; Nothing |
---|
| 84 | ;-------------------------------------------------------------------- |
---|
| 85 | ALIGN JUMP_ALIGN |
---|
| 86 | FileIO_ReadCXbytesToDSSIusingHandleFromBX: |
---|
[51] | 87 | xchg dx, si ; DS:DX now points to destination buffer |
---|
[41] | 88 | mov ah, READ_FROM_FILE_OR_DEVICE |
---|
| 89 | int DOS_INTERRUPT_21h |
---|
| 90 | xchg si, dx |
---|
| 91 | ret |
---|
| 92 | |
---|
| 93 | |
---|
| 94 | ;-------------------------------------------------------------------- |
---|
[66] | 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 |
---|
[293] | 102 | ; CF: Clear if successful |
---|
[66] | 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 | ;-------------------------------------------------------------------- |
---|
[41] | 116 | ; File position is updated so next write will start where |
---|
| 117 | ; previous write stopped. |
---|
[133] | 118 | ; |
---|
[41] | 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: |
---|
[293] | 125 | ; AX: Number of bytes actually written if successful (EOF check) |
---|
[41] | 126 | ; DOS error code if CF set |
---|
[293] | 127 | ; CF: Clear if successful |
---|
[41] | 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 | ;-------------------------------------------------------------------- |
---|
[66] | 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 |
---|
[293] | 150 | ; CF: Clear if successful |
---|
[66] | 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: |
---|
[105] | 172 | call NormalizeDSSI |
---|
[66] | 173 | call bp ; Transfer function |
---|
| 174 | jc SHORT .ErrorOccurredDuringTransfer |
---|
[67] | 175 | add si, SPLIT_SIZE_FOR_LARGE_TRANSFERS |
---|
[66] | 176 | dec dx |
---|
| 177 | jnz SHORT .TransferNextBytes |
---|
| 178 | .TransferRemainingBytes: |
---|
| 179 | pop cx ; CX = Bytes for last transfer |
---|
| 180 | jcxz .ReturnErrorCodeInAX ; No remaining bytes |
---|
[105] | 181 | call NormalizeDSSI |
---|
[66] | 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 | |
---|
[105] | 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 |
---|
[66] | 210 | |
---|
[105] | 211 | |
---|
[66] | 212 | ;-------------------------------------------------------------------- |
---|
[51] | 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) |
---|
[293] | 219 | ; CF: Clear if successful |
---|
[51] | 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 | ;-------------------------------------------------------------------- |
---|
[133] | 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 | ;-------------------------------------------------------------------- |
---|
[41] | 268 | ; FileIO_SeekFromOriginInALtoOffsetInDXAXusingHandleFromBX: |
---|
| 269 | ; Parameters: |
---|
| 270 | ; AL: SEEK_FROM.(origin) |
---|
| 271 | ; BX: File handle |
---|
[51] | 272 | ; CX:DX: Signed offset to seek starting from AL |
---|
[41] | 273 | ; Returns: |
---|
[51] | 274 | ; DX:AX: New file position in bytes from start of file (if CF cleared) |
---|
| 275 | ; AX: DOS error code (if CF set) |
---|
[293] | 276 | ; CF: Clear if successful |
---|
[41] | 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 |
---|