Advertisement
Guest User

Untitled

a guest
Nov 5th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. `timescale 1ns / 1ps
  2. //////////////////////////////////////////////////////////////////////////////////
  3. // Company:
  4. // Engineer:
  5. //
  6. // Create Date:    21:23:40 11/05/2018
  7. // Design Name:
  8. // Module Name:    counter
  9. // Project Name:
  10. // Target Devices:
  11. // Tool versions:
  12. // Description:
  13. //
  14. // Dependencies:
  15. //
  16. // Revision:
  17. // Revision 0.01 - File Created
  18. // Additional Comments:
  19. //
  20. //////////////////////////////////////////////////////////////////////////////////
  21. module counter #(parameter max=10'd30) (
  22.     input rst,
  23.     input clk,
  24.     input load,
  25.     input [5:0] initVal,
  26.     output [5:0] cout
  27.     );
  28.  
  29. localparam nff = clogb2(max);
  30. reg [nff-1:0] cnt;
  31. reg [nff-1:0] initValCopy;
  32.  
  33. function integer clogb2(input [5:0] value);
  34. begin
  35.     value = value - 1;
  36.     for (clogb2 = 0; value > 0; clogb2 = clogb2 + 1)
  37.         value = value >> 1;
  38. end
  39. endfunction
  40.  
  41. always @(posedge clk, posedge rst)
  42.     if (rst)
  43.         cnt <= {nff{1'b0}};
  44.     else if (cnt == {nff{1'b0}})
  45.         cnt <= initValCopy;
  46.     else
  47.         cnt <= cnt - 1;
  48.        
  49. always @(posedge clk, posedge rst)
  50.     if (rst)
  51.         initValCopy = {nff{1'b0}};
  52.     else if (load)
  53.         initValCopy = initVal;
  54.  
  55. assign cout = cnt;
  56.  
  57. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement