Merge branch 'cmake_flow' into develop

This commit is contained in:
2025-05-22 12:19:52 +02:00
93 changed files with 3507 additions and 3935 deletions

View File

@ -1,55 +1,41 @@
IF(NOT DEFINED _MK_LIBWRAP)
SET(_MK_LIBWRAP TRUE)
SET(LIBWRAP_DIR ${CMAKE_CURRENT_LIST_DIR})
SET(LIBWRAP_SRCS
${LIBWRAP_DIR}/stdlib/malloc.c
${LIBWRAP_DIR}/sys/open.c
${LIBWRAP_DIR}/sys/lseek.c
${LIBWRAP_DIR}/sys/read.c
${LIBWRAP_DIR}/sys/write.c
${LIBWRAP_DIR}/sys/fstat.c
${LIBWRAP_DIR}/sys/stat.c
${LIBWRAP_DIR}/sys/close.c
${LIBWRAP_DIR}/sys/link.c
${LIBWRAP_DIR}/sys/unlink.c
${LIBWRAP_DIR}/sys/execve.c
${LIBWRAP_DIR}/sys/fork.c
${LIBWRAP_DIR}/sys/getpid.c
${LIBWRAP_DIR}/sys/kill.c
${LIBWRAP_DIR}/sys/wait.c
${LIBWRAP_DIR}/sys/isatty.c
${LIBWRAP_DIR}/sys/times.c
${LIBWRAP_DIR}/sys/sbrk.c
${LIBWRAP_DIR}/sys/_exit.c
${LIBWRAP_DIR}/misc/write_hex.c
${LIBWRAP_DIR}/sys/printf.c
${LIBWRAP_DIR}/sys/puts.c
include(CMakePrintHelpers)
set(LIB_SOURCES
sys/_exit.c
sys/close.c
sys/execve.c
sys/fork.c
sys/fstat.c
sys/getpid.c
sys/isatty.c
sys/kill.c
sys/link.c
sys/lseek.c
sys/open.c
sys/openat.c
sys/printf.c
sys/puts.c
sys/read.c
sys/sbrk.c
sys/stat.c
sys/times.c
sys/unlink.c
sys/wait.c
sys/write.c
# Standard library
stdlib/malloc.c
# Miscellaneous
misc/write_hex.c
)
IF(${SEMIHOSTING})
SET(LIBWRAP_SRCS ${LIBWRAP_SRCS} ${LIBWRAP_DIR}/semihosting/semihosting.c ${LIBWRAP_DIR}/semihosting/trap.c)
ENDIF()
set(WRAP_ARGS "")
foreach(FILE ${LIB_SOURCES})
get_filename_component(DIR ${FILE} DIRECTORY)
if(NOT DIR STREQUAL "misc")
get_filename_component(BASE_NAME ${FILE} NAME_WE)
list(APPEND WRAP_ARGS "LINKER:--wrap=${BASE_NAME}")
endif()
endforeach()
SET(LIBWRAP_SYMS malloc free open lseek read write fstat stat close link unlink execve fork getpid jukk wait isatty times sbrk _exit printf puts)
# Includes
INCLUDE_DIRECTORIES(
${LIBWRAP_DIR}
${LIBWRAP_DIR}/../include
${LIBWRAP_DIR}/../drivers
${LIBWRAP_DIR}/../env
${LIBWRAP_DIR}/../env/iss
)
add_library(wrap STATIC ${LIB_SOURCES} ../env/${BOARD_BASE}/bsp_write.c ../env/${BOARD_BASE}/bsp_read.c)
target_include_directories(wrap PUBLIC ../include)
target_link_options(wrap INTERFACE ${WRAP_ARGS})
ADD_LIBRARY(LIBWRAP_TGC STATIC ${LIBWRAP_SRCS})
TARGET_COMPILE_OPTIONS(LIBWRAP_TGC PRIVATE -march=${RISCV_ARCH}_zicsr_zifencei -mabi=${RISCV_ABI} "-DBOARD_${BOARD}")
FOREACH(SYM ${LIBWRAP_SYMS})
LIST(APPEND WRAP_LDFLAGS "-Wl,--wrap=${SYM}")
ENDFOREACH()
SET(LIBWRAP_TGC_LDFLAGS ${WRAP_LDFLAGS} "-Wl,--start-group" "-Wl,--end-group" "-L. -lLIBWRAP_TGC")
ENDIF(NOT DEFINED _MK_LIBWRAP)

View File

@ -2,18 +2,16 @@
#include <stdint.h>
#include <unistd.h>
#include "platform.h"
void write_hex(int fd, uint32_t hex)
{
void write_hex(int fd, uint32_t hex) {
uint8_t ii;
uint8_t jj;
char towrite;
write(fd , "0x", 2);
for (ii = 8 ; ii > 0; ii--) {
write(fd, "0x", 2);
for (ii = 8; ii > 0; ii--) {
jj = ii - 1;
uint8_t digit = ((hex & (0xF << (jj*4))) >> (jj*4));
towrite = digit < 0xA ? ('0' + digit) : ('A' + (digit - 0xA));
uint8_t digit = ((hex & (0xF << (jj * 4))) >> (jj * 4));
towrite = digit < 0xA ? ('0' + digit) : ('A' + (digit - 0xA));
write(fd, &towrite, 1);
}
}

View File

@ -1,195 +0,0 @@
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <sys/types.h>
#include "semihosting.h"
#define SEMIHOSTING_SYS_OPEN 0x01
#define SEMIHOSTING_SYS_CLOSE 0x02
#define SEMIHOSTING_SYS_WRITEC 0x03
#define SEMIHOSTING_SYS_WRITE0 0x04
#define SEMIHOSTING_SYS_WRITE 0x05
#define SEMIHOSTING_SYS_READ 0x06
#define SEMIHOSTING_SYS_READC 0x07
#define SEMIHOSTING_SYS_ISERROR 0x08
#define SEMIHOSTING_SYS_ISTTY 0x09
#define SEMIHOSTING_SYS_SEEK 0x0A
#define SEMIHOSTING_SYS_FLEN 0x0C
#define SEMIHOSTING_SYS_TMPNAM 0x0D
#define SEMIHOSTING_SYS_REMOVE 0x0E
#define SEMIHOSTING_SYS_RENAME 0x0F
#define SEMIHOSTING_SYS_CLOCK 0x10
#define SEMIHOSTING_SYS_TIME 0x11
#define SEMIHOSTING_SYS_SYSTEM 0x12
#define SEMIHOSTING_SYS_ERRNO 0x13
#define SEMIHOSTING_SYS_GET_CMDLINE 0x15
#define SEMIHOSTING_SYS_HEAPINFO 0x16
#define SEMIHOSTING_EnterSVC 0x17
#define SEMIHOSTING_SYS_EXIT 0x18
#define SEMIHOSTING_SYS_EXIT_EXTENDED 0x20
#define SEMIHOSTING_SYS_ELAPSED 0x30
#define SEMIHOSTING_SYS_TICKFREQ 0x31
#define RISCV_SEMIHOSTING_CALL_NUMBER 7
typedef struct {
char *str;
int mode;
size_t length;
} OpenVector;
typedef struct {
char *old;
int old_len;
char *new;
int new_len;
} RenameVector;
typedef struct {
char *path;
size_t len;
} RemoveVector;
typedef struct {
int fd;
off_t pos;
} SeekVector;
static inline int __attribute__((always_inline))
call_host(int reason, void *arg) {
#if 1
// This must always be set back to 0 to cover the case where a host wasn't
// initially present, but only connected while the program was already up and
// running. In that case, trap() suddenly won't be called anymore, so we have
// to clear this variable *before* EBREAK is called.
sh_missing_host = 0;
register int value asm("a0") = reason;
register void *ptr asm("a1") = arg;
asm volatile(
// Workaround for RISC-V lack of multiple EBREAKs.
" .option push \n"
" .option norvc \n"
// Force 16-byte alignment to make sure that the 3 instruction fall
// within the same virtual page. If you the instruction straddle a page
// boundary the debugger fetching the instructions could lead to a page
// fault. Note: align 4 means, align by 2 to the power of 4!
" .align 4 \n"
" slli x0, x0, 0x1f \n"
" ebreak \n"
" srai x0, x0, 0x07 \n"
" .option pop \n"
: "=r"(value) /* Outputs */
: "0"(value),
"r"(ptr), [swi] "i"(RISCV_SEMIHOSTING_CALL_NUMBER) /* Inputs */
: "memory" /* Clobbers */
);
return value;
#else
return 0;
#endif
}
int sh_errno(void) { return call_host(SEMIHOSTING_SYS_ERRNO, (void *)NULL); }
int sh_time(void) { return call_host(SEMIHOSTING_SYS_TIME, (void *)NULL); }
int sh_remove(char *path) {
size_t len = strlen(path);
RemoveVector vec = {path, len};
return call_host(SEMIHOSTING_SYS_REMOVE, &vec);
}
void sh_seek(int file_handle, off_t pos) {
SeekVector vec = {file_handle, pos};
call_host(SEMIHOSTING_SYS_SEEK, &vec);
return;
}
void sh_write(char *str, int file_handle) {
size_t length = strlen(str);
OpenVector vec = {str, file_handle, length};
call_host(SEMIHOSTING_SYS_WRITE, &vec);
return;
}
int sh_close(int file_handle) {
return call_host(SEMIHOSTING_SYS_CLOSE, file_handle);
}
void sh_exit(void) {
call_host(SEMIHOSTING_SYS_EXIT, (void *)NULL);
return;
}
void sh_exit_extended(void) {
call_host(SEMIHOSTING_SYS_EXIT_EXTENDED, (void *)NULL);
return;
}
int sh_flen(int file_handle) {
return call_host(SEMIHOSTING_SYS_FLEN, file_handle);
}
int sh_iserror(int num) { return call_host(SEMIHOSTING_SYS_ISERROR, num); }
int sh_istty(int file_handle) {
return call_host(SEMIHOSTING_SYS_ISTTY, file_handle);
}
/*
int sh_remove(char* path) {
size_t len = strlen(path);
RemoveVector vec = {path, len};
return call_host(SEMIHOSTING_SYS_REMOVE, &vec);
}*/
void sh_rename(char *old, char *new) {
int old_len = strlen(old);
int new_len = strlen(new);
RenameVector vec = {old, old_len, new, new_len};
call_host(SEMIHOSTING_SYS_RENAME, &vec);
return;
}
void sh_write0(const char *buf) {
// Print zero-terminated string
call_host(SEMIHOSTING_SYS_WRITE0, (void *)buf);
}
void sh_writec(char c) {
// Print single character
call_host(SEMIHOSTING_SYS_WRITEC, (void *)&c);
}
char sh_readc(void) {
// Read character from keyboard. (Blocking operation!)
char c = call_host(SEMIHOSTING_SYS_READC, (void *)NULL);
return c;
}
int sh_open(char *str, int mode) {
// mode = 0;
// int length = 44;
size_t length = strlen(str);
OpenVector vec = {str, mode, length};
return call_host(SEMIHOSTING_SYS_OPEN, &vec);
}
int sh_read(char *buf, int file_handle, size_t length) {
OpenVector vec = {buf, file_handle, length};
int i = call_host(SEMIHOSTING_SYS_READ, &vec);
return i;
}
int sh_clock(void) {
int clock = call_host(SEMIHOSTING_SYS_CLOCK, (void *)NULL);
return clock;
}
/*
void sh_write(char* str, int file_handle) {
return;
}*/

View File

@ -1,31 +0,0 @@
#ifndef SEMIHOSTING_H
#define SEMIHOSTING_H
#include <sys/types.h>
#include <unistd.h>
// int32_t trace_write(const char* buf, uint32_t nbyte);
void sh_seek(int, off_t);
void sh_write0(const char *buf);
void sh_writec(char c);
char sh_readc(void);
int sh_clock(void);
int sh_read(char *, int, size_t);
void sh_write(char *, int);
int sh_open(char *, int);
void sh_rename(char *, char *);
int sh_remove(char *);
int sh_istty(int);
int sh_iserror(int);
int sh_flen(int);
void sh_exit(void);
void sh_exit_extended(void);
int sh_close(int);
int sh_time(void);
int sh_errno(void);
int getchar(void);
extern int sh_missing_host;
#endif

View File

@ -1,60 +0,0 @@
#include <math.h>
#include <stdint.h>
#include <stdlib.h>
#include "encoding.h"
#if defined(SEMIHOSTING)
#define EBREAK_OPCODE 0x00100073
#define EBREAK_MCAUSE 0x00000003
#define SLLI_X0_X0_0X1F_OPCODE 0x01f01013
#define SRAI_X0_X0_0X07_OPCODE 0x40705013
int sh_missing_host = 0;
void trap() { // ToDo: Check why macro CSR_MEPC and others are not
// resolved
uint32_t mepc = read_csr(0x341); // Address of trap
uint32_t mtval = read_csr(0x343); // Instruction value of trap
uint32_t mcause = read_csr(0x342); // Reason for the trap
if (mcause == EBREAK_MCAUSE && mtval == EBREAK_OPCODE) {
// This trap was caused by an EBREAK...
int aligned = ((mepc - 4) & 0x0f) == 0;
if (aligned && *(uint32_t *)mepc == EBREAK_OPCODE &&
*(uint32_t *)(mepc - 4) == SLLI_X0_X0_0X1F_OPCODE &&
*(uint32_t *)(mepc + 4) == SRAI_X0_X0_0X07_OPCODE) {
// The EBREAK was part of the semihosting call. (See semihosting.c)
//
// If a debugger were connected, this would have resulted in a CPU halt,
// and the debugger would have serviced the the semihosting call.
//
// However, the semihosting function was called without a debugger being
// attached. The best course of action is to simply return from the trap
// and let the semihosting function continue after the call to EBREAK to
// prevent the CPU from hanging in the trap handler.
write_csr(mepc, mepc + 4);
// Set a global variable to tell the semihosting code the the semihosting
// call
// didn't execute on the host.
sh_missing_host = 1;
return;
}
// EBREAK was not part of a semihosting call. This should not have happened.
// Hang forever.
while (1)
;
}
// Trap was issued for another reason than an EBREAK.
// Replace the code below with whatever trap handler you'd normally use. (e.g.
// interrupt processing.)
while (1)
;
}
#endif

View File

@ -1,8 +1,9 @@
/* See LICENSE of license details. */
#include "platform.h"
#include "weak_under_alias.h"
//#include <stdint.h>
#include <unistd.h>
#include <stdint.h>
#if defined(SEMIHOSTING)
#include "semihosting.h"
#endif
@ -18,19 +19,17 @@ extern volatile uint32_t fromhost;
void write_hex(int fd, uint32_t hex);
void __wrap_exit(int code) {
/*#if defined(SEMIHOSTING)
sh_exit();
return;
#endif*/
// volatile uint32_t* leds = (uint32_t*) (GPIO_BASE_ADDR + GPIO_OUT_OFFSET);
const char message[] = "\nProgam has exited with code:";
//*leds = (~(code));
write(STDERR_FILENO, message, sizeof(message) - 1);
write_hex(STDERR_FILENO, code);
write(STDERR_FILENO, "\n", 1);
tohost = code + 1;
write(STDERR_FILENO, "\n", 1);
// tohost = (code << 1) + 1; // here used to stop simulation
write(STDERR_FILENO, "\x04", 1);
for (;;)
;
}

View File

@ -1,45 +1,11 @@
/* See LICENSE of license details. */
#include <errno.h>
#include <stdint.h>
#include <sys/types.h>
#include "weak_under_alias.h"
#include <string.h>
#include <unistd.h>
#include "platform.h"
#include "stub.h"
#include "weak_under_alias.h"
#if defined(SEMIHOSTING)
#include "semihosting.h"
#endif
extern ssize_t _bsp_write(int, const void *, size_t);
int __wrap_puts(const char *s) {
#if defined(SEMIHOSTING)
sh_write0(s);
return 0;
#endif
while (*s != '\0') {
#if defined(BOARD_ehrenberg) || defined(BOARD_tgc_vp)
while (get_uart_rx_tx_reg_tx_free(uart) == 0)
;
uart_write(uart, *s);
#elif defined(BOARD_iss)
*((uint32_t *)0xFFFF0000) = *s;
#elif defined(BOARD_TGCP)
// TODO: implement
#else
while (UART0_REG(UART_REG_TXFIFO) & 0x80000000)
;
UART0_REG(UART_REG_TXFIFO) = *s;
if (*s == '\n') {
while (UART0_REG(UART_REG_TXFIFO) & 0x80000000)
;
UART0_REG(UART_REG_TXFIFO) = '\r';
}
#endif
++s;
}
return 0;
int len = strlen(s);
return _bsp_write(STDOUT_FILENO, s, len);
}
weak_under_alias(puts);

View File

@ -1,62 +1,10 @@
/* See LICENSE of license details. */
#include <errno.h>
#include <stdint.h>
#include <sys/types.h>
#include "weak_under_alias.h"
#include <unistd.h>
#include "platform.h"
#include "stub.h"
#include "weak_under_alias.h"
#if defined(SEMIHOSTING)
#include "semihosting.h"
#endif
extern ssize_t _bsp_read(int fd, void *ptr, size_t len);
ssize_t __wrap_read(int fd, void *ptr, size_t len) {
uint8_t *current = (uint8_t *)ptr;
#if defined(SEMIHOSTING)
int i = sh_read(current, fd, len);
return i;
#endif
#if defined(BOARD_hifive1)
volatile uint32_t *uart_rx = (uint32_t *)(UART0_CTRL_ADDR + UART_REG_RXFIFO);
volatile uint8_t *uart_rx_cnt =
(uint8_t *)(UART0_CTRL_ADDR + UART_REG_RXCTRL + 2);
#elif defined(BOARD_iss)
volatile uint32_t *uart_rx = (uint32_t *)0xFFFF0000;
#elif defined(BOARD_TGCP)
// TODO: implement
#elif !defined(BOARD_ehrenberg) && !defined(BOARD_tgc_vp)
volatile uint32_t *uart_rx = (uint32_t *)(UART0_BASE_ADDR + UART_REG_RXFIFO);
volatile uint8_t *uart_rx_cnt =
(uint8_t *)(UART0_BASE_ADDR + UART_REG_RXCTRL + 2);
#endif
ssize_t result = 0;
if (isatty(fd)) {
#if defined(BOARD_ehrenberg) || defined(BOARD_tgc_vp)
for (current = (uint8_t *)ptr; (current < ((uint8_t *)ptr) + len) &&
(get_uart_rx_tx_reg_rx_avail(uart) > 0);
current++) {
*current = uart_read(uart);
result++;
}
#elif defined(BOARD_iss)
for (current = (uint8_t *)ptr; (current < ((uint8_t *)ptr) + len);
current++) {
*current = *uart_rx;
result++;
}
#elif defined(BOARD_TGCP)
// TODO: implement
#else
for (current = (uint8_t *)ptr;
(current < ((uint8_t *)ptr) + len) && (*uart_rx_cnt > 0); current++) {
*current = *uart_rx;
result++;
}
#endif
return result;
}
return _stub(EBADF);
return _bsp_read(fd, ptr, len);
}
weak_under_alias(read);

View File

@ -1,72 +1,9 @@
/* See LICENSE of license details. */
#include <errno.h>
#include <stdint.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdint.h>
#include "platform.h"
#include "stub.h"
#include "weak_under_alias.h"
#if defined(SEMIHOSTING)
#include "semihosting.h"
#endif
extern uint32_t tohost;
#include <stdint.h>
#include <unistd.h>
extern ssize_t _bsp_write(int, const void *, size_t);
ssize_t __wrap_write(int fd, const void *ptr, size_t len) {
const uint8_t *current = (const uint8_t *)ptr;
#if defined(SEMIHOSTING)
if (isatty(fd)) {
for (size_t jj = 0; jj < len; jj++) {
sh_writec(current[jj]);
}
return len;
} else {
sh_write(current, fd);
return len;
}
// return len;
#elif defined(BOARD_iss)
volatile uint64_t payload[4];
payload[0]= 64;
payload[1]= 0;
payload[2]= (uintptr_t)ptr;
payload[3]= len;
tohost = (uint32_t)payload;
return len;
#endif
if (isatty(fd)) {
for (size_t jj = 0; jj < len; jj++) {
#if defined(BOARD_ehrenberg) || defined(BOARD_tgc_vp)
while (get_uart_rx_tx_reg_tx_free(uart) == 0)
;
uart_write(uart, current[jj]);
if (current[jj] == '\n') {
while (get_uart_rx_tx_reg_tx_free(uart) == 0)
;
uart_write(uart, '\r');
}
#elif defined(BOARD_iss)
// *((uint32_t *)0xFFFF0000) = current[jj];
#elif defined(BOARD_TGCP)
// TODO: implement
#else
while (UART0_REG(UART_REG_TXFIFO) & 0x80000000)
;
UART0_REG(UART_REG_TXFIFO) = current[jj];
if (current[jj] == '\n') {
while (UART0_REG(UART_REG_TXFIFO) & 0x80000000)
;
UART0_REG(UART_REG_TXFIFO) = '\r';
}
#endif
}
return len;
}
return _stub(EBADF);
return _bsp_write(fd, ptr, len);
}
weak_under_alias(write);