Compare commits
2 Commits
fc3f6c57e5
...
develop
| Author | SHA1 | Date | |
|---|---|---|---|
| d8728e319c | |||
| fce1372494 |
@@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.21)
|
|||||||
include(CheckLinkerFlag)
|
include(CheckLinkerFlag)
|
||||||
|
|
||||||
project(mnrs-bsp LANGUAGES ASM C)
|
project(mnrs-bsp LANGUAGES ASM C)
|
||||||
|
option(NO_INIT "use an empty init routine" OFF)
|
||||||
set(LINKER_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/env/${BOARD}/link.lds"
|
set(LINKER_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/env/${BOARD}/link.lds"
|
||||||
CACHE FILEPATH "Linker script to use for BSP linking")
|
CACHE FILEPATH "Linker script to use for BSP linking")
|
||||||
get_filename_component(LINKER_SCRIPT_DIR "${LINKER_SCRIPT}" DIRECTORY)
|
get_filename_component(LINKER_SCRIPT_DIR "${LINKER_SCRIPT}" DIRECTORY)
|
||||||
@@ -30,18 +31,22 @@ target_include_directories(startup PUBLIC env include)
|
|||||||
|
|
||||||
add_subdirectory(libwrap)
|
add_subdirectory(libwrap)
|
||||||
|
|
||||||
add_library(bsp STATIC env/${BOARD}/init.c)
|
add_library(runtime STATIC env/${BOARD}/init.c)
|
||||||
target_link_libraries(bsp PUBLIC startup wrap)
|
target_include_directories(runtime PUBLIC env/${BOARD} env include)
|
||||||
target_include_directories(bsp PUBLIC env/${BOARD})
|
if(NO_INIT)
|
||||||
|
target_compile_definitions(runtime PRIVATE NO_INIT)
|
||||||
|
endif()
|
||||||
|
|
||||||
check_linker_flag(C "LINKER:--no-warn-rwx-segments" HAS_NO_WARN_RWX_SEGMENTS)
|
check_linker_flag(C "LINKER:--no-warn-rwx-segments" HAS_NO_WARN_RWX_SEGMENTS)
|
||||||
|
|
||||||
if(HAS_NO_WARN_RWX_SEGMENTS)
|
if(HAS_NO_WARN_RWX_SEGMENTS)
|
||||||
target_link_options(bsp INTERFACE LINKER:--no-warn-rwx-segments)
|
target_link_options(runtime INTERFACE LINKER:--no-warn-rwx-segments)
|
||||||
endif()
|
endif()
|
||||||
target_link_options(bsp INTERFACE LINKER: -nostartfiles -T ${LINKER_SCRIPT} -L${LINKER_SCRIPT_DIR})
|
target_link_options(runtime INTERFACE LINKER: -nostartfiles -T ${LINKER_SCRIPT} -L${LINKER_SCRIPT_DIR})
|
||||||
|
|
||||||
if(SEMIHOSTING)
|
if(SEMIHOSTING)
|
||||||
target_include_directories(bsp INTERFACE include)
|
target_sources(runtime INTERFACE env/semihosting.c env/trap.c)
|
||||||
target_sources(bsp INTERFACE env/semihosting.c env/trap.c)
|
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
add_library(bsp INTERFACE)
|
||||||
|
target_link_libraries(bsp INTERFACE startup runtime wrap)
|
||||||
|
|
||||||
|
|||||||
9
env/entry.S
vendored
9
env/entry.S
vendored
@@ -10,7 +10,11 @@
|
|||||||
.align 2
|
.align 2
|
||||||
.global trap_entry
|
.global trap_entry
|
||||||
trap_entry:
|
trap_entry:
|
||||||
addi sp, sp, -32*REGBYTES
|
#ifdef __riscv_abi_rve
|
||||||
|
addi sp, sp, -12*REGBYTES
|
||||||
|
#else
|
||||||
|
addi sp, sp, -28*REGBYTES
|
||||||
|
#endif
|
||||||
|
|
||||||
sw x1, 1*REGBYTES(sp)
|
sw x1, 1*REGBYTES(sp)
|
||||||
sw x5, 5*REGBYTES(sp)
|
sw x5, 5*REGBYTES(sp)
|
||||||
@@ -78,6 +82,9 @@ trap_entry:
|
|||||||
lw x29, 29*REGBYTES(sp)
|
lw x29, 29*REGBYTES(sp)
|
||||||
lw x30, 30*REGBYTES(sp)
|
lw x30, 30*REGBYTES(sp)
|
||||||
lw x31, 31*REGBYTES(sp)
|
lw x31, 31*REGBYTES(sp)
|
||||||
|
addi sp, sp, 28*REGBYTES
|
||||||
|
#else
|
||||||
|
addi sp, sp, 12*REGBYTES
|
||||||
#endif
|
#endif
|
||||||
mret
|
mret
|
||||||
|
|
||||||
|
|||||||
98
env/riscv_vp/init.c
vendored
98
env/riscv_vp/init.c
vendored
@@ -2,9 +2,8 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "platform.h"
|
|
||||||
#include "encoding.h"
|
#include "encoding.h"
|
||||||
|
#include "platform.h"
|
||||||
|
|
||||||
extern int main(int argc, char** argv);
|
extern int main(int argc, char** argv);
|
||||||
extern void trap_entry(void);
|
extern void trap_entry(void);
|
||||||
@@ -12,98 +11,76 @@ extern void trap_entry(void);
|
|||||||
#define IRQ_M_TIMER 7
|
#define IRQ_M_TIMER 7
|
||||||
#define IRQ_M_EXT 11
|
#define IRQ_M_EXT 11
|
||||||
|
|
||||||
#define NUM_INTERRUPTS 16
|
|
||||||
#define MTIMER_NEXT_TICK_INC 1000
|
|
||||||
|
|
||||||
void handle_m_ext_interrupt(void);
|
void handle_m_ext_interrupt(void);
|
||||||
void handle_m_time_interrupt(void);
|
void handle_m_time_interrupt(void);
|
||||||
uint32_t handle_trap(uint32_t mcause, uint32_t mepc, uint32_t sp);
|
uint32_t handle_trap(uint32_t mcause, uint32_t mepc, uint32_t sp);
|
||||||
void default_handler(void);
|
void default_handler(void);
|
||||||
void _init(void);
|
void _init(void);
|
||||||
|
|
||||||
typedef void (*my_interrupt_function_ptr_t) (void);
|
typedef void (*my_interrupt_function_ptr_t)(void);
|
||||||
my_interrupt_function_ptr_t localISR[NUM_INTERRUPTS] __attribute__((aligned(64)));
|
my_interrupt_function_ptr_t localISR[NUM_INTERRUPTS] __attribute__((aligned(64)));
|
||||||
|
|
||||||
static unsigned long mtime_lo(void)
|
static unsigned long mtime_lo(void) {
|
||||||
{
|
|
||||||
unsigned long ret;
|
unsigned long ret;
|
||||||
__asm volatile("rdtime %0":"=r"(ret));
|
__asm volatile("rdtime %0" : "=r"(ret));
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if __riscv_xlen == 32
|
||||||
|
|
||||||
#if __riscv_xlen==32
|
static uint32_t mtime_hi(void) {
|
||||||
|
|
||||||
static uint32_t mtime_hi(void)
|
|
||||||
{
|
|
||||||
unsigned long ret;
|
unsigned long ret;
|
||||||
__asm volatile("rdtimeh %0":"=r"(ret));
|
__asm volatile("rdtimeh %0" : "=r"(ret));
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t get_timer_value(void)
|
uint64_t get_timer_value(void) {
|
||||||
{
|
while(1) {
|
||||||
while (1) {
|
|
||||||
uint32_t hi = mtime_hi();
|
uint32_t hi = mtime_hi();
|
||||||
uint32_t lo = mtime_lo();
|
uint32_t lo = mtime_lo();
|
||||||
if (hi == mtime_hi())
|
if(hi == mtime_hi())
|
||||||
return ((uint64_t)hi << 32) | lo;
|
return ((uint64_t)hi << 32) | lo;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#elif __riscv_xlen==64
|
#elif __riscv_xlen == 64
|
||||||
|
|
||||||
uint64_t get_timer_value()
|
uint64_t get_timer_value() { return mtime_lo(); }
|
||||||
{
|
|
||||||
return mtime_lo();
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
unsigned long get_timer_freq()
|
unsigned long get_timer_freq() { return 32768; }
|
||||||
{
|
|
||||||
return 32768;
|
unsigned long get_cpu_freq() { return 100000000; }
|
||||||
|
|
||||||
|
void init_pll(void) {
|
||||||
|
// TODO: implement initialization
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned long get_cpu_freq()
|
static void uart_init(size_t baud_rate) {
|
||||||
{
|
// TODO: implement initialization
|
||||||
return 100000000;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void init_pll(void){
|
void __attribute__((weak)) handle_m_ext_interrupt() {}
|
||||||
//TODO: implement initialization
|
|
||||||
}
|
|
||||||
|
|
||||||
static void uart_init(size_t baud_rate)
|
void __attribute__((weak)) handle_m_time_interrupt() {
|
||||||
{
|
|
||||||
//TODO: implement initialization
|
|
||||||
}
|
|
||||||
|
|
||||||
void __attribute__((weak)) handle_m_ext_interrupt(){
|
|
||||||
}
|
|
||||||
|
|
||||||
void __attribute__((weak)) handle_m_time_interrupt(){
|
|
||||||
uint64_t time = get_aclint_mtime(aclint);
|
uint64_t time = get_aclint_mtime(aclint);
|
||||||
time+=MTIMER_NEXT_TICK_INC;
|
time += MTIMER_NEXT_TICK_INC;
|
||||||
set_aclint_mtimecmp(aclint, time);
|
set_aclint_mtimecmp(aclint, time);
|
||||||
}
|
}
|
||||||
|
|
||||||
void __attribute__((weak)) default_handler(void) {
|
void __attribute__((weak)) default_handler(void) { puts("default handler\n"); }
|
||||||
puts("default handler\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
void __attribute__((weak)) interrupt_handler(unsigned) {
|
void __attribute__((weak)) interrupt_handler(unsigned) { puts("interrupt handler\n"); }
|
||||||
puts("interrupt handler\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t handle_trap(uint32_t mcause, uint32_t mepc, uint32_t sp){
|
uint32_t handle_trap(uint32_t mcause, uint32_t mepc, uint32_t sp) {
|
||||||
if ((mcause & MCAUSE_INT)) {
|
if((mcause & MCAUSE_INT)) {
|
||||||
if ((mcause & MCAUSE_CAUSE) == IRQ_M_EXT) {
|
if((mcause & MCAUSE_CAUSE) == IRQ_M_EXT) {
|
||||||
handle_m_ext_interrupt();
|
handle_m_ext_interrupt();
|
||||||
} else if (((mcause & MCAUSE_CAUSE) == IRQ_M_TIMER)){
|
} else if(((mcause & MCAUSE_CAUSE) == IRQ_M_TIMER)) {
|
||||||
handle_m_time_interrupt();
|
handle_m_time_interrupt();
|
||||||
} else {
|
} else {
|
||||||
interrupt_handler(mcause& ~MCAUSE_INT);
|
interrupt_handler(mcause & ~MCAUSE_INT);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
write(1, "trap\n", 5);
|
write(1, "trap\n", 5);
|
||||||
@@ -112,27 +89,22 @@ uint32_t handle_trap(uint32_t mcause, uint32_t mepc, uint32_t sp){
|
|||||||
return mepc;
|
return mepc;
|
||||||
}
|
}
|
||||||
|
|
||||||
void _init()
|
void __attribute__((weak)) _init() {
|
||||||
{
|
|
||||||
|
|
||||||
#ifndef NO_INIT
|
#ifndef NO_INIT
|
||||||
init_pll();
|
init_pll();
|
||||||
uart_init(115200);
|
uart_init(115200);
|
||||||
printf("core freq at %lu Hz\n", get_cpu_freq());
|
printf("core freq at %lu Hz\n", get_cpu_freq());
|
||||||
write_csr(mtvec, &trap_entry);
|
write_csr(mtvec, &trap_entry);
|
||||||
if (read_csr(misa) & (1 << ('F' - 'A'))) { // if F extension is present
|
if(read_csr(misa) & (1 << ('F' - 'A'))) { // if F extension is present
|
||||||
write_csr(mstatus, MSTATUS_FS); // allow FPU instructions without trapping
|
write_csr(mstatus, MSTATUS_FS); // allow FPU instructions without trapping
|
||||||
write_csr(fcsr, 0); // initialize rounding mode, undefined at reset
|
write_csr(fcsr, 0); // initialize rounding mode, undefined at reset
|
||||||
}
|
}
|
||||||
int i=0;
|
int i = 0;
|
||||||
while(i<NUM_INTERRUPTS) {
|
while(i < NUM_INTERRUPTS) {
|
||||||
localISR[i++] = default_handler;
|
localISR[i++] = default_handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void _fini(void)
|
void _fini(void) {}
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|||||||
3
env/riscv_vp/platform.h
vendored
3
env/riscv_vp/platform.h
vendored
@@ -42,6 +42,9 @@
|
|||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#define NUM_INTERRUPTS 16
|
||||||
|
#define MTIMER_NEXT_TICK_INC 1000
|
||||||
|
|
||||||
void init_pll(void);
|
void init_pll(void);
|
||||||
unsigned long get_cpu_freq(void);
|
unsigned long get_cpu_freq(void);
|
||||||
unsigned long get_timer_freq(void);
|
unsigned long get_timer_freq(void);
|
||||||
|
|||||||
2
env/start.S
vendored
2
env/start.S
vendored
@@ -60,7 +60,7 @@ _start:
|
|||||||
fssr x0
|
fssr x0
|
||||||
1:
|
1:
|
||||||
#endif
|
#endif
|
||||||
|
call _init
|
||||||
/* argc = argv = 0 */
|
/* argc = argv = 0 */
|
||||||
li a0, 0
|
li a0, 0
|
||||||
li a1, 0
|
li a1, 0
|
||||||
|
|||||||
Reference in New Issue
Block a user