Debian Codegraph @

examples/blinky-clkdiv-dhcen.v

raw
1 module top (
2 input clk,
3 input key_i,
4 output [`LEDS_NR-1:0] led
5 );
6
7 /* Expected Result:
8 - 1 or 2 LEDs blinking, depending on your board.
9 - The `faster` LED is associated with the input clock.
10 - The `slower` LED is associated with a divided clock.
11 - On boards with multiple CLKDIVs, the slower LED's blink rate changes as it cycles through CLKDIVs
12 configured with different DIV_MODES.
13 - If your board has only one LED, it should blink with the divided clock.
14 - Holding the appropriate button should stop the blinking.
15 */
16
17 localparam /*string*/ DIV_MODE_2 = "2";
18 localparam /*string*/ DIV_MODE_35 = "3.5";
19 localparam /*string*/ DIV_MODE_4 = "4";
20 localparam /*string*/ DIV_MODE_5 = "5";
21
22
23 wire key = key_i ^ `INV_BTN;
24
25 reg [25:0] ctr_q;
26 wire [25:0] ctr_d;
27 // localparam NUM_HCLKi=NUM_HCLK;
28
29 wire [`NUM_HCLK-1:0] hclk_counts;
30 reg [$clog2(`NUM_HCLK)-1:0] curr_hclk_idx;
31 wire curr_hclk;
32 reg [30:0] sup_count;
33
34
35 wire managed_clk;
36 DHCEN dhcen(
37 .CLKIN(clk),
38 .CE(key),
39 .CLKOUT(managed_clk)
40 );
41
42 genvar i;
43 generate
44 for (i=0; i < `NUM_HCLK; i=i+1) begin:hcount
45 localparam /*string*/ div_mode =(i % 4 == 0) ? DIV_MODE_5 :
46 (i % 4 == 1) ? DIV_MODE_4 :
47 (i % 4 == 2) ? DIV_MODE_35 :
48 DIV_MODE_2;
49
50 wire div2_out;
51 wire o_clk;
52
53 CLKDIV2 my_div2 (
54 .RESETN(1'b1),
55 .HCLKIN(managed_clk),
56 .CLKOUT(div2_out)
57 );
58
59 CLKDIV #(.DIV_MODE(div_mode)) my_div (
60 .RESETN(1'b1),
61 .HCLKIN(div2_out),
62 .CLKOUT(o_clk)
63 );
64
65 reg [23:0] count;
66 always @(posedge o_clk)begin
67 count <= count + 1'b1;
68 end
69 assign hclk_counts[i] = count[23];
70 end
71 endgenerate
72
73 reg old_bit;
74 always @(posedge clk) begin
75 sup_count <= sup_count + 1'b1;
76
77 curr_hclk_idx <= curr_hclk_idx;
78 old_bit <= old_bit;
79 if (old_bit != sup_count[29]) begin
80 old_bit <= sup_count[29];
81 if (curr_hclk_idx + 1 == `NUM_HCLK) begin
82 curr_hclk_idx <= 0;
83 end else begin
84 curr_hclk_idx <= curr_hclk_idx + 1;
85 end
86 end
87 end
88
89 assign curr_hclk = hclk_counts[curr_hclk_idx];
90 assign led = {curr_hclk, sup_count[23]};
91
92 endmodule