start.S revision 1.1 1 1.1 itohy |-----------------------------------------------------------
2 1.1 itohy |
3 1.1 itohy | Simple C runtime startup for Human68k
4 1.1 itohy |
5 1.1 itohy | o no stdio support (DOS/IOCS only)
6 1.1 itohy | o HUPAIR support
7 1.1 itohy |
8 1.1 itohy | written by Yasha (ITOH Yasufumi)
9 1.1 itohy |
10 1.1 itohy | This file is in the public domain
11 1.1 itohy |
12 1.1 itohy | $NetBSD: start.S,v 1.1 1998/09/01 19:51:56 itohy Exp $
13 1.1 itohy
14 1.1 itohy |-----------------------------------------------------------
15 1.1 itohy |
16 1.1 itohy | configuration
17 1.1 itohy |
18 1.1 itohy #ifndef STACK_SIZE
19 1.1 itohy #define STACK_SIZE 65536 /* stack size in bytes */
20 1.1 itohy #endif
21 1.1 itohy
22 1.1 itohy #ifndef STACK_SYMBOL
23 1.1 itohy #define STACK_SYMBOL stack_8K_hUMAn6 /* has largest hash val on NetBSD ld */
24 1.1 itohy #endif /* and will be at the end of bss */
25 1.1 itohy
26 1.1 itohy #ifndef DUMMY___main
27 1.1 itohy #define DUMMY___main 1 /* dummy __main() */
28 1.1 itohy #endif
29 1.1 itohy
30 1.1 itohy #ifndef SUPPORT_R_EXEC /* support ".r" relocatable executable */
31 1.1 itohy #define SUPPORT_R_EXEC 0 /* (clear bss, don't use a1 at startup) */
32 1.1 itohy #endif /* XXX impossible for a.out */
33 1.1 itohy
34 1.1 itohy #ifndef SUPPORT_HUPAIR
35 1.1 itohy #define SUPPORT_HUPAIR 1 /* HUPAIR argument interface support */
36 1.1 itohy #endif
37 1.1 itohy
38 1.1 itohy #ifndef HUPAIR_ARGV0
39 1.1 itohy #define HUPAIR_ARGV0 1 /* use argv[0] passed in HUPAIR manner */
40 1.1 itohy #endif
41 1.1 itohy
42 1.1 itohy #ifndef ADD_PATHNAME
43 1.1 itohy #define ADD_PATHNAME 0 /* add command path to argv[0] if not HUPAIR */
44 1.1 itohy #endif
45 1.1 itohy
46 1.1 itohy #ifndef STRICT_SETBLOCK
47 1.1 itohy #define STRICT_SETBLOCK 1 /* free unused memory after creating args */
48 1.1 itohy #endif
49 1.1 itohy
50 1.1 itohy #ifndef C_REGPARM
51 1.1 itohy #define C_REGPARM 0 /* main() arguments are passed in registers */
52 1.1 itohy #endif /* (for gcc -mregparm) */
53 1.1 itohy
54 1.1 itohy #ifndef NEED_MEMCP
55 1.1 itohy #define NEED_MEMCP 0 /* __memcp: MCB address */
56 1.1 itohy #endif
57 1.1 itohy #ifndef NEED_PROCP
58 1.1 itohy #define NEED_PROCP 0 /* __procp: PDB address */
59 1.1 itohy #endif
60 1.1 itohy #ifndef NEED_VERNUM
61 1.1 itohy #define NEED_VERNUM 1 /* __vernum: Human68k version */
62 1.1 itohy #endif
63 1.1 itohy #ifndef NEED_PROGNAME
64 1.1 itohy #define NEED_PROGNAME 1 /* ___progname: program basename */
65 1.1 itohy #endif
66 1.1 itohy #ifndef NEED_ENVIRON
67 1.1 itohy #define NEED_ENVIRON 1 /* _environ: environment vector */
68 1.1 itohy #endif
69 1.1 itohy
70 1.1 itohy |-----------------------------------------------------------
71 1.1 itohy |
72 1.1 itohy | DOS call
73 1.1 itohy |
74 1.1 itohy #define DOS(x) .word x
75 1.1 itohy
76 1.1 itohy #define __FPUTS 0xFF1E
77 1.1 itohy #define __VERNUM 0xFF30
78 1.1 itohy #define __SETBLOCK 0xFF4A
79 1.1 itohy #define __EXIT2 0xFF4C
80 1.1 itohy
81 1.1 itohy |
82 1.1 itohy | seed to estimate argument string/vector and environment vector size
83 1.1 itohy | (max nohupair argv[0](92+4) + NULLs(8) + alignment(3)) <- donburi?
84 1.1 itohy |
85 1.1 itohy #define estimated_argsz 107
86 1.1 itohy #define estimated_com 92 /* estimated command name length (included) */
87 1.1 itohy
88 1.1 itohy |
89 1.1 itohy | other constants
90 1.1 itohy |
91 1.1 itohy #define char_tab 0x09
92 1.1 itohy #define char_space 0x20
93 1.1 itohy #define char_dquote 0x22
94 1.1 itohy #define char_squote 0x27
95 1.1 itohy #define char_slash 0x2f
96 1.1 itohy #define char_backslash 0x5c
97 1.1 itohy
98 1.1 itohy #define pdb_mcb 0x10 /* PDB address - MCB address */
99 1.1 itohy #define drvpath_pdb 0x070 /* drive and path address - PDB address */
100 1.1 itohy #define command_pdb 0x0b4 /* command name address - PDB address */
101 1.1 itohy #define top_pdb 0xf0 /* program load address - PDB address */
102 1.1 itohy
103 1.1 itohy #define stderr 2 /* stderr file handle */
104 1.1 itohy #define exit_nomem 127 /* exit status on SETBLOCK failure */
105 1.1 itohy
106 1.1 itohy |-----------------------------------------------------------
107 1.1 itohy |
108 1.1 itohy | execution start
109 1.1 itohy |
110 1.1 itohy | a0: MCB address, a1: program end + 1,
111 1.1 itohy | a2: command line, a3: environ, a4: execution start
112 1.1 itohy |
113 1.1 itohy |.cpu 68000
114 1.1 itohy .text
115 1.1 itohy .even
116 1.1 itohy
117 1.1 itohy .globl start,_main
118 1.1 itohy
119 1.1 itohy start:
120 1.1 itohy #if SUPPORT_HUPAIR
121 1.1 itohy .word 0x611e,0x2348,0x5550,0x4149,0x5200
122 1.1 itohy #else
123 1.1 itohy .word 0x6016
124 1.1 itohy #endif
125 1.1 itohy #if SUPPORT_R_EXEC
126 1.1 itohy .word 0x7263
127 1.1 itohy #else
128 1.1 itohy .word 0x7863
129 1.1 itohy #endif
130 1.1 itohy .long 0x72743020,0x56312E31,0x42206279,0x20596173,0x68610000
131 1.1 itohy
132 1.1 itohy |
133 1.1 itohy | check if hupair
134 1.1 itohy |
135 1.1 itohy #if SUPPORT_HUPAIR
136 1.1 itohy moveal a7@+,a4
137 1.1 itohy lea a2@(-8),a6
138 1.1 itohy moveql #7,d3
139 1.1 itohy chkhupair:
140 1.1 itohy cmpmb a6@+,a4@+
141 1.1 itohy dbne d3,chkhupair
142 1.1 itohy | d3.l: 0xFFFF: hupair, 0x000x: not hupair
143 1.1 itohy addqw #1,d3
144 1.1 itohy beqs ishupair
145 1.1 itohy #endif
146 1.1 itohy moveql #char_tab,d3 | tab (= 9)
147 1.1 itohy ishupair: | d3.l: 0: hupair, 9: not hupair
148 1.1 itohy
149 1.1 itohy |
150 1.1 itohy | (over)estimate and allocate argument/environ area beforehand
151 1.1 itohy |
152 1.1 itohy addql #1,a2 | skip byte count
153 1.1 itohy moveql #estimated_argsz,d1 | byte counter
154 1.1 itohy moveal a2,a6
155 1.1 itohy moveql #char_space,d4 | space
156 1.1 itohy acou1: addql #1,d1
157 1.1 itohy moveb a6@+,d0
158 1.1 itohy beqs acou2
159 1.1 itohy cmpb d4,d0 | space
160 1.1 itohy beqs acous
161 1.1 itohy cmpb d3,d0 | tab (if not hupair)
162 1.1 itohy bnes acou1
163 1.1 itohy acous: addql #4,d1 | for argv area
164 1.1 itohy bras acou1
165 1.1 itohy
166 1.1 itohy acou2:
167 1.1 itohy #if SUPPORT_HUPAIR && HUPAIR_ARGV0
168 1.1 itohy tstb d3
169 1.1 itohy bnes anohp
170 1.1 itohy moveql #-estimated_com,d2 | reset argv[0] length
171 1.1 itohy moveal a6,a4 | preserve argv[0] string address
172 1.1 itohy acouhp: addql #1,d2
173 1.1 itohy tstb a6@+
174 1.1 itohy bnes acouhp
175 1.1 itohy addl d2,d1
176 1.1 itohy anohp:
177 1.1 itohy #endif
178 1.1 itohy | d1: estimated argument bytes
179 1.1 itohy
180 1.1 itohy #if NEED_ENVIRON
181 1.1 itohy addql #4,a3 | skip length field
182 1.1 itohy moveal a3,a6
183 1.1 itohy ecou1: addql #4,d1
184 1.1 itohy tstb a6@+
185 1.1 itohy beqs ecoue
186 1.1 itohy ecou2: tstb a6@+
187 1.1 itohy bnes ecou2
188 1.1 itohy bras ecou1
189 1.1 itohy ecoue:
190 1.1 itohy #endif
191 1.1 itohy | d1: estimated byte count
192 1.1 itohy
193 1.1 itohy |
194 1.1 itohy | free memory
195 1.1 itohy | and ensure the bss/stack (for .r executable) and argument areas valid
196 1.1 itohy |
197 1.1 itohy lea a0@(pdb_mcb),a5 | a5: PDB address
198 1.1 itohy subl a5,d1
199 1.1 itohy #if SUPPORT_R_EXEC
200 1.1 itohy #define RELOC(sym, reg) lea sym+top_pdb,reg; addl a5,reg
201 1.1 itohy moveal a1,a6 | end of data
202 1.1 itohy RELOC(_end, a1) | end of bss
203 1.1 itohy #endif
204 1.1 itohy pea a1@(0,d1:l) | _end + size - pdb
205 1.1 itohy movel a5,a7@-
206 1.1 itohy DOS(__SETBLOCK)
207 1.1 itohy tstl d0
208 1.1 itohy bpls sbnoerr
209 1.1 itohy
210 1.1 itohy setblock_err:
211 1.1 itohy movew #stderr,a7@
212 1.1 itohy bsrs sberr1 | pea pc@
213 1.1 itohy .asciz "setblock failed\r\n"
214 1.1 itohy .even
215 1.1 itohy sberr1: DOS(__FPUTS)
216 1.1 itohy movew #exit_nomem,a7@
217 1.1 itohy DOS(__EXIT2) | _exit(exit_nomem)
218 1.1 itohy
219 1.1 itohy sbnoerr:
220 1.1 itohy
221 1.1 itohy | here, the bss, stack, and argument/environ areas are certainly valid
222 1.1 itohy
223 1.1 itohy |
224 1.1 itohy | set stack
225 1.1 itohy |
226 1.1 itohy moveal #STACK_SYMBOL+STACK_SIZE,a7
227 1.1 itohy
228 1.1 itohy #if SUPPORT_R_EXEC
229 1.1 itohy |
230 1.1 itohy | clear bss section
231 1.1 itohy |
232 1.1 itohy loop_clrbss:
233 1.1 itohy clrl a6@+
234 1.1 itohy cmpal a1,a6
235 1.1 itohy bcss loop_clrbss
236 1.1 itohy #endif
237 1.1 itohy
238 1.1 itohy |
239 1.1 itohy | save MCB address
240 1.1 itohy |
241 1.1 itohy #if NEED_MEMCP
242 1.1 itohy # if SUPPORT_R_EXEC
243 1.1 itohy RELOC(__memcp, a6)
244 1.1 itohy movel a0,a6@
245 1.1 itohy # else
246 1.1 itohy movel a0,__memcp
247 1.1 itohy # endif
248 1.1 itohy #endif
249 1.1 itohy
250 1.1 itohy |
251 1.1 itohy | save PDB address
252 1.1 itohy |
253 1.1 itohy #if NEED_PROCP
254 1.1 itohy # if SUPPORT_R_EXEC
255 1.1 itohy RELOC(__procp, a6)
256 1.1 itohy movel a5,a6@
257 1.1 itohy # else
258 1.1 itohy movel a5,__procp
259 1.1 itohy # endif
260 1.1 itohy #endif
261 1.1 itohy
262 1.1 itohy |
263 1.1 itohy | get version no of Human
264 1.1 itohy |
265 1.1 itohy #if NEED_VERNUM
266 1.1 itohy DOS(__VERNUM)
267 1.1 itohy # if SUPPORT_R_EXEC
268 1.1 itohy RELOC(__vernum, a6)
269 1.1 itohy movel d0,a6@
270 1.1 itohy # else
271 1.1 itohy movel d0,__vernum
272 1.1 itohy # endif
273 1.1 itohy #endif
274 1.1 itohy
275 1.1 itohy |
276 1.1 itohy | create argv[0]
277 1.1 itohy |
278 1.1 itohy moveal a1,a0 | top of argument strings
279 1.1 itohy #if SUPPORT_HUPAIR && HUPAIR_ARGV0
280 1.1 itohy tstb d3
281 1.1 itohy beqs arg0lp
282 1.1 itohy #endif
283 1.1 itohy #if ADD_PATHNAME
284 1.1 itohy lea a5@(drvpath_pdb),a4 | drive and path name
285 1.1 itohy arg0path:
286 1.1 itohy moveb a4@+,a1@+
287 1.1 itohy bnes arg0path
288 1.1 itohy subql #1,a1 | remove nul char
289 1.1 itohy #endif
290 1.1 itohy lea a5@(command_pdb),a4 | command name
291 1.1 itohy arg0lp: moveb a4@+,a1@+
292 1.1 itohy bnes arg0lp
293 1.1 itohy
294 1.1 itohy #if NEED_PROGNAME
295 1.1 itohy |
296 1.1 itohy | find program basename
297 1.1 itohy |
298 1.1 itohy moveal a1,a4
299 1.1 itohy prognlp:
300 1.1 itohy cmpal a0,a4
301 1.1 itohy beqs prognexit
302 1.1 itohy moveb a4@-,d0
303 1.1 itohy cmpib #char_slash,d0
304 1.1 itohy beqs prognfou
305 1.1 itohy cmpib #char_backslash,d0
306 1.1 itohy bnes prognlp
307 1.1 itohy prognfou:
308 1.1 itohy addql #1,a4 | next of slash
309 1.1 itohy prognexit:
310 1.1 itohy # if SUPPORT_R_EXEC
311 1.1 itohy RELOC(___progname, a6)
312 1.1 itohy movel a4,a6@
313 1.1 itohy # else
314 1.1 itohy movel a4,___progname
315 1.1 itohy # endif
316 1.1 itohy #endif
317 1.1 itohy
318 1.1 itohy |
319 1.1 itohy | create argument strings
320 1.1 itohy |
321 1.1 itohy moveql #1,d0 | (d0:l) # arg
322 1.1 itohy
323 1.1 itohy spskip: moveb a2@+,d2
324 1.1 itohy beqs comline_end
325 1.1 itohy cmpb d4,d2 | space
326 1.1 itohy beqs spskip
327 1.1 itohy cmpb d3,d2 | tab (if not hupair)
328 1.1 itohy beqs spskip
329 1.1 itohy
330 1.1 itohy | create an arg
331 1.1 itohy clrb d1 | no quote here
332 1.1 itohy addql #1,d0 | increment argc
333 1.1 itohy
334 1.1 itohy arglp: tstb d1
335 1.1 itohy bnes in_quote
336 1.1 itohy cmpib #char_dquote,d2
337 1.1 itohy beqs quote
338 1.1 itohy cmpib #char_squote,d2
339 1.1 itohy bnes notquote
340 1.1 itohy quote: moveb d2,d1 | save quote character
341 1.1 itohy bras argnextc
342 1.1 itohy
343 1.1 itohy in_quote:
344 1.1 itohy cmpb d1,d2
345 1.1 itohy bnes argcopyc
346 1.1 itohy clrb d1 | quote ended
347 1.1 itohy bras argnextc
348 1.1 itohy
349 1.1 itohy notquote:
350 1.1 itohy cmpb d4,d2 | space
351 1.1 itohy beqs arg_end
352 1.1 itohy cmpb d3,d2 | tab (if not hupair)
353 1.1 itohy bnes argcopyc
354 1.1 itohy arg_end:
355 1.1 itohy clrb a1@+
356 1.1 itohy bras spskip
357 1.1 itohy
358 1.1 itohy argcopyc:
359 1.1 itohy moveb d2,a1@+ | copy char
360 1.1 itohy
361 1.1 itohy argnextc:
362 1.1 itohy moveb a2@+,d2
363 1.1 itohy bnes arglp
364 1.1 itohy clrb a1@+
365 1.1 itohy
366 1.1 itohy comline_end:
367 1.1 itohy
368 1.1 itohy |
369 1.1 itohy | create argv vector
370 1.1 itohy |
371 1.1 itohy addql #3,a1
372 1.1 itohy movel a1,d1
373 1.1 itohy andib #0xfc,d1 | long alignment
374 1.1 itohy moveal d1,a1 | argv
375 1.1 itohy movel d0,d4 | argc
376 1.1 itohy | a0 is at argument strings
377 1.1 itohy mkargv:
378 1.1 itohy movel a0,a1@+ | argv[0] ...
379 1.1 itohy nxtarg: tstb a0@+
380 1.1 itohy bnes nxtarg
381 1.1 itohy #if STRICT_SETBLOCK
382 1.1 itohy subqw #1,d0
383 1.1 itohy #else
384 1.1 itohy subqw #1,d4
385 1.1 itohy #endif
386 1.1 itohy bnes mkargv
387 1.1 itohy
388 1.1 itohy clrl a1@+ | argv[argc] should be NULL
389 1.1 itohy
390 1.1 itohy |
391 1.1 itohy | create envp vector
392 1.1 itohy |
393 1.1 itohy #if NEED_ENVIRON
394 1.1 itohy movel a1,d2
395 1.1 itohy envlp: tstb a3@
396 1.1 itohy beqs envend
397 1.1 itohy movel a3,a1@+
398 1.1 itohy envskp: tstb a3@+
399 1.1 itohy bnes envskp
400 1.1 itohy bras envlp
401 1.1 itohy envend: clrl a1@+ | NULL termination
402 1.1 itohy # if SUPPORT_R_EXEC
403 1.1 itohy RELOC(_environ, a0)
404 1.1 itohy movel d2,a0@
405 1.1 itohy # else
406 1.1 itohy movel d2,_environ
407 1.1 itohy # endif
408 1.1 itohy #endif
409 1.1 itohy
410 1.1 itohy |
411 1.1 itohy | free unused memory
412 1.1 itohy |
413 1.1 itohy #if STRICT_SETBLOCK
414 1.1 itohy subal a5,a1
415 1.1 itohy movel a1,a7@-
416 1.1 itohy movel a5,a7@-
417 1.1 itohy DOS(__SETBLOCK) | reset donburi-kanjo (never fails)
418 1.1 itohy addql #8,a7
419 1.1 itohy movel d4,d0 | argc
420 1.1 itohy #endif
421 1.1 itohy
422 1.1 itohy |
423 1.1 itohy | make parameter
424 1.1 itohy |
425 1.1 itohy #if NEED_ENVIRON
426 1.1 itohy movel d2,a7@- | arg #3 --- envp
427 1.1 itohy #endif
428 1.1 itohy #if !C_REGPARM
429 1.1 itohy movel d1,a7@- | arg #2 --- argv
430 1.1 itohy movel d0,a7@- | arg #1 --- argc
431 1.1 itohy #endif
432 1.1 itohy
433 1.1 itohy #if SUPPORT_R_EXEC
434 1.1 itohy RELOC(_main, a0)
435 1.1 itohy jsr a0@
436 1.1 itohy #else
437 1.1 itohy jsr _main
438 1.1 itohy #endif
439 1.1 itohy
440 1.1 itohy #if !C_REGPARM || NEED_ENVIRON
441 1.1 itohy movew d0,a7@
442 1.1 itohy #else
443 1.1 itohy movew d0,a7@-
444 1.1 itohy #endif
445 1.1 itohy DOS(__EXIT2)
446 1.1 itohy
447 1.1 itohy #if DUMMY___main
448 1.1 itohy .globl ___main
449 1.1 itohy ___main:
450 1.1 itohy rts
451 1.1 itohy #endif
452 1.1 itohy
453 1.1 itohy |-----------------------------------------------------------
454 1.1 itohy |
455 1.1 itohy | variables
456 1.1 itohy |
457 1.1 itohy #if NEED_MEMCP
458 1.1 itohy .comm __memcp,4
459 1.1 itohy #endif
460 1.1 itohy
461 1.1 itohy #if NEED_PROCP
462 1.1 itohy .comm __procp,4 | PDB address
463 1.1 itohy #endif
464 1.1 itohy
465 1.1 itohy #if NEED_VERNUM
466 1.1 itohy .comm __vernum,4
467 1.1 itohy #endif
468 1.1 itohy
469 1.1 itohy #if NEED_PROGNAME
470 1.1 itohy .comm ___progname,4
471 1.1 itohy #endif
472 1.1 itohy
473 1.1 itohy #if NEED_ENVIRON
474 1.1 itohy .comm _environ,4 | environ address
475 1.1 itohy #endif
476 1.1 itohy
477 1.1 itohy |-----------------------------------------------------------
478 1.1 itohy |
479 1.1 itohy | stack
480 1.1 itohy |
481 1.1 itohy .comm STACK_SYMBOL,STACK_SIZE
482