aboutsummaryrefslogtreecommitdiff
path: root/include/risc-v.h
blob: 946ca7c643e4243d00d1be45f8deef496b2fd433 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#pragma once

#include <stdint.h>

/* Macros and functions for generic RISC-V cores. */

/* Wait for interrupt macro. */
static inline void wfi()
{
  asm volatile("wfi");
}

/* The mode for the mtvec. */
typedef enum {
  MODE_DIRECT = 0,
  MODE_VECTORED = 1,
} mtvec_mode_t;

/* Macro to read the value from a RISC-V CSR. */
#define csrr(csr)                                   \
  ({                                                \
    uint32_t _tmp_csr;                              \
    asm volatile("csrr %0, " csr : "=r"(_tmp_csr)); \
    _tmp_csr;                                       \
  })

/* Macro to write a value to a RISC-V CSR. */
#define csrw(csr, v)                             \
  {                                              \
    asm volatile("csrw " csr ", %0" : : "r"(v)); \
  }

/* Sets the mtvec to point to the given vector_table with the given mode. */
static inline void set_mtvec(void* vector_table, mtvec_mode_t mode)
{
  uint32_t mtvec = (uint32_t)vector_table;
  mtvec |= !!mode;
  csrw("mtvec", mtvec);
}

#define MCAUSE csrr("mcause")
#define MEPC csrr("mepc")
#define MTVAL csrr("mtval")

#define __nop() asm volatile ("nop")