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