Arthur2 69.2 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
; 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.
;
        TTL  => Arthur2

;        GET  $.Hdr.Variables  - got at start

        MACRO
$l      GSVarGetWSpace
Robert Sprowson's avatar
Robert Sprowson committed
21
$l      LDR     R12, =GSVarWSpace
Neil Turton's avatar
Neil Turton committed
22 23
        MEND

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
;mjs performance enhancements for Ursula (ChocolateSysVars)
;
                          GBLL    SysVars_QuickContext
                          GBLL    SysVars_StickyNodes
SysVars_QuickContext      SETL    {TRUE} :LAND: ChocolateSysVars   ;avoid abysmal O(n*n) enumeration of vars
SysVars_StickyNodes       SETL    {TRUE} :LAND: ChocolateSysVars   ;attempt to avoid lots of SysHeap operations,
                                                                   ;especially grows and shrinks

SysVars_Vindex_NStart     * 256   ;initial no. of vars supported by index
SysVars_Vindex_NBump      *  32   ;additional no. of vars each time index size is bumped up

  [ SysVars_QuickContext
;
;format of block anchored at VariableList
;
                     ^   0
SysVars_LastContext  #   4                    ;last var table index for last context ptr returned, or -1 if invalid
SysVars_VTableOffset #   0
;
;immediately followed by table data as in old code:
;  1 word   = total number of variables (N)
;  N words  = ptrs to variable blocks (sorted table)
;
  ]
Neil Turton's avatar
Neil Turton committed
48

49 50 51 52 53 54 55 56 57
  [ SysVars_StickyNodes
SysVars_StickyNode_UnitSize *  32             ;quantise node size to multiples of this many bytes
                                              ;(must be multiple of 8, and power of 2)
SysVars_StickyNode_Log2US   *   5             ;Log2 of unit size
SysVars_StickyNode_MaxSize  * 320             ;maximum size of node that may stick (be retained on removal as active node)
;
;There are currently 10 words allocated in kernel workspace for sticky pointers
      ASSERT  SysVars_StickyNode_UnitSize*10 = SysVars_StickyNode_MaxSize
  ]
Neil Turton's avatar
Neil Turton committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

        GBLL    DebugSysVars
DebugSysVars SETL {FALSE}


;-----------------------------------------------------------------------------------
;
; This file covers:
;   System variables:
;     InitVariables
;     OS_ReadVarVal
;     OS_SetVarVal
;   GSTrans:
;     OS_GSInit
;     OS_GSRead
;     OS_GSTrans
;   OS_BinaryToDecimal
; These have been grouped because GSTrans makes direct use of the system variables'
; structures and OS_BinaryToDecimal is used by readvarval.
;
; The system variables are stored as a one way sorted alphabetically linked list hanging
; off the zero-page location VariableList:
;
81
; VariableList---->sorted table of pointers to variable blocks (QuickIndex) - format as above
Neil Turton's avatar
Neil Turton committed
82 83 84
;
; The end is indicated by the link having the value 0.
;
85 86
; Each variable is stored in one block in the system heap (block will be word aligned). The
; format of each block is:
Neil Turton's avatar
Neil Turton committed
87
;
88 89
; No. Bytes     Use
; N+1           Variable's name (length N, plus terminator).
Neil Turton's avatar
Neil Turton committed
90 91 92 93 94 95 96 97 98 99 100 101
; 1             Variable's type:
;                       0       string
;                       1       number
;                       2       macro
;                       3       expanded (not valid within sysvar structure)
;                       16      code
; M             data - depends on the variable's type
;
; The structure of the data is as follows:
;
; Type 0 - string
; Bytes         Use
102
; 3             Length (N)
Neil Turton's avatar
Neil Turton committed
103 104 105 106 107 108 109 110
; N             the bytes of the string - may contain any characters
;
; Type 1 - number
; Bytes         Use
; 4             its value (not necessarily word aligned)
;
; Type 2 - macro
; Bytes         Use
111
; 3             Length (N)
Neil Turton's avatar
Neil Turton committed
112 113 114 115 116 117 118 119 120 121 122
; N             the bytes of the string - must be a valid GSTransable string
;                       including terminator
;
; Type 16 - code
; Bytes         Use
; x             Sufficient to word align...
; 4             Write entry
; 4             Read entry
; N             The rest of the code

InitVariables  ROUT
Robert Sprowson's avatar
Robert Sprowson committed
123
        Push    "lr"
Neil Turton's avatar
Neil Turton committed
124 125

        ; Blank the sysvar list
Robert Sprowson's avatar
Robert Sprowson committed
126 127 128
        MOV     R0, #0
        LDR     R12, =ZeroPage+VariableList
        STR     R0, [R12]
Neil Turton's avatar
Neil Turton committed
129 130

        ; Set up the preset system variables
Robert Sprowson's avatar
Robert Sprowson committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
        ADR     R0, SystemVarList       ; R0 pointer to name
01      MOV     R1, R0
        LDRB    R2, [R1], #1
        CMP     R2, #0
        Pull    "PC", EQ
02      LDRB    R3, [R1], #1
        CMP     R3, #0
        BNE     %BT02
        LDRB    R4, [R1], #1            ; get type
        ADD     R1, R1, #3
        BIC     R1, R1, #3
        LDR     R2, [R1], #4
        SWI     XOS_SetVarVal
        ADD     R0, R1, R2
        B       %BT01
Neil Turton's avatar
Neil Turton committed
146 147 148 149 150 151 152 153 154 155 156 157

        LTORG

; System vars have Thunks :
; read thunk returns R0 ptr to string, R2 length. R1 corruptible
; set thunk takes R1 ptr to value, R2 length. Value is always a string.
; Can corrupt R1, R2, R4, R10-12

; The list of nodes to copy into RAM :
; name, 0 , type, ALIGN, size of value, value

SystemVarList  ROUT
Robert Sprowson's avatar
Robert Sprowson committed
158
        =       "Sys$$Time", 0, VarType_Code
Neil Turton's avatar
Neil Turton committed
159
        ALIGN
Robert Sprowson's avatar
Robert Sprowson committed
160 161 162
        &       sv2-.-4
        LDR     PC, %FT01
        LDR     PC, %FT02
Neil Turton's avatar
Neil Turton committed
163
01
Robert Sprowson's avatar
Robert Sprowson committed
164
        &       SetTimeVar
Neil Turton's avatar
Neil Turton committed
165
02
Robert Sprowson's avatar
Robert Sprowson committed
166
        &       ReadTimeVar
Neil Turton's avatar
Neil Turton committed
167

Robert Sprowson's avatar
Robert Sprowson committed
168
sv2     =       "Sys$$Year", 0, VarType_Code
Neil Turton's avatar
Neil Turton committed
169
        ALIGN
Robert Sprowson's avatar
Robert Sprowson committed
170 171 172
        &       sv3-.-4
        LDR     PC, %FT03
        LDR     PC, %FT04
Neil Turton's avatar
Neil Turton committed
173
03
Robert Sprowson's avatar
Robert Sprowson committed
174
        &       SetYear
Neil Turton's avatar
Neil Turton committed
175
04
Robert Sprowson's avatar
Robert Sprowson committed
176
        &       ReadYear
Neil Turton's avatar
Neil Turton committed
177

Robert Sprowson's avatar
Robert Sprowson committed
178
sv3     =       "Sys$$Date", 0, VarType_Code
Neil Turton's avatar
Neil Turton committed
179
        ALIGN
Robert Sprowson's avatar
Robert Sprowson committed
180 181 182
        &       sv4-.-4
        LDR     PC, %FT05
        LDR     PC, %FT06
Neil Turton's avatar
Neil Turton committed
183
05
Robert Sprowson's avatar
Robert Sprowson committed
184
        &       SetDate
Neil Turton's avatar
Neil Turton committed
185
06
Robert Sprowson's avatar
Robert Sprowson committed
186
        &       ReadDate
Neil Turton's avatar
Neil Turton committed
187

Robert Sprowson's avatar
Robert Sprowson committed
188
sv4     =       "Sys$$ReturnCode", 0, VarType_Code
Neil Turton's avatar
Neil Turton committed
189
        ALIGN
Robert Sprowson's avatar
Robert Sprowson committed
190 191 192
        &       sv5-.-4
        LDR     PC, %FT07
        LDR     PC, %FT08
Neil Turton's avatar
Neil Turton committed
193
07
Robert Sprowson's avatar
Robert Sprowson committed
194
        &       SetRC
Neil Turton's avatar
Neil Turton committed
195
08
Robert Sprowson's avatar
Robert Sprowson committed
196
        &       ReadRC
Neil Turton's avatar
Neil Turton committed
197

Robert Sprowson's avatar
Robert Sprowson committed
198
sv5     =       "Sys$$RCLimit", 0, VarType_Code
Neil Turton's avatar
Neil Turton committed
199
        ALIGN
Robert Sprowson's avatar
Robert Sprowson committed
200 201 202
        &       sv6-.-4
        LDR     PC, %FT09
        LDR     PC, %FT10
Neil Turton's avatar
Neil Turton committed
203
09
Robert Sprowson's avatar
Robert Sprowson committed
204
        &       SetRCL
Neil Turton's avatar
Neil Turton committed
205
10
Robert Sprowson's avatar
Robert Sprowson committed
206
        &       ReadRCL
Neil Turton's avatar
Neil Turton committed
207

Robert Sprowson's avatar
Robert Sprowson committed
208
sv6     =       "Alias$.", 0, VarType_String
Neil Turton's avatar
Neil Turton committed
209
        ALIGN
Robert Sprowson's avatar
Robert Sprowson committed
210 211
        &       sv7-.-4
        =       "Cat ", 10
Neil Turton's avatar
Neil Turton committed
212

Robert Sprowson's avatar
Robert Sprowson committed
213
sv7     =       "Sys$$DateFormat", 0, VarType_String
Neil Turton's avatar
Neil Turton committed
214
        ALIGN
Robert Sprowson's avatar
Robert Sprowson committed
215
        &       sv8-.-4
Neil Turton's avatar
Neil Turton committed
216 217

        [ {TRUE}
Robert Sprowson's avatar
Robert Sprowson committed
218
        =       "%24:%mi:%se %dy-%m3-%ce%yr", 10
Neil Turton's avatar
Neil Turton committed
219
        |
Robert Sprowson's avatar
Robert Sprowson committed
220
        =       "%w3,%dy %m3 %ce%yr.%24:%mi:%se", 10
Neil Turton's avatar
Neil Turton committed
221 222
        ]

Robert Sprowson's avatar
Robert Sprowson committed
223
sv8     =       0
Neil Turton's avatar
Neil Turton committed
224

Kevin Bracey's avatar
Kevin Bracey committed
225
SysTimeFormat
Robert Sprowson's avatar
Robert Sprowson committed
226
        =       "%24:%mi:%se", 0
Kevin Bracey's avatar
Kevin Bracey committed
227
SysDateFormat
Robert Sprowson's avatar
Robert Sprowson committed
228
        =       "%w3,%dy %m3", 0
Kevin Bracey's avatar
Kevin Bracey committed
229
SysYearFormat
Robert Sprowson's avatar
Robert Sprowson committed
230
        =       "%ce%yr", 0
Kevin Bracey's avatar
Kevin Bracey committed
231

Neil Turton's avatar
Neil Turton committed
232 233 234 235 236
        ALIGN

; Now the code for our system variables.

ReadTimeVar
Robert Sprowson's avatar
Robert Sprowson committed
237 238
        ADR     R0, SysTimeFormat
        B       ReadTimeFormatted
Neil Turton's avatar
Neil Turton committed
239
SetTimeVar ROUT
Robert Sprowson's avatar
Robert Sprowson committed
240 241 242
        CMP     R2, #&FE
        BHI     TimeVarTooLong
        Push    "R0, lr"
Ben Avison's avatar
Ben Avison committed
243
 [ GSWorkspaceInKernelBuffers
Robert Sprowson's avatar
Robert Sprowson committed
244
        LDR     R12, =SysVarWorkSpace
Ben Avison's avatar
Ben Avison committed
245
 |
Neil Turton's avatar
Neil Turton committed
246
        GSVarGetWSpace
Robert Sprowson's avatar
Robert Sprowson committed
247
        ADD     R12, R12, #GSNameBuff
Ben Avison's avatar
Ben Avison committed
248
 ]
Robert Sprowson's avatar
Robert Sprowson committed
249 250 251 252 253 254 255 256 257 258 259 260 261 262
        MOV     R11, #8
        STRB    R11, [R12], #1
        MOV     R10, #0
        STRB    R10, [R12, R2]
        MOV     R10, R2
01      SUBS    R10, R10, #1
        LDRPLB  R11, [R1, R10]
        STRPLB  R11, [R12, R10]
        BPL     %BT01
        SUB     R1, R12, #1
        MOV     R0, #15
        SWI     XOS_Word
        STRVS   R0, [R13]
        Pull    "R0, PC"
Neil Turton's avatar
Neil Turton committed
263

Kevin Bracey's avatar
Kevin Bracey committed
264
TimeVarTooLong
Robert Sprowson's avatar
Robert Sprowson committed
265
        ADRL    R0, ErrorBlock_VarTooLong
