From dfd949efdd54a1272d58eabcef862b863a61d36d Mon Sep 17 00:00:00 2001 From: Felix Kopp Date: Fri, 12 Jun 2020 00:01:15 +0200 Subject: [PATCH] Add stupid string.h implementation --- .gitignore | 1 + .vscode/c_cpp_properties.json | 5 +- Makefile | 54 ++++++++---- include/arch/string.h | 36 ++++++++ lib/Makefile | 29 +++++++ lib/string.c | 150 ++++++++++++++++++++++++++++++++++ 6 files changed, 255 insertions(+), 20 deletions(-) create mode 100644 include/arch/string.h create mode 100644 lib/Makefile create mode 100644 lib/string.c diff --git a/.gitignore b/.gitignore index 0683cb6..94765f6 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ *.elf *.bin *.hex +/.config diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index b48a4dc..742a9d0 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -49,7 +49,7 @@ "ARCH=at91sam3x8e", "ARCH_AT91SAM3X8E" ], - "compilerPath": "/usr/bin/avr-gcc", + "compilerPath": "/usr/bin/arm-none-eabi-gcc", "cStandard": "c11", "cppStandard": "gnu++14", "intelliSenseMode": "gcc-arm", @@ -57,7 +57,8 @@ "./include" ], "compilerArgs": [ - "-mmcu=at91sam3x8e", + "-mcpu=at91sam3x8e", + "-marm", "-nostdlib", "-nodefaultlibs", "-fno-builtin", diff --git a/Makefile b/Makefile index 7606c3a..ec757d1 100644 --- a/Makefile +++ b/Makefile @@ -24,19 +24,36 @@ # AVR_CC ?= $(shell which avr-gcc) -CC = $(AVR_CC) -AVR_LD ?= $(shell which avr-gcc) -LD = $(AVR_LD) AVR_OBJCOPY ?= $(shell which avr-objcopy) -OBJCOPY = $(AVR_OBJCOPY) +AVR_LD ?= $(shell which avr-ld) + +ARM_CC ?= $(shell which arm-none-eabi-gcc) +ARM_LD ?= $(shell which arm-none-eabi-ld) +ARM_OBJCOPY ?= $(shell which arm-none-eabi-objcopy) EXTRA_CFLAGS ?= CFLAGS = $(EXTRA_CFLAGS) -CFLAGS += -g -nodefaultlibs -fno-builtin +CFLAGS += -g -nodefaultlibs -nostartfiles CFLAGS += -I$(PWD)/include CFLAGS += -DARCH=$(ARCH) -CFLAGS += -mmcu=$(ARCH) -fpack-struct -std=gnu11 +CFLAGS += -fpack-struct -std=gnu11 + +EXTRA_LDFLAGS ?= +LDFLAGS = $(EXTRA_LDFLAGS) + +ifeq ($(ARCH), at91sam3x8e) + CFLAGS += -mcpu=cortex-m3 -mthumb -mabi=aapcs -march=armv7-m + CC = $(ARM_CC) + LD = $(ARM_LD) + OBJCOPY = $(ARM_OBJCOPY) +else + CFLAGS += -mmcu=$(ARCH) + LDFLAGS += -mmcu=$(ARCH) + CC = $(AVR_CC) + LD = $(AVR_LD) + OBJCOPY = $(AVR_OBJCOPY) +endif CFLAGS += -Wall \ -Wstrict-prototypes \ @@ -52,31 +69,32 @@ ifdef DEBUG CFLAGS += -DDEBUG endif -EXTRA_LDFLAGS ?= -LDFLAGS = $(EXTRA_LDFLAGS) - -LDFLAGS += \ - -mmcu=$(ARCH) \ - -nodefaultlibs - ARDIX_ASM_SOURCES = include arch/Makefile ARDIX_SOURCES = include init/Makefile +include lib/Makefile + ARDIX_OBJS = $(ARDIX_SOURCES:.c=.o) ARDIX_ASM_OBJS = $(ARDIX_ASM_SOURCES:.S=.o) %.o: %.S | %.c - $(CC) -c -Os -o $@ $(CFLAGS) $< + $(CC) -c -Os $(CFLAGS) $< -ardix.elf: $(ARDIX_OBJS) $(ARDIX_ASM_OBJS) +ardix.elf: $(ARDIX_ASM_OBJS) $(ARDIX_OBJS) $(LD) $(LDFLAGS) -o $@ $^ ardix.hex: ardix.elf $(OBJCOPY) -O ihex -R .eeprom $^ $@ -clean: - rm -f ardix.elf ardix.hex $(ARDIX_OBJS) $(ARDIX_ASM_OBJS) +ardix.bin: ardix.elf + $(OBJCOPY) -O binary -R .eeprom $^ $@ -all: ardix.hex +clean: + rm -f ardix.elf ardix.hex ardix.bin $(ARDIX_OBJS) $(ARDIX_ASM_OBJS) + +config: + ./configure + +all: ardix.hex ardix.bin diff --git a/include/arch/string.h b/include/arch/string.h new file mode 100644 index 0000000..0661429 --- /dev/null +++ b/include/arch/string.h @@ -0,0 +1,36 @@ +/* + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (c) 2020 Felix Kopp + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be + * used to endorse or promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#ifdef ARCH_ATMEGA328P +#include +#endif /* ARCH_ATMEGA328P */ + +#ifdef ARCH_AT91SAM3X8E +#include +#endif /* ARCH_AT91SAM3X8E */ diff --git a/lib/Makefile b/lib/Makefile new file mode 100644 index 0000000..edb5bf2 --- /dev/null +++ b/lib/Makefile @@ -0,0 +1,29 @@ +# +# Copyright (c) 2020 Felix Kopp +# +# Redistribution and use in source and binary forms, with or without modification, are permitted +# provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of +# conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright notice, this list of +# conditions and the following disclaimer in the documentation and/or other materials +# provided with the distribution. +# 3. Neither the name of the copyright holder nor the names of its contributors may be +# used to endorse or promote products derived from this software without specific prior +# written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR +# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY +# WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +ARDIX_LIB_PWD := $(PWD)/lib + +ARDIX_SOURCES += \ + $(ARDIX_LIB_PWD)/string.c diff --git a/lib/string.c b/lib/string.c new file mode 100644 index 0000000..6b3c924 --- /dev/null +++ b/lib/string.c @@ -0,0 +1,150 @@ +/* + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (c) 2020 Felix Kopp + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be + * used to endorse or promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include + +#ifndef __HAVE_ASM_MEMCMP +int memcmp(const void *s1, const void *s2, size_t n) +{ + int delta = 0; + + while (n-- > 0) { + /* + * TODO + * See if avr-gcc is smart enough to combine the pointer + * dereferencing and increment into a single `ld Rd,X+` + * instruction even if there is an if statement in between + */ + delta = *(const unsigned char *)s1 - *(const unsigned char *)s2; + if (delta != 0) + break; + + s1++; + s2++; + } + + return delta; +} +#endif /* __HAVE_ASM_MEMCMP */ + +#ifndef __HAVE_ASM_MEMCPY +void *memcpy(void *dest, const void *src, size_t n) +{ + uint8_t *tmp = (uint8_t *)dest; + + while (n-- > 0) + *tmp++ = *(const uint8_t *)src++; + + return dest; +} +#endif /* __HAVE_ASM_MEMCPY */ + +#ifndef __HAVE_ASM_MEMSET +void *memset(void *ptr, int c, size_t n) +{ + char *tmp = (char *)ptr; + + while (n-- > 0) + *tmp++ = (char)c; + + return ptr; +} +#endif /* __HAVE_ASM_MEMSET */ + +#ifndef __HAVE_ASM_MEMMOVE +void *memmove(void *dest, const void *src, size_t n) +{ + char *tmp = (char *)dest; + const char *s = (const char *)src; + + if (dest == src) + return dest; + + if (dest < src) { + while (n-- != 0) + *tmp++ = *s++; + } else { + tmp += n; + s += n; + while (n-- != 0) + *--tmp = *--s; + } + + return dest; +} +#endif /* __HAVE_ASM_MEMMOVE */ + +#ifndef __HAVE_ASM_STRCMP +int strcmp(const char *s1, const char *s2) +{ + while (*s1++ == *s2++) { + if (*s1 == '\0' || *s2 == '\0') + break; + } + + return *((const unsigned char *)s1) - *((const unsigned char *)s2); +} +#endif /* __HAVE_ASM_STRCMP */ + +#ifndef __HAVE_ASM_STRCPY +char *strcpy(char *dest, const char *src) +{ + char *tmp = dest; + + while ((*tmp++ = *src++) != '\0'); + /* nothing */ + + return dest; +} +#endif /* __HAVE_ASM_STRCPY */ + +#ifndef __HAVE_ASM_STRNCPY +char *strncpy(char *dest, const char *src, size_t n) +{ + char *tmp = dest; + + while (n-- != 0) { + if ((*tmp++ = *src++) == '\0') + break; + } + + return dest; +} +#endif /* __HAVE_ASM_STRNCPY */ + +#ifndef __HAVE_ASM_STRLEN +size_t strlen(const char *s) +{ + const char *tmp = s; + + while (*tmp++ != '\0'); + /* nothing */ + + return (size_t)tmp - (size_t)s - (size_t)1; +} +#endif /* __HAVE_ASM_STRLEN */