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

Make working with packed objects easier #3857

Open
ktbarrett opened this issue Apr 21, 2024 · 0 comments
Open

Make working with packed objects easier #3857

ktbarrett opened this issue Apr 21, 2024 · 0 comments
Labels
category:codebase:handle relating to handles or handle types (BinaryValue) type:feature new or enhanced functionality

Comments

@ktbarrett
Copy link
Member

ktbarrett commented Apr 21, 2024

Based on #3608 where I realized that we really shouldn't be indexing into packed object (this includes packed structs), and that there are multi-dimensional packed objects (such as arrays of structs) where accessing fields in the packed object is currently quite obtuse. One way to resolve that is to introduce a some types to make this easier. This should work with the existing LogicArray object.

One possible UI is by creating something similar to dataclass. There would be a class decorator that would create an object that maps to a packed struct in SystemVerilog by using type annotations to describe the structure.

@packed_struct
class Example:
    field1: LogicArray[7:"to":0]  # specify direction in bounds
    field2: LogicArray[0:4][31:0]  # infer bound directions and support multidimensional vectors
    field3: OtherPackedStruct  # support structures in structures
    field4: OtherPackedStruct[0:4]  # and arrays of structures

This does not necessary need to reuse LogicArray for the description. But it would need to be able to be constructed from LogicArray and converted back to it.

packed_struct_obj = Example(dut.handle.value)
dut.handle.value = LogicArray(Example())

In addition simply describing the fields, supporting methods would be ideal, but non-"packable" attributes and custom __init__s would not be supported. We can support overriding generated methods by making the result type the child type of the implementation base class and the given type, so that super() resolves to the implementation base class. I think this would end up requiring a metaclass approach to make mypy happy.

class AnotherExample(PackedStruct):
    field1: LogicArray[7:0]

    def __logic_array__(self) -> LogicArray:
        print("ASDGHJKADSKVNH") # Important debugging line DO NOT REMOVE
        return super().__logic_array__()

This would support field access, individual bit access, and everything else.

e = Example(...)
a.field1 = "01010101"  # set the whole LogicArray
obj = a.field2[0]
obj[:] = LogicArray.from_unsigned(123, length=len(obj))
print(a.field2[0].to_unsigned())  # 123

This will require a series of "view" objects that maintain a reference to the underlying value and the current partially-applied indexing value.

@ktbarrett ktbarrett added type:feature new or enhanced functionality category:codebase:handle relating to handles or handle types (BinaryValue) labels Apr 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category:codebase:handle relating to handles or handle types (BinaryValue) type:feature new or enhanced functionality
Projects
None yet
Development

No branches or pull requests

1 participant