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