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

Subversion Repositories ha1588

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

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

Line No. Rev Author Line
1 3 ash_riple
`timescale 1ns/1ns
2
 
3 15 edn_walter
module rtc (
4 3 ash_riple
  input rst, clk,
5
  // 1. direct time adjustment: ToD set up
6
  input time_ld,
7
  input [37:0] time_reg_ns_in,   // 37:8 ns, 7:0 ns_fraction
8
  input [47:0] time_reg_sec_in,  // 47:0 sec
9
  // 2. frequency adjustment: frequency set up for drift compensation
10
  input period_ld,
11
  input [39:0] period_in,        // 39:32 ns, 31:0 ns_fraction
12
  input [37:0] time_acc_modulo,  // 37: 8 ns,  7:0 ns_fraction
13
  // 3. precise time adjustment: small time difference adjustment with a time mark
14
  input adj_ld,
15
  input [31:0] adj_ld_data,
16
  input [39:0] period_adj,  // 39:32 ns, 31:0 ns_fraction
17
 
18 32 edn_walter
  // time output: for internal with ns fraction
19 3 ash_riple
  output [37:0] time_reg_ns,  // 37:8 ns, 7:0 ns_fraction
20 32 edn_walter
  output [47:0] time_reg_sec, // 47:0 sec
21
  // time output: for external with ptp standard
22
  output [31:0] time_ptp_ns,  // 31:0 ns
23
  output [47:0] time_ptp_sec  // 47:0 sec
24 3 ash_riple
);
25
 
26
reg  [39:0] period_fix;  // 39:32 ns, 31:0 ns_fraction
27
reg  [31:0] adj_cnt;
28
reg  [39:0] time_adj;    // 39:32 ns, 31:0 ns_fraction
29
// frequency and small time difference adjustment registers
30
always @(posedge rst or posedge clk) begin
31
  if (rst) begin
32 22 edn_walter
    period_fix <= period_fix;  //40'd0;
33 3 ash_riple
    adj_cnt    <= 32'hffffffff;
34 22 edn_walter
    time_adj   <= time_adj;    //40'd0;
35 3 ash_riple
  end
36
  else begin
37
    if (period_ld)  // load period adjustment
38
      period_fix <= period_in;
39
    else
40
      period_fix <= period_fix;
41
 
42
    if (adj_ld)  // load precise time adjustment time mark
43
      adj_cnt  <= adj_ld_data;
44
    else if (adj_cnt==32'hffffffff)
45
      adj_cnt  <= adj_cnt;  // no cycling
46
    else
47
      adj_cnt  <= adj_cnt - 1;  // counting down
48
 
49
    if (adj_cnt==0)  // change period temparorily
50
      time_adj <= period_fix + period_adj;
51
    else
52
      time_adj <= period_fix + 0;
53
  end
54
end
55
 
56 19 edn_walter
reg  [39:0] time_adj_08n_32f;  // 39:32 ns, 31:0 ns_fraction
57 3 ash_riple
wire [15:0] time_adj_08n_08f;  // 15: 8 ns,  7:0 ns_fraction
58
reg  [23:0] time_adj_00n_24f;  //           23:0 ns_fraction
59
// delta-sigma circuit to keep the lower 24bit of time_adj
60 19 edn_walter
always @(posedge rst or posedge clk) begin
61 3 ash_riple
  if (rst) begin
62 19 edn_walter
    time_adj_08n_32f <= 40'd0;
63 3 ash_riple
    time_adj_00n_24f <= 24'd0;
64
  end
65
  else begin
66 19 edn_walter
    time_adj_08n_32f <= time_adj[39: 0] + {16'd0, time_adj_00n_24f};  // add the delta
67
    time_adj_00n_24f <= time_adj_08n_32f[23: 0];                      // save the delta
68 3 ash_riple
  end
69
end
70 19 edn_walter
assign time_adj_08n_08f = time_adj_08n_32f[39:24];  // output w/o the delta
71 3 ash_riple
 
72
reg  [37:0] time_acc_30n_08f;  // 37:8 ns , 7:0 ns_fraction
73
reg  [47:0] time_acc_48s;      // 47:0 sec
74 19 edn_walter
reg         time_acc_48s_inc;
75 3 ash_riple
// time accumulator (48bit_s + 30bit_ns + 8bit_ns_fraction)
76
always @(posedge rst or posedge clk) begin
77
  if (rst) begin
78
    time_acc_30n_08f <= 38'd0;
79
    time_acc_48s     <= 48'd0;
80 19 edn_walter
    time_acc_48s_inc <=  1'b0;
81 3 ash_riple
  end
82
  else begin
83
    if (time_ld) begin  // direct write
84
      time_acc_30n_08f <= time_reg_ns_in;
85
      time_acc_48s     <= time_reg_sec_in;
86
    end
87
    else begin
88 19 edn_walter
 
89
      if (time_acc_30n_08f + {22'd0, time_adj_08n_08f} >= time_acc_modulo)
90
        time_acc_30n_08f <= time_acc_30n_08f + {22'd0, time_adj_08n_08f} - time_acc_modulo;
91
      else
92
        time_acc_30n_08f <= time_acc_30n_08f + {22'd0, time_adj_08n_08f};
93
 
94
      if (time_acc_48s_inc)
95
        time_acc_48s_inc <= 1'b0;
96 22 edn_walter
      else if (time_acc_modulo == 38'd0)
97
        time_acc_48s_inc <= 1'b0;
98 19 edn_walter
      else if (time_acc_30n_08f + {22'd0, time_adj_08n_08f} + {22'd0, time_adj_08n_08f} >= time_acc_modulo)
99
        time_acc_48s_inc <= 1'b1;
100
      else
101
        time_acc_48s_inc <= 1'b0;
102
 
103
      if (time_acc_48s_inc)
104
        time_acc_48s     <= time_acc_48s + 1;
105
      else
106
        time_acc_48s     <= time_acc_48s;
107
 
108 3 ash_riple
    end
109
  end
110
end
111
 
112
// time output (48bit_s + 30bit_ns + 8bit_ns_fraction)
113
assign time_reg_ns  = time_acc_30n_08f;
114
assign time_reg_sec = time_acc_48s;
115 32 edn_walter
// time output (48bit_s + 32bit_ns)
116
assign time_ptp_ns  = {2'b00, time_acc_30n_08f[37:8]};
117
assign time_ptp_sec = time_acc_48s;
118 3 ash_riple
 
119
endmodule

powered by: WebSVN 2.1.0

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