Commit 5e5ddca4 by Zachary Snow

fix broken two's complement logic

parent 59d37468
...@@ -17,7 +17,7 @@ module Language.SystemVerilog.AST.Number ...@@ -17,7 +17,7 @@ module Language.SystemVerilog.AST.Number
, bitToVK , bitToVK
) where ) where
import Data.Bits ((.&.), shiftL) import Data.Bits ((.&.), shiftL, xor)
import Data.Char (digitToInt, intToDigit, toLower) import Data.Char (digitToInt, intToDigit, toLower)
import Data.List (elemIndex) import Data.List (elemIndex)
import Text.Read (readMaybe) import Text.Read (readMaybe)
...@@ -222,9 +222,10 @@ numberToInteger (UnbasedUnsized Bit0) = Just 0 ...@@ -222,9 +222,10 @@ numberToInteger (UnbasedUnsized Bit0) = Just 0
numberToInteger UnbasedUnsized{} = Nothing numberToInteger UnbasedUnsized{} = Nothing
numberToInteger (Decimal sz sg num) numberToInteger (Decimal sz sg num)
| not sg || num .&. pow == 0 = Just num | not sg || num .&. pow == 0 = Just num
| num == 1 = Just $ -1 | otherwise = Just $ negate $ num `xor` mask + 1
| otherwise = Just $ negate $ num - pow where
where pow = 2 ^ (abs sz - 1) pow = 2 ^ (abs sz - 1)
mask = pow + pow - 1
numberToInteger (Based sz sg _ num 0) = numberToInteger (Based sz sg _ num 0) =
numberToInteger $ Decimal sz sg num numberToInteger $ Decimal sz sg num
numberToInteger Based{} = Nothing numberToInteger Based{} = Nothing
......
...@@ -43,6 +43,7 @@ module top; ...@@ -43,6 +43,7 @@ module top;
$display("args %b", $size(RamPair, 1'sb1)); $display("args %b", $size(RamPair, 1'sb1));
$display("args %b", $size(RamPair, 2'sb1)); $display("args %b", $size(RamPair, 2'sb1));
$display("args %b", $size(RamPair, 2'sb01)); $display("args %b", $size(RamPair, 2'sb01));
$display("args %b", $size(RamPair, 2'sb10));
$display("args %b", $size(RamPair, 2'sb11)); $display("args %b", $size(RamPair, 2'sb11));
$display("args %b", $size(RamPair, '1)); $display("args %b", $size(RamPair, '1));
$display("args %b", $size(RamPair, 'o1)); $display("args %b", $size(RamPair, 'o1));
......
...@@ -41,6 +41,7 @@ module top; ...@@ -41,6 +41,7 @@ module top;
$display("args %b", 2); $display("args %b", 2);
$display("args %b", 2); $display("args %b", 2);
$display("args %b", 1'bx); $display("args %b", 1'bx);
$display("args %b", 1'bx);
$display("args %b", 2); $display("args %b", 2);
$display("args %b", 2); $display("args %b", 2);
$display("args %b", 2); $display("args %b", 2);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment