Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- `timescale 1ns / 1ps
- //////////////////////////////////////////////////////////////////////////////////
- // Company:
- // Engineer:
- //
- // Create Date: 21:23:40 11/05/2018
- // Design Name:
- // Module Name: counter
- // Project Name:
- // Target Devices:
- // Tool versions:
- // Description:
- //
- // Dependencies:
- //
- // Revision:
- // Revision 0.01 - File Created
- // Additional Comments:
- //
- //////////////////////////////////////////////////////////////////////////////////
- module counter #(parameter max=10'd30) (
- input rst,
- input clk,
- input load,
- input [5:0] initVal,
- output [5:0] cout
- );
- localparam nff = clogb2(max);
- reg [nff-1:0] cnt;
- reg [nff-1:0] initValCopy;
- function integer clogb2(input [5:0] value);
- begin
- value = value - 1;
- for (clogb2 = 0; value > 0; clogb2 = clogb2 + 1)
- value = value >> 1;
- end
- endfunction
- always @(posedge clk, posedge rst)
- if (rst)
- cnt <= {nff{1'b0}};
- else if (cnt == {nff{1'b0}})
- cnt <= initValCopy;
- else
- cnt <= cnt - 1;
- always @(posedge clk, posedge rst)
- if (rst)
- initValCopy = {nff{1'b0}};
- else if (load)
- initValCopy = initVal;
- assign cout = cnt;
- endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement