Verilog Theory Complete
Verilog Theory Complete
Syntax :
1. Comments : It makes the code more readable.
// single line comment
/* */ multiline comment
2.Case Sensitive :
input x;
input X;
these both are different
3.keywords :
Special words used in verilog.
5. Identifier :
Used to identify an object like module, input or output port etc.
module ha
input a
6. Number Specification :
Syntax:
<size>'<base_format> <number>
base format : b -> binary
d -> decimal
h -> hexa
o -> octal
Example:
4'b 1001
wire
wor and wand insets an OR/AND gate at the connection
wor
wand
2.Register
a. reg : It can store value between assignments.
Example : reg a;
reg [2:0] a;
Arrays:
Arrays are multiple elements which can either be 1-bit or n-bits
syntax : <datatype> <name> <row width> <column width>
Example:
integer count [0:7];
0
1
2
3
4
5
6
7
count[2] = 0
integer count [2:0] [3:0]; // here each element is single bit wide
2,3 2,2 2,1 2,0
1,3 1,2 1,1 1,0
0,3 0,2 0,1 0,0
[0:2] [0:3]
0,0 0,1 0,2 0,3
1,0 1,1 1,2 1,3
2,0 2,1 2,2 2,3
count [0][1] =1
integer [2:0] count [2:0] [3:0]; //same as above. But here each element is 3 bits
wide.
count [0][0] = 3’b 001;
Operators in verilog :
1. Bitwise operators --> unary, binary
a. unary bitwise operators : has only 1 operand
& - and
| - or
~ - not
~& - nand
~| - nor
Example :
a = 4'b 1001
&a = (1)&(0)&(0)&(1)
= (0)&(0)&(1)
=0&1
= 1'b 0
Note : Unary bitwise are also called as reduction operators beacuse they reduce
the no.of bits.
2. Arithmetic operators:
+, -, *, /, %
3. Relational operators :
It compares 2 values and returns either 1 or 0.
a = 3'b 001 b = 3'b 100
a>b --> 0
a<b -->1
4.Equality operators :
It compares 2 values and returns either 1 or 0.
a = 3'b 001 b = 3'b 100
a == b --> 0
5.Logical operators :
&&, ||, !
a = 3'b 101 (non zero)
b = 3'b 110 (non zero)
a && b = 1
a || b = 1
!a = 0
6. Concatenation operators :
{}
It combines 2 or more data
a = 3’b 101
b = 4’ b 1001
{a,b} = 7’b 1011001
{b,a} = 7’b 1001101
7.Replication operators :
It is used to replicate the data.
Y = {3{2’b10}} = 6’b 101010
8. Conditional operators : ?:
Syntax:
Output = condition to be checked ? True value : False value
9. Shift operators :
>> right shift
<< left shift
Shifted bits are lost and zeroes are filled.
a = 4’b 1010
a <<2 = 1000
b = 4’1010
b >> 1 = 4’b 0101
module :
It is the building block in Verilog.
Syntax :
module <module_name> (port_list);
<port declaration>
<functionality>
endmodule
Ports:
They are used to send or receive data from outside world.
input : design module receives data from outside world using input port.
output : design module sends data from this port.
inout : design module sends out data from this port
2. Data flow : Digital systems are designed in terms of its function. It uses
Boolean expression and operators.
module ha(a,b,sum,carry);
input a,b;
output sum,carry;
xor g1(sum,a,b);
and g2(carry,a,b);
endmodule
module ha(a,b,sum,carry);
input a,b;
output sum,carry;
endmodule
3. Behavioural modelling :
module ha(a,b,sum,carry);
input a,b;
output reg sum,carry;
always@(a or b)begin
case({a,b})
2'b00:begin
sum = 1'b0;
carry = 1'b0;
end
2'b01:begin
sum = 1'b1;
carry = 1'b0;
end
2'b10:begin
sum = 1'b1;
carry = 1'b0;
end
2'b11:begin
sum = 1'b0;
carry = 1'b1;
end
endcase
end
endmodule
Structural modelling :
It refers to describing a design using module instances.
Structural model is a module that instantiates other modules and verilog
primitives.
Example : FA using HA
4 bit ripple carry adder
Procedural Blocks :
1. always block :
o All statements inside always block is executed sequentially.
o A single module can have multiple always blocks and they all run
simultaneously at time = 0.
o always block executes continuously at t = 0 and repeatedly
thereafter.
2. initial block :
o All statements inside initial block is executed sequentially.
o There can be more than 1 initial block in a single module. All
initial blocks execute simultaneously at time = 0.They are
executed parallely.
o Initial block executes only once, starting at t = 0.
Example :
initial block
reg v1, v2, v3, v4;
initial begin
v1 = 1;
#2 v2 = v1 +1;
v3 = v2 +1;
#2 v4 = v3 +1;
v1 = v4 +1;
#2 v2 = v1 +1;
v3 = v2 +1;
end
V1 V2 V3 V4
0 1 x x x
2 1 2 3 x
4 5 2 3 4
6 5 6 7 4
always block :
reg v1, v2, v3, v4;
always begin
v1 = 1;
#2 v2 = v1 +1;
v3 = v2 +1;
#2 v4 = v3 +1;
v1 = v4 +1;
#2 v2 = v1 +1;
v3 = v2 +1;
end
V1 V2 V3 V4
0 1 x x x
2 1 2 3 x
4 5 2 3 4
6 1 6 7 4
Blocking and non -blocking assignments :
Blocking : One statement blocks the execution of other statements until it is
executed.
“=” operator is used to specify blocking assignments.
Used for combinational logic design.
if else statement :
if (<expression>)
statement;
else
statement;
case statement : It will begin with a case and endcase. The expression within
parantheses will be evaluated exactly once and it is compared with the list of
alternatives in the order they are written and the statements for which the
alternative matches the given expression will be executed.
Example :
case({s1,s0})
2'b00:begin
y=i0;
end
2'b01:begin
y=i1;
end
2'b10:begin
y=i2;
end
2'b11:begin
y=i3;
end
endcase
for loop :
Syntax:
for (initialization; condition; increment)
begin
<body of for loop>
end
while loop :
Syntax :
Initialization
while (condition)
begin
//statements
end
repeat block :
It is a loop which repeats a block of code or a given statement a fixed no. of
times (the no. of times we specify).
Example :
repeat(20) begin
a_tb=$random;
b_tb=$random;
c_tb=$random;
#10;
end
Generate block :
It is used when the same operation is repeated for multiple times.
It starts with keyword generate and endgenerate.
Generate loop
genvar is used as a loop variable.
Example :
genvar I;
generate
for (I=0; I<=n-1; I=I+1)
assign out [I] =a[I] &b [I];
endgenerate
Generate conditional
generate
if (mod == 1)
upcount uc (clk,reset,count);
else if (mod == 0)
downcount dc (clk,reset,count);
endgenerate
Generate case
generate
case (n)
1 : upcount uc (clk,reset,count);
2 : downcount dc (clk,reset,count);
endcase
endgenerate
fork-join :
Parallel blocks are specified by keywords fork and join. All the statements start
at the time when the block was entered. Thus the order in which the statements
are written in the block is not important.
Example :
fork
x = 1’b 0; //starts at time 0
#5 y =1’b1; //starts at time 5
#10 z = 2’b 10 ; //starts at time 10
join
Note :
Reference Link of that code:
https://www.fpga4student.com/2017/02/verilog-code-for-d-flip-flop.html
Testbench :
Once the design block is completed, it must be tested to check its functionality.
So testbenches are written to check the functionality.
Structure :
include design_file_name
module testbench_name
//data type declaration
//instantiation of the design to be tested
//test pattern generation
//monitoring the response