5
Programming Techniques in Verilog II

5.1 Programming Techniques in Verilog II

Verilog is a Hardware Description language (HDL) used to illustrate a digital system such as a microprocessor, flip-flops (F/Fs), network switch, memory, etc. The Verilog language can be used to describe any digital hardware at any level. The circuit designs developed using HDL are not dependent on technology, are more helpful than schematics and are very simple for debugging and designing, especially for huge circuits. In this chapter, the dataflow model of the circuit is described with the help of different types of circuits.

5.2 Dataflow Model of Circuits

Dataflow modeling does not explain the combinational circuits logic gate. It describes the Boolean funtion of output variable in terms of input variables using operators available in the Verilog library. Here, the data flows from register to register. It requires lesser design steps in comparison to the gate-level model. A number of operators are used by dataflow modeling for the operator to produce the desired results. The model makes use of continuous assignments with the keyword “assign.”

HDL Operators for the dataflow model include:

  • + Binary addition
  • − Binary subtraction
  • & Bit – wise AND
  • | Bit – wise OR
  • ^ Bit – wise XOR
  • ~ Bit – wise NOT
  • ?: Conditional

5.3 Dataflow Model of Combinational Circuits

Adder and Subtractor

Multiplexer

Decoder

Comparator

5.3.1 Adder and Subtractor

5.3.1.1 Half Adder

In a half-adder circuit, X and Y are the inputs, however S and C are the outputs. For two inputs, there are four possibilities: 00, 01, 10, 11. All four possible combinations, as well as corresponding outputs, are described in Table 5.1 [1].

Table 5.1 Half adder.

InputsOutputs
XYSC
0000
0110
1010
1101

Dataflow description of the half adder

Based on the observations given in Table 5.1, the Boolean expression for a half adder is defined as follows:

begin inline style bold S equals bold X apostrophe bold Y plus bold X straight Y apostrophe equals bold X circled plus bold Y text   end text bold a nd text   end text bold C equals bold X straight Y end style

Verilog Code

In Verilog code of half adder using dataflow modeling, inputs and outputs are defined inside the module Half_Add. Inside the module, two logic continuous assignments, keyword assign, are called (2).

begin inline style table attributes columnalign left end attributes row cell text moduleHalf end text _ text Add end text left parenthesis straight X comma straight Y comma straight S text   end text comma straight C right parenthesis semicolon end cell row cell text input  end text straight X comma straight Y semicolon end cell row cell text output  end text straight S comma straight C semicolon end cell row cell text assign   end text straight S text     end text equals straight X hat straight Y semicolon end cell row cell text assign  end text straight C text  = end text straight X & amp semicolon straight Y semicolon end cell row cell text endmodule end text end cell end table end style

5.3.1.2 Half Subtractor

In a half-adder circuit, X and Y are the inputs, whereas D and B are considered outputs. Since there are only two inputs, the input combinations are four: 00, 01, 10, 11. All four possible combinations, as well as corresponding outputs, are described in Table 5.2 (3).

Table 5.2 Half subtractor.

InputsOutputs
XYDB
0000
0111
1010
1100

Based on the observations given in Table 5.2, the Boolean expression for a half subtractor is defined as follows:

The difference, D=XY+XY and borrow, B=XY

Verilog Code

In Verilog code of the half subtractor using dataflow modeling, inputs and outputs are defined inside the module Half_Subtractor. Inside the module, continuous assignment keyword assign is called two times.

table attributes columnalign left end attributes row cell m o d u l e H a l f _ S u b t r a c t o r left parenthesis X comma Y comma text end text D comma B right parenthesis semicolon end cell row cell i n p u t text end text X comma Y semicolon end cell row cell o u t p u t text end text D comma B semicolon end cell row cell a s s i g n text     end text D equals X hat text   end text Y semicolon end cell row cell a s s i g n text end text B text     end text equals text  ~ end text X & amp semicolon Y semicolon end cell row cell e n d m o d u l e end cell end table

5.3.2 Multiplexer

5.3.2.1 2 × 1 Multiplexer

