Skip to content
Snippets Groups Projects
ideal_timestamper.svh 2.26 KiB
Newer Older
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
`include "timestamp.svh"

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

   input [31:0] csync_utc_i,
   input [27:0] csync_coarse_i,
   input csync_p1_i
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
   parameter g_frac_range    = 4096;
   parameter g_coarse_range  = 125000000;
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed

   const time c_frac_step   = 8ns / g_frac_range;
   
   reg [27:0] cntr_coarse;
   reg [31:0] cntr_utc;
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
   reg [12:0] cntr_frac;
   reg tag_valid_p1;
   
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
   Timestamp ts_queue[$];
   
   always@(posedge clk_ref_i)
     if(!rst_n_i)
       begin
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
	  cntr_coarse      <= 0;
	  cntr_utc         <= 0;
	  cntr_frac        <= 0;
	  tag_valid_p1     <= 0;
	  ts_queue          = '{};
       end
   
   always@(posedge clk_ref_i)
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
     cntr_frac             <= 0;

   always@(posedge clk_ref_i)
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
     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;	   
	  end else
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
	    cntr_coarse  <= cntr_coarse + 1;
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
  initial forever #(c_frac_step) cntr_frac <= cntr_frac + 1;
   
   always@(posedge trig_a_i) begin
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
      
      if(enable_i)
	begin
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
           Timestamp ts;
           ts         = new (cntr_utc, cntr_coarse, cntr_frac);
	   ts_queue.push_back(ts);
	end
   end

Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
   function int poll();
      return (ts_queue.size() > 0);
   endfunction // poll

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

/* -----\/----- EXCLUDED -----\/-----
   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
 -----/\----- EXCLUDED -----/\----- */
   
endmodule // ideal_timestamper