// /////////////////////////////////////////////
// Cycle-to-cycle jitter meter             /////
// Fuding Ge  fudingge@yahoo.com             ///
// Copyright by Fuding Ge, All right reserved //
////////////////////////////////////////////////

// This verilogA model output the rms cycle-to-cycle jitter of the input
// clock signal. The definition of the cycle-to-cycle jitter is the rms
// variation in itd period.

// JitCTC=sqrt((sum(ti-tavg)**2)/M) where M is the number of periods of test.
// M should be as large as possible. ti is the instantaneous and tavg
// is the average of the periods: tavg=(t1+t2+...+tM)/M

// In this model we use the following equation:
////////////////////////////////////////////////
// JitCTC=sqrt((sum(ti)**2)/M-(tavg)**2)     ///
////////////////////////////////////////////////

// You need to change the value of hlfvcc to match the power supply
// of the clock signal. In this version vcc=2.5 and hlfvcc=1.25

`include "constants.h"
`include "discipline.h"

nature  Time
	abstol = 1e-25;
	access = TT;
	units = "ps";
	blowup = 1.0e10;
endnature

nature  Number
        abstol = 1e-3;
        access = NUM;
        units = "";
        blowup = 1.0e200;
endnature

discipline time_current
	potential Time;
        flow Current;
enddiscipline
   
discipline number_current
        potential Number;
        flow Current;
enddiscipline


////////////////////////////////////////////////////////////

module Jittermeter(vin,jitter,num_period); 
input vin;
output jitter,num_period;

electrical vin;
time_current jitter;
number_current num_period;


   real tlatest;
   real tearly;
   real period_early, period_latest;

   real period_square;
   real period_square_sum;
   real period_square_avg;

   real period_total;
   real period_avg;
   real period_avg_square;
 
   real jitter_val;
   real hlfvcc;
   integer counter;
   integer counter_begin;

////////////////////////////////////   
   analog begin

      ///////   initialize the parameters///////////////	
      @ ( initial_step ) begin 
        tearly=0.0;
        tlatest=0.0; 
        hlfvcc=1.25;          //Change your hlfvcc here !
	counter =0;
	counter_begin=20000;
        period_early=0.0;
	period_latest=0.0;   

        period_square=0.0;
        period_square_sum=0.0;
        period_square_avg=0.0;

        period_total=0.0;
        period_avg=0.0;
        period_avg_square=0.0; 
      end


///////////////////////////////////////////////////////////////////
      @ ( cross ((V(vin)-hlfvcc),+1 )) begin
         tlatest = $realtime*1e12;
	 period_latest  = tlatest-tearly; //Current period value
         tearly = tlatest;
         period_early = period_latest;
         counter = counter +1;

if (counter >= counter_begin+2) begin

	 period_square = period_latest*period_latest;
         period_square_sum = period_square_sum + period_square; 
         period_square_avg =  period_square_sum/(counter-counter_begin-1);

         period_total = period_total + period_latest;
         period_avg = period_total/(counter-counter_begin-1);
	 period_avg_square =  period_avg*period_avg;

         jitter_val = period_square_avg - period_avg_square;   
end  
end
         /////////////////////////////////////////
     
      //// output jitter value and number of measure periods /////   
         if  (counter-counter_begin < 10000) 
            begin 
           	 TT(jitter) <+ 0;
        	 NUM(num_period) <+ 0;
            end  	         
	 else  
            begin
         	TT(jitter) <+ sqrt(jitter_val);        
                NUM(num_period) <+ counter-counter_begin;
            end 
    
    end
endmodule








    Source: geocities.com/fudinggepll/model

               ( geocities.com/fudinggepll)