ideal_timestamper.svh 2.26 KB
Newer Older
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
1 2
`include "timestamp.svh"

3 4 5 6 7 8 9
module ideal_timestamper
  (
   input clk_ref_i,
   input rst_n_i,
   input enable_i,

   input trig_a_i,
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
10 11 12 13

   input [31:0] csync_utc_i,
   input [27:0] csync_coarse_i,
   input csync_p1_i
14 15
   );

Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
16
   parameter g_frac_range    = 4096;
17
   parameter g_coarse_range  = 125000000;
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
18 19

   const time c_frac_step   = 8ns / g_frac_range;
20 21 22
   
   reg [27:0] cntr_coarse;
   reg [31:0] cntr_utc;
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
23
   reg [12:0] cntr_frac;
24 25
   reg tag_valid_p1;
   
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
26
   Timestamp ts_queue[$];
27 28 29 30
   
   always@(posedge clk_ref_i)
     if(!rst_n_i)
       begin
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
31 32 33 34 35
	  cntr_coarse      <= 0;
	  cntr_utc         <= 0;
	  cntr_frac        <= 0;
	  tag_valid_p1     <= 0;
	  ts_queue          = '{};
36 37 38
       end
   
   always@(posedge clk_ref_i)
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
39
     cntr_frac             <= 0;
40 41

   always@(posedge clk_ref_i)
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55
     if(!rst_n_i) begin
        cntr_coarse        <= 0;
        cntr_utc           <= 0;
     end else begin
        if(csync_p1_i)
          begin
             cntr_coarse <= csync_coarse_i+1;
             cntr_utc    <= csync_utc_i;
          end else if(cntr_coarse == g_coarse_range) begin
	     cntr_coarse <= 1;
	     cntr_utc    <= cntr_utc + 1;	   
          end else if(cntr_coarse == g_coarse_range - 1) begin
	     cntr_coarse <= 0;
	     cntr_utc    <= cntr_utc + 1;	   
56
	  end else
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
57
	    cntr_coarse  <= cntr_coarse + 1;
58 59
       end

Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
60
  initial forever #(c_frac_step) cntr_frac <= cntr_frac + 1;
61 62
   
   always@(posedge trig_a_i) begin
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
63
      
64 65
      if(enable_i)
	begin
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
66 67
           Timestamp ts;
           ts         = new (cntr_utc, cntr_coarse, cntr_frac);
68 69 70 71
	   ts_queue.push_back(ts);
	end
   end

Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
72 73 74 75 76 77 78 79 80 81
   function int poll();
      return (ts_queue.size() > 0);
   endfunction // poll

   function Timestamp get();
      return ts_queue.pop_front();
   endfunction // get
     

/* -----\/----- EXCLUDED -----\/-----
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
   always@(posedge clk_ref_i)
     if(tag_valid_p1)
       tag_valid_p1 <= 0;
     else if(ts_queue.size() > 0)
       begin
	  timestamp_t ts;
	  ts 		  = ts_queue.pop_front();
	  tag_frac_o 	 <= ts.frac;
	  tag_utc_o 	 <= ts.utc;
	  tag_coarse_o 	 <= ts.coarse;
	  tag_valid_p1 	 <= 1'b1;
       end
       

   assign tag_valid_p1_o = tag_valid_p1;
   assign cntr_coarse_o   = cntr_coarse;
   assign cntr_utc_o 	  = cntr_utc;
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
99
 -----/\----- EXCLUDED -----/\----- */
100 101
   
endmodule // ideal_timestamper