Re-worked the makefiles and includes to target z80 and gbz80 as well
[fw/sdcc] / device / lib / z80 / mul.s
index 57c06a91df7950ea62ba6c7147891aa08b98103d..9c33ac30aa4054d95e9c6db7958b1a43198630fb 100644 (file)
@@ -1,47 +1,33 @@
        ;; Originally from GBDK by Pascal Felber.
        
        .area   _CODE
-__mulschar::   
-__muluchar::
-       push    de
-       push    bc
-       push    ix
-       ld      ix,#0
-       add     ix,sp
-
-       ld      c,8(ix)
-       ld      e,9(ix)
-       call    .mulu8
        
-       pop     ix
-       pop     bc
-       pop     de
-       ret
+__mulschar::
+        ;; Need to sign extend before going in.
+        push    bc
+        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::
 __mulsint::
 __muluint::
-       push    de
+       ;; Parameters:
+       ;;      HL, DE (left, right irrelivent)
+       ;; Must preserve BC
        push    bc
-       push    ix
-       ld      ix,#0
-       add     ix,sp
-
-       ld      c,8(ix)
-       ld      b,9(ix)
-       ld      e,10(ix)
-       ld      d,11(ix)
-       call    .mulu16
-       
-       pop     ix
-       pop     bc
-       pop     de
-       ret
-               
-.mul8:
-.mulu8:
-       LD      B,#0x00         ; Sign extend is not necessary with mul
-       LD      D,B
-       ; Fall through
+       ld      b,h
+       ld      c,l
        
        ;; 16-bit multiplication
        ;; 
@@ -63,16 +49,18 @@ __muluint::
 .mlp:
        SLA     E               ; Shift multiplier left 1 bit
        RL      D
-       JR      NC,.mlp1        ; Jump if MSB of multiplier = 0
+       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
-       JR      NZ,.mlp         ; Continue until count = 0
+       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:
-       RET                     ; HL = result
+                               ; HL = result
+       pop     bc
+       ret