Skip to content

Commit

Permalink
mtvec made WARL
Browse files Browse the repository at this point in the history
  • Loading branch information
kalinf committed Nov 20, 2024
1 parent 515de1f commit bd239dd
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 16 deletions.
6 changes: 6 additions & 0 deletions coreblocks/arch/isa_consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ class PrivilegeLevel(IntEnum, shape=2):
MACHINE = 0b11


@unique
class TrapVectorMode(IntEnum, shape=2):
DIRECT = 0b00
VECTORED = 0b01


@unique
class InterruptCauseNumber(IntEnum):
SSI = 1 # supervisor software interrupt
Expand Down
4 changes: 2 additions & 2 deletions coreblocks/backend/retirement.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,12 @@ def flush_instr(rob_entry):

handler_pc = Signal(self.gen_params.isa.xlen)
mtvec_offset = Signal(self.gen_params.isa.xlen)
mtvec = m_csr.mtvec.read(m).data
mtvec = m_csr.mtvec._fu_read(m).data
mcause = m_csr.mcause.read(m).data

# mtvec without mode is [mxlen-1:2], mode is two last bits.
# When mode=1 (Vectored), interrupts set pc to base + 4 * cause_number
with m.If(mcause & (mtvec << (self.gen_params.isa.xlen - 1))):
with m.If(mcause[-1] & (mtvec[0:1] == 1)):
m.d.av_comb += mtvec_offset.eq(mcause << 2)

m.d.av_comb += handler_pc.eq((mtvec & ~(0b11)) + mtvec_offset)
Expand Down
18 changes: 16 additions & 2 deletions coreblocks/priv/csr/csr_instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from coreblocks.arch import CSRAddress
from coreblocks.arch.csr_address import MstatusFieldOffsets
from coreblocks.arch.isa import Extension
from coreblocks.arch.isa_consts import PrivilegeLevel, XlenEncoding
from coreblocks.arch.isa_consts import PrivilegeLevel, XlenEncoding, TrapVectorMode
from coreblocks.params.genparams import GenParams
from coreblocks.priv.csr.csr_register import CSRRegister
from coreblocks.priv.csr.aliased import AliasedCSR
Expand Down Expand Up @@ -75,7 +75,7 @@ def __init__(self, gen_params: GenParams):

self.mcause = CSRRegister(CSRAddress.MCAUSE, gen_params)

self.mtvec = CSRRegister(CSRAddress.MTVEC, gen_params)
self.mtvec = AliasedCSR(CSRAddress.MTVEC, gen_params)

mepc_ro_bits = 0b1 if Extension.C in gen_params.isa.extensions else 0b11 # pc alignment (SPEC)
self.mepc = CSRRegister(CSRAddress.MEPC, gen_params, ro_bits=mepc_ro_bits)
Expand All @@ -93,6 +93,7 @@ def __init__(self, gen_params: GenParams):
self.priv_mode_public.add_field(0, self.priv_mode)

self.mstatus_fields_implementation(gen_params, self.mstatus, self.mstatush)
self.mtvec_fields_implementation(gen_params, self.mtvec)

def elaborate(self, platform):
m = Module()
Expand All @@ -103,6 +104,19 @@ def elaborate(self, platform):

return m

def mtvec_fields_implementation(self, gen_params: GenParams, mtvec: AliasedCSR):
def filter_legal_mode(m: TModule, v: Value):
legal = Signal(1)
m.d.av_comb += legal.eq((v == TrapVectorMode.DIRECT) | (v == TrapVectorMode.VECTORED))
return (legal, v)

self.mtvec_base = CSRRegister(None, gen_params, width=gen_params.isa.xlen - 2)
mtvec.add_field(TrapVectorMode.as_shape().width, self.mtvec_base)
self.mtvec_mode = CSRRegister(
None, gen_params, width=TrapVectorMode.as_shape().width, fu_write_filtermap=filter_legal_mode
)
mtvec.add_field(0, self.mtvec_mode)

def mstatus_fields_implementation(self, gen_params: GenParams, mstatus: AliasedCSR, mstatush: AliasedCSR):
def filter_legal_priv_mode(m: TModule, v: Value):
legal = Signal(1)
Expand Down
3 changes: 1 addition & 2 deletions test/asm/interrupt_vectored.asm
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ fail:
csrwi 0x7ff, 2
j fail


.org 0x200
nop
nop
Expand All @@ -133,7 +132,7 @@ fail:
nop
nop
nop
nop
j fail
j int0_handler
j int1_handler
li x31, 0xae # should never happen
Expand Down
10 changes: 0 additions & 10 deletions test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,16 +186,6 @@ def test_asm_source(self):
("interrupt.asm", 600, {4: 89, 8: 843}, {2: 89, 7: 843, 31: 0xDE}, 30, 50, False),
# interrupts are only inserted on branches, we always have some forward progression. 15 for trigger variantion.
("interrupt.asm", 80, {4: 21, 8: 9349}, {2: 21, 7: 9349, 31: 0xDE}, 0, 15, False),
("interrupt_vectored.asm", 900, {4: 89, 8: 843, 15: 322}, {2: 89, 7: 843, 14: 322, 31: 0xDE}, 30, 50, False),
(
"interrupt_vectored.asm",
1600,
{4: 24157817, 8: 199, 15: 521},
{2: 24157817, 7: 199, 14: 521, 31: 0xDE},
100,
200,
False,
),
(
"interrupt_vectored.asm",
300,
Expand Down

0 comments on commit bd239dd

Please sign in to comment.