Skip to content

Instantly share code, notes, and snippets.

@ISSOtm
Last active February 24, 2021 21:04
Show Gist options
  • Save ISSOtm/40320998e010cd06646becd661063b82 to your computer and use it in GitHub Desktop.
Save ISSOtm/40320998e010cd06646becd661063b82 to your computer and use it in GitHub Desktop.
Various routines for subpixel positions, primarily intended for 12.4 format. Code licensed under CC0 license.
; Get the low byte of the integer (pixel) part of a 12.4 precision coordinate
; @param hl A pointer to the coordinate
; @return a The pixel part
; @return Carry Clear
; @return Z Set depending on whether `a` is zero or not
GetPixelsLowByte::
ld a, [hli]
xor [hl]
and $0F
xor [hl]
ret
; 12 M-cycles, 6 bytes
; Add velocity to 16-bit coordinate pair (can be fixed-point)
; @param hl Pointer to the pair to be modified
; @param e Vertical velocity
; @param d Horizontal velocity
; @return a High byte of the final vertical position
; @return hl Points to just after the coord pair
AddVelocity::
ld a, e
add a, [hl]
ld [hli], a
rl e ; Get sign in carry, and carry in bit 0
sbc a, a ; Perform sign extension (preserves carry!)
rr e ; Get carry back from `add`
adc a, [hl]
ld [hli], a
; Repeat with vertical...
ld a, d
add a, [hl]
ld [hli], a
rl d
sbc a, a
rr d
adc a, [hl]
ld [hli], a
ret
; 30 M-cycles, 21 bytes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment