Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADD: Add CMake build option for tests and example C++ program #2036

Merged
merged 14 commits into from
Sep 9, 2024
Merged
5 changes: 3 additions & 2 deletions .github/scripts/build-extra-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
34 changes: 23 additions & 11 deletions .github/scripts/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ project/project/
.sbt
.classpath_cache/
.vscode/
tests/build/
12 changes: 11 additions & 1 deletion docs/Software/Baremetal.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/ucb-bar/chipyard/tree/master/tests>`_ in the chipyard repository.
We have provided a set of example programs in the `tests/ directory <https://github.com/ucb-bar/chipyard/tree/master/tests>`_ 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 <https://github.com/ucb-bar/libgloss-htif/blob/master/README.md>`_.
4 changes: 4 additions & 0 deletions scripts/build-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

cmake ./tests/ -S ./tests/ -B ./tests/build/ -D CMAKE_BUILD_TYPE=Debug
T-K-233 marked this conversation as resolved.
Show resolved Hide resolved
cmake --build ./tests/build/ --target all
103 changes: 103 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
)
71 changes: 0 additions & 71 deletions tests/Makefile

This file was deleted.

10 changes: 10 additions & 0 deletions tests/cpp-hello.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <iostream>
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;
}
54 changes: 0 additions & 54 deletions tests/libgloss.mk

This file was deleted.

18 changes: 12 additions & 6 deletions tests/spiflash.py
Original file line number Diff line number Diff line change
@@ -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"))
Loading