diff --git a/coreblocks/priv/csr/csr_instances.py b/coreblocks/priv/csr/csr_instances.py index 3215b42ee..adc598b33 100644 --- a/coreblocks/priv/csr/csr_instances.py +++ b/coreblocks/priv/csr/csr_instances.py @@ -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, @@ -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() @@ -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): @@ -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):