0
\$\begingroup\$

I am trying to write verilog code that will set the initial value of the output of a positive-edge triggered flip-flop to 0. The behaviour of the flip-flop circuit is exactly what I want AFTER the clock or the input c hits. Unfortunately, at the very beginning (before any activity in c or clk) the x value is wrong because my initial statement seems to have no effect:

Waveform Simulation Constructed Quartus Circuit

I understand that the problem may be that the Q output of the flipflop is always initially 0, and the inverter sets the x value to 1. Therefore, could someone suggest how to change the code so that the initial value of x is 0? Maybe use a series of latches? Essentially, the behaviour I want is that when the c value is high, x is also high, but when the clock hits, x is always reset to 0.

module test (c, x, clk);
   input c;
   input clk;
   output x;
   reg x;
   initial begin
     x = 0;
   end
   always @(posedge clk or posedge c) begin
      if (c) begin
        x = c;
      end else begin
        x = 1'b0;
      end
   end
endmodule
\$\endgroup\$
  • \$\begingroup\$ x is a really bad choice for a verilog variable. As x has a special meaning of an unknown value. \$\endgroup\$ – pre_randomize Mar 12 '15 at 16:43
  • 1
    \$\begingroup\$ Have you tried to put x=1 in your initial statement, so that when it is inverted it becomes 0? \$\endgroup\$ – Roger C. Mar 12 '15 at 17:47
1
\$\begingroup\$

You are using c as an active high reset, then setting the value to c rather than 1'b0. It is active high so c is always 1 when this code executes. The solution to this is the reset condition to 0 not c:

always @(posedge clk or posedge c) begin
  if (c) begin
    x <= 1'b0; //Reset Condition
  end else begin
    //x <= ...;
  end
end 

Your code makes use of an initial and asynchronous reset. This does not really represent any type of hardware you have to choose one or the other, you can not have both.

The waveform in the question does not show the async reset being applied at the start of the simulation. you should apply it for at least 1 clock cycle and release it on the posedge of your clock.

\$\endgroup\$
  • \$\begingroup\$ It should with non-blocking assignments \$\endgroup\$ – Greg Mar 12 '15 at 23:26
  • \$\begingroup\$ This does not resolve my problem. When I do a timing simulation, x is still HIGH. \$\endgroup\$ – roro172 Mar 23 '15 at 3:07
  • \$\begingroup\$ @Julia it is still high while reset (c) is applied? Your wave form shown in the quaetsion does not apply reset (c) at the start of simulation. \$\endgroup\$ – pre_randomize Mar 23 '15 at 8:19
  • \$\begingroup\$ If c will be high, then x will be high. Unfortunately I want x to be 0 in the initial state, but Quartus constructs what it thinks to be the optimal version as that little flip-flop component, and ignores my initial declaration. \$\endgroup\$ – roro172 Mar 26 '15 at 2:12
  • \$\begingroup\$ @Julia I have expanded answer a little. \$\endgroup\$ – pre_randomize Mar 26 '15 at 8:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.