diff --git a/.settings/language.settings.xml b/.settings/language.settings.xml index bc8a6e9..f6c8464 100644 --- a/.settings/language.settings.xml +++ b/.settings/language.settings.xml @@ -4,7 +4,7 @@ - + @@ -14,7 +14,7 @@ - + diff --git a/bsp/N22-ILM/newlib/crt0.S b/bsp/N22-ILM/newlib/crt0.S index 97b61f2..a29969a 100644 --- a/bsp/N22-ILM/newlib/crt0.S +++ b/bsp/N22-ILM/newlib/crt0.S @@ -7,10 +7,16 @@ _start: .cfi_startproc .cfi_undefined ra -.option push -.option norelax + + /* setup trap vector */ + la a0, _mtvec + csrw mtvec, a0 + csrs mtvec, 1 + + .option push + .option norelax la gp, __global_pointer$ -.option pop + .option pop la sp, _sp /* Load data section */ @@ -36,7 +42,6 @@ _start: bltu a0, a1, 1b 2: - /* Call global constructors */ la a0, __libc_fini_array call atexit @@ -65,3 +70,5 @@ _init: ret .type _fini, @function _fini: ret .size _fini, .-_fini + + .include "mtvec.s" diff --git a/bsp/N22-ILM/newlib/mtvec.s b/bsp/N22-ILM/newlib/mtvec.s new file mode 100644 index 0000000..6dc3bd9 --- /dev/null +++ b/bsp/N22-ILM/newlib/mtvec.s @@ -0,0 +1,47 @@ +/* Copyright(C) 2020 Hex Five Security, Inc. - All Rights Reserved */ + + .align 2 + +_mtvec: + +irq0: .word trp_isr +irq1: .word 1f +irq2: .word 1f +irq3: .word msi_isr +irq4: .word 1f +irq5: .word 1f +irq6: .word 1f +irq7: .word tmr_isr +irq8: .word 1f +irq9: .word 1f +irq10: .word 1f +irq11: .word 1f +irq12: .word 1f +irq13: .word 1f +irq14: .word 1f +irq15: .word 1f +irq16: .word 1f +irq17: .word 1f +irq18: .word 1f +irq19: .word 1f +irq20: .word 1f +irq21: .word 1f +irq22: .word 1f +irq23: .word 1f +irq24: .word 1f +irq25: .word gpio_isr +irq26: .word 1f +irq27: .word uart_isr +irq28: .word dma_isr +irq29: .word 1f +irq30: .word 1f +irq31: .word 1f + + .weak trp_isr, msi_isr, tmr_isr, dma_isr, uart_isr, gpio_isr + +trp_isr: +msi_isr: +tmr_isr: +dma_isr: +gpio_isr: +1: j . diff --git a/bsp/N22-ILM/newlib/newlib.mk b/bsp/N22-ILM/newlib/newlib.mk index 1188ad2..22b7867 100644 --- a/bsp/N22-ILM/newlib/newlib.mk +++ b/bsp/N22-ILM/newlib/newlib.mk @@ -6,6 +6,7 @@ all: $(TARGET) ASM_SRCS += $(NEWLIB_DIR)/crt0.S C_SRCS += $(NEWLIB_DIR)/newlib.c +INCLUDES += -I$(NEWLIB_DIR) INCLUDES += -I$(PLATFORM_DIR) LDFLAGS += -T $(PLATFORM_DIR)/memory.lds diff --git a/multizone.jar b/multizone.jar index 24c1cca..6bdc4a4 100644 Binary files a/multizone.jar and b/multizone.jar differ diff --git a/zone1/main.c b/zone1/main.c index 871d688..3bbac2d 100644 --- a/zone1/main.c +++ b/zone1/main.c @@ -1,38 +1,32 @@ /* Copyright(C) 2020 Hex Five Security, Inc. - All Rights Reserved */ -#include // open() -#include // read() write() -#include // strxxx() -#include // printf() sprintf() -#include // qsort() strtoul() -#include // UINT_MAX ULONG_MAX +#include // open() +#include // read() write() +#include // strxxx() +#include // printf() sprintf() +#include // qsort() strtoul() +#include // UINT_MAX ULONG_MAX #include "platform.h" #include "multizone.h" #define MIN(a,b) (((a)<(b))?(a):(b)) -typedef enum {zone1=1, zone2, zone3, zone4} Zone; - #define BUFFER_SIZE 32 static volatile struct{ - char data[BUFFER_SIZE]; - int r; // read - int w; // write + char data[BUFFER_SIZE]; + int r; // read + int w; // write } buffer; int buffer_empty(void){ - return (buffer.w==0); + return (buffer.w==0); } +typedef enum {zone1=1, zone2, zone3, zone4} Zone; + static char inputline[BUFFER_SIZE+1]=""; static volatile char inbox[4][16] = { {'\0'}, {'\0'}, {'\0'}, {'\0'} }; -int inbox_empty(void){ - return (inbox[0][0]=='\0' && inbox[1][0]=='\0' && inbox[2][0]=='\0' && inbox[3][0]=='\0'); -} - -// ------------------------------------------------------------------------ -static void (*trap_vect[45+1])(void) = {}; // 15.6 Interr Assignment (CLIC) __attribute__((interrupt())) void trp_isr(void) { // nmi traps (0) @@ -88,6 +82,7 @@ __attribute__((interrupt())) void trp_isr(void) { // nmi traps (0) asm ("j _start"); } + __attribute__((interrupt())) void msi_isr(void) { // msip/inbox (3) for (Zone zone = zone1; zone <= zone4; zone++) { @@ -97,19 +92,21 @@ __attribute__((interrupt())) void msi_isr(void) { // msip/inbox (3) } } + __attribute__((interrupt())) void tmr_isr(void) { // timer (7) - const unsigned mcause = MZONE_CSRR(CSR_MCAUSE); - const unsigned mepc = MZONE_CSRR(CSR_MEPC); - const unsigned mtval = MZONE_CSRR(CSR_MTVAL); + const unsigned long mcause = MZONE_CSRR(CSR_MCAUSE); + const unsigned long mepc = MZONE_CSRR(CSR_MEPC); + const unsigned long mtval = MZONE_CSRR(CSR_MTVAL); - write(1, "\e7\e[2K", 6); // save curs pos & clear entire line + write(1, "\e7\e[2K", 6); // save curs pos & clear entire line printf("\rTimer interrupt : 0x%08x 0x%08x 0x%08x \n", (unsigned)mcause, (unsigned)mepc, (unsigned)mtval); write(1, "\nZ1 > %s", 6); write(1, inputline, strlen(inputline)); - write(1, "\e8\e[2B", 6); // restore curs pos & curs down +2 lines - CSRC(mie, 1<<7); // disable one-shot timer + write(1, "\e8\e[2B", 6); // restore curs pos & curs down +2 lines + CSRC(mie, 1<<7); // disable one-shot timer } + __attribute__((interrupt())) void uart_isr(void) { // uart #ifdef PLIC_BASE @@ -149,9 +146,120 @@ __attribute__((interrupt())) void dma_isr(void) { // dma #endif } -// ------------------------------------------------------------------------ + +int inbox_empty(void){ + return (inbox[0][0]=='\0' && inbox[1][0]=='\0' && inbox[2][0]=='\0' && inbox[3][0]=='\0'); +} + +int readline() { + + static size_t p=0; + static int esc=0; + static char history[8][sizeof(inputline)]={"","","","","","","",""}; static int h=-1; + + int eol = 0; // end of line + + while ( !eol && buffer.w > buffer.r ) { + + CSRC(mstatus, 1<<3); // CLIC_REG(CLIC_INT_PENDING+(CLIC_SRC_UART<= buffer.w) {buffer.r = 0; buffer.w = 0;} + CSRS(mstatus, 1<<3); //CLIC_REG(CLIC_INT_PENDING+(CLIC_SRC_UART<0){ + p--; + write(1, "\e[D", 3); + } + + } else if (esc==2 && c=='A'){ // up arrow (history) + esc=0; + if (h<8-1 && strlen(history[h+1])>0){ + h++; + strcpy(inputline, history[h]); + write(1, "\e[2K", 4); // 2K clear entire line - cur pos dosn't change + write(1, "\rZ1 > ", 6); + write(1, inputline, strlen(inputline)); + p=strlen(inputline); + + } + + } else if (esc==2 && c=='B'){ // down arrow (history) + esc=0; + if (h>0 && strlen(history[h-1])>0){ + h--; + strcpy(inputline, history[h]); + write(1, "\e[2K", 4); // 2K clear entire line - cur pos dosn't change + write(1, "\rZ1 > ", 6); + write(1, inputline, strlen(inputline)); + p=strlen(inputline); + } + + } else if ((c=='\b' || c=='\x7f') && p>0 && esc==0){ // backspace + p--; + for (size_t i=p; i=' ' && c<='~' && p < sizeof(inputline)-1 && esc==0){ + for (size_t i = sizeof(inputline)-1-1; i > p; i--) inputline[i]=inputline[i-1]; // make room for 1 ch + inputline[p]=c; + write(1, "\e7", 2); // save curs pos + write(1, "\e[K", 3); // clear line from curs pos + write(1, &inputline[p], strlen(inputline)-p); p++; + write(1, "\e8", 2); // restore curs pos + write(1, "\e[C", 3); // move curs right 1 pos + + } else if (c=='\r') { + p=0; esc=0; eol = 1; + // trim + while (inputline[strlen(inputline)-1]==' ') inputline[strlen(inputline)-1]='\0'; + while (inputline[0]==' ') for (size_t i = 0; i < strlen(inputline); i++) inputline[i]=inputline[i+1]; + // save line to history + if (strlen(inputline)>0 && strcmp(inputline, history[0])!=0){ + for (int i = 8-1; i > 0; i--) strcpy(history[i], history[i-1]); + strcpy(history[0], inputline); + } h = -1; + write(1, "\n", 1); + + } else + esc=0; + + } + + return eol; + +} + void print_sys_info(void) { -// ------------------------------------------------------------------------ // misa unsigned long misa; asm volatile ("csrr %0, misa" : "=r"(misa) : ); @@ -221,7 +329,6 @@ __attribute__((interrupt())) void dma_isr(void) { // dma } -// ------------------------------------------------------------------------ int cmpfunc(const void* a, const void* b){ const int ai = *(const int* )a; @@ -229,7 +336,6 @@ __attribute__((interrupt())) void dma_isr(void) { // dma return ai < bi ? -1 : ai > bi ? 1 : 0; } -// ------------------------------------------------------------------------ void print_stats(void){ #define MHZ (CPU_FREQ/1000000) @@ -295,9 +401,7 @@ void print_stats(void){ } -// ------------------------------------------------------------------------ void print_pmp(void){ -// ------------------------------------------------------------------------ #define TOR 0b00001000 #define NA4 0b00010000 @@ -367,7 +471,6 @@ void print_pmp(void){ } -// ------------------------------------------------------------------------ void msg_handler() { CSRC(mie, 1<<3); @@ -396,7 +499,6 @@ void msg_handler() { } -// ------------------------------------------------------------------------ void cmd_handler(){ char * tk[9]; tk[0] = strtok(inputline, " "); for (int i=1; i<9; i++) tk[i] = strtok(NULL, " "); @@ -539,122 +641,22 @@ void cmd_handler(){ } -// ------------------------------------------------------------------------ -int readline() { -// ------------------------------------------------------------------------ - - static size_t p=0; - static int esc=0; - static char history[8][sizeof(inputline)]={"","","","","","","",""}; static int h=-1; - - int eol = 0; // end of line - - while ( !eol && buffer.w > buffer.r ) { - - CSRC(mstatus, 1<<3); // CLIC_REG(CLIC_INT_PENDING+(CLIC_SRC_UART<= buffer.w) {buffer.r = 0; buffer.w = 0;} - CSRS(mstatus, 1<<3); //CLIC_REG(CLIC_INT_PENDING+(CLIC_SRC_UART<0){ - p--; - write(1, "\e[D", 3); - } - - } else if (esc==2 && c=='A'){ // up arrow (history) - esc=0; - if (h<8-1 && strlen(history[h+1])>0){ - h++; - strcpy(inputline, history[h]); - write(1, "\e[2K", 4); // 2K clear entire line - cur pos dosn't change - write(1, "\rZ1 > ", 6); - write(1, inputline, strlen(inputline)); - p=strlen(inputline); - - } - - } else if (esc==2 && c=='B'){ // down arrow (history) - esc=0; - if (h>0 && strlen(history[h-1])>0){ - h--; - strcpy(inputline, history[h]); - write(1, "\e[2K", 4); // 2K clear entire line - cur pos dosn't change - write(1, "\rZ1 > ", 6); - write(1, inputline, strlen(inputline)); - p=strlen(inputline); - } - - } else if ((c=='\b' || c=='\x7f') && p>0 && esc==0){ // backspace - p--; - for (size_t i=p; i=' ' && c<='~' && p < sizeof(inputline)-1 && esc==0){ - for (size_t i = sizeof(inputline)-1-1; i > p; i--) inputline[i]=inputline[i-1]; // make room for 1 ch - inputline[p]=c; - write(1, "\e7", 2); // save curs pos - write(1, "\e[K", 3); // clear line from curs pos - write(1, &inputline[p], strlen(inputline)-p); p++; - write(1, "\e8", 2); // restore curs pos - write(1, "\e[C", 3); // move curs right 1 pos - - } else if (c=='\r') { - p=0; esc=0; eol = 1; - // trim - while (inputline[strlen(inputline)-1]==' ') inputline[strlen(inputline)-1]='\0'; - while (inputline[0]==' ') for (size_t i = 0; i < strlen(inputline); i++) inputline[i]=inputline[i+1]; - // save line to history - if (strlen(inputline)>0 && strcmp(inputline, history[0])!=0){ - for (int i = 8-1; i > 0; i--) strcpy(history[i], history[i-1]); - strcpy(history[0], inputline); - } h = -1; - write(1, "\n", 1); - - } else - esc=0; - - } - - return eol; - -} - -// ------------------------------------------------------------------------ int main (void) { - //while(1) MZONE_WFI(); - //while(1) MZONE_YIELD(); - //while(1); + // enable interrupt sources + CSRS(mie, 1<<3); +#ifdef PLIC_BASE + CSRS(mie, 1< ", 7); while(1){ diff --git a/zone2/main.c b/zone2/main.c index 899cf23..630b6e9 100644 --- a/zone2/main.c +++ b/zone2/main.c @@ -9,8 +9,7 @@ static volatile int led = LED1; static volatile char msg[16] = {'\0'}; -static void (*trap_vect[45+1])(void) = {}; // 15.6 Interr Assignment (CLIC) -__attribute__((interrupt())) void trp_handler(void) { // trap handler (0) +__attribute__((interrupt())) void trp_isr(void) { // trap handler (0) const unsigned long mcause = MZONE_CSRR(CSR_MCAUSE); @@ -28,7 +27,8 @@ __attribute__((interrupt())) void trp_handler(void) { // trap handler (0) for(;;); } -__attribute__((interrupt())) void msi_handler(void) { // machine software interrupt (3) + +__attribute__((interrupt())) void msi_isr(void) { // machine software interrupt (3) char const tmp[16]; @@ -36,7 +36,8 @@ __attribute__((interrupt())) void msi_handler(void) { // machine software inter memcpy((char *)msg, tmp, sizeof msg); } -__attribute__((interrupt())) void tmr_handler(void) { // machine timer interrupt (7) + +__attribute__((interrupt())) void tmr_isr(void) { // machine timer interrupt (7) // togle led BITINV(GPIO_BASE+GPIO_OUTPUT_VAL, led); @@ -45,7 +46,7 @@ __attribute__((interrupt())) void tmr_handler(void) { // machine timer interrup MZONE_ADTIMECMP((uint64_t)(RTC_FREQ/1000*1000)); } -__attribute__((interrupt())) void gpio_handler(void) { // local interrupt (25) +__attribute__((interrupt())) void gpio_isr(void) { // local interrupt (25) // GPIO: read int const uint32_t gpio_int = GPIO_REG(GPIO_INT_STATUS); @@ -65,18 +66,6 @@ __attribute__((interrupt())) void gpio_handler(void) { // local interrupt (25) int main (void){ - //while(1) MZONE_WFI(); - //while(1) MZONE_YIELD(); - //while(1); - - // vectored trap handler - trap_vect[0] = trp_handler; - trap_vect[3] = msi_handler; - trap_vect[7] = tmr_handler; - trap_vect[GPIO_IRQ] = gpio_handler; - CSRW(mtvec, trap_vect); - CSRS(mtvec, 0x1); - // GPIO: enable interrupts GPIO_REG(GPIO_INT_STATUS) = 0xFFFFFFFF; GPIO_REG(GPIO_INT_EN) |= ( BTN1 | BTN2 | BTN3 | BTN4 ); diff --git a/zone3/main.c b/zone3/main.c index 951dfb4..9cacd7b 100644 --- a/zone3/main.c +++ b/zone3/main.c @@ -135,7 +135,7 @@ uint64_t task2(){ // Keep alive 1sec return time + KEEP_ALIVE_TIME; } -__attribute__(( interrupt())) void trap_handler(void){ +__attribute__(( interrupt())) void trap_isr(void){ #define IRQ (1UL << (__riscv_xlen-1)) @@ -222,13 +222,9 @@ void msg_handler(const char *msg){ int main (void){ - //while(1) MZONE_WFI(); - //while(1) MZONE_YIELD(); - //while(1); - GPIO_REG(GPIO_OUTPUT_EN) |= ((0x1 << SPI_TCK) | (0x1<< SPI_TDO)); - CSRW(mtvec, trap_handler); // register trap handler + CSRW(mtvec, trap_isr); // register trap handler CSRS(mie, 1<<3); // enable msip/inbox interrupt CSRS(mie, 1<<7); // enable timer interrupts CSRS(mstatus, 1<<3); // enable global interrupts diff --git a/zone4/main.c b/zone4/main.c index bd41e4e..57644a0 100644 --- a/zone4/main.c +++ b/zone4/main.c @@ -3,7 +3,7 @@ #include #include "multizone.h" -__attribute__(( interrupt())) void trap_handler(void){ +__attribute__(( interrupt())) void trap_isr(void){ for( ;; ); @@ -11,13 +11,9 @@ __attribute__(( interrupt())) void trap_handler(void){ int main (void){ - //while(1) MZONE_WFI(); - //while(1) MZONE_YIELD(); - //while(1); - - CSRW(mtvec, trap_handler); // register trap handler - CSRS(mie, 1<<3); // wake up on msip/inbox - CSRC(mstatus, 1<<3); // disable global interrupts + CSRW(mtvec, trap_isr); // register trap handler + CSRS(mie, 1<<3); // wake up on msip/inbox + CSRC(mstatus, 1<<3); // disable global interrupts while (1) {