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.

97 lines
2.1KB

  1. ; core
  2. ;
  3. ; Routines used pretty much all everywhere. Unlike all other kernel units,
  4. ; this unit is designed to be included directly by userspace apps, not accessed
  5. ; through jump tables. The reason for this is that jump tables are a little
  6. ; costly in terms of machine cycles and that these routines are not very costly
  7. ; in terms of binary space.
  8. ; Therefore, this unit has to stay small and tight because it's repeated both
  9. ; in the kernel and in userspace. It should also be exclusively for routines
  10. ; used in the kernel.
  11. ; add the value of A into DE
  12. addDE:
  13. push af
  14. add a, e
  15. jr nc, .end ; no carry? skip inc
  16. inc d
  17. .end:
  18. ld e, a
  19. pop af
  20. noop: ; piggy backing on the first "ret" we have
  21. ret
  22. ; add the value of A into HL
  23. ; affects carry flag according to the 16-bit addition, Z, S and P untouched.
  24. addHL:
  25. push de
  26. ld d, 0
  27. ld e, a
  28. add hl, de
  29. pop de
  30. ret
  31. ; copy (HL) into DE, then exchange the two, utilising the optimised HL instructions.
  32. ; ld must be done little endian, so least significant byte first.
  33. intoHL:
  34. push de
  35. ld e, (hl)
  36. inc hl
  37. ld d, (hl)
  38. ex de, hl
  39. pop de
  40. ret
  41. intoDE:
  42. ex de, hl
  43. call intoHL
  44. ex de, hl ; de preserved by intoHL, so no push/pop needed
  45. ret
  46. intoIX:
  47. push ix
  48. ex (sp), hl ;swap hl with ix, on the stack
  49. call intoHL
  50. ex (sp), hl ;restore hl from stack
  51. pop ix
  52. ret
  53. ; Write the contents of HL in (DE)
  54. ; de and hl are preserved, so no pushing/popping necessary
  55. writeHLinDE:
  56. ex de, hl
  57. ld (hl), e
  58. inc hl
  59. ld (hl), d
  60. dec hl
  61. ex de, hl
  62. ret
  63. ; Call the method (IX) is a pointer to. In other words, call intoIX before
  64. ; callIX
  65. callIXI:
  66. push ix
  67. call intoIX
  68. call callIX
  69. pop ix
  70. ret
  71. ; jump to the location pointed to by IX. This allows us to call IX instead of
  72. ; just jumping it. We use IX because we seldom use this for arguments.
  73. callIX:
  74. jp (ix)
  75. callIY:
  76. jp (iy)
  77. ; Ensures that Z is unset (more complicated than it sounds...)
  78. ; There are often better inline alternatives, either replacing rets with
  79. ; appropriate jmps, or if an 8 bit register is known to not be 0, an inc
  80. ; then a dec. If a is nonzero, 'or a' is optimal.
  81. unsetZ:
  82. or a ;if a nonzero, Z reset
  83. ret nz
  84. cp 1 ;if a is zero, Z reset
  85. ret