cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
reetbansal
Adventurer
Adventurer
12,795 Views
Registered: ‎12-17-2014

verilog code to find remainder

i had written verilog code in order to find remainder when we divide two numbers but i face one problem.....my algorithm is

i have 'q' dividend and 'm' divisor, 'rem' is remainder.....so if(q>m)

q=q-m otherwise rem=q

i write its verilog code but in that if statemnet runs for once....in next clock cycle it take value of p=q which i dont want i want my value in p comes as p=p-m;

code is:

module div(q,clk,rem,p,count
    );
parameter m=13'd840;
input [12:0] q;
input clk;
output reg [12:0] rem;
output reg [3:0] count;
output reg [12:0] p;
initial
 begin
 count=4'b0;
//+ rem=9'b0;
//p=13'b0;
 end
 
 
always@(posedge clk)
begin
 p=q;
    if (p>m)
     begin
     p=p-m;
	  
     count=count+1;
     rem=p;
	  end
	 else 
	 rem=9'b0;
end
 
 
endmodule

 

0 Kudos
Reply
3 Replies
vijayak
Xilinx Employee
Xilinx Employee
12,730 Views
Registered: ‎10-24-2013

Hi,
Post Waveform for easy understanding and then post how are you expecting the output to be.
Thanks,Vijay
--------------------------------------------------------------------------------------------
Please mark the post as an answer "Accept as solution" in case it helped resolve your query.
Give kudos in case a post in case it guided to the solution.
0 Kudos
Reply
dprasad
Xilinx Employee
Xilinx Employee
12,726 Views
Registered: ‎09-13-2014

Not sure what's your intention. You can simply used module(%) operator in Verilog. --dhiRAj
0 Kudos
Reply
markcurry
Scholar
Scholar
12,718 Views
Registered: ‎09-16-2009

 

Couple of things. One, if you intend to synthesize your design, then the modulus operator '%' has restrictions - the divisor can only be a power of two.  So dprasad's solution would NOT work.

 

If you're just playing around with simulation, then yes, that solution is fine.

 

In any case, some more comments on your original code.  Ask yourself - "how does the operation begin.  I.e. when do you initialize count to zero, and set p=q?  How long (in clock cycles) does it take to calculate my remainder?  When can I change my input 'q' and have it calculate a new remainder?

 

Regards,

 

Mark

 

0 Kudos
Reply