Skip to content

Update bitstruct to use Python type annotations #455

Open
@whitequark

Description

@whitequark

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.

Metadata

Metadata

Assignees

Labels

gatewareComponent: gatewaresoftwareComponent: software

Type

No type

Projects

Status

Todo

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions