FSystem 89.7 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
                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

464 465
                LDR     r3, [fr, #file_Flags]
                ORR     r3,r3, #ff_FileForRX :OR: ff_ModifiedCounters ; being used for RX
Neil Turton's avatar
Neil Turton committed
466 467 468 469 470 471
                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
472
		LDR	r4, [fr, #file_FSwitchHandle]
Neil Turton's avatar
Neil Turton committed
473 474 475
                MOV     r1, pr
                MOV     r0, #DeviceCall_Initialise
                BL      CallDevice                              ; attempt to call the device
476 477
                STRVC   r2, [fr, #file_InternalHandle]
                BLVC    make_buffer                             ; make a usable buffer (if needed)
Neil Turton's avatar
Neil Turton committed
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 514
                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

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

Neil Turton's avatar
Neil Turton committed
518 519 520 521 522 523 524 525 526 527 528 529
                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

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

                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
581 582
                ASSERT  device_RXBufferFlags + 4 = device_RXBufferSize
                ASSERT  device_TXBufferFlags + 4 = device_TXBufferSize
Neil Turton's avatar
Neil Turton committed
583 584 585 586 587 588 589 590 591 592 593 594
                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

595 596 597 598 599 600 601
                ; Buffer_Create will create a buffer size of r4 -1 or r4 -4
                ; depending on word aligned flag. So r4 is increased so that
                ; we get the size requested.
                TST     r3, #BufferFlags_WordAlignedInserts
                ADDEQ   r4, r4, #1
                ADDNE   r4, r4, #4

Neil Turton's avatar
Neil Turton committed
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
                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
650 651 652 653 654
 [ 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
655 656
                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
657
 ]
Malcolm Boura's avatar
Malcolm Boura committed
658
                ADRL    r2, detach                              ; try to close stream if someone tries to detach
Neil Turton's avatar
Neil Turton committed
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
                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
687
wakeup          EntryS  "r0-r3"
Neil Turton's avatar
Neil Turton committed
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
                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
703 704
 [ wakeup_data_present
; wake up code, this is called when some data is inserted into a dormant RX buffer.
705
wakeup_rx	Entry	"r0-r9"				; Stack everything, in case CallAVector corrupts it
Neil Turton's avatar
Neil Turton committed
706

Stewart Brodie's avatar
Stewart Brodie committed
707 708 709 710
                ASSERT (IRQ_mode :AND: :NOT: SVC_mode) = 0

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

714
		Push	"r2,lr"				; Preserve r2 and lr across CallAVector
Neil Turton's avatar
Neil Turton committed
715 716
		MOV	r0, #UpCall_DeviceRxDataPresent
                LDR     r1, [r8, #file_FSwitchHandle]
717 718 719 720
;		SWI	XOS_UpCall
		MOV	r9, #UpCallV
		SWI	XOS_CallAVector
		Pull	"r2,lr"
Neil Turton's avatar
Neil Turton committed
721

Stewart Brodie's avatar
Stewart Brodie committed
722
                RestPSR r2,,cf
Neil Turton's avatar
Neil Turton committed
723 724
		NOP

725 726 727 728 729
 [ 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
730
                Debug   fs, "Waking up pollword"
731
 ]
Stewart Brodie's avatar
Stewart Brodie committed
732
		EXIT
Neil Turton's avatar
Neil Turton committed
733
 ]
Neil Turton's avatar
Neil Turton committed
734 735
; detach code, this is called when someone tries to deregister the buffer, or link it some other device

736
detach          Entry   "r0-r2"
Neil Turton's avatar
Neil Turton committed
737 738 739
                MOV     r1, #Service_DeviceFSCloseRequest
                LDR     r2, [r8, #file_FSwitchHandle]           ; get file handle
                SWI     XOS_ServiceCall
Stewart Brodie's avatar
Stewart Brodie committed
740
                CMP     r1, #Service_Serviced                   ; if someone responded (then Z set, V cleared)
Neil Turton's avatar
Neil Turton committed
741
                PullEnv
Stewart Brodie's avatar
Stewart Brodie committed
742
                MOVEQ   pc, lr
Neil Turton's avatar
Neil Turton committed
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
                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

758
RequestCloseInputs Entry "r0-r3,r5"
Neil Turton's avatar
Neil Turton committed
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 809 810 811 812 813 814 815 816 817 818 819

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

820
fs_get          Entry   "r0-r2, fr,pr"
Neil Turton's avatar
Neil Turton committed
821

Kevin Bracey's avatar
Kevin Bracey committed
822 823
		Debug	fs, "fs_get"

Neil Turton's avatar
Neil Turton committed
824 825 826 827 828 829
                MOV     fr, r1                                  ; -> file record
                LDR     pr, [fr, #file_Parent]

                MOV     r0, #DeviceCall_WakeUpRX
                MOV     r1, pr
                LDR     r2, [fr, #file_InternalHandle]
830 831
                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
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
                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
851

Neil Turton's avatar
Neil Turton committed
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
                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
878
 [ TWSleep
Neil Turton's avatar
Neil Turton committed
879 880
20

881 882 883 884 885 886 887
                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
888 889 890
                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
891 892 893 894 895 896 897
                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
898 899 900 901
                STR     r0, [sp, #CallerR0]                     ; write to return frame

                BL      %FT80                                   ; SleepRX - ignore error returned

Stewart Brodie's avatar
Stewart Brodie committed
902
                CLC                                             ; ensure C and V clear on return
Neil Turton's avatar
Neil Turton committed
903
                PullEnv
Stewart Brodie's avatar
Stewart Brodie committed
904
                MOV     pc, lr
Neil Turton's avatar
Neil Turton committed
905 906 907 908 909 910
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
911
                RETURNVS                                        ; return the error
Neil Turton's avatar
Neil Turton committed
912 913 914 915 916

40
                BL      %FT80

                PullEnv
Stewart Brodie's avatar
Stewart Brodie committed
917 918
                SEC                                             ; C set, V clear - EOF encountered
                MOV     pc, lr
Neil Turton's avatar
Neil Turton committed
919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934


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
935
checkfileRXOK   Entry "r0-r2"
Neil Turton's avatar
Neil Turton committed
936 937 938 939 940 941 942 943 944
                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
945 946 947
		DebugIf	NE, fs, "monitor calls supported"
		DebugIf	EQ, fs, "monitor calls not supported"
		EXIT	EQ
Neil Turton's avatar
Neil Turton committed
948

Kevin Bracey's avatar
Kevin Bracey committed
949 950 951 952 953
                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
954 955 956 957 958 959 960 961 962

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

963
fs_put		Entry	"r0-r2, fr, r9"
Neil Turton's avatar
Neil Turton committed
964

Kevin Bracey's avatar
Kevin Bracey committed
965
		Debug	fs, "fs_put"
Neil Turton's avatar
Neil Turton committed
966

Kevin Bracey's avatar
Kevin Bracey committed
967 968 969 970 971 972 973 974 975
 [ 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
976
		MOV	r1, #0                  ; ready for OS_ChangeEnvironment
Kevin Bracey's avatar
Kevin Bracey committed
977 978
		;Check SWI version of escape flag
		SWI	XOS_ReadEscapeState
Stewart Brodie's avatar
Stewart Brodie committed
979
                ADC     r0, r1, #0
Kevin Bracey's avatar
Kevin Bracey committed
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
		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
1002
 [ FastBufferMan
Kevin Bracey's avatar
Kevin Bracey committed
1003 1004
		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
1005
 |
Kevin Bracey's avatar
Kevin Bracey committed
1006
		MOVNE	r9, #INSV				; saves an extra label
Neil Turton's avatar
Neil Turton committed
1007
 ]
Kevin Bracey's avatar
Kevin Bracey committed
1008 1009 1010
		BNE	%10

		; Non-buffered device
Neil Turton's avatar
Neil Turton committed
1011
00
Kevin Bracey's avatar
Kevin Bracey committed
1012 1013
		BL	checkescape				; is an escape pending?
		BVS	%99
Neil Turton's avatar
Neil Turton committed
1014

Kevin Bracey's avatar
Kevin Bracey committed
1015 1016
		BL	checkfileTXOK
		BVS	%99
Neil Turton's avatar
Neil Turton committed
1017

Kevin Bracey's avatar
Kevin Bracey committed
1018 1019
		LDR	r1, [fr, #file_RXTXWord]
		CMP	r1, #-1					; is the RX/TX word free yet?
1020 1021 1022 1023 1024 1025
 [ 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
1026
                BL      sleep_on_tx
1027
                BVS     %FT99
Neil Turton's avatar
Neil Turton committed
1028

1029 1030 1031 1032 1033
                B       %00                                     ; loop again
5
 |
		BNE	%00					; no, so loop again
 ]
Kevin Bracey's avatar
Kevin Bracey committed
1034
		STR	r0, [fr, #file_RXTXWord]		; yes, so write the word in now
Neil Turton's avatar
Neil Turton committed
1035

Kevin Bracey's avatar
Kevin Bracey committed
1036 1037 1038 1039 1040 1041 1042
		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
1043
10
Kevin Bracey's avatar
Kevin Bracey committed
1044 1045 1046 1047 1048
		BL	checkescape				; is an escape pending
		BLVS	purgeoutputbuffer			; if so then purge output buffer
		BVS	%99

		BL	checkfileTXOK
1049
		BLVS	purgeoutputbuffer			; if an error then purge output buffer
Kevin Bracey's avatar
Kevin Bracey committed
1050 1051
		BVS	%99

Neil Turton's avatar
Neil Turton committed
1052
 [ FastBufferMan
Kevin Bracey's avatar
Kevin Bracey committed
1053 1054
		MOV	r0, #BufferReason_InsertByte
		CallBuffMan					; r1,r2 already set up
Neil Turton's avatar
Neil Turton committed
1055
 |
Kevin Bracey's avatar
Kevin Bracey committed
1056
		SWI	XOS_CallAVector
Neil Turton's avatar
Neil Turton committed
1057
 ]
Kevin Bracey's avatar
Kevin Bracey committed
1058
		BVS	%99					; return any errors that may occur
1059 1060 1061 1062 1063 1064
 [ 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
1065 1066
;                MOV     r1, #ff_Sleeping                        ; reset pollword
;                STR     r1, [fr, #file_PollWord]
1067

Malcolm Boura's avatar
Malcolm Boura committed
1068
                BL      sleep_on_tx
1069 1070 1071 1072 1073
                BVS     %FT99

		B	%10					; keep looping
20
 |
Kevin Bracey's avatar
Kevin Bracey committed
1074
		BCS	%10					; keep looping until inserted
1075
 ]
Neil Turton's avatar
Neil Turton committed
1076

Kevin Bracey's avatar
Kevin Bracey committed
1077 1078 1079
		CLRV
99		STRVS	r0, [sp]
		PullEnv
Stewart Brodie's avatar
Stewart Brodie committed
1080
                MOV     pc, lr
Neil Turton's avatar
Neil Turton committed
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
; 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
1091 1092 1093

		Debug	fs, "fs_args", r0

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

Stewart Brodie's avatar
Stewart Brodie committed
1096 1097
                CMP     r0, #fsargs_SetEXT                      ; ignore SetEXT
                MOVEQ   pc, lr
Neil Turton's avatar
Neil Turton committed
1098 1099 1100 1101

                TEQ     r0, #fsargs_SetPTR
                TEQNE   r0, #fsargs_EnsureSize
                TEQNE   r0, #fsargs_WriteZeroes
Neil Turton's avatar
Neil Turton committed
1102
                BEQ     fs_badop                ; these ones give nice errors
Neil Turton's avatar
Neil Turton committed
1103 1104 1105

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

Neil Turton's avatar
Neil Turton committed
1109 1110 1111 1112 1113 1114
                TEQ	r0, #fsargs_ReadEXT
		BEQ	args_ext

		TEQ	r0, #fsargs_IOCtl
		BEQ	args_ioctl

Neil Turton's avatar
Neil Turton committed
1115 1116 1117
                TEQ     r0, #fsargs_ReadLoadExec
                BEQ     args_loadexec

Neil Turton's avatar
Neil Turton committed
1118
                TEQ     r0, #fsargs_Flush               ; is it a flush?
Neil Turton's avatar
Neil Turton committed
1119 1120
                BEQ     args_flush

Neil Turton's avatar
Neil Turton committed
1121 1122
                TEQ     r0, #fsargs_EOFCheck            ; is it a eof read?
                BEQ     args_eof                        ; then do it
Neil Turton's avatar
Neil Turton committed
1123 1124 1125

                B       fs_badop

Neil Turton's avatar
Neil Turton committed
1126 1127
; handle ioctl's

Kevin Bracey's avatar
Kevin Bracey committed
1128
args_ioctl	ROUT
1129
		Entry	"r0-r3, fr,pr"
Kevin Bracey's avatar
Kevin Bracey committed
1130 1131 1132 1133

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

; catch remaining cases and pass into device driver
Stewart Brodie's avatar
Stewart Brodie committed
1139
05
Neil Turton's avatar
Neil Turton committed
1140 1141 1142 1143 1144 1145
                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
1146
                LDR     r2, [fr, #file_InternalHandle]
Neil Turton's avatar
Neil Turton committed
1147 1148
                BL      CallDevice

Kevin Bracey's avatar
Kevin Bracey committed
1149
10
Neil Turton's avatar
Neil Turton committed
1150
                STRVS   r0, [sp]
Stewart Brodie's avatar
Stewart Brodie committed
1151
                EXIT
Neil Turton's avatar
Neil Turton committed
1152

Kevin Bracey's avatar
Kevin Bracey committed
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167

; 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
1168
		MOV	pc, lr				; 0 nothing
Kevin Bracey's avatar
Kevin Bracey committed
1169
		B	ioctl_miscop_nonblock		; 1 set non-blocking I/O
1170 1171 1172 1173
                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
1174 1175
20
                PullEnv
Stewart Brodie's avatar
Stewart Brodie committed
1176
                MOVEQ   pc, lr                          ; V must be clear to get here
Kevin Bracey's avatar
Kevin Bracey committed
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
		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
1206 1207
                CLRV
		MOV	pc, lr
Kevin Bracey's avatar
Kevin Bracey committed
1208

1209 1210
ioctl_miscop_sleep
                ROUT
Dan Ellis's avatar
Dan Ellis committed
1211
		LDR	r0, [r2, #0]			; load ioctl reason codes and flags
1212 1213 1214 1215 1216 1217 1218 1219 1220

		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
1221 1222
                MOVEQ   r1, #ff_DontSleep               ; sleeping disabled
                MOVNE   r1, #ff_WakeUp                  ; sleeping enabled
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
		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
1238
		LDR	r0, [r2, #0]			; load ioctl reason codes and flags
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255

		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
1256

Neil Turton's avatar
Neil Turton committed
1257 1258
; handle checking of the file extent

1259
args_ext	Entry	"r0-r1,r3-r6, fr"
Neil Turton's avatar
Neil Turton committed
1260 1261

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

Neil Turton's avatar
Neil Turton committed
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
                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
1281
                MOVVC   r2, r6
Neil Turton's avatar
Neil Turton committed
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
 ]
		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
1295
                MOVVC	r2, r5
Neil Turton's avatar
Neil Turton committed
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318
 ]
		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
1319

1320
checkeof        Entry   "r2"                                    ; internal routine,
Neil Turton's avatar
Neil Turton committed
1321 1322 1323 1324 1325
                BL      args_eof
                CMPVC   r2, #1                                  ; exits CS for EOF, else CC
                EXIT


1326
args_eof        Entry   "r0,r1,r3-r6, fr"
Neil Turton's avatar
Neil Turton committed
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345

                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
1346
                EXIT    VS
Neil Turton's avatar
Neil Turton committed
1347 1348 1349 1350 1351 1352 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

 [ 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
1380 1381
                CLRV
                EXIT
Neil Turton's avatar
Neil Turton committed
1382 1383 1384 1385 1386 1387


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

1388
args_flush      Entry   "r1-r3,fr"
Neil Turton's avatar
Neil Turton committed
1389