1
\$\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\$
2
  • \$\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\$
5
  • \$\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
0
\$\begingroup\$

(Answering after 5 years; might get useful to someone like me.)

The schematic output from Vivado 2020.2 gives this schematic. So the problem seems to be according to schematic in question. Although the two schematics are functionally same, the functioning before inputs are applied isn't same.

Schematic output from Vivado 2020.2

Somehow, the Quartus (used in question) needs to be forced to utilize PRE instead of CLRN input for c. Accordingly, the following Code should work as desired in question with all simulators.

module test (input c, x, output reg clk);
   initial x = 0;
   always @(posedge clk or posedge c)
      if (c) x = 1'b1;
      else x = 1'b0;
endmodule
\$\endgroup\$
2
  • \$\begingroup\$ It is a little unclear whether you are talking about Quartus or Vivado here. Also, you start talking about synthesis and then say your approach "might work...with all simulators". If you really are talking about synthesis then the target implementation becomes relevant. Designing a flip-flop that assume a known state as power is coming up is very difficult, so assuming an unknown output at t=0 seems prudent. \$\endgroup\$ – Elliot Alderson Apr 4 at 13:51
  • \$\begingroup\$ Hey Elliot, thank you for pointing out. Do the edits resolve it? Meanwhile, "might" because I haven't used many simulators. Vivado, ISE I can predict the output schematic. Quartus, ModelSim etc, not sure (now after seeing the question). \$\endgroup\$ – Himanshu Tanwar Apr 4 at 13:56

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.