Altera_Forum
Honored Contributor
15 years agoProblems with Verilog % Modulo and if-Statements
Hi all,
I got a problem with a normally relatively straightforward piece of Verilog code representing some modulo arithmetics and an if-statement. In the code below, the modulo of the registers a and b is calculated ( c=a mod b). The next if statement then checks if c, the result of the mod operation is equal to zero. Depending on how the values for a and b are put into the registers, the code works fine or produces rubbish: If the FIRST Option is used to obtain values for a and b, the condition of the if-statement doesn't become true and the following code isn't executed. However, if the SECOND Option is used to hardwire the values, the if-statement works fine. I already checked the output register c of the modulo operation which appears to be 4'b0000 in both cases. I'm stuck here and could really use some help on what's wrong with the modulo operation and/or the if-statement. I'm using Quartus II 10.0 Build 262 Web Edition on Ubuntu 10.04 LTS. The testing is done on the actual hardware, a Cyclone II FPGA on a TS7300 Board by embeddedarm. If you need any more information, I'll be more than happy to supply. Any help is appreciated, Regards, Jonas Lindmann
reg ip;
reg iq;
reg iStage;
reg a;
reg b;
reg c;
always @(posedge wb_clk_i)
begin
// FIRST OPTION: Calculate a and b
ip = 3'b001; // 1
iq = 3'b011; // 3
iStage = 3'b010; // 2
a = iq + 1'b1; // 4 = 3 + 1
b = 1'b1<<(iStage); // 4
// ---------------------------------
// SECOND OPTION "Hardwire" a and b
// a = 4'b0100; // 4
// b = 4'b0100; // 4
// ---------------------------------
c = a % b; // 0 = 4 mod 4
if (c == 4'b0000)
begin
// Do something
// This code here is never executed when using 1st option.
end
else begin
// Do something else
end
end