FSystem 77.1 KB
Newer Older
Neil Turton's avatar
Neil Turton committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
; Copyright 1996 Acorn Computers Ltd
;
; Licensed under the Apache License, Version 2.0 (the "License");
; you may not use this file except in compliance with the License.
; You may obtain a copy of the License at
;
;     http://www.apache.org/licenses/LICENSE-2.0
;
; Unless required by applicable law or agreed to in writing, software
; distributed under the License is distributed on an "AS IS" BASIS,
; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
; See the License for the specific language governing permissions and
; limitations under the License.
;
; > FSystem (filing system interface, stream handling)



; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

                MakeErrorBlock DeviceFS_BadOp
                MakeErrorBlock DeviceFS_Escape
                MakeErrorBlock DeviceFS_CannotDetach
Kevin Bracey's avatar
Kevin Bracey committed
24
		MakeErrorBlock DeviceFS_BadIOCtlReasonCode
Neil Turton's avatar
Neil Turton committed
25 26 27

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

28 29
flush		SETD	false	;debug for file flushing
close		SETD	false	;sets the close debug flag, somehow not visable from version
Neil Turton's avatar
Neil Turton committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
; perform an escape check, set r0 -> error block if escape is pressed.
;

checkescape     ENTRY
 [ Use0PageForESC
                MOV     lr, #0
                LDRB    lr, [lr, #ESC_Status]
                TST     lr, #&40                                ; if ESC flag clear
                EXIT    EQ                                      ; then exit
 |
                SWI     XOS_ReadEscapeState                     ; C set if escape held
                EXIT    VS                                      ; exit if an error
                EXIT    CC                                      ; or if no escape
 ]

Kevin Bracey's avatar
Kevin Bracey committed
48
		Debug	fs, "escape read in DeviceFS!"
Neil Turton's avatar
Neil Turton committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
                Push    "r1,r2"
                MOV     r0, #&7E                                ; acknowledge escape
                SWI     XOS_Byte
                Pull    "r1,r2"

                ADR     r0, ErrorBlock_DeviceFS_Escape          ; so set r0 -> error block
                PullEnv
                DoError                                         ; translate error and return V set

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++




; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
; StartFSystem, this simply registers the filing system with FileSwitch, no
; errors are reported and no global states are setup.
;

StartFSystem    ENTRY   "r0-r3"
                MOV     r0, #FSControl_AddFS
                ADRL    r1, module_start                        ; -> base of module
                LDR     r2, =fs_control -module_start           ;  = offset of block from module base
                MOV     r3, wp                                  ; -> workspace
                SWI     XOS_FSControl
                EXITS

                LTORG

; control block used by the module to register itself with FileSwitch as a suitable
; filing system.

Kevin Bracey's avatar
Kevin Bracey committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
fs_control	&		fs_name -module_start		; -> fs name, eg: ADFS
		&		fs_banner -module_start		; -> fs startup banner, eg. Acorn ADFS
		&		fs_open -module_start		; -> fs open
		&		fs_get -module_start		; -> fs get bytes
		&		fs_put -module_start		; -> fs put bytes
		&		fs_args -module_start		; -> fs args
		&		fs_close -module_start		; -> fs close
		&		fs_file -module_start		; -> fs file
fs_infoword	&		fsinfo_special+ fsinfo_flushnotify+ fsinfo_dontusesave+ fsinfo_extrainfo+ fsnumber_DeviceFS
		&		fs_func -module_start		; -> fs func
		[		false
		=		0
		|
		&		fs_gbpb -module_start           ; -> gbpb routine
		]
		&		fsextra_IOCtl
Neil Turton's avatar
Neil Turton committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181

fs_badop
                ADR     r0, ErrorBlock_DeviceFS_BadOp
 [ international
                B       MakeErrorWithFS_name                    ; "Bad operation on filing system devices:"
 |
                ORRS    pc, lr, #V_bit
 ]

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++




; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
; This code will remove the filing system from FileSwitch.  The code simply
; removes it and returns no errors if it went wrong.
;

RemoveFSystem   ENTRY   "r0-r1"
                MOV     r0, #FSControl_RemoveFS
                ADRL    r1, fs_name                             ; -> fs name to be removed
                SWI     XOS_FSControl
                EXITS

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++




; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
; call: IssueUpCall
;
; in:   r0  = reason code for up call
;       dr -> device record being modified
;
; out:  -
;
; This call issues an UpCall to the outside world.  The routine will
; call the UpCallV to inform tasks such as filer that the directory
; structure has changed.
;

IssueUpCall     ENTRY   "r0-r9"
                MOV     r9, r0                                  ;  = reason code
                LDR     r8, fs_infoword                         ;  = fsystem information word
                MOV     r6, #0                                  ;  = no special fields
                ADR     r1, null
                MOV     r0, #UpCall_ModifyingFile
                SWI     XOS_UpCall                              ; ignore any errors
                EXITS

null            = 0                                             ; no string so whole structure marked invalid!
                ALIGN

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++




; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
; fs_open
;
; This routine handles the opening of files (devices) within the system, this
; involves locating the parent device, seeing if the object can be opened, allocating
; a file block and then validating this within the linked list.
;
; When the block is created it contains the following parameters:
;
;  -> next pointer
;  <- back pointer
;   = validation word
;   = handle (0)
;
; The handle is not validated until the file has finally been opened and we are sure
; that nothing else can go wrong.
;

fs_open         ROUT

                Debug   open, "fsopen reason", r0
Kevin Bracey's avatar
Kevin Bracey committed
182
                Debug   fs, "fs_open ", r0
Neil Turton's avatar
Neil Turton committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268

                TEQ     r0, #fsopen_ReadOnly
                TEQNE   r0, #fsopen_Update
                BNE     fs_badop                                ; was it a valid reason code

                Push    "r0-pr, lr"

                MOV     fr, #0                                  ; setup the file record pointer

                BL      findobject                              ; setup other important registers
                CMP     r0, #object_subdevice                   ; if a subdevice, eg "$.Parallel.Fred"
                MOVEQ   r0, #object_file                        ; then treat as "$.Parallel" for now

                TEQ     r0, #object_file
                BNE     %80                                     ; don't open the file

                TEQ     pr, #0
                TEQNE   dr, #0
                BEQ     %80                                     ; not linked to any valid record and exit

                MOV     r0, #ModHandReason_Claim
                LDR     r3, =file_SIZE                          ; allocation size
                SWI     XOS_Module
                BVS     %90                                     ; if it goes wrong then deallocate blocks

                MOV     fr, r2                                  ; fr -> file record

                Debug   open, "file record at", fr

                Link    FilesAt, file, fr, r0                   ; link file record

                MOV     r0, #0                                  ; reset most words to zero
                STR     r0, [fr, #file_MadeBuffer]
                STR     r0, [fr, #file_Flags]
                STR     r0, [fr, #file_SpecialField]
                STR     r0, [fr, #file_InternalHandle]          ; magic value that allows the finalise to be issued
                STR     dr, [fr, #file_Device]
                STR     pr, [fr, #file_Parent]

                MOV     r0, #-1                                 ; and indicate that no pending data
                STR     r0, [fr, #file_RXTXWord]
                STR     r0, [fr, #file_BufferHandle]

                ADD     r0, sp, #CallerR0
                LDMIA   r0, {r0, r1}
                LDR     r3, [sp, #CallerR3]
                LDR     r6, [sp, #CallerR6]                     ; get special fields and parameters

                BL      HandleSpecial
                BVS     %90                                     ; attempt to decode special fields and return

                STR     r3, [fr, #file_FSwitchHandle]           ; I need a copy of the fileswitch handle

                Debug   open, "open type", r0
                DebugS  open, "path ", r1
                Debug   open, "file switch handle", r3

                TEQ     r0, #fsopen_ReadOnly                    ; despatch to relevant open handler
                BEQ     open_input
                B       open_output
70
                Debug   open, "opening has worked, info word", r0
                Debug   open, "my internal handle", r1
                Debug   open, "buffer size allocated", r2

                ADD     sp, sp, #CallerR5                       ; skip unimportant registers on frame
                Push    "r0-r4"

                MOV     r0, #DeviceCall_StreamCreated
                LDR     r1, [fr, #file_Parent]                  ; handle to call is the parents
                LDR     r2, [fr, #file_InternalHandle]          ; giving it the internal handle
                LDR     r3, [fr, #file_BufferHandle]
                BL      CallDevice
                BVS     %FT90

                LDR     r2, [fr, #file_Flags]
                TST     r2, #ff_FileForTX                       ; is it for RX or TX?
                MOVEQ   r2, #0
                MOVNE   r2, #-1

                MOV     r0, #UpCall_StreamCreated
                MOV     r1, pr                                  ; -> parent record
                LDR     r3, [fr, #file_FSwitchHandle]
                LDR     r4, [fr, #file_InternalHandle]
                SWI     XOS_UpCall

Kevin Bracey's avatar
Kevin Bracey committed
269 270 271 272 273 274 275 276
		[	false
		; Tell FileSwitch that this is not a buffered device, so it doesn't call fs_gbpb.
		LDR	r0, [sp]
		BIC	r0, r0, #1 :SHL: 28
		STR	r0, [sp]
		Debug	fs, "fs file info word ", r0
		]

Neil Turton's avatar
Neil Turton committed
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
                Pull    "r0-pr, pc",VC,^                        ; return as the file object has been created OK
90
                Debug   open, "failing to open, returning error"

                TEQ     fr, #0
                BLNE    removefileblock                         ; remove the file node keeping r0 intact!

                ADD     sp, sp, #4
                Pull    "r1-pr, lr"
                ORRS    pc, lr, #V_bit


80
                Debug   open, "failing to open, returning a null handle"

                TEQ     fr, #0
                BLNE    removefileblock
                Pull    "r0-pr, lr"
                MOV     r1, #0
                MOVS    pc, lr                                  ; remove file node and return duff handle


; attempt to create a stream for input, this involves creating any buffers required and
; stuff like that.
;

open_input
                LDR     r5, [pr, #parent_Flags]
                TST     r5, #pf_FullDuplex                      ; is the device full duplex
                BNE     %FT05                                   ; if so, then skip

                LDR     r5, [pr, #parent_UsedOutputs]           ; not full duplex, so if any outputs in use
                TEQ     r5, #0                                  ; we've got trouble
                BEQ     %FT05

                BL      RequestCloseOutputs                     ; try offering service to close r5 output streams
                BNE     %BT80                                   ; if failed then return zero handle

05
                LDR     r5, [pr, #parent_MaxInputs]
                LDR     r6, [pr, #parent_UsedInputs]

                Debug   open, "max RX streams",r5
                Debug   open, "used RX streams",r6

                CMP     r6, r5                                  ; are all the inputs used yet?
                BLT     %FT10                                   ; no, so OK to continue

                MOV     r5, #1                                  ; need to close one input
                BL      RequestCloseInputs
                BNE     %BT80                                   ; if failed then return zero handle

                LDR     r6, [pr, #parent_UsedInputs]            ; reload since it will have changed
10
                ADD     r6, r6, #1
                STR     r6, [pr, #parent_UsedInputs]            ; store a modified input counter

                MOV     r3, #ff_FileForRX :OR: ff_ModifiedCounters ; being used for RX
                STR     r3, [fr, #file_Flags]                   ; stash these flags

                Debug   open, "file flags", r3

                LDR     r6, [fr, #file_UserSpecialData]         ; -> decoded special field string
                MOV     r2, fr                                  ;  = internal handle
Neil Turton's avatar
Neil Turton committed
341
		LDR	r4, [fr, #file_FSwitchHandle]
Neil Turton's avatar
Neil Turton committed
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
                MOV     r1, pr
                MOV     r0, #DeviceCall_Initialise
                BL      CallDevice                              ; attempt to call the device
                STRVC   r2, [fr, #file_InternalHandle]
                BLVC    make_buffer                             ; make a usable buffer (if needed)
                BVS     %90                                     ; if failed then return error

                MOV     r1, fr                                  ; setup the new handle as required
                MOV     r2, #0                                  ; tell fileswitch it is un-buffered

                MOV     r0, #fsopen_ReadPermission
                LDR     r3, [fr, #file_BufferHandle]
                CMP     r3, #-1                                 ; is there a buffer attached to this file object?
                ORRNE   r0, r0, #fsopen_UnbufferedGBPB          ; yes, so mark as supporting unbuffered GBPB

                B       %70                                     ; it is valid so mark as so!



; attempt to create a stream for output, this involves creating any buffers required and
; stuff like that.
;

open_output
                LDR     r5, [pr, #parent_Flags]
                TST     r5, #pf_FullDuplex                      ; is the device full duplex?
                BNE     %FT15                                   ; if so, then skip

                LDR     r5, [pr, #parent_UsedInputs]            ; not full duplex, so if any inputs in use
                TEQ     r5, #0                                  ; we've got trouble
                BEQ     %FT15

                BL      RequestCloseInputs                      ; try offering service to close r5 input streams
                BNE     %BT80                                   ; if failed then return zero handle

15
                LDR     r5, [pr, #parent_MaxOutputs]
                LDR     r6, [pr, #parent_UsedOutputs]

                Debug   open, "max TX streams", r5
                Debug   open, "used TX streams", r6

                CMP     r6, r5                                  ; are all the outputs used yet?
                BLT     %FT20                                   ; no, so OK to continue

                MOV     r5, #1                                  ; need to close one output
                BL      RequestCloseOutputs
                BNE     %BT80                                   ; if failed then return zero handle

                LDR     r6, [pr, #parent_UsedOutputs]           ; reload since it will have changed
20
                ADD     r6, r6, #1
                STR     r6, [pr, #parent_UsedOutputs]           ; store a modified input counter

                MOV     r3, #ff_FileForTX :OR: ff_ModifiedCounters ; being used for TX
                STR     r3, [fr, #file_Flags]                   ; stash some important flags

                Debug   open, "file flags", r3

                LDR     r6, [fr, #file_UserSpecialData]         ; -> decoded special field string
Neil Turton's avatar
Neil Turton committed
402
		LDR	r4, [fr, #file_FSwitchHandle]
Neil Turton's avatar
Neil Turton committed
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
                MOV     r2, fr
                MOV     r1, pr
                MOV     r0, #DeviceCall_Initialise
                BL      CallDevice                              ; call the device
                STRVC   r2, [fr, #file_InternalHandle]
                BLVC    make_buffer                             ; make a usable buffer (if needed)
                BVS     %90                                     ; if that fails then attempt to create the buffer

                MOV     r1, fr                                  ; setup the new handle as required
                MOV     r2, #0                                  ; tell fileswitch it is un-buffered

                MOV     r0, #fsopen_WritePermission
                LDR     r3, [fr, #file_BufferHandle]
                CMP     r3, #-1                                 ; is there a buffer attached to this file object?
                ORRNE   r0, r0, #fsopen_UnbufferedGBPB          ; yes, so mark as supporting unbuffered GBPB

                B       %70                                     ; it is valid so mark as so!



; now the fun really starts, we must attempt to create a buffer for the buffered
; devices and ignore it if this is not possible.  The routine reads the
; information about the buffer that is going to be created, if one does
; not exist then it will create one, if one does exist then we attempt to
; use that.
;

make_buffer     ROUT

                Push    "r3, lr"

                LDR     r0, [dr, #device_Flags]
                TST     r0, #df_BufferedDevice                  ; is it a buffered device?
                Pull    "r3, pc", EQ, ^                         ; no, so we cannot even consider creating a buffer

                Debug   open, "device must be buffered"

                LDR     r0, [fr, #file_Flags]
                TST     r0, #ff_FileForTX                       ; is it a RX or TX buffer?
                MOVEQ   r0, #DeviceCall_CreateBufferRX
                ADDEQ   r3, dr, #device_RXBufferFlags
                MOVNE   r0, #DeviceCall_CreateBufferTX
                ADDNE   r3, dr, #device_TXBufferFlags
                LDMIA   r3, {r3, r4}                            ; get the buffer flags + size

                Debug   open, "suggested buffer flags", r3
                Debug   open, "suggested buffer size", r4

                MOV     r1, pr
                LDR     r2, [fr, #file_InternalHandle]          ; giving it the internal handle
                LDR     r5, [fr, #file_BufferHandle]
                MOV     r6, #-1                                 ; no threshold at the moment
                BL      CallDevice
                Pull    "r3, pc", VS                            ; it went wrong so give up now buster

                Debug   open, "modified flags for buffer", r3
                Debug   open, "modified buffer size", r4
                Debug   open, "first buffer handle", r5
                Debug   open, "modified threshold value", r6

                MOV     r0, r5                                  ; r0 = buffer handle

 [ FastBufferMan
05
                SWI     XBuffer_InternalInfo                    ; want buffer managers private buffer id
                STRVC   r0, [fr, #file_BufferPrivId]            ; store private buffer id
                STRVC   r1, BuffManService                      ; keep service routine addr up to date
                STRVC   r2, BuffManWkSpace                      ; keep buffer manager workspace ptr up to date
                BVC     %FT10

                Debug   open, "buffer #1 does not already exist"

                Push    "r3-r5"
                Pull    "r0-r2"                                 ; want flags, size and handle in r0-r2
                SWI     XBuffer_Create                          ; for buffer create
                Pull    "r3, pc", VS

                STR     pc, [fr, #file_MadeBuffer]              ; <>0 if buffer was made
                MOV     r5, r0                                  ; save buffer handle in r5
                B       %BT05
10
                MOV     r0, r5                                  ; move buffer handle back into r0
 |
                Push    "r3-r6"
                SWI     XBuffer_GetInfo                         ; does it exist already?
                Pull    "r0-r2, r6"                             ; restore registers no matter what
                MOVVC   r0, r2                                  ; if already exists then make r0 = buffer handle
                BVC     %10                                     ; it does so don't create the doofer

                Debug   open, "buffer #1 does not already exist"

                SWI     XBuffer_Create                          ; attempt to create the first buffer
                Pull    "r3, pc", VS                            ; and if we can't (ie. error) then give up now

                STR     pc, [fr, #file_MadeBuffer]              ; <>0 if buffer was made
10
 ]

                Debug   open, "handle of the first buffer", r0

                STR     r0, [fr, #file_BufferHandle]

                LDR     r1, [fr, #file_Flags]
Neil Turton's avatar
Neil Turton committed
506 507 508 509 510
 [ wakeup_data_present
		TST	r1, #ff_FileForTX
		ADRNE	r1, wakeup			; normal tx wakeup
		ADREQ	r1, wakeup_rx			; special rx wakeup
 |
Neil Turton's avatar
Neil Turton committed
511 512
                ANDS    r1, r1, #ff_FileForTX                   ; if an output device (r1<>0)
                ADRNE   r1, wakeup                              ; then use real wake up code else use none
Neil Turton's avatar
Neil Turton committed
513
 ]
Neil Turton's avatar
Neil Turton committed
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
                ADR     r2, detach                              ; try to close stream if someone tries to detach
                MOV     r3, fr
                MOV     r4, wp                                  ; workspace + private word

                Debug   open, "attempting to link to buffer", r0
                Debug   open, "wake up routine at", r1
                Debug   open, "detach routine at", r2
                Debug   open, "file record", fr
                Debug   open, "workspace at", wp

                SWI     XBuffer_LinkDevice                      ; attempt to link the device to the buffer
                Pull    "r3, pc", VS                            ; return if went wrong!!!

                LDR     r1, [fr, #file_Flags]                   ; set flag that indicates
                ORR     r1, r1, #ff_DeviceLinked                ; device is linked
                STR     r1, [fr, #file_Flags]

                Debug   open, "handle", r0
                Debug   open, "threshold about to be set to", r6

                MOVS    r1, r6                                  ; if -VE then don't set threshold
                SWIPL   XBuffer_Threshold

                Pull    "r3, pc"                                ; return VS, VC from the previous call, r0 correct if needed


; wake up code, this is called when some data is inserted into a dormant TX buffer.
; after calling device, r0 = 0 if the device wishes to remain dormant, otherwise preserved

wakeup          ENTRY   "r0-r3"
                LDR     r2, [r8, #file_InternalHandle]          ; if device being opened or closed
                TEQ     r2, #0
                EXITS   EQ                                      ; then ignore
                MOV     r0, #DeviceCall_WakeUpTX
                LDR     r1, [r8, #file_Parent]
                BL      CallDevice                              ; attempt to wake up device
                EXITS   VS                                      ; if we got an error, go no further
                TEQ     r0, #DeviceCall_WakeUpTX                ; if device wishes to become active
                EXITS   EQ                                      ; then do nothing, buffer manager assumes this
                LDR     r0, [r8, #file_BufferHandle]
                MOV     r1, #0
                MOV     r2, #:NOT: BufferFlags_NotDormant       ; indicate dormant
                SWI     XBuffer_ModifyFlags
                EXITS

Neil Turton's avatar
Neil Turton committed
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
 [ wakeup_data_present
; wake up code, this is called when some data is inserted into a dormant RX buffer.
wakeup_rx	ENTRY	"r0-r2"

		MOV	r2, pc
		ORR	r0, r2, #SVC_mode
		TEQP	r0, #0
		NOP

		Push	"lr"
		MOV	r0, #UpCall_DeviceRxDataPresent
                LDR     r1, [r8, #file_FSwitchHandle]
		SWI	XOS_UpCall
		Pull	"lr"

		TEQP	r2, #0
		NOP

		EXITS
 ]
Neil Turton's avatar
Neil Turton committed
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
; detach code, this is called when someone tries to deregister the buffer, or link it some other device

detach          ENTRY   "r0-r2"
                MOV     r1, #Service_DeviceFSCloseRequest
                LDR     r2, [r8, #file_FSwitchHandle]           ; get file handle
                SWI     XOS_ServiceCall
                TEQ     r1, #Service_Serviced                   ; if someone responded
                PullEnv
                BICEQS  pc, lr, #V_bit                          ; then exit VC
                ADRL    r0, ErrorBlock_DeviceFS_CannotDetach    ; else return error
                DoError

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
;       RequestCloseInputs - Issue services to request the closing of a number of input streams
;       RequestCloseOutputs - Ditto for output streams
;
; in:   r5 = number of streams that need closing
;       pr -> parent record of device that we want to free streams on
;       fr -> file record we're currently working on, so ignore that one
;
; out:  EQ => succeeded in closing enough
;       NE => failed to close enough

RequestCloseInputs ENTRY "r0-r3,r5"

        Debug   open, "RequestCloseInputs called"

        MOV     r3, #ff_FileForRX
        B       RequestCloseStreams

RequestCloseOutputs ALTENTRY

        Debug   open, "RequestCloseOutputs called"

        MOV     r3, #ff_FileForTX
RequestCloseStreams

        Debug   open, "Number of streams to close", r5

        LDR     r0, FilesAt
10
        TEQ     r0, #0                          ; if at end of files list
        BEQ     %FT90                           ; then no more files to try

        TEQ     r0, fr                          ; if it's the one we're building
        LDREQ   r0, [r0, #file_Next]            ; then ignore it
        BEQ     %BT10

        LDR     lr, [r0, #file_Parent]
        TEQ     lr, pr                          ; if the right device

        LDREQ   lr, [r0, #file_Flags]
        ANDEQ   lr, lr, #ff_FileInputOutput
        TEQEQ   lr, r3                          ; then check for right input/output type

        LDREQ   r2, [r0, #file_FSwitchHandle]   ; if OK then load fileswitch handle
        LDR     r0, [r0, #file_Next]            ; always move onto next file (as current file may close under our feet)
        BNE     %BT10                           ; if no good then goto next file

        Debug   open, "Issuing service to close handle", r2

        MOV     r1, #Service_DeviceFSCloseRequest
        SWI     XOS_ServiceCall
        TEQ     r1, #Service_Serviced           ; if that file was closed for us
        SUBEQS  r5, r5, #1                      ; then one less file to close
        BNE     %BT10                           ; if no response for that file, or still more to do, then loop
90
        TEQ     r5, #0                          ; set EQ if successfully closed the required number of files
        EXIT


; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++




; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
; fs_get
;
; Get a single byte from the filing system, this involves waking the device,
; seeing if the device is buffered and reading the non-buffering word
; or extracting a character from the buffer.
;

fs_get          ENTRY   "r0-r2, fr,pr"

Kevin Bracey's avatar
Kevin Bracey committed
667 668
		Debug	fs, "fs_get"

Neil Turton's avatar
Neil Turton committed
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
                MOV     fr, r1                                  ; -> file record
                LDR     pr, [fr, #file_Parent]

                MOV     r0, #DeviceCall_WakeUpRX
                MOV     r1, pr
                LDR     r2, [fr, #file_InternalHandle]
                BL      CallDevice                              ; ensure wake for calling
                BVS     %FT30                                   ; return an error

                B       %FT01


00
                BL      checkfileRXOK
                BVS     %FT30

                LDR     r1, [pr, #parent_Flags]
                TST     r1, #pf_MonitorEOF
                BEQ     %FT01

                MOV     r1, fr                                  ; -> file record
                BL      checkeof                                ; read the EOF status
                BCS     %FT40                                   ; give up if EOF

01
                BL      checkescape                             ; is escape pressed
                BVS     %FT30                                   ; yes then return the escape error

                LDR     r1, [fr, #file_BufferHandle]
                CMP     r1, #-1                                 ; is the device buffered?
                BNE     %FT10

                LDR     r0, [fr, #file_RXTXWord]
                CMP     r0, #-1                                 ; is there a pending word
                MOVCC   r1, #-1
                STRCC   r1, [fr, #file_RXTXWord]                ; yes, we have it so reset the word to a new state
                B       %FT20
10
 [ FastBufferMan
                MOV     r0, #BufferReason_RemoveByte
                LDR     r1, [fr, #file_BufferPrivId]            ; get buffer manager private buffer id
                CallBuffMan
                MOVCC   r0, r2
 |
                CLRV                                            ; ensure V clear to remove character
                Push    "r1-r2, r9"
                MOV     r9, #REMV
                SWI     XOS_CallAVector                         ; attempt to remove the byte
                Pull    "r1-r2, r9"                             ; preserve important registers
 ]
                BVS     %FT30                                   ; return any errors
20
                BCS     %BT00                                   ; loop again to make sure actually received data

                STR     r0, [sp, #CallerR0]                     ; write to return frame

                BL      %FT80                                   ; SleepRX - ignore error returned

                PullEnv
                BICS    pc, lr, #C_bit+ V_bit                   ; ensure C clear on return
30
                STR     r0, [sp, #CallerR0]                     ; ensure error pointer on return frame valid

                BL      %FT80                                   ; SleepRX - ignore error returned

                PullEnv
                ORRS    pc, lr, #V_bit                          ; return the error

40
                BL      %FT80

                PullEnv
                ORR     lr, lr, #C_bit                          ; CS return - EOF encountered
                BICS    pc, lr, #V_bit


80              ; SleepRX - corrupts r0, r1 and r2
                MOV     r0, #DeviceCall_SleepRX
                MOV     r1, pr
                LDR     r2, [fr, #file_InternalHandle]
                B       CallDevice



; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

; In
;  fr
; Out
; V and r0=error possible
checkfileRXOK   Entry   "r0-r2"
                MOV     r0, #DeviceCall_MonitorRX
                B       %FT10

checkfileTXOK   ALTENTRY
                MOV     r0, #DeviceCall_MonitorTX
10
                LDR     r1, [fr, #file_Parent]
                LDR     r2, [r1, #parent_Flags]
                TST     r2, #ParentFlag_MonitorTransfers
Kevin Bracey's avatar
Kevin Bracey committed
769 770 771
		DebugIf	NE, fs, "monitor calls supported"
		DebugIf	EQ, fs, "monitor calls not supported"
		EXIT	EQ
Neil Turton's avatar
Neil Turton committed
772

Kevin Bracey's avatar
Kevin Bracey committed
773 774 775 776 777
                LDR	r2, [fr, #file_InternalHandle]
                BL	CallDevice		;ensure wake for calling
		STRVS   r0, [sp]		;logic changed to be independent of flags on entry
						;JRC Tue 29th April 1997
                EXIT
Neil Turton's avatar
Neil Turton committed
778 779 780 781 782 783 784 785 786

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
; fs_put
;
; Put a byte to a device, this involves the following.  Inserting some
; data into a buffer (if applicable), writing the RXTX word with this
; piece of data and then waking the device up.

Kevin Bracey's avatar
Kevin Bracey committed
787
fs_put		ENTRY	"r0-r2, fr, r9"
Neil Turton's avatar
Neil Turton committed
788

Kevin Bracey's avatar
Kevin Bracey committed
789
		Debug	fs, "fs_put"
Neil Turton's avatar
Neil Turton committed
790

Kevin Bracey's avatar
Kevin Bracey committed
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
 [ debug
		Push	"r0-r3"

		;Check page 0 escape flag
                MOV	r0, #0
                LDRB	r0, [r0, #ESC_Status]
		BIC	r0, r0, #&CF
		Debug	fs, "ESC_Status", r0

		;Check SWI version of escape flag
		SWI	XOS_ReadEscapeState
		MOV	r0, #:NOT: C_bit
		BIC	r0, pc, r0
		Debug	fs, "ReadEscapeState", r0

		MOV	r0, #9 ;escape
		MOV	r1, #0
		MOV	r2, #0
		MOV	r3, #0
		SWI	XOS_ChangeEnvironment
		Debug	fs, "esc handler", r1

		MOV	r0, #10 ;event
		MOV	r1, #0
		MOV	r2, #0
		MOV	r3, #0
		SWI	XOS_ChangeEnvironment
		Debug	fs, "event handler", r1

		Pull	"r0-r3"
 ]

		MOV	fr, r1					; -> file record

		LDR	r1, [fr, #file_BufferHandle]
		CMP	r1, #-1					; is the object buffered?
Neil Turton's avatar
Neil Turton committed
827
 [ FastBufferMan
Kevin Bracey's avatar
Kevin Bracey committed
828 829
		MOVNE	r2, r0					; need byte in r2
		LDRNE	r1, [fr, #file_BufferPrivId]		; get buffer managers private buffer id
Neil Turton's avatar
Neil Turton committed
830
 |
Kevin Bracey's avatar
Kevin Bracey committed
831
		MOVNE	r9, #INSV				; saves an extra label
Neil Turton's avatar
Neil Turton committed
832
 ]
Kevin Bracey's avatar
Kevin Bracey committed
833 834 835
		BNE	%10

		; Non-buffered device
Neil Turton's avatar
Neil Turton committed
836
00
Kevin Bracey's avatar
Kevin Bracey committed
837 838
		BL	checkescape				; is an escape pending?
		BVS	%99
Neil Turton's avatar
Neil Turton committed
839

Kevin Bracey's avatar
Kevin Bracey committed
840 841
		BL	checkfileTXOK
		BVS	%99
Neil Turton's avatar
Neil Turton committed
842

Kevin Bracey's avatar
Kevin Bracey committed
843 844 845
		LDR	r1, [fr, #file_RXTXWord]
		CMP	r1, #-1					; is the RX/TX word free yet?
		BNE	%00					; no, so loop again
Neil Turton's avatar
Neil Turton committed
846

Kevin Bracey's avatar
Kevin Bracey committed
847
		STR	r0, [fr, #file_RXTXWord]		; yes, so write the word in now
Neil Turton's avatar
Neil Turton committed
848

Kevin Bracey's avatar
Kevin Bracey committed
849 850 851 852 853 854 855
		MOV	r0, #DeviceCall_WakeUpTX
		LDR	r1, [fr, #file_Parent]
		LDR	r2, [fr, #file_InternalHandle]
		BL	CallDevice				; wake up for transmit now
		B	%99

		; Buffered device
Neil Turton's avatar
Neil Turton committed
856
10
Kevin Bracey's avatar
Kevin Bracey committed
857 858 859 860 861 862 863
		BL	checkescape				; is an escape pending
		BLVS	purgeoutputbuffer			; if so then purge output buffer
		BVS	%99

		BL	checkfileTXOK
		BVS	%99

Neil Turton's avatar
Neil Turton committed
864
 [ FastBufferMan
Kevin Bracey's avatar
Kevin Bracey committed
865 866
		MOV	r0, #BufferReason_InsertByte
		CallBuffMan					; r1,r2 already set up
Neil Turton's avatar
Neil Turton committed
867
 |
Kevin Bracey's avatar
Kevin Bracey committed
868
		SWI	XOS_CallAVector
Neil Turton's avatar
Neil Turton committed
869
 ]
Kevin Bracey's avatar
Kevin Bracey committed
870 871
		BVS	%99					; return any errors that may occur
		BCS	%10					; keep looping until inserted
Neil Turton's avatar
Neil Turton committed
872

Kevin Bracey's avatar
Kevin Bracey committed
873 874 875 876 877
		CLRV
99		STRVS	r0, [sp]
		PullEnv
		BICVCS	pc, lr, #V_bit
		ORRS	pc, lr, #V_bit
Neil Turton's avatar
Neil Turton committed
878 879 880 881 882 883 884 885 886 887

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
; fs_args
;
; Handle the fs_args entry, this is used for streams to read EOF, flush streams
; etc.
;

fs_args         ROUT
Kevin Bracey's avatar
Kevin Bracey committed
888 889 890

		Debug	fs, "fs_args", r0

Neil Turton's avatar
Neil Turton committed
891 892 893 894 895 896 897 898
		CMP	r3, #3
		BNE	%1
		Push	"r0-r1"
		MOV	r0, #&3500000
		LDR	r1, =&40ff0000
		STR	r1, [r0]
		Pull	"r0-r1"
1
Neil Turton's avatar
Neil Turton committed
899 900 901 902 903 904 905 906
                Debug   args, "fs_args called with reason code", r0

                TEQ     r0, #fsargs_SetEXT                      ; ignore SetEXT
                MOVEQS  pc, lr

                TEQ     r0, #fsargs_SetPTR
                TEQNE   r0, #fsargs_EnsureSize
                TEQNE   r0, #fsargs_WriteZeroes
Neil Turton's avatar
Neil Turton committed
907
                BEQ     fs_badop                ; these ones give nice errors
Neil Turton's avatar
Neil Turton committed
908 909 910

                TEQ     r0, #fsargs_ReadPTR
                TEQNE   r0, #fsargs_ReadSize
Neil Turton's avatar
Neil Turton committed
911
                MOVEQ   r2, #0			; return 0
Neil Turton's avatar
Neil Turton committed
912 913
                MOVEQS  pc, lr

Neil Turton's avatar
Neil Turton committed
914 915 916 917 918 919
                TEQ	r0, #fsargs_ReadEXT
		BEQ	args_ext

		TEQ	r0, #fsargs_IOCtl
		BEQ	args_ioctl

Neil Turton's avatar
Neil Turton committed
920 921 922
                TEQ     r0, #fsargs_ReadLoadExec
                BEQ     args_loadexec

Neil Turton's avatar
Neil Turton committed
923
                TEQ     r0, #fsargs_Flush               ; is it a flush?
Neil Turton's avatar
Neil Turton committed
924 925
                BEQ     args_flush

Neil Turton's avatar
Neil Turton committed
926 927
                TEQ     r0, #fsargs_EOFCheck            ; is it a eof read?
                BEQ     args_eof                        ; then do it
Neil Turton's avatar
Neil Turton committed
928 929 930

                B       fs_badop

Neil Turton's avatar
Neil Turton committed
931 932
; handle ioctl's

Kevin Bracey's avatar
Kevin Bracey committed
933 934 935 936 937 938 939 940 941 942
args_ioctl	ROUT
		ENTRY	"r0-r3, fr,pr"

		LDR	r0, [r2, #0]			; load ioctl reason codes and flags
		BIC	r0, r0, #&ff00ffff
		TEQ	r0, #&00ff0000
		BLEQ	ioctl_miscop
		BLEQ	%10				; only V flag corrupted so OK

; catch remaining cases and pass into device driver
Neil Turton's avatar
Neil Turton committed
943 944 945 946 947 948 949

                MOV     fr, r1                     	; -> file record
		LDR	pr, [fr, #file_Parent]

                MOV     r0, #DeviceCall_IOCtl
                MOV     r1, pr
		MOV	r3, r2				; shift r2 up a reg
950
                LDR     r2, [fr, #file_InternalHandle]
Neil Turton's avatar
Neil Turton committed
951 952
                BL      CallDevice

Kevin Bracey's avatar
Kevin Bracey committed
953
10
Neil Turton's avatar
Neil Turton committed
954 955 956 957 958 959
                STRVS   r0, [sp]
                PullEnv VS
                ORRVSS  pc, lr, #V_bit

		EXIT

Kevin Bracey's avatar
Kevin Bracey committed
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011

; deal with non-device specific ioctls

ioctl_miscop	ROUT

		LDR	r0, [r2, #0]			; load ioctl reason codes and flags

; mask off top 16 bits of r0 to obtain reason code
		MOV	r0, r0, LSL #16
		MOV	r0, r0, LSR #16

                CMP     r0, #(%20-%10)/4		; validate reason code
                ADDCC   pc, pc, r0, LSL #2 		; despatch
		B	%20
10
		MOVS	pc, lr				; 0 nothing
		B	ioctl_miscop_nonblock		; 1 set non-blocking I/O
20
                PullEnv
                BICEQS  pc, lr, #V_bit                  ; then exit VC
		ADRL	r0, ErrorBlock_DeviceFS_BadIOCtlReasonCode
                DoError


ioctl_miscop_nonblock
		ROUT
		LDR	r0, [r2, #0]			; load ioctl reason codes and flags

		MOV	fr, r1				; -> file record
		LDR	r1, [fr, #file_Flags]

		TST	r0, #&80000000			; test write flag
		BEQ	%10

		LDR	r3, [r2, #4]			; load ioctl data word
		CMP	r3, #0
		BICEQ	r1, r1, #ff_NonBlocking		; clear non-blocking I/O enabled flag
		CMP	r3, #1
		ORREQ	r1, r1, #ff_NonBlocking		; set non-blocking I/O enabled flag
		STR	r1, [fr, #file_Flags]		; write back flags
10
		TST	r0, #&40000000			; test read flag
		BEQ	%20

		TST	r1, #ff_NonBlocking		; test non-blocking I/O enabled flag
		MOVNE	r0, #1				; non-blocking I/O enabled
		MOVEQ	r0, #0				; non-blocking I/O disabled
		STR	r0, [r2, #4]			; store result in ioctl block
20
		MOVS	pc, lr


Neil Turton's avatar
Neil Turton committed
1012 1013 1014 1015 1016
; handle checking of the file extent

args_ext	ENTRY	"r0-r1,r3-r6, fr"

                MOV     fr, r1                     	; -> file record
Neil Turton's avatar
Neil Turton committed
1017

Neil Turton's avatar
Neil Turton committed
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
                LDR     r0, [fr, #file_BufferHandle]
                CMP     r0, #-1                         ; is the object buffered
                BEQ     %FT10				; look at rx/tx word

; is this a tx stream
		LDR	r0, [fr, #file_Flags]
		TST	r0, #ff_FileForTX
		BNE	%5

 [ FastBufferMan
                MOV     r0, #BufferReason_UsedSpace
                LDR     r1, [fr, #file_BufferPrivId]
                CallBuffMan
 |
                SWI     XBuffer_GetInfo
 ]
                STRVS   r0, [sp]
                PullEnv VS
                ORRVSS  pc, lr, #V_bit

 [ :LNOT: FastBufferMan
                MOV	r6, r2
 ]
		EXIT
5
; deal with tx stream
 [ FastBufferMan
                MOV     r0, #BufferReason_FreeSpace
                LDR     r1, [fr, #file_BufferPrivId]
                CallBuffMan
 |
                SWI     XBuffer_GetInfo
 ]
                STRVS   r0, [sp]
                PullEnv VS
                ORRVSS  pc, lr, #V_bit

 [ :LNOT: FastBufferMan
                MOV	r6, r2
 ]
		EXIT

10
; handle the case where none-buffered, just a word
                LDR     r0, [fr, #file_RXTXWord]
                CMP     r0, #-1                    	; RX/TX word full
		MOVEQ	r2, #0				; no
		MOVNE	r2, #1				; yes
; is this a tx stream
		LDR	r0, [fr, #file_Flags]
		TST	r0, #ff_FileForTX
		EXIT	EQ

		CMP	r2, #0
		MOVEQ	r2, #1
		MOVNE	r2, #0

		EXIT

; handle checking the EOF flag within the system.  Could return errors, assumes
; that r1 on entry contains the internal handle passed to and from fileswitch to
; the filing system.
Neil Turton's avatar
Neil Turton committed
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148

checkeof        ENTRY   "r2"                                    ; internal routine,
                BL      args_eof
                CMPVC   r2, #1                                  ; exits CS for EOF, else CC
                EXIT


args_eof        ENTRY   "r0,r1,r3-r6, fr"

                Debug   eof, "args_eof called"

                MOV     fr, r1                                  ; -> file record

                LDR     r0, [fr, #file_BufferHandle]
                CMP     r0, #-1                                 ; is the object buffered?
                BEQ     %FT10                                   ; no, so check the RX/TX word

 [ FastBufferMan
                MOV     r0, #BufferReason_UsedSpace
                LDR     r1, [fr, #file_BufferPrivId]            ; get buffer managers private buffer id
                CallBuffMan
 |
                SWI     XBuffer_GetInfo                         ; try to read information about this buffer
 ]

05
                STRVS   r0, [sp]
                PullEnv VS
                ORRVSS  pc, lr, #V_bit

 [ FastBufferMan
                TEQ     r2, #0                                  ; any chars in buffer?
 |
                TEQ     r6, #0                                  ; any chars in buffer?
 ]
                BNE     %FT25
                B       %FT20

10
                LDR     r0, [fr, #file_RXTXWord]
                CMP     r0, #-1                                 ; does the RX/TX word contain any data?
                BNE     %FT25
20
                MOV     r0, #DeviceCall_EndOfData
                LDR     r1, [fr, #file_Parent]
                LDR     r2, [fr, #file_InternalHandle]
                MOV     r3, #-1                                 ; pass -1 to get EOF if device does not modify
                BL      CallDevice
                BVS     %BT05                                   ; if it fails then returns error to outside world

                CMP     r3, #-1
25
 [ debug
                BEQ     %FT26
                Debug   eof, "Indicating not EOF"
                B       %FT27
26
                Debug   eof, "Indicating EOF"
27
 ]
                MOVEQ   r2, #-1                                 ; no data, indicate EOF
                MOVNE   r2, #0                                  ; otherwise indicate not EOF
                EXITS


; flush a stream.  Wait for the stream to empty, calling OS_ReadEscape state to allow
; quick exit from these operations.
;

1149
args_flush      ENTRY   "r1-r3,fr"
Neil Turton's avatar
Neil Turton committed
1150

1151
		Debug	flush, "Entered Flush"
Neil Turton's avatar
Neil Turton committed
1152 1153 1154
                LDR     r0, [r1, #file_Flags]
                TST     r0, #ff_FileForTX                       ; is the file an output object
                EXITS   EQ                                      ; no, so return now
1155
                MOV     fr, r1
Neil Turton's avatar
Neil Turton committed
1156
10
1157 1158 1159
                BL      checkeof                                ; CS = exit loop until EOF or Escape pressed
		DebugIf CS, flush, "Exited  Flush EOF"
                EXITS	CS					; normal exit eof found
Neil Turton's avatar
Neil Turton committed
1160

1161 1162
                BLVC    checkescape				; VS = exit not always enabled
                BLVC	checkfileTXOK				; VS = exit Monitor TX for errors call 12.
Neil Turton's avatar
Neil Turton committed
1163

1164 1165 1166 1167
		DebugE  flush, "Exited  Flush forced exit"
                PullEnv VS
                ORRVSS  pc, lr, #V_bit                          ; return the error (if one)
                B       %BT10                                   ; if C clear then still no end of data
Neil Turton's avatar
Neil Turton committed
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191

; get load and exec address of a stream and return them back to the caller.
;

args_loadexec   MOV     r2, #0
                MOV     r3, #0
                MOVS    pc, lr

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++




; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
; fs_close
;
; The code handles the closing of a file within the filing system, this involves
; unlinking the device from the buffer and then closing down the device.
;

fs_close        ENTRY   "dr, pr"

                Debug   close,"Closing file"
Kevin Bracey's avatar
Kevin Bracey committed
1192
                Debug   fs, "fs_close"
Neil Turton's avatar
Neil Turton committed
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316

                MOV     fr, r1                                  ; fr is the file handle to be used
                BL      removefileblock                         ; and then remove the block associated with it
                EXIT


removefileblock ENTRY   "r0-r5, dr, pr"                         ; attempt to zap record at 'FR'

                Debug   close,"removing file block"

                ADD     dr, fr, #file_Device
                LDMIA   dr, {dr, pr}                            ; get parent + device records

                LDR     r2, [fr, #file_InternalHandle]
                TEQ     r2, #0                                  ; was the device setup?
                BEQ     %FT10

                MOV     r0, #DeviceCall_Finalise
                MOV     r1, pr
                BL      CallDevice                              ; attempt to close down the device
                BVS     %FT90                                   ; if device refuses, then fail operation

                Debug   close,"device call succeeded"

                MOV     r0, #0
                STR     r0, [fr, #file_InternalHandle]          ; indicate we've successfully closed stream
10
                LDR     r0, [fr, #file_BufferHandle]
                CMP     r0, #-1                                 ; is a buffer allocated
                BEQ     %FT20                                   ; no, so skip

                LDR     r1, [fr, #file_Flags]                   ; did we link the device to the buffer?
                TST     r1, #ff_DeviceLinked
                BEQ     %FT12                                   ; no, so skip

                MOV     r1, r0                                  ; save buffer handle, in case of error
                SWI     XBuffer_UnlinkDevice                    ; unlink device from buffer
                MOV     r0, r1                                  ; restore buffer handle

                Debug   close,"unlinked buffer from device"
12
                LDR     r1, [fr, #file_MadeBuffer]
                TEQ     r1, #0                                  ; and if it is did we create the buffer
                BEQ     %FT15                                   ; no, so attempt to skip the removal

                SWI     XBuffer_Remove                          ; remove buffer, but ignore errors
                                                                ; (we've started closing so we'll finish!)

                Debug   close,"removed the buffer"

                MOV     r0, #0
                STR     r0, [fr, #file_MadeBuffer]
15
                MOV     r0, #-1
                STR     r0, [fr, #file_BufferHandle]            ; mark as buffer no longer important
 [ FastBufferMan
                STR     r0, [fr, #file_BufferPrivId]            ; nullify buffer manager private buffer id
 ]
20
                LDR     r2, [fr, #file_SpecialField]
                TEQ     r2, #0                                  ; is there an allocation for special field decoding?
                BEQ     %FT30

                MOV     r0, #ModHandReason_Free                 ; attempt to release it
                SWI     XOS_Module                              ; (again ignore errors, cos it's too late)

                MOV     r0, #0
                STR     r0, [fr, #file_SpecialField]            ; mark as now released incase called again
30
                LDR     r5, [fr, #file_Flags]                   ; get global flags word
                TST     r5, #ff_FileForTX                       ; is it for RX or TX?
                MOVEQ   r2, #0
                MOVNE   r2, #-1

                MOV     r0, #UpCall_StreamClosed
                MOV     r1, pr                                  ; -> parent record
                LDR     r3, [fr, #file_FSwitchHandle]
                LDR     r4, [fr, #file_InternalHandle]
                SWI     XOS_UpCall                              ; broadcast to the UpCallV

                Debug   close,"broadcast UpCall_StreamClosed"

                Unlink  FilesAt, file, fr, r3, r4               ; unlink and free the file object (ignore errors)

                TST     r5, #ff_FileForTX                       ; is it a TX file
                ADDEQ   r0, pr, #parent_UsedInputs
                ADDNE   r0, pr, #parent_UsedOutputs

                TST     r5, #ff_ModifiedCounters                ; had we actually modified the counters?
                LDRNE   r1, [r0]
                SUBNE   r1, r1, #1
                STRNE   r1, [r0]                                ; update the used inputs/outputs counters

                LDR     fr, =&DEADDEAD                          ; invalidate the file record pointer

                EXITS

; external error

90
                Debug   close,"error removing file block"

                STR     r0, [sp]                                ; return an error to the outside world
                PullEnv
                ORRS    pc, lr, #V_bit

                LTORG

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++




; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
; fs_file
;
; This is used for reading information about devices and other data, only
; a couple of objects are supported within device fs.
;

fs_file         ROUT

                Debug   file, "fs_file called with reason code", r0
Kevin Bracey's avatar
Kevin Bracey committed
1317
                Debug   fs, "fs_file ", r0
Neil Turton's avatar
Neil Turton committed
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351

                TEQ     r0, #fsfile_WriteInfo
                TEQNE   r0, #fsfile_WriteLoad
                TEQNE   r0, #fsfile_WriteExec
                TEQNE   r0, #fsfile_WriteAttr
                TEQNE   r0, #fsfile_Create
                MOVEQS  pc, lr                                  ; ignore all these

                TEQ     r0, #fsfile_ReadInfo                    ; read info on a file?
                BNE     fs_badop                                ; no, then can't do any others, so error

; fsfile_ReadInfo

                Push    "dr, pr, lr"                            ; save dr, pr round call
                BL      findobject
                CMP     r0, #object_subdevice                   ; if a sub-device it doesn't exist
                MOVEQ   r0, #object_nothing                     ; for the purposes of OS_File 5
                Pull    "dr, pr, pc"

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++




; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
; fs_func.
;
; Handle fs func operations within DeviceFS.
;

fs_func         ROUT

                Debug   func, "fs_func called with reason code", r0
Kevin Bracey's avatar
Kevin Bracey committed
1352
                Debug   fs, "fs_func ", r0
Neil Turton's avatar
Neil Turton committed
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636

                TEQ     r0, #fsfunc_ReadDirEntries              ; if directory enumeration then allow
                TEQNE   r0, #fsfunc_ReadDirEntriesInfo
                TEQNE   r0, #fsfunc_CatalogObjects
                BEQ     file_enumdir

                TEQ     r0, #fsfunc_Dir                         ; fault the following....
                TEQNE   r0, #fsfunc_Lib
                TEQNE   r0, #fsfunc_Cat
                TEQNE   r0, #fsfunc_Ex
                TEQNE   r0, #fsfunc_LCat
                TEQNE   r0, #fsfunc_LEx
                TEQNE   r0, #fsfunc_Info
                TEQNE   r0, #fsfunc_Opt
                TEQNE   r0, #fsfunc_Rename
                TEQNE   r0, #fsfunc_Access
                TEQNE   r0, #fsfunc_ReadDiscName
                TEQNE   r0, #fsfunc_ReadCSDName
                TEQNE   r0, #fsfunc_ReadLIBName
                TEQNE   r0, #fsfunc_SetContexts
                TEQNE   r0, #fsfunc_FileInfo
                BEQ     fs_badop

                MOVS    pc, lr                                  ; others such as boot, shutdown are ignored


; Directory enumeration, this is quite a complex operation.  Firstly
; we need to match all objects the meet the specified path.  The handle
; returned for reading the next entry contains the counter for skipping
; entries that have been processed.
;
; The routine works like this:
;
; It locates an object that matches the specified path, if it has already
; seen this object (ie. skip counter is still +ve) then it will skip it
; and update the skip counter (subtract 1), this continues until the skip
; counter is equal to zero at which point data starts being returned.
;
; We then have an entry, we see if this record will fit into the specified
; buffer, if not then we return its handle (the skip pointer to get to this
; object).  When an item is written we then carry on scanning until the next
; object picked from the tree has a different leaf name to the one we last wrote,
; if this does not fit then we will return, and so on....
;
; Reading all objects beginning "$."
;
; Index         Name                    Written         Reason
; 0             $.aardvark              yes             unique
; 1             $.parallel.device       yes             unique
; 2             $.parallel.zoot         no              same path at top level
; 3             $.quick                 yes             unique
;

; in:   r0 = 14, 15 or 19
;       r1 -> directory name (not wildcarded any more)
;       r2 -> buffer for data
;       r3 = number of object names to read
;       r4 = index of first item to read in directory (0 => start of directory)
;       r5 = buffer length
;       r6 -> special field if present, else 0
;
; out:  r3 = number of names read
;       r4 = index of next item to read (-1 if you were at end and didn't return any this call ie r3=0)


file_enumdir    ENTRY   "r0-r2, r5-r7, dr, pr"

                TEQ     r3, #0                                  ; if asking for none
                EXITS   EQ                                      ; then return straight away

                MOV     r2, r1                                  ; -> path to enumerate
                MOV     r1, #0                                  ; and broadcast it to all devices
                MOV     r0, #DeviceCall_EnumDir                 ; about to enumerate directory
                BL      CallDevice

                LDMIA   sp, {r0-r2}                             ; pull parameters (as CallDevice corrupts the important ones!)

                ADD     r5, r5, r2                              ; setup buffer end pointer

                DebugS  enumdir, "directory to enum ", r1
                Debug   enumdir, "record count", r3
                Debug   enumdir, "to", r2
                Debug   enumdir, "until", r5
                Debug   enumdir, "offset", r4
                DebugS  enumdir, "special field ", r6

                BL      skipdollar                              ; skip "$" or "$."

                MOV     r7, #0                                  ; setup the skip counter

                LDR     dr, DevicesAt                           ; -> start of the devices list
00
                SUBS    lr, r7, r4                              ; number returned so far (-ve => still skipping)
                CMPCS   lr, r3                                  ; if we've returned all the requested items
                BCS     %FT90                                   ; then setup skip counter and then return

                TEQ     dr, #0                                  ; end of the devices search?
                BEQ     %FT88                                   ; yes, so we didn't deliver all

                LDR     r0, [dr, #device_DeviceName]            ; -> start of the filename
                Push    "r1, r7"                                ; preserve for next record scan

                Debug   enumdir, "trying record at", dr
                DebugS  enumdir, "it has the following name ", r0

                LDRB    r7, [r1]                                ; if directory to scan = "$"
                TEQ     r7, #0
                BEQ     %FT12                                   ; then match anything
05
                LDRB    r6, [r0], #1
                LDRB    r7, [r1], #1                            ; get two characters?
                UpperCase r6, lr
                UpperCase r7, lr                                ; ensure they are the same case

                TEQ     r6, r7                                  ; are they the same character?
                BNE     %FT10                                   ; no, so skip

                TEQ     r7, #0                                  ; if haven't terminated
                BNE     %BT05                                   ; then loop, else directory was a file, so ignore
07
                Pull    "r1, r7"
                LDR     dr, [dr, #device_Next]
                B       %BT00

10
                TEQ     r7, #0                                  ; if end of match string?
                TEQEQ   r6, #"."                                ; and device name is a directory reference, then OK
                BNE     %BT07                                   ; else try again for next record

; we have a match; r0 -> leaf part of name

12
                Pull    "r1, r7"                                ; restore pushed registers
                MOV     lr, #0                                  ; zero length counter
15
                LDRB    r8, [r0, lr]
                ADD     lr, lr, #1                              ; no, so increase the index
                TEQ     r8, #0
                TEQNE   r8, #"."                                ; end of the leaf section to be copied?
                BNE     %BT15

                CMP     r7, r4                                  ; if current index is < than requested one
                BCC     %FT25                                   ; then don't copy record, but do skip all identical ones

                Debug   enumdir, "writing record at", r2
                Debug   enumdir, "length of leaf to be copied", lr

                Push    "lr"                                    ; save length indication

                ADD     lr, lr, r2                              ; buffer address + length of name (including null)
                LDR     r6, [sp, #4]                            ; reload type of operation
                CMP     r6, #fsfunc_ReadDirEntriesInfo          ; if ReadDirEntriesInfo
                ADDEQ   lr, lr, #&14+3                          ; then add length of faff
                ADDHI   lr, lr, #&1D+3                          ; if CatalogObjects then more faff
                BICCS   lr, lr, #3                              ; if not ReadDirEntries then word align it
                CMP     lr, r5                                  ; is there enough room to copy record?
                Pull    "lr", HI                                ; if not then restore stack
                SUBHI   lr, r7, r4                              ; and indicate number copied
                BHI     %FT90                                   ; and finish

                Push    "r0,r1, r3-r5, lr"

                CMP     r6, #fsfunc_ReadDirEntries
                BEQ     %FT20                                   ; if ReadDirEntries then don't output info

                TEQ     r8, #"."                                ; is it a directory or a device?
                MOVEQ   r1, #object_directory
                LDREQ   r3, =FileType_Data:SHL:8                ; setup objects type + file type
                MOVNE   r1, #object_file
                LDRNE   r3, =FileType_Device:SHL:8
                STR     r1, [r2, #&10]                          ; store its object type

                Debug   enumdir, "object type", r1
                Debug   enumdir, "file type (shifted left 8)", r3

                ORR     r3, r3, #&FF:SHL:24
                ORR     r3, r3, #&F0:SHL:16                     ; sure is a file type of a load address

                LDR     pr, [dr, #device_Parent]                ; load the parent pointer, to get time stamp

                ADD     r4, pr, #parent_TimeStamp
                LDMIA   r4, {r4, r5}                            ; get the the bits of the time stamp
                ORR     r3, r3, r5                              ; and munge byte 5 of the time stamp into the load address

                CMP     r6, #fsfunc_CatalogObjects
                STREQ   r4, [r2, #&18]                          ; store bottom 4 bytes of time
                STREQB  r5, [r2, #&1C]                          ; and top byte of time
                STREQ   dr, [r2, #&14]                          ; store device record as SIN

                Debug   enumdir, "load + exec", r3, r4

                STR     r3, [r2, #&00]
                STR     r4, [r2, #&04]                          ; write the load + exec addresses

                MOV     r3, #0
                STR     r3, [r2, #&08]                          ; and then write its length

                ADD     r4, pr, #parent_MaxInputs
                LDMIA   r4, {r4, r5}                            ; get the i/o stream counts

                MOVS    r3, r4                                  ; does this device have input streams?
                ORRNE   r3, r3, #read_attribute
                TEQ     r5, #0
                ORRNE   r3, r3, #write_attribute
                STR     r3, [r2, #&0C]

                Debug   enumdir, "attributes", r3

                CMP     r6, #fsfunc_CatalogObjects
                ADDNE   r2, r2, #&14                            ; r2 -> destination address for name section
                ADDEQ   r2, r2, #&1D
20
                LDRB    r4, [r0], #1
                TEQ     r4, #0
                TEQNE   r4, #"."
                MOVEQ   r4, #0
                STRB    r4, [r2], #1                            ; copy a section of the name down
                BNE     %BT20                                   ; and loop until all copied (null or '.')

                Pull    "r0,r1, r3-r5, lr"
                MOV     r2, lr                                  ; move buffer pointer on
                Pull    "lr"                                    ; restore length indication

; skip all following device names with same leading part as the object we've just returned
; eg if cataloguing "Serial" and just returned "fred" for "Serial.fred.jim"
; then skip "Serial.fred.nosbod", "Serial.fred.sheila" etc

25
                ADD     r7, r7, #1                              ; one more entry processed
                ADD     r0, r0, lr                              ; r0 -> "j" in "Serial.fred.jim"
                Push    "r1, r2"
                LDR     r1, [dr, #device_DeviceName]            ; r1 -> start of device name, eg "Serial.fred.jim"
30
                LDR     dr, [dr, #device_Next]
                TEQ     dr, #0                                  ; end of the list?
                BEQ     %FT40                                   ; then no more skips

                LDR     r2, [dr, #device_DeviceName]
                BL      CompareStrings                          ; compare r1, r2 up to but not including r0
                BEQ     %BT30                                   ; if the same then skip
40
                Pull    "r1, r2"
                B       %BT00


88
                CMP     lr, #0                                  ; if we didn't return any because there weren't any
                MOVLE   lr, #0
                MOVLE   r7, #-1                                 ; then return with -1 in r4
90
                MOV     r4, r7                                  ; return index of next item
                MOV     r3, lr                                  ; number of items returned
                EXITS                                           ; and then return quickly

; in:   R1 -> wildcarded pathname
; out:  R1 -> pathname, with "$." removed

skipdollar      ENTRY
                LDRB    lr, [r1]
                CMP     lr, #"$"
                EXIT    NE
                LDRB    lr, [r1, #1]
                CMP     lr, #" "
                ADDLS   r1, r1, #1              ; skip "$"
                CMP     lr, #"."
                ADDEQ   r1, r1, #2              ; skip "$."
                EXIT

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++




; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
; fs_gbpb
;
; Write multiple blocks of data to a stream, can assume that the device
; is buffered and write directly into the buffer as required.
;

fs_gbpb         ROUT

                Debug   gbpb, "fs_gbpb called with reason code", r0
Kevin Bracey's avatar
Kevin Bracey committed
1637 1638 1639 1640 1641 1642 1643
                Debug   fs, "fs_gbpb ", r0
		[	debug
		Push	r0
		MOV	r0, sp
		Debug	fs, "entry sp", r0
		Pull	r0
		]
Neil Turton's avatar
Neil Turton committed
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668

                TEQ     r0, #2
                TEQNE   r0, #4                                  ; is the operation valid?
                BNE     fs_badop                                ; no, so give an error

                TEQ     r0, #2
                BEQ     gbpb_put


; read a largish lump from the device.

gbpb_get        ENTRY   "r0-r3, fr, r9, pr"

                MOV     fr, r1                                  ; -> file record
                LDR     pr, [fr, #file_Parent]

                MOV     r0, #DeviceCall_WakeUpRX
                MOV     r1, pr
                LDR     r2, [fr, #file_InternalHandle]
                BL      CallDevice                              ; ensure wake for calling
                BVS     %FT90                                   ; return an error

                ADD     r1, sp, #CallerR2
                LDMIA   r1, {r2-r3}                             ; get entry registers

Kevin Bracey's avatar
Kevin Bracey committed
1669 1670
		LDR	r4, [fr, #file_Flags]
                B       %FT12
Neil Turton's avatar
Neil Turton committed
1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684

10
                BL      checkfileRXOK
                BVS     %FT90

                LDR     r1, [pr, #parent_Flags]
                TST     r1, #pf_MonitorEOF
                BEQ     %FT11

                MOV     r1, fr
                BL      checkeof
                BCS     %FT20

11
Kevin Bracey's avatar
Kevin Bracey committed
1685 1686 1687 1688
		TST	r4, #ff_NonBlocking			; is this stream non-blocking?
		BNE	%FT20					; if so, finish now

12
Neil Turton's avatar
Neil Turton committed
1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740
                BL      checkescape                             ; if escape not pressed
 [ FastBufferMan
                MOVVC   r0, #BufferReason_RemoveBlock
                LDR     r1, [fr, #file_BufferPrivId]            ; get buffer managers private buffer id
                CallBuffMan VC                                  ; r2,r3 already set up
 |
                LDR     r1, [fr, #file_BufferHandle]
                ORR     r1, r1, #1:SHL:31                       ; mark for a block transfer
                MOV     r9, #REMV                               ; vector to be called
                SWIVC   XOS_CallAVector                         ; then try remove
 ]
                BVS     %FT90                                   ; either escape pressed or REMV gave error

                TEQ     r3, #0                                  ; all the data obtained?
                BNE     %BT10                                   ; no, so loop again until done

20
                LDR     r4, [sp, #CallerR3]                     ; How many did we ask for?
                SUB     r4, r4, r3                              ; file ptr = 0 + initial request - amount left
                ADD     r0, sp, #CallerR2
                STMIA   r0, {r2-r3}                             ; stash register list

                MOV     r0, #DeviceCall_SleepRX
                MOV     r1, pr
                LDR     r2, [fr, #file_InternalHandle]
                BL      CallDevice                              ; call the device

                EXITS                                           ; yippee it all worked

; (external) error

90
                STR     r0, [sp, #CallerR0]                     ; stash error pointer

                LDR     r4, [sp, #CallerR3]
                SUB     r4, r4, r3
                ADD     r0, sp, #CallerR2
                STMIA   r0, {r2-r3}                             ; stash register list

                MOV     r0, #DeviceCall_SleepRX
                LDR     r1, [fr, #file_Parent]
                LDR     r2, [fr, #file_InternalHandle]
                BL      CallDevice                              ; hold on device, sleep for a little while

                PullEnv
                ORRS    pc, lr, #V_bit                          ; return the error


; Give a huge lump of data to the device.

gbpb_put        ENTRY   "r0-r3, fr, r9"

Kevin Bracey's avatar
Kevin Bracey committed
1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772
		[		false
		Push		"r0-r3"

		;Check page 0 escape flag
                MOV		r0, #0
                LDRB		r0, [r0, #ESC_Status]
		BIC		r0, r0, #&CF
		Debug		fs, "ESC_Status", r0

		;Check SWI version of escape flag
		SWI		XOS_ReadEscapeState
		MOV		r0, #:NOT: C_bit
		BIC		r0, pc, r0
		Debug		fs, "ReadEscapeState", r0

		MOV		r0, #9 ;escape
		MOV		r1, #0
		MOV		r2, #0
		MOV		r3, #0
		SWI		XOS_ChangeEnvironment
		Debug		fs, "esc handler", r1

		MOV		r0, #10 ;event
		MOV		r1, #0
		MOV		r2, #0
		MOV		r3, #0
		SWI		XOS_ChangeEnvironment
		Debug		fs, "event handler", r1

		Pull		"r0-r3"
		]

Neil Turton's avatar
Neil Turton committed
1773 1774
                MOV     fr, r1

Kevin Bracey's avatar
Kevin Bracey committed
1775 1776
		LDR	r4, [fr, #file_Flags]

Neil Turton's avatar
Neil Turton committed
1777 1778 1779 1780 1781 1782 1783
 [ FastBufferMan
                LDR     r1, [fr, #file_BufferPrivId]            ; get buffer managers private buffer id
 |
                LDR     r1, [fr, #file_BufferHandle]
                ORR     r1, r1, #1:SHL:31                       ; it is a block insert
                MOV     r9, #INSV
 ]
Kevin Bracey's avatar
Kevin Bracey committed
1784
		;Loop till data written
Neil Turton's avatar
Neil Turton committed
1785
00
Kevin Bracey's avatar
Kevin Bracey committed
1786
		Debug	fs, "fsgbpb_put", r3
Neil Turton's avatar
Neil Turton committed
1787
                BL      checkescape
Kevin Bracey's avatar
Kevin Bracey committed
1788 1789 1790 1791 1792 1793 1794
		DebugIf	VS, fs, "detected escape---purging output buffer"
		BLVS	purgeoutputbuffer
		BVS	%90

                BL      checkfileTXOK
                BVS     %90

Neil Turton's avatar
Neil Turton committed
1795
 [ FastBufferMan
Kevin Bracey's avatar
Kevin Bracey committed
1796 1797
                MOV	r0, #BufferReason_InsertBlock
                CallBuffMan					; r2,r3 already set up
Neil Turton's avatar
Neil Turton committed
1798
 |
Kevin Bracey's avatar
Kevin Bracey committed
1799
                SWI	XOS_CallAVector                         ; attempt to write the block to the device
Neil Turton's avatar
Neil Turton committed
1800 1801 1802
 ]
                BVS     %90

Kevin Bracey's avatar
Kevin Bracey committed
1803 1804 1805
		TST	r4, #ff_NonBlocking			; is this stream non-blocking?
		BNE	%FT02					; if so, finish now

Neil Turton's avatar
Neil Turton committed
1806 1807
                TEQ     r3, #0                                  ; all written yet?
                BNE     %00
Kevin Bracey's avatar
Kevin Bracey committed
1808
		Debug	fs, "fsgbpb_put done"
Neil Turton's avatar
Neil Turton committed
1809

Kevin Bracey's avatar
Kevin Bracey committed
1810 1811 1812
02
                LDR     r4, [sp, #CallerR3]                     ; How many did we ask for?
                SUB     r4, r4, r3                              ; how many we transferred
Neil Turton's avatar
Neil Turton committed