Advertisement
Guest User

atan128

a guest
Sep 10th, 2018
66
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.     xor a
  13.     sub l
  14.     ld l,a
  15.     sbc a,a
  16.     sub h
  17.     ld h,a
  18.     ret
  19. _:
  20. ;at this point, DE is positive
  21. ;if D!=0, then we need to return 64-atan(1.0/D.E)
  22.   inc d
  23.   dec d
  24.   jr z,+_
  25.     call InvDE
  26.     call +_
  27.     ex de,hl
  28.     ld hl,64
  29.     or a
  30.     sbc hl,de
  31.     ret
  32. _:
  33.   ld hl,atan28LUT
  34.   add hl,de
  35.   ld e,(hl)
  36.   ex de,hl
  37.   ret
  38. InvDE:
  39. ;Special case, D.E >= 1.0
  40. ;returns 1.0/D.E
  41. ;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
  42.   ld hl,256
  43. //  or a
  44. //  sbc hl,de
  45. //  ret z
  46. //  add hl,de
  47.   ld b,8
  48. _:
  49.   add.s hl,hl \ sbc.s hl,de \ jr nc,$+3 \ add hl,de \ rla \ djnz -_
  50.   cpl
  51.   ld e,a
  52.   ld d,b
  53.   ret
  54.  
  55. atan128LUT:
  56. .db $00,$00,$00,$00,$01,$01,$01,$01,$01,$01,$02,$02,$02,$02,$02,$02
  57. .db $03,$03,$03,$03,$03,$03,$03,$04,$04,$04,$04,$04,$04,$05,$05,$05
  58. .db $05,$05,$05,$06,$06,$06,$06,$06,$06,$06,$07,$07,$07,$07,$07,$07
  59. .db $08,$08,$08,$08,$08,$08,$08,$09,$09,$09,$09,$09,$09,$0a,$0a,$0a
  60. .db $0a,$0a,$0a,$0a,$0b,$0b,$0b,$0b,$0b,$0b,$0b,$0c,$0c,$0c,$0c,$0c
  61. .db $0c,$0c,$0d,$0d,$0d,$0d,$0d,$0d,$0d,$0e,$0e,$0e,$0e,$0e,$0e,$0e
  62. .db $0f,$0f,$0f,$0f,$0f,$0f,$0f,$10,$10,$10,$10,$10,$10,$10,$11,$11
  63. .db $11,$11,$11,$11,$11,$11,$12,$12,$12,$12,$12,$12,$12,$13,$13,$13
  64. .db $13,$13,$13,$13,$13,$14,$14,$14,$14,$14,$14,$14,$14,$15,$15,$15
  65. .db $15,$15,$15,$15,$15,$15,$16,$16,$16,$16,$16,$16,$16,$16,$17,$17
  66. .db $17,$17,$17,$17,$17,$17,$17,$18,$18,$18,$18,$18,$18,$18,$18,$18
  67. .db $19,$19,$19,$19,$19,$19,$19,$19,$19,$19,$1a,$1a,$1a,$1a,$1a,$1a
  68. .db $1a,$1a,$1a,$1b,$1b,$1b,$1b,$1b,$1b,$1b,$1b,$1b,$1b,$1c,$1c,$1c
  69. .db $1c,$1c,$1c,$1c,$1c,$1c,$1c,$1c,$1d,$1d,$1d,$1d,$1d,$1d,$1d,$1d
  70. .db $1d,$1d,$1d,$1e,$1e,$1e,$1e,$1e,$1e,$1e,$1e,$1e,$1e,$1e,$1f,$1f
  71. .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