Announce SDCC 4.5.0 RC1.
[sdcc.git] / sdcc / device / lib / z80 / __ultobcd.s
blobb3dc62b8ac9ac4f3efa482dadb7b8e8b59ae2836
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 .module __ultobcd
30 .optsdcc -mz80 sdcccall(1)
32 .area _CODE
34 .globl ___ultobcd
36 ; void __ultobcd (unsigned long v, unsigned char bcd[5])
37 ; __ultobcd converts v to BCD representation to the bcd.
38 ; bcd[] will contain BCD value.
40 ___ultobcd:
41 pop af
42 pop bc
43 push af
44 push bc
45 push ix
46 ld ix, #0
47 add ix, sp
48 ; ld sp, ix ; sp already equals ix
50 ld bc, #0x2000
52 ;--- begin speed optimization
54 ld a, l
55 or a, h
56 jr NZ, 101$
57 ;high 2 bytes are zero
58 ld b, #0x10
59 ex de, hl
60 101$:
61 ld a, h
62 or a, a
63 jr NZ, 102$
64 ;high byte is zero
65 ld h, l
66 ld l, d
67 ld d, e
68 ld a, #-8
69 add a, b
70 ld b, a
71 102$:
72 push hl
73 push de
75 ;--- end speed optimization
77 ld hl, #0x0000
78 ld e, l
79 ld d, h
80 ; (ix+0)..(ix+3) - binary value
81 ; CDEHL - future BCD value
82 ; B - bits count (32)
83 103$:
84 sla -4 (ix)
85 rl -3 (ix)
86 rl -2 (ix)
87 rl -1 (ix)
88 ld a, l
89 adc a, a
90 daa
91 ld l, a
92 ld a, h
93 adc a, a
94 daa
95 ld h, a
96 ld a, e
97 adc a, a
98 daa
99 ld e, a
100 ld a, d
101 adc a, a
103 ld d, a
104 ld a, c
105 adc a, a
107 ld c, a
108 djnz 103$
110 ld b, l
111 ld a, h
112 ld sp, ix
113 pop ix
114 pop hl
115 ld (hl), b
116 inc hl
117 ld (hl), a
118 inc hl
119 ld (hl), e
120 inc hl
121 ld (hl), d
122 inc hl
123 ld (hl), c