FileCore80 156 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 24 25
; 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.
;
;>FileCore80


        TTL     "FileCore80 - random access file cache"

; >>>>>>>>>>>>>
; GetBytesEntry
; >>>>>>>>>>>>>

; entry:
;  R1   file handle
26 27 28
;  R2   start RAM address (no alignment guarantee)
;  R3   number of bytes to read (FileSwitch buffer multiple)
;  R4   file address (FileSwitch buffer multiple)
Neil Turton's avatar
Neil Turton committed
29 30 31 32 33 34 35 36 37

; exit:
;  R1-R4 ?
; V clear
;  R0   ?
; V set
;  R0   -> error block

GetBytesEntry ROUT
38
        SemEntry  Flag          ;allow reentrance, leaves R12,LR stacked
Neil Turton's avatar
Neil Turton committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
 [ DebugB :LOR: DebugBE
        DLINE   "handle  ,start   ,length  ,file ptr >GetBytesEntry"
        DREG    R1,,cc
        DREG    R2,,cc
        DREG    R3,,cc
        DREG    R4
 ]
 [ DebugBt
        DLINE   ">G",cc
 ]
 [ Debug3L
        DLINE   "G",cc
 ]
        Push    "R5-R11"
        BL      HandleCheck     ;(R1->R0,R1,LR,V)
        BVS     %FT00

56 57 58 59 60 61 62
      [ BigFiles
        ; If transfer ends at exactly 4G, use foreground
        ; If transfer ends near 4G, use foreground
        ADDS    R5, R3, R4
        CMPCC   R5, #-1*K
        BCS     %FT03
      ]
Neil Turton's avatar
Neil Turton committed
63

64
        ; Do in background if got some file buffers
Neil Turton's avatar
Neil Turton committed
65 66 67
        LDRB    R5, MaxFileBuffers
        TEQS    R5, #0
        BNE     %FT05
68 69 70 71 72
03
        ; Foreground
        MOV     R5, R4
        MOV     R4, R3
        MOV     R3, R2
