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.

290 lines
11KB

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