struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / device / lib / r800 / __ultobcd.s
blob37c34eafc230a60050636a3be6cb34c9abd3f1d6
1 ;--------------------------------------------------------------------------
2 ; __ultobcd.s
4 ; Copyright (C) 2020-2021, 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
31 .globl ___ultobcd
33 ; void __ultobcd (unsigned long v, unsigned char bcd[5])
34 ; __ultobcd converts v to BCD representation to the bcd.
35 ; bcd[] will contain BCD value.
37 ___ultobcd:
38 pop af
39 pop bc
40 push af
41 push bc
42 push ix
43 ld ix, #0
44 add ix, sp
45 ld sp, ix
47 ld bc, #0x2000
49 ;--- begin speed optimization
51 ld a, l
52 or a, h
53 jr NZ, 101$
54 ;high 2 bytes are zero
55 ld b, #0x10
56 ex de, hl
57 101$:
58 ld a, h
59 or a, a
60 jr NZ, 102$
61 ;high byte is zero
62 ld h, l
63 ld l, d
64 ld d, e
65 ld a, #-8
66 add a, b
67 ld b, a
68 102$:
69 push hl
70 push de
72 ;--- end speed optimization
74 ld hl, #0x0000
75 ld e, l
76 ld d, h
77 ; (ix+0)..(ix+3) - binary value
78 ; CDEHL - future BCD value
79 ; B - bits count (32)
80 103$:
81 sla -4 (ix)
82 rl -3 (ix)
83 rl -2 (ix)
84 rl -1 (ix)
85 ld a, l
86 adc a, a
87 daa
88 ld l, a
89 ld a, h
90 adc a, a
91 daa
92 ld h, a
93 ld a, e
94 adc a, a
95 daa
96 ld e, a
97 ld a, d
98 adc a, a
99 daa
100 ld d, a
101 ld a, c
102 adc a, a
104 ld c, a
105 djnz 103$
107 ld b, l
108 ld a, h
109 ld sp, ix
110 pop ix
111 pop hl
112 ld (hl), b
113 inc hl
114 ld (hl), a
115 inc hl
116 ld (hl), e
117 inc hl
118 ld (hl), d
119 inc hl
120 ld (hl), c