Verilog.pptx1
Verilog.pptx1
(Combinational Circuits)
“A common approach is to design each IC chip, using an
HDL, and then verify system functionality via simulation”
Introduction
Digital circuit design evolved rapidly over the last 25 years.
The earliest digital circuits designed with vacuum tubes and
transistors.
With the advent of LSI (Large Scale Integration), designers could put
1000 of gates on a single chip.
At this point, design processes started getting very complicated, and
designers felt the need to automate these processes.
Design
entity
functional
verification
Logical
synthesis
Timing
verification
& fault
simulation
In any design Specifications are written first.
Specifications describe abstractly the functionality, interface,
and overall architecture of the digital circuit
Behavioral description is then created to analyze the
design in terms of functionality, performance, compliance to standards,
and other high-level issues, can be written with HDLs.
Input a, b;
Signal declaring statements
Output s, cout;
Ex: y= &x
if x =1010 then y = (1 & 0 & 1 & 0)
Verilog Relation operators :
== Equality
!= Inequality
=== Equality inclusive
!== Inclusive inequality
< less-than
<= less-than or equal
> greater-than
>= greater-than or equal
{ } concantation
? ! conditional
Verilog arithmetic operators :
+A+B
-A-B
*A*B
/A/B
%A%B
** A ** B ---Exponential operator
Ex : integer no_bits;
declares no_bits as an integer
“Registers declared as data type reg store values as unsigned
quantities, whereas integers store values as signed quantities”
(v) Real (floating point) are declared by the pre-
defined keyword real
real weight
(vi) parameters represent global constants
Ex : module comp_ckt(x,y,xgty,xlty,xeqy)
parameter N =3;
input [N:0]x,y;
(vii) Arrays parameter N = 3;
parameter M = 3;
reg [M:0] b [0:N];
(viii) String Strings can be stored in reg. The width of the
register variables must be large enough to hold the string.
Each character in the string takes up 8 bits (1 byte)
reg [8*18:1] string-value; //Declare a variable that is l8 bytes
wide initial
string-value = "Hello Verilog World"; / / String can be stored
/ / in variable
Types of descriptions
input a,b;
output y1,y2,y3,y4,y5,y6,y7;
o/p first then i/ps
not (y1,a);
and (y2,a,b);
or (y3,a,b);
nand (y4,a,b);
nor (y5,a,b);
xor (y6,a,b);
xnor (y7,a,b);
endmodule
Verilog code in dataflow modeling for all the basic gates
module dgates(a, b, y1,y2,y3,y4,y5,y6,y7);
input a,b;
output y1,y2,y3,y4,y5,y6,y7;
assign y1 = ~a;
assign y2 = a & b;
assign y3 = a | b;
assign y4 = ~(a & b);
assign y5 = ~(a | b);
assign y6 = a ^ b;
assign y7 = ~(a ^ b);
endmodule
Verilog code in behavirol modeling for all the basic gates
module bgates(a, b, y1,y2,y3,y4,y5,y6,y7);
input a,b;
output y1,y2,y3,y4,y5,y6,y7;
always @(a,b)
begin
y1 = ~a;
y2 = a & b;
y3 = a | b;
y4 = ~(a & b);
y5 = ~(a | b);
y6 = a ^ b;
y7 = ~(a ^ b);
end
endmodule
Verilog code in dataflow modeling for all the vectored basic gates
module vdgates(a, b, y1,y2,y3,y4,y5,y6,y7);
a[0]
assign y1 = ~a; C[0]
b[0]
assign y2 = a & b;
assign y3 = a | b;
a[1]
assign y4 = ~(a & b); C[1]
b[1]
assign y5 = ~(a | b);
assign y6 = a ^ b; a[2] C[2]
assign y7 = ~(a ^ b); b[2]
endmodule a[3]
C[3]
b[3]
Verilog code in structural modeling for the given logic diagram
(gate level description)
A w1
G1
B G3 D
C G2 E
A w1
C
B
module logic_dia(A,B,C);
input A,B;
output C;
reg w1,C;
always @(*)
begin
w1 = a&b;
C = ~w1;
end
endmodule
Verilog code to realize the following code
(i) y1 = AB + C’
(ii) y2 = A + BC + B’D
(iii) y3 = B’C + BC’D’
module bexp(A,B,C,D,y1,y2,y3);
input A,B,C,D;
output y1,y2,y3;
endmodule
Verilog code to realize the user defined primitive
(Truth table) D = Σ(0,2,4,6,7)
primitive UDP_02467(D,A,B,C);
output D;
input A,B,C;
table
A B C : D
0 0 0 : 1
0 0 1 : 0
0 1 0 : 1
0 1 1 : 0
1 0 0 : 1
1 0 1 : 0
1 1 0 : 1
1 1 1 : 1
endtable
endprimitive
Verilog code to realize the instantiate primitive
A E
UDP_02467
B
C
F
D
Module block_dia(E,F,A,B,C);
input A,B,C;
output E,F;
UDP_02467 (E,A,B,C);
and (F,E,D);
endmodule
Verilog code to realize half adder in structural modeling
endmodule
Verilog code to realize half adder in dataflow modeling
endmodule
Verilog code to realize half adder in behavioral modeling
always @(a,b)
begin
sum = a^b;
cout = a&b;
end
endmodule
Verilog code to realize full adder in structural modeling
module fa(a, b, cin, sum, cout);
a s1
b sum input a,b,cin;
cin output sum, cout;
c1 wire s1,c1,c2,c3;
endmodule
Verilog code to realize full adder in dataflow modeling
module fa(a, b, cin, sum, cout);
a s1
b sum input a,b,cin;
cin output sum, cout;
c1 wire s1,c1,c2,c3;
endmodule
Verilog code to realize full adder in behaviral modeling
module fa(a, b, cin, sum, cout);
a s1
b sum input a,b,cin;
cin output sum, cout;
c1 reg s1,c1,c2,c3,sum,cout;
a s1
b sum
c1 c2
cout
cin
module fa(a, b, cin, sum, cout);
input a,b,cin;
output sum, cout;
wire s1,c1,c2;
xor (s1,a,b);
and (c1,a,b);
xor (sum,s1,cin);
and (c2,s1,cin);
or (c ,c1,c2);
Verilog code to realize full adder using two half adder in
dataflow modeling
a s1
b sum
c1 c2
cout
cin
module fa(a, b, cin, sum, cout);
input a,b,cin;
output sum, cout;
wire s1,c1,c2;
assign s1 = a^b;
assign c1 = a&b;
assign sum = s1^cin);
assign c2 = s1&cin;
assign c = c1&c2;
Verilog code to realize full adder using 2-half adder in structural
modeling based on instantiation
endmodule
Verilog code to realize 4-bit full adder (cascading of 4 FAs) in structural
modeling based on instantiationModule full_adder(a, b, cin, sum, cout);
input a, b, cin;
Module 4-bitfa(a, b, cin, sum, cout); output sum, cout;
input[3:0] a, b;
input cin;
output [3:0]sum;
output cout;
endmodule
Verilog code to realize full adder using 2-half adder in mixed modeling
Module fa-mixed(a, b, cin, sum, cout);
input a,b,cin;
output sum,cout;
wire s1,c1,c2;
reg cout;
xor x1(s1,a,b);
and a1(c1,a,b);
always @(c1,c2)
begin
cout = c1 | c2;
Verilog code to realize 8-bit full adder (cascading of two 4-bit FAs) in
structural modeling based on instantiation
Module 8-bitfa(x, y, cin, cout,sum);
input[7:0]x,y;
input cin;
output[7:0]sum;
output cout; instantiate
wire cout1;
wire c1,c2,c3;
full_adder fa_1(x[0],y[0],cin,c1,sum[0]);
full_adder fa_2(x[1],y[1],c1,c2,sum[1]);
full_adder fa_3(x[2],y[2],c2,c3,sum[2]);
full_adder fa_4(x[3],y[3],c3,cout,sum[3]);
endmodule
module full_adder(x, y, cin, cout, sum);
input x,y,cin;
output sum, cout; Module half_adder(x, y, c, s);
parameter m = 3;
input [m-1:0] a;
input [m-1:0] b;
input Cin;
output reg [m-1:0] s;
output reg cout;
always @ (*)
{ Cout, S} = a + b + Cin; //Concatenation operator
endmodule
Conditional operator
Ex: 2 to 1 MUX
endmodule
module 4bc( input[3:0]A, input[3:0]B, output E, G, L);
endmodule
Verilog code to realize n-bit comparator in data flow modeling Using
conditional operator
endmodule
Verilog code to realize n-bit comparator in behavioral modeling
module nbc (x, y, g, e, l);
parameter m = 2;
input [m-1:0]x,y;
output reg g, e, l;
always @ (*)
begin
if (x > y) else
begin begin
g=1; g=0;
e=0; e=1;
l=0; l=0;
end end
if (x < y) end
begin endmodule
g=0;
e=0;
l=1;
Verilog code to realize 4-bit magnitude comparator in dataflow
modeling
input [3:0] a, b;
input Iagtb, Ialtb, Iaeqb;
output Oagtb, Oaltb, Oaeqb ;
endmodule
Verilog code to realize binary to grey code and vice-versa conveter in
dataflow modeling
module bin2grey( input[3:0]bin, output[3:0]G); G3 = B3
assign G[3] = bin[3]; G2 = B3 ʘ B2
assign G[2] = bin[3] ^ bin[2]; G1 = B2 ʘ B1
assign G[1] = bin[2] ^ bin[1];
assign G[0] = bin[1] ^ bin[0];
G0 = B1 ʘ B0
endmodule
module grey2bin( input[3:0]G, output[3:0]bin);
B3 = G3 assign bin[3] = G[3];
B2 = G3 ʘ G2 assign bin[2] = G[3] ^ G[2];
assign bin[1] = G[3] ^ G[2] ^ G[1];
B1 = B2 ʘ G1 assign bin[0] = G[3] ^ G[2] ^ G[1] ^ G[0];
B0 = B1 ʘ G0
endmodule
Verilog code to realize n-bit binary to grey code and vice-versa
conveter in behavioral modeling
module nb2g( x,y);
parameter n = 4;
input [n-1:0]x;
output reg[ n-1:0]y;
integer i;
always @ (x)
begin
y[n-1] = x[n-1];
for(i=0; i < n; i++)
y[n-1-i] = x[n-i] ^ x[n-1-i];
end
endmodule
Verilog code to realize BCD to excess-3 converter in behavioral
modeling
E3 = B2B0 + B2B1 + B3 E2 = B2’(B1+B0)+B2B1’B0’
= B2(B0+B1)+B3
E1 = B1’B0’ + B1B0 E0 = B0’
=(B1 ʘ B0)’
module bcd2ex3( input[3:0]b, output reg[3:0]e);
always @(b)
begin
e[3] = b[3] | (b[2] & b[0]) | (b[2] & b[1]);
e[2] = (b[2] & (~b[1]) & (~b[0])) | (~b[2] & b[0]) | (~b[2] & b[1]);
e[1] = ~(b[1] ^ b[0]);
e[0] = ~b[0];
end
endmodule
Verilog code to realize excess-3 to BCD converter in behavioral
modeling
B3 = E3E2 + E3E2E0
B2 = E0(E1E2+E1’E3)+E2’E0’
=E3(E1 E0+E2)
B1 = E1’E0 + E1E0’ B0 =E0’
= E1 ʘ E0
always @(e)
begin
b[3] = (e[3] & (e[2]) | (e[3]) & e[1] & e[2]);
b[2] = ((~e[2] & e[1]) | (e[2] & e[1] & e[0]) | (~e[2] & (~e[1]));
b[1] = e[1] ^ e[0];
b[0] = ~e[0];
end
endmodule
Verilog code to realize 9-bit generator using a reduction operator
oP
X[8:0]
eP
module pg(x, ep, op);
input [8:0] x;
output ep, op;
endmodule
Verilog code to realize an all-bit zero/one detector using a reduction
operator
zero
X[7:0] one
module nzo(x, zero, one);
input [7:0] x;
output zero, one;
endmodule
Verilog code to realize shift operators
Module logical_shift; Module arithmetic_shift;
reg[3:0] start, result; reg signed [3:0] start, result;
initial initial
begin begin
start = 4’b0001; //0001 start = 4’b1000; //1000
result = (start <<2); //0100 result = (start >>>2); //1110
end end
endmodule endmodule
Module arithmetic_shift(x,y,z);
input signed [3:0] x;
output [3:0] y;
output signed [3:0] z;
endmodule
Verilog code to realize 2:1 MUX in dataflow modeling using
conditional operator
module 2t1mux(y, S0, I0, I1);
assign y = S0 ? I1 : I0;
endmodule
Verilog code to realize 2:1 MUX in behavioral modeling using case
statement
w6
Verilog code to realize 4:1 MUX in structural modeling
module 4t1mux(y, S0, S1, I0, I1 , I2, I3);
endmodule
Verilog code to realize 4:1 MUX in dataflow modeling
module 4t1mux(y, S0, S1, I0, I1 , I2, I3);
assign w1 = ~S1;
assign w2 = ~S0;
assign w3 = w1 & w2 & I0;
assign w4 = w1 & S0 & I1;
assign w5 = S1 & w2 & I2;
assign w6 = S1 & S0 & I3;
or y = w3 | w4 | w5 | w6;
endmodule
Verilog code to realize 4:1 MUX in dataflow modeling
Z= S1’S0’I0+S1’S0I1 +S1S0’I2+S1S0I3
module 4t1mux(y, S, I);
input[1:0]S;
input [3:0] I;
output y;
endmodule
Verilog code to realize 4:1 MUX in dataflow modeling using
conditional operator
endmodule
Verilog code to realize 4:1 MUX in behavioral modeling using case
statement
always @(I, S)
begin
case (S)
2’b00 : y = I[0];
2’b01 : Y = I[1];
2’b10 : y = I[2];
2’b11 : y = I[3];
endcase
end
endmodule
Verilog code to realize n:1 MUX in behavioral modeling
module nt1mux(in, sel, y);
parameter m=8;
parameter n=3;
input[m-1:0] in;
input[n-1:0] sel;
output reg y;
integer I;
always @(*)
begin
y=1’b0;
for(i=0;i<m;i=i+1)
begin
if (sel==i) y == in[i];
end
end
endmodule
Verilog code to realize 1:4 dMUX in behavioral modeling using case
statement
always @(i, s)
begin
y=0;
case (s)
2’b00 : y[0] = i;
2’b01 : Y[1] = i;
2’b10 : y[2] = i;
2’b11 : y[3] = i;
endcase
end
endmodule
Verilog code to realize 1:4 dMUX in behavioral modeling
always @(i, s)
begin
y[0] = (~s[0]) & (~s[1]) & i;
y[0] = (~s[0]) & (s[1]) & i;
y[0] = (s[0]) & (~s[1]) & i;
y[0] = (s[0]) & (s[1]) & i;
end
endmodule
Verilog code to realize 2 to 4 line decoder in dataflow modeling
endmodule
Verilog code to realize 3 to 8 line decoder in behavioral modeling
Using case statement
module 3t8_decoder(input[2:0] x, output reg[7:0] y);
always @(x)
begin
case (x)
3’b000 : y[0] = 1;
3’b001 : Y[1] = 1;
3’b010 : y[2] = 1;
3’b011 : y[3] = 1;
3’b100 : y[4] = 1;
3’b101 : y[5] = 1;
3’b110 : y[6] = 1;
3’b111 : y[7] = 1;
endcase
end
endmodule
Verilog code to realize 3 to 8 line decoder in behavioral modeling
Using if-else statement
module 3t8_decoder(input[2:0] x, output reg[7:0] y);
always @(x)
begin
if (x == 3’b000) y = 8’b00000001;
else if (x == 3’b001) y = 8’b00000010;
else if (x == 3’b010) y = 8’b00000100;
else if (x == 3’b011) y = 8’b00001000;
else if (x == 3’b100) y = 8’b00010000;
else if (x == 3’b101) y = 8’b00100000;
else if (x == 3’b110) y = 8’b01000000;
else if (x == 3’b111) y = 8’b10000000;
end
endmodule
Verilog code to realize 8 to 3 line encoder in behavioral modeling
Without priority
module 8t3_encoder(input[7:0] x, output reg[2:0] y);
always @(x)
begin
y[0] = x[7] | x[5] | x[3] | x[1];
y[1] = x[7] | x[6] | x[3] | x[2];
y[2] = x[7] | x[6] | x[5] | x[4];
end
endmodule
B2 = D4 + D5 + D6 + D7; B1 = D2 + D3 + D6 + D7; B0 = D1 + D3 + D5 + D7
Verilog code to realize 8 to 3 line encoder in behavioral modeling
With priority using case statement
module 8t3_encoder(input[7:0] x, output reg[2:0] y);
always @(x)
begin
case (x)
8’b1xxxxxxx : y = 3’b111;
8’b01xxxxxx : Y = 3’b110;
8’b001xxxxx : y = 3’b101;
3’b0001xxxx : y = 3’b100;
3’b00001xxx : y = 3’b011;
3’b000001xx : y = 3’b010;
3’b0000001x : y = 3’b001;
3’b00000001 : y = 3’b000;
endcase
end
endmodule