Mirror of CollapseOS
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. ## Code conventions
  2. The code in this project follow certain project-wide conventions, which are
  3. described here. Kernel code and userspace code follow additional conventions
  4. which are described in `kernel/README.md` and `apps/README.md`.
  5. ## Defines
  6. Each unit can have its own constants, but some constant are made to be defined
  7. externally. We already have some of those external definitions in platform
  8. includes, but we can have more defines than this.
  9. Many units have a "DEFINES" section listing the constant it expects to be
  10. defined. Make sure that you have these constants defined before you include the
  11. file.
  12. ## Variable management
  13. Each unit can define variables. These variables are defined as addresses in
  14. RAM. We know where RAM start from the `RAMSTART` constant in platform includes,
  15. but because those parts are made to be glued together in no pre-defined order,
  16. we need a system to align variables from different modules in RAM.
  17. This is why each unit that has variable expect a `<PREFIX>_RAMSTART`
  18. constant to be defined and, in turn, defines a `<PREFIX>_RAMEND` constant to
  19. carry to the following unit.
  20. Thus, code that glue parts together could look like:
  21. MOD1_RAMSTART .equ RAMSTART
  22. #include "mod1.asm"
  23. MOD2_RAMSTART .equ MOD1_RAMEND
  24. #include "mod2.asm"
  25. ## Register protection
  26. As a general rule, all routines systematically protect registers they use,
  27. including input parameters. This allows us to stop worrying, each time we call
  28. a routine, whether our registers are all messed up.
  29. Some routines stray from that rule, but the fact that they destroy a particular
  30. register is documented. An undocumented register change is considered a bug.
  31. Clean up after yourself, you nasty routine!
  32. Another exception to this rule are "top-level" routines, that is, routines that
  33. aren't designed to be called from other parts of Collapse OS. Those are
  34. generally routines close to an application's main loop.
  35. It is important to note, however, that shadow registers aren't preserved.
  36. Therefore, shadow registers should only be used in code that doesn't call
  37. routines or that call a routine that explicitly states that it preserves
  38. shadow registers.
  39. Another important note is that routines returning success with Z generally don't
  40. preserve AF: too complicated. But otherwise, AF is often preserved. For example,
  41. register fiddling routines in core try to preserve AF.
  42. ## Z for success
  43. The vast majority of routines use the Z flag to indicate success. When Z is set,
  44. it indicates success. When Z is unset, it indicates error. This follows the
  45. tradition of a zero indicating success and a nonzero indicating error.
  46. Important note: only Z indicate success. Many routines return a meaningful
  47. nonzero value in A and still set Z to indicate success.
  48. In error conditions, however, most of the time A is set to an error code.
  49. In many routines, this is specified verbosely, but it's repeated so often that
  50. I started writing it in short form, "Z for success", which means what is
  51. described here.
  52. ## Stack management
  53. Keeping the stack "balanced" is a big challenge when writing assembler code.
  54. Those push and pop need to correspond, otherwise we end up with completely
  55. broken code.
  56. The usual "push/pop" at the beginning and end of a routine is rather easy to
  57. manage, nothing special about them.
  58. The problem is for the "inner" push and pop, which are often necessary in
  59. routines handling more data at once. In those cases, we walk on eggshells.
  60. A naive approach could be to indent the code between those push/pop, but indent
  61. level would quickly become too big to fit in 80 chars.
  62. I've tried ASCII art in some places, where comments next to push/pop have "|"
  63. indicating the scope of the push/pop. It's nice, but it makes code complicated
  64. to edit, especially when dense comments are involved. The pipes have to go
  65. through them.
  66. Of course, one could add descriptions next to each push/pop describing what is
  67. being pushed, and I do it in some places, but it doesn't help much in easily
  68. tracking down stack levels.
  69. So, what I've started doing is to accompany each "non-routine" (at the
  70. beginning and end of a routine) push/pop with "--> lvl X" and "<-- lvl X"
  71. comments. Example:
  72. push af ; --> lvl 1
  73. inc a
  74. push af ; --> lvl 2
  75. inc a
  76. pop af ; <-- lvl 2
  77. pop af ; <-- lvl 1
  78. I think that this should do the trick, so I'll do this consistently from now on.
  79. ## String length
  80. Pretty much every routine expecting a string have no provision for a string
  81. that doesn't have null termination within 0xff bytes. Treat strings of such
  82. lengths with extra precaution and distrust proper handling of existing routines
  83. for those strings.
  84. [zasm]: ../apps/zasm/README.md