riscv-formal-nmigen/rvfi/insns/insn.py

106 lines
3.7 KiB
Python

from nmigen import *
"""
General RISC-V Instruction
"""
class Insn(Elaboratable):
def __init__(self, params):
# Core-specific parameters
self.params = params
# RVFI input ports
self.rvfi_valid = Signal(1)
self.rvfi_insn = Signal(self.params.ilen)
self.rvfi_pc_rdata = Signal(self.params.xlen)
self.rvfi_rs1_rdata = Signal(self.params.xlen)
self.rvfi_rs2_rdata = Signal(self.params.xlen)
self.rvfi_mem_rdata = Signal(self.params.xlen)
if self.params.csr_misa:
self.rvfi_csr_misa_rdata = Signal(self.params.xlen)
# RVFI output ports
if self.params.csr_misa:
self.spec_csr_misa_rmask = Signal(self.params.xlen)
self.spec_valid = Signal(1)
self.spec_trap = Signal(1)
self.spec_rs1_addr = Signal(5)
self.spec_rs2_addr = Signal(5)
self.spec_rd_addr = Signal(5)
self.spec_rd_wdata = Signal(self.params.xlen)
self.spec_pc_wdata = Signal(self.params.xlen)
self.spec_mem_addr = Signal(self.params.xlen)
self.spec_mem_rmask = Signal(int(self.params.xlen // 8))
self.spec_mem_wmask = Signal(int(self.params.xlen // 8))
self.spec_mem_wdata = Signal(self.params.xlen)
# Additional wires and registers
self.insn_padding = Signal(self.params.ilen)
self.insn_imm = Signal(self.params.xlen)
self.insn_funct7 = Signal(7)
self.insn_funct6 = Signal(6)
self.insn_shamt = Signal(6)
self.insn_rs2 = Signal(5)
self.insn_rs1 = Signal(5)
self.insn_funct3 = Signal(3)
self.insn_rd = Signal(5)
self.insn_opcode = Signal(7)
self.misa_ok = Signal(1)
self.ialign16 = Signal(1)
def ports(self):
input_ports = [
self.rvfi_valid,
self.rvfi_insn,
self.rvfi_pc_rdata,
self.rvfi_rs1_rdata,
self.rvfi_rs2_rdata,
self.rvfi_mem_rdata
]
if self.params.csr_misa:
input_ports.append(self.rvfi_csr_misa_rdata)
output_ports = [
self.spec_valid,
self.spec_trap,
self.spec_rs1_addr,
self.spec_rs2_addr,
self.spec_rd_addr,
self.spec_rd_wdata,
self.spec_pc_wdata,
self.spec_mem_addr,
self.spec_mem_rmask,
self.spec_mem_wmask,
self.spec_mem_wdata
]
if self.params.csr_misa:
output_ports.append(self.spec_csr_misa_rmask)
return input_ports + output_ports
def elaborate(self, platform):
m = Module()
m.d.comb += self.insn_padding.eq(self.rvfi_insn >> 32)
m.d.comb += self.insn_funct7.eq(self.rvfi_insn[25:32])
m.d.comb += self.insn_funct6.eq(self.rvfi_insn[26:32])
m.d.comb += self.insn_shamt.eq(self.rvfi_insn[20:26])
m.d.comb += self.insn_rs2.eq(self.rvfi_insn[20:25])
m.d.comb += self.insn_rs1.eq(self.rvfi_insn[15:20])
m.d.comb += self.insn_funct3.eq(self.rvfi_insn[12:15])
m.d.comb += self.insn_rd.eq(self.rvfi_insn[7:12])
m.d.comb += self.insn_opcode.eq(self.rvfi_insn[:7])
# default assignments
m.d.comb += self.spec_valid.eq(0)
m.d.comb += self.spec_trap.eq(~self.misa_ok)
m.d.comb += self.spec_rs1_addr.eq(0)
m.d.comb += self.spec_rs2_addr.eq(0)
m.d.comb += self.spec_rd_addr.eq(0)
m.d.comb += self.spec_rd_wdata.eq(0)
m.d.comb += self.spec_pc_wdata.eq(0)
m.d.comb += self.spec_mem_addr.eq(0)
m.d.comb += self.spec_mem_rmask.eq(0)
m.d.comb += self.spec_mem_wmask.eq(0)
m.d.comb += self.spec_mem_wdata.eq(0)
return m