In the 2 × 1 multiplexer (Figure 5.1), there are two inputs, one select line and one output line. Based on the select line, inputs are reflected in terms of output at the output terminal. As only one select line is present in a 2 × 1 multiplexer, there are two options; 0 and 1.

Figure 5.1 Block diagram of 2 × 1 multiplexer.

As shown in Figure 5.1, when the select line becomes 0, the I0 is reflected at output port Y, and when the select line becomes 1, then I1 is considered as output as shown in Table 5.4.

Table 5.4 4 × 1 multiplexer.

Select linesOutput
S1S0Y
00I0
01I1
10I2
11I3

Based on the observations given in Table 5.3, the Boolean expression for a 2 × 1 multiplexer is defined as follows:

Table 5.3 2 × 1 multiplexer.

SelectOutput
SY
010
111

Y=S'I0+SI1

Verilog Code

In Verilog code of the 2 × 1 multiplexer using dataflow modeling, inputs and outputs are defined inside the module mux_2x1_df. Inside the module, one continuous assignment keyword assign is used.

table attributes columnalign left end attributes row cell m o d u l e text end text m u x _ 2 x 1 _ d f text end text left parenthesis I 0 comma text end text I 1 comma text end text S comma text end text Y right parenthesis semicolon end cell row cell i n p u t I 0 comma text end text I 1 comma text end text S semicolon end cell row cell o u t p u t text end text Y semicolon end cell row cell a s s i g n text end text Y equals left parenthesis tilde S text   end text & amp semicolon text   end text I 0 right parenthesis text end text l text end text left parenthesis S text   end text & amp semicolon text   end text I 1 right parenthesis semicolon end cell row cell e n d m o d u l e end cell end table

5.3.2.1 4 × 1 Multiplexer

In the 4 × 1 multiplexer, there are four inputs, two select lines and one output line. Based on the select lines, the inputs are reflected in terms of output at the output terminal. As only two select lines are present in a 4 × 1 multiplexer (Figure 5.2), there are four options: 00, 01, 10, 11. As shown in Figure 5.2, when the select line becomes 00, the I0 is reflected at the output port Y, and when the select line becomes 11, then I3 is considered as an output [3].

Figure 5.2 Block diagram of 4 × 1 multiplexer.

Based on the observations given in Table 5.4, the Boolean expression for a 4 × 1 multiplexer is defined as follows:

Verilog Code

In Verilog code of a 4 × 1 multiplexer using dataflow modeling, inputs and outputs are defined inside the module mux_4x1. Inside the module, one continuous assignment keyword assign is used according to the Boolean expression (2).

table attributes columnalign left end attributes row cell m o d u l e text end text m u x _ 4 x 1 left parenthesis y comma text end text i 0 comma i 1 comma i 2 comma i 3 comma text end text s 0 comma text end text s 1 right parenthesis semicolon end cell row cell i n p u t text end text i 0 comma text end text i 1 comma text end text i 2 comma text end text i 3 comma text end text s 0 comma text end text s 1 semicolon end cell row cell o u t p u t text  y end text semicolon end cell row cell a s s i g n text  y  end text equals text end text left parenthesis tilde s 0 text end text & amp semicolon text end text tilde s 1 text end text & amp semicolon text end text i 0 right parenthesis text end text end cell row cell table row cell text                                                         end text left parenthesis s 0 text end text & amp semicolon text end text tilde s 1 text end text & amp semicolon text end text i 1 right parenthesis text end text vertical line end cell row cell space space space space space space space space space space space space space space space space space space space space space space space space space space space space space space space space space space space space space space left parenthesis tilde text end text s 0 text end text & amp semicolon text end text s 1 text end text & amp semicolon text end text i 2 right parenthesis text end text vertical line end cell row cell space space space space space space space space space space space space space space space space space space space space space space space space space space space space space space space space space space left parenthesis s 0 text end text & amp semicolon text end text s 1 text end text & amp semicolon text end text i 3 right parenthesis semicolon end cell end table end cell row cell e n d m o d u l e end cell end table

Conditional Operator

