Imported Upstream version 2.9.0
[debian/cc1111] / device / lib / gbz80 / mul.s
1         ;; Originally from GBDK by Pascal Felber.
2
3         .area   _CODE
4
5 __mulschar_rrx_s::
6         ld      hl,#2
7         add     hl,sp
8
9         ld      e,(hl)
10         inc     hl
11         ld      l,(hl)
12
13         ;; Fall through
14 __mulschar_rrx_hds::
15         ;; Need to sign extend before going in.
16         ld      c,l
17
18         ld      a,l
19         rla
20         sbc     a,a
21         ld      b,a
22
23         ld      a,e
24         rla
25         sbc     a,a
26         ld      d,a
27
28         jp      .mul16
29
30 __muluchar_rrx_s::
31         ld      hl,#2
32         add     hl,sp
33
34         ld      e,(hl)
35
36         inc     hl
37         ld      c,(hl)
38
39         ;; Clear the top
40         xor     a
41         ld      d,a
42         ld      b,a
43
44         jp      .mul16
45
46 __mulint_rrx_s::
47         ld      hl,#2
48         add     hl,sp
49
50         ld      e,(hl)
51         inc     hl
52         ld      d,(hl)
53         inc     hl
54         ld      a,(hl)
55         inc     hl
56         ld      h,(hl)
57         ld      l,a
58
59         ;; Fall through
60
61 __muluchar_rrx_hds::
62 __mulint_rrx_hds::
63         ;; Parameters:
64         ;;      HL, DE (left, right irrelivent)
65         ld      b,h
66         ld      c,l
67
68         ;; 16-bit multiplication
69         ;;
70         ;; Entry conditions
71         ;;   BC = multiplicand
72         ;;   DE = multiplier
73         ;;
74         ;; Exit conditions
75         ;;   DE = less significant word of product
76         ;;
77         ;; Register used: AF,BC,DE,HL
78 .mul16:
79         ld      hl,#0
80         ld      a,b
81         ; ld c,c
82         ld      b,#16
83
84         ;; Optimise for the case when this side has 8 bits of data or
85         ;; less.  This is often the case with support address calls.
86         or      a
87         jp      NZ,1$
88
89         ld      b,#8
90         ld      a,c
91 1$:
92         ;; Taken from z88dk, which originally borrowed from the
93         ;; Spectrum rom.
94         add     hl,hl
95         rl      c
96         rla                     ;DLE 27/11/98
97         jr      NZ,2$
98         add     hl,de
99 2$:
100         dec     b
101         jr      NZ,1$
102
103         ;; Return in DE
104         ld      e,l
105         ld      d,h
106
107         ret