diff --git a/sim/common/bitmanip.h b/sim/common/bitmanip.h index 90f8c5d4..71430122 100644 --- a/sim/common/bitmanip.h +++ b/sim/common/bitmanip.h @@ -70,31 +70,12 @@ inline uint64_t bit_getw(uint64_t bits, uint32_t start, uint32_t end) { return (bits << shift) >> (shift + start); } -// Apply integer sign extension -inline uint32_t sext(uint32_t word, uint32_t width) { +template +T sext(const T& word, uint32_t width) { assert(width > 1); - assert(width <= 32); - if (width == 32) + assert(width <= (sizeof(T) * 8)); + if (width == (sizeof(T) * 8)) return word; - uint32_t mask = (uint32_t(1) << width) - 1; + T mask((static_cast(1) << width) - 1); return ((word >> (width - 1)) & 0x1) ? (word | ~mask) : word; -} - -inline uint64_t sext(uint64_t word, uint32_t width) { - assert(width > 1); - assert(width <= 64); - if (width == 64) - return word; - uint64_t mask = (uint64_t(1) << width) - 1; - return ((word >> (width - 1)) & 0x1) ? (word | ~mask) : word; -} - -inline __uint128_t sext(__uint128_t word, uint32_t width) { - assert(width > 1); - assert(width <= 128); - if (width == 128) - return word; - __uint128_t mask = (__uint128_t(1) << width) - 1; - return ((word >> (width - 1)) & 0x1) ? (word | ~mask) : word; -} - +} \ No newline at end of file diff --git a/sim/simx/decode.cpp b/sim/simx/decode.cpp index f79fea6b..86b9251c 100644 --- a/sim/simx/decode.cpp +++ b/sim/simx/decode.cpp @@ -502,6 +502,13 @@ std::shared_ptr Decoder::decode(uint32_t code) const { instr->setFunc7(func7); switch (op) { case Opcode::SYS_INST: + if (func3 != 0) { + // RV32I: CSR* + instr->setDestReg(rd, RegType::Integer); + } + // uint12 + instr->setImm(code >> shift_rs2); + break; case Opcode::FENCE: // uint12 instr->setImm(code >> shift_rs2);