| 1 |
34 |
edn_walter |
/*
|
| 2 |
38 |
edn_walter |
* rtc.v
|
| 3 |
34 |
edn_walter |
*
|
| 4 |
37 |
edn_walter |
* Copyright (c) 2012, BABY&HW. All rights reserved.
|
| 5 |
34 |
edn_walter |
*
|
| 6 |
|
|
* This library is free software; you can redistribute it and/or
|
| 7 |
|
|
* modify it under the terms of the GNU Lesser General Public
|
| 8 |
|
|
* License as published by the Free Software Foundation; either
|
| 9 |
|
|
* version 2.1 of the License, or (at your option) any later version.
|
| 10 |
|
|
*
|
| 11 |
|
|
* This library is distributed in the hope that it will be useful,
|
| 12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
| 14 |
|
|
* Lesser General Public License for more details.
|
| 15 |
|
|
*
|
| 16 |
|
|
* You should have received a copy of the GNU Lesser General Public
|
| 17 |
|
|
* License along with this library; if not, write to the Free Software
|
| 18 |
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
| 19 |
|
|
* MA 02110-1301 USA
|
| 20 |
|
|
*/
|
| 21 |
|
|
|
| 22 |
3 |
ash_riple |
`timescale 1ns/1ns
|
| 23 |
|
|
|
| 24 |
15 |
edn_walter |
module rtc (
|
| 25 |
3 |
ash_riple |
input rst, clk,
|
| 26 |
|
|
// 1. direct time adjustment: ToD set up
|
| 27 |
39 |
edn_walter |
input time_ld,
|
| 28 |
3 |
ash_riple |
input [37:0] time_reg_ns_in, // 37:8 ns, 7:0 ns_fraction
|
| 29 |
|
|
input [47:0] time_reg_sec_in, // 47:0 sec
|
| 30 |
|
|
// 2. frequency adjustment: frequency set up for drift compensation
|
| 31 |
39 |
edn_walter |
input period_ld,
|
| 32 |
3 |
ash_riple |
input [39:0] period_in, // 39:32 ns, 31:0 ns_fraction
|
| 33 |
|
|
// 3. precise time adjustment: small time difference adjustment with a time mark
|
| 34 |
39 |
edn_walter |
input adj_ld,
|
| 35 |
3 |
ash_riple |
input [31:0] adj_ld_data,
|
| 36 |
38 |
edn_walter |
output reg adj_ld_done,
|
| 37 |
3 |
ash_riple |
input [39:0] period_adj, // 39:32 ns, 31:0 ns_fraction
|
| 38 |
|
|
|
| 39 |
32 |
edn_walter |
// time output: for internal with ns fraction
|
| 40 |
3 |
ash_riple |
output [37:0] time_reg_ns, // 37:8 ns, 7:0 ns_fraction
|
| 41 |
32 |
edn_walter |
output [47:0] time_reg_sec, // 47:0 sec
|
| 42 |
|
|
// time output: for external with ptp standard
|
| 43 |
|
|
output [31:0] time_ptp_ns, // 31:0 ns
|
| 44 |
|
|
output [47:0] time_ptp_sec // 47:0 sec
|
| 45 |
3 |
ash_riple |
);
|
| 46 |
|
|
|
| 47 |
38 |
edn_walter |
parameter time_acc_modulo = 38'd256000000000;
|
| 48 |
|
|
|
| 49 |
3 |
ash_riple |
reg [39:0] period_fix; // 39:32 ns, 31:0 ns_fraction
|
| 50 |
|
|
reg [31:0] adj_cnt;
|
| 51 |
|
|
reg [39:0] time_adj; // 39:32 ns, 31:0 ns_fraction
|
| 52 |
|
|
// frequency and small time difference adjustment registers
|
| 53 |
|
|
always @(posedge rst or posedge clk) begin
|
| 54 |
|
|
if (rst) begin
|
| 55 |
38 |
edn_walter |
period_fix <= period_fix; //40'd0;
|
| 56 |
|
|
adj_cnt <= 32'hffffffff;
|
| 57 |
|
|
time_adj <= time_adj; //40'd0;
|
| 58 |
|
|
adj_ld_done <= 1'b0;
|
| 59 |
3 |
ash_riple |
end
|
| 60 |
|
|
else begin
|
| 61 |
|
|
if (period_ld) // load period adjustment
|
| 62 |
|
|
period_fix <= period_in;
|
| 63 |
|
|
else
|
| 64 |
|
|
period_fix <= period_fix;
|
| 65 |
|
|
|
| 66 |
|
|
if (adj_ld) // load precise time adjustment time mark
|
| 67 |
|
|
adj_cnt <= adj_ld_data;
|
| 68 |
|
|
else if (adj_cnt==32'hffffffff)
|
| 69 |
|
|
adj_cnt <= adj_cnt; // no cycling
|
| 70 |
|
|
else
|
| 71 |
|
|
adj_cnt <= adj_cnt - 1; // counting down
|
| 72 |
|
|
|
| 73 |
|
|
if (adj_cnt==0) // change period temparorily
|
| 74 |
|
|
time_adj <= period_fix + period_adj;
|
| 75 |
|
|
else
|
| 76 |
|
|
time_adj <= period_fix + 0;
|
| 77 |
38 |
edn_walter |
|
| 78 |
|
|
if (adj_cnt==32'hffffffff)
|
| 79 |
|
|
adj_ld_done <= 1'b1;
|
| 80 |
|
|
else
|
| 81 |
|
|
adj_ld_done <= 1'b0;
|
| 82 |
3 |
ash_riple |
end
|
| 83 |
|
|
end
|
| 84 |
|
|
|
| 85 |
19 |
edn_walter |
reg [39:0] time_adj_08n_32f; // 39:32 ns, 31:0 ns_fraction
|
| 86 |
3 |
ash_riple |
wire [15:0] time_adj_08n_08f; // 15: 8 ns, 7:0 ns_fraction
|
| 87 |
|
|
reg [23:0] time_adj_00n_24f; // 23:0 ns_fraction
|
| 88 |
|
|
// delta-sigma circuit to keep the lower 24bit of time_adj
|
| 89 |
19 |
edn_walter |
always @(posedge rst or posedge clk) begin
|
| 90 |
3 |
ash_riple |
if (rst) begin
|
| 91 |
19 |
edn_walter |
time_adj_08n_32f <= 40'd0;
|
| 92 |
3 |
ash_riple |
time_adj_00n_24f <= 24'd0;
|
| 93 |
|
|
end
|
| 94 |
|
|
else begin
|
| 95 |
19 |
edn_walter |
time_adj_08n_32f <= time_adj[39: 0] + {16'd0, time_adj_00n_24f}; // add the delta
|
| 96 |
|
|
time_adj_00n_24f <= time_adj_08n_32f[23: 0]; // save the delta
|
| 97 |
3 |
ash_riple |
end
|
| 98 |
|
|
end
|
| 99 |
19 |
edn_walter |
assign time_adj_08n_08f = time_adj_08n_32f[39:24]; // output w/o the delta
|
| 100 |
3 |
ash_riple |
|
| 101 |
41 |
edn_walter |
reg [37:0] time_acc_30n_08f_pre_pos; // 37:8 ns , 7:0 ns_fraction
|
| 102 |
|
|
reg [37:0] time_acc_30n_08f_pre_neg; // 37:8 ns , 7:0 ns_fraction
|
| 103 |
|
|
wire time_acc_48s_inc = (time_acc_30n_08f_pre_pos >= time_acc_modulo)? 1'b1: 1'b0;
|
| 104 |
|
|
// time accumulator pre adder (48bit_s + 30bit_ns + 8bit_ns_fraction)
|
| 105 |
|
|
always @(posedge rst or posedge clk) begin
|
| 106 |
|
|
if (rst) begin
|
| 107 |
45 |
edn_walter |
time_acc_30n_08f_pre_pos <= 38'd0;
|
| 108 |
|
|
time_acc_30n_08f_pre_neg <= 38'd0;
|
| 109 |
41 |
edn_walter |
end
|
| 110 |
|
|
else begin
|
| 111 |
|
|
if (time_ld) begin // direct write
|
| 112 |
|
|
time_acc_30n_08f_pre_pos <= time_reg_ns_in + {22'd0, time_adj_08n_08f};
|
| 113 |
|
|
time_acc_30n_08f_pre_neg <= time_reg_ns_in + {22'd0, time_adj_08n_08f};
|
| 114 |
|
|
end
|
| 115 |
|
|
else begin
|
| 116 |
|
|
if (time_acc_48s_inc) begin
|
| 117 |
|
|
time_acc_30n_08f_pre_pos <= time_acc_30n_08f_pre_neg + {22'd0, time_adj_08n_08f};
|
| 118 |
|
|
time_acc_30n_08f_pre_neg <= time_acc_30n_08f_pre_neg + {22'd0, time_adj_08n_08f} - time_acc_modulo;
|
| 119 |
|
|
end
|
| 120 |
|
|
else begin
|
| 121 |
|
|
time_acc_30n_08f_pre_pos <= time_acc_30n_08f_pre_pos + {22'd0, time_adj_08n_08f};
|
| 122 |
|
|
time_acc_30n_08f_pre_neg <= time_acc_30n_08f_pre_pos + {22'd0, time_adj_08n_08f} - time_acc_modulo;
|
| 123 |
|
|
end
|
| 124 |
|
|
end
|
| 125 |
|
|
end
|
| 126 |
|
|
end
|
| 127 |
|
|
|
| 128 |
|
|
reg [37:0] time_acc_30n_08f; // 37:8 ns , 7:0 ns_fraction
|
| 129 |
|
|
reg [47:0] time_acc_48s; // 47:0 sec
|
| 130 |
3 |
ash_riple |
// time accumulator (48bit_s + 30bit_ns + 8bit_ns_fraction)
|
| 131 |
|
|
always @(posedge rst or posedge clk) begin
|
| 132 |
|
|
if (rst) begin
|
| 133 |
|
|
time_acc_30n_08f <= 38'd0;
|
| 134 |
|
|
time_acc_48s <= 48'd0;
|
| 135 |
|
|
end
|
| 136 |
|
|
else begin
|
| 137 |
|
|
if (time_ld) begin // direct write
|
| 138 |
|
|
time_acc_30n_08f <= time_reg_ns_in;
|
| 139 |
|
|
time_acc_48s <= time_reg_sec_in;
|
| 140 |
|
|
end
|
| 141 |
|
|
else begin
|
| 142 |
19 |
edn_walter |
|
| 143 |
|
|
if (time_acc_48s_inc)
|
| 144 |
41 |
edn_walter |
time_acc_30n_08f <= time_acc_30n_08f_pre_neg;
|
| 145 |
19 |
edn_walter |
else
|
| 146 |
41 |
edn_walter |
time_acc_30n_08f <= time_acc_30n_08f_pre_pos;
|
| 147 |
19 |
edn_walter |
|
| 148 |
|
|
if (time_acc_48s_inc)
|
| 149 |
|
|
time_acc_48s <= time_acc_48s + 1;
|
| 150 |
|
|
else
|
| 151 |
|
|
time_acc_48s <= time_acc_48s;
|
| 152 |
|
|
|
| 153 |
3 |
ash_riple |
end
|
| 154 |
|
|
end
|
| 155 |
|
|
end
|
| 156 |
|
|
|
| 157 |
|
|
// time output (48bit_s + 30bit_ns + 8bit_ns_fraction)
|
| 158 |
|
|
assign time_reg_ns = time_acc_30n_08f;
|
| 159 |
|
|
assign time_reg_sec = time_acc_48s;
|
| 160 |
32 |
edn_walter |
// time output (48bit_s + 32bit_ns)
|
| 161 |
|
|
assign time_ptp_ns = {2'b00, time_acc_30n_08f[37:8]};
|
| 162 |
|
|
assign time_ptp_sec = time_acc_48s;
|
| 163 |
3 |
ash_riple |
|
| 164 |
|
|
endmodule
|