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

assignment not respecting RHS signedness #502

Closed
programmerjake opened this issue Oct 6, 2020 · 1 comment
Closed

assignment not respecting RHS signedness #502

programmerjake opened this issue Oct 6, 2020 · 1 comment

Comments

@programmerjake
Copy link
Contributor

I'm using nmigen commit 69ed491

I expect nmigen to behave more like most other programming languages in that when converting from a smaller RHS type to a larger LHS type, the sign/zero extension is decided based on the RHS. The expected behavior matches what was decided for #464

Code:

from nmigen import Signal, Module, signed, unsigned, Const
from nmigen.sim import Simulator, Delay

m = Module()
inp = Signal(unsigned(8))
out = Signal(unsigned(16))

m.d.sync += out.eq(inp.as_signed())

sim = Simulator(m)

sim.add_clock(1e-6)

def process():
    yield inp.eq(0xFF)
    yield
    yield
    inp_v = yield inp
    expected = Const.normalize(inp_v, signed(8))
    expected = Const.normalize(expected, unsigned(16))
    out_v = yield out
    print(hex(inp_v), hex(expected), hex(out_v))
    assert expected == out_v

sim.add_sync_process(process)
sim.run()

Output:

0xff 0xffff 0xff
Traceback (most recent call last):
  File "nmigen-bug.py", line 26, in <module>
    sim.run()
  File "/home/jacob/projects/libreriscv/nmigen/nmigen/sim/core.py", line 165, in run
    while self.advance():
  File "/home/jacob/projects/libreriscv/nmigen/nmigen/sim/core.py", line 156, in advance
    return self._engine.advance()
  File "/home/jacob/projects/libreriscv/nmigen/nmigen/sim/pysim.py", line 315, in advance
    self._step()
  File "/home/jacob/projects/libreriscv/nmigen/nmigen/sim/pysim.py", line 304, in _step
    process.run()
  File "/home/jacob/projects/libreriscv/nmigen/nmigen/sim/_pycoro.py", line 123, in run
    self.coroutine.throw(exn)
  File "/home/jacob/projects/libreriscv/nmigen/nmigen/sim/_pycoro.py", line 64, in run
    command = self.coroutine.send(response)
  File "/home/jacob/projects/libreriscv/nmigen/nmigen/sim/core.py", line 90, in wrapper
    yield from process()
  File "nmigen-bug.py", line 23, in process
    assert expected == out_v
AssertionError

Expected output:

0xff 0xffff 0xffff
@whitequark whitequark added the bug label Oct 6, 2020
@whitequark whitequark added this to the 0.3 milestone Oct 6, 2020
@whitequark
Copy link
Member

@programmerjake I believe this is actually a sim/synth mismatch, since it translates to the following Verilog:

module top(inp);
  wire [15:0] \$1 ;
  input [7:0] inp;
  wire [15:0] out;
  assign \$1  = + $signed(inp);
  assign out = \$1 ;
endmodule

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

No branches or pull requests

2 participants