mulf16.asm 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #include once <neg32.asm>
  2. #include once <_mul32.asm>
  3. ;
  4. __MULF16: ;
  5. lda z80_d ;- ld a,d ; load sgn into a
  6. sta z80_a
  7. ldx z80_ap ;- ex af,af' ; saves it
  8. sta z80_ap
  9. txa
  10. jsr __ABS32 ;- call __ABS32 ; convert to positive
  11. lda z80_c ;- exx
  12. ldx z80_cp
  13. stx z80_c
  14. sta z80_cp
  15. lda z80_b
  16. ldx z80_bp
  17. stx z80_b
  18. sta z80_bp
  19. lda z80_e
  20. ldx z80_ep
  21. stx z80_e
  22. sta z80_ep
  23. lda z80_d
  24. ldx z80_dp
  25. stx z80_d
  26. sta z80_dp
  27. lda z80_l
  28. ldx z80_lp
  29. stx z80_l
  30. sta z80_lp
  31. lda z80_h
  32. ldx z80_hp
  33. stx z80_h
  34. sta z80_hp
  35. pla ;- pop hl ; Return address
  36. sta z80_h
  37. pla
  38. sta z80_l
  39. pla ;- pop de ; Low part
  40. sta z80_d
  41. pla
  42. sta z80_e
  43. tsx ;- ex (sp), hl ; CALLEE caller convention; Now HL = Hight part, (SP) = Return address
  44. lda $0103,x
  45. ldy z80_h
  46. sta z80_h
  47. tya
  48. sta $0103,x
  49. lda $0104,x
  50. ldy z80_l
  51. sta z80_l
  52. tya
  53. sta $104,x
  54. lda z80_e ;- ex de,hl ; D'E' = High part (B), H'L' = Low part (B) (must be in DE)
  55. ldx z80_l
  56. stx z80_e
  57. sta z80_l
  58. lda z80_d
  59. ldx z80_h
  60. stx z80_d
  61. sta z80_h
  62. ldx z80_ap ;- ex af,af'
  63. sta z80_ap
  64. txa
  65. eor z80_d ;- xor d ; A register contains resulting sgn
  66. ldx z80_ap ;- ex af,af'
  67. sta z80_ap
  68. txa
  69. jsr __ABS32 ;- call __ABS32 ; convert to positive
  70. jsr call __MUL32_64START ;- call __MUL32_64START
  71. ; rounding (was not included in zx81)
  72. __ROUND_FIX: ; rounds a 64bit (32.32) fixed point number to 16.16
  73. ; result returned in dehl
  74. ; input in h'l'hlb'c'ac
  75. asl z80_a ;- sla a ; result bit 47 to carry
  76. lda z80_c ;- exx
  77. ldx z80_cp
  78. stx z80_c
  79. sta z80_cp
  80. lda z80_b
  81. ldx z80_bp
  82. stx z80_b
  83. sta z80_bp
  84. lda z80_e
  85. ldx z80_ep
  86. stx z80_e
  87. sta z80_ep
  88. lda z80_d
  89. ldx z80_dp
  90. stx z80_d
  91. sta z80_dp
  92. lda z80_l
  93. ldx z80_lp
  94. stx z80_l
  95. sta z80_lp
  96. lda z80_h
  97. ldx z80_hp
  98. stx z80_h
  99. sta z80_hp
  100. lda #<0 ;- ld hl,0 ; ld does not change carry
  101. sta z80_l
  102. lda #>0
  103. sta z80_h
  104. ;- adc hl,bc ; hl = hl + 0 + carry
  105. lda z80_l ;- push hl
  106. pha
  107. lda z80_h
  108. pha
  109. lda z80_c ;- exx
  110. ldx z80_cp
  111. stx z80_c
  112. sta z80_cp
  113. lda z80_b
  114. ldx z80_bp
  115. stx z80_b
  116. sta z80_bp
  117. lda z80_e
  118. ldx z80_ep
  119. stx z80_e
  120. sta z80_ep
  121. lda z80_d
  122. ldx z80_dp
  123. stx z80_d
  124. sta z80_dp
  125. lda z80_l
  126. ldx z80_lp
  127. stx z80_l
  128. sta z80_lp
  129. lda z80_h
  130. ldx z80_hp
  131. stx z80_h
  132. sta z80_hp
  133. lda #<0 ;- ld bc,0
  134. sta z80_c
  135. lda #>0
  136. sta z80_b
  137. ;- adc hl,bc ; hl = hl + 0 + carry
  138. lda z80_e ;- ex de,hl
  139. ldx z80_l
  140. stx z80_e
  141. sta z80_l
  142. lda z80_d
  143. ldx z80_h
  144. stx z80_d
  145. sta z80_h
  146. pla ;- pop hl ; rounded result in de.hl
  147. sta z80_h
  148. pla
  149. sta z80_l
  150. ldx z80_ap ;- ex af,af' ; recovers result sign
  151. sta z80_ap
  152. txa
  153. ora z80_a ;- or a
  154. jmi __NEG32 ;- jp m, __NEG32 ; if negative, negates it
  155. rts ;- ret