SECTION "Utilities", ROM0
|
|
|
|
; Busy-wait until vertical blank occurs
|
|
wait_for_vblank::
|
|
ld a, [$ff41]
|
|
and 3
|
|
cp 1
|
|
jr nz, wait_for_vblank
|
|
ret
|
|
|
|
; Copy data between two regions (takes 40*N+12 cycles)
|
|
; @param bc Pointer to the destination region
|
|
; @param hl Pointer to the source region
|
|
; @param d Size (in bytes) to copy. Must be >0
|
|
; @destroy a, b, c, d, h, l
|
|
memcpy::
|
|
ld a, [hli] ; 8
|
|
ld [bc], a ; 8
|
|
inc bc ; 8
|
|
dec d ; 4
|
|
jr nz, memcpy ; 12/8
|
|
ret ; 16
|
|
|
|
; Copy data between two regions (takes TODO cycles). Only use when size > 255
|
|
; @param bc Pointer to the destination region
|
|
; @param hl Pointer to the source region
|
|
; @param de Size (in bytes) to copy. Must be >0
|
|
; @destroy a, b, c, d, e, h, l
|
|
memcpy16::
|
|
ld a, [hli] ; 8
|
|
ld [bc], a ; 8
|
|
inc bc ; 8
|
|
|
|
dec de ; 8
|
|
xor a ; 4
|
|
or d ; 4
|
|
or e ; 4
|
|
|
|
jr nz, memcpy16 ; 12/8
|
|
ret ; 16
|
|
|
|
; Fill a memory region with a value
|
|
; @param hl Pointer to the destination region
|
|
; @param a Byte to fill with
|
|
; @param c Number of bytes to fill. Must be >0
|
|
; @destroy a, c, hl
|
|
memset::
|
|
ld [hli], a
|
|
dec c
|
|
jr nz, memset
|
|
ret
|
|
|
|
; Divide a value by 10 (http://homepage.divms.uiowa.edu/~jones/bcd/decimal.html#division)
|
|
; @param b Dividend
|
|
; @return a Quotient
|
|
; @destroy b
|
|
div10::
|
|
ld a, b
|
|
srl a
|
|
srl a
|
|
add b
|
|
srl a
|
|
add b
|
|
srl a
|
|
srl a
|
|
srl a
|
|
add b
|
|
srl a
|
|
add b
|
|
srl a
|
|
srl a
|
|
srl a
|
|
srl a
|
|
ret
|
|
|
|
; Compute the minimum of two unsigned integers
|
|
; @param a First integer
|
|
; @param b Second integer
|
|
; @returns The minimum of the two in A
|
|
; 21 cycles if A < B, 26 cycles otherwise
|
|
min::
|
|
cp b
|
|
ret c
|
|
ld a, b
|
|
ret
|