A ternary operator is another name for a conditional operator (?:), it uses three operands at a time. When the first is true, the operand at the second is evaluated and when the first is false, the operand at the third is evaluated.

Example of a conditional operator.

assign OUT = select ? A: B;

2 × 1 multiplexer using a Conditional Operator

Based on the observations given in Table 5.5, the Boolean expression for a 2 × 1 multiplexer is defined as follows:

Table 5.5 2 × 1 multiplexer.

Select lineOutput
SY
0I0
1I1

Y = S’I0 + SI1.

The conditional operator is used for Y output. The conditional operator (?) suggests output Y is equal to data I1, if the select line S is true otherwise, it is I0 [1].

Verilog Code

In Verilog code of a 2 × 1 multiplexer using a conditional operator, inputs and outputs are defined inside the module mux_2x1. Inside the module, one continuous assignment keyword assign is used. Under the assign statement, a conditional operator is used.

table attributes columnalign left end attributes row cell m o d u l e text end text m u x _ 2 x 1 text end text left parenthesis I 0 comma text end text I 1 comma text end text S comma text end text Y right parenthesis semicolon end cell row cell i n p u t I 0 comma text end text I 1 comma text end text S semicolon end cell row cell o u t p u t text  Y end text semicolon end cell row cell a s s i g n text end text Y equals S ? I 1 colon I 0 text end text semicolon end cell row cell e n d m o d u l e end cell end table

4 × 1 multiplexer using a Conditional Operator:

Based on the observations given in Table 5.6, the Boolean expression for a 4 × 1 multiplexer is defined as follows:

bold Y equals text end text bold S bold 1 ’ bold S bold 0 ’ bold I bold 0 text end text plus text end text bold S bold 1 ’ bold S bold 0 bold I bold 1 text end text plus bold S bold 1 bold S bold 0 ’ bold I bold 2 text end text plus bold S bold 1 bold S bold 0 bold I bold 3

Table 5.6 4 × 1 multiplexer.

Select linesOutput
S1S0Y
00I0
01I1
10I2
11I3

Verilog Code

In Verilog code of a 4 × 1 multiplexer using the conditional operator, inputs and outputs are defined inside the module mux_4x1. Inside the module, one continuous assignment keyword assign is used. Under the assign statement, three conditional operators are used according to the Boolean expression.

table attributes columnalign left end attributes row cell m o d u l e text end text m u x _ 4 x 1 left parenthesis y comma text end text i 0 comma i 1 comma i 2 comma i 3 comma text end text s 0 comma text end text s 1 right parenthesis semicolon end cell row cell i n p u t text end text i 0 comma text end text i 1 comma text end text i 2 comma text end text i 3 comma text end text s 0 comma text end text s 1 semicolon end cell row cell o u t p u t text end text y semicolon end cell row cell a s s i g n text end text y equals text end text s 1 ? left parenthesis s 0 text end text ? I 3 colon i 2 right parenthesis colon left parenthesis s 0 ? I 1 colon i 0 right parenthesis semicolon end cell row cell e n d m o d u l e end cell end table

5.3.3 Decoder

2-to-4 Line Decoder

Based on the observations given in Table 5.7, the Boolean expression for a 2-to-4 decoder is defined as follows:

Table 5.7 2-to-4 decoder.

InputsOutputs
ABD0D1D2D3
001000
010100
100010
110001

D0 = A’B’

D1 = A’B

D2 = AB’

D3 = AB

Verilog Code

In Verilog code of a 2-to-4 decoder using dataflow modeling, inputs and outputs are defined inside the module Dec_2to4. Inside the module, four continuous assignment keyword assign are used according to the Boolean expression (2).

