Mirror of CollapseOS
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

288 linhas
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. :* x a -- Define a new alias
  54. :** x a -- Define a new switch
  55. CREATE x -- Create cell named x. Doesn't allocate a PF.
  56. [COMPILE] x -- *I* Compile word x and write it to HERE.
  57. IMMEDIATE words are *not* executed.
  58. COMPILE x -- *I* Meta compiles: write wordrefs that will
  59. compile x when executed.
  60. CONSTANT x n -- Creates cell x that when called pushes its
  61. value.
  62. DOES> -- See primer.txt
  63. IMMED? a -- f Checks whether wordref at a is immediate.
  64. IMMEDIATE -- Flag the latest defined word as immediate.
  65. LITN n -- Write number n as a literal.
  66. VARIABLE c -- Creates cell x with 2 bytes allocation.
  67. # Flow
  68. Note that flow words can only be used in definitions. In the
  69. INTERPRET loop, they don't have the desired effect because each
  70. word from the input stream is executed immediately. In this
  71. context, branching doesn't work.
  72. f IF A ELSE B THEN: if f is true, execute A, if false, execute
  73. B. ELSE is optional.
  74. [IF] .. [THEN]: Meta-IF. Works outside definitions. No [ELSE].
  75. BEGIN .. f UNTIL: if f is false, branch to BEGIN.
  76. BEGIN .. AGAIN: Always branch to BEGIN.
  77. x y DO .. LOOP: LOOP increments y. if y != x, branch to DO.
  78. x CASE y OF A ENDOF z OF B ENDOF C ENDCASE: If x == y, execute
  79. A, if x == z, execute B. Otherwise, execute C. x is dropped
  80. in case of an OF match, *but it is kept if it reaches C*. You
  81. have to consume it to avoid PSP leak.
  82. (br) -- Branches by the number specified in the 2
  83. following bytes. Can be negative.
  84. (?br) f -- Branch if f is false.
  85. ( -- *I* Comment. Ignore input until ")" is read.
  86. [ -- *I* Begin interpretative mode. In a definition,
  87. execute words instead of compiling them.
  88. ] -- End interpretative mode.
  89. ABORT -- Resets PS and RS and returns to interpreter.
  90. ABORT" x" -- *I* Compiles a ." followed by a ABORT.
  91. ERR a -- Prints a and ABORT. Defined early and used by
  92. drivers.
  93. EXECUTE a -- Execute wordref at addr a
  94. INTERPRET -- Main interpret loop.
  95. LEAVE -- In a DO..LOOP, exit at the next LOOP call.
  96. QUIT -- Return to interpreter prompt immediately.
  97. # Parameter Stack
  98. DROP a --
  99. DUP a -- a a
  100. ?DUP DUP if a is nonzero
  101. NIP a b -- b
  102. OVER a b -- a b a
  103. ROT a b c -- b c a
  104. SWAP a b -- b a
  105. TUCK a b -- b a b
  106. 2DROP a a --
  107. 2DUP a b -- a b a b
  108. 2OVER a b c d -- a b c d a b
  109. 2SWAP a b c d -- c d a b
  110. 'S Returns current stack pointer, not counting the
  111. push it's making right now.
  112. S0 Returns address of PSP TOS. When PSP is empty,
  113. 'S == S0
  114. PICK Pick nth item from stack. "0 PICK" = DUP,
  115. "1 PICK" = OVER.
  116. ROLL Rotate PSP over n items. "1 ROLL" = SWAP,
  117. "2 ROLL" = ROT. 0 is noop.
  118. # Return Stack
  119. >R n -- R:n Pops PS and push to RS
  120. 2>R x y -- R:x y Equivalent to SWAP >R >R
  121. R> R:n -- n Pops RS and push to PS
  122. 2R> R:x y -- x y Equivalent to R> R> SWAP
  123. I -- n Copy RS TOS to PS
  124. I' -- n Copy RS second item to PS
  125. J -- n Copy RS third item to PS
  126. # Memory
  127. @ a -- n Set n to value at address a
  128. ! n a -- Store n in address a
  129. ? a -- Print value of addr a
  130. +! n a -- Increase value of addr a by n
  131. C@ a -- c Set c to byte at address a
  132. C@+ a -- a+1 c Fetch c from a and inc a.
  133. C@- a -- a-1 c Fetch c from a and dec a.
  134. C! c a -- Store byte c in address a
  135. C!+ c a -- a+1 Store byte c in a and inc a.
  136. C!- c a -- a-1 Store byte c in a and dec a.
  137. *! a al -- Change alias al's addr to a.
  138. **! a sw -- Change switch sw's addr to a.
  139. CURRENT -- a Set a to wordref of last added entry.
  140. CURRENT* -- a A pointer to active CURRENT*. Useful
  141. when we have multiple active dicts.
  142. FILL a n b -- Fill n bytes at addr a with val b.
  143. HERE -- a Push HERE's address
  144. H@ -- a HERE @
  145. MOVE a1 a2 u -- Copy u bytes from a1 to a2, starting
  146. with a1, going up.
  147. MOVE- a1 a2 u -- Copy u bytes from a1 to a2, starting
  148. with a1+u, going down.
  149. MOVE, a u -- Copy u bytes from a to HERE.
  150. # Addressed devices
  151. ADEV$ -- Initialize adev subsystem
  152. A@ a -- c Indirect C@
  153. A! c a -- Indirect C!
  154. A@* -- a Address for A@ word
  155. A!* -- a Address for A! word
  156. AMOVE src dst u -- Same as MOVE, but with A@ and A!
  157. AMOVEW src dst u -- Same as AMOVE, but with words
  158. AMOVEW notes: this word's purpose is to interface with word-
  159. based systems. src and dst are addressed as *bytes* but u is a
  160. *word* count. Every iteration increases src and dst by 2. When
  161. you use it, be aware that default values for A!* and A@* are C!
  162. and C@. If you don't adjust before using AMOVEW, you will get
  163. weird results.
  164. # Arithmetic / Bits
  165. + a b -- c a + b -> c
  166. - a b -- c a - b -> c
  167. -^ a b -- c b - a -> c
  168. * a b -- c a * b -> c
  169. / a b -- c a / b -> c
  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. MIN a b -- n Returns the lowest of a and b
  187. MAX a b -- n Returns the highest of a and b
  188. NOT f -- f Push the logical opposite of f
  189. # Strings
  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. (print) a -- Print string at addr a. Stops at 0x0 or 0xd.
  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 (print).
  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.