From db881ed5511810b174e1e591af73531fb4c3c511 Mon Sep 17 00:00:00 2001 From: Alex Forencich Date: Fri, 22 Jul 2022 18:39:21 -0700 Subject: [PATCH] Remove magic numbers Signed-off-by: Alex Forencich --- rtl/ptp_clock.v | 6 ++++-- rtl/ptp_clock_cdc.v | 14 ++++++++------ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/rtl/ptp_clock.v b/rtl/ptp_clock.v index ca646c14d..ce3bf6927 100644 --- a/rtl/ptp_clock.v +++ b/rtl/ptp_clock.v @@ -96,6 +96,8 @@ module ptp_clock # parameter INC_NS_WIDTH = $clog2(2**PERIOD_NS_WIDTH + 2**OFFSET_NS_WIDTH + 2**DRIFT_NS_WIDTH); +localparam [30:0] NS_PER_S = 31'd1_000_000_000; + reg [PERIOD_NS_WIDTH-1:0] period_ns_reg = PERIOD_NS; reg [FNS_WIDTH-1:0] period_fns_reg = PERIOD_FNS; @@ -252,13 +254,13 @@ always @(posedge clk) begin // if the overflow lookahead did not borrow, one second has elapsed // increment seconds field, pre-compute both normal increment and overflow values {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, ts_96_fns_ovf_reg} <= {ts_96_ns_ovf_reg, ts_96_fns_ovf_reg} + {ts_inc_ns_reg, ts_inc_fns_reg} - {31'd1_000_000_000, {FNS_WIDTH{1'b0}}}; + {ts_96_ns_ovf_reg, ts_96_fns_ovf_reg} <= {ts_96_ns_ovf_reg, ts_96_fns_ovf_reg} + {ts_inc_ns_reg, ts_inc_fns_reg} - {NS_PER_S, {FNS_WIDTH{1'b0}}}; {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; end else begin // no increment seconds field, pre-compute both normal increment and overflow values {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} + {ts_inc_ns_reg, ts_inc_fns_reg} - {31'd1_000_000_000, {FNS_WIDTH{1'b0}}}; + {ts_96_ns_ovf_reg, ts_96_fns_ovf_reg} <= {ts_96_ns_inc_reg, ts_96_fns_inc_reg} + {ts_inc_ns_reg, ts_inc_fns_reg} - {NS_PER_S, {FNS_WIDTH{1'b0}}}; {ts_96_ns_reg, ts_96_fns_reg} <= {ts_96_ns_inc_reg, ts_96_fns_inc_reg}; ts_96_s_reg <= ts_96_s_reg; end diff --git a/rtl/ptp_clock_cdc.v b/rtl/ptp_clock_cdc.v index 16141542c..cd7c5174b 100644 --- a/rtl/ptp_clock_cdc.v +++ b/rtl/ptp_clock_cdc.v @@ -89,6 +89,8 @@ parameter SAMPLE_ACC_WIDTH = LOG_SAMPLE_SYNC_RATE+2; parameter DEST_SYNC_LOCK_WIDTH = 7; parameter PTP_LOCK_WIDTH = 8; +localparam [30:0] NS_PER_S = 31'd1_000_000_000; + reg [NS_WIDTH-1:0] period_ns_reg = 0, period_ns_next; reg [FNS_WIDTH-1:0] period_fns_reg = 0, period_fns_next; @@ -574,13 +576,13 @@ always @* begin // if the overflow lookahead did not borrow, one second has elapsed // increment seconds field, pre-compute both normal increment and overflow values {ts_ns_inc_next, ts_fns_inc_next} = {ts_ns_ovf_reg, ts_fns_ovf_reg} + {period_ns_reg, period_fns_reg}; - {ts_ns_ovf_next, ts_fns_ovf_next} = {ts_ns_ovf_reg, ts_fns_ovf_reg} + {period_ns_reg, period_fns_reg} - {31'd1_000_000_000, {FNS_WIDTH{1'b0}}}; + {ts_ns_ovf_next, ts_fns_ovf_next} = {ts_ns_ovf_reg, ts_fns_ovf_reg} + {period_ns_reg, period_fns_reg} - {NS_PER_S, {FNS_WIDTH{1'b0}}}; {ts_ns_next, ts_fns_next} = {ts_ns_ovf_reg, ts_fns_ovf_reg}; ts_s_next = ts_s_next + 1; end else begin // no increment seconds field, pre-compute both normal increment and overflow values {ts_ns_inc_next, ts_fns_inc_next} = {ts_ns_inc_reg, ts_fns_inc_reg} + {period_ns_reg, period_fns_reg}; - {ts_ns_ovf_next, ts_fns_ovf_next} = {ts_ns_inc_reg, ts_fns_inc_reg} + {period_ns_reg, period_fns_reg} - {31'd1_000_000_000, {FNS_WIDTH{1'b0}}}; + {ts_ns_ovf_next, ts_fns_ovf_next} = {ts_ns_inc_reg, ts_fns_inc_reg} + {period_ns_reg, period_fns_reg} - {NS_PER_S, {FNS_WIDTH{1'b0}}}; {ts_ns_next, ts_fns_next} = {ts_ns_inc_reg, ts_fns_inc_reg}; ts_s_next = ts_s_next; end @@ -639,14 +641,14 @@ always @* begin ts_ns_diff_corr_next = ts_ns_diff_reg[16:0]; ts_fns_diff_corr_next = ts_fns_diff_reg; diff_corr_valid_next = 1'b1; - end else if ($signed(ts_s_diff_reg) == 1 && ts_ns_diff_reg[30:16] == 15'h4465) begin + end else if ($signed(ts_s_diff_reg) == 1 && ts_ns_diff_reg[30:16] == ~NS_PER_S[30:16]) begin // difference is small with 1 second difference; adjust and slew - ts_ns_diff_corr_next = ts_ns_diff_reg[16:0] + 17'h0ca00; + ts_ns_diff_corr_next = ts_ns_diff_reg[16:0] + NS_PER_S[16:0]; ts_fns_diff_corr_next = ts_fns_diff_reg; diff_corr_valid_next = 1'b1; - end else if ($signed(ts_s_diff_reg) == -1 && ts_ns_diff_reg[30:16] == 15'h3b9a) begin + end else if ($signed(ts_s_diff_reg) == -1 && ts_ns_diff_reg[30:16] == NS_PER_S[30:16]) begin // difference is small with 1 second difference; adjust and slew - ts_ns_diff_corr_next = ts_ns_diff_reg[16:0] - 17'h0ca00; + ts_ns_diff_corr_next = ts_ns_diff_reg[16:0] - NS_PER_S[16:0]; ts_fns_diff_corr_next = ts_fns_diff_reg; diff_corr_valid_next = 1'b1; end else begin