cscg22-gearboy

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

memcpy.s (2249B)


      1;--------------------------------------------------------------------------
      2;  memcpy.s
      3;
      4;  Copies in groups of four. Algorithm is Duff's device.
      5;
      6;  Copyright (c) 2021, Philipp Klaus Krause
      7;  Copyright (c) 2022, Sebastian 'basxto' Riedel <sdcc@basxto.de>
      8;
      9;  This library is free software; you can redistribute it and/or modify it
     10;  under the terms of the GNU General Public License as published by the
     11;  Free Software Foundation; either version 2, or (at your option) any
     12;  later version.
     13;
     14;  This library is distributed in the hope that it will be useful,
     15;  but WITHOUT ANY WARRANTY; without even the implied warranty of
     16;  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     17;  GNU General Public License for more details.
     18;
     19;  You should have received a copy of the GNU General Public License 
     20;  along with this library; see the file COPYING. If not, write to the
     21;  Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
     22;   MA 02110-1301, USA.
     23;
     24;  As a special exception, if you link this library with other files,
     25;  some of which are compiled with SDCC, to produce an executable,
     26;  this library does not by itself cause the resulting executable to
     27;  be covered by the GNU General Public License. This exception does
     28;  not however invalidate any other reasons why the executable file
     29;   might be covered by the GNU General Public License.
     30;--------------------------------------------------------------------------
     31
     32	.area   _CODE
     33
     34	.globl	_memcpy
     35	.globl	___memcpy
     36
     37	; gets called by memmove for (s >= d)
     38	; bigger than the old algorithm but faster for n>=5
     39_memcpy::
     40___memcpy::
     41	;dest in de
     42	;src in bc
     43	;n in sp+2,sp+3
     44	push	de
     45	ldhl	sp, #5
     46	ld	a, (hl-)
     47	ld	l, (hl)
     48	ld	h, b
     49	ld	b, a
     50	ld	a, l
     51	ld	l, c
     52	srl	b
     53	rra
     54	ld	c, a
     55	;dest in de (backup in sp+0,sp+1)
     56	;src in hl
     57	;n/2 in bc
     58	;LSB of bc in carry
     59	jr nc, skip_one
     60	ld	a, (hl+)
     61	ld	(de), a
     62	inc	de
     63skip_one:
     64	;n/2 in bc
     65	;shift second LSB to carry
     66	srl	b
     67	rr	c
     68	;n/4 in bc
     69	inc	b
     70	inc	c
     71	jr nc, test
     72	jr	copy_two
     73.irp	idx,copy_four,copy_two
     74	idx:
     75	.rept	2
     76		ld	a, (hl+)
     77		ld	(de), a
     78		inc	de
     79	.endm
     80.endm
     81test:
     82.irp	idx,c,b
     83	dec	idx
     84	jr	nz, copy_four
     85.endm
     86
     87end:
     88	;restore dest
     89	pop	bc
     90	;get return address
     91	pop	hl
     92	;throw away n
     93	pop	af
     94	jp	(hl)