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

Update bitstruct to use Python type annotations #455

Open
whitequark opened this issue Oct 21, 2023 · 0 comments
Open

Update bitstruct to use Python type annotations #455

whitequark opened this issue Oct 21, 2023 · 0 comments
Assignees
Labels
gateware Component: gateware software Component: software

Comments

@whitequark
Copy link
Member

whitequark commented Oct 21, 2023

Right now we have structure definitions that look like this:

class PaSelect(enum.IntEnum):
    PA_BOOST        = 0b1
    RFO             = 0b0

REG_PA_CONFIG = bitstruct("REG_PA_CONFIG", 8, [
    ("OutputPower", 4),
    ("MaxPower",    3),
    ("PaSelect",    1),
])

This is Not Ideal for a number of reasons: doesn't work at all with Pylance's type inferencing, ugly, isn't compatible with Amaranth's all-new lib.data module, doesn't support enums, and probably others.

As an incremental step towards fixing all these, we should add support for using Python type annotation syntax here, so that the annotation syntax becomes identical to Amaranth's:

class PaSelect(amaranth.lib.enum.IntEnum, shape=1):
    PA_BOOST        = 0b1
    RFO             = 0b0

class REG_PA_CONFIG(bitstruct, shape=8):
    OutputPower     : 4
    MaxPower        : 3
    PaSelect        : PaSelect

(The fields currently lacking a name i.e. listed as None in the field lists would become _1 and so on. No need to be too clever here.)

The actual implementation of bitstruct should remain the same for compatibility, such that no applet code changes are required. The class would therefore become a ShapeCastable (and its instances ValueCastable) for Amaranth purposes (desugaring down to a simple amaranth.lib.data.StructLayout, probably), promoting code reuse between software and gateware, and enabling future transmigration of applet code from software to gateware for acceleration purposes.

This will also serve as an excellent testbed for Amaranth's shape-castable and value-castable extensibility systems.

Do you think you could take a look, @mwkmwkmwk? This seems like something up your alley.

@whitequark whitequark added software Component: software gateware Component: gateware labels Oct 21, 2023
@whitequark whitequark assigned mwkmwkmwk and wanda-phi and unassigned mwkmwkmwk Oct 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gateware Component: gateware software Component: software
Projects
Status: Todo
Development

No branches or pull requests

3 participants