Advertisement
Legomany3448

eZ80 Fixed-Point Routines

Jan 17th, 2016
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; Output in HL
  2. ; Destroys A
  3. ; Preserves everything else (except DE, if result is negative)
  4. DEMultBC:
  5.     call AbsDE
  6.     ld a,$B7
  7.     jp m,+_ ; Jump if BC was POSITIVE
  8.     xor a,%10000000
  9. _:  call AbsBC
  10.     jp m,+_ ; Jump if DE was POSITIVE
  11.     xor a,%10000000
  12. _:  ld (MultSMC),a
  13.     or a,a \ sbc hl,hl
  14.     dec sp \ push bc \ inc sp \ pop af
  15.                 rla \ jr nc,$+3 \ add hl,de
  16.     add hl,hl \ rla \ jr nc,$+3 \ add hl,de
  17.     add hl,hl \ rla \ jr nc,$+3 \ add hl,de
  18.     add hl,hl \ rla \ jr nc,$+3 \ add hl,de
  19.     add hl,hl \ rla \ jr nc,$+3 \ add hl,de
  20.     add hl,hl \ rla \ jr nc,$+3 \ add hl,de
  21.     add hl,hl \ rla \ jr nc,$+3 \ add hl,de
  22.     add hl,hl \ rla \ jr nc,$+3 \ add hl,de
  23.     ld a,b
  24.     add hl,hl \ rla \ jr nc,$+3 \ add hl,de
  25.     add hl,hl \ rla \ jr nc,$+3 \ add hl,de
  26.     add hl,hl \ rla \ jr nc,$+3 \ add hl,de
  27.     add hl,hl \ rla \ jr nc,$+3 \ add hl,de
  28.     add hl,hl \ rla \ jr nc,$+3 \ add hl,de
  29.     add hl,hl \ rla \ jr nc,$+3 \ add hl,de
  30.     add hl,hl \ rla \ jr nc,$+3 \ add hl,de
  31.     add hl,hl \ rla \ jr nc,$+3 \ add hl,de
  32.     ld a,c
  33.     add hl,hl \ rla \ jr nc,$+3 \ add hl,de
  34.     add hl,hl \ rla \ jr nc,$+3 \ add hl,de
  35.     add hl,hl \ rla \ jr nc,$+3 \ add hl,de
  36.     add hl,hl \ rla \ jr nc,$+3 \ add hl,de
  37.     add hl,hl \ rla \ jr nc,$+3 \ add hl,de
  38.     add hl,hl \ rla \ jr nc,$+3 \ add hl,de
  39.     add hl,hl \ rla \ jr nc,$+3 \ add hl,de
  40.     add hl,hl \ rla \ jr nc,$+3 \ add hl,de
  41.     res 7,a ; make sure the sign does not lie
  42.     push af \ inc sp \ push hl \ inc sp \ pop hl \ inc sp ; A -> HLu -> H -> L
  43. MultSMC:    or a,a
  44.     ret nc
  45.     ex de,hl
  46.     or a,a \ sbc hl,hl
  47.     sbc hl,de
  48.     ret
  49.  
  50. ; Output in DE
  51. DEDivBC: ; Mostly copied from Xeda
  52.     ld a,$B7
  53.     call AbsDE
  54.     jp m,+_
  55.     xor a,%10000000
  56. _:  call AbsBC
  57.     jp m,+_
  58.     xor a,%10000000
  59. _:  ld (DivSMC),a
  60.     dec sp \ push de \ inc sp \ pop af
  61.     or a,a \ sbc hl,hl
  62.     ld l,a ; DEu
  63.     ld a,d
  64.     rla \ adc hl,hl \ sbc hl,bc \ jr nc,$+3 \ add hl,bc
  65.     rla \ adc hl,hl \ sbc hl,bc \ jr nc,$+3 \ add hl,bc
  66.     rla \ adc hl,hl \ sbc hl,bc \ jr nc,$+3 \ add hl,bc
  67.     rla \ adc hl,hl \ sbc hl,bc \ jr nc,$+3 \ add hl,bc
  68.     rla \ adc hl,hl \ sbc hl,bc \ jr nc,$+3 \ add hl,bc
  69.     rla \ adc hl,hl \ sbc hl,bc \ jr nc,$+3 \ add hl,bc
  70.     rla \ adc hl,hl \ sbc hl,bc \ jr nc,$+3 \ add hl,bc
  71.     rla \ adc hl,hl \ sbc hl,bc \ jr nc,$+3 \ add hl,bc
  72.     rla \ cpl \ ld d,a
  73.     ld a,e
  74.     rla \ adc hl,hl \ sbc hl,bc \ jr nc,$+3 \ add hl,bc
  75.     rla \ adc hl,hl \ sbc hl,bc \ jr nc,$+3 \ add hl,bc
  76.     rla \ adc hl,hl \ sbc hl,bc \ jr nc,$+3 \ add hl,bc
  77.     rla \ adc hl,hl \ sbc hl,bc \ jr nc,$+3 \ add hl,bc
  78.     rla \ adc hl,hl \ sbc hl,bc \ jr nc,$+3 \ add hl,bc
  79.     rla \ adc hl,hl \ sbc hl,bc \ jr nc,$+3 \ add hl,bc
  80.     rla \ adc hl,hl \ sbc hl,bc \ jr nc,$+3 \ add hl,bc
  81.     rla \ adc hl,hl \ sbc hl,bc \ jr nc,$+3 \ add hl,bc
  82.     rla \ cpl \ ld e,a
  83.     ; HL = remainder
  84.     ; DE = integer result
  85.     ; Let's do a byte shift on DE to make room for the decimal result
  86.     push de \ dec sp \ pop de \ inc sp
  87.     xor a,a
  88.     rla \ add hl,hl \ sbc hl,bc \ jr nc,$+3 \ add hl,bc
  89.     rla \ add hl,hl \ sbc hl,bc \ jr nc,$+3 \ add hl,bc
  90.     rla \ add hl,hl \ sbc hl,bc \ jr nc,$+3 \ add hl,bc
  91.     rla \ add hl,hl \ sbc hl,bc \ jr nc,$+3 \ add hl,bc
  92.     rla \ add hl,hl \ sbc hl,bc \ jr nc,$+3 \ add hl,bc
  93.     rla \ add hl,hl \ sbc hl,bc \ jr nc,$+3 \ add hl,bc
  94.     rla \ add hl,hl \ sbc hl,bc \ jr nc,$+3 \ add hl,bc
  95.     rla \ add hl,hl \ sbc hl,bc \ jr nc,$+3 \ add hl,bc
  96.     rla \ cpl \ ld e,a
  97. DivSMC:     or a,a
  98.     ret nc
  99.     or a,a \ sbc hl,hl
  100.     ; or a,a ; shouldn't be necessary
  101.     sbc hl,de
  102.     ex de,hl
  103.     ret
  104.  
  105. ; Result in DE
  106. ; S flag is set to opposite of input sign
  107. AbsDE:
  108.     or a,a
  109.     sbc hl,hl
  110.     sbc hl,de
  111.     ret m ; If ( 0 - DE ) is negative, then DE is positive (so return)
  112.     ex de,hl
  113.     ret
  114.  
  115. ; Result in BC
  116. ; S flag is set to opposite of input sign
  117. AbsBC:
  118.     or a,a
  119.     sbc hl,hl
  120.     sbc hl,bc
  121.     ret m ; If ( 0 - BC ) is negative, then BC is positive (so return)
  122.     push hl \ pop bc
  123.     ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement