Your code was unreadable and I know it is unlikely that you will get many responses if people cannot easily understand your code, so I took the liberty of reformatting it to make it easier to understand:
Code:module measure_riset_fallt (drive_en, pad);
input pad;
input drive_en;
parameter real padh = 1.05;
real time_vhi, time_vlo, rise_time, fall_time;
analog begin
@(cross(V(pad) - padh*0.3, 1)) begin
time_vlo = $abstime;
end
@(cross(V(pad) - padh*0.7, 1)) begin
time_vhi =$abstime;
// For rise time . Calculate between 70% and 30% of the peak Voltage
rise_time=time_vhi-time_vlo;
$display("Rise Time= %e",rise_time);
end
if (drive_en==1'b1) begin
@(cross(V(pad) - padh*0.7, -1)) begin
time_vhi= $abstime;
end
@(cross(V(pad) - padh*0.3, -1)) begin
time_vlo=$abstime;
fall_time = time_vhi - time_vlo;
$display("Fall Time= %e",fall_time);
end
end
end
endmodule
First a few generic comments:
- One problem with this code is that you put cross functions inside an if statement. That is a no-no. And it seems unnecessary. After all, you did not put your rise time code inside an if statement.
- You did not specify tolerances on your cross functions. If your signals are small, your variation may simply be due to loose tolerances.
- To get better accuracy, you might want to use last_crossing (see "Time interval measurement" on verilogams.com).
None of this explains the problem you are having, which seems to be due to the fact that your vlo rising edge threshold detection simply fails to operate on every fourth edge. The code looks okay to me, so I suspect the problem is elsewhere.
What simulator is this?
-Ken