lib.enum: add .format()
implementation.
This commit is contained in:
parent
3c870d6b73
commit
1fdd9bf4e9
|
@ -2,8 +2,8 @@ import enum as py_enum
|
||||||
import warnings
|
import warnings
|
||||||
import operator
|
import operator
|
||||||
|
|
||||||
from ..hdl import Value, ValueCastable, Shape, ShapeCastable, Const, SyntaxWarning
|
from ..hdl import Value, ValueCastable, Shape, ShapeCastable, Const, SyntaxWarning, Format
|
||||||
from ..hdl._repr import *
|
from ..hdl._repr import Repr, FormatEnum
|
||||||
|
|
||||||
|
|
||||||
__all__ = py_enum.__all__ + ["EnumView", "FlagView"]
|
__all__ = py_enum.__all__ + ["EnumView", "FlagView"]
|
||||||
|
@ -176,6 +176,11 @@ class EnumType(ShapeCastable, py_enum.EnumMeta):
|
||||||
def from_bits(cls, bits):
|
def from_bits(cls, bits):
|
||||||
return cls(bits)
|
return cls(bits)
|
||||||
|
|
||||||
|
def format(cls, value, format_spec):
|
||||||
|
if format_spec != "":
|
||||||
|
raise ValueError(f"Format specifier {format_spec!r} is not supported for enums")
|
||||||
|
return Format.Enum(value, cls)
|
||||||
|
|
||||||
def _value_repr(cls, value):
|
def _value_repr(cls, value):
|
||||||
yield Repr(FormatEnum(cls), value)
|
yield Repr(FormatEnum(cls), value)
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,7 @@ from amaranth.hdl._ir import *
|
||||||
from amaranth.sim import *
|
from amaranth.sim import *
|
||||||
from amaranth.lib.memory import Memory
|
from amaranth.lib.memory import Memory
|
||||||
from amaranth.lib.data import View, StructLayout
|
from amaranth.lib.data import View, StructLayout
|
||||||
|
from amaranth.lib import enum
|
||||||
|
|
||||||
from .utils import *
|
from .utils import *
|
||||||
from amaranth._utils import _ignore_deprecated
|
from amaranth._utils import _ignore_deprecated
|
||||||
|
@ -1165,7 +1166,7 @@ class SimulatorIntegrationTestCase(FHDLTestCase):
|
||||||
Counter: 009
|
Counter: 009
|
||||||
"""))
|
"""))
|
||||||
|
|
||||||
def test_print(self):
|
def test_print_str(self):
|
||||||
def enc(s):
|
def enc(s):
|
||||||
return Cat(
|
return Cat(
|
||||||
Const(b, 8)
|
Const(b, 8)
|
||||||
|
@ -1196,6 +1197,38 @@ class SimulatorIntegrationTestCase(FHDLTestCase):
|
||||||
Counter: non-zero
|
Counter: non-zero
|
||||||
"""))
|
"""))
|
||||||
|
|
||||||
|
def test_print_enum(self):
|
||||||
|
class MyEnum(enum.Enum, shape=unsigned(2)):
|
||||||
|
A = 0
|
||||||
|
B = 1
|
||||||
|
CDE = 2
|
||||||
|
|
||||||
|
sig = Signal(MyEnum)
|
||||||
|
ctr = Signal(2)
|
||||||
|
m = Module()
|
||||||
|
m.d.comb += sig.eq(ctr)
|
||||||
|
m.d.sync += [
|
||||||
|
Print(sig),
|
||||||
|
ctr.eq(ctr + 1),
|
||||||
|
]
|
||||||
|
output = StringIO()
|
||||||
|
with redirect_stdout(output):
|
||||||
|
with self.assertSimulation(m) as sim:
|
||||||
|
sim.add_clock(1e-6, domain="sync")
|
||||||
|
def process():
|
||||||
|
yield Tick()
|
||||||
|
yield Tick()
|
||||||
|
yield Tick()
|
||||||
|
yield Tick()
|
||||||
|
sim.add_testbench(process)
|
||||||
|
self.assertEqual(output.getvalue(), dedent("""\
|
||||||
|
A
|
||||||
|
B
|
||||||
|
CDE
|
||||||
|
[unknown]
|
||||||
|
"""))
|
||||||
|
|
||||||
|
|
||||||
def test_assert(self):
|
def test_assert(self):
|
||||||
m = Module()
|
m = Module()
|
||||||
ctr = Signal(16)
|
ctr = Signal(16)
|
||||||
|
|
Loading…
Reference in a new issue