Neil Turton's avatar
Neil Turton committed
73
        LDR     R2, [R1,#FcbIndDiscAdd]
74
        MOV     R1, #DiscOp_ReadSecs :OR: DiscOp_Op_IgnoreEscape_Flag
75
        BL      GenIndDiscOp    ;(R1-R5->R0-R5,V)
Neil Turton's avatar
Neil Turton committed
76 77 78 79 80 81 82 83 84 85 86
00
        BLVS    FindErrBlock    ; (R0->R0,V)
        BL      FileCoreExit
 [ Debug3L
        DLINE   "g",cc
 ]
 [ DebugBt
        DLINE   " <G"
 ]
 [ DebugB :LOR: DebugBE
        DLINE   "<GetBytes"
87
        DebugError "GetBytes error"
Neil Turton's avatar
Neil Turton committed
88 89 90 91 92 93 94 95 96 97
 ]
        Pull    "R5-R11,SB,PC"

;Stack workspace for read buffers

                ^ 0
PendingReadStart # 4
PendingReadEnd  # 4
ReadRamAdjust   # 4
ReadDiscRec     # 4
98
ReadCylLength   # 4             ;bytes per cylinder [BigDisc it's sectors]
Neil Turton's avatar
Neil Turton committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112
ReadProcessBlock  # 4
LastDiscAdjust  # 4
LastType        # 4
TrkReadAheadLimit * LastType    ;shared
LastEnd         # 4
SeqReadAheadLimit * LastEnd     ;shared
LastStart       # 4
ReadBuffersWork # 0

; R1 FCB
; R2 RAM PTR
; R3 LENGTH
; R4 FILE PTR
; LR PREVIOUS FCB
113
05
Neil Turton's avatar
Neil Turton committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
        MOV     R8, LR
 [ DebugJ
        BL      Check
 ]
 [ DebugH
        DLINE   "R ",cc         ;Read
        DREG    R4,,cc
 ]

        MOV     R0, #1
        BL      GetPutCommon ;R0-R4->R0,R2,R3,BufSz,FileOff,R6,TransferEnd,Fcb,R10,Z,V
        BVS     %BT00
 [ DebugB
        CPBs
 ]

130 131
        LDRB    R11,[R6,#DiscRecord_SecsPerTrk]
        LDRB    LR, [R6,#DiscRecord_Heads]
132
 [ BigDisc
133
        MUL     LR, R11,LR      ; sectors per cylinder
Neil Turton's avatar
Neil Turton committed
134 135
 |
        MOV     R11,R11,LSL R10
136
        MUL     LR, R11,LR      ; bytes per cylinder - need to change to sectors?
Neil Turton's avatar
Neil Turton committed
137 138
 ]

139 140 141 142 143 144 145 146 147
        ; Build read buffers workspace on the stack
        MOV     R1, #0
        STR     R1, DirectError
        MOV     R1, #bit31      ; LastType = NOP
        ASSERT  LastType > ReadProcessBlock
        STR     R1, [SP,#LastType-ReadBuffersWork] !
        STR     R0, [SP,#ReadProcessBlock-LastType] !
        MOV     R1, #bit1       ; PendindReadEnd

Neil Turton's avatar
Neil Turton committed
148 149 150 151 152
        ASSERT  PendingReadEnd = 4
        ASSERT  ReadRamAdjust = PendingReadEnd+4
        ASSERT  ReadDiscRec = ReadRamAdjust+4
        ASSERT  ReadCylLength = ReadDiscRec+4
        ASSERT  ReadProcessBlock = ReadCylLength+4
153
        Push    "R0-R2,R6,LR"   ; note R0 is junk value for PendingReadStart
Neil Turton's avatar
Neil Turton committed
154 155 156 157

;DEAL WITH SEQUENTIAL / NON SEQUENTIAL
        BL      ClaimFileCache
        LDRB    R1, [Fcb,#FcbFlags]
158 159 160 161 162 163
        AND     LR, R1, #Sequential
        CMP     LR, #0                  ;C<=>sequential
        LDR     LR, [Fcb,#FcbLastReadEnd]
        TEQS    LR, FileOff             ;does read start = last end ?
        BEQ     %FT14                   ;yes, so it can be sequential
        BCC     %FT16                   ;no, and CC, it wasn't sequential previously anyway
Neil Turton's avatar
Neil Turton committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

;HERE WHEN CONVERTING A FILE FROM SEQUENTIAL TO NON SEQUENTIAL
 [ DebugG
        DLINE   "end sequential"
 ]
        BIC     R1, R1, #Sequential
        STRB    R1, [Fcb,#FcbFlags]

;move position of Fcb in chain to be after all sequential files
        LDR     R1, [Fcb, #FcbNext]
        MOV     R2, Fcb                 ;will be last sequential
        MOV     R10,R1                  ;will be first non sequential
08
        CMPS    R10,#-1
        LDRNEB  LR, [R10,#FcbFlags]
        TSTNES  LR, #Sequential
        MOVNE   R2, R10
        LDRNE   R10,[R10,#FcbNext]
        BNE     %BT08

        TEQS    R2, Fcb                 ;nothing to do if already last sequential
        STRNE   R1, [R8, #FcbNext]
        STRNE   R10,[Fcb,#FcbNext]
        STRNE   Fcb,[R2, #FcbNext]

        LDR     R8, =AwaitsSeqRead*&1010101
        MOV     BufPtr, Fcb
        BL      LessValid
        B       %FT12
10
        LDR     R2, [BufPtr,#BufFlags]
        ANDS    LR, R2, R8                      ;convert any sub buffers awaiting
        ASSERT  NormalBuf=AwaitsSeqRead :SHR: 1 ;sequential read to normal
        SUBNE   R2, R2, LR, LSR #1
        BLNE    UpdateBufState  ;R2,BufSz,BufPtr
12
        LDR     BufPtr, [BufPtr,#NextInFile]
        TEQS    BufPtr, Fcb
        BNE     %BT10   ;loop if more buffers for this file
203

Neil Turton's avatar
Neil Turton committed
204
        BL      MoreValid
205
        LDRB    R8, [Fcb,#FcbRdAheadBufs]        ;halve read ahead
Neil Turton's avatar
Neil Turton committed
206
        MOVS    R8, R8, LSR #1
207
        STRNEB  R8, [Fcb,#FcbRdAheadBufs]
Neil Turton's avatar
Neil Turton committed
208 209 210
        B       %FT16

14
211
;READ AHEAD (BECOMES) SEQUENTIAL
Neil Turton's avatar
Neil Turton committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
        ORRCC   R1, R1, #Sequential
        STRCCB  R1, [Fcb,#FcbFlags]

        sbaddr  R6, FirstFcb-FcbNext    ;promote to start of Fcb chain if sequential
        TEQS    R6, R8                  ;already at start ?
        LDRNE   R1, [R6, #FcbNext]
        LDRNE   LR, [Fcb,#FcbNext]
        STRNE   LR, [R8, #FcbNext]
        STRNE   R1, [Fcb,#FcbNext]
        STRNE   Fcb,[R6, #FcbNext]

16
;CHECK IF WE HAVE WHOLE READ ALREADY CACHED
 [ DebugB
        CPBs
 ]
        MOV     R6, FileOff
        LDR     BufPtr,[Fcb,#PrevInFile]
        BL      FindSubBuf      ;FileOff,Fcb,BufPtr -> BufOff,BufPtr
        MOV     FragEnd, TransferEnd
232
        BL      ScanBuffers     ;FileOff,FragEnd,BufSz,BufOff,BufPtr->FileOff,BufOff,BufPtr, LO/EQ/HI
Neil Turton's avatar
Neil Turton committed
233 234 235 236
        BHS     %FT17
        TEQS    FileOff, FragEnd
        BNE     %FT17

237
        MOV     FileOff, R6     ;Restore FileOff
Neil Turton's avatar
Neil Turton committed
238 239 240 241 242 243 244 245 246 247
        MOV     R2, TransferEnd
        LDR     R1, [SP,#ReadRamAdjust]
        BL      BufsToRam       ;R1,R2,BufSz,FileOff,Fcb
        BL      ReleaseFileCache
        LDR     R1, FloppyProcessBlk
        CMPS    R0, R1          ;V=0
        MOVEQ   R1, #FloppyLock
        MOVNE   R1, #WinnieLock
        BL      BackgroundOps   ;(R0,R1->R3-R11)
        ADD     SP, SP, #ReadBuffersWork
248
        B       %BT00           ;Exit (checking V)
Neil Turton's avatar
Neil Turton committed
249 250

17
251
; SCHEDULE A NEW READ TRANSFER
Neil Turton's avatar
Neil Turton committed
252 253 254
 [ DebugB
        CPBs
 ]
255
        MOV     FileOff, R6     ;Restore FileOff
Neil Turton's avatar
Neil Turton committed
256 257 258 259 260 261 262
        BL      ReleaseFileCache
        BL      DiscWriteBehindWait                  ;(R3)
 [ DebugB
        CPBs
 ]
        BL      ClaimFileCache
        LDR     BufPtr,[Fcb,#PrevInFile]
263
        STR     TransferEnd, [Fcb,#FcbLastReadEnd]
Neil Turton's avatar
Neil Turton committed
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278

;CONSIDER WHETHER CURRENT READ AHEAD IF ANY COULD BE EXTENDED FOR THIS REQUEST

        BL      ClaimController ;(R0)
        LDRB    LR, [R0,#Process]
        TEQS    LR, #ReadAhead
        LDREQ   LR, [R0,#ProcessFcb]
        TEQEQS  LR, Fcb
        LDREQ   LR, [R0,#ProcessStatus]
        ANDEQ   LR, LR, #CanExtend
        TEQEQS  LR, #CanExtend
        BNE     %FT20           ;If not reading ahead or not able to extend

 [ DebugB
        CPBs
279
        DLINE   "GetBytesEntry : reading ahead and can extend"
Neil Turton's avatar
Neil Turton committed
280 281 282 283 284 285 286 287 288 289 290 291
 ]
        LDR     FileOff, [R0,#ProcessEndOff]
        CMPS    FileOff, R6
        CMPHS   TransferEnd, FileOff
        LDRHI   FragEnd, [R0,#ProcessFragEnd]
        CMPHIS  FragEnd, FileOff
        BLS     %FT18           ;IF would not help request

        BL      FindSubBuf      ;FileOff,Fcb,BufPtr -> BufOff,BufPtr
        MOV     R2, FileOff
        CMPS    TransferEnd, FragEnd
        MOVLO   FragEnd, TransferEnd
292
        BL      ScanBuffers     ;FileOff,FragEnd,BufSz,BufOff,BufPtr->FileOff,BufOff,BufPtr, LO/EQ(/HI)
Neil Turton's avatar
Neil Turton committed
293 294 295 296
        BNE     %FT18           ;(R0-R2->Z)

        LDR     R1, [SP,#ReadRamAdjust]
        BL      AddPair         ;(R0,R1,FileOff->C)
297
        BCC     %FT18
Neil Turton's avatar
Neil Turton committed
298 299 300
 [ DebugH
        DLINE   "RFX ",cc       ;Read Foreground eXtend
 ]
301
        BL      IncReadAhead    ;(Fcb)
Neil Turton's avatar
Neil Turton committed
302 303
        ASSERT  PendingReadStart=0
        ASSERT  PendingReadEnd=4
304 305
        STMIA   SP, {FileOff,TransferEnd}
        MOV     TransferEnd,R2
Neil Turton's avatar
Neil Turton committed
306
18
307
        MOV     FileOff,R6      ;Restore FileOff
Neil Turton's avatar
Neil Turton committed
308 309 310
20
        BL      UpdateProcesses
        BL      BeforeReadFsMap ;(R3->R0,V)
311
        BVS     %FT70           ;Error exit, map not locked
Neil Turton's avatar
Neil Turton committed
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
;LOOP TO DO TWO PARTS OF READ IF SPLIT BY READ AHEAD TO MIDDLE
22
 [ DebugB
        CPBs
 ]
        CMPS    FileOff,TransferEnd
        BHS     %FT32
        BL      FindSubBuf      ;FileOff,Fcb,BufPtr -> BufOff,BufPtr
        BL      DefFileFrag     ;FileOff,Fcb -> R0,DiscAdjust,FragEnd
        CMPS    FragEnd, TransferEnd
        MOVHI   FragEnd, TransferEnd

;LOOP TO DEAL WITH FRAGMENTS
24
        CMPS    FileOff,FragEnd
327 328 329
        BLO     %FT25
        BL      DefFileFrag     ;FileOff,Fcb -> R0,DiscAdjust,FragEnd
        CMPS    FragEnd, TransferEnd
Neil Turton's avatar
Neil Turton committed
330
        MOVHI   FragEnd, TransferEnd
331
25
Neil Turton's avatar
Neil Turton committed
332
        Push    "FileOff"
333
        BL      ScanBuffers     ;FileOff,FragEnd,BufSz,BufOff,BufPtr->FileOff,BufOff,BufPtr,LR, LO/EQ/HI
Neil Turton's avatar
Neil Turton committed
334 335 336 337 338 339 340 341 342 343 344 345 346
        Push    "FileOff,TransferEnd,FragEnd,BufOff,BufPtr"
        MOV     R3, FileOff
        LDR     FileOff,[SP,#5*4]       ;old FileOff

        ASSERT  LastType=LastDiscAdjust+4
        ASSERT  LastEnd=LastType+4
        ASSERT  LastStart=LastEnd+4

        ASSERT  TransferEnd=R7
        ASSERT  FragEnd=R8
        ASSERT  BufOff =R10
        ASSERT  BufPtr =R11
        ADD     R7, SP, #6*4+LastDiscAdjust
347
        LDMIA   R7, {R7,R8,R10,R11}     ; aka {LastDiscAdjust,LastType,LastEnd,LastStart}
Neil Turton's avatar
Neil Turton committed
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
 [ DebugG
        DREG    R7,,cc
        DREG    R8,,cc
        DREG    R10,,cc
        DREG    R11,,cc
        DLINE   "Last"
 ]
        MOVLO   R2, R3
        BLO     %FT28                   ;cached

        MOVHI   R2, #ReadAhead
        MOVEQ   R2, #EmptyBuf

 [ DebugB
        CPBs
 ]
        STR     DiscAdjust, [SP, #6*4+LastDiscAdjust]
        ADD     LR, SP, #6*4+LastType
366 367 368
        STMIA   LR, {R2,R3,FileOff}     ; aka {LastType,LastEnd,LastStart}
        TSTS    R8, #bit31              ; check last type
        BMI     %FT30                   ; no last op
Neil Turton's avatar
Neil Turton committed
369 370 371 372 373 374 375 376
        TSTS    R8, #ReadAhead
        BNE     %FT26

;PREVIOUS EMPTY
 [ DebugH
        DLINE   "RPE ",cc         ;Read Previous Empty
 ]
        Push    "R4"
377
        MOV     R1, #DiscOp_ReadSecs :OR: DiscOp_Op_IgnoreEscape_Flag
Neil Turton's avatar
Neil Turton committed
378 379
 [ BigDisc
        LDR     LR, [Fcb, #FcbIndDiscAdd]
380
        MOV     LR, LR, LSR #(32-3)
381 382
        DiscRecPtr LR,LR                ; disc record pointer
        LDRB    LR, [LR,#DiscRecord_Log2SectorSize]    ; sector size
Neil Turton's avatar
Neil Turton committed
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
        ADD     R2, R7, R11, LSR LR     ; disc address
 |
        ADD     R2, R11,R7      ;disc address
 ]
        LDR     LR, [SP, #(1+6)*4 + ReadRamAdjust]
        ADD     R3, R11,LR      ;RAM ptr
        SUB     R4, R10,R11     ;length
 [ Debug3L
        DLINE   "H",cc
 ]
        BL      DoDiscOp        ;(R1-R4->R0,R2-R4,V)
 [ Debug3L
        DLINE   "h",cc
 ]
        Pull    "R4",VC
        BVC     %FT30
        ADDVS   SP, SP, #(1+6)*4
400
        BVS     %FT68           ;error exit, map needs unlocking
Neil Turton's avatar
Neil Turton committed
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418

26
;PREVIOUS READING AHEAD
 [ DebugH
        DLINE   "RPR ",cc         ;Read Previous Reading Ahead
 ]
        BL      IncReadAhead            ;(Fcb)
        LDR     R0, [SP,#6*4+ReadProcessBlock]
        BL      WaitForControllerFree   ;(R0)

        LDR     R0, [R0, #ProcessError]
        TEQS    R0, #0
        ADDNE   SP, SP, #6*4
        BNE     %FT66

        MOV     FileOff,R11
        MOV     R2, R10
28
419
; GOT SOMETHING, COPY TO USER RAM
Neil Turton's avatar
Neil Turton committed
420 421 422 423 424 425 426 427 428 429 430 431
        LDR     R1, [SP,#6*4+ReadRamAdjust]
        BL      BufsToRam       ;R1,R2,BufSz,FileOff,Fcb

30
        Pull    "FileOff,TransferEnd,FragEnd,BufOff,BufPtr"
        ADD     SP, SP, #4
        CMPS    FileOff, TransferEnd
        BLO     %BT24

32
        ASSERT  PendingReadStart=0
        ASSERT  PendingReadEnd=4
432 433
        LDMIA   SP, {FileOff,TransferEnd}     ; aka {PendingReadStart,PendingReadEnd}
        MOV     LR, #bit1
Neil Turton's avatar
Neil Turton committed
434
        STR     LR, [SP,#PendingReadEnd]
435
        TEQS    TransferEnd, #bit1            ; flag not set, go round again
Neil Turton's avatar
Neil Turton committed
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
        BNE     %BT22
        LDR     LR, [SP,#LastType]
        LDR     DiscAdjust, [SP,#LastDiscAdjust]
        TSTS    LR, #bit31
        BMI     %FT36
        TSTS    LR, #ReadAhead
        BNE     %FT40

;FINAL EMPTY
 [ DebugH
        DLINE   "RFE ",cc         ;Read Final Empty
 ]
        LDR     FileOff, [SP,#LastEnd]
        BL      FindSubBuf      ;FileOff,Fcb,BufPtr -> BufOff,BufPtr
        SUB     FileOff, FileOff, #1
        BL      DefFileFrag     ;FileOff,Fcb -> R0,DiscAdjust,FragEnd
        Push    "FragEnd"
        ADD     FileOff, FileOff, #1
        MOV     R3, R0          ;Frag start
        MOV     TransferEnd, FileOff

        Push    "FileOff,BufOff,BufPtr"
458 459
        BL      SkipEmpty       ;BufSz,FileOff,FragEnd,BufOff,BufPtr->FileOff,BufOff,BufPtr

Neil Turton's avatar
Neil Turton committed
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
        CMPS    FileOff, FragEnd
        MOVLO   FragEnd, FileOff
        LDMIA   SP, {FileOff,BufOff,BufPtr}

;round lower bound above first cached sub buf below read end
34
        SUBS    BufOff,BufOff,BufSz
        ADDMI   BufOff,BufOff,#32
        LDRMI   BufPtr,[BufPtr,#PrevInFile]
        ASSERT  BufFlags=0
        LDRB    LR, [BufPtr,BufOff,LSR #3]
        TSTS    LR, #EmptyBuf
        BNE     %BT34
        LDR     R0, [BufPtr,#BufFileOff]
        ADD     R0, R0, BufOff, LSL #5
        ADD     R0, R0, BufSz, LSL #5
        Pull    "FileOff,BufOff,BufPtr"
        CMPS    R0, R3
478
        MOVHI   R3, R0
Neil Turton's avatar
Neil Turton committed
479 480 481 482 483 484

;round lower bound above cylinder boundary below read end, old map winnie
;defect skipping may give wrong answer

 [ BigDisc
        SUB     R2, FileOff, #1
485

Neil Turton's avatar
Neil Turton committed
486
        LDR     LR, [Fcb, #FcbIndDiscAdd]
487
        MOV     LR, LR, LSR #(32-3)     ; reassess disc (DiscAdjust may contain incorrect number)
488
        DiscRecPtr LR,LR
489

490
        LDRB    LR, [LR, #DiscRecord_Log2SectorSize]
Neil Turton's avatar
Neil Turton committed
491

492
        MOV     R2, R2, LSR LR
Neil Turton's avatar
Neil Turton committed
493 494
        ADD     R0, DiscAdjust, R2
        BIC     R0, R0, #DiscBits
495
        LDR     R1, [SP,#4+ReadCylLength]
496
        Push    "LR"
497 498
        BL      Divide                  ; r0 := disc address / cylinder size
        Pull    "LR"                    ; r1 := remainder
Neil Turton's avatar
Neil Turton committed
499 500
        SUBS    R0, R2, R1
        MOVMI   R0, #0
501
        MOV     R0, R0, LSL LR
Neil Turton's avatar
Neil Turton committed
502 503 504 505 506 507 508 509 510
 |
        SUB     R2, FileOff, #1
        ADD     R0, R2, DiscAdjust
        BIC     R0, R0, #DiscBits
        LDR     R1, [SP,#4+ReadCylLength]
        BL      Divide
        SUBS    R0, R2, R1
        MOVMI   R0, #0
 ]
511
 [ {FALSE}
Neil Turton's avatar
Neil Turton committed
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
        BIC     R0, R0, #&3FC           ;force to 1K boundary for add buffer
 ]
        CMPS    R0, R3
        MOVHI   R3, R0

        ANDS    LR, TransferEnd, #&3FC  ;extend end to 1K boundary
        SUB     R1, TransferEnd, LR
        ADDNE   R1, R1, #1*K
        MOV     R2, R1
        MOVNE   R0, #NormalChain*2
 [ DebugG
        BEQ     %FT01
        DLINE   "Extend up to 1K boundary"
01
 ]
        BLNE    ExtendUp                ;(R0,R1,TransferEnd,FragEnd->R1,TransferEnd)

        MOV     R1, R2
        LDRB    R2, [Fcb,#FcbFlags]     ;if reading sequentially try to add read ahead
        TSTS    R2, #Sequential

        MOVNE   R0, #AwaitsSeqChain*2-1
534
        LDRNEB  LR, [Fcb,#FcbRdAheadBufs]
Neil Turton's avatar
Neil Turton committed
535 536 537 538 539 540 541 542 543 544 545 546 547 548
        ADDNE   R1, R1, LR, LSL #10
 [ DebugG
        BEQ     %FT01
        DLINE   "Extend for sequential"
01
 ]
        BLNE    ExtendUp                ;(R0,R1,TransferEnd,FragEnd->R1,TransferEnd)

 [ DebugB
        CPBs
 ]
        SUB     R1, FileOff, #&100
        BICS    R1, R1, #&3FC           ;IF first buffer (so small files get cached)
        TSTNES  R2, #Monotonic          ;OR not monotonic
549 550 551
        BNE     %FT35

        MOV     R0, #NormalChain*2      ;THEN extend down to 1K boundary
Neil Turton's avatar
Neil Turton committed
552 553 554
 [ DebugG
        DLINE   "Extend down to 1K boundary"
 ]
555
        BL      ExtendDown              ;(R0,R1,R3,FileOff->R1,FileOff)
Neil Turton's avatar
Neil Turton committed
556

557 558 559
        MOV     R0, #MonotonicChain*2
        LDR     R1, [SP,#4+LastStart]
        BIC     R1, R1, #&300
Neil Turton's avatar
Neil Turton committed
560 561 562
 [ DebugG
        DLINE   "Extend down to start"
 ]
563 564
        BL      ExtendDown              ;(R0,R1,R3,FileOff->R1,FileOff)
35
Neil Turton's avatar
Neil Turton committed
565
 [ BigDisc
566
        LDR     LR, [Fcb, #FcbIndDiscAdd]
567
        MOV     LR, LR, LSR #(32-3)     ; reassess disc (DiscAdjust may contain incorrect number)
568
        DiscRecPtr LR,LR
569

570
        LDRB    LR, [LR, #DiscRecord_Log2SectorSize]
Neil Turton's avatar
Neil Turton committed
571 572 573 574

        ADD     R0, DiscAdjust, TransferEnd, LSR LR
        BIC     R0, R0, #DiscBits
        LDR     R1, [SP,#4+ReadCylLength]
575
        Push    "LR"
576 577
        BL      Divide                  ; r0 := disc address / cylinder size
        Pull    "R0"                    ; r1 := remainder
Neil Turton's avatar
Neil Turton committed
578 579 580 581 582 583 584 585 586 587 588 589 590 591
        TEQS    R1, #0
        SUB     R1, TransferEnd, R1, LSL R0
        LDRNE   LR, [SP,#4+ReadCylLength]
        ADDNE   R1, R1, LR, LSL R0
 |
        ADD     R0, TransferEnd, DiscAdjust
        BIC     R0, R0, #DiscBits
        LDR     R1, [SP,#4+ReadCylLength]
        BL      Divide
        TEQS    R1, #0
        SUB     R1, TransferEnd, R1
        LDRNE   LR, [SP,#4+ReadCylLength]
        ADDNE   R1, R1, LR
 ]
592
 [ {FALSE}
Neil Turton's avatar
Neil Turton committed
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
        BIC     R1, R1, #&3FC           ;force to 1K boundary for add buffer
 ]
        MOV     R0, #MonotonicChain*2
 [ DebugG
        DLINE   "Extend up to cylinder end"
 ]
        BL      ExtendUp                ;(R0,R1,TransferEnd,FragEnd->R1,TransferEnd)

        TSTS    R2, #Monotonic
        MOVEQ   R0, #MonotonicChain*2
        MOVEQ   R1, R3
 [ DebugG
        BNE     %FT01
        DLINE   "Extend down to cylinder start"
01
 ]
        BLEQ    ExtendDown              ;(R0,R1,R3,FileOff->R1,FileOff)

        Pull    "FragEnd"
        LDR     R0, [SP,#ReadProcessBlock]
        LDR     LR, [SP,#LastStart]

        SUBS    R2, FileOff, LR
        MOVHI   FileOff, LR
        LDRHI   R1, [SP,#ReadRamAdjust]
        LDRLS   R1, [SP,#LastDiscAdjust]
        MOVLS   R2, #0
620
        MOV     R3, #DiscOp_ReadSecs
Neil Turton's avatar
Neil Turton committed
621 622
        LDR     R6, [SP, #LastEnd]
        SUB     R6, R6, FileOff
623
        BL      BackgroundFileCacheOp1  ;R0-R3,FileOff,R6,TransferEnd,FragEnd,Fcb,BufPtr -> R1,R3,R10,V
Neil Turton's avatar
Neil Turton committed
624 625
        BL      UpdateProcesses
        MOVVS   R0, R3
626
        BVS     %FT68                   ;error exit, map needs unlocking
Neil Turton's avatar
Neil Turton committed
627 628 629 630 631 632 633
 [ DebugB
        CPBs
 ]

        ADD     FileOff, FileOff, R2    ;end of direct read
        ASSERT  LastStart=LastEnd+4
        ADD     R2, SP, #LastEnd
634
        LDMIA   R2, {R2,LR}             ; aka {LastStart,LastEnd}
Neil Turton's avatar
Neil Turton committed
635 636 637 638 639
        CMPS    FileOff, LR
        MOVLO   FileOff, LR
        CMPS    R2, FileOff
        LDRHI   R1, [SP, #ReadRamAdjust]
        BLHI    BufsToRam               ;(R1,R2,BufSz,FileOff,Fcb)
640 641
        CLRV                            ; incase CMPs above set it
        B       %FT68                   ; exit, map needs unlocking
Neil Turton's avatar
Neil Turton committed
642

643 644
36
;MANAGED TO SATISY READ ONLY FROM BUFFERS AND/OR EXTENDING READ AHEAD
Neil Turton's avatar
Neil Turton committed
645 646 647 648 649 650 651 652 653 654 655 656 657
 [ DebugH
        DLINE   "RFC ",cc
 ]
 [ DebugB
        CPBs
 ]
        LDRB    LR, [Fcb,#FcbFlags]
        ANDS    R0, LR, #Sequential
        BEQ     %FT64

        BL      UnlockMap
        BL      ReleaseFileCache
        LDR     R0, [SP,#ReadProcessBlock]
658
        BL      ReleaseController       ;(R0)
Neil Turton's avatar
Neil Turton committed
659 660 661 662 663 664 665 666 667 668 669
        LDR     R1, FloppyProcessBlk
        TEQS    R0, R1
        MOVEQ   R1, #FloppyLock
        MOVNE   R1, #WinnieLock
        BL      BackgroundOps   ;(R0,R1->R3-R11)
        BL      ClaimController ;(R0)
38
        LDRB    LR, [R0,#ProcessDirect]
        TEQS    LR, #0
        BLNE    UpdateProcesses
        BNE     %BT38
670
        BL      ReleaseController       ;(R0)
Neil Turton's avatar
Neil Turton committed
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
        LDR     R0, DirectError
        BL      SetVOnR0
        ADD     SP, SP, #ReadBuffersWork
        B       %BT00

;FINAL READ AHEAD
40
 [ DebugH
        DLINE   "RFR ",cc         ;Read Final Reading ahead
 ]
 [ DebugB
        CPBs
 ]
        BL      IncReadAhead            ;(Fcb)
        LDR     R0, [SP, #ReadProcessBlock]
        LDR     FragEnd, [R0, #ProcessFragEnd]
        LDR     TransferEnd, [SP,#LastEnd]
        LDR     FileOff, [R0, #ProcessEndOff]
        MOV     R3, FileOff
        BL      FindSubBuf      ;FileOff,Fcb,BufPtr -> BufOff,BufPtr
691
        BL      SkipEmpty       ;BufSz,FileOff,FragEnd,BufOff,BufPtr->FileOff,BufOff,BufPtr
Neil Turton's avatar
Neil Turton committed
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
        MOV     FragEnd, FileOff
        CMPS    FileOff, R3
        STRLS   R3, [SP,#SeqReadAheadLimit]
        STRLS   R3, [SP,#SeqReadAheadLimit]
        BLS     %FT44                   ;no scope for extending

        MOV     FileOff, R3
        LDRB    LR, [Fcb, #FcbFlags]
        MOV     R1, TransferEnd
        TSTS    LR, #Sequential
        BEQ     %FT42

        TSTS    R1, #&3FC
        BICNE   R1, TransferEnd, #&3FC
        ADDNE   R1, R1, #1*K
707
        LDRB    LR, [Fcb,#FcbRdAheadBufs]
Neil Turton's avatar
Neil Turton committed
708 709 710 711 712 713 714 715 716 717 718 719 720
        ADD     R1, R1, LR, LSL #10
        CMPS    R1, FragEnd
        MOVHI   R1, FragEnd

42
 [ DebugB
        CPBs
 ]

 [ BigDisc
 ; again, we have to round to a cylinder

        STR     R1, [SP,#SeqReadAheadLimit]
721

722
        LDR     LR, [Fcb, #FcbIndDiscAdd]
723
        MOV     LR, LR, LSR #(32-3)     ; reassess disc (DiscAdjust may contain incorrect number)
724
        DiscRecPtr LR,LR
Neil Turton's avatar
Neil Turton committed
725

726
        LDRB    LR, [LR, #DiscRecord_Log2SectorSize]
Neil Turton's avatar
Neil Turton committed
727
        SUB     FileOff, FileOff, #1
728
        MOV     R2, FileOff, LSR LR
Neil Turton's avatar
Neil Turton committed
729 730 731 732

        ADD     R0, R2, DiscAdjust
        BIC     R0, R0, #DiscBits
        LDR     R1, [SP,#ReadCylLength]
733
        Push    "LR"
734 735
        BL      Divide                  ; r0 := disc address / cylinder size
        Pull    "LR"                    ; r1 := remainder
Neil Turton's avatar
Neil Turton committed
736 737 738
        SUB     R2, R2, R1
        LDR     R1, [SP,#ReadCylLength]
        ADD     R2, R2, R1
739
        MOV     LR, R2, LSL LR
Neil Turton's avatar
Neil Turton committed
740
        CMPS    LR, FragEnd
741
        MOVHI   LR, FragEnd
Neil Turton's avatar
Neil Turton committed
742 743 744 745 746 747 748 749 750 751 752 753 754 755

 |

        STR     R1, [SP,#SeqReadAheadLimit]

        SUB     FileOff, FileOff, #1
        ADD     R0, FileOff, DiscAdjust
        BIC     R0, R0, #DiscBits
        LDR     R1, [SP,#ReadCylLength]
        BL      Divide
        SUB     LR, FileOff, R1
        LDR     R1, [SP,#ReadCylLength]
        ADD     LR, LR, R1
        CMPS    LR, FragEnd
756
        MOVHI   LR, FragEnd
Neil Turton's avatar
Neil Turton committed
757
 ]
758
 [ {FALSE}
Neil Turton's avatar
Neil Turton committed
759 760 761 762 763 764 765 766 767
        BIC     LR, LR, #&3FC           ;force to 1K boundary for add buffer
 ]
        STR     LR, [SP,#TrkReadAheadLimit]

        LDR     R0, [SP, #ReadProcessBlock]
44
        BL      UpdateProcess   ;(R0)
;LOOP

768
;COPY ANY DATA THAT HAS APPEARED FROM READ AHEAD
Neil Turton's avatar
Neil Turton committed
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 820 821 822 823 824 825 826 827 828 829 830 831
 [ DebugB
        CPBs
 ]
        LDR     FileOff, [SP, #LastStart]
50
        LDR     R2, [R0, #ProcessStartOff]
        CMPS    R2, TransferEnd
        MOVHI   R2, TransferEnd
        CMPS    R2, FileOff
        LDRHI   R1, [SP, #ReadRamAdjust]
        BLHI    BufsToRam       ;(R1,R2,BufSz,FileOff,Fcb)
        STRHI   R2, [SP, #LastStart]

;ADD BUFFERS
        LDRB    LR, [R0,#Process]
        ASSERT  ReadAhead>Inactive
        ASSERT  WriteBehind>Inactive
        CMPS    LR, #Inactive+1
        BLO     %FT62                           ;process finished
        LDR     FileOff, [R0, #ProcessEndOff]
        BL      LessValid
52
 [ DebugB
        CPBs
 ]
        LDR     LR, [R0, #ProcessStatus]
        TSTS    LR, #CanExtend
        BEQ     %FT60
        SUBS    R3, FragEnd, FileOff            ;if extended to frag end
        BLS     %FT60                           ;don't consider more extension
        BL      FindSubBuf      ;FileOff,Fcb,BufPtr -> BufOff,BufPtr
        CMPS    BufOff, #32
        LDRLO   R2, [BufPtr,#BufFlags]
        MOVLO   R6, #-1
        BLO     %FT56
        LDR     R3, [SP, #SeqReadAheadLimit]
        SUBS    R3, R3, FileOff
        MOVHI   R0, #AwaitsSeqChain*2-1         ;special
        BHI     %FT54
        LDR     R3, [SP, #TrkReadAheadLimit]
        SUBS    R3, R3, FileOff
        BLS     %FT60

        MOV     R0, #MonotonicChain*2
54
        BL      FindFreeBuf             ;(R0,FileOff,Fcb,BufPtr->R2,BufPtr,Z)
        LDR     R0, [SP, #ReadProcessBlock]
        BEQ     %FT60
        MOV     R6, R2
        BIC     LR, FileOff, #&3FC
        STR     LR, [BufPtr,#BufFileOff]
        STR     Fcb, [BufPtr,#BufFcb]
        LDR     R2, = EmptyBuf * &01010101
        AND     BufOff, FileOff, #&300
        MOV     BufOff, BufOff, LSR #5
56
        RSB     LR, BufOff, #32
        CMPS    R3, LR, LSL #5
        MOVHI   R3, LR, LSL #5
        BL      AddBuffer               ;(R0,R3,FileOff,BufOff,BufPtr->FileOff,C)
        BCC     %FT58

        MOV     R1, #ReadAhead
832
        MOV     LR, #AllBufFlags
Neil Turton's avatar
Neil Turton committed
833 834 835 836 837 838 839 840 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 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
57
        BIC     R2, R2, LR, LSL BufOff
        ORR     R2, R2, R1, LSL BufOff
        ADD     BufOff,BufOff,BufSz
        SUBS    R3, R3, BufSz, LSL #5
        BNE     %BT57

        BL      UpdateBufState          ;(R2,BufSz,BufPtr)
        MOVS    R2, R6
        BLPL    LinkFileChain           ;(R2,BufPtr)
        B       %BT52

58                              ;here if failed to add buffer
        TEQS    R2, #0
        LDRPL   R2, =EmptyBuf * &01010101
        BLPL    UpdateBufState          ;(R2,BufSz,BufPtr)
        LDRPL   BufPtr, [Fcb, #PrevInFile]
60
 [ DebugB
        CPBs
 ]
        BL      MoreValid
        BL      UpdateProcesses
        LDR     FileOff, [SP, #LastStart]
        CMPS    FileOff, TransferEnd
        BLO     %BT50

62
        LDRLO   FileOff, [SP, #LastStart]
        CMPLOS  FileOff, TransferEnd
        LDRLO   R0, [R0,#ProcessError]
        BLO     %FT66

64
        MOV     R0, #0
66
 [ DebugB
        CPBs
 ]
        BL      SetVOnR0
68                      ;FOLLOWING MUST PRESERVE V
 [ DebugB
        CPBs
 ]
        BL      UnlockMap
70
 [ DebugB
        CPBs
 ]
        BL      ReleaseFileCache
        MOV     R1, R0
        LDR     R0, [SP, #ReadProcessBlock]
72                              ;wait if necessary for direct part to complete
 [ DebugB
        CPBs
 ]
        LDRB    LR, [R0, #ProcessDirect]
        TEQS    LR, #&FF
        BLEQ    UpdateProcesses
        BEQ     %BT72
        BL      ReleaseController       ;(R0)
        MOVVS   R0, R1
        LDRVC   R0, DirectError
        BLVC    SetVOnR0
        ADD     SP, SP, #ReadBuffersWork
898
        B       %BT00                   ;Exit (checking V)
Neil Turton's avatar
Neil Turton committed
899

900
        LTORG
Neil Turton's avatar
Neil Turton committed
901 902 903 904 905 906 907 908


; >>>>>>>>>>>>>
; PutBytesEntry
; >>>>>>>>>>>>>

; entry:
;  R1   file handle
909 910 911
;  R2   start RAM address (no alignment guarantee)
;  R3   number of bytes to write (FileSwitch buffer multiple)
;  R4   file address (FileSwitch buffer multiple)
Neil Turton's avatar
Neil Turton committed
912 913 914 915 916 917 918 919 920

; exit:
;  R1-R4 ?
; V clear
;  R0   ?
; V set
;  R0   -> error block

PutBytesEntry ROUT
921
        SemEntry  Flag          ;allow reentrance, leaves SB,LR stacked
Neil Turton's avatar
Neil Turton committed
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
 [ Debug3L
        DLINE   "P",cc
 ]
 [ DebugB :LOR: DebugBE
        DLINE   "handle  |start   |length  |file ptr - PutBytesEntry"
        DREG    R1,,cc
        DREG    R2,,cc
        DREG    R3,,cc
        DREG    R4
 ]
 [ DebugBt
        DLINE   ">P",cc
 ]
        Push    "R5-R11"
        BL      HandleCheck     ;(R1->R0,R1,LR,V)
937 938 939 940 941
        BVS     %FT05
        LDR     R0, [R1,#FcbAllocLen]
      [ BigFiles
        TEQ     R0, #RoundedTo4G
        MOVEQ   R0, #&FFFFFFFF
Neil Turton's avatar
Neil Turton committed
942
        ADDS    LR, R4, R3
943 944 945 946 947 948 949
        BCC     %FT01           ; No carry out, proceed with comparison
        CMP     LR, #1          ; Allow carry out of only 4G exactly
        MOVCC   LR, R0          ; LR <=> 0, C=0, be sure to pass the test
01
      |
        ADDS    LR, R4, R3
      ]
Neil Turton's avatar
Neil Turton committed
950
        SUBCC   LR, LR, #1
951
        CMPCCS  LR, R0          ; Outside allocation?
Neil Turton's avatar
Neil Turton committed
952 953
        MOVCS   R0, #BadParmsErr
        BLCS    SetV
954
        BCS     %FT05
Neil Turton's avatar
Neil Turton committed
955

956
        ; Non filecore disc images - do transfer in foreground
Neil Turton's avatar
Neil Turton committed
957 958
        LDRB    r5, [r1, #FcbFlags]
        TST     r5, #FcbDiscImage
959
        BNE     %FT03
Neil Turton's avatar
Neil Turton committed
960 961 962 963 964 965

        ; Ensure a new Id on FileCore disc only
        Push    "r3"
        LDR     r3, [r1, #FcbIndDiscAdd]
        BL      EnsureNewId
        Pull    "r3"
966
        BVS     %FT05
Neil Turton's avatar
Neil Turton committed
967

968 969 970 971 972 973 974
      [ BigFiles
        ; If transfer ends at exactly 4G, use foreground
        ; If transfer ends near 4G, use foreground
        ADDS    R5, R3, R4
        CMPCC   R5, #-1*K
        BCS     %FT03
      ]
Neil Turton's avatar
Neil Turton committed
975

976 977 978 979 980 981
        ; Do in background if got some file buffers
        LDRB    R5, MaxFileBuffers
        TEQ     R5, #0
        BNE     %FT06
03
        ; Foreground
Neil Turton's avatar
Neil Turton committed
982
        Push    "R1,R3,R4"
983 984 985 986 987 988
        MOV     R5, R4
        MOV     R4, R3
        MOV     R3, R2
        LDR     R2, [R1,#FcbIndDiscAdd]
        MOV     R1, #DiscOp_WriteSecs :OR: DiscOp_Op_IgnoreEscape_Flag
        BL      GenIndDiscOp    ;(R1-R5->R0-R5,V)
Neil Turton's avatar
Neil Turton committed
989

990
        ; Discard any cached clashing read ahead buffers
Neil Turton's avatar
Neil Turton committed
991 992
        Pull    "R1,R3,R4"
        LDRB    r5, [r1, #FcbFlags]
993 994 995 996 997 998 999 1000
      [ BigFiles
        SavePSR r6              ; V from GenIndDiscOp
        ASSERT  FcbDiscImage = 128
        MOVS    r5, r5, LSR #8  ; FcbDiscImage set => C set
        ADDCCS  r5, r3, r4      ; or foreground forced because write ended at 4G => C set
        BLCS    ReleaseFcbBuffersInRange ;(r1,r3,r4) drop all buffers in range
        RestPSR r6,,f
      |
Neil Turton's avatar
Neil Turton committed
1001 1002
        TST     r5, #FcbDiscImage
        BLNE    ReleaseFcbBuffersInRange ;(r1,r3,r4) drop all buffers in range
1003 1004
      ]
05
Neil Turton's avatar
Neil Turton committed
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
        BLVS    FindErrBlock    ; (R0->R0,V)
        BL      FileCoreExit
 [ Debug3L
        DLINE   "p",cc
 ]
 [ DebugBt
        DLINE   " <P"
 ]
 [ DebugB :LOR: DebugBE
        DLINE   "<PutBytes"
1015
        DebugError "PutBytes error"
Neil Turton's avatar
Neil Turton committed
1016 1017 1018 1019 1020 1021 1022 1023 1024
 ]
        Pull    "R5-R11,SB,PC"

;layout of write stack workspace
                        ^ 0
WriteProcessBlock       # 4
WriteRamAdjust          # 4
WriteBuffersWork        # 0

1025
06
Neil Turton's avatar
Neil Turton committed
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039

;INITIALISATION
 [ DebugJ
        BL      Check
 ]
 [ DebugB
        CPBs
 ]
 [ DebugH
        DLINE   "W ",cc
        DREG    R4,,cc
 ]
        MOV     R0, #2
        BL      GetPutCommon  ;R0-R4->R0,R2,R3,BufSz,FileOff,R6,TransferEnd,Fcb,R10,Z,V
1040
        BVS     %BT05
Neil Turton's avatar
Neil Turton committed
1041 1042 1043 1044 1045 1046 1047 1048 1049
        ASSERT  WriteProcessBlock = 0
        ASSERT  WriteRamAdjust = 4
        ASSERT  WriteBuffersWork = 8
        Push    "R0,R2"

;IF FLOPPY LET WRITE BEHIND ON ANY OTHER FLOPPY FINISH
        BL      DiscWriteBehindWait             ;(R3)
        BL      ClaimController                 ;(R0)

1050
        MOV     R2, R3, LSR #(32-3)
Neil Turton's avatar
Neil Turton committed
1051 1052 1053 1054 1055 1056 1057 1058
        STRB    R2, WriteDisc
; ENSURE DISC PRESENT AND DRIVE LOCKED
        LDRB    LR, [R0, #ProcessWriteBehindDisc]
        TEQ     LR, R2
        BEQ     %FT09

        LDRB    LR, LockedDisc
        CMPS    LR, R2                  ;V=0
1059
        BLEQ    DiscAddToRec            ;(R3->LR)
Neil Turton's avatar
Neil Turton committed
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
        LDREQB  R1, [LR, #DiscsDrv]
        BEQ     %FT07
        BL      LockDisc                ;(R3->R0,R1,V)
        MOVVS   R3, #&FF
        STRVSB  R3, WriteDisc
        MOVVS   R3, R0
        LDRVS   R0, [SP, #WriteProcessBlock]
        BLVS    ReleaseController       ;(R0)
        BVS     %FT93

07
; Set writebehind disc and drive"
        ASSERT  (ProcessWriteBehindDrive) :MOD: 4 =0      ;word write so atomic
        ASSERT  ProcessWriteBehindDisc-ProcessWriteBehindDrive=1
        ORR     LR, R1, R2, LSL #8
        STR     LR, [R0, #ProcessWriteBehindDrive]
09
 [ DebugB
        CPBs
 ]

 [ DebugBv
        DLINE   "Write behind finished, controller claimed, set write behind disc and drive"
 ]

;IF TRANSFER OVERLAPS ACTIVE BUFFERS WAIT FOR THEM TO FINISH
1086
        LDR     LR, [R0,#ProcessFcb]    ; LR := file control block of current process on this controller
Neil Turton's avatar
Neil Turton committed
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
        TEQS    Fcb,LR
        BNE     %FT17
13
 [ DebugBv
        DLINE   "Process Fcb matches"
 ]
        ; Fcb matches Process Fcb
        ; Loop until process is inactive or transfer range doesn't
        ; intersect requested transfer

        LDRB    LR, [R0,#Process]
        TSTS    LR, #ReadAhead :OR: WriteBehind
        BEQ     %FT17
 [ DebugBv
        DLINE   "Process still active"
 ]
        ASSERT  ProcessEndOff = ProcessStartOff+4
        ADD     R2, R0, #ProcessStartOff
        LDMIA   R2, {R2,LR}
        CMPS    R2, TransferEnd
        CMPLOS  FileOff, LR
        BLLO    UpdateProcesses
 [ Debug3L
        BHS     %FT01
        DLINE   "w",cc
01
 ]
        BLO     %BT13

17
 [ DebugBv
        DLINE   "Process now quiet"
        CPBs
 ]
;CONVERT ANY SUB BUFFERS BETWEEN START AND END TO EMPTY
        BL      ClaimFileCache
        MOV     R6, FileOff
        BL      EmptyBuffers    ;R0,BufSz,FileOff,TransferEnd,Fcb->BufOff,BufPtr

;WORK BACKWARDS FROM END CLAIMING BUFFERS
 [ DebugBv
        DLINE   "FileCache claimed and buffers emptied"
        CPBs
 ]

;R6 - start  of transfer
;TransferEnd

        BL      UpdateProcesses
 [ DebugBv
        DLINE   "Processes updated"
 ]
        MOV     R8, TransferEnd
33
 [ DebugBv
        DLINE   "Stuff a buffer..."
 ]
 [ DebugJ
        CMPS    R6, R8
        BLO     %FT01

        DREG    R6, "", cc
        DREG    R8, "", cc
        MOV     LR, PC
        MOV     PC, #0
01
 ]
        SUB     FileOff, R8, BufSz, LSL #5
        BIC     FileOff, FileOff, #&3FC
        BL      FindSubBuf      ;FileOff,Fcb,BufPtr -> BufOff,BufPtr
        TEQS    BufOff, #0
        LDREQ   R2, [BufPtr,#BufFlags]
        MOVEQ   R3, #-1
        BEQ     %FT37

        MOV     R0, #AwaitsSeqChain*2
        BL      FindFreeBuf             ;(R0,FileOff,Fcb,BufPtr->R2,BufPtr,Z)
        BEQ     %FT45                   ;couldn't get buffer
        MOV     R3, R2
        STR     Fcb, [BufPtr,#BufFcb]
        STR     FileOff, [BufPtr,#BufFileOff]
1168
; MB FIX
1169
; FindFreeBuf returns a buffer that has rubbish in its prev and next pointers
1170 1171
; so set them to 0 so that UpdateBufState can detect it. If it doesn't get into UpdateBufState
; then the machine is probably dead... Kill or cure...
1172 1173 1174
        MOV     LR,#0
        STR     LR,[BufPtr,#NextInFile]
        STR     LR,[BufPtr,#PrevInFile]
1175
; end MB FIX
Neil Turton's avatar
Neil Turton committed
1176 1177 1178 1179 1180 1181 1182 1183 1184
        LDR     R2, =EmptyBuf * &01010101
37
        SUBS    R1, R6, FileOff
        MOVLO   R1, #0
        MOVLO   R0, FileOff
        MOVHS   R0, R6
41
        CMPS    FileOff, R6
        CMPHSS  R8, FileOff
1185 1186
        BICHI   R2, R2, #AllBufFlags
        ORRHI   R2, R2, #WriteBehind    ; turn that sub buffer from empty to write behind
Neil Turton's avatar
Neil Turton committed
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
        MOV     R2, R2, ROR BufSz
        ADD     FileOff, FileOff, BufSz, LSL #5
        TSTS    FileOff, #&300
        BNE     %BT41
        BL      UpdateBufState  ;(R2,BufSz,BufPtr)
        SUB     R2, R8, R0
        LDR     LR, [SP, #WriteRamAdjust]
        ADD     R0, R0, LR
        ADD     R1, R1, #BufferData
        ADD     R1, BufPtr, R1
        LDR     R8, [SP, #WriteProcessBlock]
        LDR     LR, [R8, #ProcessWriteBehindLeft]
        ADD     LR, LR, R2
        STR     LR, [R8, #ProcessWriteBehindLeft]
  [ DebugH :LOR: DebugBv
1202
        DREG    R2,"+",cc
Neil Turton's avatar
Neil Turton committed
1203 1204
        Push    "r0"
        MOV     r0, LR
1205
        DREG    r0," "
Neil Turton's avatar
Neil Turton committed
1206 1207 1208 1209 1210 1211 1212 1213 1214
        Pull    "r0"
  ]
        TSTS    R0, #3
        BLEQ    Move256n        ;(R0-R2->R0-R2) copy data into buffer
        BLNE    BlockMove       ;(R0-R2)

        MOVS    R2, R3
        BLPL    LinkFileChain   ;(R2,BufPtr) must be done last for write behind

1215
        SUB     R8, FileOff, #1*K       ; One more buffer filled and linked
Neil Turton's avatar
Neil Turton committed
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
        CMPS    R8, R6
        BHI     %BT33
 [ DebugBv
        DLINE   "Finished filling buffers"
 ]
        MOV     R8, R6
45
        MOV     R2, R8
        LDR     R0, [SP,#WriteProcessBlock]
        LDRB    R10,[R0,#Process]
        CMPS    R10,#WriteBehind        ;V=0
        LDREQ   LR, [R0,#ProcessFcb]
        TEQEQS  LR, Fcb
        LDREQ   LR, [R0,#ProcessEndOff]
        TEQEQS  LR, R6
        LDREQ   R1, [R0,#ProcessStatus]
        ANDEQ   R1, R1, #CanExtend
        TEQEQS  R1, #CanExtend
        BEQ     %FT62

 [ DebugBv
        DLINE   "Process inconvenient (wrong Fcb, not this bit of disc or can't extend)"
 ]
;HERE IF NOT CONTIGUOUS IN FILE WITH EXISTING WRITE BEHIND
48                      ;V=0
 [ DebugBv
        CPBs
 ]
        ASSERT  Inactive = bit0
        TEQS    R10,R10,LSR #1  ;C=1 <=> Inactive
        TEQS    R2, R6          ;Z=1 <=> all buffered, preserves C
 [ DebugH
        BNE     %FT01
        DLINE   "WB ",cc
01
 ]
        BHI     %FT49           ;(  C  ~Z ) Inactive unbuffered, write unbuffered and adjacent
        BNE     %FT58           ;((~C) ~Z ) Active unbuffered, wait for process to advance
        BCC     %FT85           ;( ~C  (Z)) Active buffered, just return

 [ DebugBv
        DLINE   "Process inactive and all data buffered"
 ]

        MOV     R1, #&FF        ;( (C) (Z)) Inactive buffered, consider restarting
        STRB    R1, WriteDisc
        LDRB    R1, [R0, #ProcessWriteBehindDisc]
        TEQ     R1, #&FF
        MOVNE   R1, #0
        BLNE    ReduceWriteBehind       ;(R0,R1)
        LDR     R1, FloppyProcessBlk
        TEQ     R0, R1
        MOVEQ   R1, #FloppyLock
        MOVNE   R1, #WinnieLock
        BL      ReleaseFileCache
 [ DebugBv
        DLINE   "FileCache released"
 ]
        BL      ReleaseController       ;(R0)
 [ DebugBv
        DLINE   "Controller released"
 ]
        BL      BackgroundOps           ;(R0,R1->R3-R11)
 [ DebugBv
        DLINE   "BackgroundOps performed"
        CPBs
 ]
        B       %FT95

49
 [ DebugBv
        DLINE   "Process inactive and not all the data has been buffered"
        CPBs
 ]
        LDR     R3, [Fcb, #FcbIndDiscAdd]
        BL      BeforeReadFsMap ;(R3->R0,V)
 [ DebugBv
        DLINE   "Got the FSMap (maybe!)"
 ]
50
 [ DebugBv
        CPBs
 ]
        MOVVS   R3, R0
        LDRVS   R0, [SP,#WriteProcessBlock]
        BVS     %FT87
53
 [ DebugBv
        DLINE   "FS map definitely got"
        CPBs
 ]
 [ DebugH
        DLINE   "WN ",cc
 ]

        MOV     FileOff, R6
1312
        BL      DefFileFrag     ;FileOff,Fcb -> R0,DiscAdjust,FragEnd
Neil Turton's avatar
Neil Turton committed
1313 1314 1315
        MOV     R6, FileOff

        BL      FindSubBuf      ;FileOff,Fcb,BufPtr -> BufOff,BufPtr
1316
        BL      BackwardsSkipWriteBehind ;R0,BufSz,FileOff,BufOff,BufPtr->FileOff,BufOff,BufPtr
Neil Turton's avatar
Neil Turton committed
1317 1318 1319 1320
        MOV     R3, FileOff

        MOV     FileOff, R2
        BL      FragLeft        ;(FileOff,Fcb->LR,Z)
1321
        ADD     FragEnd, R2, LR
Neil Turton's avatar
Neil Turton committed
1322 1323 1324 1325 1326 1327 1328

        CMPS    FragEnd, TransferEnd
        MOVLS   TransferEnd, FragEnd
        BLS     %FT57

        MOV     FileOff, TransferEnd
        BL      FindSubBuf      ;FileOff,Fcb,BufPtr -> BufOff,BufPtr
1329
        BL      SkipWriteBehind ;BufSz, FileOff, FragEnd, BufOff, BufPtr -> FileOff, BufOff, BufPtr
Neil Turton's avatar
Neil Turton committed
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
        MOV     TransferEnd, FileOff
57
 [ DebugBv
        CPBs
 ]
        BL      UnlockMap
        ASSERT  WriteProcessBlock = 0
        ASSERT  WriteRamAdjust = 4
        LDMIA   SP, {R0,R1}
        SUB     R2, R2, R3
        MOV     FileOff, R3
        SUB     R10,R6,R3
1342
        MOV     R3, #DiscOp_WriteSecs
Neil Turton's avatar
Neil Turton committed
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 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
        MOV     R6, R2

 [ DebugBv
        DLINE   "BackgroundFileCacheOp going off"
 ]
        BL      BackgroundFileCacheOp
;R0-R3,FileOff,R6,TransferEnd,FragEnd,Fcb,R10,BufPtr -> R1,R3,V
 [ DebugBv
        DLINE   "Updating processes..."
 ]
        BL      UpdateProcesses
 [ DebugBv
        DLINE   "Processes updated"
        CPBs
 ]
        B       %FT85


58                              ;wait for process to advance, freeing buffers
 [ DebugBv
        DLINE   "Process still active, but not all data buffered"
        DLINE   "Drum fingers waiting for change",cc
 ]
        LDR     R1, [R0, #ProcessStartOff]
        MOV     R1, R1, LSR #10
        LDRB    R8, [R0, #Process]
59
        BL      UpdateProcesses
        LDR     R3, [R0, #ProcessError]
        TEQS    R3, #0
        BLNE    SetV
        BNE     %FT87
        LDR     LR, [R0, #ProcessStartOff]
        TEQS    R1, LR, LSR #10
        LDREQB  LR, [R0, #Process]
        TEQEQS  R8, LR
 [ DebugBv
        DLINE   ".",cc
 ]
        BEQ     %BT59
        MOV     R8, R2
 [ DebugBv
        DLINE   "Aha!"
 ]
        B       %BT33           ;loop to fill any finished buffers
1388 1389 1390
 [ DebugBv
        LTORG
 ]
Neil Turton's avatar
Neil Turton committed
1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462
60
 [ DebugBv
        CPBs
 ]
        Pull    "R2"
61
 [ DebugBv
        DLINE   "Resort to 'process inconvenient' path"
        CPBs
 ]
        MOV     R10, #WriteBehind
        CMPS    R10, R10        ;V=0
        B       %BT48

;HERE IF CONTIGUOUS IN FILE WITH EXISTING WRITE BEHIND
62
 [ DebugBv
        DLINE   "Process writing behind on this Fcb, ends at where we want to start and can be extended"
        CPBs
 ]
        LDR     FragEnd, [R0, #ProcessFragEnd]
        CMPS    R2, R6
        BLS     %FT72
        Push    "R2"
        SUBS    R2, FragEnd, R6
        BLS     %BT60           ;can't extend background process over frag end

 [ DebugH
        DLINE   "WC ",cc
 ]
 [ DebugBv
        DLINE   "Filling out any partial buffer at start"
 ]
;FILL OUT ANY PARTIAL BUFFER AT START
        ANDS    R1, R6, #&300
        BEQ     %FT66

        RSB     LR, R1, #&400
        CMPS    LR, R2
        MOVLO   R2, LR
        MOV     R3, R2
        MOV     FileOff, R6
        BL      FindSubBuf      ;FileOff,Fcb,BufPtr -> BufOff,BufPtr

        LDR     R0, [SP, #4+WriteRamAdjust]
        ADD     R0, R0, FileOff
        ADD     R1, R1, #BufferData
        ADD     R1, R1, BufPtr
        TSTS    R0, #3
        BLEQ    Move256n        ;(R0-R2->R0-R2)
        BLNE    BlockMove       ;(R0-R2)
        LDR     R0, [SP, #4+WriteProcessBlock]
        BL      AddBuffer       ;(R0,R3,FileOff,BufOff,BufPtr->FileOff,C)
        BCC     %BT60

        LDR     LR, [R0, #ProcessWriteBehindLeft]
        ADD     LR, LR, R3
  [ DebugH :LOR: DebugBv
        DLINE   "P+",cc
        DREG    R3,,cc
        Push    "r0"
        MOV     r0, lr
        DREG    r0, "", cc
        Pull    "r0"
  ]
        STR     LR, [R0, #ProcessWriteBehindLeft]
        LDR     R2, [BufPtr,#BufFlags]
        MOV     R1, #WriteBehind :EOR: EmptyBuf
64
        EOR     R2, R2, R1, LSL BufOff
        ADD     BufOff, BufOff, BufSz
        SUBS    R3, R3, BufSz, LSL #5
1463
        BGT     %BT64           ;Loop for each sub-buffer
Neil Turton's avatar
Neil Turton committed
1464 1465 1466 1467 1468