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.

blockdev.md 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # Using block devices
  2. The `blockdev.asm` part manage what we call "block devices", an abstraction over
  3. something that we can read a byte to, write a byte to, optionally at arbitrary
  4. offsets.
  5. A Collapse OS system can define up to `0xff` devices. Those definitions are made
  6. in the glue code, so they are static.
  7. Definition of block devices happen at include time. It would look like:
  8. [...]
  9. BLOCKDEV_COUNT .equ 1
  10. #include "blockdev.asm"
  11. ; List of devices
  12. .dw aciaGetC, aciaPutC
  13. [...]
  14. That tells `blockdev` that we're going to set up one device, that its GetC and
  15. PutC are the ones defined by `acia.asm`.
  16. If your block device is read-only or write-only, use dummy routines. `unsetZ`
  17. is a good choice since it will return with the `Z` flag unset, indicating an
  18. error (dummy methods aren't supposed to be called).
  19. Each defined block device, in addition to its routine definition, holds a
  20. seek pointer. This seek pointer is used in shell commands described below.
  21. ## Routine definitions
  22. Parts that implement GetC and PutC do so in a loosely-coupled manner, but
  23. they should try to adhere to the convention, that is:
  24. **GetC**: Get the character at position specified by `HL`. If it supports 32-bit
  25. addressing, `DE` contains the high-order bytes. Return the result in
  26. `A`. If there's an error (for example, address out of range), unset
  27. `Z`. This routine is not expected to block. We expect the result to be
  28. immediate.
  29. **PutC**: The opposite of GetC. Write the character in `A` at specified
  30. position. `Z` unset on error.
  31. ## Shell usage
  32. `blockdev.asm` supplies 4 shell commands that you can graft to your shell thus:
  33. [...]
  34. SHELL_EXTRA_CMD_COUNT .equ 4
  35. #include "shell.asm"
  36. ; extra commands
  37. .dw blkBselCmd, blkSeekCmd, blkLoadCmd, blkSaveCmd
  38. [...]
  39. ### bsel
  40. `bsel` select the active block device. This specify a target for `load` and
  41. `save`. Some applications also use the active blockdev. It receives one
  42. argument, the device index. `bsel 0` selects the first defined device, `bsel 1`,
  43. the second, etc. Error `0x04` when argument is out of bounds.
  44. ### seek
  45. `seek` receives one word argument and sets the pointer for the currently active
  46. device to the specified address. Example: `seek 1234`.
  47. The device position is device-specific: if you seek on a device, then switch
  48. to another device and seek again, your previous position isn't lost. You will
  49. still be on the same position when you come back.
  50. ### load
  51. `load` works a bit like `poke` except that it reads its data from the currently
  52. active blockdev at its current position. If it hits the end of the blockdev
  53. before it could load its specified number of bytes, it stops. It only raises an
  54. error if it couldn't load any byte.
  55. It moves the device's position to the byte after the last loaded byte.
  56. ### save
  57. `save` is the opposite of `load`. It writes the specified number of bytes from
  58. memory to the active blockdev at its current position.
  59. It moves the device's position to the byte after the last written byte.
  60. ### Example
  61. Let's try an example: You glue yourself a Collapse OS with ACIA as its first
  62. device and a mmap starting at `0xd000` as your second device. Here's what you
  63. could do to copy memory around:
  64. > mptr d000
  65. D000
  66. > poke 4
  67. [enter "abcd"]
  68. > peek 4
  69. 61626364
  70. > mptr c000
  71. C000
  72. > peek 4
  73. [RAM garbage]
  74. > bsel 1
  75. > load 4
  76. [returns immediately]
  77. > peek 4
  78. 61626364
  79. > seek 00 0002
  80. > load 2
  81. > peek 4
  82. 63646364
  83. Awesome, right?