back.rtlil: use slicing to match shape when reducing width.
authorwhitequark <cz@m-labs.hk>
Sun, 16 Dec 2018 16:20:45 +0000 (16:20 +0000)
committerwhitequark <cz@m-labs.hk>
Sun, 16 Dec 2018 16:20:45 +0000 (16:20 +0000)
nmigen/back/rtlil.py

index 05d4ac646e13bccbe0be8532d680f9875ca03953..7f584ffdc3a21beb6a47ed44830fd7b188fd4f8a 100644 (file)
@@ -339,19 +339,19 @@ class _RHSValueCompiler(_ValueCompiler):
             return self(ast.Const(value.value, (new_bits, new_sign)))
 
         value_bits, value_sign = value.shape()
-        if new_bits > value_bits:
-            res = self.s.rtlil.wire(width=new_bits)
-            self.s.rtlil.cell("$pos", ports={
-                "\\A": self(value),
-                "\\Y": res,
-            }, params={
-                "A_SIGNED": value_sign,
-                "A_WIDTH": value_bits,
-                "Y_WIDTH": new_bits,
-            }, src=src(value.src_loc))
-            return res
-        else:
-            return "{} [{}:0]".format(self(value), new_bits - 1)
+        if new_bits <= value_bits:
+            return self(ast.Slice(value, 0, new_bits))
+
+        res = self.s.rtlil.wire(width=new_bits)
+        self.s.rtlil.cell("$pos", ports={
+            "\\A": self(value),
+            "\\Y": res,
+        }, params={
+            "A_SIGNED": value_sign,
+            "A_WIDTH": value_bits,
+            "Y_WIDTH": new_bits,
+        }, src=src(value.src_loc))
+        return res
 
     def on_Operator_binary(self, value):
         lhs, rhs = value.operands