FSystem 89.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
25
                MakeErrorBlock DeviceFS_Timeout
Neil Turton's avatar
Neil Turton committed
26 27 28

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

29
flush		SETD	false	;debug for file flushing
Malcolm Boura's avatar
Malcolm Boura committed
30
close		SETD	true	;sets the close debug flag, somehow not visable from version
Neil Turton's avatar
Neil Turton committed
31 32 33 34 35 36

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

37
checkescape     Entry
Neil Turton's avatar
Neil Turton committed
38 39 40 41 42 43 44 45 46 47 48
 [ 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
49
		Debug	fs, "escape read in DeviceFS!"
Neil Turton's avatar
Neil Turton committed
50 51 52 53 54 55 56 57 58 59 60
                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

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

61 62 63 64 65 66 67 68 69 70 71 72 73
 [ TWSleep
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
; call this timeout after the delay time.  it must wake up the task and make
; it non-blocking so as to return to the user.
;
; On entry, r12 = context, IRQs disabled
; must preserve all registers
;

timeout
                Entry   "r0"
                MOV     r0, #ff_TimedOut
Dan Ellis's avatar
Dan Ellis committed
74
                STR     r0, [r12, #file_PollWord]   ; r12 was fr when registered
75 76 77 78 79 80
                EXIT

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

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
Malcolm Boura's avatar
Malcolm Boura committed
81
; sleep receiving until pollword non-zero or error
82
;
Malcolm Boura's avatar
Malcolm Boura committed
83 84 85
sleep_on_tx     Entry   "r0-r2"
                ADR     r1, checkfileTX_sleep
                B       %f5
86

Malcolm Boura's avatar
Malcolm Boura committed
87 88 89 90 91 92 93 94
sleep_on_rx     ALTENTRY
                ADR     r1, checkfileRX_sleep
5
                Push    "r1"                        ; save r1 for later

                MOV     r0, #9
                MOV     r2, fr
                SWI     XOS_CallEvery
95 96 97 98 99 100 101 102

                LDR     r0, [fr, #file_Timeout]
                TEQ     r0, #0
                BEQ     %f10

                ADR     r1, timeout
                MOV     r2, fr
                SWI     XOS_CallAfter
Malcolm Boura's avatar
Malcolm Boura committed
103
10
104 105 106 107
                ADD     r1, fr, #file_PollWord
                MOV     r0, #UpCall_Sleep
                Debug   fs, "Pollword", r1
                SWI     XOS_UpCall
Neil Turton's avatar
Neil Turton committed
108

109 110 111
                SWI     XOS_LeaveOS                             ; trigger callbacks too
                SWI     XOS_EnterOS

Neil Turton's avatar
Neil Turton committed
112

113
                ADR     r0, timeout
Dan Ellis's avatar
Dan Ellis committed
114
                MOV     r1, fr
115
                SWI     XOS_RemoveTickerEvent
Neil Turton's avatar
Neil Turton committed
116

Malcolm Boura's avatar
Malcolm Boura committed
117 118 119 120 121 122 123 124 125 126 127 128 129
                Pull    "r0"                                    ; restore old r1 into r0
                SWI     XOS_RemoveTickerEvent

                PHPSEI  r1                                      ; disable interrupts so that there is no IRQ hole
                LDR     r0, [fr, #file_PollWord]
                Debug   fs, "Pollword contents now: ", r0
                TEQ     r0, #0
                TEQNE   r0, #ff_WakeUp
                MOVEQ   r0, #ff_Sleeping                        ; we must mark as sleeping as soon as possible,
                STREQ   r0, [fr, #file_PollWord]                ; so that we get woken up correctly
                PLP     r1
                EXIT    EQ

130
                PullEnv
Malcolm Boura's avatar
Malcolm Boura committed
131 132
                TST     r0, #ff_TimedOut
                BEQ     %f20
133 134
                ADR     r0, ErrorBlock_DeviceFS_Timeout         ; so set r0 -> error block
                DoError                                         ; translate error and return V set
Malcolm Boura's avatar
Malcolm Boura committed
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
20
                TST     r0, #ff_Error
                MOVEQ   pc, lr

                ADD     r0, fr, #file_Error
 [ debug
                ADD     r0, r0, #4
		DebugS	fs, "waking up with error: ", r0
                SUB     r0, r0, #4
 ]
                SETV
                MOV     pc, lr


checkfileTX_sleep Entry "fr"
                MOV     fr, r12
                BL      checkfileTXOK
                EXIT    VC
                B       %f10

checkfileRX_sleep ALTENTRY
                MOV     fr, r12
                BL      checkfileRXOK
                EXIT    VC
10
                Push    "r0-r3"
                ADD     r2, fr, #file_Error
                MOV     r1, #252
20
                LDR     r3, [r0, r1]
                STR     r3, [r2, r1]
                SUBS    r1, r1, #4
                BGE     %b20

                LDR     r2, [fr, #file_PollWord]
                ORR     r2, r2, #ff_Error
                STR     r2, [fr, #file_PollWord]

                Pull    "r0-r3"

                CLRV
                EXIT

178 179 180 181


; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 ]
Neil Turton's avatar
Neil Turton committed
182 183 184 185 186 187
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
; StartFSystem, this simply registers the filing system with FileSwitch, no
; errors are reported and no global states are setup.
;

188
StartFSystem    Entry   "r0-r3"
Neil Turton's avatar
Neil Turton committed
189 190 191 192 193
                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
Stewart Brodie's avatar
Stewart Brodie committed
194 195
                CLRV
                EXIT
Neil Turton's avatar
Neil Turton committed
196 197 198 199 200 201

                LTORG

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

Kevin Bracey's avatar
Kevin Bracey committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
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
218 219 220 221 222 223

fs_badop
                ADR     r0, ErrorBlock_DeviceFS_BadOp
 [ international
                B       MakeErrorWithFS_name                    ; "Bad operation on filing system devices:"
 |
Stewart Brodie's avatar
Stewart Brodie committed
224
                RETURNVS
Neil Turton's avatar
Neil Turton committed
225 226 227 228 229 230 231 232 233 234 235 236 237
 ]

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




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

238
RemoveFSystem   Entry   "r0-r1"
Neil Turton's avatar
Neil Turton committed
239 240 241
                MOV     r0, #FSControl_RemoveFS
                ADRL    r1, fs_name                             ; -> fs name to be removed
                SWI     XOS_FSControl
Stewart Brodie's avatar
Stewart Brodie committed
242 243
                CLRV
                EXIT
Neil Turton's avatar
Neil Turton committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263

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




; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
; 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.
;

264
IssueUpCall     Entry   "r0-r9"
Neil Turton's avatar
Neil Turton committed
265 266 267 268 269 270
                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
Stewart Brodie's avatar
Stewart Brodie committed
271 272
                CLRV
                EXIT
Neil Turton's avatar
Neil Turton committed
273 274 275 276 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

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
304
                Debug   fs, "fs_open ", r0
Neil Turton's avatar
Neil Turton committed
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 341 342

                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]
343 344 345 346
 [ TWSleep
                MOV     r0, #ff_DontSleep
                STR     r0, [fr, #file_PollWord]
 ]
Neil Turton's avatar
Neil Turton committed
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

                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]
381
                MOV     r5, r3                                  ; save buffer handle for the UpCall
Neil Turton's avatar
Neil Turton committed
382 383 384 385 386 387 388 389 390 391 392 393 394 395
                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
396 397 398 399 400 401 402 403
		[	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
		]

Stewart Brodie's avatar
Stewart Brodie committed
404
                Pull    "r0-pr, pc",VC                          ; return as the file object has been created OK
Neil Turton's avatar
Neil Turton committed
405 406 407 408 409 410 411 412
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"
Stewart Brodie's avatar
Stewart Brodie committed
413
                RETURNVS
Neil Turton's avatar
Neil Turton committed
414 415 416 417 418 419 420 421


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

                TEQ     fr, #0
                BLNE    removefileblock
                Pull    "r0-pr, lr"
Stewart Brodie's avatar
Stewart Brodie committed
422 423
                SUBS    r1, r1, r1                              ; R1=0 means file not opened.  Clears V.
                RETURN                                          ; remove file node and return duff handle
Neil Turton's avatar
Neil Turton committed
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


; 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

449 450 451
                CMP     r5, #-1
                BEQ     %FT10                                   ; no limit on number of inputs

Neil Turton's avatar
Neil Turton committed
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
                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
471
		LDR	r4, [fr, #file_FSwitchHandle]
Neil Turton's avatar
Neil Turton committed
472 473 474
                MOV     r1, pr
                MOV     r0, #DeviceCall_Initialise
                BL      CallDevice                              ; attempt to call the device
475 476
                STRVC   r2, [fr, #file_InternalHandle]
                BLVC    make_buffer                             ; make a usable buffer (if needed)
Neil Turton's avatar
Neil Turton committed
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 506 507 508 509 510 511 512 513
                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

514 515 516
                CMP     r5, #-1
                BEQ     %FT20                                   ; no limit on number of outputs

Neil Turton's avatar
Neil Turton committed
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
                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
535
		LDR	r4, [fr, #file_FSwitchHandle]
Neil Turton's avatar
Neil Turton committed
536 537 538 539
                MOV     r2, fr
                MOV     r1, pr
                MOV     r0, #DeviceCall_Initialise
                BL      CallDevice                              ; call the device
540 541
                STRVC   r2, [fr, #file_InternalHandle]
                BLVC    make_buffer                             ; make a usable buffer (if needed)
Neil Turton's avatar
Neil Turton committed
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
                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]
568
                TST     r0, #df_BufferedDevice                  ; is it a buffered device?
Dan Ellis's avatar
Dan Ellis committed
569
                Pull    "r3, pc", EQ                            ; no, so we cannot even consider creating a buffer
Neil Turton's avatar
Neil Turton committed
570 571 572 573 574 575 576 577 578 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

                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
639 640 641 642 643
 [ 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
644 645
                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
646
 ]
Malcolm Boura's avatar
Malcolm Boura committed
647
                ADRL    r2, detach                              ; try to close stream if someone tries to detach
Neil Turton's avatar
Neil Turton committed
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
                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

Stewart Brodie's avatar
Stewart Brodie committed
676
wakeup          EntryS  "r0-r3"
Neil Turton's avatar
Neil Turton committed
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
                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
692 693
 [ wakeup_data_present
; wake up code, this is called when some data is inserted into a dormant RX buffer.
694
wakeup_rx	Entry	"r0-r9"				; Stack everything, in case CallAVector corrupts it
Neil Turton's avatar
Neil Turton committed
695

Stewart Brodie's avatar
Stewart Brodie committed
696 697 698 699
                ASSERT (IRQ_mode :AND: :NOT: SVC_mode) = 0

                SavePSR r2
                ORR     r0, r2, #SVC_mode
Malcolm Boura's avatar
Malcolm Boura committed
700
                RestPSR r0,,cf                          ; set SVC mode
Stewart Brodie's avatar
Stewart Brodie committed
701
                NOP
Neil Turton's avatar
Neil Turton committed
702

703
		Push	"r2,lr"				; Preserve r2 and lr across CallAVector
Neil Turton's avatar
Neil Turton committed
704 705
		MOV	r0, #UpCall_DeviceRxDataPresent
                LDR     r1, [r8, #file_FSwitchHandle]
706 707 708 709
;		SWI	XOS_UpCall
		MOV	r9, #UpCallV
		SWI	XOS_CallAVector
		Pull	"r2,lr"
Neil Turton's avatar
Neil Turton committed
710

Stewart Brodie's avatar
Stewart Brodie committed
711
                RestPSR r2,,cf
Neil Turton's avatar
Neil Turton committed
712 713
		NOP

714 715 716 717 718
 [ TWSleep
                LDR     r0, [r8, #file_PollWord]
                TEQ     r0, #ff_DontSleep
                MOVNE   r0, #ff_WakeUp
                STRNE   r0, [r8, #file_PollWord]
Malcolm Boura's avatar
Malcolm Boura committed
719
                Debug   fs, "Waking up pollword"
720
 ]
Stewart Brodie's avatar
Stewart Brodie committed
721
		EXIT
Neil Turton's avatar
Neil Turton committed
722
 ]
Neil Turton's avatar
Neil Turton committed
723 724
; detach code, this is called when someone tries to deregister the buffer, or link it some other device

725
detach          Entry   "r0-r2"
Neil Turton's avatar
Neil Turton committed
726 727 728
                MOV     r1, #Service_DeviceFSCloseRequest
                LDR     r2, [r8, #file_FSwitchHandle]           ; get file handle
                SWI     XOS_ServiceCall
Stewart Brodie's avatar
Stewart Brodie committed
729
                CMP     r1, #Service_Serviced                   ; if someone responded (then Z set, V cleared)
Neil Turton's avatar
Neil Turton committed
730
                PullEnv
Stewart Brodie's avatar
Stewart Brodie committed
731
                MOVEQ   pc, lr
Neil Turton's avatar
Neil Turton committed
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
                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

747
RequestCloseInputs Entry "r0-r3,r5"
Neil Turton's avatar
Neil Turton committed
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808

        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.
;

809
fs_get          Entry   "r0-r2, fr,pr"
Neil Turton's avatar
Neil Turton committed
810

Kevin Bracey's avatar
Kevin Bracey committed
811 812
		Debug	fs, "fs_get"

Neil Turton's avatar
Neil Turton committed
813 814 815 816 817 818
                MOV     fr, r1                                  ; -> file record
                LDR     pr, [fr, #file_Parent]

                MOV     r0, #DeviceCall_WakeUpRX
                MOV     r1, pr
                LDR     r2, [fr, #file_InternalHandle]
819 820
                MOV     r3, #1                                  ; since we support unbuffered GBPB, FileSwitch only
                                                                ; calls this entry point to handle OS_BGet
Neil Turton's avatar
Neil Turton committed
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
                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
840

Neil Turton's avatar
Neil Turton committed
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
                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
867
 [ TWSleep
Neil Turton's avatar
Neil Turton committed
868 869
20

870 871 872 873 874 875 876
                BCC     %FT25                                   ; if data was returned don't loop
                LDR     r1, [fr, #file_PollWord]
                TEQ     r1, #ff_DontSleep
                BEQ     %BT00                                   ; we don't want to sleep

                ASSERT  (BufferReason_RemoveByte <> -1)
                CMP     r0, #-1                                 ; if we're not buffered, r0 = -1 here.
Malcolm Boura's avatar
Malcolm Boura committed
877 878 879
                MOVEQ   r1, #ff_WakeUp                        ; reset pollword if we're buffered, if not then
                STREQ   r1, [fr, #file_PollWord]                ; we can't set a pollword to wake us up
                BL      sleep_on_rx
880 881 882 883 884 885 886
                BVS     %FT30

                B       %BT00                                   ; loop again to make sure actually received data
25
 |
                BCS     %BT00                                   ; loop again to make sure actually received data
 ]
Neil Turton's avatar
Neil Turton committed
887 888 889 890
                STR     r0, [sp, #CallerR0]                     ; write to return frame

                BL      %FT80                                   ; SleepRX - ignore error returned

Stewart Brodie's avatar
Stewart Brodie committed
891
                CLC                                             ; ensure C and V clear on return
Neil Turton's avatar
Neil Turton committed
892
                PullEnv
Stewart Brodie's avatar
Stewart Brodie committed
893
                MOV     pc, lr
Neil Turton's avatar
Neil Turton committed
894 895 896 897 898 899
30
                STR     r0, [sp, #CallerR0]                     ; ensure error pointer on return frame valid

                BL      %FT80                                   ; SleepRX - ignore error returned

                PullEnv
Stewart Brodie's avatar
Stewart Brodie committed
900
                RETURNVS                                        ; return the error
Neil Turton's avatar
Neil Turton committed
901 902 903 904 905

40
                BL      %FT80

                PullEnv
Stewart Brodie's avatar
Stewart Brodie committed
906 907
                SEC                                             ; C set, V clear - EOF encountered
                MOV     pc, lr
Neil Turton's avatar
Neil Turton committed
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923


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
Malcolm Boura's avatar
Malcolm Boura committed
924
checkfileRXOK   Entry "r0-r2"
Neil Turton's avatar
Neil Turton committed
925 926 927 928 929 930 931 932 933
                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
934 935 936
		DebugIf	NE, fs, "monitor calls supported"
		DebugIf	EQ, fs, "monitor calls not supported"
		EXIT	EQ
Neil Turton's avatar
Neil Turton committed
937

Kevin Bracey's avatar
Kevin Bracey committed
938 939 940 941 942
                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
943 944 945 946 947 948 949 950 951

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
; 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.

952
fs_put		Entry	"r0-r2, fr, r9"
Neil Turton's avatar
Neil Turton committed
953

Kevin Bracey's avatar
Kevin Bracey committed
954
		Debug	fs, "fs_put"
Neil Turton's avatar
Neil Turton committed
955

Kevin Bracey's avatar
Kevin Bracey committed
956 957 958 959 960 961 962 963 964
 [ 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

Stewart Brodie's avatar
Stewart Brodie committed
965
		MOV	r1, #0                  ; ready for OS_ChangeEnvironment
Kevin Bracey's avatar
Kevin Bracey committed
966 967
		;Check SWI version of escape flag
		SWI	XOS_ReadEscapeState
Stewart Brodie's avatar
Stewart Brodie committed
968
                ADC     r0, r1, #0
Kevin Bracey's avatar
Kevin Bracey committed
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
		Debug	fs, "ReadEscapeState", r0

		MOV	r0, #9 ;escape
		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
991
 [ FastBufferMan
Kevin Bracey's avatar
Kevin Bracey committed
992 993
		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
994
 |
Kevin Bracey's avatar
Kevin Bracey committed
995
		MOVNE	r9, #INSV				; saves an extra label
Neil Turton's avatar
Neil Turton committed
996
 ]
Kevin Bracey's avatar
Kevin Bracey committed
997 998 999
		BNE	%10

		; Non-buffered device
Neil Turton's avatar
Neil Turton committed
1000
00
Kevin Bracey's avatar
Kevin Bracey committed
1001 1002
		BL	checkescape				; is an escape pending?
		BVS	%99
Neil Turton's avatar
Neil Turton committed
1003

Kevin Bracey's avatar
Kevin Bracey committed
1004 1005
		BL	checkfileTXOK
		BVS	%99
Neil Turton's avatar
Neil Turton committed
1006

Kevin Bracey's avatar
Kevin Bracey committed
1007 1008
		LDR	r1, [fr, #file_RXTXWord]
		CMP	r1, #-1					; is the RX/TX word free yet?
1009 1010 1011 1012 1013 1014
 [ TWSleep
		BEQ	%05					; yes, so don't loop again
                LDR     r1, [fr, #file_PollWord]
                TEQ     r1, #ff_DontSleep
                BEQ     %00                                     ; we don't want to sleep

Malcolm Boura's avatar
Malcolm Boura committed
1015
                BL      sleep_on_tx
1016
                BVS     %FT99
Neil Turton's avatar
Neil Turton committed
1017

1018 1019 1020 1021 1022
                B       %00                                     ; loop again
5
 |
		BNE	%00					; no, so loop again
 ]
Kevin Bracey's avatar
Kevin Bracey committed
1023
		STR	r0, [fr, #file_RXTXWord]		; yes, so write the word in now
Neil Turton's avatar
Neil Turton committed
1024

Kevin Bracey's avatar
Kevin Bracey committed
1025 1026 1027 1028 1029 1030 1031
		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
1032
10
Kevin Bracey's avatar
Kevin Bracey committed
1033 1034 1035 1036 1037
		BL	checkescape				; is an escape pending
		BLVS	purgeoutputbuffer			; if so then purge output buffer
		BVS	%99

		BL	checkfileTXOK
1038
		BLVS	purgeoutputbuffer			; if an error then purge output buffer
Kevin Bracey's avatar
Kevin Bracey committed
1039 1040
		BVS	%99

Neil Turton's avatar
Neil Turton committed
1041
 [ FastBufferMan
Kevin Bracey's avatar
Kevin Bracey committed
1042 1043
		MOV	r0, #BufferReason_InsertByte
		CallBuffMan					; r1,r2 already set up
Neil Turton's avatar
Neil Turton committed
1044
 |
Kevin Bracey's avatar
Kevin Bracey committed
1045
		SWI	XOS_CallAVector
Neil Turton's avatar
Neil Turton committed
1046
 ]
Kevin Bracey's avatar
Kevin Bracey committed
1047
		BVS	%99					; return any errors that may occur
1048 1049 1050 1051 1052 1053
 [ TWSleep
		BCC	%20					; don't loop if inserted
                LDR     r1, [fr, #file_PollWord]
                TEQ     r1, #ff_DontSleep
                BEQ     %10                                     ; we don't want to sleep

Malcolm Boura's avatar
Malcolm Boura committed
1054 1055
;                MOV     r1, #ff_Sleeping                        ; reset pollword
;                STR     r1, [fr, #file_PollWord]
1056

Malcolm Boura's avatar
Malcolm Boura committed
1057
                BL      sleep_on_tx
1058 1059 1060 1061 1062
                BVS     %FT99

		B	%10					; keep looping
20
 |
Kevin Bracey's avatar
Kevin Bracey committed
1063
		BCS	%10					; keep looping until inserted
1064
 ]
Neil Turton's avatar
Neil Turton committed
1065

Kevin Bracey's avatar
Kevin Bracey committed
1066 1067 1068
		CLRV
99		STRVS	r0, [sp]
		PullEnv
Stewart Brodie's avatar
Stewart Brodie committed
1069
                MOV     pc, lr
Neil Turton's avatar
Neil Turton committed
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
; 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
1080 1081 1082

		Debug	fs, "fs_args", r0

Neil Turton's avatar
Neil Turton committed
1083 1084
                Debug   args, "fs_args called with reason code", r0

Stewart Brodie's avatar
Stewart Brodie committed
1085 1086
                CMP     r0, #fsargs_SetEXT                      ; ignore SetEXT
                MOVEQ   pc, lr
Neil Turton's avatar
Neil Turton committed
1087 1088 1089 1090

                TEQ     r0, #fsargs_SetPTR
                TEQNE   r0, #fsargs_EnsureSize
                TEQNE   r0, #fsargs_WriteZeroes
Neil Turton's avatar
Neil Turton committed
1091
                BEQ     fs_badop                ; these ones give nice errors
Neil Turton's avatar
Neil Turton committed
1092 1093 1094

                TEQ     r0, #fsargs_ReadPTR
                TEQNE   r0, #fsargs_ReadSize
Stewart Brodie's avatar
Stewart Brodie committed
1095 1096
                SUBEQS  r2, r2, r2              ; return 0, Z still set, clear V
                MOVEQ   pc, lr
Neil Turton's avatar
Neil Turton committed
1097

Neil Turton's avatar
Neil Turton committed
1098 1099 1100 1101 1102 1103
                TEQ	r0, #fsargs_ReadEXT
		BEQ	args_ext

		TEQ	r0, #fsargs_IOCtl
		BEQ	args_ioctl

Neil Turton's avatar
Neil Turton committed
1104 1105 1106
                TEQ     r0, #fsargs_ReadLoadExec
                BEQ     args_loadexec

Neil Turton's avatar
Neil Turton committed
1107
                TEQ     r0, #fsargs_Flush               ; is it a flush?
Neil Turton's avatar
Neil Turton committed
1108 1109
                BEQ     args_flush

Neil Turton's avatar
Neil Turton committed
1110 1111
                TEQ     r0, #fsargs_EOFCheck            ; is it a eof read?
                BEQ     args_eof                        ; then do it
Neil Turton's avatar
Neil Turton committed
1112 1113 1114

                B       fs_badop

Neil Turton's avatar
Neil Turton committed
1115 1116
; handle ioctl's

Kevin Bracey's avatar
Kevin Bracey committed
1117
args_ioctl	ROUT
1118
		Entry	"r0-r3, fr,pr"
Kevin Bracey's avatar
Kevin Bracey committed
1119 1120 1121 1122

		LDR	r0, [r2, #0]			; load ioctl reason codes and flags
		BIC	r0, r0, #&ff00ffff
		TEQ	r0, #&00ff0000
Stewart Brodie's avatar
Stewart Brodie committed
1123 1124 1125
                BNE     %FT05
		BL	ioctl_miscop
		B	%10				; only V flag corrupted so OK
Kevin Bracey's avatar
Kevin Bracey committed
1126 1127

; catch remaining cases and pass into device driver
Stewart Brodie's avatar
Stewart Brodie committed
1128
05
Neil Turton's avatar
Neil Turton committed
1129 1130 1131 1132 1133 1134
                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
1135
                LDR     r2, [fr, #file_InternalHandle]
Neil Turton's avatar
Neil Turton committed
1136 1137
                BL      CallDevice

Kevin Bracey's avatar
Kevin Bracey committed
1138
10
Neil Turton's avatar
Neil Turton committed
1139
                STRVS   r0, [sp]
Stewart Brodie's avatar
Stewart Brodie committed
1140
                EXIT
Neil Turton's avatar
Neil Turton committed
1141

Kevin Bracey's avatar
Kevin Bracey committed
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156

; 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
Stewart Brodie's avatar
Stewart Brodie committed
1157
		MOV	pc, lr				; 0 nothing
Kevin Bracey's avatar
Kevin Bracey committed
1158
		B	ioctl_miscop_nonblock		; 1 set non-blocking I/O
1159 1160 1161 1162
                MOV     pc, lr                          ; 2 buffer threshold
                MOV     pc, lr                          ; 3 flush stream buffers
                B       ioctl_miscop_sleep              ; 4 sleeping I/O (Calls UpCall 6)
                B       ioctl_miscop_timeout            ; 5 timeout if sleeping, 0 means sleep forever
Kevin Bracey's avatar
Kevin Bracey committed
1163 1164
20
                PullEnv
Stewart Brodie's avatar
Stewart Brodie committed
1165
                MOVEQ   pc, lr                          ; V must be clear to get here
Kevin Bracey's avatar
Kevin Bracey committed
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
		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
Stewart Brodie's avatar
Stewart Brodie committed
1195 1196
                CLRV
		MOV	pc, lr
Kevin Bracey's avatar
Kevin Bracey committed
1197

1198 1199
ioctl_miscop_sleep
                ROUT
Dan Ellis's avatar
Dan Ellis committed
1200
		LDR	r0, [r2, #0]			; load ioctl reason codes and flags
1201 1202 1203 1204 1205 1206 1207 1208 1209

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

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

		LDR	r3, [r2, #4]			; load ioctl data word
		CMP	r3, #0
Dan Ellis's avatar
Dan Ellis committed
1210 1211
                MOVEQ   r1, #ff_DontSleep               ; sleeping disabled
                MOVNE   r1, #ff_WakeUp                  ; sleeping enabled
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
		STR	r1, [fr, #file_PollWord]	; write back
10
		TST	r0, #&40000000			; test read flag
		BEQ	%20

                TEQ     r1, #ff_DontSleep               ; test if we're not going to sleep
		MOVEQ	r0, #1				; sleeping disabled
		MOVNE	r0, #0				; sleeping enabled
		STR	r0, [r2, #4]			; store result in ioctl block
20
                CLRV
		MOV	pc, lr

ioctl_miscop_timeout
                ROUT
Dan Ellis's avatar
Dan Ellis committed
1227
		LDR	r0, [r2, #0]			; load ioctl reason codes and flags
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244

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

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

		LDR	r3, [r2, #4]			; load ioctl data word
		STR	r3, [fr, #file_Timeout]	        ; write back
10
		TST	r0, #&40000000			; test read flag
		BEQ	%20

		STR	r1, [r2, #4]			; store result in ioctl block
20
                CLRV
		MOV	pc, lr
Kevin Bracey's avatar
Kevin Bracey committed
1245

Neil Turton's avatar
Neil Turton committed
1246 1247
; handle checking of the file extent

1248
args_ext	Entry	"r0-r1,r3-r6, fr"
Neil Turton's avatar
Neil Turton committed
1249 1250

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

Neil Turton's avatar
Neil Turton committed
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
                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]
 [ :LNOT: FastBufferMan
1270
                MOVVC   r2, r6
Neil Turton's avatar
Neil Turton committed
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
 ]
		EXIT
5
; deal with tx stream
 [ FastBufferMan
                MOV     r0, #BufferReason_FreeSpace
                LDR     r1, [fr, #file_BufferPrivId]
                CallBuffMan
 |
                SWI     XBuffer_GetInfo
 ]
                STRVS   r0, [sp]
 [ :LNOT: FastBufferMan
1284
                MOVVC	r2, r5
Neil Turton's avatar
Neil Turton committed
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
 ]
		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
1308

1309
checkeof        Entry   "r2"                                    ; internal routine,
Neil Turton's avatar
Neil Turton committed
1310 1311 1312 1313 1314
                BL      args_eof
                CMPVC   r2, #1                                  ; exits CS for EOF, else CC
                EXIT


1315
args_eof        Entry   "r0,r1,r3-r6, fr"
Neil Turton's avatar
Neil Turton committed
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334

                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]
Stewart Brodie's avatar
Stewart Brodie committed
1335
                EXIT    VS
Neil Turton's avatar
Neil Turton committed
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368

 [ 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
Stewart Brodie's avatar
Stewart Brodie committed
1369 1370
                CLRV
                EXIT
Neil Turton's avatar
Neil Turton committed
1371 1372 1373 1374 1375 1376


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

1377
args_flush      Entry   "r1-r3,fr"
Neil Turton's avatar
Neil Turton committed
1378

1379
		Debug	flush, "Entered Flush"
Stewart Brodie's avatar
Stewart Brodie committed
1380
                ADDS    fr, r1, #0                              ; clear V!
Neil Turton's avatar
Neil Turton committed
1381 1382
                LDR     r0, [r1, #file_Flags]
                TST     r0, #ff_FileForTX                       ; is the file an output object
Stewart Brodie's avatar
Stewart Brodie committed
1383
                EXIT    EQ                                      ; no, so return now
Neil Turton's avatar
Neil Turton committed
1384
10
1385
                BLVC    checkeof                                ; CS = exit loop until EOF or Escape pressed
1386
		DebugIf CS, flush, "Exited  Flush EOF"
Stewart Brodie's avatar
Stewart Brodie committed
1387
                EXIT    CS                                      ; normal exit eof found
Kevin Bracey's avatar