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.

glue-code.md 4.6KB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # Writing the glue code
  2. Collapse OS's kernel code is loosely knit. It supplies parts that you're
  3. expected to glue together in a "glue code" asm file. Here is what a minimal
  4. glue code for a shell on a Classic [RC2014][rc2014] with an ACIA link would
  5. look like:
  6. ; The RAM module is selected on A15, so it has the range 0x8000-0xffff
  7. .equ RAMSTART 0x8000
  8. .equ RAMEND 0xffff
  9. .equ ACIA_CTL 0x80 ; Control and status. RS off.
  10. .equ ACIA_IO 0x81 ; Transmit. RS on.
  11. jr init
  12. ; interrupt hook
  13. .fill 0x38-$
  14. jp aciaInt
  15. init:
  16. di
  17. ; setup stack
  18. ld hl, RAMEND
  19. ld sp, hl
  20. im 1
  21. call aciaInit
  22. xor a
  23. ld de, BLOCKDEV_SEL
  24. call blkSel
  25. call stdioInit
  26. call shellInit
  27. ei
  28. jp shellLoop
  29. #include "core.asm"
  30. .equ ACIA_RAMSTART RAMSTART
  31. #include "acia.asm"
  32. .equ BLOCKDEV_RAMSTART ACIA_RAMEND
  33. .equ BLOCKDEV_COUNT 1
  34. #include "blockdev.asm"
  35. ; List of devices
  36. .dw aciaGetC, aciaPutC, 0, 0
  37. .equ STDIO_RAMSTART BLOCKDEV_RAMEND
  38. #include "stdio.asm"
  39. .equ SHELL_RAMSTART STDIO_RAMEND
  40. .equ SHELL_EXTRA_CMD_COUNT 0
  41. #include "shell.asm"
  42. Once this is written, building it is easy:
  43. zasm < glue.asm > collapseos.bin
  44. ## Building zasm
  45. Collapse OS has its own assembler written in z80 assembly. We call it
  46. [zasm][zasm]. Even on a "modern" machine, it is that assembler that is used,
  47. but because it is written in z80 assembler, it needs to be emulated (with
  48. [libz80][libz80]).
  49. So, the first step is to build zasm. Open `tools/emul/README.md` and follow
  50. instructions there.
  51. ## Platform constants
  52. The upper part of the code contains platform-related constants, information
  53. related to the platform you're targeting. You might want to put it in an
  54. include file if you're writing multiple glue code that targets the same machine.
  55. In all cases, `RAMSTART` are necessary. `RAMSTART` is the offset at which
  56. writable memory begins. This is where the different parts store their
  57. variables.
  58. `RAMEND` is the offset where writable memory stop. This is generally
  59. where we put the stack, but as you can see, setting up the stack is the
  60. responsibility of the glue code, so you can set it up however you wish.
  61. `ACIA_*` are specific to the `acia` part. Details about them are in `acia.asm`.
  62. If you want to manage ACIA, you need your platform to define these ports.
  63. ## Header code
  64. Then comes the header code (code at `0x0000`), a task that also is in the glue
  65. code's turf. `jr init` means that we run our `init` routine on boot.
  66. `jp aciaInt` at `0x38` is needed by the `acia` part. Collapse OS doesn't dictate
  67. a particular interrupt scheme, but some parts might. In the case of `acia`, we
  68. require to be set in interrupt mode 1.
  69. ## Includes
  70. This is the most important part of the glue code and it dictates what will be
  71. included in your OS. Each part is different and has a comment header explaining
  72. how it works, but there are a couple of mechanisms that are common to all.
  73. ### Defines
  74. Parts can define internal constants, but also often document a "Defines" part.
  75. These are constant that are expected to be set before you include the file.
  76. See comment in each part for details.
  77. ### RAM management
  78. Many parts require variables. They need to know where in RAM to store these
  79. variables. Because parts can be mixed and matched arbitrarily, we can't use
  80. fixed memory addresses.
  81. This is why each part that needs variable define a `<PARTNAME>_RAMSTART`
  82. constant that must be defined before we include the part.
  83. Symmetrically, each part define a `<PARTNAME>_RAMEND` to indicate where its
  84. last variable ends.
  85. This way, we can easily and efficiently chain up the RAM of every included part.
  86. ### Tables grafting
  87. A mechanism that is common to some parts is "table grafting". If a part works
  88. on a list of things that need to be defined by the glue code, it will place a
  89. label at the very end of its source file. This way, it becomes easy for the
  90. glue code to "graft" entries to the table. This approach, although simple and
  91. effective, only works for one table per part. But it's often enough.
  92. For example, to define extra commands in the shell:
  93. [...]
  94. .equ SHELL_EXTRA_CMD_COUNT 2
  95. #include "shell.asm"
  96. .dw myCmd1, myCmd2
  97. [...]
  98. ### Initialization
  99. Then, finally, comes the `init` code. This can be pretty much anything really
  100. and this much depends on the part you select. But if you want a shell, you will
  101. usually end it with `shellLoop`, which never returns.
  102. [rc2014]: https://rc2014.co.uk/
  103. [zasm]: ../tools/emul/README.md
  104. [libz80]: https://github.com/ggambetta/libz80