Advertisement
Zeda

atan128

Sep 10th, 2018
601
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. atan:
  2. ;D.E is x
  3. ;returns atan(D.E)*128/pi
  4. ;if DE is negative, return -atan(-D.E)
  5.   ld a,d
  6.   or a
  7.   jr nc,+_
  8.     sbc hl,hl
  9.     sbc hl,de
  10.     ex de,hl
  11.     call +_
  12.     neg
  13.     ret
  14. _:
  15. ;at this point, DE is positive
  16. ;if D!=0, then we need to return 64-atan(1.0/D.E)
  17.   inc d
  18.   dec d
  19.   jr z,+_
  20.     call InvDE
  21.     call +_
  22.     cpl
  23.     add a,65
  24.     ret
  25. _:
  26.   ld hl,atan28LUT
  27.   add hl,de
  28.   ld a,(hl)
  29.   ret
  30. InvDE:
  31. ;Special case, D.E >= 1.0
  32. ;returns 1.0/D.E
  33. ;Note: In the case D.E=1.0, actually returns 255/256, but in this context it is fine for the atan routine. Otherwise, we'd just need to add a .db $20 to the end of the LUT, but that's just a copy of 255/256 :P
  34.   ld hl,256
  35. ;  or a
  36. ;  sbc hl,de
  37. ;  ret z
  38. ;  add hl,de
  39.   ld b,8
  40. _:
  41.   add.s hl,hl \ sbc.s hl,de \ jr nc,$+3 \ add hl,de \ rla \ djnz -_
  42.   cpl
  43.   ld e,a
  44.   ld d,b
  45.   ret
  46.  
  47. atan128LUT:
  48. .db $00,$00,$00,$00,$01,$01,$01,$01,$01,$01,$02,$02,$02,$02,$02,$02
  49. .db $03,$03,$03,$03,$03,$03,$03,$04,$04,$04,$04,$04,$04,$05,$05,$05
  50. .db $05,$05,$05,$06,$06,$06,$06,$06,$06,$06,$07,$07,$07,$07,$07,$07
  51. .db $08,$08,$08,$08,$08,$08,$08,$09,$09,$09,$09,$09,$09,$0a,$0a,$0a
  52. .db $0a,$0a,$0a,$0a,$0b,$0b,$0b,$0b,$0b,$0b,$0b,$0c,$0c,$0c,$0c,$0c
  53. .db $0c,$0c,$0d,$0d,$0d,$0d,$0d,$0d,$0d,$0e,$0e,$0e,$0e,$0e,$0e,$0e
  54. .db $0f,$0f,$0f,$0f,$0f,$0f,$0f,$10,$10,$10,$10,$10,$10,$10,$11,$11
  55. .db $11,$11,$11,$11,$11,$11,$12,$12,$12,$12,$12,$12,$12,$13,$13,$13
  56. .db $13,$13,$13,$13,$13,$14,$14,$14,$14,$14,$14,$14,$14,$15,$15,$15
  57. .db $15,$15,$15,$15,$15,$15,$16,$16,$16,$16,$16,$16,$16,$16,$17,$17
  58. .db $17,$17,$17,$17,$17,$17,$17,$18,$18,$18,$18,$18,$18,$18,$18,$18
  59. .db $19,$19,$19,$19,$19,$19,$19,$19,$19,$19,$1a,$1a,$1a,$1a,$1a,$1a
  60. .db $1a,$1a,$1a,$1b,$1b,$1b,$1b,$1b,$1b,$1b,$1b,$1b,$1b,$1c,$1c,$1c
  61. .db $1c,$1c,$1c,$1c,$1c,$1c,$1c,$1c,$1d,$1d,$1d,$1d,$1d,$1d,$1d,$1d
  62. .db $1d,$1d,$1d,$1e,$1e,$1e,$1e,$1e,$1e,$1e,$1e,$1e,$1e,$1e,$1f,$1f
  63. .db $1f,$1f,$1f,$1f,$1f,$1f,$1f,$1f,$1f,$1f,$20,$20,$20,$20,$20,$20
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement