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.

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