EmbDev.net

Forum: FPGA, VHDL & Verilog Verilog code for modulus of negative number query


von Lakshita J. (lakshita)


Attached files:

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)

von Duke Scarring (Guest)


Rate this post
useful
not useful
Lakshita J. wrote:
> Should I declare my input or output as signed data
> type to get correct results?
Of course. Unsigned data can only represent positive numbers...

von Lothar M. (Company: Titel) (lkmiller) (Moderator)


Rate this post
useful
not useful
Lakshita J. wrote:
> assign y =(-x1)+(($floor(-x1/p))* p);
A hint: split this calculation up into several steps.
Then you can see much more easy where the bug is hidden.

> Should I declare my input or output as signed data type to get correct
> results?
To be honest: I would try that out myself. Before asking in a forum...

: Edited by Moderator
von Lakshita J. (lakshita)


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.

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.