From 5955f54a4d606de1ba23dfbdca160de98b859717 Mon Sep 17 00:00:00 2001 From: gabriel Date: Mon, 26 Feb 2024 20:41:13 +0100 Subject: [PATCH] first semihosting integration --- .gitignore | 3 ++ semihosting_test/CMakeLists.txt | 85 +++++++++++++++++++++++++++++++++ semihosting_test/test.c | 14 ++++++ 3 files changed, 102 insertions(+) create mode 100644 semihosting_test/CMakeLists.txt create mode 100644 semihosting_test/test.c diff --git a/.gitignore b/.gitignore index c6c7f67..ab6b6b6 100644 --- a/.gitignore +++ b/.gitignore @@ -151,3 +151,6 @@ compile_commands.json CTestTestfile.cmake *.dump +.vscode/c_cpp_properties.json +semihosting_test/build/semihosting_test +semihosting_test/build/Makefile diff --git a/semihosting_test/CMakeLists.txt b/semihosting_test/CMakeLists.txt new file mode 100644 index 0000000..0395857 --- /dev/null +++ b/semihosting_test/CMakeLists.txt @@ -0,0 +1,85 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 3.13 FATAL_ERROR) + + + +SET(BOARD "iss" CACHE STRING "board to be compiled for") + +SET(RISCV_ARCH "rv32imc" CACHE STRING "arch") +SET(RISCV_ABI "ilp32" CACHE STRING "abi") + +EXECUTE_PROCESS( + COMMAND which riscv64-unknown-elf-gcc + OUTPUT_VARIABLE COMPILER + OUTPUT_STRIP_TRAILING_WHITESPACE) + +GET_FILENAME_COMPONENT(TOOL_DIR ${COMPILER} DIRECTORY) + +SET(TRIPLET "riscv64-unknown-elf") + +SET(CMAKE_C_COMPILER ${TOOL_DIR}/${TRIPLET}-gcc) +SET(CMAKE_CXX_COMPILER ${TOOL_DIR}/${TRIPLET}-g++) +SET(CMAKE_ASM_COMPILER ${TOOL_DIR}/${TRIPLET}-gcc) +SET(CMAKE_LINKER ${TOOL_DIR}/${TRIPLET}-ld) +SET(CMAKE_OBJCOPY ${TOOL_DIR}/${TRIPLET}-objcopy) +SET(CMAKE_AR ${TOOL_DIR}/${TRIPLET}-ar) + +PROJECT(semihosting_test) + +SET(BSP_BASE "../bare-metal-bsp") +SET(ENV_DIR ${BSP_BASE}/env) +SET(PLATFORM_DIR ${ENV_DIR}/${BOARD}) + +SET(SEMIHOSTING TRUE) + +INCLUDE(${BSP_BASE}/libwrap/CMakeLists.txt) + +INCLUDE_DIRECTORIES( + ${BSP_BASE}/include + ${BSP_BASE}/drivers + ${BSP_BASE}/libwrap/semihosting + ${ENV_DIR} + ${PLATFORM_DIR}) + + +# Source files +SET(ASM_SRCS + ${ENV_DIR}/entry.S + ${ENV_DIR}/start.S +) +SET(C_SRCS + ${PLATFORM_DIR}/init.c + test.c +) +SET_SOURCE_FILES_PROPERTIES(${ASM_SRCS} PROPERTIES LANGUAGE C) + +# Compiler Flags +SET(COMMON_FLAGS "") + +# GCC Version Check +EXECUTE_PROCESS( + COMMAND ${CMAKE_C_COMPILER} --version + OUTPUT_VARIABLE GCC_VERSION +) +IF(GCC_VERSION MATCHES "9.2") + LIST(APPEND COMMON_FLAGS "-march=${RISCV_ARCH}") +ELSE() + LIST(APPEND COMMON_FLAGS "-march=${RISCV_ARCH}_zicsr_zifencei") +ENDIF() +LIST(APPEND COMMON_FLAGS "-mabi=${RISCV_ABI}" "-mcmodel=medany" "-DBOARD_${BOARD}") + +# Compiler Options +ADD_COMPILE_OPTIONS("${COMMON_FLAGS}") + +ADD_EXECUTABLE(${PROJECT_NAME} ${ASM_SRCS} ${C_SRCS} ${SRC_FILES}) + +TARGET_LINK_LIBRARIES(${PROJECT_NAME} PUBLIC ${LIBWRAP_TGC_LDFLAGS} LIBWRAP_TGC) + +# Linker Flags +TARGET_LINK_LIBRARIES(${PROJECT_NAME} + PUBLIC + -march=${RISCV_ARCH} -mabi=${RISCV_ABI} + -T/scratch/gabriel/repos/Firmwares/bare-metal-bsp/env/iss/link.lds + -Wl,-Map=${PROJECT_NAME}.map + -nostartfiles + -L${ENV_DIR} +) diff --git a/semihosting_test/test.c b/semihosting_test/test.c new file mode 100644 index 0000000..a0fcebf --- /dev/null +++ b/semihosting_test/test.c @@ -0,0 +1,14 @@ +#include +#include +#include + +#include "platform.h" +#include "encoding.h" +#include "semihosting.h" + +int main() +{ + char c = sh_readc(); + printf(c); + return 0; +}