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.4KB

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