Mirror of CollapseOS
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
5.7KB

  1. # Collapse OS usage guide
  2. If you already know Forth, start here. Otherwise, read primer
  3. first.
  4. We begin with a few oddities in Collapse OS compared to tradi-
  5. tional forths, then cover higher level operations.
  6. # Signed-ness
  7. For simplicity purposes, numbers are generally considered
  8. unsigned. For convenience, decimal parsing and formatting
  9. support the "-" prefix, but under the hood, it's all unsigned.
  10. This leads to some oddities. For example, "-1 0 <" is false.
  11. To compare whether something is negative, use the "0<" word
  12. which is the equivalent to "0x7fff >".
  13. # Branching
  14. Branching in Collapse OS is limited to 8-bit. This represents
  15. 64 word references forward or backward. While this might seem
  16. a bit tight at first, having this limit saves us a non-
  17. negligible amount of resource usage.
  18. The reasoning behind this intentional limit is that huge
  19. branches are generally an indicator that a logic ought to be
  20. simplified. So here's one more constraint for you to help you
  21. towards simplicity.
  22. # Interpreter I/O
  23. The INTERPRET loop, the heart of Collapse OS, feeds itself
  24. from the C< word, which yields a character every time it is
  25. called. If no character is available to interpret, it blocks.
  26. During normal operations, C< is simply a buffered layer over
  27. KEY, which has the same behavior (but unbuffered). Before
  28. yielding any character, the C< routine fetches a whole line
  29. from KEY, puts it in a buffer, then yields the buffered line,
  30. one character at a time.
  31. Both C< and KEY can be overridden by setting an alternate
  32. routine at the proper RAM offset (see impl.txt). For example,
  33. C< overrides are used during LOAD so that input comes from disk
  34. blocks instead of keyboard.
  35. KEY overrides can be used to, for example, temporarily give
  36. prompt control to a RS-232 device instead of the keyboard.
  37. Interpreter output is unbuffered and only has EMIT. This
  38. word can also be overriden, mostly as a companion to the
  39. raison d'etre of your KEY override.
  40. # Aliases and Switches
  41. A common pattern in Forth is to add an indirection layer with
  42. a pointer word. For example, if you have a word "FOO" for
  43. which you would like to add an indirection layer, you would
  44. rename "FOO" to "_FOO", add a variable "FOO*" pointing to
  45. "_FOO" and re-defining "FOO" as ": FOO FOO* @ EXECUTE".
  46. This is all well and good, but it is resource intensive and
  47. verbose, which make us want to avoid this pattern for words
  48. that are often used.
  49. For this purpose, Collapse OS has two special word types:
  50. alias and switches.
  51. An alias is a variable that contains a pointer to another word.
  52. When invoked, we invoke the specified pointer with minimal over-
  53. head. Using our FOO example above, we would create an alias
  54. with "' _FOO :* FOO". Invoking FOO will then invoke "_FOO". You
  55. can change the alias' pointer with "*!" like this:
  56. "' BAR ' FOO *!". FOO now invokes BAR.
  57. A switch is like an alias, but with a second level of indi-
  58. rection. The variable points to a cell pointing to our word.
  59. It works like an alias, except you have to use ":**" and "**!".
  60. Switches are used by core code which point to hardcoded
  61. addresses in RAM (because the core code is designed to run from
  62. ROM, we can't have regular variables). You are unlikely to
  63. need switches in regular code.
  64. # Addressed devices
  65. A@ and A! are the indirect versions of C@ and C!. They are
  66. aliases and initially point to C@ and C!. There is also a AMOVE
  67. word that is the same as MOVE but using A@ and A!.
  68. Addressed device words can be useful to "pipe" processing to
  69. places outside of regular memory.
  70. # Disk blocks
  71. Disk blocks are Collapse OS' main access to permanent storage.
  72. The system is exceedingly simple: blocks are contiguous
  73. chunks of 1024 bytes each living on some permanent media such
  74. as floppy disks or SD cards. They are mostly used for text,
  75. either informational or source code, which is organized into
  76. 16 lines of 64 characters each.
  77. Blocks are referred to by number, 0-indexed. They are read
  78. through BLK@ and written through BLK!. When a block is read,
  79. its 1024 bytes content is copied to an in-memory buffer
  80. starting at BLK( and ending at BLK). Those read/write
  81. operations are often implicit. For example, LIST calls BLK@.
  82. When a word modifies the buffer, it sets the buffer as dirty
  83. by calling BLK!!. BLK@ checks, before it reads its buffer,
  84. whether the current buffer is dirty and implicitly calls BLK!
  85. when it is.
  86. The index of the block currently in memory is kept in BLK>.
  87. Many blocks contain code. That code can be interpreted through
  88. LOAD. Programs stored in blocks frequently have "loader blocks"
  89. that take care of loading all blocks relevant to the program.
  90. Blocks spanning multiple disks are tricky. If your media isn't
  91. large enough to hold all Collapse OS blocks in one unit, you'll
  92. have to make it span multiple disks. Block reference in
  93. informational texts aren't a problem: When you swap your disk,
  94. you mentally adjust the block number you fetch.
  95. However, absolute LOAD operations in Collapse OS aren't aware
  96. of disk spanning and will not work properly in your spanned
  97. system.
  98. Although the usage of absolute LOAD calls are minimally used
  99. (relative LOADs are preferred), they are sometimes unavoidable.
  100. When you span Collapse OS over multiple disks, don't forget to
  101. adjust those absolute LOADs.
  102. # How blocks are organized
  103. Organization of contiguous blocks is an ongoing challenge and
  104. Collapse OS' blocks are never as tidy as they should, but we
  105. try to strive towards a few goals:
  106. 1. Block 0 contains documentation discovery core keys to the
  107. uninitiated.
  108. 2. B1-B4 are for a master index of blocks.
  109. 3. B5-B259 are for programs loaded at runtime.
  110. 4. B260-B599 are for bootstrapping a new core.
  111. 5. B600-B650 are for recipes.
  112. Recipes blocks do not live in the main blkfs, but each recipe
  113. has its own blkfs overlay, with blocks beginning at 600.