Arthur2 66.8 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

        GBLL    DebugSysVars
DebugSysVars SETL {FALSE}


;-----------------------------------------------------------------------------------
;
; This file covers:
;   System variables:
;     InitVariables
;     OS_ReadVarVal
;     OS_SetVarVal
;   GSTrans:
;     OS_GSInit
;     OS_GSRead
;     OS_GSTrans
; These have been grouped because GSTrans makes direct use of the system variables'
Robert Sprowson's avatar
Robert Sprowson committed
75
; structures.
Neil Turton's avatar
Neil Turton committed
76 77 78 79
;
; The system variables are stored as a one way sorted alphabetically linked list hanging
; off the zero-page location VariableList:
;
80
; VariableList---->sorted table of pointers to variable blocks (QuickIndex) - format as above
Neil Turton's avatar
Neil Turton committed
81 82 83
;
; The end is indicated by the link having the value 0.
;
84 85
; 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
86
;
87 88
; No. Bytes     Use
; N+1           Variable's name (length N, plus terminator).
Neil Turton's avatar
Neil Turton committed
89 90 91 92 93 94 95 96 97 98 99 100
; 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
101
; 3             Length (N)
Neil Turton's avatar
Neil Turton committed
102 103 104 105 106 107 108 109
; 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
110
; 3             Length (N)
Neil Turton's avatar
Neil Turton committed
111 112 113 114 115 116 117 118 119 120 121
; 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
122
        Push    "lr"
Neil Turton's avatar
Neil Turton committed
123 124

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

        ; Set up the preset system variables
Robert Sprowson's avatar
Robert Sprowson committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
        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
145 146 147 148 149 150 151 152 153 154 155 156

        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
157
        =       "Sys$$Time", 0, VarType_Code
Neil Turton's avatar
Neil Turton committed
158
        ALIGN
Robert Sprowson's avatar
Robert Sprowson committed
159 160 161
        &       sv2-.-4
        LDR     PC, %FT01
        LDR     PC, %FT02
Neil Turton's avatar
Neil Turton committed
162
01
Robert Sprowson's avatar
Robert Sprowson committed
163
        &       SetTimeVar
Neil Turton's avatar
Neil Turton committed
164
02
Robert Sprowson's avatar
Robert Sprowson committed
165
        &       ReadTimeVar
Neil Turton's avatar
Neil Turton committed
166

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

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

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

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

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

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

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

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

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

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

; Now the code for our system variables.

ReadTimeVar
Robert Sprowson's avatar
Robert Sprowson committed
236 237
        ADR     R0, SysTimeFormat
        B       ReadTimeFormatted
Neil Turton's avatar
Neil Turton committed
238
SetTimeVar ROUT
Robert Sprowson's avatar
Robert Sprowson committed
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
        CMP     R2, #&FE
        BHI     TimeVarTooLong
        Push    "R0, lr"
        LDR     R12, =SysVarWorkSpace
        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
257

Kevin Bracey's avatar
Kevin Bracey committed
258
TimeVarTooLong
Robert Sprowson's avatar
Robert Sprowson committed
259
        ADRL    R0, ErrorBlock_VarTooLong
260
      [ International
Robert Sprowson's avatar
Robert Sprowson committed
261
        B       TranslateError
262
      |
Kevin Bracey's avatar
Kevin Bracey committed
263
        RETURNVS
264
      ]
Kevin Bracey's avatar
Kevin Bracey committed
265

Neil Turton's avatar
Neil Turton committed
266
ReadYear
Robert Sprowson's avatar
Robert Sprowson committed
267 268
        ADR     R0, SysYearFormat
        B       ReadTimeFormatted
Neil Turton's avatar
Neil Turton committed
269
SetYear ROUT
Robert Sprowson's avatar
Robert Sprowson committed
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
        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
292

Kevin Bracey's avatar
Kevin Bracey committed
293
ReadDate
Robert Sprowson's avatar
Robert Sprowson committed
294 295
        ADR     R0, SysDateFormat
        B       ReadTimeFormatted
Kevin Bracey's avatar
Kevin Bracey committed
296
SetDate ROUT
Robert Sprowson's avatar
Robert Sprowson committed
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
        CMP     R2, #&F8
        BHI     TimeVarTooLong
        Push    "R0, lr"
        LDR     R12, =SysVarWorkSpace
        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
