Skip to content

Commit

Permalink
Specify RV_N_REGS via C enum
Browse files Browse the repository at this point in the history
In preparation for potential RV64 support, we might avoid specifying
essential constants manually. Instead, using C enum would thereby
automate the definitions.
  • Loading branch information
jserv committed Dec 17, 2022
1 parent 8a57eff commit f2da162
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/riscv.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ riscv_word_t rv_get_pc(riscv_t *rv)
void rv_set_reg(riscv_t *rv, uint32_t reg, riscv_word_t in)
{
assert(rv);
if (reg < RV_NUM_REGS && reg != rv_reg_zero)
if (reg < RV_N_REGS && reg != rv_reg_zero)
rv->X[reg] = in;
}

riscv_word_t rv_get_reg(riscv_t *rv, uint32_t reg)
{
assert(rv);
if (reg < RV_NUM_REGS)
if (reg < RV_N_REGS)
return rv->X[reg];

return ~0U;
Expand Down Expand Up @@ -117,7 +117,7 @@ void rv_delete(riscv_t *rv)
void rv_reset(riscv_t *rv, riscv_word_t pc)
{
assert(rv);
memset(rv->X, 0, sizeof(uint32_t) * RV_NUM_REGS);
memset(rv->X, 0, sizeof(uint32_t) * RV_N_REGS);

/* set the reset address */
rv->PC = pc;
Expand All @@ -132,7 +132,7 @@ void rv_reset(riscv_t *rv, riscv_word_t pc)

#if RV32_HAS(EXT_F)
/* reset float registers */
memset(rv->F, 0, sizeof(float) * RV_NUM_REGS);
memset(rv->F, 0, sizeof(float) * RV_N_REGS);
rv->csr_fcsr = 0;
#endif

Expand Down
1 change: 1 addition & 0 deletions src/riscv.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ enum {
rv_reg_t4,
rv_reg_t5,
rv_reg_t6,
RV_N_REGS, /* NOTE: shoule be the last */
};

/* forward declaration for internal structure */
Expand Down
8 changes: 3 additions & 5 deletions src/riscv_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
#include "decode.h"
#include "riscv.h"

#define RV_NUM_REGS 32

/* CSRs */
enum {
/* floating point */
Expand Down Expand Up @@ -79,7 +77,7 @@ struct riscv_internal {
riscv_io_t io;

/* integer registers */
riscv_word_t X[RV_NUM_REGS];
riscv_word_t X[RV_N_REGS];
riscv_word_t PC;

/* user provided data */
Expand All @@ -96,8 +94,8 @@ struct riscv_internal {
#if RV32_HAS(EXT_F)
/* float registers */
union {
riscv_float_t F[RV_NUM_REGS];
uint32_t F_int[RV_NUM_REGS]; /* integer shortcut */
riscv_float_t F[RV_N_REGS];
uint32_t F_int[RV_N_REGS]; /* integer shortcut */
};
uint32_t csr_fcsr;
#endif
Expand Down

0 comments on commit f2da162

Please sign in to comment.