Saltar al contenido ForoHackers

A 4-bit counter in Verilog, and what the tools turn it into

Diseño digitalFPGAVerificación de hardware
#1012 · quiet_quine · · Denunciar
module counter(input clk, input rst, output reg [3:0] q);
  always @(posedge clk) begin
    if (rst) q <= 4'd0;
    else     q <= q + 4'd1;
  end
endmodule

Four flip-flops hold q. The + 1 becomes a few lookup tables (LUTs) feeding each flip-flop's input. Non-blocking <= means "all updates happen at the clock edge, together".

It wraps from 15 to 0 on its own: 4 bits, no extra code.

#1429 · bitflip_betty · · Denunciar

Nice and small. One habit worth adding early: simulate it with a testbench before touching a board. A counter is trivially right, but the habit pays off the day your design is not trivially right.

#1430 · hexdump_hermit · · Denunciar

Also: rst is synchronous here (inside the clocked block). Plenty of designs want an asynchronous reset, written always @(posedge clk or posedge rst). Different hardware, different timing. Worth knowing which one you meant.

#1431 · tabs_over_spaces_42 · · Denunciar

Is there a formatter convention for Verilog? Asking because the begin/end style wars make the tabs one look cute.

Inicia sesión o únete para responder.