OpenCores
URL https://opencores.org/ocsvn/ha1588/ha1588/trunk

Subversion Repositories ha1588

[/] [ha1588/] [trunk/] [rtl/] [rtc/] [rtc.v] - Blame information for rev 38

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
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
  input time_ld,
28
  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
  input period_ld,
32
  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
  input adj_ld,
35
  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
reg  [37:0] time_acc_30n_08f;  // 37:8 ns , 7:0 ns_fraction
102
reg  [47:0] time_acc_48s;      // 47:0 sec
103 19 edn_walter
reg         time_acc_48s_inc;
104 3 ash_riple
// time accumulator (48bit_s + 30bit_ns + 8bit_ns_fraction)
105
always @(posedge rst or posedge clk) begin
106
  if (rst) begin
107
    time_acc_30n_08f <= 38'd0;
108
    time_acc_48s     <= 48'd0;
109 19 edn_walter
    time_acc_48s_inc <=  1'b0;
110 3 ash_riple
  end
111
  else begin
112
    if (time_ld) begin  // direct write
113
      time_acc_30n_08f <= time_reg_ns_in;
114
      time_acc_48s     <= time_reg_sec_in;
115
    end
116
    else begin
117 19 edn_walter
 
118
      if (time_acc_30n_08f + {22'd0, time_adj_08n_08f} >= time_acc_modulo)
119
        time_acc_30n_08f <= time_acc_30n_08f + {22'd0, time_adj_08n_08f} - time_acc_modulo;
120
      else
121
        time_acc_30n_08f <= time_acc_30n_08f + {22'd0, time_adj_08n_08f};
122
 
123
      if (time_acc_48s_inc)
124
        time_acc_48s_inc <= 1'b0;
125 38 edn_walter
      else if (time_acc_30n_08f + {22'd0, time_adj_08n_08f} + {22'd0, time_adj_08n_08f} >= time_acc_modulo)  // TODO: period_adj
126 19 edn_walter
        time_acc_48s_inc <= 1'b1;
127
      else
128
        time_acc_48s_inc <= 1'b0;
129
 
130
      if (time_acc_48s_inc)
131
        time_acc_48s     <= time_acc_48s + 1;
132
      else
133
        time_acc_48s     <= time_acc_48s;
134
 
135 3 ash_riple
    end
136
  end
137
end
138
 
139
// time output (48bit_s + 30bit_ns + 8bit_ns_fraction)
140
assign time_reg_ns  = time_acc_30n_08f;
141
assign time_reg_sec = time_acc_48s;
142 32 edn_walter
// time output (48bit_s + 32bit_ns)
143
assign time_ptp_ns  = {2'b00, time_acc_30n_08f[37:8]};
144
assign time_ptp_sec = time_acc_48s;
145 3 ash_riple
 
146
endmodule

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.