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