// counter.sv // Two modules: // counter parameterised up-counter with synchronous reset and enable // counter_top two counter instances cascaded into a 12-bit count // Lint with: verilator --lint-only -Wall -Wno-DECLFILENAME counter.sv // (DECLFILENAME only reports that counter_top is not in a file of its own.) module counter #( parameter int WIDTH = 8 ) ( input logic clk, input logic rst, // synchronous, active high input logic en, // count enable output logic [WIDTH-1:0] count ); always_ff @(posedge clk) begin if (rst) count <= '0; else if (en) count <= count + WIDTH'(1); end endmodule // Two counter instances cascaded into a 12-bit count. // Shows parameter overrides and named port connections. module counter_top ( input logic clk, input logic rst, input logic en, output logic [11:0] count ); logic [3:0] low; logic [7:0] high; logic low_wrap; // The low counter wraps when it is at 4'hF and about to count. assign low_wrap = en && (low == 4'hF); counter #(.WIDTH(4)) u_low ( .clk (clk), .rst (rst), .en (en), .count (low) ); counter #(.WIDTH(8)) u_high ( .clk (clk), .rst (rst), .en (low_wrap), .count (high) ); assign count = {high, low}; endmodule