-
Notifications
You must be signed in to change notification settings - Fork 0
/
ptp-clock.sv
165 lines (128 loc) · 3.74 KB
/
ptp-clock.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//A clock for synchronization in PTP protocol (time drift, offset adjustment)
// Language: Verilog 2001
`timescale 1ns / 1ps
`default_nettype none
/*
* PTP clock module
*/
module ptp_clock #
(
parameter DRIFT_NS = 4'h0,
parameter DRIFT_RATE = 16'h0005,
parameter totalCycles = 500,
parameter timeUnit = 31,
)
(
input wire clk,
input wire rst,
input wire input_ts_96_valid,
input wire input_period_valid,
input wire input_adj_valid,
input wire input_drift_valid,
/*
* Timestamp outputs
*/
output wire [timeUnit-1:0] output_ts_96
);
/*
* Timestamp inputs for synchronization
*/
(* anyconst *) reg [timeUnit-5:0] input_ts_96;
/*
* Period adjustment
*/
(* anyconst *) reg [timeUnit-5:0] input_period_ns;
reg [timeUnit-5:0] period_ns_reg;
/*
* Offset adjustment
*/
(* anyconst *) reg [timeUnit-5:0] input_adj_ns;
(* anyconst *) reg [timeUnit-5:0] input_adj_count;
reg [timeUnit-5:0] adj_ns_reg;
reg [timeUnit-5:0] adj_count_reg;
/*
* Drift adjustment
*/
(* anyconst *) reg [timeUnit-5:0] input_drift_ns;
(* anyconst *) reg [timeUnit-5:0] input_drift_count;
reg [timeUnit-5:0] drift_ns_reg;
reg [timeUnit-5:0] drift_cnt;
reg [timeUnit-1:0] outputTime;
assign output_ts_96 = outputTime;
//---formal verification----//
reg [timeUnit-1:0] globalTimer;
always @(posedge clk) begin
if(!rst) begin
// latch parameters
if (input_period_valid) begin
period_ns_reg <= input_period_ns;
end
if (input_adj_valid) begin
adj_ns_reg <= input_adj_ns;
end
if (input_drift_valid) begin
drift_ns_reg <= input_drift_ns;
end
// timestamp increment calculation
outputTime <= input_ts_96 + period_ns_reg + ( (drift_cnt==0)? drift_ns_reg : 0) +
((adj_count_reg == 0) ? adj_ns_reg : 0);
// offset adjust counter
if (adj_count_reg > 0) begin
adj_count_reg <= adj_count_reg - 1;
end
else begin
adj_count_reg <= adj_count_reg;
end
// drift counter
if (drift_cnt > 0) begin
drift_cnt <= drift_cnt - 1;
end else begin
drift_cnt <= 0; //reset
end
// 96 bit timestamp
// no increment seconds field, pre-compute both normal increment and overflow values
end
else begin //rst
period_ns_reg <= 0;
adj_count_reg <= input_adj_count;
drift_cnt <= input_drift_count;
adj_ns_reg <= 0;
drift_ns_reg <= 0;
outputTime <= 0;
end
end
//absolute timer
always @(posedge clk) begin
if(rst) globalTimer <= 0;
else globalTimer <= globalTimer + 1;
end
`ifdef FORMAL
always @(posedge clk) begin
//system configurations//
assume(input_period_valid);
assume(input_adj_valid);
assume(input_drift_valid);
assume(input_adj_count + input_drift_count == totalCycles );
/*self-test
assume(input_start == 0);
assume(input_ts_96 == 160);
assume(input_period == 60); pulseCounter == 3
*/
if(!$initstate) begin
assume(rst == 0);
//a timing property
if( globalTimer < totalCycles ) assert( outputTime <= adj_ns_reg + drift_ns_reg + period_ns_reg + input_ts_96 );
// if( globalTimer == totalCycles )assert(pulseCounter == 2);
//Invariant
// assume(pulseCounter <= input_ts_96 - input_start ); //-- not automatic
// assume(pulseCounter <= 10 ); //-- not automatic
//maxValue of input_period: maxPeriod as a efficient
//-----//
end
else begin
assume(rst);
end
end
`endif
endmodule
`resetall