X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=device%2Flib%2Fz80%2Fmul.s;h=61ff5cf34179e99e6ab25d8ac8c2726725d46555;hb=e56ff7ea8ea1bffdcf4fee43ec8a125453a6ad8d;hp=fa3fb63ebfcebfa43eb9e0686ef39e767a8daa55;hpb=c7ab63a07c2c3da85a15a3be5e5e20cb2b432065;p=fw%2Fsdcc diff --git a/device/lib/z80/mul.s b/device/lib/z80/mul.s index fa3fb63e..61ff5cf3 100644 --- a/device/lib/z80/mul.s +++ b/device/lib/z80/mul.s @@ -1,47 +1,9 @@ - ;; Originally from GBDK by Pascal Felber. - - .area _CODE + .area _CODE -__mulschar_rr_s:: +__mulint_rrx_s:: ld hl,#2 add hl,sp - ld e,(hl) - inc hl - ld l,(hl) - - ;; Fall through -__mulschar_rr_hds:: - ;; Need to sign extend before going in. - ld c,l - - ld a,l - rla - sbc a,a - ld b,a - - ld a,e - rla - sbc a,a - ld d,a - - jp .mul16 - -__muluchar_rr_s:: - ld hl,#2 - add hl,sp - - ld e,(hl) - inc hl - ld c,(hl) - - jp .mulu16 - -__mulsint_rr_s:: -__muluint_rr_s:: - ld hl,#2 - add hl,sp - ld e,(hl) inc hl ld d,(hl) @@ -52,46 +14,45 @@ __muluint_rr_s:: ld l,a ;; Fall through - -__muluchar_rr_hds:: -__mulsint_rr_hds:: -__muluint_rr_hds:: + +__muluchar_rrx_hds:: +__mulint_rrx_hds:: ;; Parameters: - ;; HL, DE (left, right irrelivent) + ;; hl, de (left, right irrelevant) ld b,h ld c,l - + ;; 16-bit multiplication - ;; + ;; ;; Entry conditions - ;; BC = multiplicand - ;; DE = multiplier - ;; + ;; bc = multiplicand + ;; de = multiplier + ;; ;; Exit conditions - ;; DE = less significant word of product + ;; hl = less significant word of product ;; ;; Register used: AF,BC,DE,HL -.mul16: -.mulu16: - LD HL,#0x00 ; Product = 0 - LD A,#15 ; Count = bit length - 1 - ;; Shift-and-add algorithm - ;; If MSB of multiplier is 1, add multiplicand to partial product - ;; Shift partial product, multiplier left 1 bit -.mlp: - SLA E ; Shift multiplier left 1 bit - RL D - jp NC,.mlp1 ; Jump if MSB of multiplier = 0 - ADD HL,BC ; Add multiplicand to partial product -.mlp1: - ADD HL,HL ; Shift partial product left - DEC A - jp NZ,.mlp ; Continue until count = 0 - ;; Add multiplicand one last time if MSB of multiplier is 1 - BIT 7,D ; Get MSB of multiplier - JR Z,.mend ; Exit if MSB of multiplier is 0 - ADD HL,BC ; Add multiplicand to product -.mend: - ; HL = result - ret +__mul16:: + xor a,a + ld l,a + or a,b + ld b,#16 + + ;; Optimise for the case when this side has 8 bits of data or + ;; less. This is often the case with support address calls. + jr NZ,2$ + ld b,#8 + ld a,c +1$: + ;; Taken from z88dk, which originally borrowed from the + ;; Spectrum rom. + add hl,hl +2$: + rl c + rla ;DLE 27/11/98 + jr NC,3$ + add hl,de +3$: + djnz 1$ + ret