1
module latch(clk,d,q);
    input clk,d;
    output q;
    
    if(clk==1)
    assign q = d;
endmodule

Why am I getting error in the above code?

2
  • 1
    Can you add the details of the error in your question? Maybe a screenshot? Commented Aug 8, 2020 at 15:30
  • Error: Peak virtual memory: 4690 megabytes Error (10170): Verilog HDL syntax error at latch.v(4) near text: "if"; expecting "endmodule". Check for and fix any syntax errors that appear immediately before or at the specified keyword. Commented Aug 9, 2020 at 10:44

1 Answer 1

4

if is not a top level block inside a module. It should be used inside an always, or initial (if for simulation purposes) block, or other legal blocks. While assign is. (generate is not taken into consideration, for simplicity)

I guess what you want to achieve is:

module latch(clk, d, q);
input clk, d;
output q;
reg q;
always@(*)begin
  if(clk)begin
    q = d;
  end
end
Sign up to request clarification or add additional context in comments.

2 Comments

So, I can't use any control flow statements in conjunction in "assign"?
You can use ?: like assign q = clk ? d : q; but not all tools will be happy with this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.