Added functions to read registers and print information on failure. Also added a getTime function for a pretty boot display.

This commit is contained in:
Jacob Pease 2024-08-02 15:14:30 -05:00
parent 906fa73747
commit 3fde6c13f7
6 changed files with 84 additions and 0 deletions

19
fpga/zsbl/fail.c Normal file
View File

@ -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) {
}
}

4
fpga/zsbl/fail.h Normal file
View File

@ -0,0 +1,4 @@
#pragma once
#include <stdint.h>
void fail();

29
fpga/zsbl/riscv.S Normal file
View File

@ -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

7
fpga/zsbl/riscv.h Normal file
View File

@ -0,0 +1,7 @@
#pragma once
#include <stdint.h>
uint64_t read_mcycle();
uint64_t get_ra();
void set_status_fs();
void clear_status_fs();

20
fpga/zsbl/time.c Normal file
View File

@ -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("] ");
}

5
fpga/zsbl/time.h Normal file
View File

@ -0,0 +1,5 @@
#pragma once
#include <stdint.h>
float getTime();
void print_time();