Skip to content

Commit

Permalink
Risc-V CI
Browse files Browse the repository at this point in the history
  • Loading branch information
Chuck Benedict committed Jul 30, 2024
1 parent fa7aba4 commit c0360bf
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 10 deletions.
86 changes: 86 additions & 0 deletions .github/workflows/embedded.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
name: CI

env:
LLVM_RELEASE_VERSION_WINDOWS: 18
LLVM_RELEASE_VERSION_MAC: 18
LLVM_RELEASE_VERSION_LINUX: 18
LLVM_DEV_VERSION: 20
jobs:

build-linux:
runs-on: ubuntu-latest
strategy:
# Don't abort runners if a single one fails
fail-fast: false
matrix:
build_type: [Release]
llvm_version: [18]

steps:
- uses: actions/checkout@v4
- name: Install common deps
run: |
sudo apt-get install zlib1g zlib1g-dev python3 ninja-build curl
- name: Install Clang ${{matrix.llvm_version}}
run: |
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
if [[ "${{matrix.llvm_version}}" < 18 ]]; then
sudo add-apt-repository "deb http://apt.llvm.org/focal/ llvm-toolchain-focal-${{matrix.llvm_version}} main"
sudo apt-get update
sudo apt-get install -y -t llvm-toolchain-focal-${{matrix.llvm_version}} libpolly-${{matrix.llvm_version}}-dev \
clang-${{matrix.llvm_version}} llvm-${{matrix.llvm_version}} llvm-${{matrix.llvm_version}}-dev \
lld-${{matrix.llvm_version}} liblld-${{matrix.llvm_version}}-dev libmlir-${{matrix.llvm_version}} \
libmlir-${{matrix.llvm_version}}-dev mlir-${{matrix.llvm_version}}-tools
else
if [[ "${{matrix.llvm_version}}" < "${{env.LLVM_DEV_VERSION}}" ]]; then
sudo add-apt-repository "deb http://apt.llvm.org/focal/ llvm-toolchain-focal-${{matrix.llvm_version}} main"
sudo apt-get update
sudo apt-get install -y -t llvm-toolchain-focal-${{matrix.llvm_version}} libpolly-${{matrix.llvm_version}}-dev \
clang-${{matrix.llvm_version}} llvm-${{matrix.llvm_version}} llvm-${{matrix.llvm_version}}-dev \
lld-${{matrix.llvm_version}} liblld-${{matrix.llvm_version}}-dev
else
sudo add-apt-repository "deb http://apt.llvm.org/focal/ llvm-toolchain-focal main"
sudo apt-get install -y -t llvm-toolchain-focal libpolly-${{matrix.llvm_version}}-dev \
clang-${{matrix.llvm_version}} llvm-${{matrix.llvm_version}} llvm-${{matrix.llvm_version}}-dev \
lld-${{matrix.llvm_version}} liblld-${{matrix.llvm_version}}-dev
fi
fi
- name: CMake
if: matrix.llvm_version < 18 || matrix.llvm_version == env.LLVM_DEV_VERSION
run: |
cmake -B build \
-G Ninja \
-DCMAKE_BUILD_TYPE=${{matrix.build_type}} \
-DCMAKE_C_COMPILER=clang-${{matrix.llvm_version}} \
-DCMAKE_CXX_COMPILER=clang++-${{matrix.llvm_version}} \
-DCMAKE_LINKER=lld-link-${{matrix.llvm_version}} \
-DCMAKE_OBJCOPY=llvm-objcopy-${{matrix.llvm_version}} \
-DCMAKE_STRIP=llvm-strip-${{matrix.llvm_version}} \
-DCMAKE_DLLTOOL=llvm-dlltool-${{matrix.llvm_version}} \
-DC3_LLVM_VERSION=${{matrix.llvm_version}}
cmake --build build
- name: CMake18
if: matrix.llvm_version >= 18 && matrix.llvm_version != env.LLVM_DEV_VERSION
run: |
cmake -B build \
-G Ninja \
-DCMAKE_BUILD_TYPE=${{matrix.build_type}} \
-DCMAKE_C_COMPILER=clang-${{matrix.llvm_version}} \
-DCMAKE_CXX_COMPILER=clang++-${{matrix.llvm_version}} \
-DCMAKE_LINKER=lld-link-${{matrix.llvm_version}} \
-DCMAKE_OBJCOPY=llvm-objcopy-${{matrix.llvm_version}} \
-DCMAKE_STRIP=llvm-strip-${{matrix.llvm_version}} \
-DCMAKE_DLLTOOL=llvm-dlltool-${{matrix.llvm_version}} \
-DC3_LLVM_VERSION=${{matrix.llvm_version}}.1
cmake --build build
- name: Install QEMU and Risc-V toolchain
run: |
sudo apt-get install qemu gcc-riscv64-linux-gnu
- name: Compile and run Risc-V Example
run: |
cd resources/examples/embedded/riscv-qemu
make C3C_PATH=../../../../build/ run
8 changes: 3 additions & 5 deletions resources/examples/embedded/riscv-qemu/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ SRCS_C3 := $(wildcard *.c3)
default: hello.elf

