aboutsummaryrefslogtreecommitdiff
path: root/rtl/src/clock_divider.v
blob: 8d25fdfadc22584ad9d6cb91257f206c588206d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// clock divider:
// This is used to scale the input clock signal by a certain amount, which then feeds into the cpu,
// to decrease its frequency, useful for debugging for example.

module clock_divider #( 
  parameter  N = 2
)(
  input      clk,
  input      rstn,

  output reg clk_div
);

reg [31:0] counter = 0;

always @(posedge clk or negedge rstn) begin
  if (!rstn) begin
      counter <= 0;
      clk_div <= 0;
  end else begin
    if (counter == (N-1)/2) begin
      clk_div <= ~clk_div;
      counter <= counter + 1;
    end else if (counter >= (N-1)) begin
      clk_div <= ~clk_div;
      counter <= 0;
    end else begin
      counter <= counter + 1;
    end
  end
end

endmodule