From e6b95298cdbc282d01dcfd5646e92a0f95421e15 Mon Sep 17 00:00:00 2001 From: mariusmonton Date: Fri, 21 Sep 2018 09:26:02 +0200 Subject: [PATCH] new C examples ,modified Makefile --- C/forloop/Makefile | 6 ++++-- C/func1/Makefile | 52 +++++++++++++++++++++++++++++++++++++++++++++ C/func1/func1.c | 25 ++++++++++++++++++++++ C/func2/Makefile | 52 +++++++++++++++++++++++++++++++++++++++++++++ C/func2/func2.c | 29 +++++++++++++++++++++++++ C/stdlibs/Makefile | 52 +++++++++++++++++++++++++++++++++++++++++++++ C/stdlibs/stdlibs.c | 17 +++++++++++++++ C/trace/Makefile | 6 ++++-- C/trace2/Makefile | 7 ++++-- C/trace3/Makefile | 6 ++++-- 10 files changed, 244 insertions(+), 8 deletions(-) create mode 100644 C/func1/Makefile create mode 100644 C/func1/func1.c create mode 100644 C/func2/Makefile create mode 100644 C/func2/func2.c create mode 100644 C/stdlibs/Makefile create mode 100644 C/stdlibs/stdlibs.c diff --git a/C/forloop/Makefile b/C/forloop/Makefile index 91110ad..83ab5e5 100644 --- a/C/forloop/Makefile +++ b/C/forloop/Makefile @@ -5,11 +5,13 @@ TARGET_ARCH=riscv32 CC = riscv32-unknown-linux-gnu-gcc # compiling flags here #CFLAGS = -Wall -I. -O0 -nostartfiles -march=rv32i -mabi=ilp32 -CFLAGS = -Wall -I. -O0 -nostdlib -march=rv32i -mabi=ilp32 +CFLAGS = -Wall -I. -O0 -nostdlib -march=rv32i -mabi=ilp32 --entry main +#CFLAGS = -Wall -I. -O0 -nodefaultlibs -march=rv32i -mabi=ilp32 + LINKER = riscv32-unknown-linux-gnu-gcc # linking flags here -LFLAGS = -I. +LFLAGS = -I. --entry main LIBS = $(EXTRA_LIBS) diff --git a/C/func1/Makefile b/C/func1/Makefile new file mode 100644 index 0000000..3cb3b47 --- /dev/null +++ b/C/func1/Makefile @@ -0,0 +1,52 @@ +TARGET = func1 + +TARGET_ARCH=riscv32 + +CC = riscv32-unknown-linux-gnu-gcc +# compiling flags here +#CFLAGS = -Wall -I. -O0 -nostartfiles -march=rv32i -mabi=ilp32 +CFLAGS = -Wall -I. -O0 -nostdlib -march=rv32i -mabi=ilp32 --entry main +#CFLAGS = -Wall -I. -O0 -nodefaultlibs -march=rv32i -mabi=ilp32 + + +LINKER = riscv32-unknown-linux-gnu-gcc +# linking flags here +LFLAGS = -I. --entry main +LIBS = $(EXTRA_LIBS) + + +# change these to proper directories where each file should be +SRCDIR = ./ +OBJDIR = . +BINDIR = ./ +INCDIR = -I. +LIBDIR = -L. + + +SOURCES := $(wildcard $(SRCDIR)/*.c) +INCLUDES := $(wildcard $(INCDIR)/*.h) +OBJECTS := $(SOURCES:$(SRCDIR)/%.c=$(OBJDIR)/%.o) +rm = rm -f + + +$(BINDIR)/$(TARGET): $(OBJECTS) +# $(LINKER) $(OBJECTS) $(LFLAGS) $(LIBS) $(LIBDIR) -o $@ + riscv32-unknown-linux-gnu-objdump -d $< > dump + objcopy -Oihex $< $(TARGET).hex +# @echo "Linking complete!" + +$(OBJECTS): $(OBJDIR)/%.o : $(SRCDIR)/%.c + @echo "Compiling "$<" ..." +# $(CC) $(CFLAGS) $(INCDIR) -c $< -o $@ + $(CC) $(CFLAGS) $(INCDIR) $< -o $@ + @echo "Done!" + +.PHONY: clean +clean: + @$(rm) $(OBJECTS) + @echo "Cleanup complete!" + +.PHONY: remove +remove: clean + @$(rm) $(BINDIR)/$(TARGET) + @echo "Executable removed!" diff --git a/C/func1/func1.c b/C/func1/func1.c new file mode 100644 index 0000000..e317581 --- /dev/null +++ b/C/func1/func1.c @@ -0,0 +1,25 @@ +#define TRACE (*(unsigned char *)0x40000000) + +int func1(int a, int* b) { + return a + (*b); +} + + +void main(void) { + int x1, x2, x3; + + x1 = 6; + x2 = 7; + + x3 = func1(x1, &x2); + + if (x3 == 13) { + TRACE = 'O'; + TRACE = 'K'; + TRACE = '\n'; + } else { + TRACE = 'E'; + TRACE = 'R'; + TRACE = '\n'; + } +} diff --git a/C/func2/Makefile b/C/func2/Makefile new file mode 100644 index 0000000..92a39b7 --- /dev/null +++ b/C/func2/Makefile @@ -0,0 +1,52 @@ +TARGET = func2 + +TARGET_ARCH=riscv32 + +CC = riscv32-unknown-linux-gnu-gcc +# compiling flags here +#CFLAGS = -Wall -I. -O0 -nostartfiles -march=rv32i -mabi=ilp32 +CFLAGS = -Wall -I. -O0 -nostdlib -march=rv32i -mabi=ilp32 --entry main +#CFLAGS = -Wall -I. -O0 -nodefaultlibs -march=rv32i -mabi=ilp32 + + +LINKER = riscv32-unknown-linux-gnu-gcc +# linking flags here +LFLAGS = -I. --entry main +LIBS = $(EXTRA_LIBS) + + +# change these to proper directories where each file should be +SRCDIR = ./ +OBJDIR = . +BINDIR = ./ +INCDIR = -I. +LIBDIR = -L. + + +SOURCES := $(wildcard $(SRCDIR)/*.c) +INCLUDES := $(wildcard $(INCDIR)/*.h) +OBJECTS := $(SOURCES:$(SRCDIR)/%.c=$(OBJDIR)/%.o) +rm = rm -f + + +$(BINDIR)/$(TARGET): $(OBJECTS) +# $(LINKER) $(OBJECTS) $(LFLAGS) $(LIBS) $(LIBDIR) -o $@ + riscv32-unknown-linux-gnu-objdump -d $< > dump + objcopy -Oihex $< $(TARGET).hex +# @echo "Linking complete!" + +$(OBJECTS): $(OBJDIR)/%.o : $(SRCDIR)/%.c + @echo "Compiling "$<" ..." +# $(CC) $(CFLAGS) $(INCDIR) -c $< -o $@ + $(CC) $(CFLAGS) $(INCDIR) $< -o $@ + @echo "Done!" + +.PHONY: clean +clean: + @$(rm) $(OBJECTS) + @echo "Cleanup complete!" + +.PHONY: remove +remove: clean + @$(rm) $(BINDIR)/$(TARGET) + @echo "Executable removed!" diff --git a/C/func2/func2.c b/C/func2/func2.c new file mode 100644 index 0000000..531112d --- /dev/null +++ b/C/func2/func2.c @@ -0,0 +1,29 @@ +#define TRACE (*(unsigned char *)0x40000000) + +void print(char *msg) { + int i = 0; + while(msg[i] != '\0') { + TRACE = msg[i]; + i++; + } +} + +int func1(int a, int* b) { + return a + (*b); +} + + +void main(void) { + int x1, x2, x3; + + x1 = 6; + x2 = 7; + + x3 = func1(x1, &x2); + + if (x3 == 13) { + print("OK\n"); + } else { + print("ERROR\n"); + } +} diff --git a/C/stdlibs/Makefile b/C/stdlibs/Makefile new file mode 100644 index 0000000..4eca9d0 --- /dev/null +++ b/C/stdlibs/Makefile @@ -0,0 +1,52 @@ +TARGET = stdlibs + +TARGET_ARCH=riscv32 + +CC = riscv32-unknown-linux-gnu-gcc +# compiling flags here +#CFLAGS = -Wall -I. -O0 -march=rv32 -mabi=ilp32 +#CFLAGS = -Wall -I. -O0 -nostdlib -march=rv32i -mabi=ilp32 --entry main -lc +CFLAGS = -Wall -I. -O0 -nostdlib -march=rv32i -mabi=ilp32 --entry main + + +LINKER = riscv32-unknown-linux-gnu-gcc +# linking flags here +LFLAGS = -I. +LIBS = $(EXTRA_LIBS) + + +# change these to proper directories where each file should be +SRCDIR = ./ +OBJDIR = . +BINDIR = ./ +INCDIR = -I. +LIBDIR = -L. + + +SOURCES := $(wildcard $(SRCDIR)/*.c) +INCLUDES := $(wildcard $(INCDIR)/*.h) +OBJECTS := $(SOURCES:$(SRCDIR)/%.c=$(OBJDIR)/%.o) +rm = rm -f + + +$(BINDIR)/$(TARGET): $(OBJECTS) +# $(LINKER) $(OBJECTS) $(LFLAGS) $(LIBS) $(LIBDIR) -o $@ + riscv32-unknown-linux-gnu-objdump -d $< > dump + objcopy -Oihex $< $(TARGET).hex +# @echo "Linking complete!" + +$(OBJECTS): $(OBJDIR)/%.o : $(SRCDIR)/%.c + @echo "Compiling "$<" ..." +# $(CC) $(CFLAGS) $(INCDIR) -c $< -o $@ + $(CC) $(CFLAGS) $(INCDIR) $< -o $@ + @echo "Done!" + +.PHONY: clean +clean: + @$(rm) $(OBJECTS) + @echo "Cleanup complete!" + +.PHONY: remove +remove: clean + @$(rm) $(BINDIR)/$(TARGET) + @echo "Executable removed!" diff --git a/C/stdlibs/stdlibs.c b/C/stdlibs/stdlibs.c new file mode 100644 index 0000000..a1047b0 --- /dev/null +++ b/C/stdlibs/stdlibs.c @@ -0,0 +1,17 @@ +#include + +#define TRACE (*(unsigned char *)0x40000000) + +void print(char *msg) { + int i = 0; + while(msg[i] != '\0') { + TRACE = msg[i]; + i++; + } +} + +void main(void) { + char msg[50]; + print("hello"); + sprintf(msg, "%i", 5); +} diff --git a/C/trace/Makefile b/C/trace/Makefile index ed1360b..7026d26 100644 --- a/C/trace/Makefile +++ b/C/trace/Makefile @@ -5,11 +5,13 @@ TARGET_ARCH=riscv32 CC = riscv32-unknown-linux-gnu-gcc # compiling flags here #CFLAGS = -Wall -I. -O0 -nostartfiles -march=rv32i -mabi=ilp32 -CFLAGS = -Wall -I. -O0 -nostdlib -march=rv32i -mabi=ilp32 +CFLAGS = -Wall -I. -O0 -nostdlib -march=rv32i -mabi=ilp32 --entry main +#CFLAGS = -Wall -I. -O0 -nodefaultlibs -march=rv32i -mabi=ilp32 + LINKER = riscv32-unknown-linux-gnu-gcc # linking flags here -LFLAGS = -I. +LFLAGS = -I. --entry main LIBS = $(EXTRA_LIBS) diff --git a/C/trace2/Makefile b/C/trace2/Makefile index ec102f3..9b2501f 100644 --- a/C/trace2/Makefile +++ b/C/trace2/Makefile @@ -4,11 +4,14 @@ TARGET_ARCH=riscv32 CC = riscv32-unknown-linux-gnu-gcc # compiling flags here -CFLAGS = -Wall -I. -O0 -nostdlib -march=rv32i -mabi=ilp32 +#CFLAGS = -Wall -I. -O0 -nostartfiles -march=rv32i -mabi=ilp32 +CFLAGS = -Wall -I. -O0 -nostdlib -march=rv32i -mabi=ilp32 --entry main +#CFLAGS = -Wall -I. -O0 -nodefaultlibs -march=rv32i -mabi=ilp32 + LINKER = riscv32-unknown-linux-gnu-gcc # linking flags here -LFLAGS = -I. +LFLAGS = -I. --entry main LIBS = $(EXTRA_LIBS) diff --git a/C/trace3/Makefile b/C/trace3/Makefile index 0abc24f..554024f 100644 --- a/C/trace3/Makefile +++ b/C/trace3/Makefile @@ -5,11 +5,13 @@ TARGET_ARCH=riscv32 CC = riscv32-unknown-linux-gnu-gcc # compiling flags here #CFLAGS = -Wall -I. -O0 -nostartfiles -march=rv32i -mabi=ilp32 -CFLAGS = -Wall -I. -O0 -nostdlib -march=rv32i -mabi=ilp32 +CFLAGS = -Wall -I. -O0 -nostdlib -march=rv32i -mabi=ilp32 --entry main +#CFLAGS = -Wall -I. -O0 -nodefaultlibs -march=rv32i -mabi=ilp32 + LINKER = riscv32-unknown-linux-gnu-gcc # linking flags here -LFLAGS = -I. +LFLAGS = -I. --entry main LIBS = $(EXTRA_LIBS)