Initial revision
[ssr] / StraySrc / Utilities / s / setSlot
1 ;
2 ; setSlot.s
3 ;
4 ; Set up a WimpSlot
5 ;
6 ; © 1995-1998 Straylight
7 ;
8
9 ;----- Licensing note -------------------------------------------------------
10 ;
11 ; This file is part of Straylight's core utilities (coreutils).
12 ;
13 ; Coreutils is free software; you can redistribute it and/or modify
14 ; it under the terms of the GNU General Public License as published by
15 ; the Free Software Foundation; either version 2, or (at your option)
16 ; any later version.
17 ;
18 ; Coreutils is distributed in the hope that it will be useful,
19 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ; GNU General Public License for more details.
22 ;
23 ; You should have received a copy of the GNU General Public License
24 ; along with Coreutils. If not, write to the Free Software Foundation,
25 ; 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26
27 ;----- Standard header ------------------------------------------------------
28
29 GET libs:header
30 GET libs:swis
31
32 GET libs:stream
33
34 ;----- External dependencies ------------------------------------------------
35
36 IMPORT version
37
38 ;----- Main code ------------------------------------------------------------
39
40 AREA |!!!Util$$Code|,CODE,READONLY
41
42 ; --- main ---
43 ;
44 ; On entry: R0 == pointer to command line
45 ; R1 == pointer to command tail
46 ; R12 == pointer to workspace
47 ; R13 == pointer to stack
48 ;
49 ; On exit: May return an error
50 ;
51 ; Use: Sets up a wimpslot.
52
53 main ROUT
54
55 ; --- Find the command line ---
56
57 STR R14,[R12,#0] ;Store the link away
58 ADR R0,ss__keys ;Point to key tag definition
59 ADD R2,R12,#256 ;Output into workspace
60 MOV R3,#256 ;Allow 256 bytes for this
61 SWI XOS_ReadArgs ;Read the arguments
62 BVS ss__error ;If it failed, return error
63
64 ; --- Now process the arguments ---
65
66 LDR R0,[R12,#256+0] ;Load the `-help' switch
67 CMP R0,#0 ;Is that set?
68 BNE ss__help ;Yes -- give some help then
69
70 LDR R0,[R12,#256+4] ;Load the base offset
71 CMP R0,#0 ;Is that defined?
72 ADREQ R0,ss__usage ;No -- point to error
73 BEQ ss__error ;And return an error
74 BL ss__read ;Read this number
75 MOV R9,R0 ;And look after the result
76
77 LDR R0,[R12,#256+8] ;Load the heap offset
78 CMP R0,#0 ;Is that defined?
79 BLNE ss__read ;Yes -- read the value
80 MOV R10,R0 ;And look after that too
81
82 LDR R0,[R12,#256+12] ;Load the flex offset
83 CMP R0,#0 ;Is that defined?
84 BLNE ss__read ;Yes -- read the value
85 ADD R10,R10,R0 ;And bump on the heap size
86
87 LDR R8,[R12,#256+16] ;Load the application name
88 CMP R8,#0 ;Is that defined?
89 ADREQ R8,ss__app ;No -- use a default then
90
91 ; --- Read the memory limits imposed on me ---
92
93 MOV R0,#14 ;Read application space
94 MOV R1,#0 ;Don't provide new value
95 SWI XOS_ChangeEnvironment ;Read the value
96 MOV R7,R1 ;Look after that then
97 MOV R0,#0 ;Read the memory limit
98 MOV R1,#0 ;Don't provide new value
99 SWI XOS_ChangeEnvironment ;Read the value
100 MOV R6,R1 ;Look after that too
101 CMP R6,R7 ;Are these the same?
102 BCS %f00 ;We're OK -- skip onwards
103
104 ; --- We're running as a subprogram ---
105
106 SUB R14,R6,#&8000 ;Find how much memory I have
107 CMP R14,R9 ;Is there enough for base?
108 BCC ss__badSub ;No -- complain then
109
110 MOV R1,R7 ;Extend the memory limit
111 SWI XOS_ChangeEnvironment ;Now I can extend the slot
112 MOV R0,#-1 ;I want to read the size
113 MOV R1,#-1 ;Leave next slot alone too
114 SWI XWimp_SlotSize ;Read the current slot size
115 MOV R5,R0 ;Remember this value
116 ADD R0,R5,R10 ;Add on the heap size
117 MOV R1,#-1 ;Leave next slot alone still
118 SWI XWimp_SlotSize ;Try doing that then
119 SUB R7,R0,R5 ;Find how much we added
120 MOV R0,R5 ;Restore to old value
121 MOV R1,#-1 ;Leave next slot alone too
122 SWI XWimp_SlotSize ;Try doing that then
123 MOV R0,#0 ;Now put the memorylimit back
124 MOV R1,R6 ;Get the old memory limit
125 SWI XOS_ChangeEnvironment ;Put that back now
126
127 CMP R7,R10 ;Did we get enough memory?
128 BCC ss__noRoom ;No -- complain bitterly
129 B ss__end ;We finished OK then
130
131 ; --- We're in control here ---
132
133 00 ADD R6,R9,R10 ;Try to get it all
134 MOV R0,R6 ;Get this value
135 MOV R1,#-1 ;Don't change next slot
136 SWI XWimp_SlotSize ;Try doing that then
137 CMP R0,R6 ;Did we get enough?
138 BCC ss__noRoom ;No -- complain then
139 MOV R0,R9 ;Now set up base slot
140 MOV R1,#-1 ;Don't change next slot
141 SWI XWimp_SlotSize ;Try doing that then
142 B ss__end ;And stop the program
143
144 ss__keys DCB "help/S," ;0
145 DCB "base," ;4
146 DCB "heap," ;8
147 DCB "flex," ;12
148 DCB "appName",0 ;16
149
150 ss__usage DCD 1
151 DCB "Syntax: SetSlot -base <size>[K|M] "
152 DCB "[-heap <size>[K|M]] [-flex <size>[K|M]] "
153 DCB "[-appName <name>]",0
154
155 ss__app DCB "Application",0
156
157 LTORG
158
159 ; --- ss__read ---
160 ;
161 ; On entry: R0 == pointer to a string
162 ;
163 ; On exit: R0 == numeric value read from string
164 ;
165 ; Use: Reads a number represented by a string. The number may be
166 ; postfixed by `K' or `M' to indicate that it's shifted left by
167 ; 10 or 20 bits. The resulting value is then aligned up to
168 ; the next multiple of the machine's page size. Scary.
169
170 ss__read ROUT
171
172 STMFD R13!,{R1,R2,R14} ;Save some registers
173
174 ; --- Read the numeric value ---
175
176 MOV R1,R0 ;Point to the string
177 MOV R0,#10 ;By default it's base 10
178 SWI XOS_ReadUnsigned ;Read that value
179 BVS ss__error ;Handle a possible error
180 LDRB R14,[R1],#1 ;Find the terminating char
181 ORR R0,R14,#&20 ;Force it to lowercase
182 CMP R0,#'m' ;Is value in megabytes?
183 MOVEQ R2,R2,LSL #10 ;Yes -- shift it then
184 CMPNE R0,#'k' ;Is value in kilobytes?
185 MOVEQ R2,R2,LSL #10 ;Yes -- shift it then
186 LDREQB R14,[R1],#1 ;And get another byte
187 CMP R14,#&20 ;Make sure this is ctrl char
188 ADRCS R0,ss__junk ;No -- point to error
189 BCS ss__error ;And raise the error
190
191 ; --- Now align it to size ---
192
193 SWI XOS_ReadMemMapInfo ;Find out about memory map
194 BVS ss__error ;Handle a possible error
195 SUB R14,R0,#1 ;Turn page size into bitmask
196 ADD R0,R2,R14 ;And proceed to align
197 BIC R0,R0,R14
198 LDMFD R13!,{R1,R2,PC}^ ;Return to caller
199
200 ss__junk DCD 1
201 DCB "Number not recognised",0
202
203 LTORG
204
205 ; --- ss__badSub ---
206 ;
207 ; On entry: R9, R10 == memory requirements
208 ;
209 ; On exit: --
210 ;
211 ; Use: We didn't have enough memory to start as a subprogram, so
212 ; we complain to the user.
213
214 ss__badSub ROUT
215
216 ADD R1,R12,#512 ;Point to spare bit of memory
217 MOV R14,#1 ;Get an error number
218 STR R14,[R1],#4 ;Store at the beginning
219 MOV R0,R8 ;Point to application name
220 BL ss__strcpy ;Copy that over
221 ADR R0,ss__badSubErr ;Point to the error
222 BL ss__strcpy ;Copy that too
223 MOV R0,R9,LSR #10 ;Get the size in K
224 MOV R2,#256 ;And a bogus buffer size
225 SWI XOS_ConvertInteger4 ;Write that out too
226 ADR R0,ss__badSubEr2 ;Point to rest of the text
227 BL ss__strcpy ;Copy that over
228 ADD R0,R12,#512 ;Point to base of error
229 B ss__error ;Complain now
230
231 ss__badSubErr DCB " must have at least ",0
232 ss__badSubEr2 DCB "K left in application space to start up as a "
233 DCB "subprogram",0
234
235 LTORG
236
237 ; --- ss__noRoom ---
238 ;
239 ; On entry: R9, R10 == memory requirements
240 ;
241 ; On exit: --
242 ;
243 ; Use: We don't have enough memory for the heap or somesuch.
244
245 ss__noRoom ROUT
246
247 ADD R1,R12,#512 ;Point to spare bit of memory
248 MOV R14,#1 ;Get an error number
249 STR R14,[R1],#4 ;Store at the beginning
250 MOV R0,R8 ;Point to application name
251 BL ss__strcpy ;Copy that over
252 ADR R0,ss__noRoomErr ;Point to the error
253 BL ss__strcpy ;Copy that too
254 MOV R0,R9,LSR #10 ;Get the size in K
255 ADD R0,R0,R10,LSR #10 ;Add on the heap size
256 MOV R2,#256 ;And a bogus buffer size
257 SWI XOS_ConvertInteger4 ;Write that out too
258 ADR R0,ss__noRoomEr2 ;Point to rest of the text
259 BL ss__strcpy ;Copy that over
260 ADD R0,R12,#512 ;Point to base of error
261 B ss__error ;Complain now
262
263 ss__noRoomErr DCB " needs at least ",0
264 ss__noRoomEr2 DCB "K of memory available to start up",0
265
266 LTORG
267
268 ; --- ss__strcpy ---
269 ;
270 ; On entry: R0 == pointer to source
271 ; R1 == pointer to destination
272 ;
273 ; On exit: R1 == pointer to terminating null
274 ;
275 ; Use: Copies a string.
276
277 ss__strcpy ROUT
278
279 STMFD R13!,{R14} ;Save a register
280 00 LDRB R14,[R0],#1 ;Load a byte
281 CMP R14,#&20 ;Is this the end?
282 MOVCC R14,#0 ;Yes -- null terminate then
283 STRB R14,[R1],#1 ;Store it in the output
284 BCS %b00 ;Loop back if not done
285 SUB R1,R1,#1 ;Point back at the null
286 LDMFD R13!,{PC}^ ;And return to caller
287
288 LTORG
289
290 ; --- ss__help ---
291 ;
292 ; On entry: --
293 ;
294 ; On exit: --
295 ;
296 ; Use: Gives help about setSlot.
297
298 ss__help ROUT
299
300 ADR R0,ss__helpText ;Point to the help text
301 MOV R1,#0 ;Use system dictionary
302 LDR R2,=version ;Find the version string
303 ADR R14,main ;Find my base address
304 ADD R2,R14,R2 ;And get the string address
305 SWI XOS_PrettyPrint ;Display the text
306 B ss__end ;And return to caller
307
308 ss__helpText DCB "SetSlot ",27,0,13
309 DCB 13
310 DCB "Syntax: SetSlot -base <size>[K|M] "
311 DCB "[-heap <size>[K|M]] [-flex <size>[K|M]] "
312 DCB "[-appName <name>]",13
313 DCB 13
314 DCB "Ensures that there is enough memory for an "
315 DCB "application to start up. The base size is the "
316 DCB "amount of memory for the main image. The heap "
317 DCB "and flex sizes are added together to give the "
318 DCB "required initial size for the shifting heap.",13
319 DCB 0
320
321 LTORG
322
323 ; --- ss__error ---
324 ;
325 ; On entry: R0 == pointer to error
326 ;
327 ; On exit: Doesn't
328 ;
329 ; Use: Returns to the OS with an error.
330
331 ss__error ROUT
332
333 LDR R14,[R12,#0] ;Load the return address
334 ORRS PC,R14,#V_flag ;And return
335
336 LTORG
337
338 ; --- ss__end ---
339 ;
340 ; On entry: --
341 ;
342 ; On exit: Doesn't
343 ;
344 ; Use: Returns to the OS successfully.
345
346 ss__end ROUT
347
348 LDR R14,[R12,#0] ;Load the return address
349 BICS PC,R14,#V_flag ;And return
350
351 LTORG
352
353 ;----- That's all, folks ----------------------------------------------------
354
355 END