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

Subversion Repositories ha1588

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

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

Line No. Rev Author Line
1 34 edn_walter
/*
2
 * $rtc.v
3
 *
4
 * Copyright (c) 2012, BBY&HW. All rights reserved.
5
 *
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
  input [37:0] time_acc_modulo,  // 37: 8 ns,  7:0 ns_fraction
34
  // 3. precise time adjustment: small time difference adjustment with a time mark
35
  input adj_ld,
36
  input [31:0] adj_ld_data,
37
  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
reg  [39:0] period_fix;  // 39:32 ns, 31:0 ns_fraction
48
reg  [31:0] adj_cnt;
49
reg  [39:0] time_adj;    // 39:32 ns, 31:0 ns_fraction
50
// frequency and small time difference adjustment registers
51
always @(posedge rst or posedge clk) begin
52
  if (rst) begin
53 22 edn_walter
    period_fix <= period_fix;  //40'd0;
54 3 ash_riple
    adj_cnt    <= 32'hffffffff;
55 22 edn_walter
    time_adj   <= time_adj;    //40'd0;
56 3 ash_riple
  end
57
  else begin
58
    if (period_ld)  // load period adjustment
59
      period_fix <= period_in;
60
    else
61
      period_fix <= period_fix;
62
 
63
    if (adj_ld)  // load precise time adjustment time mark
64
      adj_cnt  <= adj_ld_data;
65
    else if (adj_cnt==32'hffffffff)
66
      adj_cnt  <= adj_cnt;  // no cycling
67
    else
68
      adj_cnt  <= adj_cnt - 1;  // counting down
69
 
70
    if (adj_cnt==0)  // change period temparorily
71
      time_adj <= period_fix + period_adj;
72
    else
73
      time_adj <= period_fix + 0;
74
  end
75
end
76
 
77 19 edn_walter
reg  [39:0] time_adj_08n_32f;  // 39:32 ns, 31:0 ns_fraction
78 3 ash_riple
wire [15:0] time_adj_08n_08f;  // 15: 8 ns,  7:0 ns_fraction
79
reg  [23:0] time_adj_00n_24f;  //           23:0 ns_fraction
80
// delta-sigma circuit to keep the lower 24bit of time_adj
81 19 edn_walter
always @(posedge rst or posedge clk) begin
82 3 ash_riple
  if (rst) begin
83 19 edn_walter
    time_adj_08n_32f <= 40'd0;
84 3 ash_riple
    time_adj_00n_24f <= 24'd0;
85
  end
86
  else begin
87 19 edn_walter
    time_adj_08n_32f <= time_adj[39: 0] + {16'd0, time_adj_00n_24f};  // add the delta
88
    time_adj_00n_24f <= time_adj_08n_32f[23: 0];                      // save the delta
89 3 ash_riple
  end
90
end
91 19 edn_walter
assign time_adj_08n_08f = time_adj_08n_32f[39:24];  // output w/o the delta
92 3 ash_riple
 
93
reg  [37:0] time_acc_30n_08f;  // 37:8 ns , 7:0 ns_fraction
94
reg  [47:0] time_acc_48s;      // 47:0 sec
95 19 edn_walter
reg         time_acc_48s_inc;
96 3 ash_riple
// time accumulator (48bit_s + 30bit_ns + 8bit_ns_fraction)
97
always @(posedge rst or posedge clk) begin
98
  if (rst) begin
99
    time_acc_30n_08f <= 38'd0;
100
    time_acc_48s     <= 48'd0;
101 19 edn_walter
    time_acc_48s_inc <=  1'b0;
102 3 ash_riple
  end
103
  else begin
104
    if (time_ld) begin  // direct write
105
      time_acc_30n_08f <= time_reg_ns_in;
106
      time_acc_48s     <= time_reg_sec_in;
107
    end
108
    else begin
109 19 edn_walter
 
110
      if (time_acc_30n_08f + {22'd0, time_adj_08n_08f} >= time_acc_modulo)
111
        time_acc_30n_08f <= time_acc_30n_08f + {22'd0, time_adj_08n_08f} - time_acc_modulo;
112
      else
113
        time_acc_30n_08f <= time_acc_30n_08f + {22'd0, time_adj_08n_08f};
114
 
115
      if (time_acc_48s_inc)
116
        time_acc_48s_inc <= 1'b0;
117 22 edn_walter
      else if (time_acc_modulo == 38'd0)
118
        time_acc_48s_inc <= 1'b0;
119 19 edn_walter
      else if (time_acc_30n_08f + {22'd0, time_adj_08n_08f} + {22'd0, time_adj_08n_08f} >= time_acc_modulo)
120
        time_acc_48s_inc <= 1'b1;
121
      else
122
        time_acc_48s_inc <= 1'b0;
123
 
124
      if (time_acc_48s_inc)
125
        time_acc_48s     <= time_acc_48s + 1;
126
      else
127
        time_acc_48s     <= time_acc_48s;
128
 
129 3 ash_riple
    end
130
  end
131
end
132
 
133
// time output (48bit_s + 30bit_ns + 8bit_ns_fraction)
134
assign time_reg_ns  = time_acc_30n_08f;
135
assign time_reg_sec = time_acc_48s;
136 32 edn_walter
// time output (48bit_s + 32bit_ns)
137
assign time_ptp_ns  = {2'b00, time_acc_30n_08f[37:8]};
138
assign time_ptp_sec = time_acc_48s;
139 3 ash_riple
 
140
endmodule

powered by: WebSVN 2.1.0

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