Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement misa CSR #743

Merged
merged 3 commits into from
Nov 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why user_mode is handled separately? From ISA point of view it is "U" extension.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is treated as extension only in misa. It is not present in isa strings, not defined as extension in spec, doesn't define instructions and only changes CSR behaviour (and can't be determined from FU config, so needs config flag anyway)
I think it is better to not add it to isa.extensions

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