The Designer's Guide Community Forum
https://designers-guide.org/forum/YaBB.pl
Design Languages >> Verilog-AMS >> Detecting Minimum and maximum voltage
https://designers-guide.org/forum/YaBB.pl?num=1476553251

Message started by Dan M on Oct 15th, 2016, 10:40am

Title: Detecting Minimum and maximum voltage
Post by Dan M on Oct 15th, 2016, 10:40am

I have the following VerilogA code and it's not working so well

module sample (in);

input in;
electrical in;

real minv, maxv;

analog begin
  @(initial_step) begin minv=V(in)-0.01; maxv=V(in)+0.01; end
  @(cross(V(in)-minv,-1)) minv=V(in)-0.01;
  @(cross(V(in)-maxv,1)) maxv=V(in)+0.01;
  @(timer(1n,1n)) begin $display ("%f %f",minv,maxv); minv=V(in)-0.01; maxv=V(in)+0.01; end
end
endmodule

So effectively I'm trying to find the minimum and maximum voltage on 'in' over 1 nanosecond blocks of time.  I'm putting in the '0.01' offsets to try to provide some sort of "hysteresis" so it's not constantly triggering the @cross, with the understanding that it obviously throws in up 10mV of error.

What I'm seeing is that the @crosss statements aren't triggering consistently.  If I force a lot of time steps with an @(timer(100f,100f)) it seems to work better, but that's a horrible solution.  If I increase time or voltage tolerance in the @cross it seems to work better, but not much, and if I add to hysteresis it works a lot better, but to get good results I need to change the 10mV to well over 100mV, and even then it's not perfect, and I'm not comfortable with the lack of accuracy.  The problem seems to come when there is a very high frequency signal (i.e. on input edges).

If anyone has some thoughts as to how to maek this work better, I'd definitely appreciate it.

Thanks!

Title: Re: Detecting Minimum and maximum voltage
Post by Ken Kundert on Oct 16th, 2016, 1:52pm

I recommend simply getting rid of the cross events. And of course, you need to add if statements so you only overwrite minv and maxv if the input is small or larger than the saved value.


Code:
module sample (in);
input in;
electrical in;
real minv, maxv;
analog begin
   @(initial_step) begin
       minv=V(in);
       maxv=V(in);
   end
   if (V(in) < minv)
       minv = V(in);
   if (V(in) > maxv)
       maxv = V(in);
   @(timer(1n,1n)) begin
       $display ("minimum = %rV, maximum = %rV", minv, maxv);
       minv=V(in);
       maxv=V(in);
   end
end
endmodule


-Ken

Title: Re: Detecting Minimum and maximum voltage
Post by Dan M on Oct 17th, 2016, 4:57am

Any particular reason why the @cross is causing problems?

Title: Re: Detecting Minimum and maximum voltage
Post by Ken Kundert on Oct 19th, 2016, 9:03pm

Fundamentally cross functions are used to model thresholds in your circuit. But what you are trying to model is essentially a peak detector. No thresholds involved.  Instead you were trying to use cross functions to detect when the signal had changed by some small amount. Doing so is inherently inefficient.

-Ken

Title: Re: Detecting Minimum and maximum voltage
Post by Geoffrey_Coram on Oct 26th, 2016, 8:13am

The cross event is trying to resolve precisely when the crossing occurred, and the simulator may reject timepoints that are computed too far past the crossing time.  But you don't actually care when the crossing occurred.

The Designer's Guide Community Forum » Powered by YaBB 2.2.2!
YaBB © 2000-2008. All Rights Reserved.