back.rtlil: actually match shape of left hand side.
authorwhitequark <whitequark@whitequark.org>
Sat, 3 Aug 2019 23:43:57 +0000 (23:43 +0000)
committerwhitequark <whitequark@whitequark.org>
Sat, 3 Aug 2019 23:48:28 +0000 (23:48 +0000)
This comes up in code such as:
  Array([Signal(1), Signal(8)]).eq(Const(0, 8))

nmigen/back/rtlil.py

index 895606582eaea1b9e8fc734248847fa426a50363..6ef0d8691561b4cda7a6a6daabff675fe35e804e 100644 (file)
@@ -572,8 +572,15 @@ class _LHSValueCompiler(_ValueCompiler):
         raise TypeError # :nocov:
 
     def match_shape(self, value, new_bits, new_sign):
-        assert value.shape() == (new_bits, new_sign)
-        return self(value)
+        value_bits, value_sign = value.shape()
+        if new_bits == value_bits:
+            return self(value)
+        elif new_bits < value_bits:
+            return self(ast.Slice(value, 0, new_bits))
+        else: # new_bits > value_bits
+            # It is legal to assign to constants on LHS in RTLIL; such assignments are ignored.
+            dummy_bits = new_bits - value_bits
+            return "{{ {}'{} {} }}".format(dummy_bits, "x" * dummy_bits, self(value))
 
     def on_Signal(self, value):
         if value not in self.s.driven: