TOC PREV NEXT INDEX

Put your logo here!


Section 10
System tasks and functions
This section describes system tasks and functions available in Verilog-AMS HDL.
10.1 Environment parameter functions
The syntax for these functions are shown in Syntax 101.

Syntax 10-1- Syntax for the environment parameter functions
These functions return information about the current environment parameters as a real value.
$temperature does not take any input arguments and returns the circuit's ambient temperature in Kelvin units.
$abstime returns the absolute time, that is a real value number representing time in seconds.
$realtime can have an optional argument which scales the time. If no argument is given, $realtime's return value is scaled to the `time_unit of the module which invoked it. If an argument is given, $realtime shall divide the absolute time by the value of the argument (i.e., scale to the value specified in the argument). The argument for $realtime follows the symantics of the `time_unit, that is it shall consist of an integer followed by a scale factor. Valid integers are: 1, 10, and 100; valid scale factors are: s (seconds), ms (milliseconds), us (microseconds), ns (nanoseconds), ps (picoseconds), and fs (femtoseconds).
$vt can optionally have temperature (in Kelvin units) as an input argument and returns the thermal voltage (kT/q) at the given temperature. $vt without the optional input temperature argument returns the thermal voltage using $temperature.
Note: Previous Verilog-A modules using $realtime may not produce the same results as in the past as $realtime used to return time scaled to one (1) second. If the time_unit of the `timescale directive is set to 1s, the behavior shall be the same. For analog blocks, the $abstime function should typically be used, as it returns time in seconds.
10.2 $random function
The syntax for this function is shown in Syntax 102.

Syntax 10-2- Syntax for the random_function
$random provides a mechanism for generating random numbers. The function returns a new 32-bit random number each time it is called. The random number is a signed integer; it can be positive or negative.
seed_expression controls the numbers $random returns. seed shall be a reg, integer, or time variable. The seed value shall be assigned to this variable prior to calling $random.

Examples:
Where b > 0, the expression ($random % b) gives a number in the following range:
[(-b+1) : (b-1)].
The following code fragment shows an example of random number generation between -59 and 59:
integer rand;
rand = $random % 60;
10.3 $dist_ functions
The syntax for these functions are shown in Syntax 103.

Syntax 10-3- Syntax for the probabilistic distribution functions
The following rules apply to these functions.
· All parameters to the system functions are real values, except for seed (which is defined by $random()). For the $rdist_exponential, $rdist_poisson, $rdist_chi_square, $rdist_t, and $rdist_erlang functions, the parameters mean, degree_of_freedom, and k_stage shall be greater than zero (0).
· Each of these functions returns a pseudo-random number whose characteristics are described by the function name, e.g., $rdist_uniform returns random numbers uniformly distributed in the interval specified by its parameters.
· For each system function, the seed parameter is an inout parameter; that is, a value is passed to the function and a different value is returned. The system functions shall always return the same value given the same seed. This facilitates debugging by making the operation of the system repeatable. The argument for seed shall be an integer variable, which is initialized by the user and only updated by the system function. This ensures the desired distribution is achieved.
· All functions return a real value.
· In $rdist_uniform, the start and end parameters are real inputs which bound the values returned. The start value shall be smaller than the end value.
· The mean parameter used by $rdist_normal, $rdist_exponential, $rdist_poisson, and $rdist_erlang is an real input which causes the average value returned by the function to approach the value specified.
· The standard_deviation parameter used by $rdist_normal is a real input, which helps determine the shape of the density function. Using larger numbers for standard_deviation spreads the returned values over a wider range. Using a mean of zero (0) and a standard_deviation of one (1), $rdist_normal generates Gaussian distribution.
· The degree_of_freedom parameter used by $rdist_chi_square and $rdist_t is a real input, which helps determine the shape of the density function. Using larger numbers for degree_of_freedom spreads the returned values over a wider range.
10.4 Simulation control system tasks
There are two simulation control system tasks, $finish and $stop.
10.4.1 $finish
The syntax for this task is shown in Syntax 104.

Syntax 10-4- Syntax for the finish_task
$finish simply makes the simulator exit. If an expression is supplied to this task, its value determines which diagnostic messages are printed before the prompt is issued, as shown in Table 101. One (1) is the default if no argument is supplied.
Table 10-1- Diagnostic messages 
Parameter Message
0 Prints nothing
1 Prints simulation time and location
2 Prints simulation time, location, and statistics about the memory and CPU time used in simulation

10.4.2 $stop
The syntax for this task is shown in Syntax 105.

Syntax 10-5- Syntax for the stop_task
$stop causes simulation to be suspended at a converged timepoint. This task takes an optional expression argument (0, 1, or 2), which determines what type of diagnostic message is printed. The amount of diagnostic messages output increases with the value of n, as shown in Table 101.
10.5 File operation tasks
This section details the file operation tasks.
10.5.1 $fopen
The syntax for this task is shown in Syntax 106.

Syntax 10-6- Syntax for the file_open_task
$fopen opens the file specified as an argument and returns a 32-bit multichannel descriptor which is uniquely associated with the file. It returns 0 if the file could not be opened for writing.
The multichannel descriptor can be thought of as a set of 32 flags, where each flag represents a single output channel. The least significant bit (bit 0) of a multichannel descriptor always refers to the standard output. The standard output is also called channel 0. The other bits refer to channels which have been opened by $fopen.
The first call to $fopen opens channel 1 and returns a multichannel descriptor value of 2-that is, bit 1 of the descriptor is set. A second call to $fopen opens channel 2 and returns a value of 4-that is, only bit 2 of the descriptor is set. Subsequent calls to $fopen open channels 3, 4, 5, and so on and return values of 8, 16, 32, and so on, up to a maximum of 32 open channels. Thus, a channel number corresponds to an individual bit in a multichannel descriptor.
10.5.2 $fclose
The syntax for this task is shown in Syntax 107.

Syntax 10-7- Syntax for the file_close_task
$fclose closes the channels specified in the multichannel descriptor and does not allow any further output to the closed channels. $fopen reuses channels which have been closed.
10.6 Display tasks
The syntax for these functions are shown in Syntax 108.

Syntax 10-8- Syntax for the display_tasks
The following rules apply to these functions.
- $strobe provides the ability to display simulation data when the simulator has converged on a solution for all nodes.
- $strobe displays its arguments in the same order they appear in the argument list. Each argument can be a quoted string, an expression which returns a value, or a null argument.
- The contents of string arguments are output literally, except when certain escape sequences are inserted to display special characters or specify the display format for a subsequent expression.
- Escape sequences are inserted into a string in three ways:
· The special character \ indicates the character to follow is a literal or non-printable character (see Table 102).
· The special character % indicates the next character shall be interpreted as a format specification which establishes the display format for a subsequent expression argument (see Table 103). For each % character which appears in a string, a corresponding expression argument shall be supplied after the string.
· The special character string %% indicates the display of the percent sign character (%) (see Table 102).
- Any null argument produces a single space character in the display. (A null argument is characterized by two adjacent commas (,,) in the argument list.)
- When $strobe is invoked without arguments, it simply prints a newline character.
The $display task provides the same capabilities as $strobe. The $write task provides the same capabilities as $strobe, but with no newline. The $monitor task provides the same capabilities as $strobe, but outputs only when a parameter changes.
10.6.1 Escape sequences for special characters
The escape sequences shown in Table 102, when included in a string argument, print special characters.
Table 10-2- Escape sequences for printing special characters
\n The newline character
\t The tab character
\\ The \ character
\" The " character
\ddd A character specified by 1 to 3 octal digits
%% The % character

10.6.2 Format specifications
Table 103 shows the escape sequences used for format specifications. Each escape sequence, when included in a string argument, specifies the display format for a subsequent expression. For each % character (except %m and %%) which appears in a string, a corresponding expression shall follow the string in the argument list, except a null argument. The value of the expression replaces the format specification when the string is displayed.
Table 10-3- Escape sequences for format specifications
%h or %H Display in hexadecimal format
%d or %D Display in decimal format
%o or %O Display in octal format
%b or %B Display in binary format
%c or %C Display in ASCII character format
%m or %M Display hierarchical name
%s or %S Display as a string

Any expression argument which has no corresponding format specification is displayed using the default decimal format in $strobe.
The format specifications in Table 104 are used for real numbers and have the full formatting capabilities available in the C language. For example, the format specification %10.3g sets a minimum field width of 10 with three (3) fractional digits.
Table 10-4- Format specifications for real numbers
%e or %E Display `real' in an exponential format
%f or %F Display `real' in a decimal format
%g or %G Display `real' in exponential or decimal format, whichever format results in the shorter printed output