266
      [ International
Robert Sprowson's avatar
Robert Sprowson committed
267
        B       TranslateError
268
      |
Kevin Bracey's avatar
Kevin Bracey committed
269
        RETURNVS
270
      ]
Kevin Bracey's avatar
Kevin Bracey committed
271

Neil Turton's avatar
Neil Turton committed
272
ReadYear
Robert Sprowson's avatar
Robert Sprowson committed
273 274
        ADR     R0, SysYearFormat
        B       ReadTimeFormatted
Neil Turton's avatar
Neil Turton committed
275
SetYear ROUT
Robert Sprowson's avatar
Robert Sprowson committed
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
        CMP     R2, #4
        BHI     TimeVarTooLong
        Push    "R0, lr"
        Push    "R1,R2"
        ADR     R0, SetYearPrefixFormat
        BL      ReadTimeFormatted
        ADD     R12, R0, R2             ; R12 -> position to copy year to
        Pull    "R1,R2"
        BVS     %FT02
        MOV     R10, #0
        STRB    R10, [R12, R2]
        MOV     R10, R2
01      SUBS    R10, R10, #1
        LDRPLB  R11, [R1, R10]
        STRPLB  R11, [R12, R10]
        BPL     %BT01
        SUB     R1, R0, #1
        MOV     R0, #15
        STRB    R0, [R1]
        SWI     XOS_Word
02      STRVS   R0, [R13]
        Pull    "R0, PC"
Neil Turton's avatar
Neil Turton committed
298

Kevin Bracey's avatar
Kevin Bracey committed
299
ReadDate
Robert Sprowson's avatar
Robert Sprowson committed
300 301
        ADR     R0, SysDateFormat
        B       ReadTimeFormatted
Kevin Bracey's avatar
Kevin Bracey committed
302
SetDate ROUT
Robert Sprowson's avatar
Robert Sprowson committed
303 304 305
        CMP     R2, #&F8
        BHI     TimeVarTooLong
        Push    "R0, lr"
Ben Avison's avatar
Ben Avison committed
306
 [ GSWorkspaceInKernelBuffers
Robert Sprowson's avatar
Robert Sprowson committed
307
        LDR     R12, =SysVarWorkSpace
Ben Avison's avatar
Ben Avison committed
308
 |
Kevin Bracey's avatar
Kevin Bracey committed
309
        GSVarGetWSpace
Robert Sprowson's avatar
Robert Sprowson committed
310
        ADD     R12, R12, #GSNameBuff
Ben Avison's avatar
Ben Avison committed
311
 ]
Robert Sprowson's avatar
Robert Sprowson committed
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
        MOV     R11, #15
        STRB    R11, [R12], #1
        MOV     R10, #0
        STRB    R10, [R12, R2]
        MOV     R10, R2
01      SUBS    R10, R10, #1
        LDRPLB  R11, [R1, R10]
        STRPLB  R11, [R12, R10]
        BPL     %BT01
        ADD     R1, R12, R2
        ADR     R0, SetDateSuffixFormat ; append year to supplied date
        Push    "R12"
        BL      ReadTimeFormattedAtR1
        Pull    "R12"
        SUB     R1, R12, #1
        MOV     R0, #15
        SWI     XOS_Word
        STRVS   R0, [R13]
        Pull    "R0, PC"
Kevin Bracey's avatar
Kevin Bracey committed
331 332 333 334 335 336 337


; in: R0 = format string
; out: R0 -> time string (0 terminated)
;      R2 = length of string (excluding terminator)
;      R1 corrupt
ReadTimeFormatted ROUT
Robert Sprowson's avatar
Robert Sprowson committed
338
        Push    "R3,R4,LR"
Ben Avison's avatar
Ben Avison committed
339
 [ GSWorkspaceInKernelBuffers
Robert Sprowson's avatar
Robert Sprowson committed
340 341
        LDR     R12, =SysVarWorkSpace
        ADD     R2, R12, #1
Ben Avison's avatar
Ben Avison committed
342
 |
Kevin Bracey's avatar
Kevin Bracey committed
343
        GSVarGetWSpace
Robert Sprowson's avatar
Robert Sprowson committed
344
        ADD     R2, R12, #GSNameBuff+1  ; This code copied from OsWord0EAlpha
Ben Avison's avatar
Ben Avison committed
345
 ]
Robert Sprowson's avatar
Robert Sprowson committed
346 347 348 349 350 351 352 353 354 355
01      MOV     R4, R0
        SUB     R13, R13, #8
        MOV     R1, R13
        MOV     R0, #3
        STRB    R0, [R1]
        MOV     R0, #14
        SWI     XOS_Word
        BVS     %FT02
        MOV     R0, #-1
        MOV     R1, R13
Ben Avison's avatar
Ben Avison committed
356
 [ GSWorkspaceInKernelBuffers
Robert Sprowson's avatar
Robert Sprowson committed
357
        MOV     R3, #?SysVarWorkSpace-1
Ben Avison's avatar
Ben Avison committed
358
 |
Robert Sprowson's avatar
Robert Sprowson committed
359
        MOV     R3, #?GSNameBuff-1
Ben Avison's avatar
Ben Avison committed
360
 ]
Robert Sprowson's avatar
Robert Sprowson committed
361 362 363 364
        SWI     XTerritory_ConvertDateAndTime
02      ADD     R13, R13, #8
        SUBVC   R2, R1, R0
        Pull    "R3,R4,PC"
Kevin Bracey's avatar
Kevin Bracey committed
365 366 367 368 369 370 371

; in: R0 = format string
;     R1 -> output buffer
; out: R0 -> time string (0 terminated)
;      R1 corrupt
;      R2 = length of string (excluding terminator)
ReadTimeFormattedAtR1
Robert Sprowson's avatar
Robert Sprowson committed
372 373 374
        Push    "R3,R4,LR"
        MOV     R2, R1
        B       %BT01
Neil Turton's avatar
Neil Turton committed
375 376

