From 7ddacbfe1eaccaaa94637562909639ad7d8ee468 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A0rius=20Mont=C3=B3n?= Date: Thu, 11 Jun 2020 09:24:48 +0200 Subject: [PATCH] C++ example --- tests/CPP/cout/Makefile | 50 ++++++++++++++++++++++++++++ tests/CPP/cout/cout.cpp | 21 ++++++++++++ tests/CPP/cout/helper_functions.cpp | 51 +++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+) create mode 100644 tests/CPP/cout/Makefile create mode 100644 tests/CPP/cout/cout.cpp create mode 100644 tests/CPP/cout/helper_functions.cpp diff --git a/tests/CPP/cout/Makefile b/tests/CPP/cout/Makefile new file mode 100644 index 0000000..25420a3 --- /dev/null +++ b/tests/CPP/cout/Makefile @@ -0,0 +1,50 @@ +TARGET = cout + +TARGET_ARCH = riscv32 + +CC = riscv32-unknown-elf-g++ + +# compiling flags here +CFLAGS = -Wall -I. -O0 -static -march=rv32imac -mabi=ilp32 --specs=nosys.specs + +LINKER = riscv32-unknown-elf-g++ +# linking flags here +LDFLAGS = -I. -static +LIBS = $(EXTRA_LIBS) + + +# change these to proper directories where each file should be +SRCDIR = ./ +OBJDIR = . +BINDIR = ./ +INCDIR = -I. +LIBDIR = -L. + + +SOURCES := $(wildcard $(SRCDIR)/*.cpp) +INCLUDES := $(wildcard $(INCDIR)/*.h) +OBJECTS := $(SOURCES:$(SRCDIR)/%.c=$(OBJDIR)/%.o) + +rm = rm -f + + +$(BINDIR)/$(TARGET): $(OBJECTS) + $(LINKER) $(CFLAGS) $(LDFLAGS) $(LIBS) $(LIBDIR) $(OBJECTS) -o $@ + riscv32-unknown-elf-objdump -d $@ > dump + riscv32-unknown-elf-objcopy -Oihex $@ $(TARGET).hex + @echo "Linking complete!" + +$(OBJECTS): $(OBJDIR)/%.o : $(SRCDIR)/%.cpp + @echo "Compiling "$<" ..." + $(CC) $(CFLAGS) $(INCDIR) -c $< -o $@ + @echo "Done!" + +.PHONY: clean +clean: + @$(rm) $(OBJECTS) *.hex dump + @echo "Cleanup complete!" + +.PHONY: remove +remove: clean + @$(rm) $(BINDIR)/$(TARGET) + @echo "Executable removed!" diff --git a/tests/CPP/cout/cout.cpp b/tests/CPP/cout/cout.cpp new file mode 100644 index 0000000..1752b5f --- /dev/null +++ b/tests/CPP/cout/cout.cpp @@ -0,0 +1,21 @@ +#include +using namespace std; + + +int main(void) { + + int aux[5] = {0, 1, 2, 3, 4}; + int aux2[5]; + int counter = 0; + + cout << "Test Start" << endl; + + + for ( const auto &x : aux ) std::cout << x << ' '; + std::cout << std::endl; + + cout << "Test End" << endl; + asm volatile ("ecall"); + + return 0; +} diff --git a/tests/CPP/cout/helper_functions.cpp b/tests/CPP/cout/helper_functions.cpp new file mode 100644 index 0000000..a137bfc --- /dev/null +++ b/tests/CPP/cout/helper_functions.cpp @@ -0,0 +1,51 @@ +#include +#include + + +#define TRACE (*(unsigned char *)0x40000000) +extern "C" { + +int _read(int file, char* ptr, int len) { + return 0; +} + +int _open(int fd) { + return 0; +} + +int _close(int fd){ + return 0; +} + +int _fstat_r(int fd) { + return 0; +} + +int _lseek_r(struct _reent *ptr, FILE *fp, long offset, int whence){ + return 0; +} + +int _isatty_r(struct _reent *ptr, int fd) { + return 0; +} + + +int _write(int file, const char *ptr, int len) { + int x; + + for (x = 0; x < len; x++) { + TRACE = *ptr++; + } + + return (len); +} + +int _getpid(void) { + return 0; +} + +void _kill(int pid) { + return; +} + +}