Skip to content

Commit 25aa104

Browse files
committed
Initial commit
0 parents  commit 25aa104

26 files changed

+6156
-0
lines changed

.clang-format

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
BasedOnStyle: Chromium
2+
Language: Cpp
3+
MaxEmptyLinesToKeep: 3
4+
IndentCaseLabels: false
5+
AllowShortIfStatementsOnASingleLine: false
6+
AllowShortCaseLabelsOnASingleLine: false
7+
AllowShortLoopsOnASingleLine: false
8+
DerivePointerAlignment: false
9+
PointerAlignment: Right
10+
SpaceAfterCStyleCast: true
11+
TabWidth: 4
12+
UseTab: Never
13+
IndentWidth: 4
14+
BreakBeforeBraces: Linux
15+
AccessModifierOffset: -4
16+
AttributeMacros:
17+
- UNUSED
18+
- __ALIGNED
19+
- PACKED
20+
- MUST_TAIL
21+
- FORCE_INLINE
22+
StatementAttributeLikeMacros:
23+
- IIF
24+
StatementMacros:
25+
- __UNREACHABLE

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build/

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "riscv-gnu-toolchain"]
2+
path = riscv-gnu-toolchain
3+
url = https://github.com/riscv-collab/riscv-gnu-toolchain.git

