Verilog code for modulus of negative number query

OP #5378018
Rate this post
• ▲ useful
▼ not useful
Hello,

I have written code for calculating modulus of negative number for 
number of bits (n=5),it is giving me correct result for n=5 bits but 
when i increase number of bits for eg n=6,it is giving me incorrect 
result.Why is it so? Should I declare my input or output as signed data 
type to get correct results?
please tell if anybody knows about signed and unsigned data type.

below is my code :-

module negmod #(parameter n = 5) (clk, reset, p, x1,x3,result);
input clk, reset;
input  [n-1:0] p, x1;

output reg  [n-1:0] x3;

output reg result;
wire [n-1:0] y;

assign y =(-x1)+(($floor(-x1/p))* p);
always @(posedge clk) begin
if(reset)begin
x3<=0;
end
else begin
x3<=y;

result<=1;
end
end
endmodule

I have attached 2 simulation results for n=5 and n=6 bits
-3 mod 17= 14 (n=5 )
-3 mod 17 =48 (n=6)
Attached files:
OP #5378236
Rate this post
• ▲ useful
▼ not useful
Lothar M. wrote:
> A hint: split this calculation up into several steps.
> Then you can see much more easy where the bug is hidden.

okay.I will try to debug this step-wise. Thanks


> To be honest: I would try that out myself. Before asking in a forum...

I tried it before asking in a forum but I am not getting correct results 
after declaring input as signed type.

Reply

Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.

Formatting
  • [c]C-Code[/c]
  • [vhdl]VHDL-Code[/vhdl]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.