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.

README.md 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. # TRS-80 Model 4p
  2. The TRS-80 (models 1, 3 and 4) are among the most popular z80 machines. They're
  3. very nicely designed and I got my hands on a 4p with two floppy disk drives and
  4. a RS-232 port. In this recipe, we're going to get Collapse OS running on it.
  5. ![Collapse OS on a TRS-80 Model 4P](collapseos-on-trs80.jpg)
  6. ## Not entirely standalone
  7. Collapse OS uses the TRS-80 drivers rather than its own. On most TRS-80 models,
  8. those drivers are on ROM, but in the case of the 4P model, those drivers are on
  9. the TRSDOS disk (well, from what I understand, not all of it, but still, a big
  10. part of it).
  11. It would be preferable to develop drivers from scratch, but it represents a
  12. significant effort for a modest payout (because it's only actually useful when
  13. you want to use a 4P model that has no TRSDOS disk).
  14. Maybe those drivers will be developed later, but it's not a priority for now.
  15. ## Floppy or RS-232?
  16. There are many ways to get Collapse OS to run on it. One would involve writing
  17. it to a floppy. I bought myself old floppy drives for that purpose, but I happen
  18. to not have any functional computer with a floppy port on it. I still have the
  19. motherboard of my old pentium, but I don't seem to have a video card for it any
  20. more.
  21. Because my 4p has a RS-232 port and because I have equipment to do serial
  22. communication from modern machines (I didn't have a DB-9 to DB-25 adapter
  23. though, I had to buy one), I chose that route.
  24. ## Gathering parts
  25. * A TRS-80 model 4p with a RS-232 port
  26. * A TRSDOS 6.x disk
  27. * A means to do serial communication. In my case, that meant:
  28. * A USB-to-serial device
  29. * A null modem cable
  30. * A DB-9 gender changer
  31. * A DB-9 to DB-25 adapter
  32. ## Overview
  33. We need to send sizeable binary programs through the RS-232 port and then run
  34. it. The big challenge here is ensuring data integrity. Sure, serial
  35. communication has parity check, but it has no helpful way of dealing with
  36. parity errors. When parity check is enabled and that a parity error occurs, the
  37. byte is simply dropped on the receiving side. Also, a double bit error could be
  38. missed by those checks.
  39. What we'll do here is to ping back every received byte back and have the sender
  40. do the comparison and report mismatched data.
  41. Another problem is ASCII control characters. When those are sent across serial
  42. communication channels, all hell breaks lose. When sending binary data, those
  43. characters have to be avoided. We use `tools/ttysafe` for that.
  44. Does TRSDOS have a way to receive this binary inside these constraints? Not to
  45. my knowledge. As far as I know, the COMM program doesn't allow this.
  46. What are we going to do? We're going to punch in a binary program to handle that
  47. kind of reception! You're gonna feel real badass about it too...
  48. ## Keyboard tips
  49. * `_` is `CLEAR+ENTER`.
  50. ## Building the binary
  51. You can build the binary to send to the TRS-80 with `make`, which will yield
  52. `os.bin`. You'll need it later.
  53. ## Testing serial communication
  54. The first step here is ensuring that you have bi-directional serial
  55. communication. To do this, first prepare your TRS-80:
  56. set *cl to com
  57. setcomm (word=8,parity=no)
  58. The first line loads the communication driver from the `COM/DRV` file on the
  59. TRSDOS disk and binds it to `*cl`, the name generally used for serial
  60. communication devices. The second line sets communication parameters in line
  61. with what is generally the default on modern machine. Note that I left the
  62. default of 300 bauds as-is.
  63. Then, you can run `COMM *cl` to start a serial communication console.
  64. Then, on the modern side, use your favorite serial communication program and set
  65. the tty to 300 baud with option "raw". Make sure you have `-parenb`.
  66. If your line is good, then what you type on either side should echo on the
  67. other side. If it does not, something's wrong. Debug.
  68. ## Punching in the goodie
  69. As stated in the overview, we need a program on the TRS-80 that:
  70. 1. Listens to `*cl`
  71. 2. Echoes each character back to `*cl`
  72. 3. Adjusts `ttysafe` escapes
  73. 4. Stores received bytes in memory
  74. You're in luck: that program has already been written. It's in B502 and B503.
  75. You can compile it with:
  76. 212 LOAD ( z80 assembler )
  77. 0x0238 CONSTANT COM_DRV_ADDR
  78. 0x3000 CONSTANT DEST_ADDR
  79. 502 LOAD
  80. 503 LOAD
  81. Then, you can use `DUMP` to visualize the data you'll need to punch in:
  82. H@ ORG @ - ORG @ DUMP
  83. It can run from any offset (all jumps in it are relative), but writes to
  84. `DEST_ADDR`. Make sure you don't place it in a way to be overwritten by its
  85. received data.
  86. Wondering what is that `COM_DRV_ADDR` constant? That's the DCB handle of your
  87. `*cl` device. You will need to get that address before you continue. Go read
  88. the following section and come back here.
  89. How will you punch that in? The `debug` program! This very useful piece of
  90. software is supplied in TRSDOS. To invoke it, first run `debug (on)` and then
  91. press the `BREAK` key. You'll get the debug interface which allows you to punch
  92. in any data in any memory address. Let's use `0x5000` which is the offset it's
  93. designed for.
  94. For reference: to go back to the TRSDOS prompt, it's `o<return>`.
  95. First, display the `0x5000-0x503f` range with the `d5000<space>` command (I
  96. always press Enter by mistake, but it's space you need to press). Then, you can
  97. begin punching in with `h5000<space>`. This will bring up a visual indicator of
  98. the address being edited. Punch in the stuff with a space in between each byte
  99. and end the edit session with `x`.
  100. ## Getting your DCB address
  101. In the previous step, you need to set `COM_DRV_ADDR` to your "DCB" address for
  102. `*cl`. That address is your driver "handle". To get it, first get the address
  103. where the driver is loaded in memory. You can get this by running `device
  104. (b=y)`. That address you see next to `*cl`? that's it. But that's not our DCB.
  105. To get your DBC, go explore that memory area. Right after the part where there's
  106. the `*cl` string, there's the DCB address (little endian). On my setup, the
  107. driver was loaded in `0x0ff4` and the DCB address was 8 bytes after that, with
  108. a value of `0x0238`. Don't forget that z80 is little endian. `38` will come
  109. before `02`.
  110. ## Saving that program for later
  111. If you want to save yourself typing for later sessions, why not save the
  112. program you've painfully typed to disk? TRSDOS enables that easily. Let's say
  113. that you typed your program at `0x5000` and that you want to save it to
  114. `RECV/CMD` on your second floppy drive, you'd do:
  115. dump recv/cmd:1 (start=x'5000',end=x'5030',tra='5000')
  116. A memory range dumped this way will be re-loaded at the same offset through
  117. `load recv/cmd:1`. Even better, `TRA` indicates when to jump after load when
  118. using the `RUN` command. Therefore, you can avoid all this work above in later
  119. sessions by simply typing `recv` in the DOS prompt.
  120. Note that you might want to turn `debug` off for these commands to run. I'm not
  121. sure why, but when the debugger is on, launching the command triggers the
  122. debugger.
  123. ## Sending binary through the RS-232 port
  124. Once you're finished punching your program in memory, you can run it with
  125. `g5000<enter>` (not space). If you've saved it to disk, run `recv` instead.
  126. Because it's an infinite loop, your screen will freeze. You can start sending
  127. your data.
  128. To that end, there's the `tools/pingpong` program. It takes a device and a
  129. filename to send. Before you send the binary, make it go through
  130. `tools/ttysafe` first (which just takes input from stdin and spits tty-safe
  131. content to stdout):
  132. ./ttysafe < stage1.bin > stage1.ttysafe
  133. On OpenBSD, the invocation can look like:
  134. doas ./pingpong /dev/ttyU0 os.ttysafe
  135. You will be prompted for a key before the contents is sent. This is because on
  136. OpenBSD, TTY configuration is lost as soon as the TTY is closed, which means
  137. that you can't just run `stty` before running `pingpong`. So, what you'll do is,
  138. before you press your key, run `doas stty -f /dev/ttyU0 300 raw` and then press
  139. any key on the `pingpong` invocation.
  140. If everything goes well, the program will send your contents, verifying every
  141. byte echoed back, and then send a null char to indicate to the receiving end
  142. that it's finished sending. This will end the infinite loop on the TRS-80 side
  143. and return. That should bring you back to a refreshed debug display and you
  144. should see your sent content in memory, at the specified address (`0x3000` if
  145. you didn't change it).
  146. If there was no error during `pingpong`, the content should be exact.
  147. Nevertheless, I recommend that you manually validate a few bytes using TRSDOS
  148. debugger before carrying on.
  149. *debugging tip*: Sometimes, the communication channel can be a bit stubborn and
  150. always fail, as if some leftover data was consistently blocking the channel. It
  151. would cause a data mismatch at the very beginning of the process, all the time.
  152. What I do in these cases is start a `COMM *cl` session on one side and a screen
  153. session on the other, type a few characters, and try `pingpong` again.
  154. ## Saving to disk
  155. If everything went well, you could run Collapse OS with `g3000<return>`.
  156. But instead of doing that, why not save it to disk?
  157. dump cos/cmd:1 (start=x'5000',end=x'7000',tra='5000')
  158. And there we go! A Collapse OS launchable from floppy!
  159. ## Sending blkfs to floppy
  160. As it is, your system fully supports reading and writing to floppy drive 1. It
  161. also had `*CL<` to read a char from `*cl` and `*CL>` to emit a char to `*cl`.
  162. That's all you need to have a full Collapse OS with access to disk blocks.
  163. First, make sure your floppies are formatted. Collapse OS is currently
  164. hardcoded to single side and single density, which means there's a limit of 100
  165. blocks per disk.
  166. You'll need to send those blocks through RS-232. Begin by taking over the
  167. prompt:
  168. ' *CL> 0x53 RAM+ !
  169. ' *CL< 0x55 RAM+ !
  170. See B80 for details about those RAM offsets. Your serial link now has the
  171. prompt. You will also have to make your newlines CRLF. The TRS-80 wants CR
  172. only, but serial communications (and `blkup`) expect CRLF:
  173. ' CRLF 0x0a RAM+ !
  174. Now, you can use `/tools/blkup` to send a disk's contents. First,
  175. extract the first 100 blocks from blkfs:
  176. dd if=emul/blkfs bs=1024 count=100 > d1
  177. Now, insert your formatted disk in drive 1 and push your blocks:
  178. tools/blkup /dev/ttyUSB0 0 d1
  179. It takes a while, but you will end up having your first 100 blocks on floppy!
  180. Go ahead, `LIST` around. Then, repeat for other disks.
  181. ## Floppy organisation
  182. Making blkfs span multiple disk is a bit problematic with regards to absolute
  183. block references in the code. You'll need to work a bit to design your very
  184. own Collapse OS floppy set. See Usage guide (B3) for details.
  185. ## Coming back to keyboard
  186. Once you're done, you will want to go back to local control:
  187. ' CR 0x0a RAM+ !
  188. 0 0x55 RAM+ !
  189. 0 0x53 RAM+ !
  190. ## Self-hosting
  191. As it is, your installment of Collapse OS is self-hosting using instructions
  192. very similar to `recipes/rc2014/selhost`. The difference is that instead of
  193. writing the binary you have in memory to EEPROM, you'll quit to TRSDOS with
  194. `BYE` and use TRSDOS' `DUMP` utility to save to disk like you already did
  195. before.