-
Notifications
You must be signed in to change notification settings - Fork 399
/
Copy pathsim_clk_gen.sv
72 lines (59 loc) · 1.8 KB
/
sim_clk_gen.sv
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//------------------------------------------------------------------------------
// sim_clk_gen.sv
// published as part of https://github.com/pConst/basic_verilog
// Konstantin Pavlov, [email protected]
//------------------------------------------------------------------------------
// INFO ------------------------------------------------------------------------
// Testbench clock generator written in System Verilog
//
`timescale 1ns / 1ps
module sim_clk_gen #( parameter
FREQ = 200_000_000, // in Hz
PHASE = 0, // in degrees
DUTY = 50, // in percentage
DISTORT = 200 // in picoseconds
)(
input ena,
output logic clk, // ideal clock
output logic clkd // distorted clock
);
real clk_pd = 1.0 / FREQ * 1e9; // convert to ns
real clk_on = DUTY / 100.0 * clk_pd;
real clk_off = (100.0 - DUTY) / 100.0 * clk_pd;
real start_dly = clk_pd / 4 * PHASE / 90;
logic do_clk;
initial begin
$display("FREQ = %0d Hz", FREQ);
$display("PHASE = %0d deg", PHASE);
$display("DUTY = %0d %%", DUTY);
$display("DISTORT = %0d ps", DISTORT);
$display("PERIOD = %0.3f ns", clk_pd);
$display("CLK_ON = %0.3f ns", clk_on);
$display("CLK_OFF = %0.3f ns", clk_off);
$display("START_DLY = %0.3f ns", start_dly);
end
initial begin
clk <= 0;
do_clk <= 1;
end
always @ (posedge ena or negedge ena) begin
if (ena) begin
#(start_dly) do_clk = 1;
end else begin
#(start_dly) do_clk = 0;
end
end
always @(posedge do_clk) begin
if( do_clk ) begin
clk = 1;
while ( do_clk ) begin
#(clk_on) clk = 0;
#(clk_off) clk = 1;
end
clk = 0;
end
end
always @(*) begin
clkd = #($urandom_range(0, DISTORT)*1ps) clk;
end
endmodule