# A Soft Limiter

This is a definition for a soft limiter device. This will pass the input signal through unchanged up to some limit after which it will follow a decaying exponential in the form:

`1 - exp( -(v-vlim) )`

The same in reverse occurs for the lower limit. Here is the full definition:

```'include "disciplines.vams"
(* pins="L;R" *)module soft_limiter(in, out) ;

electrical in, out ;
parameter real  vlow=-1.0,
vhigh=1.0,
soft=0.1 from (0:1.0) ;

localparam real band = (vhigh-vlow)*soft,
vlow_1 = vlow+band,
vhigh_1 = vhigh-band ;

real vin ;

if (vhigh<vlow)
\$fatal(1, "Lower limit must be less than higher limit") ;

analog
begin

vin = V(in) ;

if (vin>vhigh_1)
V(out) <+ vhigh_1+band*(1.0-exp(-(vin-vhigh_1)/band));
else if (vin<vlow_1)
V(out) <+ vlow_1-band*(1.0-exp((vin-vlow_1)/band)) ;
else
V(out) <+ vin ;
end
endmodule```

See Examples/Manual/Soft-limiter example

The above example introduces the following new concepts:

1. Pin location attribute. The module attribute (* pins="L;R" *) defines the location of the pins for an auto-generated symbol
2. Variables. We use vin to hold the value of V(in). In this example we have done this simply to make the code a little more readable. But variables can store any value or expression and have a much wider use
4. The \$fatal system task. This aborts the simulation immediately and displays the given message
5. The exp function
6. Local parameters using the localparam keyword
7. Parameter value range limits using the from keyword. (Also used in the resistor previous)

The soft limit example also uses a conditional statements using if and else which we first saw with the resistor example above.

In this topic:

## Pin Location Attribute

The module attribute (* pins="L;R" *) defines the location of the pins for an auto-generated symbol. The value is a list of semi-colon delimited letters that define the location of the corresponding module port pin on the schematic symbol. The letters accepted are L, R, T and B for "Left", "Right", "Top" and "Bottom" respectively. For more information see Specify Pin Positions

## Variables

Variables, such as vin in the example must be declared first. In the above example this declaration is the line:

`real vin ;`

This declares the variable 'real'. This is 'real' in the computing sense meaning that the value is stored using floating point arithmetic and can take non-integer values. Alternative declarations are integer, which means the variable stores whole numbers, and string which defines a variable that takes text strings. Variable declarations, like parameter declarations must be placed within the module - endmodule block. They can be declared outside the analog block, as in the example above, or they can be declared inside a named begin - end block. For example

```begin : main
real vin ;
...
end```

If declared this way, the variable may only be used within the begin - end block in which it was declared.

This is not the same as the initial_step event introduced earlier although in many cases the end result is the same. analog initial blocks are executed just once before any simulation starts. initial_step event code is executed for every iteration performed during the DC operating point calculation. In most situations any kind of initialisation is most efficiently performed in an analog initial block.

The following are not permitted.
• Expressions containing access functions such as V(in)
• Expressions containing analog operators such as ddt. (These are introduced in later examples)
• Branch contributions
• Event statements (introduced in later examples)

## \$fatal

The \$fatal system task aborts the simulation unconditionally and immediately and displays the given message. For more information see \$fatal

## Functions

Verilog-A has a range of mathematical functions built-in. In the above example we have used the exp function. See Verilog-A Functions for a complete list.

## Local Parameters

A local parameter is one that cannot be changed by the user via the .MODEL statement or any other means. Local parameters are a way of defining constant vaues as, unlike variables, they cannot be assigned except in their declaration. In our example we declared the band local parameter as:

`localparam real band = (vhigh-vlow)*soft`

We could just as simply have defined a variable to do this. However, by using a local parameter we know it cannot be subsequently modified. This aids readability but more importantly tells the compiler that it cannot change allowing it to optimise the result effectively.

## Parameter Limits

Parameters can be given maximum and minimum limits. This is done using the from keyword. In the above example:

`soft=0.1 from (0:1.0)`

defines the limits for soft from 0 to 1.0 exclusive. This means that any value greater than 0 and less than 1.0 will be accepted but the values 0 and 1.0 will not be allowed. You can also define inclusive limits using a square bracket instead of a round parenthesis. E.g in the following 1.0 is allowed:

`soft=0.1 from (0:1.0]`

## Conditional Statements

Conditional statements are in the form:

```if (conditional-expression)
statement ;
else
statement ;```

statement may be a single statement such as a branch contribution or it may be a collection of statements enclosed by begin and end.