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

288 рядки
11KB

  1. # Dictionary
  2. List of words defined in arch-specific boot code (for example,
  3. B280 for Z80), Core words (B350) and Extra words (B150).
  4. # Glossary
  5. Stack notation: "<stack before> -- <stack after>". Rightmost is
  6. top of stack (TOS). For example, in "a b -- c d", b is TOS
  7. before, d is TOS after. "R:" means that the Return Stack is
  8. modified.
  9. Word references (wordref): When we say we have a "word
  10. reference", it's a pointer to a word's *entry type field*. For
  11. example, the address that "' DUP" puts on the stack is a
  12. wordref, that is, a reference to the entry type field of the
  13. word DUP. See impl.txt for details.
  14. PF: Parameter field. The area following the entry type field of
  15. a word. For example, "' H@ 1+" points to the PF of the word H@.
  16. Words between "()" are "support words" that aren't really meant
  17. to be used directly, but as part of another word.
  18. "*I*" in description indicates an IMMEDIATE word.
  19. # Symbols
  20. Throughout words, different symbols are used in different
  21. contexts, but we try to been consistent in their use. Here's
  22. their definitions:
  23. ! - Store
  24. @ - Fetch
  25. $ - Initialize
  26. ^ - Arguments in their opposite order
  27. < - Input
  28. > - 1. Pointer in a buffer 2. Opposite of "<".
  29. ( - Lower boundary
  30. ) - Upper boundary
  31. * - Word indirection (pointer to word)
  32. ? - Is it ...? (example: IMMED?)
  33. # Entry management
  34. '? x -- a f Find x it in dict. If found, f=1 and
  35. a = wordref. If not found, f=0 and
  36. a = string addr.
  37. ' x -- a Push addr of word x to a. If not found,
  38. aborts.
  39. ['] x -- *I* Like "'", but spits the addr as a number
  40. literal. If not found, aborts.
  41. , n -- Write n in HERE and advance it.
  42. ALLOT n -- Move HERE by n bytes
  43. C, b -- Write byte b in HERE and advance it.
  44. C,* b -- Indirect C,
  45. FIND w -- a f Like '?, but for w.
  46. EMPTY -- Rewind HERE and CURRENT where they were at
  47. system initialization.
  48. FORGET x -- Rewind the dictionary (both CURRENT and HERE)
  49. up to x's previous entry.
  50. PREV a -- a Return a wordref's previous entry.
  51. WORD( a -- a Get wordref's beginning addr.
  52. # Defining words
  53. : x ... ; -- Define a new word
  54. :* x a -- Define a new alias
  55. :** x a -- Define a new ialias
  56. CREATE x -- Create cell named x. Doesn't allocate a PF.
  57. [COMPILE] x -- *I* Compile word x and write it to HERE.
  58. IMMEDIATE words are *not* executed.
  59. COMPILE x -- *I* Meta compiles: write wordrefs that will
  60. compile x when executed.
  61. CONSTANT x n -- Creates cell x that when called pushes its
  62. value.
  63. DOES> -- See primer.txt
  64. IMMED? a -- f Checks whether wordref at a is immediate.
  65. IMMEDIATE -- Flag the latest defined word as immediate.
  66. LITN n -- Write number n as a literal.
  67. VARIABLE c -- Creates cell x with 2 bytes allocation.
  68. # Flow
  69. Note that flow words can only be used in definitions. In the
  70. INTERPRET loop, they don't have the desired effect because each
  71. word from the input stream is executed immediately. In this
  72. context, branching doesn't work.
  73. f IF A ELSE B THEN: if f is true, execute A, if false, execute
  74. B. ELSE is optional.
  75. [IF] .. [THEN]: Meta-IF. Works outside definitions. No [ELSE].
  76. BEGIN .. f UNTIL: if f is false, branch to BEGIN.
  77. BEGIN .. AGAIN: Always branch to BEGIN.
  78. x y DO .. LOOP: LOOP increments y. if y != x, branch to DO.
  79. x CASE y OF A ENDOF z OF B ENDOF C ENDCASE: If x == y, execute
  80. A, if x == z, execute B. Otherwise, execute C. x is dropped
  81. in case of an OF match, *but it is kept if it reaches C*. You
  82. have to consume it to avoid PSP leak.
  83. (br) -- Branches by the number specified in the 2
  84. following bytes. Can be negative.
  85. (?br) f -- Branch if f is false.
  86. ( -- *I* Comment. Ignore input until ")" is read.
  87. [ -- *I* Begin interpretative mode. In a definition,
  88. execute words instead of compiling them.
  89. ] -- End interpretative mode.
  90. ABORT -- Resets PS and RS and returns to interpreter.
  91. ABORT" x" -- *I* Compiles a ." followed by a ABORT.
  92. ERR a -- Prints a and ABORT. Defined early and used by
  93. drivers.
  94. EXECUTE a -- Execute wordref at addr a
  95. INTERPRET -- Main interpret loop.
  96. LEAVE -- In a DO..LOOP, exit at the next LOOP call.
  97. QUIT -- Return to interpreter prompt immediately.
  98. # Parameter Stack
  99. DROP a --
  100. DUP a -- a a
  101. ?DUP DUP if a is nonzero
  102. NIP a b -- b
  103. OVER a b -- a b a
  104. ROT a b c -- b c a
  105. ROT> a b c -- c a b
  106. SWAP a b -- b a
  107. TUCK a b -- b a b
  108. 2DROP a a --
  109. 2DUP a b -- a b a b
  110. 2OVER a b c d -- a b c d a b
  111. 2SWAP a b c d -- c d a b
  112. 'S Returns current stack pointer, not counting the
  113. push it's making right now.
  114. S0 Returns address of PSP TOS. When PSP is empty,
  115. 'S == S0
  116. PICK Pick nth item from stack. "0 PICK" = DUP,
  117. "1 PICK" = OVER.
  118. ROLL Rotate PSP over n items. "1 ROLL" = SWAP,
  119. "2 ROLL" = ROT. 0 is noop.
  120. # Return Stack
  121. >R n -- R:n Pops PS and push to RS
  122. 2>R x y -- R:x y Equivalent to SWAP >R >R
  123. R> R:n -- n Pops RS and push to PS
  124. 2R> R:x y -- x y Equivalent to R> R> SWAP
  125. I -- n Copy RS TOS to PS
  126. I' -- n Copy RS second item to PS
  127. J -- n Copy RS third item to PS
  128. # Memory
  129. @ a -- n Set n to value at address a
  130. ! n a -- Store n in address a
  131. ? a -- Print value of addr a
  132. +! n a -- Increase value of addr a by n
  133. C@ a -- c Set c to byte at address a
  134. C@* a -- c Indirect C@
  135. C@+ a -- a+1 c Fetch c from a and inc a.
  136. C@- a -- a-1 c Fetch c from a and dec a.
  137. C! c a -- Store byte c in address a
  138. C!* c a -- Indirect C!
  139. C!+ c a -- a+1 Store byte c in a and inc a.
  140. C!- c a -- a-1 Store byte c in a and dec a.
  141. *! a al -- Change alias al's addr to a.
  142. **! a sw -- Change ialias sw's addr to a.
  143. CURRENT -- a Set a to wordref of last added entry.
  144. CURRENT* -- a A pointer to active CURRENT*. Useful
  145. when we have multiple active dicts.
  146. FILL a n b -- Fill n bytes at addr a with val b.
  147. HERE -- a Push HERE's address
  148. H@ -- a HERE @
  149. MOVE a1 a2 u -- Copy u bytes from a1 to a2, starting
  150. with a1, going up.
  151. MOVE- a1 a2 u -- Copy u bytes from a1 to a2, starting
  152. with a1+u, going down.
  153. MOVE, a u -- Copy u bytes from a to HERE.
  154. MOVEW src dst u -- Same as MOVE, but with words
  155. Important note: MOVE* use C@* and C!* instead of C@ and C! See
  156. "Indirect memory access" in usage.txt.
  157. MOVEW notes: this word's purpose is to interface with word-
  158. based systems. src and dst are addressed as *bytes* but u is a
  159. *word* count. Every iteration increases src and dst by 2. This
  160. shouldn't be used on regular memory, it will yield weird
  161. results. Use it with C!* ialias pointing to a word-based target.
  162. # Arithmetic / Bits
  163. + a b -- c a + b -> c
  164. - a b -- c a - b -> c
  165. -^ a b -- c b - a -> c
  166. * a b -- c a * b -> c
  167. / a b -- c a / b -> c
  168. |L n -- msb lsb Split n word in 2 bytes, LSB on TOS
  169. |M n -- lsb msb Split n word in 2 bytes, MSB on TOS
  170. MOD a b -- c a % b -> c
  171. /MOD a b -- r q r:remainder q:quotient
  172. AND a b -- c a & b -> c
  173. OR a b -- c a | b -> c
  174. XOR a b -- c a ^ b -> c
  175. LSHIFT a u -- c a << u -> c
  176. RSHIFT a u -- c a >> u -> c
  177. Shortcuts: 1+ 2+ 1- 2-
  178. # Logic
  179. = n1 n2 -- f Push true if n1 == n2
  180. < n1 n2 -- f Push true if n1 < n2
  181. > n1 n2 -- f Push true if n1 > n2
  182. >< n l h -- f Push true if l < n < h
  183. =><= n l h -- f Push true if l <= n <= h
  184. CMP n1 n2 -- n Compare n1 and n2 and set n to -1, 0, or 1.
  185. n=0: a1=a2. n=1: a1>a2. n=-1: a1<a2.
  186. NOT f -- f Push the logical opposite of f
  187. # Strings
  188. Strings in Collapse OS begin with a one byte length, followed
  189. by the contents of the string.
  190. LIT" x" -- Read following characters and write to HERE
  191. as a string literal.
  192. S= a1 a2 -- f Returns whether string a1 == a2.
  193. # I/O
  194. (parse) a -- n Parses string at a as a number and push the
  195. result in n as well as whether parsing was a
  196. success in f (false = failure, true =
  197. success)
  198. STYPE a -- EMIT all chars of string at at addr a.
  199. . n -- Print n in its decimal form
  200. .x n -- Print n's LSB in hex form. Always 2
  201. characters.
  202. .X n -- Print n in hex form. Always 4 characters.
  203. Numbers are never considered negative.
  204. "-1 .X" --> ffff
  205. ," xxx" -- Write xxx to HERE
  206. ." xxx" -- *I* Compiles string literal xxx followed by a
  207. call to STYPE.
  208. C<? -- f Returns whether there's a char waiting in buf.
  209. C< -- c Read one char from buffered input.
  210. EMIT c -- Spit char c to output stream
  211. IN> -- a Address of variable containing current pos in
  212. input buffer.
  213. KEY -- c Get char c from direct input
  214. PC! c a -- Spit c to port a
  215. PC@ a -- c Fetch c from port a
  216. WORD -- a Read one word from buffered input and push its
  217. addr. Always null terminated. If ASCII EOT is
  218. encountered, a will point to it (it is cons-
  219. idered a word).
  220. There are also ascii const emitters:
  221. BS CR LF SPC CRLF
  222. NL is an indirect word (see SYSVARS in impl.txt) that aliases to
  223. CRLF by default and that should generally be used when we want
  224. to emit a newline.
  225. # Disk
  226. BLK> -- a Address of the current block variable.
  227. BLK( -- a Beginning addr of blk buf.
  228. BLK) -- a Ending addr of blk buf.
  229. COPY s d -- Copy contents of s block to d block.
  230. FLUSH -- Write current block to disk if dirty.
  231. LIST n -- Prints the contents of the block n on screen
  232. in the form of 16 lines of 64 columns.
  233. LOAD n -- Interprets Forth code from block n
  234. LOAD+ n -- Relative load. Loads active block + n.
  235. LOADR n1 n2 -- Load block range between n1 and n2, inclusive.
  236. LOADR+ n1 n2 -- Relative ranged load.
  237. WIPE -- Empties current block
  238. WIPED? -- f Whether current block is empty
  239. # Other
  240. DUMP n a -- Prints n bytes at addr a in a hexdump format.
  241. Prints in chunks of 8 bytes. Doesn't do partial
  242. lines. Output is designed to fit in 32 columns.
  243. TICKS n -- Wait for approximately 0.1 millisecond. Don't
  244. use with n=0.