Skip to content

Commit 62a2dd1

Browse files
authored
Merge pull request #1756 from riscv-software-src/clean-up-hpm
Avoid magic constants in hpmcounter implementation
2 parents 8e05766 + eb3ccab commit 62a2dd1

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

riscv/csrs.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -738,9 +738,9 @@ bool misa_csr_t::unlogged_write(const reg_t val) noexcept {
738738
state->mie->write_with_mask(MIP_HS_MASK, 0); // also takes care of hie, sie
739739
state->mip->write_with_mask(MIP_HS_MASK, 0); // also takes care of hip, sip, hvip
740740
state->hstatus->write(0);
741-
for (reg_t i = 3; i < N_HPMCOUNTERS + 3; ++i) {
742-
const reg_t new_mevent = state->mevent[i - 3]->read() & ~(MHPMEVENT_VUINH | MHPMEVENT_VSINH);
743-
state->mevent[i - 3]->write(new_mevent);
741+
for (reg_t i = 0; i < N_HPMCOUNTERS; ++i) {
742+
const reg_t new_mevent = state->mevent[i]->read() & ~(MHPMEVENT_VUINH | MHPMEVENT_VSINH);
743+
state->mevent[i]->write(new_mevent);
744744
}
745745
}
746746

@@ -1668,9 +1668,9 @@ void scountovf_csr_t::verify_permissions(insn_t insn, bool write) const {
16681668

16691669
reg_t scountovf_csr_t::read() const noexcept {
16701670
reg_t val = 0;
1671-
for (reg_t i = 3; i < N_HPMCOUNTERS + 3; ++i) {
1672-
bool of = state->mevent[i - 3]->read() & MHPMEVENT_OF;
1673-
val |= of << i;
1671+
for (reg_t i = 0; i < N_HPMCOUNTERS; ++i) {
1672+
bool of = state->mevent[i]->read() & MHPMEVENT_OF;
1673+
val |= of << (i + FIRST_HPMCOUNTER);
16741674
}
16751675

16761676
/* In M and S modes, scountovf bit X is readable when mcounteren bit X is set, */

riscv/processor.cc

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -193,14 +193,14 @@ void state_t::reset(processor_t* const proc, reg_t max_isa)
193193
csrmap[CSR_MINSTRET] = minstret;
194194
csrmap[CSR_MCYCLE] = mcycle;
195195
}
196-
for (reg_t i = 3; i < N_HPMCOUNTERS + 3; ++i) {
197-
const reg_t which_mevent = CSR_MHPMEVENT3 + i - 3;
198-
const reg_t which_meventh = CSR_MHPMEVENT3H + i - 3;
199-
const reg_t which_mcounter = CSR_MHPMCOUNTER3 + i - 3;
200-
const reg_t which_mcounterh = CSR_MHPMCOUNTER3H + i - 3;
201-
const reg_t which_counter = CSR_HPMCOUNTER3 + i - 3;
202-
const reg_t which_counterh = CSR_HPMCOUNTER3H + i - 3;
203-
mevent[i - 3] = std::make_shared<mevent_csr_t>(proc, which_mevent);
196+
for (reg_t i = 0; i < N_HPMCOUNTERS; ++i) {
197+
const reg_t which_mevent = CSR_MHPMEVENT3 + i;
198+
const reg_t which_meventh = CSR_MHPMEVENT3H + i;
199+
const reg_t which_mcounter = CSR_MHPMCOUNTER3 + i;
200+
const reg_t which_mcounterh = CSR_MHPMCOUNTER3H + i;
201+
const reg_t which_counter = CSR_HPMCOUNTER3 + i;
202+
const reg_t which_counterh = CSR_HPMCOUNTER3H + i;
203+
mevent[i] = std::make_shared<mevent_csr_t>(proc, which_mevent);
204204
auto mcounter = std::make_shared<const_csr_t>(proc, which_mcounter, 0);
205205
csrmap[which_mcounter] = mcounter;
206206

@@ -209,19 +209,19 @@ void state_t::reset(processor_t* const proc, reg_t max_isa)
209209
csrmap[which_counter] = counter;
210210
}
211211
if (xlen == 32) {
212-
csrmap[which_mevent] = std::make_shared<rv32_low_csr_t>(proc, which_mevent, mevent[i - 3]);;
212+
csrmap[which_mevent] = std::make_shared<rv32_low_csr_t>(proc, which_mevent, mevent[i]);;
213213
auto mcounterh = std::make_shared<const_csr_t>(proc, which_mcounterh, 0);
214214
csrmap[which_mcounterh] = mcounterh;
215215
if (proc->extension_enabled_const(EXT_ZIHPM)) {
216216
auto counterh = std::make_shared<counter_proxy_csr_t>(proc, which_counterh, mcounterh);
217217
csrmap[which_counterh] = counterh;
218218
}
219219
if (proc->extension_enabled_const(EXT_SSCOFPMF)) {
220-
auto meventh = std::make_shared<rv32_high_csr_t>(proc, which_meventh, mevent[i - 3]);
220+
auto meventh = std::make_shared<rv32_high_csr_t>(proc, which_meventh, mevent[i]);
221221
csrmap[which_meventh] = meventh;
222222
}
223223
} else {
224-
csrmap[which_mevent] = mevent[i - 3];
224+
csrmap[which_mevent] = mevent[i];
225225
}
226226
}
227227
csrmap[CSR_MCOUNTINHIBIT] = std::make_shared<const_csr_t>(proc, CSR_MCOUNTINHIBIT, 0);

riscv/processor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "../fesvr/memif.h"
1919
#include "vector_unit.h"
2020

21+
#define FIRST_HPMCOUNTER 3
2122
#define N_HPMCOUNTERS 29
2223

2324
class processor_t;

0 commit comments

Comments
 (0)