320 321 322 323 324 325 326


; 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
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
        Push    "R3,R4,LR"
        LDR     R12, =SysVarWorkSpace
        ADD     R2, R12, #1
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
        MOV     R3, #?SysVarWorkSpace-1
        SWI     XTerritory_ConvertDateAndTime
02      ADD     R13, R13, #8
        SUBVC   R2, R1, R0
        Pull    "R3,R4,PC"
Kevin Bracey's avatar
Kevin Bracey committed
345 346 347 348 349 350 351

; 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
352 353 354
        Push    "R3,R4,LR"
        MOV     R2, R1
        B       %BT01
Neil Turton's avatar
Neil Turton committed
355 356

ReadRC  ROUT
Robert Sprowson's avatar
Robert Sprowson committed
357 358 359 360 361 362 363 364 365 366 367
        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
368
        CLRV
Robert Sprowson's avatar
Robert Sprowson committed
369 370 371
        Pull    "PC"
03      ADRGT   R0, ErrorBlock_RCExc
        ADRLT   R0, ErrorBlock_RCNegative
Neil Turton's avatar
Neil Turton committed
372
      [ International
Robert Sprowson's avatar
Robert Sprowson committed
373
        BL      TranslateError
Neil Turton's avatar
Neil Turton committed
374 375 376
      |
        SETV
      ]
Kevin Bracey's avatar
Kevin Bracey committed
377

Robert Sprowson's avatar
Robert Sprowson committed
378
        Pull    "PC"
Kevin Bracey's avatar
Kevin Bracey committed
379
SetYearPrefixFormat
Robert Sprowson's avatar
Robert Sprowson committed
380
        =       "%w3,%dy %m3 ", 0
Kevin Bracey's avatar
Kevin Bracey committed
381
SetDateSuffixFormat
Robert Sprowson's avatar
Robert Sprowson committed
382
        =       " %ce%yr", 0
Kevin Bracey's avatar
Kevin Bracey committed
383 384
        ALIGN

Neil Turton's avatar
Neil Turton committed
385 386 387
        MakeErrorBlock RCExc
        MakeErrorBlock RCNegative