Makefile

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# TODO: check unsupported extensions
2+
RV32_EXT ?= rv32i
3+
OPT_LEVEL := 0 1 2
4+
5+
CFLAGS := -std=c99
6+
LDFLAGS := -lsemihost -lm
7+
8+
SRC_DIR := src
9+
BUILD_DIR := build
10+
11+
SRCS := \
12+
captcha.c \
13+
fcalc.c \
14+
mandelbrot.c \
15+
nqueens.c \
16+
nyancat.c \
17+
pi.c \
18+
puzzle.c \
19+
qrcode.c \
20+
richards.c \
21+
spirograph.c
22+
23+
ifneq ($(findstring f,$(RV32_EXT)),)
24+
SRCS += \
25+
ieee754.c
26+
endif
27+
28+
EXECUTABLES := $(SRCS:%.c=%.elf)
29+
30+
SHELL_HACK := $(shell mkdir -p $(foreach LEVEL,$(OPT_LEVEL),$(BUILD_DIR)/$(RV32_EXT)/O$(LEVEL)))
31+
32+
.PHONY: all clean distclean build-O0 build-O1 build-O2
33+
34+
ifneq ($(filter $(OPT_LEVEL), 0),)
35+
all: build-O0
36+
endif
37+
38+
ifneq ($(filter $(OPT_LEVEL), 1),)
39+
all: build-O1
40+
endif
41+
42+
ifneq ($(filter $(OPT_LEVEL), 2),)
43+
all: build-O2
44+
endif
45+
46+
include mk/common.mk
47+
include mk/toolchain.mk
48+
49+
build-O0: $(addprefix $(BUILD_DIR)/$(RV32_EXT)/O0/,$(EXECUTABLES))
50+
51+
$(BUILD_DIR)/$(RV32_EXT)/O0/%.elf: $(SRC_DIR)/%.c | build-toolchain
52+
$(VECHO) " RISCVCC\t$@\n"
53+
$(Q)$(CROSS_COMPILE) -o $@ -O0 $(CFLAGS) $< $(LDFLAGS)
54+
55+
build-O1: $(addprefix $(BUILD_DIR)/$(RV32_EXT)/O1/,$(EXECUTABLES))
56+
57+
$(BUILD_DIR)/$(RV32_EXT)/O1/%.elf: $(SRC_DIR)/%.c | build-toolchain
58+
$(VECHO) " RISCVCC\t$@\n"
59+
$(Q)$(CROSS_COMPILE) -o $@ -O1 $(CFLAGS) $< $(LDFLAGS)
60+
61+
build-O2: $(addprefix $(BUILD_DIR)/$(RV32_EXT)/O2/,$(EXECUTABLES))
62+
63+
$(BUILD_DIR)/$(RV32_EXT)/O2/%.elf: $(SRC_DIR)/%.c | build-toolchain
64+
$(VECHO) " RISCVCC\t$@\n"
65+
$(Q)$(CROSS_COMPILE) -o $@ -O2 $(CFLAGS) $< $(LDFLAGS)
66+
67+
clean:
68+
$(RM) -r $(BUILD_DIR)/$(RV32_EXT)/*
69+
70+
distclean: clean
71+
$(RM) -r $(BUILD_DIR)/*
72+
$(RM) -r $(RISCV_TOOLCHAIN_DIR)/build/*
73+
$(MAKE) -C $(RISCV_TOOLCHAIN_DIR) distclean

mk/common.mk

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
ifeq ($(shell uname -s),Darwin)
2+
PRINTF = printf
3+
else
4+
PRINTF = env printf
5+
endif
6+
7+
ifeq ("$(VERBOSE)","1")
8+
Q :=
9+
VECHO = @true
10+
REDIR =
11+
else
12+
Q := @
13+
VECHO = @$(PRINTF)
14+
REDIR = >/dev/null
15+
endif

mk/toolchain.mk

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
RV32_PREFIX := riscv32-unknown-elf-
2+
3+
RISCV_TOOLCHAIN_DIR := riscv-gnu-toolchain
4+
SHELL_HACK := $(shell mkdir -p $(RISCV_TOOLCHAIN_DIR)/build/$(RV32_EXT))
5+
6+
CROSS_COMPILE := $(shell find $(RISCV_TOOLCHAIN_DIR)/build/$(RV32_EXT) -name $(RV32_PREFIX)gcc)
7+
8+
.PHONY: build-toolchain
9+
10+
build-toolchain:
11+
ifeq ($(CROSS_COMPILE),)
12+
git submodule update --init $(RISCV_TOOLCHAIN_DIR)
13+
cd $(RISCV_TOOLCHAIN_DIR) && ./configure \
14+
--prefix=$(shell pwd)/$(RISCV_TOOLCHAIN_DIR)/build/$(RV32_EXT) \
15+
--with-arch=$(RV32_EXT) \
16+
--with-abi=ilp32 \
17+
--disable-gdb
18+
$(MAKE) -C $(RISCV_TOOLCHAIN_DIR) clean all
19+
endif

riscv-gnu-toolchain

Submodule riscv-gnu-toolchain added at f133b29

src/asm-hello/Makefile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
include ../../mk/toolchain.mk
2+
3+
ASFLAGS := -march=rv32i -mabi=ilp32
4+
LDFLAGS := --oformat=elf32-littleriscv
5+
6+
.PHONY: all clean
7+
8+
%.o: %.S
9+
$(CROSS_COMPILE)as -R $(ASFLAGS) -o $@ $<
10+
11+
all: hello.elf
12+
13+
hello.elf: hello.o
14+
$(CROSS_COMPILE)ld -o $@ -T hello.ld $(LDFLAGS) $<
15+
16+
clean:
17+
$(RM) hello.elf hello.o

src/asm-hello/hello.S

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# RISC-V assembly program to print "Hello World!" to stdout.
2+
3+
.org 0
4+
# Provide program starting address to linker
5+
.global _start
6+
7+
/* newlib system calls */
8+
.set SYSEXIT, 93
9+
.set SYSWRITE, 64
10+
11+
.section .rodata
12+
str: .ascii "Hello World!\n"
13+
.set str_size, .-str
14+
15+
.text
16+
_start:
17+
li t0, 0
18+
li t1, 5
19+
20+
# dummy test for jal instruction
21+
.L1:
22+
jal x0, .L2
23+
.L2:
24+
nop
25+
26+
loop:
27+
beq t0, t1, end
28+
29+
li a7, SYSWRITE # "write" syscall
30+
li a0, 1 # 1 = standard output (stdout)
31+
la a1, str # load address of hello string
32+
li a2, str_size # length of hello string
33+
ecall # invoke syscall to print the string
34+
addi t0, t0, 1
35+
j loop
36+
37+
end:
38+
li a7, SYSEXIT # "exit" syscall
39+
add a0, x0, 0 # Use 0 return code
40+
ecall # invoke syscall to terminate the program

src/asm-hello/hello.ld

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
OUTPUT_ARCH("riscv")
2+
ENTRY(_start)
3+
4+
SECTIONS
5+
{
6+
. = 0x0;
7+
}

0 commit comments

Comments
 (0)