timestamp.svh 1.53 KB
Newer Older
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
1 2 3
`ifndef __TIMESTAMP_SVH
 `define __TIMESTAMP_SVH

4
`include "wb/simdrv_defs.svh"
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
5

6 7 8 9
class Timestamp;
   uint64_t utc;
   
   int coarse, frac, coarse_range, seq_id, source;
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
10
   
11
   function new(int _utc=0 ,int _coarse=0, int _frac=0,int _seq_id = 0, int _coarse_range = 125000000);
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
12 13 14 15 16
      utc           = _utc;
      coarse        = _coarse;
      frac          = _frac;
      coarse_range  = _coarse_range;
      seq_id        = _seq_id;
17
      source = 0;
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
18 19 20 21 22 23
   endfunction // new
   
   function real flatten();
      return real'(utc) * real'(coarse_range * 8) + real'(coarse) * 8.0 + (real'(frac)/4096.0 * 8.0);
   endfunction // flatten

24 25 26 27 28
   task from_ps(int x);
      unflatten(uint64_t'(x) * 4096 / 8000);
   endtask // from_ps
   
   
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
29 30 31 32 33 34 35
   task unflatten(int x);
      int t;
      t       =x;
      
      frac    = x % 4096;
      x       = x - frac;
      x       = x/4096;
36
      coarse  = x % coarse_range;
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
37
      x       = x - coarse;
38
      x       = x/coarse_range;
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
39 40 41 42 43
      utc     = x;
      $display("Unflat: %d %d %d %d", t, utc, coarse, frac);
      

   endtask // unflatten
44 45


Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
46 47
   

48 49 50 51 52 53 54 55 56 57 58
   function Timestamp add(Timestamp b);
      Timestamp r = new;

      r.frac = frac+b.frac;
      r.coarse = coarse + b.coarse;
      r.utc = utc + b.utc;
      if(r.frac >= 4096)
        begin
           r.frac -= 4096;
           r.coarse++;
        end
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
59

60 61 62 63 64 65 66
      if(r.coarse >= coarse_range)
        begin
           r.utc ++;
           r.coarse -= coarse_range;
        end
      return r;
      
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
67 68
   endfunction

69 70
   
   
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
71 72 73
endclass      

`endif //  `ifndef __TIMESTAMP_SVH