Verilog Clock Generator - EDA Playground
Warning! This exercise has been opened in another tab; autosave has been disabled. Close this tab or refresh to reactivate.

 Languages & Libraries

 Tools & Simulators

 Examples

203


53
 
1
/*
2
module tb;
3
  wire clk1;
4
  wire clk2;
5
  
6
  clock_gen u0(clk1);
7
  clock_gen #(.PHASE(270)) u1(clk2);
8
  
9
  initial begin
10
    #200 u1._disable();
11
    #95 u1._enable();
12
    #300 $finish;
13
  end
14
  
15
  initial begin
16
    $dumpvars;
17
    $dumpfile("dump.vcd");
18
  end
19
endmodule
20
*/
21
22
module tb;
23
  wire clk1;
24
  wire clk2;
25
  wire clk3;
26
  wire clk4;
27
  reg  enable;
28
  reg [7:0] dly;
29
  
30
  clock_gen u0(enable, clk1);
31
  clock_gen #(.PHASE(90)) u1(enable, clk2);
32
  clock_gen #(.PHASE(180)) u2(enable, clk3);
33
  clock_gen #(.PHASE(270)) u3(enable, clk4);
34
  
35
  initial begin
36
    enable <= 0;
37
    
38
    for (int i = 0; i < 10; i= i+1) begin
39
      dly = $random;
40
      #(dly) enable <= ~enable;      
41
      $display("i=%0d dly=%0d", i, dly);
42
      #50;
43
    end
44
    
45
    #50 $finish;
46
  end
47
  
48
  initial begin
49
    $dumpvars;
50
    $dumpfile("dump.vcd");
51
  end
52
endmodule
53
xxxxxxxxxx
1
120
 
1
`timescale 1ns/1ps
2
3
/*
4
module clock_gen (output reg clk);
5
  
6
  parameter FREQ = 100000;  // in kHZ
7
  parameter PHASE = 0;      // in degrees
8
  parameter DUTY = 50;      // in percentage 
9
  
10
  real clk_pd       = 1.0/(FREQ * 1e3) * 1e9;   // convert to ns
11
  real clk_on       = DUTY/100.0 * clk_pd;
12
  real clk_off      = (100.0 - DUTY)/100.0 * clk_pd;
13
  real quarter      = clk_pd/4;
14
  real start_dly     = quarter * PHASE/90;
15
  
16
  reg enable;
17
  
18
  initial begin
19
    
20
    $display("FREQ    = %0d kHz", FREQ);
21
    $display("PHASE   = %0d deg", PHASE);
22
    $display("DUTY    = %0d %%",  DUTY);
23
    
24
    $display("PERIOD  = %0.3f ns", clk_pd);    
25
    $display("CLK_ON  = %0.3f ns", clk_on);
26
    $display("CLK_OFF = %0.3f ns", clk_off);
27
    $display("QUARTER = %0.3f ns", quarter);
28
    $display("START_DLY = %0.3f ns", start_dly);
29
  end
30
  
31
  initial begin
32
    clk     <= 0;
33
    enable  <= 0;
34
    
35
    // Initial delay to setup phasing
36
    #(start_dly) clk <= 1;
37
    
38
    // Enable clock
39
    enable <= 1;
40
  end
41
  
42
  always begin
43
    // When clock is enabled, maintain clk on/off periods
44
    if (enable) begin
45
        #(clk_on)  clk <= 0;
46
        #(clk_off) clk <= 1;
47
      
48
    // When clock is off, then set clk to zero and wait
49
    // for clock to be enabled
50
    end else begin
51
        clk <= 0;
52
        @(posedge enable);
53
    end
54
  end
55
  
56
  function _enable();
57
    enable = 1;
58
  endfunction
59
    
60
    function _disable();
61
      enable = 0;
62
    endfunction
63
 
64
endmodule
65
*/
66
67
module clock_gen (  input      enable,
68
                    output reg clk);
69
  
70
  parameter FREQ = 100000;  // in kHZ
71
  parameter PHASE = 0;      // in degrees
72
  parameter DUTY = 50;      // in percentage 
73
  
74
  real clk_pd       = 1.0/(FREQ * 1e3) * 1e9;   // convert to ns
75
  real clk_on       = DUTY/100.0 * clk_pd;
76
  real clk_off      = (100.0 - DUTY)/100.0 * clk_pd;
77
  real quarter      = clk_pd/4;
78
  real start_dly     = quarter * PHASE/90;
79
  
80
  reg start_clk;
81
  
82
  initial begin    
83
    $display("FREQ      = %0d kHz", FREQ);
84
    $display("PHASE     = %0d deg", PHASE);
85
    $display("DUTY      = %0d %%",  DUTY);
86
    
87
    $display("PERIOD    = %0.3f ns", clk_pd);    
88
    $display("CLK_ON    = %0.3f ns", clk_on);
89
    $display("CLK_OFF   = %0.3f ns", clk_off);
90
    $display("QUARTER   = %0.3f ns", quarter);
91
    $display("START_DLY = %0.3f ns", start_dly);
92
  end
93
  
94
  initial begin
95
    clk <= 0;
96
    start_clk <= 0;
97
  end
98
  
99
  always @ (posedge enable or negedge enable) begin
100
    if (enable) begin
101
      #(start_dly) start_clk = 1;
102
    end else begin
103
      #(start_dly) start_clk = 0;
104
    end      
105
  end
106
  
107
  always @(posedge start_clk) begin
108
    if (start_clk) begin
109
        clk = 1;
110
      
111
        while (start_clk) begin
112
            #(clk_on)  clk = 0;
113
            #(clk_off) clk = 1;
114
        end
115
      
116
        clk = 0;
117
    end
118
  end 
119
endmodule
120
7215 views and 0 likes     
A short description will be helpful for you to remember your playground's details
 
100:0