diff --git a/fpga/zsbl/fail.c b/fpga/zsbl/fail.c new file mode 100644 index 000000000..4e6cd8db0 --- /dev/null +++ b/fpga/zsbl/fail.c @@ -0,0 +1,19 @@ +#include "fail.h" +#include "uart.h" +#include "riscv.h" +#include "time.h" + +void fail() { + // Get address that led to failure + register uint64_t addr; + asm volatile ("mv %0, ra" : "=r"(addr) : : "memory"); + + // Print message + print_time(); + println_with_addr("Failed at: 0x", addr); + + // Loop forever + while(1) { + + } +} diff --git a/fpga/zsbl/fail.h b/fpga/zsbl/fail.h new file mode 100644 index 000000000..da1a2e405 --- /dev/null +++ b/fpga/zsbl/fail.h @@ -0,0 +1,4 @@ +#pragma once +#include + +void fail(); diff --git a/fpga/zsbl/riscv.S b/fpga/zsbl/riscv.S new file mode 100644 index 000000000..7f400b1b2 --- /dev/null +++ b/fpga/zsbl/riscv.S @@ -0,0 +1,29 @@ +.section .text +.globl read_mcycle +.type read_mcycle, @function +read_mcycle: + csrr a0, mcycle + ret + +.section .text +.globl get_ra +.type get_ra, @function +get_ra: + mv a0, ra + ret + +.section .text +.globl set_status_fs +.type set_status_fs, @function +set_status_fs: + lui t1, 0x6 + csrs mstatus, t1 + ret + +.section .text +.globl clear_status_fs +.type clear_status_fs, @function +clear_status_fs: + lui t1, 0x6 + csrc mstatus, t1 + ret diff --git a/fpga/zsbl/riscv.h b/fpga/zsbl/riscv.h new file mode 100644 index 000000000..a27cd48d6 --- /dev/null +++ b/fpga/zsbl/riscv.h @@ -0,0 +1,7 @@ +#pragma once +#include + +uint64_t read_mcycle(); +uint64_t get_ra(); +void set_status_fs(); +void clear_status_fs(); diff --git a/fpga/zsbl/time.c b/fpga/zsbl/time.c new file mode 100644 index 000000000..43ac085b7 --- /dev/null +++ b/fpga/zsbl/time.c @@ -0,0 +1,20 @@ +#include "time.h" +#include "boot.h" +#include "riscv.h" +#include "uart.h" + +float getTime() { + set_status_fs(); + float numCycles = (float)read_mcycle(); + float ret = numCycles/SYSTEMCLOCK; + // clear_status_fs(); + return ret; +} + +void print_time() { + print_uart("["); + set_status_fs(); + print_uart_float(getTime(),5); + clear_status_fs(); + print_uart("] "); +} diff --git a/fpga/zsbl/time.h b/fpga/zsbl/time.h new file mode 100644 index 000000000..6cd928b88 --- /dev/null +++ b/fpga/zsbl/time.h @@ -0,0 +1,5 @@ +#pragma once +#include + +float getTime(); +void print_time();