prt0.as 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. |*************************************************************************
  2. |* *
  3. |* DSTART.S Startup module for Pascal programs using dLibs *
  4. |* *
  5. |*************************************************************************
  6. |*
  7. |* entry points
  8. |*
  9. .globl __base | basepage pointer
  10. .globl __start | startup entry point
  11. .globl _etext | end of text segment
  12. .globl _edata | end of data segment
  13. .globl _end | end of BSS segment (end of program)
  14. .globl __BREAK | location of stack/heap break
  15. .globl __ARGC | number of arguments
  16. .globl __ARGS | argument list pointer
  17. .globl __envp | environment string pointer
  18. .globl _errno | system error number
  19. |
  20. | external references
  21. |
  22. |.globl __stklen | Stack size value from C (unsigned long)
  23. |
  24. | useful constants
  25. |
  26. MINSTK = 16384 | Minimum 16K stack size
  27. MARGIN = 512 | Minimum memory to return to OS
  28. |
  29. | GEMDOS functions
  30. |
  31. Cconws = 0x09 | Console write string
  32. Pterm = 0x4C | Process terminate (with exit code)
  33. Mshrink = 0x4A | Shrink program space
  34. |
  35. | basepage offsets
  36. |
  37. p_hitpa = 0x04 | top of TPA
  38. p_tbase = 0x08 | base of text
  39. p_tlen = 0x0C | length of text
  40. p_dbase = 0x10 | base of data
  41. p_dlen = 0x14 | length of data
  42. p_bbase = 0x18 | base of BSS
  43. p_blen = 0x1C | length of BSS
  44. p_env = 0x2C | environment string
  45. p_cmdlin = 0x80 | command line image
  46. |
  47. | STARTUP ROUTINE (must be first object in link)
  48. |
  49. .text
  50. .globl start
  51. .globl _start
  52. __start:
  53. start:
  54. _start:
  55. |
  56. | save initial stack and basepage pointers
  57. |
  58. movel sp,a5 | a5 = initial stack pointer
  59. movel sp@(4),a4 | a4 = basepage address
  60. movel a4,__base
  61. movel a4@(p_tbase),d3
  62. addl a4@(p_tlen),d3
  63. movel d3,_etext | end of text segment
  64. movel a4@(p_dbase),d3
  65. addl a4@(p_dlen),d3
  66. movel d3,_edata | end of data segment
  67. movel a4@(p_bbase),d3
  68. addl a4@(p_blen),d3
  69. movel d3,_end | end of BSS (end of program)
  70. movel d3,__BREAK; | set initial _break value
  71. movel a4@(p_env),__envp | save environment pointer
  72. |
  73. | call C function to get command line arguments
  74. |
  75. lea a4@(p_cmdlin),a0 | get pointer to command line image
  76. moveb a0@+,d0
  77. extw d0 | extract length
  78. | movew d0,sp@- | cmdlen
  79. | movel a0,sp@- | cmdline
  80. | jsr __initar | call _initargs(cmdline, cmdlen)
  81. | addql #6,sp
  82. movew d0,__ARGC | save length
  83. movel a0,__ARGS | save pointer to string
  84. |
  85. | calculate free space available to program
  86. |
  87. movel __BREAK,d3
  88. movel d3,a3 | a3 = base of free space
  89. negl d3
  90. addl a4@(p_hitpa),d3
  91. subl #MARGIN,d3 | d3 = free space
  92. |
  93. | calculate new stack size (store in d2)
  94. |
  95. | ASSUME 8K STACK FOR THE MOMENT.
  96. movel __stklen,d2 | d2 = _STKSIZ
  97. tstl d2 | if __STKSIZ is zero
  98. beq minimum | use MINSTK
  99. bra setstk | use __STKSIZ
  100. minimum:
  101. movel #MINSTK,d2 | use MINSTK
  102. |
  103. | check to see if there is enough room for requested stack
  104. |
  105. setstk:
  106. cmpl d3,d2
  107. blt shrink | if (available < requested)
  108. movel #stkerr,sp@-
  109. movew #Cconws,sp@-
  110. trap #1 | report a stack error
  111. addql #6,sp
  112. movew #-39,sp@-
  113. movew #Pterm,sp@-
  114. trap #1 | and return error -39 (ENSMEM)
  115. |
  116. | set up new stack pointer and Mshrink
  117. |
  118. shrink:
  119. addl a3,d2 | new stack = free base + stack size
  120. movel d2,sp
  121. subl a4,d2 | keep space = new stack - __base
  122. movel d2,sp@-
  123. movel a4,sp@-
  124. clrw sp@-
  125. movew #Mshrink,sp@-
  126. trap #1 | Mshrink(0, _base, keep);
  127. addl #12,sp
  128. |
  129. | call C entry point function _main()
  130. |
  131. jsr PASCALMAIN | if _main returns
  132. movew #0,sp@- | Terminate program normally
  133. trap #1
  134. | movew d0,sp@(4) | insert return value and fall thru
  135. |
  136. | check for stack overflow (done after all OS traps)
  137. |
  138. chkstk:
  139. cmpl __BREAK,sp
  140. bgt nosweat | if (_break > sp)
  141. movel #stkovf,sp@-
  142. movew #Cconws,sp@-
  143. trap #1 | report a stack overflow
  144. addql #6,sp
  145. movew #-1,sp@-
  146. movew #Pterm,sp@-
  147. trap #1 | and return error -1 (ERROR)
  148. nosweat:
  149. movel traprtn,sp@- | else, restore return address
  150. rts | and do a normal return.
  151. |
  152. | this call to _main ensures that it the user's main() function will be
  153. | linked, even if it is in a library.
  154. |
  155. jsr PASCALMAIN | NO PATH TO THIS STATEMENT
  156. movew d0,sp@-
  157. movew #0x4c,sp@-
  158. trap #1
  159. |
  160. | initialized data space
  161. |
  162. .data
  163. .even
  164. stkerr: | not enough memory for stack
  165. .ascii "Not enough memory"
  166. .byte 0x0d,0x0a,0x00
  167. stkovf: | impending stack overflow
  168. .ascii "Stack overflow"
  169. .byte 0x0d,0x0a,0x00
  170. .even
  171. _errno: | system error number
  172. .word 0
  173. __ARGC: | number of command line args
  174. .word 0
  175. __ARGS: | pointer to command line arg list
  176. .long 0
  177. |
  178. | uninitialized data space
  179. |
  180. .even
  181. __base: | pointer to basepage
  182. .long 0
  183. _etext: | pointer to end of text segment
  184. .long 0
  185. _edata: | pointer to end of data segment
  186. .long 0
  187. _end: | pointer to end of BSS (end of program)
  188. .long 0
  189. __BREAK: | pointer to stack/heap break
  190. .long 0
  191. __envp: | pointer to environment string
  192. .long 0
  193. traprtn: | storage for return PC in trap hooks
  194. .long 0