Skip to content

Commit

Permalink
Merge pull request #3 from rswarbrick/shifts
Browse files Browse the repository at this point in the history
Fixes for SRL and SRA
  • Loading branch information
wallento authored Aug 21, 2020
2 parents bdb7df4 + 010c838 commit 489448d
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions riscvmodel/insn.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,23 @@ def execute(self, model: Model):
@isa("srl", RV32I, opcode=0b0110011, funct3=0b101, funct7=0b0000000)
class InstructionSRL(InstructionRType):
def execute(self, model: Model):
model.state.intreg[
self.rd] = model.state.intreg[self.rs1] >> model.state.intreg[self.rs2]
src = model.state.intreg[self.rs1]
shift = model.state.intreg[self.rs2] & 0x1f
model.state.intreg[self.rd] = src >> shift


@isa("sra", RV32I, opcode=0b0110011, funct3=0b101, funct7=0b0100000)
class InstructionSRA(InstructionRType):
def execute(self, model: Model):
model.state.intreg[
self.rd] = model.state.intreg[self.rs1] >> model.state.intreg[self.rs2]
usrc = model.state.intreg[self.rs1].unsigned()
shift = model.state.intreg[self.rs2].unsigned() & 0x1f
if usrc >> 31:
to_clear = 32 - shift
sign_mask = (((1 << 32) - 1) >> to_clear) << to_clear
else:
sign_mask = 0

model.state.intreg[self.rd] = sign_mask | (usrc >> shift)


@isa("or", RV32I, opcode=0b0110011, funct3=0b110, funct7=0b0000000)
Expand Down

0 comments on commit 489448d

Please sign in to comment.