10.6.3 Hierarchical name format
The %m format specifier does not accept an argument. Instead, it causes the display task to print the hierarchical name of the module, task, function, or named block which invokes the system task containing the format specifier. This is useful when there are many instances of the module which call the system task. One obvious application is timing check messages in a flip-flop or latch module; the %m format specifier pinpoints the module instance responsible for generating the timing check message.
10.6.4 String format
The %s format specifier is used to print ASCII codes as characters. For each %s specification which appears in a string, a corresponding parameter shall follow the string in the argument list. The associated argument is interpreted as a sequence of 8-bit hexadecimal ASCII codes, with each 8 bits representing a single character. If the argument is a variable, its value shall be right-justified so the right-most bit of the value is the least-significant bit of the last character in the string. No termination character or value is required at the end of a string and leading zeros (0) are never printed.
10.7 Announcing discontinuity
The $discontinuity function is used to give hints to the simulator about the behavior of the module so the simulator can control its simulation algorithms to get accurate results in exceptional situations. This function does not directly specify the behavior of the module. $discontinuity shall be executed whenever the analog behavior changes discontinuously.
The general form is
$discontinuity[ ( constant_expression ) ];
where constant_expression indicates the degree of the discontinuity. $discontinuity(i) implies a discontinuity in the i'th derivative of the constitutive equation with respect to either a signal value or time where i must be non-negative. Hence, $discontinuity(0) indicates a discontinuity in the equation, $discontinuity(1) indicates a discontinuity in its slope, etc.
Note: Because discontinuous behavior can cause convergence problems, discontinuity shall be avoided whenever possible.
The filter functions (transition(), slew(), laplace(), etc.) can be used to smooth discontinuous behavior. However, in some cases it is not possible to implement the desired functionality using these filters. In those cases, the $discontinuity function shall be executed when the signal behavior changes abruptly.
Note: Discontinuity created by switch branches and built-in system functions, such as transition() and slew(), does not need to be announced.
Examples:
Example 1
The following example uses the discontinuity function to model a relay.
module relay (c1, c2, pin, nin) ;
inout c1, c2 ;
input pin, nin ;
electrical c1, c2, pin, nin ;
parameter real r=1 ;
analog begin
@(cross(V(pin,nin))) $discontinuity ;
if (V(pin,nin) >= 0)
I(c1,c2) <+ V(c1,c2)/r;
else
I(c1,c2) <+ 0 ;
end
endmodule
In this example, cross() controls the time step so the time when the relay changes position is accurately resolved. It also triggers the $discontinuity function, which causes the simulator to react properly to the discontinuity. This would have been handled automatically if the type of the branch (c1,c2) had been switched between voltage and current.
Example 2
Another example is a source which generates a triangular wave. In this case, neither the model nor the waveforms generated by the model are discontinuous. Rather, the waveform generated is piecewise linear with discontinuous slope. If the simulator is aware of the abrupt change in slope, it can adapt to eliminate problems resulting from the discontinuous slope (typically changing to a first order integration method).
module triangle(out);
output out;
voltage out;
parameter real period = 10.0, amplitude = 1.0;
integer slope;
real offset;
analog begin
@(timer(0, period)) begin
slope = +1;
offset = $abstime ;
$discontinuity;
end
@(timer(period/2, period)) begin
slope = -1 ;
offset = $abstime;
$discontinuity ;
end
V(out) <+ amplitude*slope*
(4*($abstime - offset)/period - 1);
end
endmodule
10.8 Time related functions
The $bound_step() function puts a bound on the next time step. It does not specify exactly what the next time step is, but it bounds how far the next time point can be from the present time point. The function takes the maximum time step as an argument. It does not return a value.
The general form is
$bound_step ( expression ) ;
where expression is a required argument and represents the maximum timestep the simulator can advance.
Examples:
The example below implements a sinusoidal voltage source and uses the $bound_step() function to assure the simulator faithfully follows the output signal (it is forcing 20 points per cycle).
module vsine(out);
output out;
voltage out;
parameter real freq=1.0, ampl=1.0, offset=0.0;
analog begin
V(out) <+ ampl*sin(2.0*`M_PI*freq*$abstime) + offset;
$bound_step(0.05/freq);
end
endmodule
For details on the last_crossing() function, see 4.4.10.



Quadralay Corporation
http://www.webworks.com
Voice: (512) 719-3399
Fax: (512) 719-3606
sales@webworks.com
TOC PREV NEXT INDEX