From d16f0251f7eecd023b83be15cf9620ca99ff57a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Twoje=20Imi=C4=99?= Date: Tue, 11 Jun 2024 07:43:41 +0200 Subject: [PATCH] merge2 --- cpp/Make.rules | 81 ++++++++++++++++++++++++++ cpp/Makefile | 27 +++++++++ cpp/_crt0.S | 42 ++++++++++++++ cpp/_rvmain.cpp | 151 ++++++++++++++++++++++++++++++++++++++++++++++++ cpp/myfunc.cpp | 35 +++++++++++ cpp/myfunc.h | 9 +++ 6 files changed, 345 insertions(+) create mode 100644 cpp/Make.rules create mode 100644 cpp/Makefile create mode 100644 cpp/_crt0.S create mode 100644 cpp/_rvmain.cpp create mode 100644 cpp/myfunc.cpp create mode 100644 cpp/myfunc.h diff --git a/cpp/Make.rules b/cpp/Make.rules new file mode 100644 index 0000000..2bd12e9 --- /dev/null +++ b/cpp/Make.rules @@ -0,0 +1,81 @@ + +ARCH=riscv64-unknown-elf +GNU_DIR=$(HOME)/riscv/riscv/ +GNU_BIN=$(GNU_DIR)/bin + + +CC=$(GNU_BIN)/$(ARCH)-gcc +CXX=$(GNU_BIN)/$(ARCH)-g++ +AS=$(GNU_BIN)/$(ARCH)-as +LD=$(GNU_BIN)/$(ARCH)-ld +OBJCOPY=$(GNU_BIN)/$(ARCH)-objcopy +OBJDUMP=$(GNU_BIN)/$(ARCH)-objdump +SIZE=$(GNU_BIN)/$(ARCH)-size +AR=$(GNU_BIN)/$(ARCH)-ar +RANLIB=$(GNU_BIN)/$(ARCH)-ranlib + + +CFLAGS+=-ffreestanding +CFLAGS+=-fno-pic +CFLAGS+=-march=rv32i -mabi=ilp32 +CFLAGS+= -g + + +LDFLAGS+=-nostdlib +LDFLAGS+=-Wl,-Ttext=0x00000000 + +# see: https://github.com/riscv/riscv-gcc/issues/120 +#LDFLAGS+=-Wl,--no-relax + + + +ASFLAGS+=$(CFLAGS) +CXXFLAGS+=$(CFLAGS) + +CLEAN_DIRS=$(SUBDIRS:%=clean-%) +ALL_DIRS=$(SUBDIRS:%=all-%) + +OBJDUMPFLAGS+=-Mnumeric,no-aliases + +.PHONY: all clean world $(CLEAN_DIRS) $(ALL_DIRS) + + +%.bin : % + $(OBJCOPY) $< -O binary $@ + +%.lst : % + $(OBJDUMP) $(OBJDUMPFLAGS) -dr --disassemble-all $< > $<.lst + +% : %.o + $(LINK.cc) $(LDFLAGS) -o $@ $^ $(LDLIBS) + $(SIZE) -x -A $@ + +%.s: %.c + $(COMPILE.c) -S -o $@ $< + +%.s: %.cc + $(COMPILE.cc) -S -o $@ $< + +%.o: %.c + $(COMPILE.c) -o $@ $< + +%.o: %.cc + $(COMPILE.cc) -o $@ $< + +%.srec: % + $(OBJCOPY) $< -O srec $@ + + + + +all:: $(ALL_DIRS) + +clean:: $(CLEAN_DIRS) + +$(ALL_DIRS):: + $(MAKE) -C $(@:all-%=%) all + +$(CLEAN_DIRS):: + $(MAKE) -C $(@:clean-%=%) clean + +world:: clean all diff --git a/cpp/Makefile b/cpp/Makefile new file mode 100644 index 0000000..1456d22 --- /dev/null +++ b/cpp/Makefile @@ -0,0 +1,27 @@ +TOP=./ +include $(TOP)/Make.rules + +LDLIBS= +#CFLAGS+=-O0 -g +# CFLAGS+=-Og -ggdb3 +CFLAGS+=-O0 -ggdb3 + +LDFLAGS+=-Wl,--no-relax +LDFLAGS+=-Wl,-Ttext=0x80000000,-Tdata=0x80010000 +# LDFLAGS+=-T murax_128k_ram.ld + +PROGS=prog prog.bin prog.lst + +all:: $(PROGS) + +prog: _crt0.o _rvmain.o myfunc.o + $(LINK.cc) -o $@ $^ $(LDLIBS) + $(SIZE) -A -x $@ + +clean:: + rm -f $(PROGS) *.o *.s *.lst *.bin *.srec *.dis + +.PHONY: run +run: prog.bin + ../../../src/rvddt -l0x3000 -f prog.bin + diff --git a/cpp/_crt0.S b/cpp/_crt0.S new file mode 100644 index 0000000..50a0e46 --- /dev/null +++ b/cpp/_crt0.S @@ -0,0 +1,42 @@ + .text + .global _start + .type _start, @function + +_start: + # Initialize global pointer + .option push + .option norelax + la gp, __global_pointer$ + .option pop + + li sp, 0x800ffff0 + + # Clear the bss segment + la a0, __bss_start + la a1, __BSS_END__ + +clear_bss: + bgeu a0, a1, finish_bss + sb x0, 0(a0) + addi a0, a0, 1 + beq x0, x0, clear_bss +finish_bss: + + nop //! + + call main + + nop //! + + # abort execution here + ebreak + + .section .rodata +alfabet: + .string "abcdefghijklmnopqrstuwxyz" +slowo: + + .section .data +wynik: + .string "mpabi" + .space 26 # rezerwuje 26 bajtów dla wyniku, zainicjowane na 0 diff --git a/cpp/_rvmain.cpp b/cpp/_rvmain.cpp new file mode 100644 index 0000000..338a5e7 --- /dev/null +++ b/cpp/_rvmain.cpp @@ -0,0 +1,151 @@ +#include + +int strlen(char *s) { + char *p = s; + while (*p != '\0') + p++; + return p - s; +} + +void strcpy(char *s, char *t) +{ + while (*s++ = *t++); +} + + +// Effective C++ by Scott Meyers +// page. 6 +// Item 1: Prefer const and inline to #define. +// +class NewDefine{ + + private: + static const int ALLOCSIZE= 10000; + +}; + +const int NewDefine::ALLOCSIZE; + +#define ALLOCSIZE 10000 + +static char allocbuf[ALLOCSIZE]; +static char *allocp = allocbuf; + +char *alloc(int n) +{ + + if (n % 4 != 0) { + n += 4 - (n % 4); + } + + + if (allocbuf + ALLOCSIZE - allocp >= n) { + allocp += n; + return allocp - n; + } else + return 0; +} + +// def. model danych + +//pre processor +#define LEN (8+2)*10 + +struct model { + char * ptr; + uint32_t len ; +}; + + +//alg +// prosta implementacji func. z bibl. std. strok przy uzyciu gpt3.5 +// + +#define NULL ((void*) 0) +bool is_delim(char c, const char *delims) { + while (*delims) { + if (c == *delims) { + return true; + } + delims++; + } + return false; +} + +char *simple_strtok(char *str, const char *delims) { + static char *static_str = (char *) NULL; + + if (str !=(char *) NULL) { + static_str = str; + } + + if (static_str == (char *) NULL) { + return (char *)NULL; + } + + while (*static_str && is_delim(*static_str, delims)) { + static_str++; + } + + if (*static_str == '\0') { + return (char *)NULL; + } + + char *token_start = static_str; + + while (*static_str && !is_delim(*static_str, delims)) { + static_str++; + } + + if (*static_str) { + *static_str = '\0'; + static_str++; + } + + return token_start; +} + +char buf[1000]; +struct model * p = (struct model *) buf; //p[1] + +/* +int alg(char *ptr) { + const char *delims = " ,.!?:;\n\t"; + int pos = 0; + + char *token = simple_strtok(ptr, delims); + while (token != (char *)NULL) { + p[pos].ptr = token; + p[pos].len = strlen(token); + ++pos; + token = simple_strtok((char *)NULL, delims); + } + + return pos; +} +*/ + +int alg(char *ptr) { + const char *delims = " ,.!?:;\n\t"; + int pos = 0; + + char *token = simple_strtok(ptr, delims); + while (token != (char *)NULL) { + p[pos].ptr = token; + p[pos].len = strlen(token); + ++pos; + token = simple_strtok((char *)NULL, delims); + } + return pos; +} + +// gdb: p/x (model[]*)p[0].len +// gdb: p/s (char *)(model[]*)p[2].ptr +// :) powered by rv32i +int main() { + char *str = " Success is often defined as the ability to reach your goals in life, whatever those goals may be. In some ways, a better word for success might be attainment, accomplishment, or progress. It is not necessarily a destination but a journey that helps develop the skills and resources you need to thrive."; + alg(str); + asm ("nop"); + + return 1; +} diff --git a/cpp/myfunc.cpp b/cpp/myfunc.cpp new file mode 100644 index 0000000..b621842 --- /dev/null +++ b/cpp/myfunc.cpp @@ -0,0 +1,35 @@ +#include "myfunc.h" + +// Lokalna deklaracja funkcji my_strlen +static int my_strlen(const char *str) { + int length = 0; + while (str[length] != '\0') { + length++; + } + return length; +} + +void count_characters(const char* alfabet, const char* slowo, uint8_t* wynik) { + // Użycie my_strlen do obliczenia długości alfabetu + int alfabet_length = my_strlen(alfabet); + + for (int i = 0; i < alfabet_length; ++i) { + wynik[i] = 0; + } + + for (int i = 0; i < alfabet_length; ++i) { + for (int j = 0; slowo[j] != '\0'; ++j) { + if (alfabet[i] == slowo[j]) { + wynik[i]++; + } + } + } +} + +// #include "myfunc.h" + +// const char* alfabet = "abcdefghijklmnopqrstuwxyz"; +// const char* slowo = "mpabi"; +// int wynik[26] = {0}; // Inicjalizacja tablicy wyników na 0 + +// count_characters(alfabet, slowo, wynik); diff --git a/cpp/myfunc.h b/cpp/myfunc.h new file mode 100644 index 0000000..99a5984 --- /dev/null +++ b/cpp/myfunc.h @@ -0,0 +1,9 @@ +#ifndef myfunc_H +#define myfunc_H + +#include + + +void count_characters(const char* alfabet, const char* slowo, uint8_t* wynik); + +#endif