Mirror of CollapseOS
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

385 rindas
10KB

  1. ( Configuration words: RAMSTART, RS_ADDR )
  2. H@ 256 /MOD 2 PC! 2 PC!
  3. ( STABLE ABI
  4. Those jumps below are supposed to stay at these offsets,
  5. always. If they change bootstrap binaries have to be
  6. adjusted because they rely on them. Those entries are
  7. referenced directly by their offset in Forth code with a
  8. comment indicating what that number refers to.
  9. )
  10. H@ ORG !
  11. 0 JPnn, ( 00, main )
  12. 0 JPnn, ( 03, find )
  13. NOP, NOP, ( 06, unused )
  14. NOP, NOP, ( 08, LATEST )
  15. NOP, ( 0a, unused )
  16. 0 JPnn, ( 0b, cellWord )
  17. 0 JPnn, ( 0e, compiledWord )
  18. 0 JPnn, ( 11, pushRS )
  19. 0 JPnn, ( 14, popRS )
  20. JP(IY), NOP, ( 17, nativeWord )
  21. 0 JPnn, ( 1a, next )
  22. 0 JPnn, ( 1d, chkPS )
  23. NOP, NOP, ( 20, numberWord )
  24. NOP, NOP, ( 22, litWord )
  25. NOP, NOP, ( 24, unused )
  26. NOP, NOP, ( 26, unused )
  27. 0 JPnn, ( 28, flagsToBC )
  28. 0 JPnn, ( 2b, doesWord )
  29. NOP, NOP, ( 2e, unused )
  30. NOP, NOP, ( 30, unused )
  31. NOP, NOP, ( 32, unused )
  32. NOP, NOP, ( 34, unused )
  33. NOP, NOP, ( 36, unused )
  34. NOP, NOP, ( 38, unused )
  35. NOP, NOP, ( 3a, unused )
  36. ( BOOT DICT
  37. There are only 5 words in the boot dict, but these words'
  38. offset need to be stable, so they're part of the "stable
  39. ABI"
  40. )
  41. 'E' A, 'X' A, 'I' A, 'T' A,
  42. 0 A,, ( prev )
  43. 4 A,
  44. L1 BSET ( EXIT )
  45. 0x17 A,, ( nativeWord )
  46. 0x14 CALLnn, ( popRS )
  47. RAMSTART 0x06 + LD(nn)HL, ( RAMSTART+0x06 == IP )
  48. JPNEXT,
  49. NOP, NOP, NOP, ( unused )
  50. '(' A, 'b' A, 'r' A, ')' A,
  51. PC L1 @ - A,, ( prev )
  52. 4 A,
  53. L1 BSET ( BR )
  54. 0x17 A,, ( nativeWord )
  55. L2 BSET ( used in CBR )
  56. RAMSTART 0x06 + LDHL(nn), ( RAMSTART+0x06 == IP )
  57. E (HL) LDrr,
  58. HL INCss,
  59. D (HL) LDrr,
  60. HL DECss,
  61. DE ADDHLss,
  62. RAMSTART 0x06 + LD(nn)HL, ( RAMSTART+0x06 == IP )
  63. JPNEXT,
  64. '(' A, '?' A, 'b' A, 'r' A, ')' A,
  65. PC L1 @ - A,, ( prev )
  66. 5 A,
  67. L1 BSET ( CBR )
  68. 0x17 A,, ( nativeWord )
  69. HL POPqq,
  70. chkPS,
  71. A H LDrr,
  72. L ORr,
  73. JRZ, L2 BWR ( BR + 2. False, branch )
  74. ( True, skip next 2 bytes and don't branch )
  75. RAMSTART 0x06 + LDHL(nn), ( RAMSTART+0x06 == IP )
  76. HL INCss,
  77. HL INCss,
  78. RAMSTART 0x06 + LD(nn)HL, ( RAMSTART+0x06 == IP )
  79. JPNEXT,
  80. 'E' A, 'X' A, 'E' A, 'C' A, 'U' A, 'T' A, 'E' A,
  81. PC L1 @ - A,, ( prev )
  82. 7 A,
  83. L2 BSET ( used frequently below )
  84. 0x17 A,, ( nativeWord )
  85. IY POPqq, ( is a wordref )
  86. chkPS,
  87. L 0 IY+ LDrIXY,
  88. H 1 IY+ LDrIXY,
  89. ( HL points to code pointer )
  90. IY INCss,
  91. IY INCss,
  92. ( IY points to PFA )
  93. JP(HL),
  94. ( END OF STABLE ABI )
  95. ( Name of BOOT word )
  96. L1 BSET
  97. 'B' A, 'O' A, 'O' A, 'T' A, 0 A,
  98. PC ORG @ 1 + ! ( main )
  99. ( STACK OVERFLOW PROTECTION:
  100. To avoid having to check for stack underflow after each pop
  101. operation (which can end up being prohibitive in terms of
  102. costs), we give ourselves a nice 6 bytes buffer. 6 bytes
  103. because we seldom have words requiring more than 3 items
  104. from the stack. Then, at each "exit" call we check for
  105. stack underflow.
  106. )
  107. SP 0xfffa LDddnn,
  108. RAMSTART SP LD(nn)dd, ( RAM+00 == INITIAL_SP )
  109. IX RS_ADDR LDddnn,
  110. ( LATEST is a label to the latest entry of the dict. It is
  111. written at offset 0x08 by the process or person building
  112. Forth. )
  113. 0x08 LDHL(nn),
  114. RAMSTART 0x02 + LD(nn)HL, ( RAM+02 == CURRENT )
  115. RAMSTART 0x04 + LD(nn)HL, ( RAM+04 == HERE )
  116. HL L1 @ LDddnn,
  117. 0x03 CALLnn, ( 03 == find )
  118. DE PUSHqq,
  119. L2 @ 2 + JPnn,
  120. PC ORG @ 4 + ! ( find )
  121. ( Find the entry corresponding to word where (HL) points to
  122. and sets DE to point to that entry. Z if found, NZ if not.
  123. )
  124. BC PUSHqq,
  125. HL PUSHqq,
  126. ( First, figure out string len )
  127. BC 0 LDddnn,
  128. A XORr,
  129. CPIR,
  130. ( C has our length, negative, -1 )
  131. A C LDrr,
  132. NEG,
  133. A DECr,
  134. ( special case. zero len? we never find anything. )
  135. JRZ, L1 FWR ( fail )
  136. C A LDrr, ( C holds our length )
  137. ( Let's do something weird: We'll hold HL by the *tail*.
  138. Because of our dict structure and because we know our
  139. lengths, it's easier to compare starting from the end.
  140. Currently, after CPIR, HL points to char after null. Let's
  141. adjust. Because the compare loop pre-decrements, instead
  142. of DECing HL twice, we DEC it once. )
  143. HL DECss,
  144. DE RAMSTART 0x02 + LDdd(nn), ( RAM+02 == CURRENT )
  145. L3 BSET ( inner )
  146. ( DE is a wordref, first step, do our len correspond? )
  147. HL PUSHqq, ( --> lvl 1 )
  148. DE PUSHqq, ( --> lvl 2 )
  149. DE DECss,
  150. LDA(DE),
  151. 0x7f ANDn, ( remove IMMEDIATE flag )
  152. C CPr,
  153. JRNZ, L4 FWR ( loopend )
  154. ( match, let's compare the string then )
  155. DE DECss, ( Skip prev field. One less because we )
  156. DE DECss, ( pre-decrement )
  157. B C LDrr, ( loop C times )
  158. L5 BSET ( loop )
  159. ( pre-decrement for easier Z matching )
  160. DE DECss,
  161. HL DECss,
  162. LDA(DE),
  163. (HL) CPr,
  164. JRNZ, L6 FWR ( loopend )
  165. DJNZ, L5 BWR ( loop )
  166. L4 FSET L6 FSET ( loopend )
  167. ( At this point, Z is set if we have a match. In all cases,
  168. we want to pop HL and DE )
  169. DE POPqq, ( <-- lvl 2 )
  170. HL POPqq, ( <-- lvl 1 )
  171. JRZ, L4 FWR ( end, match? we're done! )
  172. ( no match, go to prev and continue )
  173. HL PUSHqq, ( --> lvl 1 )
  174. DE DECss,
  175. DE DECss,
  176. DE DECss, ( prev field )
  177. DE PUSHqq, ( --> lvl 2 )
  178. EXDEHL,
  179. E (HL) LDrr,
  180. HL INCss,
  181. D (HL) LDrr,
  182. ( DE conains prev offset )
  183. HL POPqq, ( <-- lvl 2 )
  184. ( HL is prev field's addr. Is offset zero? )
  185. A D LDrr,
  186. E ORr,
  187. JRZ, L6 FWR ( noprev )
  188. ( get absolute addr from offset )
  189. ( carry cleared from "or e" )
  190. DE SBCHLss,
  191. EXDEHL, ( result in DE )
  192. L6 FSET ( noprev )
  193. HL POPqq, ( <-- lvl 1 )
  194. JRNZ, L3 BWR ( inner, try to match again )
  195. ( Z set? end of dict, unset Z )
  196. L1 FSET ( fail )
  197. A XORr,
  198. A INCr,
  199. L4 FSET ( end )
  200. HL POPqq,
  201. BC POPqq,
  202. RET,
  203. PC ORG @ 0x29 + ! ( flagsToBC )
  204. BC 0 LDddnn,
  205. CZ RETcc, ( equal )
  206. BC INCss,
  207. CM RETcc, ( > )
  208. ( < )
  209. BC DECss,
  210. BC DECss,
  211. RET,
  212. PC ORG @ 0x12 + ! ( pushRS )
  213. IX INCss,
  214. IX INCss,
  215. 0 IX+ L LDIXYr,
  216. 1 IX+ H LDIXYr,
  217. RET,
  218. PC ORG @ 0x15 + ! ( popRS )
  219. L 0 IX+ LDrIXY,
  220. H 1 IX+ LDrIXY,
  221. IX DECss,
  222. IX DECss,
  223. RET,
  224. '(' A, 'u' A, 'f' A, 'l' A, 'w' A, ')' A, 0 A,
  225. L1 BSET ( abortUnderflow )
  226. HL PC 7 - LDddnn,
  227. 0x03 CALLnn, ( find )
  228. DE PUSHqq,
  229. L2 @ 2 + JPnn, ( EXECUTE, skip nativeWord )
  230. PC ORG @ 0x1e + ! ( chkPS )
  231. HL PUSHqq,
  232. RAMSTART LDHL(nn), ( RAM+00 == INITIAL_SP )
  233. ( We have the return address for this very call on the stack
  234. and protected registers. Let's compensate )
  235. HL DECss,
  236. HL DECss,
  237. HL DECss,
  238. HL DECss,
  239. A ORr, ( clear carry )
  240. SP SBCHLss,
  241. HL POPqq,
  242. CNC RETcc, ( INITIAL_SP >= SP? good )
  243. JR, L1 BWR ( abortUnderflow )
  244. L3 BSET ( chkRS )
  245. IX PUSHqq, HL POPqq,
  246. DE RS_ADDR LDddnn,
  247. A ORr, ( clear carry )
  248. DE SBCHLss,
  249. CNC RETcc, ( IX >= RS_ADDR? good )
  250. JR, L1 BWR ( abortUnderflow )
  251. PC ORG @ 0x1b + ! ( next )
  252. ( This routine is jumped to at the end of every word. In it,
  253. we jump to current IP, but we also take care of increasing
  254. it by 2 before jumping. )
  255. ( Before we continue: are stacks within bounds? )
  256. 0x1d CALLnn, ( chkPS )
  257. L3 @ CALLnn, ( chkRS )
  258. DE RAMSTART 0x06 + LDdd(nn), ( RAMSTART+0x06 == IP )
  259. H D LDrr,
  260. L E LDrr,
  261. DE INCss,
  262. DE INCss,
  263. RAMSTART 0x06 + DE LD(nn)dd, ( RAMSTART+0x06 == IP )
  264. ( HL is an atom list pointer. We need to go into it to
  265. have a wordref )
  266. E (HL) LDrr,
  267. HL INCss,
  268. D (HL) LDrr,
  269. DE PUSHqq,
  270. L2 @ 2 + JPnn, ( EXECUTE, skip nativeWord )
  271. ( WORD ROUTINES )
  272. PC ORG @ 0x0f + ! ( compiledWord )
  273. ( Execute a list of atoms, which always end with EXIT.
  274. IY points to that list. What do we do:
  275. 1. Push current IP to RS
  276. 2. Set new IP to the second atom of the list
  277. 3. Execute the first atom of the list. )
  278. RAMSTART 0x06 + LDHL(nn), ( RAMSTART+0x06 == IP )
  279. 0x11 CALLnn, ( 11 == pushRS )
  280. IY PUSHqq, HL POPqq,
  281. HL INCss,
  282. HL INCss,
  283. RAMSTART 0x06 + LD(nn)HL, ( RAMSTART+0x06 == IP )
  284. ( IY still is our atom reference )
  285. L 0 IY+ LDrIXY,
  286. H 1 IY+ LDrIXY,
  287. HL PUSHqq, ( arg for EXECUTE )
  288. L2 @ 2 + JPnn, ( EXECUTE, skip nativeWord )
  289. PC ORG @ 0x0c + ! ( cellWord )
  290. ( Pushes the PFA directly )
  291. IY PUSHqq,
  292. JPNEXT,
  293. PC ORG @ 0x2c + ! ( doesWord )
  294. ( The word was spawned from a definition word that has a
  295. DOES>. PFA+2 (right after the actual cell) is a link to the
  296. slot right after that DOES>. Therefore, what we need to do
  297. push the cell addr like a regular cell, then follow the
  298. linkfrom the PFA, and then continue as a regular
  299. compiledWord.
  300. )
  301. IY PUSHqq, ( like a regular cell )
  302. L 2 IY+ LDrIXY,
  303. H 3 IY+ LDrIXY,
  304. HL PUSHqq, IY POPqq,
  305. 0x0e JPnn, ( 0e == compiledWord )
  306. PC ORG @ 0x20 + ! ( numberWord )
  307. ( This is not a word, but a number literal. This works a bit
  308. differently than others: PF means nothing and the actual
  309. number is placed next to the numberWord reference in the
  310. compiled word list. What we need to do to fetch that number
  311. is to play with the IP.
  312. )
  313. RAMSTART 0x06 + LDHL(nn), ( RAMSTART+0x06 == IP )
  314. E (HL) LDrr,
  315. HL INCss,
  316. D (HL) LDrr,
  317. HL INCss,
  318. RAMSTART 0x06 + LD(nn)HL, ( RAMSTART+0x06 == IP )
  319. DE PUSHqq,
  320. JPNEXT,
  321. PC ORG @ 0x22 + ! ( litWord )
  322. ( Similarly to numberWord, this is not a real word, but a
  323. string literal. Instead of being followed by a 2 bytes
  324. number, it's followed by a null-terminated string. When
  325. called, puts the string's address on PS )
  326. RAMSTART 0x06 + LDHL(nn), ( RAMSTART+0x06 == IP )
  327. HL PUSHqq,
  328. ( skip to null char )
  329. A XORr, ( look for null )
  330. B A LDrr,
  331. C A LDrr,
  332. CPIR,
  333. ( CPIR advances HL regardless of comparison, so goes one
  334. char after NULL. This is good, because that's what we
  335. want... )
  336. RAMSTART 0x06 + LD(nn)HL, ( RAMSTART+0x06 == IP )
  337. JPNEXT,
  338. ( filler )
  339. NOP, NOP, NOP, NOP, NOP, NOP,
  340. ( DICT HOOK )
  341. ( This dummy dictionary entry serves two purposes:
  342. 1. Allow binary grafting. Because each binary dict always
  343. end with a dummy entry, we always have a predictable
  344. prev offset for the grafter's first entry.
  345. 2. Tell icore's "_c" routine where the boot binary ends.
  346. See comment there.
  347. )
  348. '_' A, 'b' A, 'e' A, 'n' A, 'd' A,
  349. PC L2 @ - A,, ( prev )
  350. 5 A,
  351. H@ 256 /MOD 2 PC! 2 PC!