Home Verilog Introduction Verilog Data Types Verilog Comments Verilog Operators Verilog Modules and Ports Verilog Conditional, Case and Looping Statements Verilog Assignment Statements Verilog Instantiation and Connecting Ports Verilog Abstraction Level Verilog Testbench Verilog Tools Usage Verilog System Tasks and Functions Verilog Compiler Directives Verilog Examples Verilog Projects

Home > Verilog > Mod-N Counter

Verilog Mod-N Counter

ShaunT

Mod-N Counter is a digital sequential circuit that counts from zero to the value N-1, i.e. total N values.

Once the counter counts the N-1 value, it is again reset to ‘0’.

The Verilog codes are programmed and simulated using EDA playground and/or Mentor Graphics Model Sim and/or Xilinx Vivado.

Tutorials for how to use HDL programming and Simulation tools.

Programs:

Design

module mod_n_counter(clk, rst, q_out);
 
	parameter WIDTH = 4;
	parameter N = 10;
	input clk, rst;
	output [WIDTH-1:0] q_out;
	reg [WIDTH-1:0] temp;
 
	always @ (posedge clk or posedge rst)
	begin
	
		if(rst)
			temp <= 0; //Reset
		else begin
			if(temp == N-1)
				temp <= 0;
			else
				temp <= temp + 1;
		end
	end
	assign q_out = temp;
	
endmodule

Testbench

module tb_mod_n_counter;

	parameter WIDTH = 4;
	parameter N = 10;
	reg clk, rst;
	wire [WIDTH-1:0] q_out;

	mod_n_counter #(.WIDTH(WIDTH), .N(N)) //Overriding WIDTH in design module
			dut (clk, rst, q_out);

	initial begin
		clk = 0;
		forever begin
			#5; clk = ~clk;
		end
	end

	initial begin
		rst = 1;
		#10; rst = 0;
		#120; $finish;
	end
endmodule





POPULAR TAGS

Travel New York London IKEA NORWAY DIY Ideas Baby Family News Clothing Shopping Sports Games