You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.8 KiB
C

3 years ago
#ifndef H_CSR
#define H_CSR
#define CSR_MSTATUS 0x300 // machine status
#define CSR_MISA 0x301 // machine ISA
#define CSR_MIE 0x304 // machine interrupt enable
#define CSR_MTVEC 0x305 // machine trap handler
#define CSR_MSCRATCH 0x340 // machine scratch register
#define CSR_MEPC 0x341 // machine exception program counter
#define CSR_MCAUSE 0x342 // machine trap cause
#define CSR_MTVAL 0x343 // machine bad address or instruction
#define CSR_MIP 0x344 // machine interrupt pending
#define CSR_TIME 0xC01 // time csr
#define CSR_TIMEH 0xC81 // high bits of time
#define CSR_MTIMECMP 0x780 // mtimecmp register for timer interrupts
#define CSR_MTIMECMPH 0x781 // mtimecmph register for timer interrupts
#define CSR_PMPCFG 0x3A0 // start of physical memory protection config
#define CSR_PMPADDR 0x3B0 // start of pmp addresses
#ifndef CSR_HALT
3 years ago
#define CSR_HALT 0x789 // writing nonzero value here will halt the cpu
#endif
3 years ago
// do not define C macros and other C stuff when this is included inside assembly
#ifndef __assembly
3 years ago
#define CSR_READ(csr_id, result) { \
__asm__ ("csrr %0, %1" : "=r"((result)) : "I"((csr_id))); \
3 years ago
}
#define CSR_WRITE(csr_id, val) { \
__asm__ ("csrw %0, %1" :: "I"((csr_id)), "r"((val))); \
3 years ago
}
#define HALT(code) { \
__asm__ ("csrw %0, %1" :: "I"(CSR_HALT), "I"(code)); \
3 years ago
}
void write_mtimecmp(uint64 mtimecmp);
inline __attribute__((always_inline)) uint64 read_time()
{
unsigned int lower, higher;
__asm__ volatile (
"csrr %0, %2\n"
"csrr %1, %3\n"
: "=r"(lower), "=r"(higher)
: "i"(CSR_TIME), "i"(CSR_TIMEH)
);
return (uint64) higher << 32 | lower;
}
3 years ago
#endif
#endif