migen.fhdl.structure: add Signal.like(other)
authorRobert Jordens <jordens@gmail.com>
Fri, 25 Jul 2014 05:45:41 +0000 (23:45 -0600)
committerSebastien Bourdeauducq <sb@m-labs.hk>
Fri, 25 Jul 2014 05:52:59 +0000 (23:52 -0600)
This is a convenience method. Signal(flen(other)) is used frequently but that
drops the signedness. Signal((other.nbits, other.signed)) would be correct but
is long.

migen/fhdl/structure.py

index 7ab425456da257c4c9ee8e75f051487efbc752c8..aa33888e2ece7fb24694b549351619d03876635e 100644 (file)
@@ -274,6 +274,20 @@ class Signal(Value):
        def __repr__(self):
                return "<Signal " + (self.backtrace[-1][0] or "anonymous") + " at " + hex(id(self)) + ">"
 
+       @classmethod
+       def like(cls, other):
+               """Create Signal based on another.
+
+               Parameters
+               ----------
+               other : Value
+                       Object to base this Signal on.
+
+               See `migen.fhdl.bitcontainer.value_bits_sign`() for details.
+               """
+               from migen.fhdl.bitcontainer import value_bits_sign
+               return cls(value_bits_sign(other))
+
 class ClockSignal(Value):
        """Clock signal for a given clock domain