Mirror of CollapseOS
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

339 рядки
9.2KB

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