I'm new here as in verilog...
I want to ask about the difference between = and <= in this program? and how to print the value of data?
module always_example();
reg clk,reset,enable,q_in,data;
always @ (posedge clk)
if (reset) begin
data <= 0;
end else if (enable) begin
data <= q_in;
end
// if i put $print("data=%d", data); there is error
endmodule
<= is a nonblocking assignment. It is used to describe sequential logic, like in your code example. Refer to IEEE Std 1800-2012, section 10.4.2 "Nonblocking procedural assignments".
= is for blocking assignments. It is used to describe combinational logic.
See also Nonblocking Assignments in Verilog Synthesis, Coding Styles That Kill!
You can use $display
instead of $print
to print the value of variables. See also IEEE Std 1800-2012, section 21.2 "Display system tasks".
= is blocking statement. In an always
block, the line of code will be executed only after it's previous line has executed. Hence, they happens one after the other, just like combinatoral logics in loop.
<= is non-blocking in nature. This means that in an always
block, every line will be executed in parallel. Hence leading to implementation of sequential elements.