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.

85 lines
1.7 KiB

2 years ago
  1. SECTION "Utilities", ROM0
  2. ; Busy-wait until vertical blank occurs
  3. wait_for_vblank::
  4. ld a, [$ff41]
  5. and 3
  6. cp 1
  7. jr nz, wait_for_vblank
  8. ret
  9. ; Copy data between two regions (takes 40*N+12 cycles)
  10. ; @param bc Pointer to the destination region
  11. ; @param hl Pointer to the source region
  12. ; @param d Size (in bytes) to copy. Must be >0
  13. ; @destroy a, b, c, d, h, l
  14. memcpy::
  15. ld a, [hli] ; 8
  16. ld [bc], a ; 8
  17. inc bc ; 8
  18. dec d ; 4
  19. jr nz, memcpy ; 12/8
  20. ret ; 16
  21. ; Copy data between two regions (takes TODO cycles). Only use when size > 255
  22. ; @param bc Pointer to the destination region
  23. ; @param hl Pointer to the source region
  24. ; @param de Size (in bytes) to copy. Must be >0
  25. ; @destroy a, b, c, d, e, h, l
  26. memcpy16::
  27. ld a, [hli] ; 8
  28. ld [bc], a ; 8
  29. inc bc ; 8
  30. dec de ; 8
  31. xor a ; 4
  32. or d ; 4
  33. or e ; 4
  34. jr nz, memcpy16 ; 12/8
  35. ret ; 16
  36. ; Fill a memory region with a value
  37. ; @param hl Pointer to the destination region
  38. ; @param a Byte to fill with
  39. ; @param c Number of bytes to fill. Must be >0
  40. ; @destroy a, c, hl
  41. memset::
  42. ld [hli], a
  43. dec c
  44. jr nz, memset
  45. ret
  46. ; Divide a value by 10 (http://homepage.divms.uiowa.edu/~jones/bcd/decimal.html#division)
  47. ; @param b Dividend
  48. ; @return a Quotient
  49. ; @destroy b
  50. div10::
  51. ld a, b
  52. srl a
  53. srl a
  54. add b
  55. srl a
  56. add b
  57. srl a
  58. srl a
  59. srl a
  60. add b
  61. srl a
  62. add b
  63. srl a
  64. srl a
  65. srl a
  66. srl a
  67. ret
  68. ; Compute the minimum of two unsigned integers
  69. ; @param a First integer
  70. ; @param b Second integer
  71. ; @returns The minimum of the two in A
  72. ; 21 cycles if A < B, 26 cycles otherwise
  73. min::
  74. cp b
  75. ret c
  76. ld a, b
  77. ret