Skip to content

Selecting part of a StructLayout using an Enum signal #1579

Answered by goekce
goekce asked this question in Q&A

You must be logged in to vote

Using a SwitchValue improves readability:
edit: the solution uses an internal API function, which may change. Read also the following comments.

from amaranth import *
from amaranth.hdl._ast import SwitchValue
from amaranth.lib import data
from amaranth.lib.enum import Enum, EnumView
from amaranth.lib.wiring import Component, In, Out


class Kind(Enum, shape=1):
    STICK = 0
    HIT = 1


LineLayout = data.StructLayout({item.name: unsigned(2) for item in Kind})


def enum_select(
    data_view: data.View,
    enum_view: EnumView,
):
    return SwitchValue(
        enum_view,
        [
            (enum_item, getattr(data_view, enum_item.name))
            for enum_item in enum_view.shape(…

Replies: 1 comment 11 replies

You must be logged in to vote
11 replies
@whitequark

@goekce

@goekce

@goekce

Answer selected by goekce
@whitequark

@goekce

@whitequark

@goekce

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants