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: 27cedf4302bb
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: 7342662bee80
Choose a head ref
  • 1 commit
  • 2 files changed
  • 1 contributor

Commits on Sep 10, 2019

  1. hdl.ast: warn if reset value is truncated.

    Fixes #183.
    whitequark committed Sep 10, 2019
    Copy the full SHA
    7342662 View commit details
Showing with 19 additions and 0 deletions.
  1. +8 −0 nmigen/hdl/ast.py
  2. +11 −0 nmigen/test/test_hdl_ast.py
8 changes: 8 additions & 0 deletions nmigen/hdl/ast.py
Original file line number Diff line number Diff line change
@@ -665,6 +665,14 @@ def __init__(self, shape=None, name=None, *, reset=0, reset_less=False, min=None

if not isinstance(self.nbits, int) or self.nbits < 0:
raise TypeError("Width must be a non-negative integer, not '{!r}'".format(self.nbits))

reset_nbits = bits_for(reset, self.signed)
if reset != 0 and reset_nbits > self.nbits:
warnings.warn("Reset value {!r} requires {} bits to represent, but the signal "
"only has {} bits"
.format(reset, reset_nbits, self.nbits),
SyntaxWarning, stacklevel=2 + src_loc_at)

self.reset = int(reset)
self.reset_less = bool(reset_less)

11 changes: 11 additions & 0 deletions nmigen/test/test_hdl_ast.py
Original file line number Diff line number Diff line change
@@ -510,6 +510,17 @@ def test_reset(self):
self.assertEqual(s1.reset, 0b111)
self.assertEqual(s1.reset_less, True)

def test_reset_narrow(self):
with self.assertWarns(SyntaxWarning,
msg="Reset value 8 requires 4 bits to represent, but the signal only has 3 bits"):
Signal(3, reset=8)
with self.assertWarns(SyntaxWarning,
msg="Reset value 4 requires 4 bits to represent, but the signal only has 3 bits"):
Signal((3, True), reset=4)
with self.assertWarns(SyntaxWarning,
msg="Reset value -5 requires 4 bits to represent, but the signal only has 3 bits"):
Signal((3, True), reset=-5)

def test_attrs(self):
s1 = Signal()
self.assertEqual(s1.attrs, {})