-4

I wrote a code like this:

   if (a) begin
   //some coding
   end
   else begin
   stage = 0;
           if (b) begin
                  if (stage == 0) begin
                  stage = 1;
                  end
                  else if (stage == 1) begin
                  stage = 2;
                  end
                  else begin
                  stage = 0;
            end
            else
            // some coding
            end
    end

When stage is 1, the code inside stage == 1 cannot perform. Is there anything wrong with my code?

Luca.A
  • 59
  • 1
  • 6

3 Answers3

1

Put like this, it cannot work. You have to put it in a process, i.e. always@(something).

//adding a signal needed
reg[1:0] stage_next;     

always@(*)begin//Means that everytime an event occurs in one of the signals used in the following code,
               //the code contained in this process will be executed
    if (a) begin
   //some coding
    end
    else begin
    stage = 0;
            if (b) begin
                  if (stage == 0) begin
                  //stage = 1;//this assignement will produce a combinatorial loop
                            //when the value of 'stage' changes the process(always@(*))
                            //will be re-executed. You have to assign another signal(stage_next here)
                  stage_next = 1
                  end
                  else if (stage == 1) begin
                  //stage = 2;//Same here
                  stage_next = 2;
                  end
                  else begin
                  //stage = 0;//Same here
                  stage_next = 0;
            end
            else
            // some coding
            end
    end
end
//The other process where stage will take the value of stage_next should be synchronous
//It will break the combinatorial loop
always@(posedge clk)
  stage <= stage_next;
Krouitch
  • 596
  • 3
  • 13
0

The blocking assignment stage = 0 before if (b) is forcing stage to zero before the stage == 1 condition is evaluated.

Use non-blocking assignment (<=) to all stage assignments and you should get the expected behavior. See How to interpret blocking vs non blocking assignments in Verilog?
Your code block must be within an sequential block (clock triggering, ex: always @(posedge clk))

Community
  • 1
  • 1
Greg
  • 18,111
  • 5
  • 46
  • 68
0

I'd say your indentation is misleading, try instead:

   always @(*)
   if (a) begin
   //some coding
   end
   else begin
           stage = 1;
           if (b) begin
                  if (stage == 0) begin
                      stage = 1;
                  end
                  else if (stage == 1) begin
                      stage = 2;
                  end
                  else begin
                      stage = 0;
                  end
            end // Your code did not have this end
            else begin // This begin was also missing
            // some coding
            end
    end

So I think you are saying if you have that code, then you don't see stage == 2??? Because without those code modifications you'll have the else binding to an unexpected if

Chris Hopkins
  • 961
  • 1
  • 7
  • 15