Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constant sign-extends incorrectly. #271

Closed
kbob opened this issue Nov 17, 2019 · 6 comments
Closed

Constant sign-extends incorrectly. #271

kbob opened this issue Nov 17, 2019 · 6 comments

Comments

@kbob
Copy link

kbob commented Nov 17, 2019

This program shows that subtracting 1 from a signed signal actually adds one.

The generated Verilog shows this statement. I think Yosys sign-extends $signed(1'h1) to 24'b111111111111111111111111 before subtracting.

  assign \$2  = $signed(bar) - (* src = "foo.py:14" *) $signed(1'h1);

Versions:

nMigen v0.1

Yosys 0.9+932 (git sha1 ab0fb19c, clang 11.0.0 -fPIC -Os)

@whitequark
Copy link
Contributor

Does it work correctly if you simulate the design?

@kbob
Copy link
Author

kbob commented Nov 18, 2019

Yes. pysim subtracts one.

@whitequark
Copy link
Contributor

Since both the Verilog and simulation behavior is correct, this isn't an nMigen bug.

@programmerjake
Copy link
Contributor

The generated Verilog is incorrect since a 1 bit signed value can only have the values -1 and 0. nmigen should zero-extend unsigned values when converting them to
a wider signed type. The integer type conversion is needed since Verilog doesn't have (as far as I know) operations that have one signed and one unsigned operand.

See also: C++ example on Compiler Explorer.

@whitequark
Copy link
Contributor

Ah, hm. You're right, I missed that it was 1-bit. That's troublesome.

@kbob
Copy link
Author

kbob commented Nov 18, 2019

I confirm that the fix works for my code. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants