this is my first question to be asked here on stackoverflow, so please be kind with me;)
I'm new to RISC-V and low level C coding and I'm wondering how to manipulate the RISC-V CSRs using GCC C code.
A read of a specific CSR (eg MISA) looks easy: csrr rd, 0x301
which is short for csrrs rd, 0x301, x0
can be done eg with
int result;
asm("csrr %0, 0x301" : "=r"(result) : );
How can I convert the code above into some kind of function / callable unit with the following interface: int read_csr(int csr_number)
?
Since the CSR number must be an immediate value in machine code, is it possible without generating the code on the fly (self modifying code)?
Thanks for your replies and discussion.
Joachim
You can use the I
constraint for an immediate constant argument:
inline int read_csr(int csr_num) __attribute__((always_inline)) {
int result;
asm("csrr %0, %1" : "=r"(result) : "I"(csr_num));
return result; }
this will fail if you ever call it with an argument that is not a constant expression, as the I
constraint requires a constant.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.