Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
2478fbff6This file is pinned to the selected release. Download the original to open it in your design tool.
// Copyright 2022 ETH Zurich and University of Bologna.
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0
//
// Nicole Narr <narrn@student.ethz.ch>
// Christopher Reinwardt <creinwar@student.ethz.ch>
// Paul Scheffler <paulsc@iis.ee.ethz.ch>
#pragma once
#include <stdint.h>
static inline volatile uint8_t *reg8(void *base, int offs) {
return (volatile uint8_t *)(base + offs);
}
static inline volatile uint32_t *reg32(void *base, int offs) {
return (volatile uint32_t *)(base + offs);
}
static inline void fence() {
asm volatile("fence" ::: "memory");
}
static inline void fencei() {
asm volatile("fence.i" ::: "memory");
}
static inline void wfi() {
asm volatile("wfi" ::: "memory");
}
// Enables or disables M-mode timer interrupts.
static inline void set_mtie(int enable) {
if (enable)
asm volatile("csrs mie, %0" ::"r"(128) : "memory");
else
asm volatile("csrc mie, %0" ::"r"(128) : "memory");
}
// Enables or disables M-mode global interrupts.
static inline void set_mie(int enable) {
if (enable)
asm volatile("csrsi mstatus, 8" ::: "memory");
else
asm volatile("csrci mstatus, 8" ::: "memory");
}
// Get cycle count since reset
static inline uint64_t get_mcycle() {
uint64_t mcycle;
asm volatile("csrr %0, mcycle" : "=r"(mcycle)::"memory");
return mcycle;
}
// This may also be used to invoke code that does not return.
static inline uint64_t invoke(void *code) {
uint64_t (*code_fun_ptr)(void) = code;
fencei();
return code_fun_ptr();
}
// Set global pointer and return prior value. Use with caution.
static inline void *gprw(void *gp) {
void *ret;
asm volatile("mv %0, gp" : "=r"(ret)::"memory");
if (gp) asm volatile("mv gp, %0" ::"r"(gp) : "memory", "gp");
return ret;
}
// If a call yields a nonzero return, return that immediately as an int.
#define CHECK_CALL(call) \
{ \
int __ccret = (volatile int)(call); \
if (__ccret) return __ccret; \
}
// If a condition; if it is untrue, ummediately return an error code.
#define CHECK_ASSERT(ret, cond) \
if (!(cond)) return (ret);
#define MIN(a, b) (((a) <= (b)) ? (a) : (b))