table attributes columnalign left end attributes row cell m o d u l e text end text D e c _ 2 t o 4 left parenthesis A comma text end text B comma text end text D 0 comma text end text D 1 comma text end text D 2 comma text end text D 3 right parenthesis semicolon end cell row cell i n p u t text end text A comma text end text B semicolon end cell row cell o u t p u t text end text D 0 comma text end text D 1 comma text end text D 2 comma text end text D 3 semicolon end cell row cell a s s i g n text end text D 0 equals tilde A text end text & amp semicolon tilde B semicolon end cell row cell a s s i g n text end text D 1 equals tilde A & amp semicolon B semicolon end cell row cell a s s i g n text end text D 2 equals A text end text & amp semicolon tilde B semicolon end cell row cell a s s i g n text end text D 3 equals A & amp semicolon B semicolon end cell row cell e n d m o d u l e end cell end table

5.3.4 Comparator

1-bit Magnitude Comparator

A comparator circuit is a combinational circuit that always has three outputs. If two inputs are present in a comparator, three outputs are A < B, A = B, and A > B. In a 1-bit magnitude comparator, two inputs and three outputs are present.

Based on the observations given in Table 5.8, the Boolean expression for a 1-bit comparator is defined as follows:

table attributes columnalign left end attributes row cell bold A less than bold B space equals & gt semicolon space bold A to the power of apostrophe bold B end cell row cell bold A & gt semicolon bold B equals & gt semicolon bold AB to the power of apostrophe end cell row cell bold A equals bold B equals & gt semicolon bold A to the power of bold apostrophe bold B to the power of bold apostrophe plus bold A B end cell end table

Table 5.8 1-bit magnitude comparator.

InputsOutputs
ABA < BA = BA > B
00010
01100
10001
11010

Verilog Code:

In Verilog code of the 1-bit magnitude comparator using dataflow modeling, inputs and outputs are defined inside the module Com_1. Inside the module, three continuous assignments keyword assign are used according to the Boolean expressions.

table attributes columnalign left end attributes row cell m o d u l e text end text C o m _ 1 left parenthesis A comma text end text B comma text end text A L B comma text end text A G B comma text end text A E B right parenthesis semicolon end cell row cell i n p u t text end text A comma B semicolon end cell row cell o u t p u t text end text A L B comma text end text A G B comma text end text A E B semicolon end cell row cell a s s i g n text end text A L B equals tilde A & amp semicolon B semicolon end cell row cell a s s i g n A E B equals tilde left parenthesis A hat B right parenthesis semicolon end cell row cell a s s i g n text end text A G B equals A text end text & amp semicolon tilde B semicolon end cell row cell e n d m o d u l e end cell end table

5.4 Testbench

Testbenches are used for simulation purposes. There is no need for physical hardware to design and simulate the testbench. To a Verilog-based design, the testbench defines a sequence of inputs to be applied by the simulator.

The testbench is incorporated into the module to be tested. There is no need for input as well as output nodes for the testbench. The reg and outputs with wire are required to define inputs. The keyword initial is used to define the inputs. All the input values are designated between begin and end [2].

The end of the simulation is determined with $finish or $stop.

# 100 $ s t o p semicolon

//the simulation will be suspended at time = 100

# 900 $ f i n i s h semicolon

//the simulation will be terminated at time = 1000

Testbench module for a simple circuit

table attributes columnalign left end attributes row cell m o d u l e c k t _ t b text end text left parenthesis text end text right parenthesis semicolon end cell row cell r e g A comma text end text B comma text end text C semicolon end cell row cell w i r e x comma text end text y semicolon end cell row cell M o d u l e _ n a m e C K T text end text left parenthesis A comma text end text B text end text comma C comma text end text x comma text end text y right parenthesis semicolon end cell row cell i n i t i a l end cell row cell b e g i n end cell row cell A equals 1 apostrophe b 0 semicolon text end text B equals 1 apostrophe b 0 semicolon text end text C equals 1 apostrophe b 0 semicolon end cell row cell # 100 end cell row cell A equals 1 apostrophe b 1 semicolon text end text B equals 1 apostrophe b 1 semicolon text end text C equals 1 apostrophe b 1 semicolon end cell row cell # 100 $ s t o p semicolon end cell row cell e n d end cell row cell e n d m o d u l e end cell end table

5.4.1 Dataflow Model of the Half Adder and Testbench

