Skip to content

Commit

Permalink
back.rtlil: handle signed and large Instance parameters correctly.
Browse files Browse the repository at this point in the history
Fixes #388.
  • Loading branch information
whitequark committed May 19, 2020
1 parent 393c27a commit fbf9e1f
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion nmigen/back/rtlil.py
Expand Up @@ -23,11 +23,28 @@ class ImplementationLimit(Exception):
})


def signed(value):
if isinstance(value, str):
return False
elif isinstance(value, int):
return value < 0
elif isinstance(value, ast.Const):
return value.signed
else:
assert False, "Invalid constant {!r}".format(value)


def const(value):
if isinstance(value, str):
return "\"{}\"".format(value.translate(_escape_map))
elif isinstance(value, int):
return "{:d}".format(value)
if value in range(0, 2**31-1):
return "{:d}".format(value)
else:
# This code path is only used for Instances, where Verilog-like behavior is desirable.
# Verilog ensures that integers with unspecified width are 32 bits wide or more.
width = max(32, bits_for(value))
return const(ast.Const(value, width))
elif isinstance(value, ast.Const):
value_twos_compl = value.value & ((1 << value.width) - 1)
return "{}'{:0{}b}".format(value.width, value_twos_compl, value.width)
Expand Down Expand Up @@ -149,6 +166,9 @@ def cell(self, kind, name=None, params={}, ports={}, attrs={}, src=""):
if isinstance(value, float):
self._append(" parameter real \\{} \"{!r}\"\n",
param, value)
elif signed(value):
self._append(" parameter signed \\{} {}\n",
param, const(value))
else:
self._append(" parameter \\{} {}\n",
param, const(value))
Expand Down

0 comments on commit fbf9e1f

Please sign in to comment.