From 656db317d221639db5d4c9e368c9c839119b291c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcelina=20Ko=C5=9Bcielnicka?= Date: Wed, 7 Jun 2023 12:18:13 +0200 Subject: [PATCH] hdl.ast: fix signed `Const` normalization. Fixes #805. --- amaranth/hdl/ast.py | 2 +- tests/test_hdl_ast.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/amaranth/hdl/ast.py b/amaranth/hdl/ast.py index 33feba7..427293e 100644 --- a/amaranth/hdl/ast.py +++ b/amaranth/hdl/ast.py @@ -647,7 +647,7 @@ class Const(Value): shape = Shape.cast(shape, src_loc_at=1 + src_loc_at) self.width = shape.width self.signed = shape.signed - if self.signed and self.value >> (self.width - 1): + if self.signed and self.value >> (self.width - 1) & 1: self.value |= -(1 << self.width) else: self.value &= (1 << self.width) - 1 diff --git a/tests/test_hdl_ast.py b/tests/test_hdl_ast.py index bfec166..b4e93e8 100644 --- a/tests/test_hdl_ast.py +++ b/tests/test_hdl_ast.py @@ -369,6 +369,8 @@ class ConstTestCase(FHDLTestCase): def test_normalization(self): self.assertEqual(Const(0b10110, signed(5)).value, -10) + self.assertEqual(Const(0b10000, signed(4)).value, 0) + self.assertEqual(Const(-16, 4).value, 0) def test_value(self): self.assertEqual(Const(10).value, 10)