Adding semihosting support for bmsp

This commit is contained in:
gabriel
2024-04-08 14:36:22 +02:00
parent 39ee91af7d
commit e83f7996bc
14 changed files with 175 additions and 7 deletions

View File

@ -1,6 +1,7 @@
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <string.h>
#include "semihosting.h"
@ -25,12 +26,31 @@
#define SEMIHOSTING_SYS_GET_CMDLINE 0x15
#define SEMIHOSTING_SYS_HEAPINFO 0x16
#define SEMIHOSTING_EnterSVC 0x17
#define SEMIHOSTING_ReportException 0x18
#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;
int len;
} RemoveVector;
*/
static inline int __attribute__ ((always_inline)) call_host(int reason, void* arg) {
#if 1
@ -66,6 +86,71 @@ static inline int __attribute__ ((always_inline)) call_host(int reason, void* ar
#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);
}
void sh_seek(int file_handle, int pos) {
int vec[2] = {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) {
int 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
@ -82,9 +167,26 @@ char sh_readc(void)
{
// Read character from keyboard. (Blocking operation!)
char c = call_host(SEMIHOSTING_SYS_READC, (void*)NULL);
return c;
}
if (sh_missing_host)
return 0;
else
return c;
int sh_open(char* str, int mode) {
//mode = 0;
//int length = 44;
size_t length = strlen(str);
OpenVector vec = {str, mode, length};
int i = call_host(SEMIHOSTING_SYS_OPEN, &vec);
return i;
}
void 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;
}

View File

@ -6,6 +6,20 @@
void sh_write0(const char* buf);
void sh_writec(char c);
char sh_readc(void);
int sh_clock(void);
void 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);

View File

@ -13,7 +13,7 @@
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