kuznia-rdzeni / coreblocks

RISC-V out-of-order core for education and research purposes
https://kuznia-rdzeni.github.io/coreblocks/
BSD 3-Clause "New" or "Revised" License
36 stars 14 forks source link

IOInterface typing wrapper for lib.data signatures #726

Open piotro888 opened 1 month ago

piotro888 commented 1 month ago

Using amaranth.lib.data Signatures with typing comes with some boilerplate and repetition :(

I came up with a class that can be used both for typing and to get amaranth Signature for use in Component constructor.

It works by crating a class with IOSignal fields declared as self. in constructor, so it can be directly for typechecking. IOSignal type derives Value with additonal shape and direction information. IOInterface is never used globally, but is intended only for interface typing. Those signals will be filled with actual objects at super().__init__(... Component constructor (that gets Signature from generated IOInterface.signature). Nested IOInterfaces are supported.

It is a bit hacky, but what do you think?

tilk commented 3 weeks ago

The boilerplate reduction is nice, but this is a little bit hacky. Maybe, instead of a custom amaranth.Value subclass, typing.Annotated could be used to store the same information?