This commit is contained in:
Blaise Tine 2022-02-05 17:58:12 -05:00
parent d297351211
commit 1bd25acb0b
2 changed files with 13 additions and 25 deletions

View file

@ -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 <typename T = uint32_t>
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<T>(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;
}
}

View file

@ -502,6 +502,13 @@ std::shared_ptr<Instr> 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);