Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;#define DEBUG
- _DispHL = 4507h
- _NewLine = 452Eh
- #define bcall(x) rst 28h \ .dw x
- scrap = 8000h
- var_c = scrap
- var_x = scrap+8
- out = scrap+8
- .db $BB,$6D
- .org $9D95
- ld hl,c0
- ld de,scrap
- ldi
- ldi
- ldi
- ldi
- di
- call +_
- ret
- _:
- ;sqrt16+H_Times_E+mul16+divsub_1+divsub_0
- ;+513+{0,3}+{0,18+{0,1}}+{0,19}+{0,1}+{0,19+{0,42}}
- ;min:513+318+190+359+1066+406
- ;max:616+351+242+717+1306+466
- ;avg:553.75+334.5+216+596.34375cc+1222+448.5
- ;min: 2852
- ;max: 3698
- ;avg: 3371.09375cc
- ld hl,(scrap) \ ld (scrap+4),hl
- ld hl,(scrap+2)
- call sqrt16
- ;a is the first 8 bits
- ld (out+3),a
- ld h,a
- ld e,a
- call H_Times_E
- ex de,hl
- ld hl,(scrap+2)
- sbc hl,de
- push af
- srl h
- rr l
- .echo "Maybe round here?"
- pop af
- push af
- ld a,l
- jr nc,$+4
- neg
- ld bc,(out+3)
- ;a/c, to 8 bits prec round -> (out+2)
- call divsub_0
- ld a,(out+3)
- ld h,a
- pop af
- jr nc,+_
- xor a
- sub l
- ld l,a
- jr c,$+3
- dec h
- _:
- ld (out+2),hl
- ex de,hl \ ld b,d \ ld c,e
- call mul16
- ld b,h \ ld c,l \ ld hl,(var_c) \ or a \ sbc hl,de
- ex de,hl
- ld hl,(var_c+2) \ sbc hl,bc
- push af ;contains overflow info
- jr nc,$+8
- xor a
- sub e
- ld e,a
- sbc a,a
- sub d
- ld d,a
- srl d
- rr e
- jr nc,$+3
- inc de
- ld hl,(var_x+2)
- ex de,hl
- call divsub_1
- pop af
- jr nc,+_
- xor a
- ld h,a
- ld l,a
- sbc hl,bc
- ld (var_x),hl
- ret nc
- ld hl,(var_x+2)
- dec hl
- ld (var_x+2),hl
- ret
- _:
- ld (var_x),bc
- ret
- divsub_1:
- ;min: 1066cc
- ;max: 1306cc
- ;avg: 1222cc
- call +_
- rla
- cpl
- ld b,a
- call +_
- rla
- cpl
- ld c,a
- ret
- _:
- ;min: 499
- ;max: 619
- ;avg: 577
- call +_+1
- call +_
- call +_
- call +_
- call +_
- call +_
- call +_
- _:
- ;min: 48
- ;max: 63
- ;avg: 57.75
- rla
- add hl,hl
- jr c,$+7
- sbc hl,de
- ret nc
- add hl,de
- ret
- or a
- sbc hl,de
- or a
- ret
- mul16:
- ;min: 359cc
- ;max: 717cc
- ;avg: 596.34375cc
- ;92 bytes
- ld a,c
- call DE_Times_A
- push hl
- push af
- ld a,b
- call DE_Times_A+2
- pop bc
- pop de
- ;AHL
- ; BDE
- ld c,d
- add hl,bc
- adc a,0
- ;AHLE
- ld d,l
- ld l,h
- ld h,a
- ;HLDE
- ret
- DE_Times_A:
- ;Input: DE,A
- ;Output: A:HL is the product, C=0, B,DE unaffected, z flag set if result is zero, c flag set if A is input as 1, else nc.
- ;A:128~255 219+6{0,10}+{0,19} avg=258.5 *1/2
- ;A:64~127 203+5{0,10}+{0,19} avg=237.5 *1/4
- ;A:32~63 187+4{0,10}+{0,19} avg=216.5 *1/8
- ;A:16~31 171+3{0,10}+{0,19} avg=195.5 *1/16
- ;A:8~15 155+2{0,10}+{0,19} avg=174.5 *1/32
- ;A:4~7 139+{0,10}+{0,19} avg=153.5 *1/64
- ;A:2~3 123+{0,19} avg=132.5 *1/128
- ;A:1 107cc avg=107 *1/256
- ;A:0 119cc avg=119 *1/256
- ;overall avg: 237.671875cc
- ld c,0
- ld h,d
- ld l,e
- rla \ jr c,mul_07
- rla \ jr c,mul_06
- rla \ jr c,mul_05
- rla \ jr c,mul_04
- rla \ jr c,mul_03
- rla \ jr c,mul_02
- rla \ jr c,mul_01
- rla
- ret c
- ld h,a
- ld l,a
- ret
- mul_07:
- add hl,hl \ rla \ jr nc,$+4 \ add hl,de \ adc a,c
- mul_06:
- add hl,hl \ rla \ jr nc,$+4 \ add hl,de \ adc a,c
- mul_05:
- add hl,hl \ rla \ jr nc,$+4 \ add hl,de \ adc a,c
- mul_04:
- add hl,hl \ rla \ jr nc,$+4 \ add hl,de \ adc a,c
- mul_03:
- add hl,hl \ rla \ jr nc,$+4 \ add hl,de \ adc a,c
- mul_02:
- add hl,hl \ rla \ jr nc,$+4 \ add hl,de \ adc a,c
- mul_01:
- add hl,hl \ rla \ ret nc \ add hl,de \ adc a,c
- ret
- divsub_0:
- ;A_div_C
- ;min: 406
- ;max: 466
- ;min: 448.5
- ld l,0
- add a,a \ jr c,$+5 \ cp c \ jr c,$+5 \ sub c \ ld l,2
- add a,a \ jr c,$+5 \ cp c \ jr c,$+4 \ sub c \ inc l
- sla l \ add a,a \ jr c,$+5 \ cp c \ jr c,$+4 \ sub c \ inc l
- sla l \ add a,a \ jr c,$+5 \ cp c \ jr c,$+4 \ sub c \ inc l
- sla l \ add a,a \ jr c,$+5 \ cp c \ jr c,$+4 \ sub c \ inc l
- sla l \ add a,a \ jr c,$+5 \ cp c \ jr c,$+4 \ sub c \ inc l
- sla l \ add a,a \ jr c,$+5 \ cp c \ jr c,$+4 \ sub c \ inc l
- sla l \ add a,a \ jr c,$+5 \ cp c \ jr c,$+4 \ sub c \ inc l
- add a,a \ jr c,$+4 \ cp c \ ret c \ inc l
- ret
- H_Times_E:
- ;min: 190
- ;max: 242
- ;avg: 216c
- ld d,0
- ld l,d
- sla h \ jr nc,$+3 \ ld l,e
- add hl,hl \ jr nc,$+3 \ add hl,de
- add hl,hl \ jr nc,$+3 \ add hl,de
- add hl,hl \ jr nc,$+3 \ add hl,de
- add hl,hl \ jr nc,$+3 \ add hl,de
- add hl,hl \ jr nc,$+3 \ add hl,de
- add hl,hl \ jr nc,$+3 \ add hl,de
- add hl,hl \ ret nc \ add hl,de \ ret
- sqrt16:
- ;source: John Metcalf, http://www.retroprogramming.com/2017/07/a-fast-z80-integer-square-root.html
- ; call with hl = number to square root
- ; returns a = square root
- ; corrupts hl, de
- ;318cc+{0,6}+{0,6}+{0,6}+{0,6}+{0,3}+{0,3}+{0,3}
- ;min:318cc
- ;max:351cc
- ;avg:334.5cc
- ld a,h
- ld de,0B0C0h
- add a,e
- jr c,sq7
- ld a,h
- ld d,0F0h
- sq7:
- ; ----------
- add a,d
- jr nc,sq6
- res 5,d
- .db 254
- sq6:
- sub d
- sra d
- ; ----------
- set 2,d
- add a,d
- jr nc,sq5
- res 3,d
- .db 254
- sq5:
- sub d
- sra d
- ; ----------
- inc d
- add a,d
- jr nc,sq4
- res 1,d
- .db 254
- sq4:
- sub d
- sra d
- ld h,a
- ; ----------
- add hl,de
- jr nc,sq3
- ld e,040h
- .db 210
- sq3:
- sbc hl,de
- sra d
- ld a,e
- rra
- ; ----------
- or 010h
- ld e,a
- add hl,de
- jr nc,sq2
- and 0DFh
- .db 218
- sq2:
- sbc hl,de
- sra d
- rra
- ; ----------
- or 04h
- ld e,a
- add hl,de
- jr nc,sq1
- and 0F7h
- .db 218
- sq1:
- sbc hl,de
- sra d
- rra
- ; ----------
- inc a
- ld e,a
- add hl,de
- jr nc,sq0
- and 0FDh
- sq0:
- sra d
- rra
- cpl
- ret
- c0:
- ;format is:
- ; 1 byte for the exponent, with +128 bias
- ; Next 31 bits is the exponent
- ; Next bit is sign
- .db $77,$77,$77,$47
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement