amaranth icon indicating copy to clipboard operation
amaranth copied to clipboard

Formatting of enums without `shape` in a `data.StructLayout` does not work

Open rroohhh opened this issue 1 year ago • 3 comments

Take the following example:

#!/usr/bin/env python3

from amaranth import *
from amaranth.lib.enum import Enum
from amaranth.lib import data
from amaranth.back.rtlil import convert

m = Module()

class test(Enum):
    a = 0
    b = 1

class B(data.Struct):
    a: test

a = Signal(test)
b = Signal(B)
input1 = Signal()
input2 = Signal()
m.d.comb += a.eq(input1)
m.d.comb += b.eq(input2)

print(convert(m, ports=(input1, input2,)))

This outputs (simplified)

module \top
  attribute \enum_base_type "test"
  attribute \enum_value_0 "a"
  attribute \enum_value_1 "b"
  wire width 1 \a

  wire width 1 \b

  attribute \enum_base_type "test"
  attribute \enum_value_0 "a"
  attribute \enum_value_1 "b"
  wire width 1 input 0  \input1

  wire width 1 input 1  \input2

  wire width 1 \b.a

connect \a \input1 [0]
connect \b \input2 [0]
connect \b.a \input2 [0]

end

I would have expected the \b.a wire to also have the enum_{base_type,value_*} attributes, but these are only generated when I give my enum an explicit shape=....

rroohhh avatar Oct 14 '24 13:10 rroohhh

This can of course be fixed by making the data.Struct, etc parts add special casing for "plain" Enums, however I am not sure if this is the most elegant way.

rroohhh avatar Oct 14 '24 13:10 rroohhh

Do you want a PR implementing my suggested solution?

rroohhh avatar Oct 14 '24 20:10 rroohhh

Let's ask @wanda-phi, who implemented this originally.

whitequark avatar Oct 14 '24 21:10 whitequark