diff --git a/.github/scripts/build-extra-tests.sh b/.github/scripts/build-extra-tests.sh index e38b50fe01..68ad20493c 100755 --- a/.github/scripts/build-extra-tests.sh +++ b/.github/scripts/build-extra-tests.sh @@ -7,5 +7,6 @@ set -ex SCRIPT_DIR="$( cd "$( dirname "$0" )" && pwd )" source $SCRIPT_DIR/defaults.sh -make -C $LOCAL_CHIPYARD_DIR/tests clean -make -C $LOCAL_CHIPYARD_DIR/tests +cmake $LOCAL_CHIPYARD_DIR/tests/ -S $LOCAL_CHIPYARD_DIR/tests/ -B $LOCAL_CHIPYARD_DIR/tests/build/ -D CMAKE_BUILD_TYPE=Debug +cmake --build $LOCAL_CHIPYARD_DIR/tests/build/ --target clean +cmake --build $LOCAL_CHIPYARD_DIR/tests/build/ --target all diff --git a/.github/scripts/run-tests.sh b/.github/scripts/run-tests.sh index 8a75285019..250f26b032 100755 --- a/.github/scripts/run-tests.sh +++ b/.github/scripts/run-tests.sh @@ -28,11 +28,17 @@ run_binary () { make run-binary-fast -C $LOCAL_SIM_DIR $DISABLE_SIM_PREREQ $MAPPING_FLAGS $@ } +build_tests() { + cmake $LOCAL_CHIPYARD_DIR/tests/ -S $LOCAL_CHIPYARD_DIR/tests/ -B $LOCAL_CHIPYARD_DIR/tests/build/ -D CMAKE_BUILD_TYPE=Debug + cmake --build $LOCAL_CHIPYARD_DIR/tests/build/ --target all +} + case $1 in chipyard-rocket) run_bmark LOADMEM=1 run_asm LOADMEM=1 - make -C $LOCAL_CHIPYARD_DIR/tests + build_tests + # Test run-binary with and without loadmem run_binary BINARY=$LOCAL_CHIPYARD_DIR/tests/hello.riscv LOADMEM=1 run_binary BINARY=$LOCAL_CHIPYARD_DIR/tests/hello.riscv @@ -77,7 +83,7 @@ case $1 in run_binary BINARY=$LOCAL_CHIPYARD_DIR/generators/compress-acc/software-zstd/compress/009987_cl0_ws12.riscv LOADMEM=1 ;; chipyard-manymmioaccels) - make -C $LOCAL_CHIPYARD_DIR/tests + build_tests # test streaming-passthrough run_binary BINARY=$LOCAL_CHIPYARD_DIR/tests/streaming-passthrough.riscv LOADMEM=1 @@ -87,31 +93,37 @@ case $1 in # test fft run_binary BINARY=$LOCAL_CHIPYARD_DIR/tests/fft.riscv LOADMEM=1 - ;; + ;; chipyard-nvdla) - make -C $LOCAL_CHIPYARD_DIR/tests + build_tests + run_binary BINARY=$LOCAL_CHIPYARD_DIR/tests/nvdla.riscv LOADMEM=1 - ;; + ;; chipyard-manyperipherals) - # SPI Flash read tests - make -C $LOCAL_CHIPYARD_DIR/tests + # SPI Flash read tests + build_tests + run_binary BINARY=$LOCAL_CHIPYARD_DIR/tests/spiflashread.riscv ;; chipyard-spiflashwrite) - make -C $LOCAL_CHIPYARD_DIR/tests + build_tests + run_binary BINARY=$LOCAL_CHIPYARD_DIR/tests/spiflashwrite.riscv LOADMEM=1 [[ "`xxd $LOCAL_CHIPYARD_DIR/tests/spiflash.img | grep 1337\ 00ff\ aa55\ face | wc -l`" == "6" ]] || false ;; chipyard-tethered) - make -C $LOCAL_CHIPYARD_DIR/tests + build_tests + run_binary BINARY=$LOCAL_CHIPYARD_DIR/tests/hello.riscv LOADMEM=1 EXTRA_SIM_FLAGS="+cflush_addr=0x2010200" ;; chipyard-symmetric) - make -C $LOCAL_CHIPYARD_DIR/tests + build_tests + run_binary BINARY=$LOCAL_CHIPYARD_DIR/tests/symmetric.riscv LOADMEM=1 ;; chipyard-llcchiplet) - make -C $LOCAL_CHIPYARD_DIR/tests + build_tests + run_binary BINARY=$LOCAL_CHIPYARD_DIR/tests/hello.riscv LOADMEM=1 ;; chipyard-rerocc) diff --git a/.gitignore b/.gitignore index d47102810d..b107b6dd83 100644 --- a/.gitignore +++ b/.gitignore @@ -29,3 +29,4 @@ project/project/ .sbt .classpath_cache/ .vscode/ +tests/build/ diff --git a/docs/Software/Baremetal.rst b/docs/Software/Baremetal.rst index 4bfeaef4bc..96bc2fac13 100644 --- a/docs/Software/Baremetal.rst +++ b/docs/Software/Baremetal.rst @@ -22,6 +22,16 @@ To build baremetal RISC-V programs to run in simulation, we use the riscv64-unkn $ spike hello.riscv Hello, World! -For more examples, look at the `tests/ directory `_ in the chipyard repository. +We have provided a set of example programs in the `tests/ directory `_ in the chipyard repository. + +The tests directory contains a CMakeLists.txt file that can be used to build the programs. To build the programs, you can use the following commands: + +.. code:: bash + + $ cmake . -S ./ -B ./build/ -D CMAKE_BUILD_TYPE=Debug + $ cmake --build ./build/ --target all + $ spike hello.riscv + Hello, World! + For more information about the libgloss port, take a look at `its README `_. diff --git a/scripts/build-tests.sh b/scripts/build-tests.sh new file mode 100755 index 0000000000..3967e982f8 --- /dev/null +++ b/scripts/build-tests.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +cmake ./tests/ -S ./tests/ -B ./tests/build/ -D CMAKE_BUILD_TYPE=Debug +cmake --build ./tests/build/ --target all diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000000..ebe8f39404 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,103 @@ +######################################################################################################################## +# file: CMakeLists.txt +# +# usage: +# Edit "VARIABLES"-section to suit project requirements. +# Build instructions: +# cmake . -S ./ -B ./build/ -D CMAKE_BUILD_TYPE=Debug +# cmake --build ./build/ --target all +# Cleaning: +# cmake --build ./build/ --target clean +######################################################################################################################## +cmake_minimum_required(VERSION 3.10) + +project(chipyard-tests LANGUAGES C CXX) + + +################################# +# RISCV Toolchain +################################# + +set(CMAKE_SYSTEM_NAME "Generic" CACHE STRING "") +set(CMAKE_SYSTEM_PROCESSOR "riscv" CACHE STRING "") + +set(TOOLCHAIN_PREFIX "riscv64-unknown-elf-") + +set(CMAKE_AR "${TOOLCHAIN_PREFIX}ar") +set(CMAKE_ASM_COMPILER "${TOOLCHAIN_PREFIX}gcc") +set(CMAKE_C_COMPILER "${TOOLCHAIN_PREFIX}gcc") +set(CMAKE_CXX_COMPILER "${TOOLCHAIN_PREFIX}g++") +set(CMAKE_LINKER "${TOOLCHAIN_PREFIX}ld") +set(CMAKE_OBJCOPY "${TOOLCHAIN_PREFIX}objcopy") +set(CMAKE_OBJDUMP "${TOOLCHAIN_PREFIX}objdump") +set(CMAKE_SIZE "${TOOLCHAIN_PREFIX}size") + + +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/..") +set(CMAKE_EXECUTABLE_SUFFIX ".riscv") + + +################################# +# Flags +################################# + +# CPU architecture +set(ARCH "rv64imafd") +set(ABI "lp64d") +set(CMODEL "medany") +set(ARCH_FLAGS -march=${ARCH} -mabi=${ABI} -mcmodel=${CMODEL}) + +# spec +set(SPECS "htif_nano.specs") +set(SPEC_FLAGS -specs=${SPECS}) + +# linker script +set(LINKER_SCRIPT "htif.ld") + +add_compile_options(-std=gnu99) +add_compile_options(-O2 -Wall -Wextra) +add_compile_options(-fno-common -fno-builtin-printf) +add_compile_options(${ARCH_FLAGS}) +add_compile_options(${SPEC_FLAGS}) + +add_link_options(-static) +add_link_options(${SPEC_FLAGS}) +add_link_options(-T ${LINKER_SCRIPT}) + + +################################# +# Build +################################# + +add_executable(pwm pwm.c) +add_executable(blkdev blkdev.c) +add_executable(accum accum.c) +add_executable(charcount charcount.c) +add_executable(cpp-hello cpp-hello.cpp) +add_executable(nic-loopback nic-loopback.c) +add_executable(big-blkdev big-blkdev.c) +add_executable(pingd pingd.c) +add_executable(streaming-passthrough streaming-passthrough.c) +add_executable(streaming-fir streaming-fir.c) +add_executable(nvdla nvdla.c) +add_executable(spiflashread spiflashread.c) +add_executable(spiflashwrite spiflashwrite.c) +add_executable(fft fft.c) +add_executable(gcd gcd.c) +add_executable(hello hello.c) +add_executable(mt-hello mt-hello.c) +add_executable(symmetric symmetric.c) + + +# Add custom command to generate spiflash.img from spiflash.py +add_custom_command( + OUTPUT ${CMAKE_BINARY_DIR}/spiflash.img + COMMAND python3 ${CMAKE_SOURCE_DIR}/spiflash.py --outfile ${CMAKE_SOURCE_DIR}/spiflash.img + DEPENDS ${CMAKE_SOURCE_DIR}/spiflash.py + COMMENT "Generating spiflash.img" +) + +# Add a target for spiflash.img +add_custom_target(spiflash_img ALL + DEPENDS ${CMAKE_BINARY_DIR}/spiflash.img +) diff --git a/tests/Makefile b/tests/Makefile deleted file mode 100644 index 1c6df31b5c..0000000000 --- a/tests/Makefile +++ /dev/null @@ -1,71 +0,0 @@ -################################# -# RISCV Toolchain -################################# - -TARGET = riscv64-unknown-elf - -GCC = $(TARGET)-gcc -CXX = $(TARGET)-g++ -CP = $(TARGET)-objcopy -OBJDUMP = $(TARGET)-objdump -DG = $(TARGET)-gdb -SIZE = $(TARGET)-size - - -################################# -# Flags -################################# - -# SoC Settings -ARCH = rv64imafdc -ABI = lp64d -ARCHFLAGS = -march=$(ARCH) -mabi=$(ABI) - -CFLAGS = -std=gnu99 -O2 -fno-common -fno-builtin-printf -Wall -CFLAGS += $(ARCHFLAGS) -LDFLAGS = -static - -include libgloss.mk - -PROGRAMS = pwm blkdev accum charcount nic-loopback big-blkdev pingd \ - streaming-passthrough streaming-fir nvdla spiflashread spiflashwrite fft gcd \ - hello mt-hello symmetric - - -.DEFAULT_GOAL := default - - -################################# -# Build -################################# - -spiflash.img: spiflash.py - python3 $< - -%.o: %.S - $(GCC) $(CFLAGS) -D__ASSEMBLY__=1 -c $< -o $@ - -%.o: %.c mmio.h spiflash.h - $(GCC) $(CFLAGS) -c $< -o $@ - -%.riscv: %.o $(libgloss) - $(GCC) $(LDFLAGS) $< -o $@ - -%.dump: %.riscv - $(OBJDUMP) -D $< > $@ - - -################################# -# Recipes -################################# - -.PHONY: clean -clean: - rm -f *.riscv *.o *.dump - $(if $(libgloss),rm -rf $(libgloss_builddir)/) - -.PHONY: default -default: $(addsuffix .riscv, $(PROGRAMS)) spiflash.img - -.PHONY: dumps -dumps: $(addsuffix .dump, $(PROGRAMS)) diff --git a/tests/cpp-hello.cpp b/tests/cpp-hello.cpp new file mode 100644 index 0000000000..f9ad6f154a --- /dev/null +++ b/tests/cpp-hello.cpp @@ -0,0 +1,10 @@ +#include +using namespace std; + +// HACK: This is a workaround for the missing __dso_handle routine in the current toolchain +extern "C" void *__dso_handle = 0; + +int main() { + cout << "Hello World!"; + return 0; +} diff --git a/tests/libgloss.mk b/tests/libgloss.mk deleted file mode 100644 index 5553bdbdd3..0000000000 --- a/tests/libgloss.mk +++ /dev/null @@ -1,54 +0,0 @@ -# Handle libgloss-htif dependency -ifndef libgloss - -ifndef GCC -$(error GCC is not defined) -endif - -ifndef TARGET -$(error TARGET is not defined) -endif - -libgloss_specs := htif_nano.specs - -# Test whether libgloss-htif is globally installed and usable -# Define BUILD_LIBGLOSS=1 to unconditionally force a local build -BUILD_LIBGLOSS ?= $(shell { echo 'int main(void) { return 0; }' | \ - $(GCC) -xc -specs=$(libgloss_specs) -o /dev/null - 2> /dev/null ; } || \ - echo "$$?") - -ifneq ($(BUILD_LIBGLOSS),) -$(info libgloss-htif: Using local build) - -libgloss_srcdir := ../toolchains/libgloss -libgloss_builddir := libgloss -libgloss_specs := $(libgloss_srcdir)/util/$(libgloss_specs) -libgloss_lib := $(libgloss_builddir)/libgloss_htif.a -libgloss := $(libgloss_lib) $(libgloss_specs) htif.ld - -LDFLAGS += -L libgloss - -$(libgloss_builddir)/Makefile: $(libgloss_srcdir)/configure - mkdir -p $(dir $@) - cd $(dir $@) && $(realpath $<) \ - --prefix=$(shell $(GCC) -print-sysroot) \ - --host=$(TARGET) \ - --disable-multilib - -$(libgloss_lib): $(libgloss_builddir)/Makefile - $(MAKE) -C $(dir $^) - -.PHONY: libgloss -libgloss: $(libgloss) - -else - -$(info libgloss-htif: Using global install) -libgloss := # No additional prerequisites - -endif - -CFLAGS += -specs=$(libgloss_specs) -LDFLAGS += -specs=$(libgloss_specs) - -endif # libgloss diff --git a/tests/spiflash.py b/tests/spiflash.py index af65b64e9a..1bf6a0d235 100755 --- a/tests/spiflash.py +++ b/tests/spiflash.py @@ -1,10 +1,16 @@ #!/usr/bin/env python3 - # Generates a binary file that the SPI test uses -outfile = "spiflash.img" +import argparse + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description="Generate a binary file for SPI test") + parser.add_argument("--outfile", type=str, default="spiflash.img", help="Output file") + args = parser.parse_args() + + outfile = args.outfile -with open(outfile, 'wb') as f: - for i in range(0,0x100000,4): - check = 0xdeadbeef - i - f.write(check.to_bytes(4,'little')) + with open(outfile, "wb") as f: + for i in range(0,0x100000,4): + check = 0xdeadbeef - i + f.write(check.to_bytes(4, "little"))