Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: m-labs/nmigen
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 7cc0b8cbf00c
Choose a base ref
...
head repository: m-labs/nmigen
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 0ab215e5ed76
Choose a head ref
  • 1 commit
  • 2 files changed
  • 1 contributor

Commits on Jul 2, 2019

  1. Copy the full SHA
    0ab215e View commit details
Showing with 24 additions and 3 deletions.
  1. +14 −3 nmigen/hdl/ast.py
  2. +10 −0 nmigen/test/test_hdl_ast.py
17 changes: 14 additions & 3 deletions nmigen/hdl/ast.py
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
import traceback
from collections import OrderedDict
from collections.abc import Iterable, MutableMapping, MutableSet, MutableSequence
from enum import Enum

from .. import tracer
from ..tools import *
@@ -575,9 +576,11 @@ class Signal(Value, DUID):
defaults to 0) and ``max`` (exclusive, defaults to 2).
attrs : dict
Dictionary of synthesis attributes.
decoder : function
decoder : function or Enum
A function converting integer signal values to human-readable strings (e.g. FSM state
names).
names). If an ``Enum`` subclass is passed, it is concisely decoded using format string
``"{0.name:}/{0.value:}"``, or a number if the signal value is not a member of
the enumeration.
Attributes
----------
@@ -627,7 +630,15 @@ def __init__(self, shape=None, name=None, reset=0, reset_less=False, min=None, m
self.reset_less = bool(reset_less)

self.attrs = OrderedDict(() if attrs is None else attrs)
self.decoder = decoder
if isinstance(decoder, type) and issubclass(decoder, Enum):
def enum_decoder(value):
try:
return "{0.name:}/{0.value:}".format(decoder(value))
except ValueError:
return str(value)
self.decoder = enum_decoder
else:
self.decoder = decoder

@classmethod
def like(cls, other, name=None, name_suffix=None, src_loc_at=0, **kwargs):
10 changes: 10 additions & 0 deletions nmigen/test/test_hdl_ast.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from enum import Enum

from ..hdl.ast import *
from .tools import *

@@ -486,6 +488,14 @@ def test_like(self):
s8 = Signal.like(s1, name_suffix="_ff")
self.assertEqual(s8.name, "s1_ff")

def test_decoder(self):
class Color(Enum):
RED = 1
BLUE = 2
s = Signal(decoder=Color)
self.assertEqual(s.decoder(1), "RED/1")
self.assertEqual(s.decoder(3), "3")


class ClockSignalTestCase(FHDLTestCase):
def test_domain(self):