In a half-adder circuit, an input port has two inputs A and B whereas the output terminal has two outputs S and C. The main module name of the half adder using dataflow modeling is Half-Add [2]. This main module is called inside the testbench module of half adder HA_tb. Since A and B are defined as input ports in the main module, these two variables are defined as reg in the testbench module. The out port defined in the main module S and C are declared by wire in the testbench module.

//Half adder module

module Half_Add(A, B, S, C)
input A,B;
output S,C;
assign S =A^B;
assign C =A&B;
endmodule

With two input variables four possibilities arise, in testbench all possibilities are given a certain time delay while calling. In the testbench of the half adder illustrated in Box 5.1, 5 ns is used for all four possible states.

5.4.2 Dataflow Model of the Half Subtractor and Testbench

In a half-subtractor circuit, the input port has two inputs X and Y, whereas the output terminal has two outputs, D and B. The main module name of a half subtractor using dataflow modeling is Half_S. This main module is called inside the testbench module of the half subtractor HS_tb. Since X and Y are defined as input ports in the main module, these two variables are defined as reg in the testbench module. The out port defined in the main module as D and B are declared by wire in the testbench module.

//Half Subtractor module

module Half_ S(X, Y, D, B);
input X,Y;
output D,B;
assign D=X^Y;
assign B=~X&Y;
endmodule

With two input variables, four possibilities arise in the testbench, all possibilities are given a certain time delay while calling. In the testbench of the half adder illustrated above in Box 5.2, 5 ns is used for all four possible states.

5.4.3 Dataflow Model of 2 × 1 Mux and Testbench

In the 2 × 1 multiplexer circuit, the input port has two inputs, I0 and I1, along with one select line S, whereas the output terminal has one output Y. The main module name of a 2 × 1 multiplexer using dataflow modeling is mux_2x1_df. This main module is called mux_tb inside the testbench module of a 2 × 1 multiplexer. Since I0, I1, and S are defined as input ports in the main module, these three variables are defined as reg in the testbench module. The output defined in main modules Y, are declared by wire in the testbench module.

//2 × 1 mux module

table attributes columnalign left end attributes row cell m o d u l e text end text m u x _ 2 x 1 _ d f text end text left parenthesis I 0 comma text end text I 1 comma text end text S comma text end text Y right parenthesis semicolon end cell row cell i n p u t I 0 comma text end text I 1 comma text end text S semicolon end cell row cell o u t p u t text end text Y semicolon end cell row cell a s s i g n text end text Y equals left parenthesis tilde S & amp semicolon I 0 right parenthesis text end text l text end text left parenthesis text end text S & amp semicolon I 1 right parenthesis semicolon end cell row cell e n d m o d u l e end cell end table

//2 × 1 mux module with a Conditional operator

table attributes columnalign left end attributes row cell m o d u l e text end text m u x _ 2 x 1 _ d f left parenthesis I 0 comma text end text I 1 comma text end text S comma text end text Y right parenthesis semicolon end cell row cell i n p u t I 0 comma I 1 comma S semicolon end cell row cell o u t p u t text end text Y semicolon end cell row cell a s s i g n text end text Y equals S ? I 1 colon I 0 text end text semicolon end cell row cell e n d m o d u l e end cell end table

5.4.4 Dataflow Model of 4 × 1 Mux and Testbench

In the 4 × 1 multiplexer circuit, the input port has four inputs: I0, I1, I2, and I3, along with two select lines S0 and S1, whereas the output terminal has one output, Y. The main module name of a 4 × 1 multiplexer using dataflow modeling is mux_4x1_df. This main module is called mux_tb inside the testbench module of a 4 × 1 multiplexer. Since I0, I1, I2, I3, and S0, S1 are defined as input ports in the main module, these six variables are defined as reg in the testbench module. The output defined in the main module is Y, which is declared by wire in the testbench module.

//4 × 1 mux module

