Using Continuous Assignment to Model Combinational Logic in Verilog
In this post, we talk about continuous assignment in verilog using the assign keyword. We then look at how we can model basic logic gates and multiplexors in verilog using continuous assignment.
There are two main classes of digital circuit which we can model in verilog – combinational and sequential.
Combinational logic is the simplest of the two, consisting solely of basic logic gates, such as ANDs, ORs and NOTs. When the circuit input changes, the output changes almost immediately (there is a small delay as signals propagate through the circuit).
In contrast, sequential circuits use a clock and require storage elements such as flip flops. As a result, output changes are synchronized to the circuit clock and are not immediate.
In this post, we talk about the techniques we can use to design combinational logic circuits in verilog. In the next post, we will discuss the techniques we use to model basic sequential circuits.
Find the source code from this video tutorial on EDA Playground
We’d really appreciate your feedback on these videos and the Fourdotday payment portal. For feedback on the videos, please contact us on video@fpgatutorial.com. For feedback on the Fourdotpay platform, please contact us on paul@fourdotzero.io.
Payments for our videos are handled by a new company called Fourdotpay. Anyone who signs up with them during this introductory period will receive a £5 welcome bonus. This means you can try out one of our new videos absolutely free. See our FAQS page for details about how to take advantage of this offer.
Continuous Assignment in Verilog
We use continuous assignment to drive data onto verilog net types in our designs. As a result of this, we often use continuous assignment to model combinational logic circuits.
We can actually use two different methods to implement continuous assignment in verilog.
The first of these is known as explicit continuous assignment. This is the most commonly used method for continuous assignment in verilog.
In addition, we can also use implicit continuous assignment, or net declaration assignment as it is also known. This method is less common but it can allow us to write less code.
Let’s look at both of these techniques in more detail.
Explicit Continuous Assignment
We normally use the assign keyword when we want to use continuous assignment in verilog. This approach is known as explicit continuous assignment.
The verilog code below shows the general syntax for continuous assignment using the assign keyword.
assign <variable> = <value>;
The <variable> field in the code above is the name of the signal which we are assigning data to. We can only use continuous assignment to assign data to net type variables.
The <value> field can be a fixed value or we can create an expression using the verilog operators we discussed in a previous post. We can use either variable or net types in this expression.
When we use continuous assignment, the <variable> value changes whenever one of the signals in the <value> field changes state.
The code snippet below shows the most basic example of continuous assignment in verilog. In this case, whenever the b signal changes states, the value of a is updated so that it is equal to b.
assign a = b;
Net Declaration Assignment
We can also use implicit continuous assignment in our verilog designs. This approach is also commonly known as net declaration assignment in verilog.
When we use net declaration assignment, we place a continuous assignment in the statement which declares our signal. This can allow us to reduce the amount of code we have to write.
To use net declaration assignment in verilog, we use the = symbol to assign a value to a signal when we declare it.
The code snippet below shows the general syntax we use for net declaration assignment.
<type> <variable> = <value>;
The variable and value fields have the same function for both explicit continuous assignment and net declaration assignment.
As an example, the verilog code below shows how we would use net declaration assignment to assign the value of b to signal a.
wire a = b;
Modelling Combinational Logic Circuits in Verilog
We use continuous assignment and the verilog operators to model basic combinational logic circuits in verilog.
To show we would do this, let’s look at the very basic example of a three input and gate as shown below.
To model this circuit in verilog, we use the assign keyword to drive the data on to the and_out output. This means that the and_out signal must be declared as a net type variable, such as a wire.
We can then use the bit wise and operator (&) to model the behavior of the and gate.
The code snippet below shows how we would model this three input and gate in verilog.
[code lang=”verilog”] assign and_out = a & b & c; [/code]This example shows how simple it is to design basic combinational logic circuits in verilog. If we need to change the functionality of the logic gate, we can simply use a different verilog bit wise operator.
If we need to build a more complex combinational logic circuit, it is also possible for us to use a mixture of different bit wise operators.
To demonstrate this, let’s consider the basic circuit shown below as an example.
To model this circuit in verilog, we need to use a mixture of the bit wise and (&) and or (|) operators. The code snippet below shows how we would implement this circuit in verilog.
[code lang=”verilog”] assign logic_out = (a & b) | c; [/code]Again, this code is relatively straight forward to understand as it makes use of the verilog bit wise operators which we discussed in the last post.
However, we need to make sure that we use brackets to model more complex logic circuit. Not only does this ensure that the circuit operates properly, it also makes our code easier to read and maintain.
Modelling Multiplexors in Verilog
Multiplexors are another component which are commonly used in combinational logic circuits.
In verilog, there are a number of ways we can model these components.
One of these methods uses a construct known as an always block. We normally use this construct to model sequential logic circuits, which is the topic of the next post in this series. Therefore, we will look at this approach in more detail the next blog post.
In the rest of this post, we will look at the other methods we can use to model multiplexors.
Verilog Conditional Operator
As we talked about in a previous blog, there is a conditional operator in verilog. This functions in the same way as the conditional operator in the C programming language.
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.
The verilog code below shows the general syntax which the conditional operator uses.
output = <expression> ? <value if true> : <value if false>;
From this example, it is clear how we can create a basic two to one multiplexor using this operator.
However, let’s look at the example of a simple 2 to 1 multiplexor as shown in the circuit diagram below.
The code snippet below shows how we would use the conditional operator to model this multiplexor in verilog.
assign q = addr ? b : a;
Nested Conditional Operators
Although this is not common, we can also write code to build larger multiplexors by nesting conditional operators.
To show how this is done, let’s consider a basic 4 to 1 multiplexor as shown in the circuit below.
To model this in verilog using the conditional operator, we treat the multiplexor circuit as if it were a pair of two input multiplexors.
This means one multiplexor will select between inputs A and B whilst the other selects between C and D. Both of these multiplexors use the LSB of the address signal as the address pin.
assign mux1 = addr[0] ? b : a; assign mux2 = addr[0] ? d : c;
To create the full four input multiplexor, we would then need another multiplexor.
This takes the outputs from the first two multiplexors and uses the MSB of the address signal to select between them.
The code snippet below shows the simplest way to do this. This code uses the signals mux1 and mux2 which we defined in the last example.
assign q = addr[1] ? mux2 : mux1;
However, we could easily remove the mux1 and mux2 signals from this code and instead use nested conditional operators.
This reduces the amount of code that we would have to write without affecting the functionality.
The code snippet below shows how we would do this.
assign q = addr[1] ? (addr[0] ? d : c) : (addr[0] ? b : a);
As we can see from this example, when we use conditional operators to model multiplexors in verilog, the code can quickly become difficult to understand. Therefore, we should only use this method to model small multiplexors.
Arrays as Multiplexors
It is also possible for us to use verilog arrays to build simple multiplexors.
To do this we combine all of the multiplexor inputs into a single array type and use the address to point at an element in the array.
To get a better idea of how this works in practise, let’s consider a basic four to one multiplexor as an example.
The first thing we must do is combine our input signals into an array. There are two ways in which we can do this.
Firstly, we can declare an array and then assign all of the individual bits, as shown in the verilog code below.
// Assigning individual bits in the vector assign in_vec[0] = a; assign in_vec[1] = b; assign in_vec[2] = c; assign in_vec[3] = d;
Alternatively we can use the verilog concatenation operator, which allows us to assign the entire array in one line of code.
To do this, we use a pair of curly braces – { } – and list the elements we wish to include in the array inside of them.
When we use the concatenation operator we can also declare and assign the variable in one statement, as long as we use a net type.
The verilog code below shows how we can use the concatenation operator to populate an array.
// Using vector assignment assign in_vec = {d, c, b, a}; // Declare and assign the vector in one line wire [3:0] in_vec = {d, c, b, a};
As verilog is a loosely typed language, we can use the two bit addr signal as if it were an integer type. This signal then acts as a pointer that determines which of the four elements to select.
The code snippet below demonstrates this method in practise. As the mux output is a wire, we must use continuous assignment in this instance.
assign mux_out = in_vec[addr];