z80 library cleanup
[fw/sdcc] / device / lib / z80 / mul.s
1         .area   _CODE
2
3 ; This multiplication routine is similar to the one
4 ; from Rodnay Zaks, "Programming the Z80".
5
6 ; Now replaced by a builtin for code generation, but
7 ; still called from some asm files in this directory.
8 __muluchar_rrx_s::
9         ld      hl, #2+1
10         add     hl, sp
11         ld      e, (hl)
12         dec     hl
13         ld      h, (hl)
14         ld      l, #0
15         ld      d, l
16         ld      b, #8
17 muluchar_rrx_s_loop:
18         add     hl, hl
19         jr      nc, muluchar_rrx_s_noadd
20         add     hl, de
21 muluchar_rrx_s_noadd:
22         djnz    muluchar_rrx_s_loop
23         ret
24
25 ; operands have different sign
26
27 __mulsuchar_rrx_s::
28         ld      hl,#2+1
29         add     hl,sp
30
31         ld      e,(hl)
32         dec     hl
33         ld      c,(hl)
34         ld      b, #0
35         jr      signexte
36
37 __muluschar_rrx_s::
38         ld      hl,#2+1
39         add     hl,sp
40
41         ld      c,(hl)
42         ld      b, #0
43         dec     hl
44         ld      e,(hl)
45         jr      signexte
46
47 ;; Originally from GBDK by Pascal Felber.
48
49 __mulschar_rrx_s::
50         ld      hl,#2+1
51         add     hl,sp
52
53         ld      e,(hl)
54         dec     hl
55         ld      l,(hl)
56
57         ;; Fall through
58 __mulschar_rrx_hds::
59         ;; Need to sign extend before going in.
60         ld      c,l
61
62         ld      a,l
63         rla
64         sbc     a,a
65         ld      b,a
66 signexte:
67         ld      a,e
68         rla
69         sbc     a,a
70         ld      d,a
71
72         jr      .mul16
73
74 __mulint_rrx_s::
75         ld      hl,#2
76         add     hl,sp
77
78         ld      e,(hl)
79         inc     hl
80         ld      d,(hl)
81         inc     hl
82         ld      a,(hl)
83         inc     hl
84         ld      h,(hl)
85         ld      l,a
86
87         ;; Fall through
88
89 __muluchar_rrx_hds::
90 __mulint_rrx_hds::
91         ;; Parameters:
92         ;;      HL, DE (left, right irrelevant)
93         ld      b,h
94         ld      c,l
95
96         ;; 16-bit multiplication
97         ;;
98         ;; Entry conditions
99         ;;   BC = multiplicand
100         ;;   DE = multiplier
101         ;;
102         ;; Exit conditions
103         ;;   DE = less significant word of product
104         ;;
105         ;; Register used: AF,BC,DE,HL
106 .mul16:
107         ld      hl,#0
108         ld      a,b
109         ; ld c,c
110         ld      b,#16
111
112         ;; Optimise for the case when this side has 8 bits of data or
113         ;; less.  This is often the case with support address calls.
114         or      a
115         jr      NZ,1$
116
117         ld      b,#8
118         ld      a,c
119 1$:
120         ;; Taken from z88dk, which originally borrowed from the
121         ;; Spectrum rom.
122         add     hl,hl
123         rl      c
124         rla                     ;DLE 27/11/98
125         jr      NC,2$
126         add     hl,de
127 2$:
128         djnz    1$
129         ret