table attributes columnalign left end attributes row cell m o d u l e text end text m u x _ 4 x 1 left parenthesis y comma text end text I 0 comma I 1 comma I 2 comma I 3 comma text end text s 0 comma text end text s 1 right parenthesis semicolon end cell row cell i n p u t text end text I 0 comma text end text I 1 comma text end text I 2 comma text end text I 3 comma text end text s 0 comma text end text s 1 semicolon end cell row cell o u t p u t text end text y semicolon end cell row cell a s s i g n text end text Y text end text equals left parenthesis tilde s 0 & amp semicolon tilde s 1 & amp semicolon I 0 right parenthesis vertical line end cell row cell text end text left parenthesis s 0 & amp semicolon tilde s 1 & amp semicolon I 1 right parenthesis vertical line end cell row cell text end text left parenthesis tilde text end text s 0 & amp semicolon s 1 & amp semicolon I 2 right parenthesis vertical line end cell row cell text end text left parenthesis s 0 & amp semicolon s 1 & amp semicolon I 3 right parenthesis semicolon end cell row cell e n d m o d u l e end cell row cell text end text end cell end table

//4 × 1 mux module with a Conditional operator

table attributes columnalign left end attributes row cell m o d u l e text end text m u x _ 4 x 1 left parenthesis y comma text end text i 0 comma i 1 comma i 2 comma i 3 comma text end text s 0 comma text end text s 1 right parenthesis semicolon end cell row cell i n p u t text end text i 0 comma text end text i 1 comma text end text i 2 comma text end text i 3 comma text end text s 0 comma text end text s 1 semicolon end cell row cell o u t p u t text end text y semicolon end cell row cell a s s i g n text end text y equals s 1 ? left parenthesis s 0 text end text ? I 3 colon i 2 right parenthesis colon left parenthesis s 0 ? I 1 colon i 0 right parenthesis semicolon end cell row cell e n d m o d u l e end cell end table

5.4.5 Dataflow Model of 2-to-4 Decoder and Testbench

In the 2-to-4 decoder circuit, input port has two inputs A and B whereas the outport has four ports: D0, D1, D2, and D3. The main module name of the 2-to-4 decoder using dataflow modeling is Dec_2to4. This main module is called Dec_tb inside the testbench module of the 2-to-4 decoder. As A and B are defined as input ports in the main module, these two variables are defined as reg in the testbench module. The outputs defined in the main module are D0, D1, D2, and D3; those are declared by wire in the testbench module.

//2-to-4 Decoder module

module Dec_2to4(A,B,D0,D1,D2,D3);
input A,B;
output D0,D1,D2,D3;
assign D0=~A&~B;
assign D1=~A&B;
assign D2=A&~B;
assign D3=A&B;
endmodule

Review Questions

Q1 Write a Verilog code of a 4-bit full adder using dataflow modeling with testbench.

Q2 Implement a 4-bit adder/subtractor circuit using dataflow modeling of Verilog.

Q3 Design a 8 × 1 multiplexer using the 2 × 1 multiplexer using the dataflow model.

Q4 Write a Verilog code of a 3-to-8 decoder using the dataflow model and illustrate their testbench.

Q5 Write a Verilog code of 2-bit magnitude comparator using the dataflow model.

Multiple Choice Questions

Q1 Which operator is not used as dataflow modeling?

  1. +
  2. @
  3. &

Q2 In the testbench module, the output ports are declared as?

  1. reg
  2. wire
  3. o/p
  4. i/p

Q3 For the conditional operator, which keyword is used?

  1. >
  2. <
  3. ?
  4. &

Q4 Which of the following is the continuous assignment keyword?

  1. module
  2. assign
  3. initial
  4. finish

Q5 Which keyword is used for negation?

  1. *
  2. $
  3. ~

References

  1. [1] Weste, N.H.E., Harris, D.,, and Banerjee, A. (2004). CMOS VLSI Design: A Circuits and Systems Perspective, 3e. Upper Saddle River, NJ: ed. Pearson.
  2. [2] Bhaskar, J. (1999). A Verilog HDL Primer. Upper Saddle River, NJ: Pearson.
  3. [3] Mano, M.M. (1992). Computer System Architecture. Upper Saddle River, NJ: Pearson.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.105.89