Mirror of CollapseOS
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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