Skip to content

Commit

Permalink
Implement misa CSR (#743)
Browse files Browse the repository at this point in the history
  • Loading branch information
piotro888 authored Nov 12, 2024
1 parent 4472a57 commit def471b
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions coreblocks/priv/csr/csr_instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ def __init__(self, gen_params: GenParams):

self.mtval = CSRRegister(CSRAddress.MTVAL, gen_params)

self.misa = CSRRegister(
CSRAddress.MISA, gen_params, init=self._misa_value(gen_params), ro_bits=(1 << gen_params.isa.xlen) - 1
)

self.priv_mode = CSRRegister(
None,
gen_params,
Expand All @@ -94,7 +98,7 @@ def __init__(self, gen_params: GenParams):
self.priv_mode_public = AliasedCSR(CSRAddress.COREBLOCKS_TEST_PRIV_MODE, gen_params)
self.priv_mode_public.add_field(0, self.priv_mode)

self.mstatus_fields_implementation(gen_params, self.mstatus, self.mstatush)
self._mstatus_fields_implementation(gen_params, self.mstatus, self.mstatush)

def elaborate(self, platform):
m = Module()
Expand All @@ -105,7 +109,7 @@ def elaborate(self, platform):

return m

def mstatus_fields_implementation(self, gen_params: GenParams, mstatus: AliasedCSR, mstatush: AliasedCSR):
def _mstatus_fields_implementation(self, gen_params: GenParams, mstatus: AliasedCSR, mstatush: AliasedCSR):
def filter_legal_priv_mode(m: TModule, v: Value):
legal = Signal(1)
with m.Switch(v):
Expand Down Expand Up @@ -179,6 +183,35 @@ def filter_legal_priv_mode(m: TModule, v: Value):
Extension.V in gen_params.isa.extensions or Extension.F in gen_params.isa.extensions,
)

def _misa_value(self, gen_params):
misa_value = 0

misa_extension_bits = {
0: Extension.A,
1: Extension.B,
2: Extension.C,
3: Extension.D,
4: Extension.E,
5: Extension.F,
8: Extension.I,
12: Extension.M,
16: Extension.Q,
21: Extension.V,
}

for bit, extension in misa_extension_bits.items():
if extension in gen_params.isa.extensions:
misa_value |= 1 << bit

if gen_params.user_mode:
misa_value |= 1 << 20
# 7 - Hypervisor, 18 - Supervisor, 23 - Custom Extensions

xml_field_mapping = {32: XlenEncoding.W32, 64: XlenEncoding.W64, 128: XlenEncoding.W128}
misa_value |= xml_field_mapping[gen_params.isa.xlen] << (gen_params.isa.xlen - 2)

return misa_value


class GenericCSRRegisters(Elaboratable):
def __init__(self, gen_params: GenParams):
Expand Down

0 comments on commit def471b

Please sign in to comment.