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.

86 lines
2.0KB

  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. ; Call the method (IX) is a pointer to. In other words, call intoIX before
  54. ; callIX
  55. callIXI:
  56. push ix
  57. call intoIX
  58. call callIX
  59. pop ix
  60. ret
  61. ; jump to the location pointed to by IX. This allows us to call IX instead of
  62. ; just jumping it. We use IX because we seldom use this for arguments.
  63. callIX:
  64. jp (ix)
  65. callIY:
  66. jp (iy)
  67. ; Ensures that Z is unset (more complicated than it sounds...)
  68. ; There are often better inline alternatives, either replacing rets with
  69. ; appropriate jmps, or if an 8 bit register is known to not be 0, an inc
  70. ; then a dec. If a is nonzero, 'or a' is optimal.
  71. unsetZ:
  72. or a ;if a nonzero, Z reset
  73. ret nz
  74. cp 1 ;if a is zero, Z reset
  75. ret