struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / device / lib / ez80_z80 / __ultobcd.s
blob2dff644aaa1a6bdd3b36d69ff4a971e65d01cc29
1 ;--------------------------------------------------------------------------
2 ; __ultobcd.s
4 ; Copyright (C) 2020, Sergey Belyashov
6 ; This library is free software; you can redistribute it and/or modify it
7 ; under the terms of the GNU General Public License as published by the
8 ; Free Software Foundation; either version 2, or (at your option) any
9 ; later version.
11 ; This library is distributed in the hope that it will be useful,
12 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ; GNU General Public License for more details.
16 ; You should have received a copy of the GNU General Public License
17 ; along with this library; see the file COPYING. If not, write to the
18 ; Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
19 ; MA 02110-1301, USA.
21 ; As a special exception, if you link this library with other files,
22 ; some of which are compiled with SDCC, to produce an executable,
23 ; this library does not by itself cause the resulting executable to
24 ; be covered by the GNU General Public License. This exception does
25 ; not however invalidate any other reasons why the executable file
26 ; might be covered by the GNU General Public License.
27 ;--------------------------------------------------------------------------
29 .area _CODE
30 .ez80
32 .globl ___ultobcd
34 ; void __ultobcd (unsigned long v, unsigned char bcd[5])
35 ; __ultobcd converts v to BCD representation to the bcd.
36 ; bcd[] will contain BCD value.
38 ___ultobcd:
39 pop af
40 pop bc
41 push af
42 push bc
43 push ix
44 ld ix, #0
45 add ix, sp
46 ld sp, ix
48 ld bc, #0x2000
50 ;--- begin speed optimization
52 ld a, l
53 or a, h
54 jr NZ, 101$
55 ;high 2 bytes are zero
56 ld b, #0x10
57 ex de, hl
58 101$:
59 ld a, h
60 or a, a
61 jr NZ, 102$
62 ;high byte is zero
63 ld h, l
64 ld l, d
65 ld d, e
66 ld a, #-8
67 add a, b
68 ld b, a
69 102$:
70 push hl
71 push de
73 ;--- end speed optimization
75 ld hl, #0x0000
76 ld e, l
77 ld d, h
78 ; (ix+0)..(ix+3) - binary value
79 ; CDEHL - future BCD value
80 ; B - bits count (32)
81 103$:
82 sla -4 (ix)
83 rl -3 (ix)
84 rl -2 (ix)
85 rl -1 (ix)
86 ld a, l
87 adc a, a
88 daa
89 ld l, a
90 ld a, h
91 adc a, a
92 daa
93 ld h, a
94 ld a, e
95 adc a, a
96 daa
97 ld e, a
98 ld a, d
99 adc a, a
101 ld d, a
102 ld a, c
103 adc a, a
105 ld c, a
106 djnz 103$
108 ld b, l
109 ld a, h
110 ld sp, ix
111 pop ix
112 pop hl
113 ld (hl), b
114 inc hl
115 ld (hl), a
116 inc hl
117 ld (hl), e
118 inc hl
119 ld (hl), d
120 inc hl
121 ld (hl), c