Debian Codegraph @

examples/blinky-clkdiv.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 genvar i;
35 generate
36 for (i=0; i < `NUM_HCLK; i=i+1) begin:hcount
37 localparam /*string*/ div_mode =(i % 4 == 0) ? DIV_MODE_2 :
38 (i % 4 == 1) ? DIV_MODE_35 :
39 (i % 4 == 2) ? DIV_MODE_4 :
40 DIV_MODE_5;
41
42 wire div2_out;
43 wire o_clk;
44
45 CLKDIV2 my_div2 (
46 .RESETN(key),
47 .HCLKIN(clk),
48 .CLKOUT(div2_out)
49 );
50
51 CLKDIV #(.DIV_MODE(div_mode)) my_div (
52 .RESETN(1'b1),
53 .HCLKIN(div2_out),
54 .CLKOUT(o_clk)
55 );
56
57 reg [23:0] count;
58 always @(posedge o_clk)begin
59 count <= count + 1'b1;
60 end
61 assign hclk_counts[i] = count[23];
62 end
63 endgenerate
64
65 reg old_bit;
66 always @(posedge clk) begin
67 sup_count <= sup_count + 1'b1;
68
69 curr_hclk_idx <= curr_hclk_idx;
70 old_bit <= old_bit;
71 if (old_bit != sup_count[29]) begin
72 old_bit <= sup_count[29];
73 if (curr_hclk_idx + 1 == `NUM_HCLK) begin
74 curr_hclk_idx <= 0;
75 end else begin
76 curr_hclk_idx <= curr_hclk_idx + 1;
77 end
78 end
79 end
80
81 assign curr_hclk = hclk_counts[curr_hclk_idx];
82 assign led = {curr_hclk, sup_count[23]};
83
84 endmodule