hello.a: $(SRCS_C3)
c3c --use-stdlib=no --no-entry --target elf-riscv32 static-lib $(SRCS_C3)
$(C3C_PATH)c3c --use-stdlib=no --no-entry --target elf-riscv32 static-lib $(SRCS_C3)

start.o: start.s
riscv64-unknown-elf-as -g -march=rv32i -mabi=ilp32 -o start.o start.s
Expand All @@ -12,12 +12,10 @@ hello.elf: hello.a start.o baremetal.ld
riscv64-unknown-elf-ld -T baremetal.ld -m elf32lriscv -o hello.elf hello.a start.o

run: hello.elf
@echo "Ctrl-A C for QEMU console, then quit to exit"
qemu-system-riscv32 -nographic -serial mon:stdio -machine virt -bios hello.elf
qemu-system-riscv32 -nographic -serial mon:stdio -machine virt -semihosting -bios hello.elf

debug: hello.elf
@echo "Ctrl-A C for QEMU console, then quit to exit"
qemu-system-riscv32 -nographic -serial mon:stdio -machine virt -s -S -bios hello.elf
qemu-system-riscv32 -nographic -serial mon:stdio -machine virt -semihosting -s -S -bios hello.elf

clean:
rm -f *.o *.a hello.elf
3 changes: 2 additions & 1 deletion resources/examples/embedded/riscv-qemu/hello.c3
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import uart;
import semihost;

const UART0_BASE = 0x10000000;

fn void main() @export("main") {
Uart* uart0 = (Uart*)UART0_BASE; // Create pointer to UART 0
uart0.fcr = uart::UARTFCR_FFENA; // Enable FIFO
uart0.puts("Hello World!\n"); // Write the string to the UART
while (1); // Loop forever to prevent program from ending
semihost::exit(0); // Semihosting call to exit host
}
20 changes: 20 additions & 0 deletions resources/examples/embedded/riscv-qemu/semihost.c3
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module semihost;

// See: https://github.com/ARM-software/abi-aa/blob/main/semihosting/semihosting.rst#sys-exit-extended-0x20

extern fn int sys_semihost(int operation, SemihostParameters* parms);

struct SemihostParameters {
int field1;
int field2;
}

const int SYS_EXIT_EXTENDED = 0x20;
const int ADP_STOPPED_APPLICATIONEXIT = 0x20026;

fn void exit(int status) {
SemihostParameters parms;
parms.field1 = ADP_STOPPED_APPLICATIONEXIT;
parms.field2 = status;
sys_semihost(SYS_EXIT_EXTENDED, &parms);
}
20 changes: 16 additions & 4 deletions resources/examples/embedded/riscv-qemu/start.s
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,23 @@
.section .text._start
.global _start
_start:
la sp, __stack_top # Load the stack pointer
add s0, sp, zero # Set the frame pointer
jal zero, main # Run main entry point - no argc
la sp, __stack_top # Load the stack pointer
add s0, sp, zero # Set the frame pointer
jal zero, main # Run main entry point - no argc
loop: j loop # Spin forever in case main returns


# Support semi-hosting calls
.option norvc
.text
.balign 16
.global sys_semihost
.type sys_semihost @function
sys_semihost: # https://github.com/riscv-non-isa/riscv-semihosting/blob/main/src/binary-interface.adoc
slli zero, zero, 0x1f
ebreak
srai zero, zero, 0x7
ret

.section .data
.space 1024*8 # allocate 8K of memory to serve as initial stack
.align 16 # Smallest stack allocation is 16 bytes, so align accordingly
Expand Down

0 comments on commit c0360bf

Please sign in to comment.