Robert Sprowson's avatar
Robert Sprowson committed
388 389
ReadRCL LDR     R0, =ZeroPage
        LDR     R0, [R0, #RCLimit]
Neil Turton's avatar
Neil Turton committed
390
ReadNumSysVar
Robert Sprowson's avatar
Robert Sprowson committed
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
        Push    "lr"
        LDR     R12, =SysVarWorkSpace
        MOV     R1, R12
        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
406 407 408

        LTORG

Robert Sprowson's avatar
Robert Sprowson committed
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
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
        LDR     R12, =SysVarWorkSpace
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
        LDR     R1, =SysVarWorkSpace
        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
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467


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

Timothy E Baldwin's avatar
Timothy E Baldwin committed
468 469 470
; The flags GS_ReadingString and GS_Macroing returned by GSREAD are used by
; ReadExpression in Arthur3.

Neil Turton's avatar
Neil Turton committed
471 472 473 474 475 476 477 478 479 480 481 482
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
483
        WritePSRc SVC_mode, R1
Neil Turton's avatar
Neil Turton committed
484 485 486

 [ GS_BufferNotStack
        AND     R2, R2, #GS_NoQuoteMess :OR: GS_NoVBarStuff :OR: GS_Spc_term
Robert Sprowson's avatar
Robert Sprowson committed
487
                                        ; get caller's flags
Neil Turton's avatar
Neil Turton committed
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
 ]

; 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
516
        STRB    R1, [R12, #GS_StackPtr] ; no stacked R0s
Neil Turton's avatar
Neil Turton committed
517 518 519 520 521 522
 ]

20
 [ GS_BufferNotStack
 |
        AND     R2, R2, #GS_NoQuoteMess :OR: GS_NoVBarStuff :OR: GS_Spc_term
Robert Sprowson's avatar
Robert Sprowson committed
523
                                        ; get caller's flags
Neil Turton's avatar
Neil Turton committed
524 525 526 527 528 529 530 531
 ]
        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
532 533
        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
534 535 536
        CMP     R1, #13
        CMPNE   R1, #10
        CMPNE   R1, #0
Robert Sprowson's avatar
Robert Sprowson committed
537
        ORREQ   lr, lr, #Z_bit          ; and move EQ/NE to return pc
Neil Turton's avatar
Neil Turton committed
538 539 540
        BICNE   lr, lr, #Z_bit
        ExitSWIHandler

Ben Avison's avatar
Ben Avison committed
541 542
        LTORG

Neil Turton's avatar
Neil Turton committed
543 544 545 546 547 548 549 550 551 552 553
; -----------------------------------------------------------------------------


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
554
        WritePSRc SVC_mode, R10
Neil Turton's avatar
Neil Turton committed
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585

        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
586 587
        ; OS_SetVarValue requires system variable names to be space or control
        ; terminated, so we shall discard anything which violates that rule
Neil Turton's avatar
Neil Turton committed
588 589 590 591 592 593 594
        GSVarGetWSpace
        ADD     R12, R12, #GSNameBuff
        MOV     R11, #0
20      LDRB    R1, [R0], #1
        STRB    R1, [R12], #1
        ADD     R11, R11, #1
        CMP     R1, #">"
595 596 597 598
        BEQ     %FT25
        CMP     R11, #?GSNameBuff
        CMPNE   R1, #" "
        TSTNE   R1, #&E0                ; i.e. EQ if <32
Neil Turton's avatar
Neil Turton committed
599
        BNE     %BT20
600
        B       GSREAD_AngleBraDaftSoIsnt
Neil Turton's avatar
Neil Turton committed
601

602
25
Neil Turton's avatar
Neil Turton committed
603 604 605 606 607
        ; 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
608
        MOV     R0, #10
Neil Turton's avatar
Neil Turton committed
609 610 611 612 613 614 615 616 617 618 619 620 621 622
        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
623 624
        MOV     R3, #0                  ; context ptr
        SUB     R0, R12, R11            ; name ptr
Neil Turton's avatar
Neil Turton committed
625
        BL      VarFindIt
Robert Sprowson's avatar
Robert Sprowson committed
626
        Pull    "R0, R3, R4, R10", EQ   ; not found mate
Neil Turton's avatar
Neil Turton committed
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
        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
651
        LDRB    R1, [R0], #1            ; type
Neil Turton's avatar
Neil Turton committed
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
        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
676
        ADD     stack, stack, #4        ; discard that R0
Neil Turton's avatar
Neil Turton committed
677 678 679
        Pull    "R2, lr"
        CMP     R1, #0
        BEQ     ZeroLengthVar
Robert Sprowson's avatar
Robert Sprowson committed
680
        ORR     R2, R2, R1              ; old flags+new count
Neil Turton's avatar
Neil Turton committed
681 682 683 684 685 686 687 688 689 690
        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
691
        B       GSREAD_XPandGetNextByte ; loop, transforming chars.
Neil Turton's avatar
Neil Turton committed
692 693 694 695

GSREAD_CantNestMore
        Pull    "R3, R4, R10"           ; no room to stack pointer, so don't expand
GSREAD_AngledThingNotThere
Robert Sprowson's avatar
Robert Sprowson committed
696
        ADD     stack, stack, #4        ; skip R0 - return null string
Neil Turton's avatar
Neil Turton committed
697
        Pull    "R2, lr"
Robert Sprowson's avatar
Robert Sprowson committed
698
        B       GSREAD_XPandGetNextByte ; get next char
Neil Turton's avatar
Neil Turton committed
699 700 701 702 703 704

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

705
GSREAD_GotToEndCheckInString
Robert Sprowson's avatar
Robert Sprowson committed
706 707
        TST     R2, #GS_In_String       ; got CR or LF
        BNE     GSREAD_BadStringError   ; bad string
Neil Turton's avatar
Neil Turton committed
708
GSREAD_GotToAnEnd
709 710
        ORR     lr, lr, #C_bit          ; got terminator
        ExitSWIHandler
Neil Turton's avatar
Neil Turton committed
711

712 713 714
GSREAD_XPandGotToEnd
        TST     R2, #GS_Macroing
        BEQ     GSREAD_GotToEndCheckInString
Neil Turton's avatar
Neil Turton committed
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
        ; 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
731
        B       GSREAD_XPandGetNextByte ; return to prevstring
Neil Turton's avatar
Neil Turton committed
732 733 734 735

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
736
        BEQ     GSREAD_GotToAnEnd       ; terminates
Neil Turton's avatar
Neil Turton committed
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764

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
765
        BEQ     GSREAD_ReturnWithChar   ; || gives |, |" gives ", |< gives <
Neil Turton's avatar
Neil Turton committed
766
        CMP     R1, #"?"
Robert Sprowson's avatar
Robert Sprowson committed
767 768
        MOVEQ   R1, #&7F                ; delete
        BEQ     GSREAD_ReturnWithChar   ; valid ch
Neil Turton's avatar
Neil Turton committed
769 770 771 772 773
        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
774 775 776 777
        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
778 779
        MOVEQ   R1, #"_"
        CMP     R1, #"@"
Robert Sprowson's avatar
Robert Sprowson committed
780
        ANDGE   R1, R1, #&1F            ; transform if @<=ch<delete
Neil Turton's avatar
Neil Turton committed
781 782 783
        B       GSREAD_ReturnWithChar

GSREAD_RStringGetNextByte
Robert Sprowson's avatar
Robert Sprowson committed
784
        SUB     R2, R2, #1              ; we're reading a string
Neil Turton's avatar
Neil Turton committed
785 786 787 788 789
  [ GS_BufferNotStack
        MOVS    R12, R2, ASL #32-GS_StackLimitPos
  |
        ANDS    r12, r2, #&00ffffff
  ]
Robert Sprowson's avatar
Robert Sprowson committed
790 791
        LDRNEB  R1, [R0], #1            ; and this is already expanded
        ExitSWIHandler NE               ; so finished
Neil Turton's avatar
Neil Turton committed
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
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
819 820 821
        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
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
        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
839
        BIC     lr, lr, #C_bit
Neil Turton's avatar
Neil Turton committed
840
        Push    "R1, R3-R5, lr"
Robert Sprowson's avatar
Robert Sprowson committed
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
        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
856 857 858
        Pull    "R1, R3-R5, lr"
        ExitSWIHandler

Robert Sprowson's avatar
Robert Sprowson committed
859
02      SUB     R2, R3, R4
Neil Turton's avatar
Neil Turton committed
860 861 862
        Pull    "R1, R3-R5, lr"
        B       SLVK_SetV               ; bad string: error set by GSRead

Robert Sprowson's avatar
Robert Sprowson committed
863
03      SUB     R2, R3, R4
Neil Turton's avatar
Neil Turton committed
864
        Pull    "R1, R3-R5, lr"
Robert Sprowson's avatar
Robert Sprowson committed
865
        ORR     lr, lr, #C_bit          ; buffer overflow
Neil Turton's avatar
Neil Turton committed
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
        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)

