cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

__ultobcd.s (2451B)


      1;--------------------------------------------------------------------------
      2;  __ultobcd.s
      3;
      4;  Copyright (C) 2020, Sergey Belyashov
      5;
      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.
     10;
     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.
     15;
     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.
     20;
     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;--------------------------------------------------------------------------
     28
     29	.area   _CODE
     30
     31	.globl ___ultobcd
     32;
     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.
     36;
     37___ultobcd:
     38	push	ix
     39	ld	ix, #0
     40	add	ix, sp
     41	ld	bc, #0x2000
     42;
     43;--- begin speed optimization
     44;
     45	ld	l, 4 (ix)
     46	ld	h, 5 (ix)
     47	ld	e, 6 (ix)
     48	ld	d, 7 (ix)
     49	ld	a, e
     50	or	a, d
     51	jr	NZ, 101$
     52;high 2 bytes are zero
     53	ld	b, #0x10
     54	ex	de, hl
     55101$:
     56	ld	a, d
     57	or	a, a
     58	jr	NZ, 102$
     59;high byte is zero
     60	ld	d, e
     61	ld	e, h
     62	ld	h, l
     63	ld	a, #-8
     64	add	a, b
     65	ld	b, a
     66102$:
     67	ld	4 (ix), l
     68	ld	5 (ix), h
     69	ld	6 (ix), e
     70	ld	7 (ix), d
     71;
     72;--- end speed optimization
     73;
     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)
     80103$:
     81	sla	4 (ix)
     82	rl	5 (ix)
     83	rl	6 (ix)
     84	rl	7 (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
    103	daa
    104	ld	c, a
    105	djnz	103$
    106;
    107	ld	b, l
    108	ld	a, h
    109	ld	l, 8 (ix)
    110	ld	h, 9 (ix)
    111	ld	(hl), b
    112	inc	hl
    113	ld	(hl), a
    114	inc	hl
    115	ld	(hl), e
    116	inc	hl
    117	ld	(hl), d
    118	inc	hl
    119	ld	(hl), c
    120;
    121	pop	ix
    122	ret