ReadRC  ROUT
Robert Sprowson's avatar
Robert Sprowson committed
377 378 379 380 381 382 383 384 385 386 387
        LDR     R0, =ZeroPage
        LDR     R0, [R0, #ReturnCode]
        B       ReadNumSysVar
SetRC   Push    "lr"
        BL      SetNumSysVar
        LDR     R4, =ZeroPage+ReturnCode
        STR     R2, [R4]
        LDR     R4, =ZeroPage+RCLimit
        LDR     R4, [R4]
        CMP     R2, R4
        BHI     %FT03
Kevin Bracey's avatar
Kevin Bracey committed
388
        CLRV
Robert Sprowson's avatar
Robert Sprowson committed
389 390 391
        Pull    "PC"
03      ADRGT   R0, ErrorBlock_RCExc
        ADRLT   R0, ErrorBlock_RCNegative
Neil Turton's avatar
Neil Turton committed
392
      [ International
Robert Sprowson's avatar
Robert Sprowson committed
393
        BL      TranslateError
Neil Turton's avatar
Neil Turton committed
394 395 396
      |
        SETV
      ]
Kevin Bracey's avatar
Kevin Bracey committed
397

Robert Sprowson's avatar
Robert Sprowson committed
398
        Pull    "PC"
Kevin Bracey's avatar
Kevin Bracey committed
399
SetYearPrefixFormat
Robert Sprowson's avatar
Robert Sprowson committed
400
        =       "%w3,%dy %m3 ", 0
Kevin Bracey's avatar
Kevin Bracey committed
401
SetDateSuffixFormat
Robert Sprowson's avatar
Robert Sprowson committed
402
        =       " %ce%yr", 0
Kevin Bracey's avatar
Kevin Bracey committed
403 404
        ALIGN

Neil Turton's avatar
Neil Turton committed
405 406 407
        MakeErrorBlock RCExc
        MakeErrorBlock RCNegative

Robert Sprowson's avatar
Robert Sprowson committed
408 409
ReadRCL LDR     R0, =ZeroPage
        LDR     R0, [R0, #RCLimit]
Neil Turton's avatar
Neil Turton committed
410
ReadNumSysVar
Robert Sprowson's avatar
Robert Sprowson committed
411
        Push    "lr"
Ben Avison's avatar
Ben Avison committed
412
 [ GSWorkspaceInKernelBuffers
Robert Sprowson's avatar
Robert Sprowson committed
413 414
        LDR     R12, =SysVarWorkSpace
        MOV     R1, R12
Ben Avison's avatar
Ben Avison committed
415
 |
Neil Turton's avatar
Neil Turton committed
416
        GSVarGetWSpace
Robert Sprowson's avatar
Robert Sprowson committed
417
        ADD     R1, R12, #GSNameBuff
Ben Avison's avatar
Ben Avison committed
418
 ]
Robert Sprowson's avatar
Robert Sprowson committed
419 420 421 422 423 424 425 426 427 428 429 430
        MOV     R2, #256
        SWI     XOS_BinaryToDecimal
        MOV     R0, R1
        Pull    "PC"
SetRCL  Push    "lr"
        BL      SetNumSysVar
        LDR     R4, =ZeroPage+RCLimit
        CMP     R2, #0                  ; can't set -ve RCLimit
        RSBMIS  R2, R2, #0
        MOVMI   R2, #0                  ; BIC of MININT
        STR     R2, [R4]
        Pull    "PC"
Neil Turton's avatar
Neil Turton committed
431 432 433

        LTORG

Robert Sprowson's avatar
Robert Sprowson committed
434 435 436 437 438
SetNumSysVar    ROUT ; R1 ptr to string, R2 string length
        Push    "lr"
        SUBS    R2, R2, #1
        ADDMI   R2, R2, #1              ; give 0 in R2 for bad length.
        Pull    "PC", MI
Ben Avison's avatar
Ben Avison committed
439
 [ GSWorkspaceInKernelBuffers
Robert Sprowson's avatar
Robert Sprowson committed
440
        LDR     R12, =SysVarWorkSpace
Ben Avison's avatar
Ben Avison committed
441
 |
Robert Sprowson's avatar
Robert Sprowson committed
442
        LDR     R12, =GSNameBuff+GSVarWSpace
Ben Avison's avatar
Ben Avison committed
443
 ]
Robert Sprowson's avatar
Robert Sprowson committed
444 445 446 447 448 449
03      LDRB    R10, [R1], #1           ; copy into a buffer so we can terminate it.
        STRB    R10, [R12], #1
        SUBS    R2, R2, #1
        BPL     %BT03
        MOV     R10, #13
        STRB    R10, [R12], #1
Ben Avison's avatar
Ben Avison committed
450
 [ GSWorkspaceInKernelBuffers
Robert Sprowson's avatar
Robert Sprowson committed
451
        LDR     R1, =SysVarWorkSpace
Ben Avison's avatar
Ben Avison committed
452
 |
Robert Sprowson's avatar
Robert Sprowson committed
453
        LDR     R1, =GSNameBuff+GSVarWSpace
Ben Avison's avatar
Ben Avison committed
454
 ]
Robert Sprowson's avatar
Robert Sprowson committed
455 456 457 458 459 460 461 462 463 464 465
        LDRB    R10, [R1]
        MOV     R12, #0
        CMP     R10, #"-"
        MOVEQ   R12, #-1
        CMPNE   R10, #"+"
        ADDEQ   R1, R1, #1
        MOV     R0, #0
        SWI     XOS_ReadUnsigned
        CMP     R12, #0
        RSBMI   R2, R2, #0
        Pull    "PC"
Neil Turton's avatar
Neil Turton committed
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512


;*****************************************************************************
; GSINIT, GSREAD, GSTRANS

; To enable GSTrans nesting to stand a chance of working don't flatten the
; stack every FSINIT. Instead, pick up the stack pointer (any value is OK)
; and wrap at 255. Stack overflow occurs if you increment the pointer to
; where it started for this GSINIT, and stack underflow occurs if you
; decrement the pointer when it is currently equal to stack limit.
; The stack limit is held in the environment value, R2.
; The stack is empty ascending.
        GBLL    GS_BufferNotStack
GS_BufferNotStack SETL {TRUE}

; some semi-arbitrary flags
GS_NoQuoteMess   * 1 :SHL: 31   ; flags passed in from user
GS_NoVBarStuff   * 1 :SHL: 30
GS_Spc_term      * 1 :SHL: 29   ; clear if user requested terminate on space
GS_In_String     * 1 :SHL: 28   ; set if waiting for closing "
GS_ReadingString * 1 :SHL: 27   ; set if reading chars from a string var.
GS_Macroing      * 1 :SHL: 26   ; set if reading chars from a macro
 [ GS_BufferNotStack
        ASSERT  GS_StackPtr_Lim = &80
GS_StackLimitBits * 7
GS_StackLimitPos * 19           ; The bit position of the LSB of the byte
                                ; which holds the stack limit
; bits 0-18 hold the string length for string transfers
 |
; bits 24-25 are unused
; bits 0-23 hold the string length for string transfers
 ]

; After GSINIT, R2 has these flags, and if expanding a count in the low byte

GSINIT  ROUT
;  In  : R0 pointer to string to expand
;        R2 has flags :
;          Bit 29 set means space is a terminator
;          Bit 30 set means | characters will not be molested
;          Bit 31 set means don't mess with quotes

;  Out : R0, R2 are values to pass back in to GSREAD
;        R1 is the first non-blank character
;        EQ means char is CR or LF, i.e. string is empty.

        ; Enable interupts as we've no right to have them disabled here
Kevin Bracey's avatar
Kevin Bracey committed
513
        WritePSRc SVC_mode, R1
Neil Turton's avatar
Neil Turton committed
514 515 516

 [ GS_BufferNotStack
        AND     R2, R2, #GS_NoQuoteMess :OR: GS_NoVBarStuff :OR: GS_Spc_term
Robert Sprowson's avatar
Robert Sprowson committed
517
                                        ; get caller's flags
Neil Turton's avatar
Neil Turton committed
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
 ]

; If no tokens to expand then don't reset evaluation stack
; This prevents conflict with modules opening messages files at lower levels

        Push    "r0"
10      LDRB    r1, [r0], #1
        CMP     r1, #13
        CMPNE   r1, #10
        CMPNE   r1, #0
        Pull    "r0",EQ
        BEQ     %FT20                   ; Jump if end of string, nothing to expand

        TEQ     r1, #"<"                ; Possibly something to expand?
        BNE     %BT10                   ; No then try next
        Pull    "r0"

; Expansion may be necessary so flatten evaluation stack

 [ GS_BufferNotStack
        GSVarGetWSpace
        LDRB    R1, [R12, #GS_StackPtr]
        AND     R1, R1, #(GS_StackPtr_Lim-1)     ; Ensure we remain in range
        STRB    R1, [R12, #GS_StackPtr]
        ORR     R2, R2, R1, LSL #GS_StackLimitPos
 |
        MOV     R1, #0
        GSVarGetWSpace
Robert Sprowson's avatar
Robert Sprowson committed
546
        STRB    R1, [R12, #GS_StackPtr] ; no stacked R0s
Neil Turton's avatar
Neil Turton committed
547 548 549 550 551 552
 ]

20
 [ GS_BufferNotStack
 |
        AND     R2, R2, #GS_NoQuoteMess :OR: GS_NoVBarStuff :OR: GS_Spc_term
Robert Sprowson's avatar
Robert Sprowson committed
553
                                        ; get caller's flags
Neil Turton's avatar
Neil Turton committed
554 555 556 557 558 559 560 561
 ]
        EOR     R2, R2, #GS_Spc_term    ; and invert for convenience

01      LDRB    R1, [R0], #1
        CMP     R1, #" "
        BEQ     %BT01
        TST     R2, #GS_NoQuoteMess
        CMPEQ   R1, #""""
Robert Sprowson's avatar
Robert Sprowson committed
562 563
        SUBNE   R0, R0, #1              ; dec if went too far
        ORREQ   R2, R2, #GS_In_String   ; set flag if in string
Neil Turton's avatar
Neil Turton committed
564 565 566
        CMP     R1, #13
        CMPNE   R1, #10
        CMPNE   R1, #0
Robert Sprowson's avatar
Robert Sprowson committed
567
        ORREQ   lr, lr, #Z_bit          ; and move EQ/NE to return pc
Neil Turton's avatar
Neil Turton committed
568 569 570
        BICNE   lr, lr, #Z_bit
        ExitSWIHandler

Ben Avison's avatar
Ben Avison committed
571 572
        LTORG

Neil Turton's avatar
Neil Turton committed
573 574 575 576 577 578 579 580 581 582 583
; -----------------------------------------------------------------------------


GSREAD  ROUT
;  In  : R0, R2 from last GSREAD/GSINIT
;  Out : R1 character, R0, R2 updated.
;        VS => "Bad String" error
;        CS => string ended (in which case R1 = terminator)

        ; enable interupts as (a) they'll get enabled by a <thing> entry
        ; and (b) GSREAD may take some time
Kevin Bracey's avatar
Kevin Bracey committed
584
        WritePSRc SVC_mode, R10
Neil Turton's avatar
Neil Turton committed
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634

        BIC     lr, lr, #C_bit
        MOV     R10, #0
        TST     R2, #GS_ReadingString
        BNE     GSREAD_RStringGetNextByte         ; take byte from stringvar

GSREAD_XPandGetNextByte
        LDRB    R1, [R0], #1
        CMP     R1, #13
        CMPNE   R1, #10
        CMPNE   R1, #0
        BEQ     GSREAD_XPandGotToEnd
        CMP     R1, #" "
        BEQ     GSREAD_XPandGotSpace
        BLT     GSREAD_BadStringError   ; bad string : control code in string
        CMP     R1, #""""
        BEQ     GSREAD_XPandGotQuote
        CMP     R1, #"|"
        TSTEQ   R2, #GS_NoVBarStuff
        BEQ     GSREAD_WorkOutBarChar
        CMP     R1, #"<"
        BNE     GSREAD_ReturnWithChar   ; OS_Exit with valid character

; got to try and get a variable value.
        Push    "R0, R2, lr"
        LDRB    R1, [R0]
        CMP     R1, #">"
        CMPNE   R1, #" "
        BEQ     GSREAD_AngleBraDaftSoIsnt  ; <> and < > are silly.

        ; Copy angle bracketed thing checking for correct termination
        GSVarGetWSpace
        ADD     R12, R12, #GSNameBuff
        MOV     R11, #0
20      LDRB    R1, [R0], #1
        STRB    R1, [R12], #1
        ADD     R11, R11, #1
        CMP     R11, #255
        CMPNE   R1, #13
        CMPNE   R1, #10
        CMPNE   R1, #0
        BEQ     GSREAD_AngleBraDaftSoIsnt
        CMP     R1, #">"
        BNE     %BT20

        ; Check for number first
        MOV     R1, #0
        STRB    R1, [R12, #-1]          ; terminate it
        SUB     R1, R12, R11            ; pointer to name or number
        Push    "R0"
Ben Avison's avatar
Ben Avison committed
635
        MOV     R0, #10
Neil Turton's avatar
Neil Turton committed
636 637 638 639 640 641 642 643 644 645 646 647 648 649
        SWI     XOS_ReadUnsigned        ; check for number
        Pull    "R0"
        BVS     GSREAD_AngledThingAintNumber   ; silly - has to be name
        LDRB    R1, [R1]                ; check terminated by the null
        CMP     R1, #0
        BNE     GSREAD_AngledThingAintNumber
        MOV     R1, R2                  ; character value
        ADD     stack, stack, #4        ; discard old R0 value.
        Pull    "R2, lr"
        B       GSREAD_ReturnWithChar   ; exit-R1's the char value.

GSREAD_AngledThingAintNumber
        ; R0, R2, lr on stack
        Push    "R0, R3, R4, R10"       ; corrupted by VarFindIt
Robert Sprowson's avatar
Robert Sprowson committed
650 651
        MOV     R3, #0                  ; context ptr
        SUB     R0, R12, R11            ; name ptr
Neil Turton's avatar
Neil Turton committed
652
        BL      VarFindIt
Robert Sprowson's avatar
Robert Sprowson committed
653
        Pull    "R0, R3, R4, R10", EQ   ; not found mate
Neil Turton's avatar
Neil Turton committed
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
        BEQ     GSREAD_AngledThingNotThere  ; return null expansion
; well, we've found it - better stack old R0
        Pull    "R0"
        GSVarGetWSpace
 [ GS_BufferNotStack
        LDRB    r1, [r12, #GS_StackPtr]
        LDR     r2, [sp, #4*4]          ; r3,r4,r10,r0,r2,lr on stack, hence r2 retrieved
        MOV     r2, r2, ASL #32-(GS_StackLimitPos+GS_StackLimitBits)
        SUB     r2, r2, #1:SHL:(32-GS_StackLimitBits)
        TEQ     r1, r2, LSR #32-GS_StackLimitBits
        BEQ     GSREAD_CantNestMore
 |
        LDRB    R1, [R12, #GS_StackPtr]
        CMP     R1, #GS_StackPtr_Lim
        BHS     GSREAD_CantNestMore
 ]
        ADD     R12, R12, #GS_Stack
        STR     R0, [R12, R1, LSL #2]
        ADD     R1, R1, #1
 [ GS_BufferNotStack
        AND     R1, R1, #(GS_StackPtr_Lim-1)
 ]
        STRB    R1, [R12, #GS_StackPtr-GS_Stack]
        MOV     R0, R4
Robert Sprowson's avatar
Robert Sprowson committed
678
        LDRB    R1, [R0], #1            ; type
Neil Turton's avatar
Neil Turton committed
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
        CMP     R1, #VarType_Code
        BEQ     GSREAD_CallCodeVar
        CMP     R1, #VarType_Number
        LDRB    R1, [R0], #1

        LDRB    R3, [R0], #1
        ORR     R1, R1, R3, LSL #8
        LDRB    R3, [R0], #1
        ORR     R1, R1, R3, LSL #16

        BLO     GSREAD_GotVarAsString
        BHI     GSREAD_GotMacroVar
        LDRB    R3, [R0], #1
        ORR     R1, R1, R3, LSL #24
        MOV     R0, R1
        ADD     R1, R12, #GSNameBuff-GS_Stack
        MOV     R2, #256
        SWI     XOS_BinaryToDecimal
        MOV     R0, R1
        MOV     R1, R2

; it's a string variable, by now.
GSREAD_GotVarAsString
        Pull    "R3, R4, R10"
Robert Sprowson's avatar
Robert Sprowson committed
703
        ADD     stack, stack, #4        ; discard that R0
Neil Turton's avatar
Neil Turton committed
704 705 706
        Pull    "R2, lr"
        CMP     R1, #0
        BEQ     ZeroLengthVar
Robert Sprowson's avatar
Robert Sprowson committed
707
        ORR     R2, R2, R1              ; old flags+new count
Neil Turton's avatar
Neil Turton committed
708 709 710 711 712 713 714 715 716 717
        ORR     R2, R2, #GS_ReadingString
        LDRB    R1, [R0], #1
        B       GSREAD_ReturnWithChar

GSREAD_GotMacroVar
        ; Macro - R0 is now the ptr to the macro value.
        Pull    "R3, R4, R10"
        ADD     stack, stack, #4
        Pull    "R2, lr"
        ORR     R2, R2, #GS_Macroing
Robert Sprowson's avatar
Robert Sprowson committed
718
        B       GSREAD_XPandGetNextByte ; loop, transforming chars.
Neil Turton's avatar
Neil Turton committed
719 720 721 722

GSREAD_CantNestMore
        Pull    "R3, R4, R10"           ; no room to stack pointer, so don't expand
GSREAD_AngledThingNotThere
Robert Sprowson's avatar
Robert Sprowson committed
723
        ADD     stack, stack, #4        ; skip R0 - return null string
Neil Turton's avatar
Neil Turton committed
724
        Pull    "R2, lr"
Robert Sprowson's avatar
Robert Sprowson committed
725
        B       GSREAD_XPandGetNextByte ; get next char
Neil Turton's avatar
Neil Turton committed
726 727 728 729 730 731 732

GSREAD_AngleBraDaftSoIsnt
        Pull    "R0, R2, lr"
        MOV     R1, #"<"
        B       GSREAD_ReturnWithChar   ; failed to get sensible variable

GSREAD_XPandGotToEnd
Robert Sprowson's avatar
Robert Sprowson committed
733 734
        TST     R2, #GS_In_String       ; got CR or LF
        BNE     GSREAD_BadStringError   ; bad string
Neil Turton's avatar
Neil Turton committed
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
        TST     R2, #GS_Macroing
GSREAD_GotToAnEnd
        ORREQ   lr, lr, #C_bit          ; got terminator
        ExitSWIHandler EQ

        ; Nest out by one level
        GSVarGetWSpace
        LDRB    R11, [R12, #GS_StackPtr]
 [ GS_BufferNotStack
        SUB     R11, R11, #1
        AND     R11, R11, #(GS_StackPtr_Lim-1)
        MOV     r2, r2, ROR #GS_StackLimitPos+GS_StackLimitBits
        TEQ     r11, r2, LSR #32-GS_StackLimitBits
        MOV     r2, r2, ROR #32-(GS_StackLimitPos+GS_StackLimitBits)
 |
        SUBS    R11, R11, #1
 ]
        BICEQ   R2, R2, #GS_Macroing
        STRB    R11, [R12, #GS_StackPtr]
        ADD     R12, R12, #GS_Stack
        LDR     R0, [R12, R11, LSL #2]
Robert Sprowson's avatar
Robert Sprowson committed
756
        B       GSREAD_XPandGetNextByte ; return to prevstring
Neil Turton's avatar
Neil Turton committed
757 758 759 760

GSREAD_XPandGotSpace
        TST     R2, #(GS_In_String :OR: GS_Spc_term :OR: GS_Macroing)
                                        ; got space : check termination
Robert Sprowson's avatar
Robert Sprowson committed
761
        BEQ     GSREAD_GotToAnEnd       ; terminates
Neil Turton's avatar
Neil Turton committed
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

GSREAD_ReturnWithChar
        ORR     R1, R1, R10             ; valid character
        ExitSWIHandler

GSREAD_XPandGotQuote
        TST     R2, #GS_In_String
        BEQ     GSREAD_ReturnWithChar   ; if not in string, " is valid.
        LDRB    R1, [R0], #1
        CMP     R1, #""""               ; "" in string?
        BEQ     GSREAD_ReturnWithChar   ; yup


; TMD 25-Sep-89: Fix termination here

10
        CMP     R1, #" "
        LDREQB  R1, [R0], #1
        BEQ     %BT10
        SUB     R0, R0, #1
        ORR     lr, lr, #C_bit          ; got terminator (second ")
        ExitSWIHandler                  ; and out

GSREAD_WorkOutBarChar
        LDRB    R1, [R0], #1            ; got |, do traditional escaping
        CMP     R1, #"|"
        CMPNE   R1, #""""
        CMPNE   R1, #"<"
Robert Sprowson's avatar
Robert Sprowson committed
790
        BEQ     GSREAD_ReturnWithChar   ; || gives |, |" gives ", |< gives <
Neil Turton's avatar
Neil Turton committed
791
        CMP     R1, #"?"
Robert Sprowson's avatar
Robert Sprowson committed
792 793
        MOVEQ   R1, #&7F                ; delete
        BEQ     GSREAD_ReturnWithChar   ; valid ch
Neil Turton's avatar
Neil Turton committed
794 795 796 797 798
        CMP     R1, #"!"
        MOVEQ   R10, #&80
        BEQ     GSREAD_XPandGetNextByte ; tbs char
        CMP     R1, #" "
        BLT     GSREAD_BadStringError   ; OS_Control character is naff
Robert Sprowson's avatar
Robert Sprowson committed
799 800 801 802
        CMP     R1, #&7F                ; CTRL-delete is delete
        EORGT   R1, R1, #&20            ; softkey
        BGE     GSREAD_ReturnWithChar   ; now valid ch
        CMP     R1, #"`"                ; CTRL-` = CTRL-_
Neil Turton's avatar
Neil Turton committed
803 804
        MOVEQ   R1, #"_"
        CMP     R1, #"@"
Robert Sprowson's avatar
Robert Sprowson committed
805
        ANDGE   R1, R1, #&1F            ; transform if @<=ch<delete
Neil Turton's avatar
Neil Turton committed
806 807 808
        B       GSREAD_ReturnWithChar

GSREAD_RStringGetNextByte
Robert Sprowson's avatar
Robert Sprowson committed
809
        SUB     R2, R2, #1              ; we're reading a string
Neil Turton's avatar
Neil Turton committed
810 811 812 813 814
  [ GS_BufferNotStack
        MOVS    R12, R2, ASL #32-GS_StackLimitPos
  |
        ANDS    r12, r2, #&00ffffff
  ]
Robert Sprowson's avatar
Robert Sprowson committed
815 816
        LDRNEB  R1, [R0], #1            ; and this is already expanded
        ExitSWIHandler NE               ; so finished
Neil Turton's avatar
Neil Turton committed
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
ZeroLengthVar
        GSVarGetWSpace
        LDRB    R0, [R12, #GS_StackPtr] ; pull an R0 from our stack
        SUB     R0, R0, #1
 [ GS_BufferNotStack
        AND     R0, R0, #(GS_StackPtr_Lim-1)
 ]
        STRB    R0, [R12, #GS_StackPtr]
        ADD     R12, R12, #GS_Stack
        LDR     R0, [R12, R0, LSL #2]
        BIC     R2, R2, #GS_ReadingString
        B       GSREAD_XPandGetNextByte

GSREAD_BadStringError
        ADR     R0, BadStrErr
      [ International
        Push    "lr"
        BL      TranslateError
        Pull    "lr"
      ]
        ORR     lr, lr, #V_bit :OR: C_bit
        ExitSWIHandler

BadStrErr
        MakeErrorBlock BadString

GSREAD_CallCodeVar
Robert Sprowson's avatar
Robert Sprowson committed
844 845 846
        ADD     R0, R0, #3 + 4          ; 3 to ALIGN, 4 to get to read entry
        MOV     lr, PC                  ; get link
        BIC     PC, R0, #3              ; call entrypoint to Read Thunk
Neil Turton's avatar
Neil Turton committed
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
        MOV     R1, R2
        B       GSREAD_GotVarAsString

; ---------------------------------------------------------------------------


GSTRANS ROUT                    ; enables interrupts
; In   : R0 ptr to input string
       ; R1 ptr to Out buffer
       ; R2 max number of chars, with flags at top.

; Out  : R0 points at terminator
       ; R1 unchanged
       ; R2 Number of chars got,
       ;  C set if too many chars
       ;  V set if bad string.

Robert Sprowson's avatar
Robert Sprowson committed
864
        BIC     lr, lr, #C_bit
Neil Turton's avatar
Neil Turton committed
865
        Push    "R1, R3-R5, lr"
Robert Sprowson's avatar
Robert Sprowson committed
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
        WritePSRc SVC_mode, R3          ; enable ints.

        MOV     R3, R1
        MOV     R4, R1                  ; two copies of start ptr
        BIC     R5, R2, #&E0000000
        ADD     R5, R5, R1              ; 1st byte we can't write to.
        SWI     XOS_GSInit
01      CMP     R3, R5
        BHS     %FT03                   ; no rheum for byte.
        SWI     XOS_GSRead
        BVS     %FT02                   ; bad string
        STRB    R1, [R3], #1
        BCC     %BT01
04      SUB     R2, R3, R4              ; no chars got
        SUB     R2, R2, #1
Neil Turton's avatar
Neil Turton committed
881 882 883
        Pull    "R1, R3-R5, lr"
        ExitSWIHandler

Robert Sprowson's avatar
Robert Sprowson committed
884
02      SUB     R2, R3, R4
Neil Turton's avatar
Neil Turton committed
885 886 887
        Pull    "R1, R3-R5, lr"
        B       SLVK_SetV               ; bad string: error set by GSRead

Robert Sprowson's avatar
Robert Sprowson committed
888
03      SUB     R2, R3, R4
Neil Turton's avatar
Neil Turton committed
889
        Pull    "R1, R3-R5, lr"
Robert Sprowson's avatar
Robert Sprowson committed
890
        ORR     lr, lr, #C_bit          ; buffer overflow
Neil Turton's avatar
Neil Turton committed
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
        ExitSWIHandler

;****************************************************************************
; Read/Write variables
; Also the binary->decimal SWI.
; All the var SWIs enable interrupts - they all take quite a while.

; First the lookup SWI, ReadVarValue
; In:
;   R0 -> name; maybe wildcarded (* and #)
;   R1 -> buffer
;   R2 = buffer max length
;   R3 = 0 or pointer to name returned from previous ReadVarVal
;   R4 = VarType_Expanded or something else

; Out:
;  Not found:
;   R0 -> VarCantFind error
;   R1 unaltered
;   R2 = 0
;   R3,r4 trashed
;   VSet
;  Found, r2 < 0 and r4 <> VarType_Expanded on entry:
;   R0, R1 unaltered
;   R2 = NOT length of value
;   R3 -> name of variable (0-terminated)
;   R4 = type of result
;  Found, r2 < 0 and r4 = VarType_Expanded on entry:
;   R0, R1 unaltered
;   R2 = -ve
;   R3 -> name of variable (0-terminated)
;   R4 = type of result
;  Found, r2 >= 0 on entry:
;   R0, R1 unaltered
;   R2 no chars got
;   R3 -> name of variable. Can be passed to this SWI to continue enumeration
;      of wildcard.
;   R4 type of result (VarType_String, VarType_Number, VarType_Macro)
;   VSet if buffer overflowed (R0->error block)

931 932 933 934
 [ Oscli_QuickAliases
;  R0 in = -1 is special case: skip call to VarFindIt (r5,r6,r7 in supply r3,r4,r12)
 ]

Neil Turton's avatar
Neil Turton committed
935
ReadVarValue ROUT
Robert Sprowson's avatar
Robert Sprowson committed
936
        WritePSRc SVC_mode, r11         ; enable interupts (mode remains unchanged)
Neil Turton's avatar
Neil Turton committed
937 938 939 940
        Entry   "r0,r1"

        MOV     r11, r4

941 942 943 944 945 946 947 948 949 950
  [ Oscli_QuickAliases
        CMP     r0, #-1
        BNE     rvv_noqaspecialentry
        MOV     r3,r5
        MOV     r4,r6
        MOV     r12,r7
        B       rvv_qaspecialentry
rvv_noqaspecialentry
  ]

Robert Sprowson's avatar
Robert Sprowson committed
951
        BL      VarFindIt               ; name=r0,context=r3 -> name found in node=r3,r4=after namein,r12=prev
Neil Turton's avatar
Neil Turton committed
952 953
        BEQ     RVVNotFound

954 955 956
  [ Oscli_QuickAliases
rvv_qaspecialentry
  ]
Neil Turton's avatar
Neil Turton committed
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
        ; Regardless of expanded or not - always call the code to get value
        LDRB    lr, [r4], #1
        TEQ     lr, #VarType_Code
        BEQ     ReadVarVal_CallCode

        ; Check whether expanded value wanted and pick up found variable's type
        TEQ     r11, #VarType_Expanded
        MOV     r11, r4
        MOV     r4, lr
        BEQ     ReadVarVal_ExpandedWanted

        ; Unexpanded value wanted....

        ; If number then want 4 bytes, else however many there are in the varval
        TEQ     r4, #VarType_Number
        MOVEQ   r10, #4

ReadVarVal_CopyStringVarToUserBuffer
        ; R1 -> user buffer
        ; R2 = user buffer size
        ; R3 -> name of sysvar found
        ; R4 = sysvar type to return
        ; R10 = length to transfer to user buffer (EQ only)
        ; R11 -> length byte(s) of sysvar (NE only)
        ;     -> bytes string to transfer (EQ only)
        LDRNEB  r10, [r11], #1
        LDRNEB  lr, [r11], #1
        ORRNE   r10, r10, lr, ASL #8
        LDRNEB  lr, [r11], #1
        ORRNE   r10, r10, lr, ASL #16

ReadVarVal_CopyR10BytesToUserBuffer
        ; R1 -> user buffer
        ; R2 = user buffer size
        ; R3 -> name of sysvar found
        ; R4 = type byte to be returned
        ; R10 = bytes to be copied
        ; R11 -> bytes to be copied

        CMP     R10, R2
        BGT     ReadVarVal_BufWillOFlow

VarNoOFlo
        ; Guaranteed the the buffer won't overflow now
Robert Sprowson's avatar
Robert Sprowson committed
1001
        MOV     R2, R10                 ; bytes he's gonna get
Neil Turton's avatar
Neil Turton committed
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
; now copy R10 bytes into buffer
02      SUBS    R10, R10, #1
        LDRPLB  R12, [R11, R10]
        STRPLB  R12, [R1, R10]
        BPL     %BT02

ReadVarVal_OKExit
        PullEnv
        ExitSWIHandler

ReadVarVal_BufWillOFlow
        ; Have determined that the buffer will overflow, so generate an error
        ; and shorten down to the buffer's size
        ADR     r0, BufferOFloError
    [ International
        BL      TranslateError
    ]
        STR     r0, [stack, #Proc_LocalStack + 0*4]
        LDR     lr, [stack, #Proc_LocalStack + 2*4]
        ORR     lr, lr, #V_bit     ; set for return
        STR     lr, [stack, #Proc_LocalStack + 2*4]

        ; ensure NOT length returned in r2 when returning with r2<0 on entry
        CMP     r2, #0
        MVNMI   r10, r10
        MOVPL   r10, r2
        B       VarNoOFlo

BufferOFloError
        MakeErrorBlock BuffOverflow

ReadVarVal_CallCode
        Push    "r0-r2"                 ; read sysvar : r4 points after type
        ADD     r11, r4, #3 + 4         ; 3 to align and 4 to get to read entry
        MOV     lr, pc                  ; construct link
        BIC     pc, r11, #3             ; call read code in var
        MOV     r11, r0                 ; ptr to value
        MOV     r10, r2                 ; no of chars.
        Pull    "r0-r2"

        ; error may be returned from reading the var val
        MOVVS   r0, r11
        BVS     ReadVarVal_TestVExit

        MOV     r4, #VarType_String
        B       ReadVarVal_CopyR10BytesToUserBuffer

ReadVarVal_ExpandedWanted
        ; Request for expanded value....

        ; Check for number, string or macro
        CMP     R4, #VarType_Number
        BLT     ReadVarVal_CopyStringVarToUserBuffer
        BEQ     ReadVarVal_FoundNumber

; macro - gstrans it. R1 buffer ptr, r2 max chars, R11+1 ptr to value.
; Macros have a terminator after their value, to allow GSTRANS.

Robert Sprowson's avatar
Robert Sprowson committed
1060 1061 1062
        CMP     r2, #0                  ; if negative, then don't call GSTrans because bits 29..31 have
        MVNMI   r10, r2                 ; return r2 out by this method
        BMI     ReadVarVal_BufWillOFlow ; a special meaning - just branch back to the normal overflow code
Neil Turton's avatar
Neil Turton committed
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077

        ADD     r0, r11, #3             ; skip length
        SWI     XOS_GSTrans
        BVS     ReadVarVal_TestVExit
        BCC     ReadVarVal_OKExit

        ADR     R0, BufferOFloError
      [ International
        BL      TranslateError
      ]
        B       ReadVarVal_SetVExit


ReadVarVal_FoundNumber
        ; Found a number - extract its value and convert to string
Robert Sprowson's avatar
Robert Sprowson committed
1078
        LDRB    R0, [R11], #1           ; number - convert to string.
Neil Turton's avatar
Neil Turton committed
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
        LDRB    R12, [R11], #1
        ORR     R0, R0, R12, LSL #8
        LDRB    R12, [R11], #1
        ORR     R0, R0, R12, LSL #16
        LDRB    R12, [R11]
        ORR     R0, R0, R12, LSL #24

        ; got number in R0, buffptr in R1, max chars in R2
        SWI     XOS_BinaryToDecimal

        MOV     r4, #VarType_String

ReadVarVal_TestVExit
        STRVS   r0, [stack, #Proc_LocalStack + 0*4]
        PullEnv
        B       SLVK_TestV

RVVNotFound
 [ International
        MOV     r4, r0
        ADR     r0, RVVNFError
        BL      TranslateError_UseR4
 |
        ADR     R0, RVVNFError
 ]
        MOV     r2, #0                  ; indicate not found.

ReadVarVal_SetVExit
        STR     r0, [stack, #Proc_LocalStack + 0*4]
        PullEnv
        B       SLVK_SetV               ; general error return

RVVNFError
        MakeErrorBlock VarCantFind

;***************************************************************************

; The convert number to string SWI
; In  : R0 signed 32-bit integer
;       R1 pointer to buffer
;       R2 max buffer length
; Out : R0, R1 unmodified
;       R2 actual chars given
;       V Set if buffer overflow

; Format : - if negative, leading zeros stripped.

CvtToDecimal ROUT
        Push    "R0, R3-R5"
        MOV     R12, R2
        MOV     R2, #0
        CMP     R0, #0
        BPL     %FT01
        SUBS    R12, R12, #1
        BMI     %FT10
        MOV     R11, #"-"
        STRB    R11, [R1]
        MOV     R2, #1
        RSB     R0, R0, #0

; now do digits.

01      RSB     R0, R0, #0          ; get negative so minint works.
        ADR     R3, TenTimesTable
        MOV     R10, #9            ; max entry
        MOV     R4, #0             ; non-0 had flag
02      LDR     R11, [R3, R10, LSL #2]
        MOV     R5, #-1            ; digit value
03      ADDS    R0, R0, R11
        ADD     R5, R5, #1
        BLE     %BT03
        SUB     R0, R0, R11
        CMP     R5, #0
        CMPEQ   R4, #0
        BNE     %FT04             ; put digit
05      SUBS    R10, R10, #1
        BPL     %BT02             ; next digit
        CMP     R4, #0
        BEQ     %FT04             ; R5 must be 0
        Pull    "R0, R3-R5"
        ExitSWIHandler

04      SUBS    R12, R12, #1
        BMI     %FT10             ; naff Exit
        ADD     R5, R5, #"0"
        MOV     R4, #-1
        STRB    R5, [R1, R2]
        ADD     R2, R2, #1
        B       %BT05
10
        ADR     R0, BufferOFloError
    [ International
        Push    "lr"
        BL      TranslateError
        Pull    "lr"
    ]
        Pull    "R3"              ; discard R0 in
        Pull    "R3-R5"
        B       SLVK_SetV

TenTimesTable
Robert Sprowson's avatar
Robert Sprowson committed
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
      &         1
      &         10
      &         100
      &         1000
      &         10000
      &         100000
      &         1000000
      &         10000000
      &         100000000
      &         1000000000
Neil Turton's avatar
Neil Turton committed
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240

; *****************************************************************************
; SWI OS_SetVarVal : create/update/destroy a variable.

; In:   R0 pointer to name (can be wildcarded for update/delete)
;             ctrl/char or space terminated
;       R1 pointer to value. String values must be CR or LF terminated.
;       R2 negative means destroy the variable. +ve is update/create
;       R3 name pointer or 0
;       R4 type.
;
;  Evaluation of value : this depends on the type.
;  VarType_String   : GSTRANS the given value
;  VarType_Number   : Value is a 4 byte (signed) integer
;  VarType_Macro    : Copy value (may be GSTRANSed on use)
;  VarType_Expanded : the value is a string which should be evaluated as an
;                     expression. Variable is then numeric or string
;  VarType_LiteralString : Copy the given value as a string
;
;  VarType_Code     : R2 is the length of the code to copy in, including
;                     padding to align the code.
;                     Can only delete sysvars if R4 = VarType_Code

; Out:  R3 new name pointer (so can delete all occurrences of f*, etc.
;          slightly more efficiently).
;       R4 type created for expressions
;       V set for :
;          1) bad name  (creation of wildcarded names is banned)
;          2) Bad string from GSTRANS
;          3) Bad macro value (control codes not allowed)
;          4) Bad expression from ReadExpression
;          5) Can't find (for deletion)
;          6) Not enough room to create/update it (system heap full)
;          7) Value too long (variables are limited to 256 bytes in length)
;          8) Bad type (update/create)


 [ DebugSysVars
SysVar_Write0 Entry "r0,r1"
        MOV     r1, r0
10
        LDRB    r0, [r1], #1
        CMP     r0, #" "
        EXIT    LO
        SWI     XOS_WriteC
        B       %BT10

 ]

SetVarValue
        ; enable IRQs
Kevin Bracey's avatar
Kevin Bracey committed
1241
        WritePSRc SVC_mode, r10
Neil Turton's avatar
Neil Turton committed
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

        Entry   "r0,r1,r2,r4,r5,r6,r9"

 [ DebugSysVars
        SWI     XOS_WriteS
        =       "SetVarVal ",0
        BL      SysVar_Write0
        SWI     XOS_NewLine
 ]

        MOV     r9, stack
        MOV     r10, r4

        CMP     r2, #0
        BMI     SetVarVal_DeleteIt

        ; Range check type
        CMP     r10, #VarType_Code
        CMPNE   r10, #VarType_LiteralString
        ADRHIL  r0, ErrorBlock_BadVarType
        BHI     SetVarValBadExit_Translate

        ; Always expand a VarType_Expanded:
        TEQ     r10, #VarType_Expanded
        BNE     SetVarVal_AintExpanded

        ; Process VarType_Expanded
1269

1270 1271 1272 1273 1274 1275 1276 1277
  [ LongCommandLines
        MOV     r0, stack, LSR #20      ; SVC stack base assumed on 1M boundary
        SUB     r0, sp, r0, LSL #20     ; amount of stack left
        CMP     r0, #LongCLISize + 2048 ; insist on 2k left after long buffer
        MOVHS   r2, #LongCLISize        ; ok, got a long buffer
        MOVLO   r2, #256                ; stack full-ish, use a 256 buffer and hope it's big enough
        SUB     stack, stack, r2
  |
Neil Turton's avatar
Neil Turton committed
1278
        SUB     stack, stack, #256
1279
        MOV     r2, #256
1280
  ]
Neil Turton's avatar
Neil Turton committed
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321

        MOV     r0, r1                  ; ptr to expression
        MOV     r1, stack
        SWI     XOS_EvaluateExpression
        BVS     SetVarVal_TestVExit
        TEQ     r1, #0   ; integer?
        MOVNE   r10, #VarType_LiteralString
        MOVEQ   r10, #VarType_Number
        STREQ   r2, [stack]
        MOVEQ   r2, #4
        STR     r10, [r9, #3*4]         ; r4 out
        MOV     r1, stack
        LDR     r0, [r9, #0*4]

SetVarVal_AintExpanded

        ; Setting a variable
        BL      VarFindIt
        BNE     SetVarVal_NodeAlreadyExists

        ; Node missing....

        ; Check variable name has no wildcards
        SUB     r4, r0, #1
05
        LDRB    lr, [r4, #1]!
        CMP     lr, #"#"
        CMPNE   lr, #"*"
        CMPNE   lr, #" "
        BHI     %BT05
        CMP     lr, #"#"
        CMPNE   lr, #"*"
        CMPNE   r4, r0
        ADREQL  r0, ErrorBlock_BadVarNam
        BEQ     SetVarValBadExit_Translate     ; error no. 1)

        ; R12 index of 1st entry in QuickIndex >= the entry we're interested in
        MOV     r3, #0                  ; To indicate absence of current
        B       SetVarVal_CreateNode

SetVarVal_NodeAlreadyExists
Robert Sprowson's avatar
Robert Sprowson committed
1322
        MOV     r0, r3                  ; If already there use that's name in case supplied name wildcarded
Neil Turton's avatar
Neil Turton committed
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
        LDRB    lr, [r4]
        TEQ     lr, #VarType_Code
        BNE     SetVarVal_CreateNode
        TEQ     r10, #VarType_Code
        BEQ     SetVarVal_CreateNode

        ; Assign non code value to code node
        CMP     r10, #VarType_Number
        BHI     SetVarVal_AssignToCodeDoIt

        BLO     SetVarVal_AssignStringToCode

Robert Sprowson's avatar
Robert Sprowson committed
1335
        SUB     stack, stack, #256      ; buffer
1336 1337
        MOV     r2, #256

Neil Turton's avatar
Neil Turton committed
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
        ; Assign a number to the code variable
        LDRB    r0, [r1], #1
        LDRB    lr, [r1], #1
        ORR     r0, r0, lr, LSL #8
        LDRB    lr, [r1], #1
        ORR     r0, r0, lr, LSL #16
        LDRB    lr, [r1], #1
        ORR     r0, r0, lr, LSL #24
        MOV     r1, stack
        SWI     XOS_BinaryToDecimal

        B       SetVarVal_AssignToCodeDoIt

SetVarVal_AssignStringToCode

1353 1354 1355 1356 1357 1358 1359 1360
  [ LongCommandLines
        MOV     r0, stack, LSR #20      ; SVC stack base assumed on 1M boundary
        SUB     r0, sp, r0, LSL #20     ; amount of stack left
        CMP     r0, #LongCLISize + 2048 ; insist on 2k left after long buffer
        MOVHS   r2, #LongCLISize        ; ok, got a long buffer
        MOVLO   r2, #256                ; stack full-ish, use a 256 buffer and hope it's big enough
        SUB     stack, stack, r2
  |
1361 1362
        SUB     stack, stack, #256
        MOV     r2, #256
1363
  ]
1364

Neil Turton's avatar
Neil Turton committed
1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
        ; Expand string to stack frame then do it
        MOV     r0, r1
        MOV     r1, stack
        SWI     XOS_GSTrans
        BVS     SetVarVal_TestVExit
        ADRCSL  r0, ErrorBlock_VarTooLong
        BCS     SetVarValBadExit_Translate

SetVarVal_AssignToCodeDoIt

        ADDS    r4, r4, #3 + 1          ; skip type, add 3 for ALIGN , clear V
        MOV     lr, PC
        BIC     PC, R4, #3              ; complete align and call

        B       SetVarVal_TestVExit


SetVarVal_CreateNode
        ; Create a node
        ;
        ; r0 -> name (already confirmed non-wildcarded)
        ; r1 -> value
        ; r2 = length (where appropriate)
        ; r3 = this or 0
        ; r10 = type
        ; r12 = insertion point

        MOV     r5, r1
        MOV     r6, r3

        ; first work out the length of those things we can work the length of

        ; Header and name...
1398
        MOV     r3, #0                  ;accumulator for length of things we know
Neil Turton's avatar
Neil Turton committed
1399 1400 1401 1402 1403 1404 1405
        MOV     r1, r0
10
        LDRB    lr, [r1], #1
        ADD     r3, r3, #1
        CMP     lr, #" "
        BHI     %BT10

1406 1407 1408
;r3 is now name length +1 for terminator
        ADD     r3, r3, #1              ;add in 1 for the type byte

Neil Turton's avatar
Neil Turton committed
1409 1410
        ; Deal with number and string type
        CMP     r10, #VarType_Number
1411
        ADDLO   r3, r3, #64             ; only an initial guess for the string type
Neil Turton's avatar
Neil Turton committed
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
        ADDEQ   r3, r3, #4
        MOVEQ   r2, #4
        BLS     SetVarVal_GotInitialLength

        CMP     r10, #VarType_Code
        ADDEQ   r3, r3, #3              ; ALIGN
        BICEQ   r3, r3, #3
        ADDEQ   r3, r3, r2              ; code
        BEQ     SetVarVal_GotInitialLength

        TEQ     r10, #VarType_LiteralString
        BEQ     %FT20

        ; Macro - strlen and check the value is vaguely OK
        MOV     r2, r5
15
        LDRB    lr, [r2], #1
        CMP     lr, #" "
        BHS     %BT15
        TEQ     lr, #0                  ; must terminate with NUL, CR or LF
        TEQNE   lr, #10
        TEQNE   lr, #13
        ADRNE   r0, ErrorBlock_BadMacVal
        BNE     SetVarValBadExit_Translate
        SUB     r2, r2, r5
20
        ADD     r3, r3, r2
        ADD     r3, r3, #3              ; for the length bytes

SetVarVal_GotInitialLength
        ; r0 -> node's name
        ; r2 = value length (Number, Macro and Code)
        ; r3 = node length needed (maybe initial guess for Strings)
        ; r5 -> value (r1 in)
        ; r6 -> name of node to be replaced (0 if no node being replaced)
        ; r10 = value's type (String, Number, Macro or Code)
        ; r12 -> insertion point

1450 1451 1452 1453 1454 1455
        Push    "r0-r2"
  [ SysVars_StickyNodes
        ADD     r3,r3,#SysVars_StickyNode_UnitSize-1
        BIC     r3,r3,#SysVars_StickyNode_UnitSize-1  ;so we don't fight sticky routines over sizes
        BL      SysVars_ClaimVNode
  |
Neil Turton's avatar
Neil Turton committed
1456
        BL      ClaimSysHeapNode
1457 1458
  ]
        Pull    "r0-r2",VS
Neil Turton's avatar
Neil Turton committed
1459 1460 1461 1462 1463
        BVS     SetVarVal_VarNoRoom

        ; Got a heap block - fill it in

        ; Copy name
1464 1465 1466

        MOV     r4, r2
        LDR     r0,[sp]
Neil Turton's avatar
Neil Turton committed
1467 1468 1469 1470 1471 1472 1473
25
        LDRB    lr, [r0], #1
        CMP     lr, #" "
        MOVLS   lr, #0
        STRB    lr, [r4], #1
        BHI     %BT25

1474
        LDR     r1,[sp,#8]
Robert Sprowson's avatar
Robert Sprowson committed
1475
        ADD     sp,sp,#12               ; balances push r0-r2 above - value length (entry r2) now in r1
1476

Neil Turton's avatar
Neil Turton committed
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
        ; Variable's type
        TEQ     r10, #VarType_LiteralString
        MOVEQ   lr, #VarType_String
        MOVNE   lr, r10
        STRB    lr, [r4], #1

        TEQ     r10, #VarType_String
        BEQ     SetVarVal_FillInString

        TEQ     r10, #VarType_Code
        ADDEQ   r4, r4, #3
        BICEQ   r4, r4, #3
1489
        Push    "r4",EQ ; Remember start of code block for code variables
Neil Turton's avatar
Neil Turton committed
1490 1491 1492 1493 1494
        TEQNE   r10, #VarType_Number
        BEQ     SetVarVal_CopyR1BytesToR4

        ; For macro type fill in a length
        TEQ     r10, #VarType_Macro
Robert Sprowson's avatar
Robert Sprowson committed
1495
        SUBEQ   r1, r1, #1              ; ghastly fudge to avoid display of terminator
Neil Turton's avatar
Neil Turton committed
1496 1497 1498 1499 1500 1501
        STRB    r1, [r4], #1
        MOV     r1, r1, ROR #8
        STRB    r1, [r4], #1
        MOV     r1, r1, ROR #8
        STRB    r1, [r4], #1
        MOV     r1, r1, ROR #16
Robert Sprowson's avatar
Robert Sprowson committed
1502
        ADDEQ   r1, r1, #1              ; undo ghastly fudge
Neil Turton's avatar
Neil Turton committed
1503 1504 1505 1506 1507 1508

        B       %FT35
30
        LDRB    lr, [r5], #1
        STRB    lr, [r4], #1
35
1509
SetVarVal_CopyR1BytesToR4
Neil Turton's avatar
Neil Turton committed
1510 1511 1512
        SUBS    r1, r1, #1
        BHS     %BT30

1513 1514 1515 1516 1517 1518 1519 1520 1521
        TEQ     r10, #VarType_Code
        BNE     SetVarVal_NewNodeReady

        Pull    "r1" ; Grab pointer to start of code block
        Push    "r0,r2"
        MOV     r0,#1
        MOV     r1,r4
        SWI     XOS_SynchroniseCodeAreas
        Pull    "r0,r2"
Neil Turton's avatar
Neil Turton committed
1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551
        B       SetVarVal_NewNodeReady

SetVarVal_FillInString
        ; Here's the real smart bit of code

        ; The idea is this:
        ; Instead of GSTransing, we GSInit and GSRead ourselves. When the
        ; block gets full expand it and carry on. At the end the block is shrunk to fit.

        ADD     r4, r4, #3              ; for the length bytes
        MOV     r11, r4                 ; preserve location of string start for when we've done
        MOV     r0, r5                  ; r1 in
        MOV     r5, r2
        MOV     r2, #0
        SWI     XOS_GSInit
        BVS     SetVarVal_DisasterExpandingString
        B       %FT45

40
        SWI     XOS_GSRead
        BVS     SetVarVal_DisasterExpandingBadString
        BCS     SetVarVal_StringFinishedExpanding
        STRB    r1, [r4], #1
        CMP     r4, r3
        BLO     %BT40

        ; Run out of room in this block - stretch it
        Push    "r0-r2"
        MOV     r0, #HeapReason_ExtendBlock
        MOV     r2, r5
Robert Sprowson's avatar
Robert Sprowson committed
1552
        MOV     r3, #64                 ;should be a multiple of StickyNode_UnitSize if SysVars_StickyNodes TRUE
1553 1554 1555
  [ SysVars_StickyNodes
        BL      SysVars_ExpandOrShrinkVNode
  |
Neil Turton's avatar
Neil Turton committed
1556
        BL      DoSysHeapOpWithExtension
1557
  ]
Neil Turton's avatar
Neil Turton committed
1558 1559 1560 1561 1562 1563 1564 1565 1566 1567
        STRVS   r0, [sp]
        SUBVC   lr, r2, r5
        ADDVC   r4, r4, lr
        ADDVC   r11, r11, lr
        MOVVC   r5, r2
        Pull    "r0-r2"
        BVS     SetVarVal_DisasterExpandingString

45
        LDR     r3, [r5, #-4]           ; The heap block's size
1568 1569 1570
  [ SysVars_StickyNodes
        SUB     r3, r3, #8              ; the amount we're allowed to use
  |
Neil Turton's avatar
Neil Turton committed
1571
        SUB     r3, r3, #4              ; the amount we're allowed to use
1572
  ]
Neil Turton's avatar
Neil Turton committed
1573 1574 1575 1576 1577 1578 1579
        ADD     r3, r3, r5              ; the block's end
        B       %BT40

SetVarVal_StringFinishedExpanding

        ; Shorten block to required size
        MOV     r0, #HeapReason_ExtendBlock
1580 1581 1582 1583 1584 1585 1586 1587 1588
  [ SysVars_StickyNodes
        Push    "r4"
        ADD     r4, r4, #SysVars_StickyNode_UnitSize-1
        BIC     r4, r4, #SysVars_StickyNode_UnitSize-1  ;so we don't fight over sticky sizes
        SUB     r3, r4, r3
        Pull    "r4"
        MOV     r2, r5
        BL      SysVars_ExpandOrShrinkVNode
  |
Neil Turton's avatar
Neil Turton committed
1589 1590 1591
        SUB     r3, r4, r3
        MOV     r2, r5
        BL      DoSysHeapOpWithExtension
1592
  ]
Neil Turton's avatar
Neil Turton committed
1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
        BVS     SetVarVal_DisasterExpandingString

        ; Relocate pointers
        SUB     lr, r2, r5
        ADD     r4, r4, lr
        ADD     r11, r11, lr

        ; Work out ultimate size and store it
        SUB     lr, r4, r11
        STRB    lr, [r11, #-3]
        MOV     lr, lr, LSR #8
        STRB    lr, [r11, #-2]
        MOV     lr, lr, LSR #8
        STRB    lr, [r11, #-1]

SetVarVal_NewNodeReady
        ; r2 -> new node
        ; r6 -> old node's name (or is 0 if no old node)
        ; r12 = insertion point

 [ DebugSysVars
        Push    "r0,r1,r2"
        SUB     sp, sp, #12
        MOV     r0, r2
        MOV     r1, sp
        MOV     r2, #12
        SWI     XOS_ConvertHex8
        SWI     XOS_Write0
        SWI     XOS_WriteI+" "
        MOV     r0, r6
        MOV     r1, sp
        MOV     r2, #12
        SWI     XOS_ConvertHex8
        SWI     XOS_Write0
        SWI     XOS_WriteI+" "
        MOV     r0, r12
        MOV     r1, sp
        MOV     r2, #12
        SWI     XOS_ConvertHex8
        SWI     XOS_Write0
        SWI     XOS_WriteI+" "
        ADD     sp, sp, #12
        Pull    "r0,r1,r2"
 ]
Jeffrey Lee's avatar
Jeffrey Lee committed
1637
        LDR     r11, =ZeroPage+VariableList
Neil Turton's avatar
Neil Turton committed
1638
        LDR     r10, [r11]
1639 1640 1641 1642
  [ SysVars_QuickContext
        TEQ     r10,#0
        ADDNE   r10,r10,#SysVars_VTableOffset
  ]
Neil Turton's avatar
Neil Turton committed
1643 1644 1645 1646 1647 1648 1649 1650 1651
        MOV     r5, r2
        TEQ     r6, #0
        BEQ     SetVarVal_Insertion

 [ DebugSysVars
        SWI     XOS_WriteS
        =       "-straight replace-",0
 ]

1652 1653 1654 1655
        MOV     r2, r6
  [ SysVars_StickyNodes
        BL      SysVars_FreeVNode
  |
Neil Turton's avatar
Neil Turton committed
1656
        BL      FreeSysHeapNode
1657
  ]
Neil Turton's avatar
Neil Turton committed
1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671
        B       SetVarVal_Replace

SetVarVal_Insertion
 [ DebugSysVars
        SWI     XOS_WriteS
        =       "-insert-",0
 ]
        TEQ     r10, #0
        BNE     SetVarVal_PossibleExtend

 [ DebugSysVars
        SWI     XOS_WriteS
        =       "-create index-",0
 ]
1672 1673 1674 1675 1676 1677

  [ SysVars_QuickContext
        ; SysVars_Vindex_NStart nodes, 1 word for the count, plus data before table
        MOV     r3, #SysVars_Vindex_NStart*4
        ADD     r3, r3, #4+SysVars_VTableOffset
  |
Robert Sprowson's avatar
Robert Sprowson committed
1678
        MOV     r3, #(10*4)+4           ; 10 nodes and 1 word for the count
1679
  ]
Robert Sprowson's avatar
Robert Sprowson committed
1680
        BL      ClaimSysHeapNode        ; this is not a variable node (its the index)
Neil Turton's avatar
Neil Turton committed
1681
        BVS     SetVarVal_NoRoomForIndex
1682 1683 1684 1685
  [ SysVars_QuickContext
        MOV     r10,#-1
        STR     r10, [r2, #SysVars_LastContext]    ; initialise last context as invalid
  ]
Neil Turton's avatar
Neil Turton committed
1686 1687 1688 1689 1690 1691 1692 1693 1694 1695
        MOV     r10, r2
        MOV     r4, #0
        B       SetVarVal_DoInsertNewBlock

SetVarVal_PossibleExtend
 [ DebugSysVars
        SWI     XOS_WriteS
        =       "-extend index-",0
 ]
        LDR     r4, [r10]
1696 1697 1698 1699
  [ SysVars_QuickContext
        LDR     lr, [r10, #-(4+SysVars_VTableOffset)] ; Block length, from heap block
        SUB     lr, lr, #4+4+SysVars_VTableOffset     ; 4 for heap adjustment, 4 for entry count word, plus name buffer
  |
Robert Sprowson's avatar
Robert Sprowson committed
1700 1701
        LDR     lr, [r10, #-4]          ; Block length, from heap block
        SUB     lr, lr, #4+4            ; 4 for heap adjustment and 4 for entry count word
1702
  ]
Neil Turton's avatar
Neil Turton committed
1703 1704 1705 1706 1707 1708 1709 1710 1711
        CMP     lr, r4, ASL #2
        BHI     SetVarVal_DoInsert      ; we've got room with this block

 [ DebugSysVars
        SWI     XOS_WriteS
        =       "-do extend-",0
 ]

        MOV     r0, #HeapReason_ExtendBlock
1712 1713 1714 1715
  [ SysVars_QuickContext
        SUB     r2, r10, #SysVars_VTableOffset
        MOV     r3, #SysVars_Vindex_NBump*4     ; room for SysVars_Vindex_NBump more nodes
  |
Neil Turton's avatar
Neil Turton committed
1716
        MOV     r2, r10
Robert Sprowson's avatar
Robert Sprowson committed
1717
        MOV     r3, #40                 ; room for 10 more nodes
1718
  ]
Robert Sprowson's avatar
Robert Sprowson committed
1719
        BL      DoSysHeapOpWithExtension        ;not a variable node (expanding index)
Neil Turton's avatar
Neil Turton committed
1720 1721 1722 1723 1724 1725
        BVS     SetVarVal_NoRoomForIndex

        MOV     r10, r2

SetVarVal_DoInsertNewBlock
        STR     r10, [r11]
1726 1727 1728
  [ SysVars_QuickContext
        ADD     r10,r10,#SysVars_VTableOffset
  ]
Neil Turton's avatar
Neil Turton committed
1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776
SetVarVal_DoInsert
 [ DebugSysVars
        SWI     XOS_WriteS
        =       "-doinsert-",0
 ]
        ADD     r0, r10, r12, ASL #2    ; insertion point
        ADD     r1, r10, r4, ASL #2     ; rover
        B       SetVarVal_DoInsertEnd

SetVarVal_DoInsertStart
        LDR     lr, [r1], #-4
        STR     lr, [r1, #8]

SetVarVal_DoInsertEnd
        CMP     r1, r0
        BHS     SetVarVal_DoInsertStart

        ADD     r4, r4, #1
        STR     r4, [r10]

SetVarVal_Replace
 [ DebugSysVars
        SWI     XOS_WriteS
        =       "-doreplace-",0
 ]
        STR     r5, [r10, r12, ASL #2]
 [ DebugSysVars
        SWI     XOS_WriteS
        =       "-done-",0
 ]

        ; All done
        B       SetVarVal_TestVExit

SetVarVal_DeleteIt
        BL      VarFindIt

        ; Error if not found
        ADREQL  r0, ErrorBlock_VarCantFind ; V set no. 1)
        BEQ     SetVarValBadExit_Translate
        ; Check if found vartype code that the supplied vartype was code too
        LDRB    lr, [r4]
        TEQ     lr, #VarType_Code
        BNE     %FT80

        TEQ     r10, #VarType_Code
        BNE     SetVarVal_TestVExit
80
Jeffrey Lee's avatar
Jeffrey Lee committed
1777
        LDR     r11, =ZeroPage+VariableList
Neil Turton's avatar
Neil Turton committed
1778
        LDR     r10, [r11]
1779 1780 1781
  [ SysVars_QuickContext
        ADD     r10,r10,#SysVars_VTableOffset
  ]
Neil Turton's avatar
Neil Turton committed
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791
        LDR     r4, [r10]
        ADD     r0, r10, r12, ASL #2    ; rover
        ADD     r1, r10, r4, ASL #2     ; end
        B       SetVarVal_DoRemoveEnd
SetVarVal_DoRemoveStart
        LDR     lr, [r0, #4]!
        STR     lr, [r0, #-4]
SetVarVal_DoRemoveEnd
        CMP     r0, r1
        BLO     SetVarVal_DoRemoveStart
1792 1793 1794 1795
        MOV     r2, r3
  [ SysVars_StickyNodes
        BL      SysVars_FreeVNode
  |
Neil Turton's avatar
Neil Turton committed
1796
        BL      FreeSysHeapNode
1797
  ]
Neil Turton's avatar
Neil Turton committed
1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816

        ; Reduce number of nodes
        SUB     r4, r4, #1
        STR     r4, [r10]

        ; Construct best guess context ptr to be prev node (if present)
        TEQ     r12, #1
        SUBHI   r12, r12, #1
        LDRHI   r3, [r10, r12, ASL #2]
        MOVLS   r3, #0


SetVarVal_TestVExit
        MOV     stack, r9
        STRVS   r0, [stack]
        PullEnv
        B       SLVK_TestV

SetVarValBadExit_Translate
1817
 [ International
Neil Turton's avatar
Neil Turton committed
1818
        BL      TranslateError
1819
 ]
1820
SetVarValBadExit_NoTranslate
Neil Turton's avatar
Neil Turton committed
1821 1822 1823 1824 1825 1826
        SETV
        B       SetVarVal_TestVExit

SetVarVal_DisasterExpandingString
SetVarVal_NoRoomForIndex
        MOV     r2, r5
1827
        BL      FreeSysHeapNode         ;forget stickiness (return node to heap is best here)
Neil Turton's avatar
Neil Turton committed
1828 1829 1830 1831 1832 1833 1834
SetVarVal_VarNoRoom
        ADR     r0, ErrorBlock_VarNoRoom
        B       SetVarValBadExit_Translate

SetVarVal_DisasterExpandingBadString
        Push    "r0"                    ; Save bad string error
        MOV     r2, r5
1835
        BL      FreeSysHeapNode         ;forget stickiness (return node to heap is best here)
Neil Turton's avatar
Neil Turton committed
1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854
        Pull    "r0"
        SETV
        B       SetVarVal_TestVExit

        MakeErrorBlock BadVarType
        MakeErrorBlock BadVarNam
        MakeErrorBlock VarTooLong
        MakeErrorBlock BadMacVal
        MakeErrorBlock VarNoRoom


; *****************************************************************************
; Utility routines.

; -----------------------------------------------------------------------------
;
; VarFindIt
;
; In
1855
;    r0 -> (wildcard) name of variable to find
Neil Turton's avatar
Neil Turton committed
1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874
;    r3 = context pointer
;
; Out
;    r3 = name pointer
;    r4 = pointer after name terminator
;    r12 = insertion point (1st node >= this node)
;    NE if found, EQ if not found
;
VarFindIt Entry "r0,r1,r2,r5,r6,r7,r8,r9,r10,r11"

; validate R3 by looking down the list to see if we find it.
; Crude, but effective!

 [ DebugSysVars
        SWI     XOS_WriteS
        =       "VarFindIt(",0
        BL      SysVar_Write0
 ]

Jeffrey Lee's avatar
Jeffrey Lee committed
1875
        LDR     r9, =ZeroPage+VariableList
Neil Turton's avatar
Neil Turton committed
1876 1877
        LDR     r9, [r9]
        TEQ     r9, #0
1878 1879 1880 1881 1882
  [ SysVars_QuickContext
        LDRNE   r12,[r9,#SysVars_LastContext]
        MOVEQ   r12,#-1                           ;r12 = var table index for last context, or -1 if notvalid/notthere
        ADDNE   r9,r9,#SysVars_VTableOffset
  ]
Neil Turton's avatar
Neil Turton committed
1883 1884 1885 1886 1887 1888 1889 1890 1891 1892
        LDRNE   r8, [r9]
        MOVEQ   r8, #0
        TEQ     r3, #0
        BEQ     %FT20

 [ DebugSysVars
        SWI     XOS_WriteS
        =       "-scan-",0
 ]
        ; r3 non-zero - scan list for entry
1893 1894 1895

  [ SysVars_QuickContext
        ;massive short cut - see if context is the last context we returned
Robert Sprowson's avatar
Robert Sprowson committed
1896
        CMP     r12, r8                 ;if not valid, or higher than current number of vars, forget it
1897 1898 1899 1900 1901 1902 1903 1904
        BHI     %FT04
        LDR     lr, [r9, r12, ASL #2]
        CMP     lr, r3
        BEQ     %FT70
04
        ;no such luck - scan list anyway
  ]

Neil Turton's avatar
Neil Turton committed
1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920
        ADD     r12, r8, #1
        B       %FT10
05
        LDR     lr, [r9, r12, ASL #2]
        CMP     lr, r3
        BEQ     %FT70                   ; continue scan down list
10
        SUBS    r12, r12, #1
        BHI     %BT05

20
 [ DebugSysVars
        SWI     XOS_WriteS
        =       "-wildcheck-",0
 ]
        ; not found in scan - check for name being wildcarded
1921

Neil Turton's avatar
Neil Turton committed
1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966
        MOV     r10, r0
25
        LDRB    lr, [r10], #1
        TEQ     lr, #"*"
        TEQNE   lr, #"#"
        BEQ     %FT65
        CMP     lr, #" "
        BHI     %BT25

 [ DebugSysVars
        SWI     XOS_WriteS
        =       "-bchop-",0
 ]

        ; Name not wildcarded - do binary chop search
        ORRS    r7, r8, r8, LSR #1
        ORR     r7, r7, r7, LSR #2
        ORR     r7, r7, r7, LSR #4
        ORR     r7, r7, r7, LSR #8
        ORR     r7, r7, r7, LSR #16
        BICS    r7, r7, r7, LSR #1      ; least 2^n <= number of entries
        MOV     r6, #0

        B       %FT60
40
        ADD     r5, r6, r7
        CMP     r5, r8
        BHI     %FT55

        MOV     r1, r0
        LDR     r4, [r9, r5, ASL #2]

45
        LDRB    r2, [r1], #1
        CMP     r2, #" "
        MOVLS   r2, #0
        LDRB    r3, [r4], #1
        CMP     r3, #" "
        MOVLS   r3, #0
        UpperCase R2,LR
        UpperCase R3,LR
        CMP     r3, r2
        BNE     %FT50
        CMP     r3, #0
        BNE     %BT45
1967

Neil Turton's avatar
Neil Turton committed
1968
50
1969
        MRSHS   r10, CPSR               ; preserve last HS result we got
Neil Turton's avatar
Neil Turton committed
1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982
        MOVHS   r11, r4
        MOVLO   r6, r5
55
        MOVS    r7, r7, LSR #1
60
        BNE     %BT40

        ; We always want the element above.
        ; If r6<r8 then we want the preserved result
        ; else we want the result HI
        ADD     r6, r6, #1
        CMP     r6, r8
        LDRLS   r3, [r9, r6, ASL #2]
1983 1984
  [ SysVars_QuickContext
        ASSERT  SysVars_LastContext = SysVars_VTableOffset - 4
Robert Sprowson's avatar
Robert Sprowson committed
1985
        STRLS   r6, [r9, #-4]           ;save var table index for context we're returning, in LastContext
1986
  ]
Neil Turton's avatar
Neil Turton committed
1987 1988
        MOVLS   r4, r11
        MOVHI   r3, #0
1989
        MSRLS   CPSR_f, r10
Neil Turton's avatar
Neil Turton committed
1990 1991 1992 1993 1994 1995 1996

 [ DebugSysVars
        SWI     XOS_WriteS
        =       "-complete-",0
        SWI     XOS_NewLine
 ]
        MOV     r12, r6
Kevin Bracey's avatar
Kevin Bracey committed
1997
        TOGPSR  Z_bit, lr
Neil Turton's avatar
Neil Turton committed
1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025
        EXIT

65
 [ DebugSysVars
        SWI     XOS_WriteS
        =       "-listscan-",0
        SWI     XOS_NewLine
 ]
        ; Scan down list looking for wildmatch
        MOV     r12, #0
70
        ADD     r12, r12, #1
        CMP     r12, r8
        BHI     %FT90                   ; end of list reached
        LDR     r4, [r9, r12, ASL #2]
        BL      WildMatch               ; trashes r10,r11
        BNE     %BT70

80
 [ DebugSysVars
        SWI     XOS_WriteS
        =       "-complete-",0
        SWI     XOS_NewLine
 ]
        ; Found
        ; r4->name end
        ; r12 = entry number
        LDR     r3, [r9, r12, ASL #2]
2026 2027 2028 2029
  [ SysVars_QuickContext
        ASSERT  SysVars_LastContext = SysVars_VTableOffset - 4
        STR     r12, [r9, #-4]          ;save var table index for context we're returning, in LastContext
  ]
Neil Turton's avatar
Neil Turton committed
2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052
        MOVS    r12, r12                ; set NE
        EXIT

90
        ; Not found
 [ DebugSysVars
        SWI     XOS_WriteS
        =       "-not found-",0
        SWI     XOS_NewLine
 ]
        MOV     r12, #1
        MOVS    r3, #0
        EXIT

WildMatch ROUT
; In  : R0 is wildcard spec ptr, R4 is name ptr.
;       Wild Terminators are any ch <=" ", name terminator 0
;       Wildcards are *, #
; Out : EQ/NE for match (EQ if matches)
;       R4 points after name terminator for found
;       R0 preserved, R10, 11 corrupted

        Push    "R0-R3"
Robert Sprowson's avatar
Robert Sprowson committed
2053 2054 2055
        MOV     R11, #0                 ; this is the wild backtrack pointer
        MOV     R3, #0                  ; and this is the name backtrack ptr.
01      LDRB    R1, [R0], #1            ; nextwild
Neil Turton's avatar
Neil Turton committed
2056
        CMP     R1, #"*"
Robert Sprowson's avatar
Robert Sprowson committed
2057 2058
        BEQ     %FT02                   ; IF nextwild = "*"
        LDRB    R2, [R4], #1            ; nextname
Neil Turton's avatar
Neil Turton committed
2059 2060 2061 2062
        CMP     R2, #0
        BEQ     %FT03
        UpperCase R1, R10
        UpperCase R2, R10
Robert Sprowson's avatar
Robert Sprowson committed
2063 2064 2065 2066 2067
        CMP     R1, R2                  ; IF nextwild=nextname
        CMPNE   R1, #"#"                ;   OR nextwild = #  (terminator checked already)
        BEQ     %BT01                   ; THEN LOOP (stepped already)
        MOV     R0, R11                 ; try backtrack
        MOVS    R4, R3                  ; if * had at all
Neil Turton's avatar
Neil Turton committed
2068
        BNE     %FT02
Robert Sprowson's avatar
Robert Sprowson committed
2069 2070
        CMP     PC, #0                  ; set NE
04      Pull    "R0-R3"                 ; return NE (failed)
Neil Turton's avatar
Neil Turton committed
2071 2072
        MOV     PC, lr

Robert Sprowson's avatar
Robert Sprowson committed
2073 2074 2075
03      CMP     R1, #" "                ; name terminated : has wildcard?
        BHI     %BA04                   ; note HI has NE set.
        CMP     R1, R1                  ; set EQ
Neil Turton's avatar
Neil Turton committed
2076 2077 2078
        Pull    "R0-R3"
        MOV     PC, lr

Robert Sprowson's avatar
Robert Sprowson committed
2079 2080
02      MOV     R11, R0                 ; wild backtrack ptr is char after *
        LDRB    R1, [R0], #1            ; step wild
Neil Turton's avatar
Neil Turton committed
2081
        CMP     R1, #"*"
Robert Sprowson's avatar
Robert Sprowson committed
2082
        BEQ     %BT02                   ; fujj **
Neil Turton's avatar
Neil Turton committed
2083
        UpperCase R1, R10
Robert Sprowson's avatar
Robert Sprowson committed
2084 2085
05      LDRB    R2, [R4], #1            ; step name
        CMP     R2, #0                  ; terminator?
Neil Turton's avatar
Neil Turton committed
2086 2087 2088
        BEQ     %BT03
        UpperCase R2, R10
        CMP     R1, R2
Robert Sprowson's avatar
Robert Sprowson committed
2089
        CMPNE   R1, #"#"                ; match if #
Neil Turton's avatar
Neil Turton committed
2090
        BNE     %BT05
Robert Sprowson's avatar
Robert Sprowson committed
2091 2092
        MOV     R3, R4                  ; name backtrack ptr is char after match
        B       %BT01                   ; LOOP
Neil Turton's avatar
Neil Turton committed
2093

2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113

  [ Oscli_QuickAliases

;routines to speed up alias checking for OS_CLI

;VarFindIt_QA - similar to VarFindIt
;
; In
;    r3 -> non-wildcarded, already upper-cased name of var to find
;          will be of form ALIAS$<whatever>
;
; Out
;    r5 = name pointer (equivalent to r3 for VarFindIt
;    r6 = pointer after name terminator (equivalent to r4 for VarFindIt)
;    r7 = insertion point (equivalent to r12 for VarFindIt)
;    NE if found, EQ if not found
;
VarFindIt_QA ROUT
        Push    "r0,r1,r2,r3,r4,r8,r9,r10,r11,LR"
        MOV     r0, r3
Jeffrey Lee's avatar
Jeffrey Lee committed
2114
        LDR     r9, =ZeroPage+VariableList
2115 2116
        LDR     r9, [r9]
        TEQ     r9,#0
Robert Sprowson's avatar
Robert Sprowson committed
2117
        BEQ     %FT99                   ;exit with EQ (not found)
2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154
  [ SysVars_QuickContext
        ADD     r9,r9,#SysVars_VTableOffset
  ]
        LDR     r8, [r9]

        ; do binary chop search
        ORRS    r7, r8, r8, LSR #1
        ORR     r7, r7, r7, LSR #2
        ORR     r7, r7, r7, LSR #4
        ORR     r7, r7, r7, LSR #8
        ORR     r7, r7, r7, LSR #16
        BICS    r7, r7, r7, LSR #1      ; least 2^n <= number of entries
        MOV     r6, #0

        B       %FT60
40
        ADD     r5, r6, r7
        CMP     r5, r8
        BHI     %FT55

        MOV     r1, r0
        LDR     r4, [r9, r5, ASL #2]

45
        LDRB    r2, [r1], #1
        CMP     r2, #" "
        MOVLS   r2, #0
        LDRB    r3, [r4], #1
        CMP     r3, #" "
        MOVLS   r3, #0
        UpperCase R3,LR
        CMP     r3, r2
        BNE     %FT50
        CMP     r3, #0
        BNE     %BT45

50
2155
        MRSHS   r10, CPSR               ; preserve last HS result we got
2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168
        MOVHS   r11, r4
        MOVLO   r6, r5
55
        MOVS    r7, r7, LSR #1
60
        BNE     %BT40

        ; We always want the element above.
        ; If r6<r8 then we want the preserved result
        ; else we want the result HI
        ADD     r6, r6, #1
        CMP     r6, r8
        LDRLS   r5, [r9, r6, ASL #2]
Robert Sprowson's avatar
Robert Sprowson committed
2169 2170 2171

        ; don't want to save context in this version of routine

2172 2173 2174
        MOV     r7, r6
        MOVLS   r6, r11
        MOVHI   r5, #0
2175
        MSRLS   CPSR_f, r10
2176

Kevin Bracey's avatar
Kevin Bracey committed
2177
        TOGPSR  Z_bit, lr
2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201
99
        Pull    "r0,r1,r2,r3,r4,r8,r9,r10,r11,PC"


  ] ;Oscli_QuickAliases

  [ SysVars_StickyNodes
;
; SysVars_ClaimVNode
;
; entry: R3 = size required
; exit:  R2 = address of allocated node, or V set for error
;
; if R3  > max sticky size, just delegates to ClaimSysHeapNode
; if R3 <= max sticky size, rounds up to next unit size, and attempts to pick
;    up sticky node of that size - if not found, gets one from heap (now of size that can
;    stick on free)
;
SysVars_ClaimVNode ROUT
        Push    "r0,r1,r3,LR"
        CMP     r3,#SysVars_StickyNode_MaxSize
        BHI     %FT80                                         ;too big for sticky node
        ADD     r3,r3,#SysVars_StickyNode_UnitSize-1
        BIC     r3,r3,#SysVars_StickyNode_UnitSize-1          ;round up to unit size
Jeffrey Lee's avatar
Jeffrey Lee committed
2202
        LDR     r1,=ZeroPage+SysVars_StickyPointers
2203 2204 2205 2206 2207 2208 2209
        LDR     r2,[r1,r3,LSR #(SysVars_StickyNode_Log2US-2)] ;sticky pointer for this size
        CMP     r2,#0                                         ;also clears V
        BEQ     %FT80
        MOV     LR,#0
        STR     LR,[r1,r3,LSR #(SysVars_StickyNode_Log2US-2)] ;used it
  [ mjsSysHeapNodesTrace
        Push    "r0-r2"
Jeffrey Lee's avatar
Jeffrey Lee committed
2210
        LDR     r0,=ZeroPage
2211 2212 2213 2214 2215 2216 2217 2218 2219 2220
        LDR     r1,[r0,#mjsSHNT_vcs_total]
        ADD     r1,r1,#1
        STR     r1,[r0,#mjsSHNT_vcs_total]
        Pull    "r0-r2"
  ]
        Pull    "r0,r1,r3,PC"
80      BL      ClaimSysHeapNode
        STRVS   r0,[SP]
  [ mjsSysHeapNodesTrace
        Push    "r0-r2"
Jeffrey Lee's avatar
Jeffrey Lee committed
2221
        LDR     r0,=ZeroPage
2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247
        LDR     r1,[r0,#mjsSHNT_vch_total]
        ADD     r1,r1,#1
        STR     r1,[r0,#mjsSHNT_vch_total]
        Pull    "r0-r2"
  ]
        Pull    "r0,r1,r3,PC"

;
; SysVars_FreeVNode
;
; entry: R2 = address of node to free (must be valid)
; exit:  -
;        OR V set, error
;
; - checks size of node (uses internal knowledge of Heap blocks - naughty!)
; - if size is a sticky size, and the corresponding sticky pointer is 0, retains it,
;   else delegates to FreeSysHeapNode
;
SysVars_FreeVNode
        Push    "r0,r1,r3,LR"
        LDR     r1,[r2,#-4]                                   ;pick up OS_Heap's size word
        SUB     r1,r1,#8                                      ;sticky sizes will be 8 less than heap sizes
        CMP     r1,#SysVars_StickyNode_MaxSize                ;is it too big?
        BHI     %FT80
        TST     r1,#SysVars_StickyNode_UnitSize-1             ;is it a multiple of unit size
        BNE     %FT80
Jeffrey Lee's avatar
Jeffrey Lee committed
2248
        LDR     r3,=ZeroPage+SysVars_StickyPointers
2249 2250 2251 2252 2253 2254
        LDR     LR,[r3,r1,LSR #(SysVars_StickyNode_Log2US-2)] ;sticky pointer for this size
        CMP     LR,#0
        STREQ   r2,[r3,r1,LSR #(SysVars_StickyNode_Log2US-2)] ;stick!
80
  [ mjsSysHeapNodesTrace
        Push    "r0-r2"
Jeffrey Lee's avatar
Jeffrey Lee committed
2255
        LDR     r0,=ZeroPage
2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294
        LDREQ   r1,[r0,#mjsSHNT_vfs_total]
        LDRNE   r1,[r0,#mjsSHNT_vfh_total]
        ADD     r1,r1,#1
        STREQ   r1,[r0,#mjsSHNT_vfs_total]
        STRNE   r1,[r0,#mjsSHNT_vfh_total]
        Pull    "r0-r2"
  ]
        BLNE    FreeSysHeapNode
        STRVS   r0,[SP]
        Pull    "r0,r1,r3,PC"

;
; SysVars_ExpandOrShrinkVNode
;
; entry: R2 = address of block
;        R3 = change of size required in bytes (signed integer)
; exit:  R2 = address of block, which may have changed (block moved/copied)
;        OR V set, error returned
;
; - checks size of node (uses internal knowledge of Heap blocks - naughty!)
; - assumes all VNodes currently less than MaxSize and of sticky size *must* be stickily
;   allocated nodes (so that maximum current size that could be presumed by client is heap
;   size minus 8, rather than minus 4)
; - if new size is small enough to be a sticky node, attempts to return a sticky node,
;   without doing an OS_Heap grow or shrink, else delegates to DoSysHeapOpWithExtension
;
SysVars_ExpandOrShrinkVNode
        Push    "r0,r1,r3-r6,LR"
        LDR     r5,[r2,#-4]                                   ;pick up OS_Heap's size word
        SUB     r5,r5,#4                                      ;usable sizes are 4 less than heap sizes
        ADD     r4,r5,r3                                      ;putative new size
        CMP     r4,#SysVars_StickyNode_MaxSize                ;is it too big?
        BHI     %FT90
        SUB     r6,r5,#4                                      ;sticky sizes will be 8 less than heap sizes
        SUB     r4,r4,#4
        ADD     r4,r4,#SysVars_StickyNode_UnitSize-1
        BIC     r4,r4,#SysVars_StickyNode_UnitSize-1          ;round up to unit size
        CMP     r4,r6                                         ;same as current size?
        BEQ     %FT55
Jeffrey Lee's avatar
Jeffrey Lee committed
2295
        LDR     r1,=ZeroPage+SysVars_StickyPointers
2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326
        LDR     LR,[r1,r4,LSR #(SysVars_StickyNode_Log2US-2)] ;sticky pointer for this size
        CMP     LR,#0
        BEQ     %FT40
        MOV     r3,#0
        STR     r3,[r1,r4,LSR #(SysVars_StickyNode_Log2US-2)] ;used it
        MOV     r5,r2
        MOV     r2,LR
        B       %FT50
40
        MOV     r5,r2
        MOV     r3,r4
        BL      SysVars_ClaimVNode
        BVS     %FT95
50      ;copy min(r6,r4) bytes (multiple of 8) from old node at r5 to new node at r2
        CMP     r4,r6
        MOVLO   r6,r4
        MOV     r3,r2
        MOV     LR,r5
52
        LDMIA   LR!,{r0,r1}
        STMIA   r3!,{r0,r1}
        SUBS    r6,r6,#8
        BGT     %BT52
        MOV     r6,r2
        MOV     r2,r5
        BL      SysVars_FreeVNode
        MOV     r2,r6
        BVS     %FT95
55
  [ mjsSysHeapNodesTrace
        Push    "r0-r2"
Jeffrey Lee's avatar
Jeffrey Lee committed
2327
        LDR     r0,=ZeroPage
2328 2329 2330 2331 2332 2333 2334 2335 2336 2337
        LDR     r1,[r0,#mjsSHNT_vxs_total]
        ADD     r1,r1,#1
        STR     r1,[r0,#mjsSHNT_vxs_total]
        Pull    "r0-r2"
  ]
        CLRV
        Pull    "r0,r1,r3-r6,PC"
90
  [ mjsSysHeapNodesTrace
        Push    "r0-r2"
Jeffrey Lee's avatar
Jeffrey Lee committed
2338
        LDR     r0,=ZeroPage
2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351
        LDR     r1,[r0,#mjsSHNT_vxh_total]
        ADD     r1,r1,#1
        STR     r1,[r0,#mjsSHNT_vxh_total]
        Pull    "r0-r2"
  ]
        MOV     r0,#HeapReason_ExtendBlock
        BL      DoSysHeapOpWithExtension
95
        STRVS   r0,[SP]
        Pull    "r0,r1,r3-r6,PC"

  ] ;SysVars_StickyNodes

Neil Turton's avatar
Neil Turton committed
2352 2353 2354
        LTORG

        END