1
\$\begingroup\$

Following is the Verilog code for a 4-bit unsigned up counter with asynchronous clear as shown in ASIC.CO.IN:

module counter (clk, clr, q);
input clk, clr;
output [3:0] q;
reg [3:0] tmp;
always @(posedge clk or posedge clr)
begin
 if (clr)
 tmp <= 4’b0000;
 else
 tmp <= tmp + 1’b1;
end
 assign q = tmp;
endmodule
 

Can I define the output as reg and write the verilog as following?:

module counter (clk, clr, q);
input clk, clr;
output [3:0] q;
reg [3:0] q;
always @(posedge clk or posedge clr)
begin
 if (clr)
 q<= 4’b0000;
 else
 q<= q+ 1’b1;
end
endmodule
 

As I see this, there is no difference between those two codes, am I wrong?

asked Dec 25, 2020 at 7:56
\$\endgroup\$
1
  • \$\begingroup\$ Yes, they are equivalent. \$\endgroup\$ Commented Dec 25, 2020 at 8:23

2 Answers 2

3
\$\begingroup\$

There is no difference in the two examples you wrote. You can even make is simpler:

module counter (
 input wire clk, clr,
 output reg [3:0] q
);
always @(posedge clk or posedge clr)
begin
 if (clr)
 q<= 4'b0000;
 else
 q<= q+ 1'b1;
end
endmodule
answered Dec 25, 2020 at 8:43
\$\endgroup\$
0
0
\$\begingroup\$

If it is anything like VHDL older than VHDL 2008, it is because you cannot read an output (you can only write to an output), and performing something like q<=q+1 requires you to read q.

answered Dec 25, 2020 at 8:37
\$\endgroup\$
1
  • \$\begingroup\$ please check the following, which is the Verilog code for flip-flop with a positive-edge clock. module flop (clk, d, q); input clk, d; output q; reg q; always @(posedge clk) begin q <= d; end endmodule as you can see, output as register can be written \$\endgroup\$ Commented Dec 25, 2020 at 8:40

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.