diff options
| author | Xavier ASUS <xavi92psx@gmail.com> | 2019-10-18 00:31:54 +0200 |
|---|---|---|
| committer | Xavier ASUS <xavi92psx@gmail.com> | 2019-10-18 00:31:54 +0200 |
| commit | 268a53de823a6750d6256ee1fb1e7707b4b45740 (patch) | |
| tree | 42c1799a9a82b2f7d9790ee9fe181d72a7274751 /device/lib/z80 | |
| download | sdcc-gas-268a53de823a6750d6256ee1fb1e7707b4b45740.tar.gz | |
sdcc-3.9.0 fork implementing GNU assembler syntax
This fork aims to provide better support for stm8-binutils
Diffstat (limited to 'device/lib/z80')
| -rw-r--r-- | device/lib/z80/Makefile | 96 | ||||
| -rw-r--r-- | device/lib/z80/Makefile.in | 96 | ||||
| -rw-r--r-- | device/lib/z80/README | 9 | ||||
| -rw-r--r-- | device/lib/z80/__sdcc_call_hl.s | 37 | ||||
| -rw-r--r-- | device/lib/z80/__sdcc_call_iy.s | 37 | ||||
| -rw-r--r-- | device/lib/z80/abs.s | 61 | ||||
| -rw-r--r-- | device/lib/z80/crt0.s | 110 | ||||
| -rw-r--r-- | device/lib/z80/crtenter.s | 41 | ||||
| -rw-r--r-- | device/lib/z80/divmixed.s | 58 | ||||
| -rw-r--r-- | device/lib/z80/divsigned.s | 137 | ||||
| -rw-r--r-- | device/lib/z80/divunsigned.s | 143 | ||||
| -rw-r--r-- | device/lib/z80/heap.s | 44 | ||||
| -rw-r--r-- | device/lib/z80/memmove.s | 65 | ||||
| -rw-r--r-- | device/lib/z80/modmixed.s | 62 | ||||
| -rw-r--r-- | device/lib/z80/modsigned.s | 57 | ||||
| -rw-r--r-- | device/lib/z80/modunsigned.s | 61 | ||||
| -rw-r--r-- | device/lib/z80/mul.s | 74 | ||||
| -rw-r--r-- | device/lib/z80/mulchar.s | 79 | ||||
| -rw-r--r-- | device/lib/z80/setjmp.s | 95 | ||||
| -rw-r--r-- | device/lib/z80/strcpy.s | 48 | ||||
| -rw-r--r-- | device/lib/z80/strlen.s | 47 |
21 files changed, 1457 insertions, 0 deletions
diff --git a/device/lib/z80/Makefile b/device/lib/z80/Makefile new file mode 100644 index 0000000..bad6bd1 --- /dev/null +++ b/device/lib/z80/Makefile @@ -0,0 +1,96 @@ +# libc/z80 Makefile + + +srcdir = . +top_builddir = ../../.. + +LIB_TYPE = RANLIB + +SCC = $(top_builddir)/bin/sdcc -mz80 +SAS = $(top_builddir)/bin/sdasz80 + +# override PORTDIR defined by super (parent) makefile +override PORTDIR = ../build/z80 + +include $(srcdir)/../incl.mk + +Z80_FLOAT = $(COMMON_FLOAT) + +Z80_INT = $(COMMON_INT) + +Z80_LONG = $(COMMON_LONG) \ + _divulong.c \ + _mullong.c + +Z80_LONGLONG = $(COMMON_LONGLONG) \ + _mullonglong.c \ + _divslonglong.c \ + _divulonglong.c \ + _modslonglong.c \ + _modulonglong.c + +Z80_SDCC = $(COMMON_SDCC) \ + _itoa.c \ + _ltoa.c \ + _startup.c \ + sprintf.c \ + vprintf.c \ + _strcmp.c \ + __memcpy.c \ + memcpy.c + +Z80SOURCES = $(addprefix ../,$(Z80_FLOAT) $(Z80_INT) $(Z80_LONG) $(Z80_LONGLONG) $(Z80_SDCC)) +Z8OBJECTS = $(patsubst %.c,%.rel,$(Z80_FLOAT) $(Z80_INT) $(Z80_LONG) $(Z80_LONGLONG) $(Z80_SDCC)) + +OBJ = divunsigned.rel divsigned.rel divmixed.rel modunsigned.rel modsigned.rel modmixed.rel mul.rel mulchar.rel \ + heap.rel memmove.rel strcpy.rel strlen.rel abs.rel __sdcc_call_hl.rel __sdcc_call_iy.rel crtenter.rel \ + setjmp.rel + +LIB = z80.lib +CC = $(SCC) +AS = $(SAS) +ASFLAGS = -plosgff + +CFLAGS = -I$(srcdir)/../../include -I. --std-c11 + +all: $(PORTDIR)/$(LIB) $(PORTDIR)/crt0.rel + +$(PORTDIR)/crt0.rel: crt0.rel + cp crt0.rel $(PORTDIR)/crt0.rel + +$(PORTDIR)/$(LIB): $(OBJ) $(Z8OBJECTS) Makefile +ifeq ($(LIB_TYPE), SDCCLIB) + rm -f $@; \ + $(top_builddir)/bin/sdcclib -a $@ $(OBJ) $(Z8OBJECTS) +else + ifeq ($(LIB_TYPE), AR) + $(top_builddir)/bin/sdar -rcSD $@ $(OBJ) $(Z8OBJECTS) + else + ifeq ($(LIB_TYPE), RANLIB) + $(top_builddir)/bin/sdar -rcD $@ $(OBJ) $(Z8OBJECTS) + else + rm -f $@; \ + for i in $(basename $(OBJ) $(Z8OBJECTS)); do echo $$i >>$@; done + cp $(OBJ) $(Z8OBJECTS) $(PORTDIR) + endif + endif +endif + +%.rel: %.c + $(CC) $(CFLAGS) -c $< + +%.rel: %.s + @# TODO: sdas should place it\'s output in the current dir + test $(srcdir) = . || cp $< . + -$(AS) $(ASFLAGS) $(notdir $<) + test $(srcdir) = . || rm $(notdir $<) + +%.rel: ../%.c + $(CC) $(CFLAGS) -c $< + +clean: + rm -f *.rel *.sym *.lst *~ $(CLEANSPEC) *.dump* *.asm *.lib + +distclean: clean + rm -f Makefile + diff --git a/device/lib/z80/Makefile.in b/device/lib/z80/Makefile.in new file mode 100644 index 0000000..e14d779 --- /dev/null +++ b/device/lib/z80/Makefile.in @@ -0,0 +1,96 @@ +# libc/z80 Makefile + +VPATH = @srcdir@ +srcdir = @srcdir@ +top_builddir = @top_builddir@ + +LIB_TYPE = @LIB_TYPE@ + +SCC = $(top_builddir)/bin/sdcc -mz80 +SAS = $(top_builddir)/bin/sdasz80 + +# override PORTDIR defined by super (parent) makefile +override PORTDIR = ../build/z80 + +include $(srcdir)/../incl.mk + +Z80_FLOAT = $(COMMON_FLOAT) + +Z80_INT = $(COMMON_INT) + +Z80_LONG = $(COMMON_LONG) \ + _divulong.c \ + _mullong.c + +Z80_LONGLONG = $(COMMON_LONGLONG) \ + _mullonglong.c \ + _divslonglong.c \ + _divulonglong.c \ + _modslonglong.c \ + _modulonglong.c + +Z80_SDCC = $(COMMON_SDCC) \ + _itoa.c \ + _ltoa.c \ + _startup.c \ + sprintf.c \ + vprintf.c \ + _strcmp.c \ + __memcpy.c \ + memcpy.c + +Z80SOURCES = $(addprefix ../,$(Z80_FLOAT) $(Z80_INT) $(Z80_LONG) $(Z80_LONGLONG) $(Z80_SDCC)) +Z8OBJECTS = $(patsubst %.c,%.rel,$(Z80_FLOAT) $(Z80_INT) $(Z80_LONG) $(Z80_LONGLONG) $(Z80_SDCC)) + +OBJ = divunsigned.rel divsigned.rel divmixed.rel modunsigned.rel modsigned.rel modmixed.rel mul.rel mulchar.rel \ + heap.rel memmove.rel strcpy.rel strlen.rel abs.rel __sdcc_call_hl.rel __sdcc_call_iy.rel crtenter.rel \ + setjmp.rel + +LIB = z80.lib +CC = $(SCC) +AS = $(SAS) +ASFLAGS = -plosgff + +CFLAGS = -I$(srcdir)/../../include -I. --std-c11 + +all: $(PORTDIR)/$(LIB) $(PORTDIR)/crt0.rel + +$(PORTDIR)/crt0.rel: crt0.rel + cp crt0.rel $(PORTDIR)/crt0.rel + +$(PORTDIR)/$(LIB): $(OBJ) $(Z8OBJECTS) Makefile +ifeq ($(LIB_TYPE), SDCCLIB) + rm -f $@; \ + $(top_builddir)/bin/sdcclib -a $@ $(OBJ) $(Z8OBJECTS) +else + ifeq ($(LIB_TYPE), AR) + $(top_builddir)/bin/sdar -rcSD $@ $(OBJ) $(Z8OBJECTS) + else + ifeq ($(LIB_TYPE), RANLIB) + $(top_builddir)/bin/sdar -rcD $@ $(OBJ) $(Z8OBJECTS) + else + rm -f $@; \ + for i in $(basename $(OBJ) $(Z8OBJECTS)); do echo $$i >>$@; done + cp $(OBJ) $(Z8OBJECTS) $(PORTDIR) + endif + endif +endif + +%.rel: %.c + $(CC) $(CFLAGS) -c $< + +%.rel: %.s + @# TODO: sdas should place it\'s output in the current dir + test $(srcdir) = . || cp $< . + -$(AS) $(ASFLAGS) $(notdir $<) + test $(srcdir) = . || rm $(notdir $<) + +%.rel: ../%.c + $(CC) $(CFLAGS) -c $< + +clean: + rm -f *.rel *.sym *.lst *~ $(CLEANSPEC) *.dump* *.asm *.lib + +distclean: clean + rm -f Makefile + diff --git a/device/lib/z80/README b/device/lib/z80/README new file mode 100644 index 0000000..259ca6d --- /dev/null +++ b/device/lib/z80/README @@ -0,0 +1,9 @@ +sdcc/device/lib/z80 +------------------- + +Z80 specific routines. + +Notes: +* Cost of ld r,(ix+n): 19 +* Cost of ld r,(hl); inc hl: 7+6 = 13 and you don't have to pop ix +
\ No newline at end of file diff --git a/device/lib/z80/__sdcc_call_hl.s b/device/lib/z80/__sdcc_call_hl.s new file mode 100644 index 0000000..9086c7b --- /dev/null +++ b/device/lib/z80/__sdcc_call_hl.s @@ -0,0 +1,37 @@ +;-------------------------------------------------------------------------- +; crtcall.s +; +; Copyright (C) 2011, Maarten Brock +; +; This library is free software; you can redistribute it and/or modify it +; under the terms of the GNU General Public License as published by the +; Free Software Foundation; either version 2, or (at your option) any +; later version. +; +; This library is distributed in the hope that it will be useful, +; but WITHOUT ANY WARRANTY; without even the implied warranty of +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +; GNU General Public License for more details. +; +; You should have received a copy of the GNU General Public License +; along with this library; see the file COPYING. If not, write to the +; Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, +; MA 02110-1301, USA. +; +; As a special exception, if you link this library with other files, +; some of which are compiled with SDCC, to produce an executable, +; this library does not by itself cause the resulting executable to +; be covered by the GNU General Public License. This exception does +; not however invalidate any other reasons why the executable file +; might be covered by the GNU General Public License. +;-------------------------------------------------------------------------- + + .area _CODE + + .globl ___sdcc_call_hl + +; The Z80 has the jp (hl) instruction, which is perfect for implementing function pointers. + +___sdcc_call_hl: + jp (hl) + diff --git a/device/lib/z80/__sdcc_call_iy.s b/device/lib/z80/__sdcc_call_iy.s new file mode 100644 index 0000000..8941745 --- /dev/null +++ b/device/lib/z80/__sdcc_call_iy.s @@ -0,0 +1,37 @@ +;-------------------------------------------------------------------------- +; crtcall.s +; +; Copyright (C) 2011, Maarten Brock, 2015 Philipp Klaus Krause +; +; This library is free software; you can redistribute it and/or modify it +; under the terms of the GNU General Public License as published by the +; Free Software Foundation; either version 2, or (at your option) any +; later version. +; +; This library is distributed in the hope that it will be useful, +; but WITHOUT ANY WARRANTY; without even the implied warranty of +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +; GNU General Public License for more details. +; +; You should have received a copy of the GNU General Public License +; along with this library; see the file COPYING. If not, write to the +; Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, +; MA 02110-1301, USA. +; +; As a special exception, if you link this library with other files, +; some of which are compiled with SDCC, to produce an executable, +; this library does not by itself cause the resulting executable to +; be covered by the GNU General Public License. This exception does +; not however invalidate any other reasons why the executable file +; might be covered by the GNU General Public License. +;-------------------------------------------------------------------------- + + .area _CODE + + .globl ___sdcc_call_iy + +; The Z80 has the jp (iy) instruction, which is perfect for implementing function pointers. + +___sdcc_call_iy: + jp (iy) + diff --git a/device/lib/z80/abs.s b/device/lib/z80/abs.s new file mode 100644 index 0000000..2aaa541 --- /dev/null +++ b/device/lib/z80/abs.s @@ -0,0 +1,61 @@ +;-------------------------------------------------------------------------- +; abs.s +; +; Copyright (C) 2010, Philipp Klaus Krause +; +; This library is free software; you can redistribute it and/or modify it +; under the terms of the GNU General Public License as published by the +; Free Software Foundation; either version 2, or (at your option) any +; later version. +; +; This library is distributed in the hope that it will be useful, +; but WITHOUT ANY WARRANTY; without even the implied warranty of +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +; GNU General Public License for more details. +; +; You should have received a copy of the GNU General Public License +; along with this library; see the file COPYING. If not, write to the +; Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, +; MA 02110-1301, USA. +; +; As a special exception, if you link this library with other files, +; some of which are compiled with SDCC, to produce an executable, +; this library does not by itself cause the resulting executable to +; be covered by the GNU General Public License. This exception does +; not however invalidate any other reasons why the executable file +; might be covered by the GNU General Public License. +;-------------------------------------------------------------------------- + + .area _CODE + + .globl _abs + +; 12B; 86T for nonnegative arguments, 78T for negative. +_abs: + pop hl + pop de + push de + push hl + xor a, a + ld l, a + ld h, a + sbc hl, de + ret P + ex de, hl + ret + +; 14B; 59T for nonegative arguments, 94T for negative: +;_abs: +; pop de +; pop hl +; push hl +; push de +; bit 7, h +; ret Z +; xor a, a +; ld e, a +; ld d, a +; ex de, hl +; sbc hl, de +; ret + diff --git a/device/lib/z80/crt0.s b/device/lib/z80/crt0.s new file mode 100644 index 0000000..eb97ce2 --- /dev/null +++ b/device/lib/z80/crt0.s @@ -0,0 +1,110 @@ +;-------------------------------------------------------------------------- +; crt0.s - Generic crt0.s for a Z80 +; +; Copyright (C) 2000, Michael Hope +; +; This library is free software; you can redistribute it and/or modify it +; under the terms of the GNU General Public License as published by the +; Free Software Foundation; either version 2, or (at your option) any +; later version. +; +; This library is distributed in the hope that it will be useful, +; but WITHOUT ANY WARRANTY; without even the implied warranty of +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +; GNU General Public License for more details. +; +; You should have received a copy of the GNU General Public License +; along with this library; see the file COPYING. If not, write to the +; Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, +; MA 02110-1301, USA. +; +; As a special exception, if you link this library with other files, +; some of which are compiled with SDCC, to produce an executable, +; this library does not by itself cause the resulting executable to +; be covered by the GNU General Public License. This exception does +; not however invalidate any other reasons why the executable file +; might be covered by the GNU General Public License. +;-------------------------------------------------------------------------- + + .module crt0 + .globl _main + + .area _HEADER (ABS) + ;; Reset vector + .org 0 + jp init + + .org 0x08 + ei + reti + ei + .org 0x10 + ei + reti + .org 0x18 + ei + reti + .org 0x20 + ei + reti + .org 0x28 + ei + reti + .org 0x30 + ei + reti + .org 0x38 + ei + reti + + .org 0x100 +init: + ;; Set stack pointer directly above top of memory. + ld sp,#0x0000 + + ;; Initialise global variables + call gsinit + call _main + jp _exit + + ;; Ordering of segments for the linker. + .area _HOME + .area _CODE + .area _INITIALIZER + .area _GSINIT + .area _GSFINAL + + .area _DATA + .area _INITIALIZED + .area _BSEG + .area _BSS + .area _HEAP + + .area _CODE +__clock:: + ld a,#2 + rst 0x08 + ret + +_exit:: + ;; Exit - special code to the emulator + ld a,#0 + rst 0x08 +1$: + halt + jr 1$ + + .area _GSINIT +gsinit:: + ld bc, #l__INITIALIZER + ld a, b + or a, c + jr Z, gsinit_next + ld de, #s__INITIALIZED + ld hl, #s__INITIALIZER + ldir +gsinit_next: + + .area _GSFINAL + ret + diff --git a/device/lib/z80/crtenter.s b/device/lib/z80/crtenter.s new file mode 100644 index 0000000..0be714a --- /dev/null +++ b/device/lib/z80/crtenter.s @@ -0,0 +1,41 @@ +;-------------------------------------------------------------------------- +; crtenter.s +; +; Copyright (C) 2015, Alan Cox, Philipp Klaus Krause +; +; This library is free software; you can redistribute it and/or modify it +; under the terms of the GNU General Public License as published by the +; Free Software Foundation; either version 2, or (at your option) any +; later version. +; +; This library is distributed in the hope that it will be useful, +; but WITHOUT ANY WARRANTY; without even the implied warranty of +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +; GNU General Public License for more details. +; +; You should have received a copy of the GNU General Public License +; along with this library; see the file COPYING. If not, write to the +; Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, +; MA 02110-1301, USA. +; +; As a special exception, if you link this library with other files, +; some of which are compiled with SDCC, to produce an executable, +; this library does not by itself cause the resulting executable to +; be covered by the GNU General Public License. This exception does +; not however invalidate any other reasons why the executable file +; might be covered by the GNU General Public License. +;-------------------------------------------------------------------------- + + .area _CODE + + .globl ___sdcc_enter_ix + +; Factor out some start of function code to reduce code size + +___sdcc_enter_ix: + pop hl ; return address + push ix ; save frame pointer + ld ix, #0 + add ix, sp ; set ix to the stack frame + jp (hl) ; and return + diff --git a/device/lib/z80/divmixed.s b/device/lib/z80/divmixed.s new file mode 100644 index 0000000..f7c983c --- /dev/null +++ b/device/lib/z80/divmixed.s @@ -0,0 +1,58 @@ +;-------------------------------------------------------------------------- +; divmixed.s +; +; Copyright (C) 2010, Philipp Klaus Krause +; +; This library is free software; you can redistribute it and/or modify it +; under the terms of the GNU General Public License as published by the +; Free Software Foundation; either version 2, or (at your option) any +; later version. +; +; This library is distributed in the hope that it will be useful, +; but WITHOUT ANY WARRANTY; without even the implied warranty of +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +; GNU General Public License for more details. +; +; You should have received a copy of the GNU General Public License +; along with this library; see the file COPYING. If not, write to the +; Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, +; MA 02110-1301, USA. +; +; As a special exception, if you link this library with other files, +; some of which are compiled with SDCC, to produce an executable, +; this library does not by itself cause the resulting executable to +; be covered by the GNU General Public License. This exception does +; not however invalidate any other reasons why the executable file +; might be covered by the GNU General Public License. +;-------------------------------------------------------------------------- + +.globl __divsuchar +.globl __divuschar + +__divsuchar: + ld hl, #2+1 + add hl, sp + + ld e, (hl) + dec hl + ld l, (hl) + ld h, #0 + + jp __div_signexte + +__divuschar: + ld hl, #2+1 + ld d, h + add hl, sp + + ld e, (hl) + dec hl + ld l, (hl) + + ld a, l ; Sign extend + rlca + sbc a, a + ld h, a + + jp __div16 + diff --git a/device/lib/z80/divsigned.s b/device/lib/z80/divsigned.s new file mode 100644 index 0000000..1ebc729 --- /dev/null +++ b/device/lib/z80/divsigned.s @@ -0,0 +1,137 @@ +;-------------------------------------------------------------------------- +; divsigned.s +; +; Copyright (C) 2000-2010, Michael Hope, Philipp Klaus Krause +; +; This library is free software; you can redistribute it and/or modify it +; under the terms of the GNU General Public License as published by the +; Free Software Foundation; either version 2, or (at your option) any +; later version. +; +; This library is distributed in the hope that it will be useful, +; but WITHOUT ANY WARRANTY; without even the implied warranty of +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +; GNU General Public License for more details. +; +; You should have received a copy of the GNU General Public License +; along with this library; see the file COPYING. If not, write to the +; Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, +; MA 02110-1301, USA. +; +; As a special exception, if you link this library with other files, +; some of which are compiled with SDCC, to produce an executable, +; this library does not by itself cause the resulting executable to +; be covered by the GNU General Public License. This exception does +; not however invalidate any other reasons why the executable file +; might be covered by the GNU General Public License. +;-------------------------------------------------------------------------- + +.area _CODE + +.globl __divsint +.globl __divschar + +__divsint: + pop af + pop hl + pop de + push de + push hl + push af + + jp __div16 + +__divschar: + ld hl, #2+1 + add hl, sp + + ld e, (hl) + dec hl + ld l, (hl) + +__div8:: + ld a, l ; Sign extend + rlca + sbc a,a + ld h, a +__div_signexte:: + ld a, e ; Sign extend + rlca + sbc a,a + ld d, a + ; Fall through to __div16 + + ;; signed 16-bit division + ;; + ;; Entry conditions + ;; HL = dividend + ;; DE = divisor + ;; + ;; Exit conditions + ;; HL = quotient + ;; DE = remainder + ;; + ;; Register used: AF,B,DE,HL +__div16:: + ;; Determine sign of quotient by xor-ing high bytes of dividend + ;; and divisor. Quotient is positive if signs are the same, negative + ;; if signs are different + ;; Remainder has same sign as dividend + ld a, h ; Get high byte of dividend + xor a, d ; Xor with high byte of divisor + rla ; Sign of quotient goes into the carry + ld a, h ; Get high byte of dividend + push af ; Save sign of both quotient and reminder + + ; Take absolute value of dividend + rla + jr NC, .chkde ; Jump if dividend is positive + sub a, a ; Substract dividend from 0 + sub a, l + ld l, a + sbc a, a ; Propagate borrow (A=0xFF if borrow) + sub a, h + ld h, a + + ; Take absolute value of divisor +.chkde: + bit 7, d + jr Z, .dodiv ; Jump if divisor is positive + sub a, a ; Subtract divisor from 0 + sub a, e + ld e, a + sbc a, a ; Propagate borrow (A=0xFF if borrow) + sub a, d + ld d, a + + ; Divide absolute values +.dodiv: + call __divu16 + +.fix_quotient: + ; Negate quotient if it is negative + pop af ; recover sign of quotient + ret NC ; Jump if quotient is positive + ld b, a + sub a, a ; Subtract quotient from 0 + sub a, l + ld l, a + sbc a, a ; Propagate borrow (A=0xFF if borrow) + sub a, h + ld h, a + ld a, b + ret + +__get_remainder:: + ; Negate remainder if it is negative and move it into hl + rla + ex de, hl + ret NC ; Return if remainder is positive + sub a, a ; Subtract remainder from 0 + sub a, l + ld l, a + sbc a, a ; Propagate remainder (A=0xFF if borrow) + sub a, h + ld h, a + ret + diff --git a/device/lib/z80/divunsigned.s b/device/lib/z80/divunsigned.s new file mode 100644 index 0000000..15f209a --- /dev/null +++ b/device/lib/z80/divunsigned.s @@ -0,0 +1,143 @@ +;-------------------------------------------------------------------------- +; divunsigned.s +; +; Copyright (C) 2000-2012, Michael Hope, Philipp Klaus Krause, Marco Bodrato +; +; This library is free software; you can redistribute it and/or modify it +; under the terms of the GNU General Public License as published by the +; Free Software Foundation; either version 2, or (at your option) any +; later version. +; +; This library is distributed in the hope that it will be useful, +; but WITHOUT ANY WARRANTY; without even the implied warranty of +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +; GNU General Public License for more details. +; +; You should have received a copy of the GNU General Public License +; along with this library; see the file COPYING. If not, write to the +; Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, +; MA 02110-1301, USA. +; +; As a special exception, if you link this library with other files, +; some of which are compiled with SDCC, to produce an executable, +; this library does not by itself cause the resulting executable to +; be covered by the GNU General Public License. This exception does +; not however invalidate any other reasons why the executable file +; might be covered by the GNU General Public License. +;-------------------------------------------------------------------------- + + ;; Originally from GBDK by Pascal Felber. + +.area _CODE + +.globl __divuint +.globl __divuchar + +__divuint: + pop af + pop hl + pop de + push de + push hl + push af + + jr __divu16 + +__divuchar: + ld hl,#2+1 + add hl,sp + + ld e,(hl) + dec hl + ld l,(hl) + + ;; Fall through +__divu8:: + ld h,#0x00 + ld d,h + ; Fall through to __divu16 + + ;; unsigned 16-bit division + ;; + ;; Entry conditions + ;; HL = dividend + ;; DE = divisor + ;; + ;; Exit conditions + ;; HL = quotient + ;; DE = remainder + ;; carry = 0 + ;; If divisor is 0, quotient is set to "infinity", i.e HL = 0xFFFF. + ;; + ;; Register used: AF,B,DE,HL +__divu16:: + ;; Two algorithms: one assumes divisor <2^7, the second + ;; assumes divisor >=2^7; choose the applicable one. + ld a,e + and a,#0x80 + or a,d + jr NZ,.morethan7bits + ;; Both algorithms "rotate" 24 bits (H,L,A) but roles change. + + ;; unsigned 16/7-bit division +.atmost7bits: + ld b,#16 ; bits in dividend and possible quotient + ;; Carry cleared by AND/OR, this "0" bit will pass trough HL.[*] + adc hl,hl +.dvloop7: + ;; HL holds both dividend and quotient. While we shift a bit from + ;; MSB of dividend, we shift next bit of quotient in from carry. + ;; A holds remainder. + rla + + ;; If remainder is >= divisor, next bit of quotient is 1. We try + ;; to compute the difference. + sub a,e + jr NC,.nodrop7 ; Jump if remainder is >= dividend + add a,e ; Otherwise, restore remainder + ;; The add above sets the carry, because sbc a,e did set it. +.nodrop7: + ccf ; Complement borrow so 1 indicates a + ; successful substraction (this is the + ; next bit of quotient) + adc hl,hl + djnz .dvloop7 + ;; Carry now contains the same value it contained before + ;; entering .dvloop7[*]: "0" = valid result. + ld e,a ; DE = remainder, HL = quotient + ret + +.morethan7bits: + ld b,#9 ; at most 9 bits in quotient. + ld a,l ; precompute the first 7 shifts, by + ld l,h ; doing 8 + ld h,#0 + rr l ; undoing 1 +.dvloop: + ;; Shift next bit of quotient into bit 0 of dividend + ;; Shift next MSB of dividend into LSB of remainder + ;; A holds both dividend and quotient. While we shift a bit from + ;; MSB of dividend, we shift next bit of quotient in from carry + ;; HL holds remainder + adc hl,hl ; HL < 2^(7+9), no carry, ever. + + ;; If remainder is >= divisor, next bit of quotient is 1. We try + ;; to compute the difference. + sbc hl,de + jr NC,.nodrop ; Jump if remainder is >= dividend + add hl,de ; Otherwise, restore remainder + ;; The add above sets the carry, because sbc hl,de did set it. +.nodrop: + ccf ; Complement borrow so 1 indicates a + ; successful substraction (this is the + ; next bit of quotient) + rla + djnz .dvloop + ;; Take care of the ninth quotient bit! after the loop B=0. + rl b ; BA = quotient + ;; Carry now contains "0" = valid result. + ld d,b + ld e,a ; DE = quotient, HL = remainder + ex de,hl ; HL = quotient, DE = remainder + ret + diff --git a/device/lib/z80/heap.s b/device/lib/z80/heap.s new file mode 100644 index 0000000..9b3ebe4 --- /dev/null +++ b/device/lib/z80/heap.s @@ -0,0 +1,44 @@ +;-------------------------------------------------------------------------- +; heap.s +; +; Copyright (C) 2001, Michael Hope +; +; This library is free software; you can redistribute it and/or modify it +; under the terms of the GNU General Public License as published by the +; Free Software Foundation; either version 2, or (at your option) any +; later version. +; +; This library is distributed in the hope that it will be useful, +; but WITHOUT ANY WARRANTY; without even the implied warranty of +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +; GNU General Public License for more details. +; +; You should have received a copy of the GNU General Public License +; along with this library; see the file COPYING. If not, write to the +; Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, +; MA 02110-1301, USA. +; +; As a special exception, if you link this library with other files, +; some of which are compiled with SDCC, to produce an executable, +; this library does not by itself cause the resulting executable to +; be covered by the GNU General Public License. This exception does +; not however invalidate any other reasons why the executable file +; might be covered by the GNU General Public License. +;-------------------------------------------------------------------------- + +; Just stubs - not copyrightable + + ;; Stubs that hook the heap in + .globl ___sdcc_heap_init + + .area _GSINIT + call ___sdcc_heap_init + + .area _HEAP +___sdcc_heap:: + ;; For now just allocate 1k of heap. + .ds 1023 + + .area _HEAP_END +___sdcc_heap_end:: + .ds 1 diff --git a/device/lib/z80/memmove.s b/device/lib/z80/memmove.s new file mode 100644 index 0000000..0e03e95 --- /dev/null +++ b/device/lib/z80/memmove.s @@ -0,0 +1,65 @@ +;-------------------------------------------------------------------------- +; memmove.s +; +; Copyright (C) 2008-2009, Philipp Klaus Krause, Marco Bodrato +; +; This library is free software; you can redistribute it and/or modify it +; under the terms of the GNU General Public License as published by the +; Free Software Foundation; either version 2, or (at your option) any +; later version. +; +; This library is distributed in the hope that it will be useful, +; but WITHOUT ANY WARRANTY; without even the implied warranty of +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +; GNU General Public License for more details. +; +; You should have received a copy of the GNU General Public License +; along with this library; see the file COPYING. If not, write to the +; Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, +; MA 02110-1301, USA. +; +; As a special exception, if you link this library with other files, +; some of which are compiled with SDCC, to produce an executable, +; this library does not by itself cause the resulting executable to +; be covered by the GNU General Public License. This exception does +; not however invalidate any other reasons why the executable file +; might be covered by the GNU General Public License. +;-------------------------------------------------------------------------- + + .area _CODE + + .globl _memmove + +; The Z80 has the ldir and lddr instructions, which are perfect for implementing memmove(). + +_memmove: + pop af + pop hl + pop de + pop bc + push bc + push de + push hl + push af + ld a, c + or a, b + ret Z + push hl + sbc hl, de ; or above cleared carry. + add hl, de ; same carry as the line before + jr C, memmove_up +memmove_down: + dec bc + add hl, bc + ex de, hl + add hl, bc + inc bc + lddr + pop hl + ret +memmove_up: + ex de, hl + ldir + pop hl + ret + diff --git a/device/lib/z80/modmixed.s b/device/lib/z80/modmixed.s new file mode 100644 index 0000000..8a87846 --- /dev/null +++ b/device/lib/z80/modmixed.s @@ -0,0 +1,62 @@ +;-------------------------------------------------------------------------- +; modmixed.s +; +; Copyright (C) 2010, Philipp Klaus Krause +; +; This library is free software; you can redistribute it and/or modify it +; under the terms of the GNU General Public License as published by the +; Free Software Foundation; either version 2, or (at your option) any +; later version. +; +; This library is distributed in the hope that it will be useful, +; but WITHOUT ANY WARRANTY; without even the implied warranty of +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +; GNU General Public License for more details. +; +; You should have received a copy of the GNU General Public License +; along with this library; see the file COPYING. If not, write to the +; Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, +; MA 02110-1301, USA. +; +; As a special exception, if you link this library with other files, +; some of which are compiled with SDCC, to produce an executable, +; this library does not by itself cause the resulting executable to +; be covered by the GNU General Public License. This exception does +; not however invalidate any other reasons why the executable file +; might be covered by the GNU General Public License. +;-------------------------------------------------------------------------- + +.globl __modsuchar +.globl __moduschar + +__modsuchar: + ld hl,#2+1 + add hl,sp + + ld e,(hl) + dec hl + ld l,(hl) + ld h,#0 + + call __div_signexte + + jp __get_remainder + +__moduschar: + ld hl,#2+1 + ld d, h + add hl,sp + + ld e,(hl) + dec hl + ld l,(hl) + + ld a,l ; Sign extend + rlca + sbc a, a + ld h, a + + call __div16 + + jp __get_remainder + diff --git a/device/lib/z80/modsigned.s b/device/lib/z80/modsigned.s new file mode 100644 index 0000000..00fc282 --- /dev/null +++ b/device/lib/z80/modsigned.s @@ -0,0 +1,57 @@ +;-------------------------------------------------------------------------- +; modsigned.s +; +; Copyright (C) 2009, Philipp Klaus Krause +; +; This library is free software; you can redistribute it and/or modify it +; under the terms of the GNU General Public License as published by the +; Free Software Foundation; either version 2, or (at your option) any +; later version. +; +; This library is distributed in the hope that it will be useful, +; but WITHOUT ANY WARRANTY; without even the implied warranty of +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +; GNU General Public License for more details. +; +; You should have received a copy of the GNU General Public License +; along with this library; see the file COPYING. If not, write to the +; Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, +; MA 02110-1301, USA. +; +; As a special exception, if you link this library with other files, +; some of which are compiled with SDCC, to produce an executable, +; this library does not by itself cause the resulting executable to +; be covered by the GNU General Public License. This exception does +; not however invalidate any other reasons why the executable file +; might be covered by the GNU General Public License. +;-------------------------------------------------------------------------- + +.area _CODE + +.globl __modschar +.globl __modsint + +__modschar: + ld hl,#2+1 + add hl,sp + + ld e,(hl) + dec hl + ld l,(hl) + + call __div8 + + jp __get_remainder + +__modsint: + pop af + pop hl + pop de + push de + push hl + push af + + call __div16 + + jp __get_remainder + diff --git a/device/lib/z80/modunsigned.s b/device/lib/z80/modunsigned.s new file mode 100644 index 0000000..ffdbf9e --- /dev/null +++ b/device/lib/z80/modunsigned.s @@ -0,0 +1,61 @@ +;-------------------------------------------------------------------------- +; modunsigned.s +; +; Copyright (C) 2009-2010, Philipp Klaus Krause +; +; This library is free software; you can redistribute it and/or modify it +; under the terms of the GNU General Public License as published by the +; Free Software Foundation; either version 2, or (at your option) any +; later version. +; +; This library is distributed in the hope that it will be useful, +; but WITHOUT ANY WARRANTY; without even the implied warranty of +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +; GNU General Public License for more details. +; +; You should have received a copy of the GNU General Public License +; along with this library; see the file COPYING. If not, write to the +; Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, +; MA 02110-1301, USA. +; +; As a special exception, if you link this library with other files, +; some of which are compiled with SDCC, to produce an executable, +; this library does not by itself cause the resulting executable to +; be covered by the GNU General Public License. This exception does +; not however invalidate any other reasons why the executable file +; might be covered by the GNU General Public License. +;-------------------------------------------------------------------------- + +.area _CODE + +.globl __moduchar +.globl __moduint + +__moduchar: + ld hl,#2+1 + add hl,sp + + ld e,(hl) + dec hl + ld l,(hl) + + call __divu8 + + ex de,hl + + ret + +__moduint: + pop af + pop hl + pop de + push de + push hl + push af + + call __divu16 + + ex de,hl + + ret + diff --git a/device/lib/z80/mul.s b/device/lib/z80/mul.s new file mode 100644 index 0000000..0b9e759 --- /dev/null +++ b/device/lib/z80/mul.s @@ -0,0 +1,74 @@ +;-------------------------------------------------------------------------- +; mulchar.s +; +; Copyright (C) 2000, Michael Hope +; +; This library is free software; you can redistribute it and/or modify it +; under the terms of the GNU General Public License as published by the +; Free Software Foundation; either version 2, or (at your option) any +; later version. +; +; This library is distributed in the hope that it will be useful, +; but WITHOUT ANY WARRANTY; without even the implied warranty of +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +; GNU General Public License for more details. +; +; You should have received a copy of the GNU General Public License +; along with this library; see the file COPYING. If not, write to the +; Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, +; MA 02110-1301, USA. +; +; As a special exception, if you link this library with other files, +; some of which are compiled with SDCC, to produce an executable, +; this library does not by itself cause the resulting executable to +; be covered by the GNU General Public License. This exception does +; not however invalidate any other reasons why the executable file +; might be covered by the GNU General Public License. +;-------------------------------------------------------------------------- + +.area _CODE + +.globl __mulint + +__mulint: + pop af + pop bc + pop de + push de + push bc + push af + + ;; 16-bit multiplication + ;; + ;; Entry conditions + ;; bc = multiplicand + ;; de = multiplier + ;; + ;; Exit conditions + ;; hl = less significant word of product + ;; + ;; Register used: AF,BC,DE,HL +__mul16:: + xor a,a + ld l,a + or a,b + ld b,#16 + + ;; Optimise for the case when this side has 8 bits of data or + ;; less. This is often the case with support address calls. + jr NZ,2$ + ld b,#8 + ld a,c +1$: + ;; Taken from z88dk, which originally borrowed from the + ;; Spectrum rom. + add hl,hl +2$: + rl c + rla ;DLE 27/11/98 + jr NC,3$ + add hl,de +3$: + djnz 1$ + ret + diff --git a/device/lib/z80/mulchar.s b/device/lib/z80/mulchar.s new file mode 100644 index 0000000..05db612 --- /dev/null +++ b/device/lib/z80/mulchar.s @@ -0,0 +1,79 @@ +;-------------------------------------------------------------------------- +; mulchar.s +; +; Copyright (c) 2017, Philipp Klaus Krause +; +; This library is free software; you can redistribute it and/or modify it +; under the terms of the GNU General Public License as published by the +; Free Software Foundation; either version 2, or (at your option) any +; later version. +; +; This library is distributed in the hope that it will be useful, +; but WITHOUT ANY WARRANTY; without even the implied warranty of +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +; GNU General Public License for more details. +; +; You should have received a copy of the GNU General Public License +; along with this library; see the file COPYING. If not, write to the +; Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, +; MA 02110-1301, USA. +; +; As a special exception, if you link this library with other files, +; some of which are compiled with SDCC, to produce an executable, +; this library does not by itself cause the resulting executable to +; be covered by the GNU General Public License. This exception does +; not however invalidate any other reasons why the executable file +; might be covered by the GNU General Public License. +;-------------------------------------------------------------------------- + +.area _CODE + +; unsigned char x unsigned char multiplication is done by code generation. + +.globl __mulsuchar +.globl __muluschar +.globl __mulschar + +; operands have different sign + +__mulsuchar: + ld hl,#2+1 + ld b, h + add hl,sp + + ld e,(hl) + dec hl + ld c,(hl) + jr signexte + +__muluschar: + ld hl,#2 + ld b, h + add hl,sp + + ld e,(hl) + inc hl + ld c,(hl) + jr signexte + +__mulschar: + ld hl,#2+1 + add hl,sp + + ld e,(hl) + dec hl + ld c,(hl) + + ;; Need to sign extend before going in. + ld a,c + rla + sbc a,a + ld b,a +signexte: + ld a,e + rla + sbc a,a + ld d,a + + jp __mul16 + diff --git a/device/lib/z80/setjmp.s b/device/lib/z80/setjmp.s new file mode 100644 index 0000000..3cc2bfb --- /dev/null +++ b/device/lib/z80/setjmp.s @@ -0,0 +1,95 @@ +;-------------------------------------------------------------------------- +; setjmp.s +; +; Copyright (C) 2011-2014, Philipp Klaus Krause +; +; This library is free software; you can redistribute it and/or modify it +; under the terms of the GNU General Public License as published by the +; Free Software Foundation; either version 2, or (at your option) any +; later version. +; +; This library is distributed in the hope that it will be useful, +; but WITHOUT ANY WARRANTY; without even the implied warranty of +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +; GNU General Public License for more details. +; +; You should have received a copy of the GNU General Public License +; along with this library; see the file COPYING. If not, write to the +; Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, +; MA 02110-1301, USA. +; +; As a special exception, if you link this library with other files, +; some of which are compiled with SDCC, to produce an executable, +; this library does not by itself cause the resulting executable to +; be covered by the GNU General Public License. This exception does +; not however invalidate any other reasons why the executable file +; might be covered by the GNU General Public License. +;-------------------------------------------------------------------------- + + .area _CODE + + .globl ___setjmp + +___setjmp: + pop hl + pop iy + push af + push hl + + ; Store return address. + ld 0(iy), l + ld 1(iy), h + + ; Store stack pointer. + xor a, a + ld l, a + ld h, a + add hl, sp + ld 2(iy), l + ld 3(iy), h + + ; Store frame pointer. + push ix + pop hl + ld 4(iy), l + ld 5(iy), h + + ; Return 0. + ld l, a + ld h, a + ret + +.globl _longjmp + +_longjmp: + pop af + pop iy + pop de + + ; Ensure that return value is non-zero. + ld a, e + or a, d + jr NZ, jump + inc de +jump: + + ; Restore frame pointer. + ld l, 4(iy) + ld h, 5(iy) + push hl + pop ix + + ; Adjust stack pointer. + ld l, 2(iy) + ld h, 3(iy) + ld sp, hl + pop hl + + ; Move return value into hl. + ex de, hl + + ; Jump. + ld c, 0(iy) + ld b, 1(iy) + push bc + ret diff --git a/device/lib/z80/strcpy.s b/device/lib/z80/strcpy.s new file mode 100644 index 0000000..933f41b --- /dev/null +++ b/device/lib/z80/strcpy.s @@ -0,0 +1,48 @@ +;-------------------------------------------------------------------------- +; strcpy.s +; +; Copyright (C) 2012, Philipp Klaus Krause +; +; This library is free software; you can redistribute it and/or modify it +; under the terms of the GNU General Public License as published by the +; Free Software Foundation; either version 2, or (at your option) any +; later version. +; +; This library is distributed in the hope that it will be useful, +; but WITHOUT ANY WARRANTY; without even the implied warranty of +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +; GNU General Public License for more details. +; +; You should have received a copy of the GNU General Public License +; along with this library; see the file COPYING. If not, write to the +; Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, +; MA 02110-1301, USA. +; +; As a special exception, if you link this library with other files, +; some of which are compiled with SDCC, to produce an executable, +; this library does not by itself cause the resulting executable to +; be covered by the GNU General Public License. This exception does +; not however invalidate any other reasons why the executable file +; might be covered by the GNU General Public License. +;-------------------------------------------------------------------------- + + .area _CODE + + .globl _strcpy + +_strcpy: + pop bc + pop de + pop hl + push hl + push de + push bc + push de + xor a, a +loop: + cp a, (hl) + ldi + jr NZ, loop + pop hl + ret + diff --git a/device/lib/z80/strlen.s b/device/lib/z80/strlen.s new file mode 100644 index 0000000..64f37da --- /dev/null +++ b/device/lib/z80/strlen.s @@ -0,0 +1,47 @@ +;-------------------------------------------------------------------------- +; strlen.s +; +; Copyright (C) 2008-2009, Philipp Klaus Krause +; +; This library is free software; you can redistribute it and/or modify it +; under the terms of the GNU General Public License as published by the +; Free Software Foundation; either version 2, or (at your option) any +; later version. +; +; This library is distributed in the hope that it will be useful, +; but WITHOUT ANY WARRANTY; without even the implied warranty of +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +; GNU General Public License for more details. +; +; You should have received a copy of the GNU General Public License +; along with this library; see the file COPYING. If not, write to the +; Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, +; MA 02110-1301, USA. +; +; As a special exception, if you link this library with other files, +; some of which are compiled with SDCC, to produce an executable, +; this library does not by itself cause the resulting executable to +; be covered by the GNU General Public License. This exception does +; not however invalidate any other reasons why the executable file +; might be covered by the GNU General Public License. +;-------------------------------------------------------------------------- + + .area _CODE + + .globl _strlen + +; The Z80 has the cpir instruction, which is perfect for implementing strlen(). + +_strlen: + pop bc + pop hl + push hl + push bc + xor a, a + ld b, a + ld c, a + cpir + ld hl, #-1 + sbc hl, bc ; C flag still cleared from xor above. + ret + |