906 907 908 909
 [ 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
910
ReadVarValue ROUT
Robert Sprowson's avatar
Robert Sprowson committed
911
        WritePSRc SVC_mode, r11         ; enable interupts (mode remains unchanged)
Neil Turton's avatar
Neil Turton committed
912 913 914 915
        Entry   "r0,r1"

        MOV     r11, r4

916 917 918 919 920 921 922 923 924 925
  [ 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
926
        BL      VarFindIt               ; name=r0,context=r3 -> name found in node=r3,r4=after namein,r12=prev
Neil Turton's avatar
Neil Turton committed
927 928
        BEQ     RVVNotFound

929 930 931
  [ Oscli_QuickAliases
rvv_qaspecialentry
  ]
Neil Turton's avatar
Neil Turton committed
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
        ; 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
976
        MOV     R2, R10                 ; bytes he's gonna get
Neil Turton's avatar
Neil Turton committed
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
; 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
1035 1036 1037
        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
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052

        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
1053
        LDRB    R0, [R11], #1           ; number - convert to string.
Neil Turton's avatar
Neil Turton committed
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 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
        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


; *****************************************************************************
; 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
1140
        WritePSRc SVC_mode, r10
Neil Turton's avatar
Neil Turton committed
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

        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
1168

1169 1170 1171 1172 1173 1174
        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
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215

        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
1216
        MOV     r0, r3                  ; If already there use that's name in case supplied name wildcarded
Neil Turton's avatar
Neil Turton committed
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
        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
1229
        SUB     stack, stack, #256      ; buffer
1230 1231
        MOV     r2, #256

Neil Turton's avatar
Neil Turton committed
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
        ; 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

1247 1248 1249 1250 1251 1252
        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
1253

Neil Turton's avatar
Neil Turton committed
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
        ; 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...
1287
        MOV     r3, #0                  ;accumulator for length of things we know
Neil Turton's avatar
Neil Turton committed
1288 1289 1290 1291 1292 1293 1294
        MOV     r1, r0
10
        LDRB    lr, [r1], #1
        ADD     r3, r3, #1
        CMP     lr, #" "
        BHI     %BT10

1295 1296 1297
;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
1298 1299
        ; Deal with number and string type
        CMP     r10, #VarType_Number
1300
        ADDLO   r3, r3, #64             ; only an initial guess for the string type
Neil Turton's avatar
Neil Turton committed
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
        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

1339 1340 1341 1342 1343 1344
        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
1345
        BL      ClaimSysHeapNode
1346 1347
  ]
        Pull    "r0-r2",VS
Neil Turton's avatar
Neil Turton committed
1348 1349 1350 1351 1352
        BVS     SetVarVal_VarNoRoom

        ; Got a heap block - fill it in

        ; Copy name
1353 1354 1355

        MOV     r4, r2
        LDR     r0,[sp]
Neil Turton's avatar
Neil Turton committed
1356 1357 1358 1359 1360 1361 1362
25
        LDRB    lr, [r0], #1
        CMP     lr, #" "
        MOVLS   lr, #0
        STRB    lr, [r4], #1
        BHI     %BT25

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

Neil Turton's avatar
Neil Turton committed
1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
        ; 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
1378
        Push    "r4",EQ ; Remember start of code block for code variables
Neil Turton's avatar
Neil Turton committed
1379 1380 1381 1382 1383
        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
1384
        SUBEQ   r1, r1, #1              ; ghastly fudge to avoid display of terminator
Neil Turton's avatar
Neil Turton committed
1385 1386 1387 1388 1389 1390
        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
1391
        ADDEQ   r1, r1, #1              ; undo ghastly fudge
Neil Turton's avatar
Neil Turton committed
1392 1393 1394 1395 1396 1397

        B       %FT35
30
        LDRB    lr, [r5], #1
        STRB    lr, [r4], #1
35
1398
SetVarVal_CopyR1BytesToR4
Neil Turton's avatar
Neil Turton committed
1399 1400 1401
        SUBS    r1, r1, #1
        BHS     %BT30

1402 1403 1404 1405 1406 1407
        TEQ     r10, #VarType_Code
        BNE     SetVarVal_NewNodeReady

        Pull    "r1" ; Grab pointer to start of code block
        Push    "r0,r2"
        MOV     r0,#1
1408
        MOV     r2,r4
1409 1410
        SWI     XOS_SynchroniseCodeAreas
        Pull    "r0,r2"
Neil Turton's avatar
Neil Turton committed
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
        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
1441
        MOV     r3, #64                 ;should be a multiple of StickyNode_UnitSize if SysVars_StickyNodes TRUE
1442 1443 1444
  [ SysVars_StickyNodes
        BL      SysVars_ExpandOrShrinkVNode
  |
Neil Turton's avatar
Neil Turton committed
1445
        BL      DoSysHeapOpWithExtension
1446
  ]
Neil Turton's avatar
Neil Turton committed
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
        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
1457 1458 1459
  [ SysVars_StickyNodes
        SUB     r3, r3, #8              ; the amount we're allowed to use
  |
Neil Turton's avatar
Neil Turton committed
1460
        SUB     r3, r3, #4              ; the amount we're allowed to use
1461
  ]
Neil Turton's avatar
Neil Turton committed
1462 1463 1464 1465 1466 1467 1468
        ADD     r3, r3, r5              ; the block's end
        B       %BT40

SetVarVal_StringFinishedExpanding

        ; Shorten block to required size
        MOV     r0, #HeapReason_ExtendBlock
1469 1470 1471 1472 1473 1474 1475 1476 1477
  [ 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
1478 1479 1480
        SUB     r3, r4, r3
        MOV     r2, r5
        BL      DoSysHeapOpWithExtension
1481
  ]
Neil Turton's avatar
Neil Turton committed
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525
        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
1526
        LDR     r11, =ZeroPage+VariableList
Neil Turton's avatar
Neil Turton committed
1527
        LDR     r10, [r11]
1528 1529 1530 1531
  [ SysVars_QuickContext
        TEQ     r10,#0
        ADDNE   r10,r10,#SysVars_VTableOffset
  ]
Neil Turton's avatar
Neil Turton committed
1532 1533 1534 1535 1536 1537 1538 1539 1540
        MOV     r5, r2
        TEQ     r6, #0
        BEQ     SetVarVal_Insertion

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

1541 1542 1543 1544
        MOV     r2, r6
  [ SysVars_StickyNodes
        BL      SysVars_FreeVNode
  |
Neil Turton's avatar
Neil Turton committed
1545
        BL      FreeSysHeapNode
1546
  ]
Neil Turton's avatar
Neil Turton committed
1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
        B       SetVarVal_Replace

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

 [ DebugSysVars
        SWI     XOS_WriteS
        =       "-create index-",0
 ]
1561 1562 1563 1564 1565 1566

  [ 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
1567
        MOV     r3, #(10*4)+4           ; 10 nodes and 1 word for the count
1568
  ]
Robert Sprowson's avatar
Robert Sprowson committed
1569
        BL      ClaimSysHeapNode        ; this is not a variable node (its the index)
Neil Turton's avatar
Neil Turton committed
1570
        BVS     SetVarVal_NoRoomForIndex
1571 1572 1573 1574
  [ SysVars_QuickContext
        MOV     r10,#-1
        STR     r10, [r2, #SysVars_LastContext]    ; initialise last context as invalid
  ]
Neil Turton's avatar
Neil Turton committed
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
        MOV     r10, r2
        MOV     r4, #0
        B       SetVarVal_DoInsertNewBlock

SetVarVal_PossibleExtend
 [ DebugSysVars
        SWI     XOS_WriteS
        =       "-extend index-",0
 ]
        LDR     r4, [r10]
1585 1586 1587 1588
  [ 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
1589 1590
        LDR     lr, [r10, #-4]          ; Block length, from heap block
        SUB     lr, lr, #4+4            ; 4 for heap adjustment and 4 for entry count word
1591
  ]
Neil Turton's avatar
Neil Turton committed
1592 1593 1594 1595 1596 1597 1598 1599 1600
        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
1601 1602 1603 1604
  [ 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
1605
        MOV     r2, r10
Robert Sprowson's avatar
Robert Sprowson committed
1606
        MOV     r3, #40                 ; room for 10 more nodes
1607
  ]
Robert Sprowson's avatar
Robert Sprowson committed
1608
        BL      DoSysHeapOpWithExtension        ;not a variable node (expanding index)
Neil Turton's avatar
Neil Turton committed
1609 1610 1611 1612 1613 1614
        BVS     SetVarVal_NoRoomForIndex

        MOV     r10, r2

SetVarVal_DoInsertNewBlock
        STR     r10, [r11]
1615 1616 1617
  [ SysVars_QuickContext
        ADD     r10,r10,#SysVars_VTableOffset
  ]
Neil Turton's avatar
Neil Turton committed
1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655
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
1656
        BEQ     SVVNotFound
Neil Turton's avatar
Neil Turton committed
1657 1658 1659 1660 1661 1662 1663 1664
        ; 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
1665
        LDR     r11, =ZeroPage+VariableList
Neil Turton's avatar
Neil Turton committed
1666
        LDR     r10, [r11]
1667 1668 1669
  [ SysVars_QuickContext
        ADD     r10,r10,#SysVars_VTableOffset
  ]
Neil Turton's avatar
Neil Turton committed
1670 1671 1672 1673 1674 1675 1676 1677 1678 1679
        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
1680 1681 1682 1683
        MOV     r2, r3
  [ SysVars_StickyNodes
        BL      SysVars_FreeVNode
  |
Neil Turton's avatar
Neil Turton committed
1684
        BL      FreeSysHeapNode
1685
  ]
Neil Turton's avatar
Neil Turton committed
1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703

        ; 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

1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714
SVVNotFound
 [ International
        MOV     r4, r0
        ADRL    r0, ErrorBlock_VarCantFind
        BL      TranslateError_UseR4
 |
        ADRL    r0, ErrorBlock_VarCantFind
        SETV
 ]
        B       SetVarVal_TestVExit

Neil Turton's avatar
Neil Turton committed
1715
SetVarValBadExit_Translate
1716
 [ International
Neil Turton's avatar
Neil Turton committed
1717
        BL      TranslateError
1718
 ]
1719
SetVarValBadExit_NoTranslate
Neil Turton's avatar
Neil Turton committed
1720 1721 1722 1723 1724 1725
        SETV
        B       SetVarVal_TestVExit

SetVarVal_DisasterExpandingString
SetVarVal_NoRoomForIndex
        MOV     r2, r5
1726
        BL      FreeSysHeapNode         ;forget stickiness (return node to heap is best here)
Neil Turton's avatar
Neil Turton committed
1727 1728 1729 1730 1731 1732 1733
SetVarVal_VarNoRoom
        ADR     r0, ErrorBlock_VarNoRoom
        B       SetVarValBadExit_Translate

SetVarVal_DisasterExpandingBadString
        Push    "r0"                    ; Save bad string error
        MOV     r2, r5
1734
        BL      FreeSysHeapNode         ;forget stickiness (return node to heap is best here)
Neil Turton's avatar
Neil Turton committed
1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753
        Pull    "r0"
        SETV
        B       SetVarVal_TestVExit

        MakeErrorBlock BadVarType
        MakeErrorBlock BadVarNam
        MakeErrorBlock VarTooLong
        MakeErrorBlock BadMacVal
        MakeErrorBlock VarNoRoom


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

; -----------------------------------------------------------------------------
;
; VarFindIt
;
; In
1754
;    r0 -> (wildcard) name of variable to find
Neil Turton's avatar
Neil Turton committed
1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773
;    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
1774
        LDR     r9, =ZeroPage+VariableList
Neil Turton's avatar
Neil Turton committed
1775 1776
        LDR     r9, [r9]
        TEQ     r9, #0
1777 1778 1779 1780 1781
  [ 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
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791
        LDRNE   r8, [r9]
        MOVEQ   r8, #0
        TEQ     r3, #0
        BEQ     %FT20

 [ DebugSysVars
        SWI     XOS_WriteS
        =       "-scan-",0
 ]
        ; r3 non-zero - scan list for entry
1792 1793 1794

  [ SysVars_QuickContext
        ;massive short cut - see if context is the last context we returned
Robert Sprowson's avatar
Robert Sprowson committed
1795
        CMP     r12, r8                 ;if not valid, or higher than current number of vars, forget it
1796 1797 1798 1799 1800 1801 1802 1803
        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
1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819
        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
1820

Neil Turton's avatar
Neil Turton committed
1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865
        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
1866

Neil Turton's avatar
Neil Turton committed
1867
50
1868
        MRSHS   r10, CPSR               ; preserve last HS result we got
Neil Turton's avatar
Neil Turton committed
1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881
        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]
1882 1883
  [ SysVars_QuickContext
        ASSERT  SysVars_LastContext = SysVars_VTableOffset - 4
Robert Sprowson's avatar
Robert Sprowson committed
1884
        STRLS   r6, [r9, #-4]           ;save var table index for context we're returning, in LastContext
1885
  ]
Neil Turton's avatar
Neil Turton committed
1886 1887
        MOVLS   r4, r11
        MOVHI   r3, #0
1888
        MSRLS   CPSR_f, r10
Neil Turton's avatar
Neil Turton committed
1889 1890 1891 1892 1893 1894 1895

 [ DebugSysVars
        SWI     XOS_WriteS
        =       "-complete-",0
        SWI     XOS_NewLine
 ]
        MOV     r12, r6
Kevin Bracey's avatar
Kevin Bracey committed
1896
        TOGPSR  Z_bit, lr
Neil Turton's avatar
Neil Turton committed
1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924
        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]
1925 1926 1927 1928
  [ 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
1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951
        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
1952 1953 1954
        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
1955
        CMP     R1, #"*"
Robert Sprowson's avatar
Robert Sprowson committed
1956 1957
        BEQ     %FT02                   ; IF nextwild = "*"
        LDRB    R2, [R4], #1            ; nextname
Neil Turton's avatar
Neil Turton committed
1958 1959 1960 1961
        CMP     R2, #0
        BEQ     %FT03
        UpperCase R1, R10
        UpperCase R2, R10
Robert Sprowson's avatar
Robert Sprowson committed
1962 1963 1964 1965 1966
        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
1967
        BNE     %FT02
Robert Sprowson's avatar
Robert Sprowson committed
1968 1969
        CMP     PC, #0                  ; set NE
04      Pull    "R0-R3"                 ; return NE (failed)
Neil Turton's avatar
Neil Turton committed
1970 1971
        MOV     PC, lr

Robert Sprowson's avatar
Robert Sprowson committed
1972 1973 1974
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
1975 1976 1977
        Pull    "R0-R3"
        MOV     PC, lr

Robert Sprowson's avatar
Robert Sprowson committed
1978 1979
02      MOV     R11, R0                 ; wild backtrack ptr is char after *
        LDRB    R1, [R0], #1            ; step wild
Neil Turton's avatar
Neil Turton committed
1980
        CMP     R1, #"*"
Robert Sprowson's avatar
Robert Sprowson committed
1981
        BEQ     %BT02                   ; fujj **
Neil Turton's avatar
Neil Turton committed
1982
        UpperCase R1, R10
Robert Sprowson's avatar
Robert Sprowson committed
1983 1984
05      LDRB    R2, [R4], #1            ; step name
        CMP     R2, #0                  ; terminator?
Neil Turton's avatar
Neil Turton committed
1985 1986 1987
        BEQ     %BT03
        UpperCase R2, R10
        CMP     R1, R2
Robert Sprowson's avatar
Robert Sprowson committed
1988
        CMPNE   R1, #"#"                ; match if #
Neil Turton's avatar
Neil Turton committed
1989
        BNE     %BT05
Robert Sprowson's avatar
Robert Sprowson committed
1990 1991
        MOV     R3, R4                  ; name backtrack ptr is char after match
        B       %BT01                   ; LOOP
Neil Turton's avatar
Neil Turton committed
1992

1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012

  [ 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
2013
        LDR     r9, =ZeroPage+VariableList
2014 2015
        LDR     r9, [r9]
        TEQ     r9,#0
Robert Sprowson's avatar
Robert Sprowson committed
2016
        BEQ     %FT99                   ;exit with EQ (not found)
2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053
  [ 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
2054
        MRSHS   r10, CPSR               ; preserve last HS result we got
2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067
        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
2068 2069 2070

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

2071 2072 2073
        MOV     r7, r6
        MOVLS   r6, r11
        MOVHI   r5, #0
2074
        MSRLS   CPSR_f, r10
2075

Kevin Bracey's avatar
Kevin Bracey committed
2076
        TOGPSR  Z_bit, lr
2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100
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
2101
        LDR     r1,=ZeroPage+SysVars_StickyPointers
2102 2103 2104 2105 2106 2107 2108
        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
2109
        LDR     r0,=ZeroPage
2110 2111 2112 2113 2114 2115 2116 2117 2118 2119
        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
2120
        LDR     r0,=ZeroPage
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
        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
2147
        LDR     r3,=ZeroPage+SysVars_StickyPointers
2148 2149 2150 2151 2152 2153
        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
2154
        LDR     r0,=ZeroPage
2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193
        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
2194
        LDR     r1,=ZeroPage+SysVars_StickyPointers
2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225
        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
2226
        LDR     r0,=ZeroPage
2227 2228 2229 2230 2231 2232 2233 2234 2235 2236
        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
2237
        LDR     r0,=ZeroPage
2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250
        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
2251 2252 2253
        LTORG

        END