Mirror of CollapseOS
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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