Skip to content

Commit

Permalink
Improve ToD timestep generation in PTP clock and PTP CDC module
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Forencich <[email protected]>
  • Loading branch information
alexforencich committed Nov 7, 2023
1 parent 9fb450b commit 957f98e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 20 deletions.
13 changes: 3 additions & 10 deletions rtl/ptp_clock.v
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,6 @@ reg [15:0] drift_rate_reg = DRIFT_RATE;

reg [INC_NS_WIDTH-1:0] ts_inc_ns_reg = 0;
reg [FNS_WIDTH-1:0] ts_inc_fns_reg = 0;
reg [INC_NS_WIDTH-1:0] ts_inc_ns_delay_reg = 0;
reg [FNS_WIDTH-1:0] ts_inc_fns_delay_reg = 0;
reg [30:0] ts_inc_ns_ovf_reg = 0;
reg [FNS_WIDTH-1:0] ts_inc_fns_ovf_reg = 0;

reg [47:0] ts_96_s_reg = 0;
reg [29:0] ts_96_ns_reg = 0;
Expand Down Expand Up @@ -254,17 +250,14 @@ always @(posedge clk) begin
end

// 96 bit timestamp
{ts_inc_ns_delay_reg, ts_inc_fns_delay_reg} <= {ts_inc_ns_reg, ts_inc_fns_reg};
{ts_inc_ns_ovf_reg, ts_inc_fns_ovf_reg} <= {NS_PER_S, {FNS_WIDTH{1'b0}}} - {ts_inc_ns_reg, ts_inc_fns_reg};

{ts_96_ns_inc_reg, ts_96_fns_inc_reg} <= {ts_96_ns_inc_reg, ts_96_fns_inc_reg} + {ts_inc_ns_delay_reg, ts_inc_fns_delay_reg};
{ts_96_ns_ovf_reg, ts_96_fns_ovf_reg} <= {ts_96_ns_inc_reg, ts_96_fns_inc_reg} - {ts_inc_ns_ovf_reg, ts_inc_fns_ovf_reg};
{ts_96_ns_inc_reg, ts_96_fns_inc_reg} <= {ts_96_ns_inc_reg, ts_96_fns_inc_reg} + {ts_inc_ns_reg, ts_inc_fns_reg};
{ts_96_ns_ovf_reg, ts_96_fns_ovf_reg} <= {ts_96_ns_inc_reg, ts_96_fns_inc_reg} + (-{NS_PER_S, {FNS_WIDTH{1'b0}}} | {ts_inc_ns_reg, ts_inc_fns_reg});
{ts_96_ns_reg, ts_96_fns_reg} <= {ts_96_ns_inc_reg, ts_96_fns_inc_reg};

if (!ts_96_ns_ovf_reg[30]) begin
// if the overflow lookahead did not borrow, one second has elapsed
// increment seconds field, pre-compute normal increment, force overflow lookahead borrow bit set
{ts_96_ns_inc_reg, ts_96_fns_inc_reg} <= {ts_96_ns_ovf_reg, ts_96_fns_ovf_reg} + {ts_inc_ns_delay_reg, ts_inc_fns_delay_reg};
{ts_96_ns_inc_reg, ts_96_fns_inc_reg} <= {ts_96_ns_ovf_reg, ts_96_fns_ovf_reg} + {ts_inc_ns_reg, ts_inc_fns_reg};
ts_96_ns_ovf_reg[30] <= 1'b1;
{ts_96_ns_reg, ts_96_fns_reg} <= {ts_96_ns_ovf_reg, ts_96_fns_ovf_reg};
ts_96_s_reg <= ts_96_s_reg + 1;
Expand Down
13 changes: 3 additions & 10 deletions rtl/ptp_clock_cdc.v
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ localparam TIME_ERR_INT_WIDTH = NS_WIDTH+FNS_WIDTH;
localparam [30:0] NS_PER_S = 31'd1_000_000_000;

reg [NS_WIDTH+FNS_WIDTH-1:0] period_ns_reg = 0, period_ns_next = 0;
reg [NS_WIDTH+FNS_WIDTH-1:0] period_ns_delay_reg = 0, period_ns_delay_next = 0;
reg [31+FNS_WIDTH-1:0] period_ns_ovf_reg = 0, period_ns_ovf_next = 0;

reg [47:0] src_ts_s_capt_reg = 0;
reg [TS_NS_WIDTH+CMP_FNS_WIDTH-1:0] src_ts_ns_capt_reg = 0;
Expand Down Expand Up @@ -595,19 +593,16 @@ always @* begin
gain_sel_next = gain_sel_reg;

// PTP clock
period_ns_delay_next = period_ns_reg;
period_ns_ovf_next = {NS_PER_S, {FNS_WIDTH{1'b0}}} - period_ns_reg;

if (TS_WIDTH == 96) begin
// 96 bit timestamp
ts_ns_inc_next = ts_ns_inc_reg + period_ns_delay_reg;
ts_ns_ovf_next = ts_ns_inc_reg - period_ns_ovf_reg;
ts_ns_inc_next = ts_ns_inc_reg + period_ns_reg;
ts_ns_ovf_next = ts_ns_inc_reg + (-{NS_PER_S, {FNS_WIDTH{1'b0}}} | period_ns_reg);
ts_ns_next = ts_ns_inc_reg;

if (!ts_ns_ovf_reg[30+FNS_WIDTH]) begin
// if the overflow lookahead did not borrow, one second has elapsed
// increment seconds field, pre-compute normal increment, force overflow lookahead borrow bit set
ts_ns_inc_next = ts_ns_ovf_reg + period_ns_delay_reg;
ts_ns_inc_next = ts_ns_ovf_reg + period_ns_reg;
ts_ns_ovf_next[30+FNS_WIDTH] = 1'b1;
ts_ns_next = ts_ns_ovf_reg;
ts_s_next = ts_s_reg + 1;
Expand Down Expand Up @@ -767,8 +762,6 @@ end

always @(posedge output_clk) begin
period_ns_reg <= period_ns_next;
period_ns_delay_reg <= period_ns_delay_next;
period_ns_ovf_reg <= period_ns_ovf_next;

ts_s_reg <= ts_s_next;
ts_ns_reg <= ts_ns_next;
Expand Down

0 comments on commit 957f98e

Please sign in to comment.