ConvOP1.z80 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. ConvOP1:
  2. ld hl,OP1
  3. convFloat:
  4. ;Inputs: HL points to the TI Float.
  5. ;Outputs: The float is converted to a 16-bit integer held in DE, or LSB in A.
  6. ;68cc if not a number
  7. ;93cc on (0,1)
  8. ;154.140625cc on [1,10) or 0
  9. ;324.5159375cc on [10,100)
  10. ;435.6565625cc on [100,1000)
  11. ;540.031875cc on [1000,10000)
  12. ;651.1725cc on [10000,100000)
  13. ;118cc if >=100000
  14. ;Average for integers on [0,65535]: ~632.43cc
  15. call +_
  16. ld a,e
  17. ret
  18. _:
  19. xor a
  20. ld d,a
  21. ld e,a
  22. or (hl)
  23. and 31
  24. ret nz
  25. inc hl
  26. ld a,(hl)
  27. sub 80h
  28. ret c
  29. inc hl
  30. jr z,lastdigit2
  31. cp 5
  32. ret nc
  33. ld b,a
  34. _:
  35. ;multiply DE by 100
  36. ex de,hl
  37. ld a,b
  38. sla l
  39. add hl,hl
  40. ld b,h
  41. ld c,l
  42. add hl,hl
  43. add hl,bc
  44. add hl,hl
  45. add hl,hl
  46. add hl,hl
  47. add hl,bc
  48. ex de,hl
  49. ld b,a
  50. call convBCDbyte
  51. inc hl
  52. dec b
  53. ret z
  54. djnz -_
  55. lastdigit:
  56. ex de,hl
  57. ld b,h
  58. ld c,l
  59. add hl,hl
  60. add hl,hl
  61. add hl,bc
  62. add hl,hl
  63. ex de,hl
  64. lastdigit2:
  65. ;49+{0,8
  66. ;min: 49cc
  67. ;max: 57cc
  68. ;avg: 49+8*4.5/256 = 49.140625
  69. ld a,(hl)
  70. rrca
  71. rrca
  72. rrca
  73. rrca
  74. and 15
  75. add a,e
  76. ld e,a
  77. ret nc
  78. inc d
  79. ret
  80. convBCDbyte:
  81. ;min: 60cc
  82. ;max: 68cc
  83. ;avg: 60+8*(99*98/2/100)/256 = 61.5159375cc
  84. ld a,(hl) ;\ I feel particularly proud of this code, so feel free to use it ^~^
  85. and $F0 ; |It converts a byte of BCD to an 8-bit int.
  86. rra ; |The catch is, I only have A and C to use, and (hl) is the BCD
  87. ld c,a ; |number.
  88. rra ; |If you come up with faster, please let me know and post it in
  89. rra ; |the optimized routines thread on popular TI forums.
  90. sub a,c ; |Algo: Multiply upper digit by -6, add the original byte.
  91. add a,(hl) ;/ Result is upper_digit*(-6+16)+lower_digit. Ha, repost.
  92. add a,e
  93. ld e,a
  94. ret nc
  95. inc d
  96. ret