Lab 6 Verilog Data Flow
Lab 6 Verilog Data Flow
Lab No. 6
Sample Output:
A B C Q
1 0 1 0
0 0 1 1
1 1 1 1
3. Implement the following circuit using gate level modeling.
Sample Output:
A B C D O
0 0 0 0 0
0 0 0 1 1
1 0 1 0 0
These statements display the values of A, B, C, and D in decimal, octal, hex and
binary number representation respectively. The directives %d, %o, %h, and %b are
sued to print values in decimal, octal, hexadecimal, and binary format respectively.
It is read as MSb down to LSb. If not specified the default value of range is taken as
one bit wide. A Similar syntax is used for declaring a variable of type register.
reg [MSB:LSB] r;
A Similar syntax is used for declaring a inputs or inputs of width more than 1 bit.
input [MSB:LSB] in;
Examples
The Verilog code in this example declares a 1 bit wide variable of type reg r, and
two 1bit wide variables of type wire w1, and w2, an 8 bit wide variable of type reg
vreg, and an 8-bite wide and 1Kbyte deep memory mem in Verilog.
Following are the declarations, a double forward slanted bar ‘//’ in Verilog is used
for comments:
Constants
Like variables, a constant in Verilog can be of any size and it can be written in
decimal, binary, octal or hexadecimal format. The decimal is the default format. As
the constant can be of any size, its size is usually written with a ‘ and d, b, o, or h to
specify decimal, binary, octal, or hexadecimal format respectively. A number 13
can be written in different format as shown in Table 2-2:
Table 2-2: Formats to represent constants
Digital Logic Design Lab (EE-2401) 4
Digital Logic Design Lab (EE-2401) 5
Introduction to Data Flow Modeling
This level of abstraction is higher than the gate level. Expressions, operands and
Operators characterize this level. Most of the operators used in Data Flow
modeling are common to software programmers but there are few more which
are very specific to HW design. In this level every expression starts with keyword
assign. Here is a simple example where two signals a and b are added to produce
c.
assign c = a + b;
-----------------------------------------------------------------------------------------------------
Operator for dataflow level modeling
Dataflow modeling is all about expressions and operators. List of operators for
dataflow modeling is as follows,
------------------------------------------------------------------------------------------------------
1. Arithmetic:
Binary: +, -, *, /, % (the modulus operator)
Unary: +, - (This is used to specify the sign)
Integer division truncates any fractional part
The result of a modulus operation takes the sign of the first operand
If any operand bit value is the unknown value x, then the entire result value
is x
Example
Code:
module arithmetic_operators;
initial begin
$display (" 5 + 10 = %d", 5 + 10);
$display (" 5 - 10 = %d", 5 - 10);
$display (" 10 - 5 = %d", 10 - 5);
Output:
5 + 10 = 15
5 - 10 = -5
10 - 5 = 5
10 * 5 = 50
10 / 5 = 2
10 / -5 = -2
10 % 3 = 1
+5 = 5
-5 = -5
5 power 2 =25
-------------------------------------------------------------------------------------------------------
2. Bitwise Arithmetic Operators:
Bitwise operators perform a bit wise operation on two operands. They take
each bit in one operand and perform the operation with the corresponding
bit in the other operand. If one operand is shorter than the other, it will be
extended on the left side with zeroes to match the length of the longer
operand.
Computations include unknown bits, in the following way:
~x = x
0&x = 0
1&x = x&x = x
1|x = 1
0|x = x|x = x
0^x = 1^x = x^x = x
0^~x = 1^~x = x^~x = x
When operands are of unequal bit length, the shorter operand is zero-filled
in the most significant bit positions.
initial begin
// Bit Wise Negation
$display (" ~4'b0001 = %b", (~4'b0001));
$display (" ~4'bx001 = %b", (~4'bx001));
// Bit Wise OR
$display (" 4'b0001 | 4'b1001 = %b", (4'b0001 | 4'b1001));
$display (" 4'b0001 | 4'bx001 = %b", (4'b0001 | 4'bx001));
// Bit Wise XOR
$display (" 4'b0001 ^ 4'b1001 = %b", (4'b0001 ^ 4'b1001));
$display (" 4'b0001 ^ 4'bx001 = %b", (4'b0001 ^ 4'bx001));
OUTPUT:
~4'b0001 = 1110
~4'bx001 = x110
4'b0001 & 4'b1001 = 0001
4'b1001 & 4'bx001 = x001
4'b0001 | 4'b1001 = 1001
4'b0001 | 4'bx001 = x001
4'b0001 ^ 4'b1001 = 1000
4'b0001 ^ 4'bx001 = x000
4'b0001 ~^ 4'b1001 = 0111
4'b0001 ~^ 4'bx001 = x111
--------------------------------------------------------------------------------------------------------
Digital Logic Design Lab (EE-2401) 8
3. Logical Operators
Expressions connected by && and || are evaluated from left to right
Evaluation stops as soon as the result is known
The result is a scalar value:
o 0 if the relation is false
o 1 if the relation is true
o x if any of the operands has x (unknown) bits
Example
CODE:
module logical_operators();
initial begin
// Logical AND
$display ("1'b1 && 1'b1 = %b", (1'b1 && 1'b1));
$display ("1'b1 && 1'b0 = %b", (1'b1 && 1'b0));
$display ("1'b1 && 1'bx = %b", (1'b1 && 1'bx));
// Logical OR
$display ("1'b1 || 1'b0 = %b", (1'b1 || 1'b0));
$display ("1'b0 || 1'b0 = %b", (1'b0 || 1'b0));
$display ("1'b0 || 1'bx = %b", (1'b0 || 1'bx));
// Logical Negation
$display ("! 1'b1 = %b", (! 1'b1));
$display ("! 1'b0 = %b", (! 1'b0));
end
endmodule
OUTPUT:
1'b1 && 1'b1 = 1
1'b1 && 1'b0 = 0
1'b1 && 1'bx = x
1'b1 || 1'b0 = 1
1'b0 || 1'b0 = 0
1'b0 || 1'bx = x
! 1'b1 = 0
! 1'b0 = 1
--------------------------------------------------------------------------------------------------------
Example
CODE:
module condition_operator;
reg enable,data1,data2;
wire out;
assign out=(enable)? data1:data2;
initial
begin
enable=1;
data1=1;
data2=0;
#20 enable=0;
data1=1;
data2=0;
#20 enable=1;
data1=0;
data2=1;
#20 enable=0;
data1=1;
data2=0;
end
endmodule
OUTPUT:
---------------------------------------------------------------------------------------------------------
OUTPUT:
{4'b1001,4'b10x1} = 100110x1
-----------------------------------------------------------------------------------------------------
6. Replication Operator
Replication operator is used to replicate a group of bits n times. Say you have a 4
bit variable and you want to replicate it 4 times to get a 16 bit variable: then we
can use the replication operator.
Operator Description
{n{m}} Replicate value m, n times
3. Prove that a + b = b + a using bitwise or logical operators. a and b are two 4 bit
numbers.
4. Make a two input calculator having functionalities of Addition, Subtraction,
Multiplication and division, using two conditional operators. The inputs are 8 bit decimal
values. Also take a select line as an input. Follow the following rules for the operations
of the calculator.
Selection Operation
0 (00) Addition
1 (01) Subtraction
2 (10) Multiplication
3 (11) Division.