Mirror of CollapseOS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

298 lines
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. Some words have a variable stack signature, most often in pair
  10. with a flag. These are indicated with "?" to tell that the argu-
  11. ment might not be there. For example, "-- n? f" means that "n"
  12. might or might not be there.
  13. Word references (wordref): When we say we have a "word
  14. reference", it's a pointer to a word's *entry type field*. For
  15. example, the address that "' DUP" puts on the stack is a
  16. wordref, that is, a reference to the entry type field of the
  17. word DUP. See impl.txt for details.
  18. PF: Parameter field. The area following the entry type field of
  19. a word. For example, "' H@ 1+" points to the PF of the word H@.
  20. Words between "()" are "support words" that aren't really meant
  21. to be used directly, but as part of another word.
  22. "*I*" in description indicates an IMMEDIATE word.
  23. # Symbols
  24. Throughout words, different symbols are used in different
  25. contexts, but we try to been consistent in their use. Here's
  26. their definitions:
  27. ! - Store
  28. @ - Fetch
  29. $ - Initialize
  30. ^ - Arguments in their opposite order
  31. < - Input
  32. > - 1. Pointer in a buffer 2. Opposite of "<".
  33. ( - Lower boundary
  34. ) - Upper boundary
  35. * - Word indirection (pointer to word)
  36. ? - Is it ...? (example: IMMED?)
  37. # Entry management
  38. '? x -- a f Find x it in dict. If found, f=1 and
  39. a = wordref. If not found, f=0 and
  40. a = string addr.
  41. ' x -- a Push addr of word x to a. If not found,
  42. aborts.
  43. ['] x -- *I* Like "'", but spits the addr as a number
  44. literal. If not found, aborts.
  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. , n -- Write n in HERE and advance it.
  132. ? a -- Print value of addr a
  133. +! n a -- Increase value of addr a by n
  134. ~C! a -- Set C! and ! overrides. See notes.
  135. C@ a -- c Set c to byte at address a
  136. C@+ a -- a+1 c Fetch c from a and inc a.
  137. C@- a -- a-1 c Fetch c from a and dec a.
  138. C! c a -- Store byte c in address a
  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. C, b -- Write byte b in HERE and advance it.
  142. *! a al -- Change alias al's addr to a.
  143. **! a sw -- Change ialias sw's addr to a.
  144. ALLOT n -- Move HERE by n bytes
  145. CURRENT -- a Set a to wordref of last added entry.
  146. CURRENT* -- a A pointer to active CURRENT*. Useful
  147. when we have multiple active dicts.
  148. FILL a n b -- Fill n bytes at addr a with val b.
  149. HERE -- a Push HERE's address
  150. H@ -- a HERE @
  151. MOVE a1 a2 u -- Copy u bytes from a1 to a2, starting
  152. with a1, going up.
  153. MOVE- a1 a2 u -- Copy u bytes from a1 to a2, starting
  154. with a1+u, going down.
  155. MOVE, a u -- Copy u bytes from a to HERE.
  156. Notes:
  157. ~C!: When supplied a non-zero address, sets the SYSVARS+3e (see
  158. impl.txt) override routine address. This should link dir-
  159. ectly to assembly code because we call this address. This
  160. routine shouldn't end with a call to next, but rather a
  161. regular assembly return. Registers used are arch-specific.
  162. When supplied 0, unsets override.
  163. # Arithmetic / Bits
  164. + a b -- c a + b -> c
  165. - a b -- c a - b -> c
  166. -^ a b -- c b - a -> c
  167. * a b -- c a * b -> c
  168. / a b -- c a / b -> c
  169. |L n -- msb lsb Split n word in 2 bytes, LSB on TOS
  170. |M n -- lsb msb Split n word in 2 bytes, MSB on TOS
  171. MOD a b -- c a % b -> c
  172. /MOD a b -- r q r:remainder q:quotient
  173. AND a b -- c a & b -> c
  174. OR a b -- c a | b -> c
  175. XOR a b -- c a ^ b -> c
  176. LSHIFT a u -- c a << u -> c
  177. RSHIFT a u -- c a >> u -> c
  178. Shortcuts: 1+ 2+ 1- 2-
  179. # Logic
  180. = n1 n2 -- f Push true if n1 == n2
  181. < n1 n2 -- f Push true if n1 < n2
  182. > n1 n2 -- f Push true if n1 > n2
  183. >< n l h -- f Push true if l < n < h
  184. =><= n l h -- f Push true if l <= n <= h
  185. CMP n1 n2 -- n Compare n1 and n2 and set n to -1, 0, or 1.
  186. n=0: a1=a2. n=1: a1>a2. n=-1: a1<a2.
  187. NOT f -- f Push the logical opposite of f
  188. # Strings
  189. Strings in Collapse OS begin with a one byte length, followed
  190. by the contents of the string.
  191. LIT" x" -- Read following characters and write to HERE
  192. as a string literal.
  193. S= a1 a2 -- f Returns whether string a1 == a2.
  194. # I/O
  195. (parse) a -- n Parses string at a as a number and push the
  196. result in n as well as whether parsing was a
  197. success in f (false = failure, true =
  198. success)
  199. STYPE a -- EMIT all chars of string at at addr a.
  200. . n -- Print n in its decimal form
  201. .x n -- Print n's LSB in hex form. Always 2
  202. characters.
  203. .X n -- Print n in hex form. Always 4 characters.
  204. Numbers are never considered negative.
  205. "-1 .X" --> ffff
  206. ," xxx" -- Write xxx to HERE
  207. ." xxx" -- *I* Compiles string literal xxx followed by a
  208. call to STYPE.
  209. C<? -- f Returns whether there's a char waiting in buf.
  210. C< -- c Read one char from buffered input.
  211. EMIT c -- Spit char c to output stream
  212. IN> -- a Address of variable containing current pos in
  213. input buffer.
  214. KEY? -- c? f Polls the keyboard for a key. If a key is
  215. pressed, f is true and c is the char. Other-
  216. wise, f is false and c is *not* on the stack.
  217. KEY -- c Get char c from direct input
  218. NL> -- Emit newline
  219. PC! c a -- Spit c to port a
  220. PC@ a -- c Fetch c from port a
  221. SPC> -- Emit space character
  222. WORD -- a Read one word from buffered input and push its
  223. addr. Always null terminated. If ASCII EOT is
  224. encountered, a will point to it (it is cons-
  225. idered a word).
  226. These ASCII consts are defined:
  227. BS CR LF SPC
  228. KEY? and EMIT are ialiases to (key?) and (emit) (see TTY proto-
  229. col in protocol.txt). KEY is a loop over KEY?.
  230. NL> spits CRLF by default, but can be configured to spit an
  231. alternate newline char. See impl.txt.
  232. # Disk
  233. BLK> -- a Address of the current block variable.
  234. BLK( -- a Beginning addr of blk buf.
  235. BLK) -- a Ending addr of blk buf.
  236. COPY s d -- Copy contents of s block to d block.
  237. FLUSH -- Write current block to disk if dirty and inval-
  238. idates current block cache.
  239. LIST n -- Prints the contents of the block n on screen
  240. in the form of 16 lines of 64 columns.
  241. LOAD n -- Interprets Forth code from block n
  242. LOAD+ n -- Relative load. Loads active block + n.
  243. LOADR n1 n2 -- Load block range between n1 and n2, inclusive.
  244. LOADR+ n1 n2 -- Relative ranged load.
  245. WIPE -- Empties current block
  246. WIPED? -- f Whether current block is empty
  247. # Other
  248. DUMP n a -- Prints n bytes at addr a in a hexdump format.
  249. Prints in chunks of 8 bytes. Doesn't do partial
  250. lines. Output is designed to fit in 32 columns.
  251. TICKS n -- Wait for approximately 0.1 millisecond. Don't
  252. use with n=0.