404 Not found
404 Not found
404 Not found
404 Not found
Verilog Arrays and Memories
Array assignment, array example, register vector, memory example, what is a verilog array .
An array declaration of a net or variable can be either scalar or vector. Any number of dimensions can be created by specifying an address range after the identifier name and is called a multi-dimensional array. Arrays are allowed in Verilog for reg , wire , integer and real data types.
An index for every dimension has to be specified to access a particular element of an array and can be an expression of other variables. An array can be formed for any of the different data-types supported in Verilog.
Note that a memory of n 1-bit reg is not the same as an n-bit vector reg.
The code shown below simply shows how different arrays can be modeled, assigned and accessed. mem1 is an 8-bit vector, mem2 is an 8-bit array with a depth of 4 (specified by the range [0:3]) and mem3 is a 16-bit vector 2D array with 4 rows and 2 columns. These variables are assigned different values and printed.
What are memories ?
Memories are digital storage elements that help store a data and information in digital circuits. RAMs and ROMs are good examples of such memory elements. Storage elements can be modeled using one-dimensional arrays of type reg and is called a memory . Each element in the memory may represent a word and is referenced using a single array index.
Verilog vectors are declared using a size range on the left side of the variable name and these get realized into flops that match the size of the variable. In the code shown below, the design module accepts clock, reset and some control signals to read and write into the block.
It contains a 16-bit storage element called register which simply gets updated during writes and returns the current value during reads. The register is written when sel and wr are high on the same clock edge. It returns the current data when sel is high and wr is low.
The hardware schematic shows that a 16-bit flop is updated when control logic for writes are active and the current value is returned when control logic is configured for reads.
In this example, register is an array that has four locations with each having a width of 16-bits. The design module accepts an additional input signal which is called addr to access a particular index in the array.
It can be seen in the hardware schematic that each index of the array is a 16-bit flop and the input address is used to access a particular set of flops.
An Introduction to the Verilog Operators
In this post, we talk about the different operators which we can use in verilog. These operators provide us with a way to process the digital data in our verilog designs.
This processing can be extremely simple, as is the case with simple logic gates . However, we may also need to perform complex logical or mathematical operations on our data.
In any case, verilog provides us with a number of operators which allow us to perform a wide range of different calculations or operations on our data.
In most instances when we use verilog operators, we create boolean expressions or logic circuits which we want to synthesize . However, there are also some operators which we can't use to write synthesizable code.
Let's take a closer look at the various different types of operator which we can use in our verilog code.
- Verilog Bit Wise Operators
We use the bit wise operators to combine a number of single bit inputs into a single bit output. In addition, we can also use the bit wise operators on verilog vector types .
We most commonly use the bit wise operators to model logic gates in verilog.
The table below shows the full list of bit wise operators which we can use in verilog.
The verilog code below shows how we use each of these operators in practise.
- Verilog Arithmetic Operators
We use arithmetic operators to perform basic mathematic functions on our variables. These operators should already be familiar as they are mostly replications of common mathematic symbols.
However, these operators also require some consideration when we use them with synthesizable code.
The plus, minus and multiplication operators can all be synthesised by most modern tools.
However, this can often result in sub-optimal logical performance. As a result, it can be necessary to design logic circuits which specifically perform these functions.
Alternatively, we may wish to use DSP blocks within our FPGA to perform these operations more efficiently.
We should never use the modulus, exponential or divide operators for synthesizable code as most tools will be unable to handle them.
The table below shows the full list of arithmetic operators in Verilog.
The code snippet below shows how we use each of these operators in practise.
- Verilog Relational Operators
We use relational operators to compare the value of two different variables in verilog. The result of this comparison returns either a logical 1 or 0 , representing true and false respectively.
These operators are similar to what we would see in other programming languages such as C or Java .
In addition to this, most of these operators are also commonly used in basic mathematics expressions so they should already feel familiar.
The table below shows the full list of relational operators in Verilog.
The verilog code below shows how we use each of the relational operators in practise.
- Verilog Logical Operators
The verilog logical operators are similar to the bit-wise operators we have already seen.
However, rather than using these operators to model gates we use them to combine relational operators. As a result, we can build more complex expressions which can perform more than one comparison.
As with relational operators, these expressions return either a 1 (true) or 0 (false).
There are only three logical operators which we can use in verilog. Again, these are similar to operators which are used in languages such as C or Java.
The table below shows the full list of logical operators in Verilog.
The verilog code below shows how we use each of the logical operators in practise.
Again, it is important that we use parentheses to separate the different elements in our expressions when using these operators.
- Verilog Shift Operators
In addition to the operators we have already seen, there are a few extra operators which we can use for specific logical functions.
One of the most useful and commonly used of these special functions are the shift operators, which are shown in the table below.
When designing digital circuits, we frequently make use of shift operations . As a result, verilog provides us with a simple technique for implementing these functions.
The shift operator actually requires two arguments. The first of these is the name of the signal which we want to shift. The second argument is the number of bits we want to shift.
When we use the logical shift operators, all the blank positions are filled with 0b after the signal has been shifted by the required number of bits.
In contrast, the arithmetic shift operators preserve the sign of the shifted signal. As a result of this, they should only be used with the verilog signed types .
The code snippet below shows how we use the shift operators in practise.
- Verilog Conditional Operator
In verilog, we use a construct known as the conditional operator to assign data to a signal based on a conditional statement .
To use the conditional operator, we write a logical expression before the ? operator which is then evaluated to see if it is true or false.
The output is assigned to one of two values depending on whether the expression is true or false.
This operator may already be familiar as it is also used in other programming languages such as C and Java . However, in this case it is known as the ternary operator.
The code snippet below shows the general syntax for the verilog conditional operator.
When the expression given in the <condition> field evaluates as true, then the output is set to the value given in the <true> field.
If the conditional expression evaluates as false, then the output is set to the value given by the <false> field.
The code snippet below shows a practical example of the verilog conditional operator. In the a future post in this series, we see how we can use the conditional operator model multiplexors .
- Concatenation and Replication Operators
The final types of verilog operator which we can use are the concatenation and replication operators.
In both instances, the output of these operators are a vector type. However, the inputs to both of these operators can be either single bit or vector types.
Both of these verilog operators are show in the table below.
We use the verilog concatenation operator to combine two or more signals into a vector.
As an example, we may have 2 single bit signals which we want to combine to use as an address for a multiplexor.
To use the concatenation operator, we list the signals which we wish to combine within the curly brackets. We separate this list of signals using a comma.
When we use the verilog concatenation operator, the bits in the output match the order in which they are listed inside the brackets.
For example, the code snippet below would result in a output vector which has the value 0011b.
We use the replication operator to assign the same value to a number of bits in a vector.
For example, if we wanted to assign all of the bits of a vector to 0b then we would use the replication operator.
When we use the replication operator we have to specify both the signal or value we want to replicate and the number of times we want to replicate it.
The verilog code below show how we use the concatenation and replication operators in practice.
Which type of operators do we use to model logic gates in verilog?
We use the bit wise operators to model logic gates in verilog.
Two of the arithmetic operators should not be used with synthesizable code – name them.
The division and modulus operators can’t be synthesized.
What is the difference between the bit wise and logical operators?
The bit wise operators work on individual bits whereas the logical operators are used to combine logical expressions.
What is the difference between the logical shift operators and the arithmetic shift operators.
The logical shift operators pad the blank positions with 0b whereas the arithmetic operator preserves the sign of the signal.
Leave a Reply Cancel reply
Your email address will not be published. Required fields are marked *
Save my name, email, and website in this browser for the next time I comment.
Table of Contents
Sign up free for exclusive content.
Don't Miss Out
We are about to launch exclusive video content. Sign up to hear about it first.