Professional Documents
Culture Documents
Fallsem2019-20 Ece2003 Eth Vl2019201000898 Reference Material I 10-Sep-2019 Cat 2 Digital Note Book Without Answer
Fallsem2019-20 Ece2003 Eth Vl2019201000898 Reference Material I 10-Sep-2019 Cat 2 Digital Note Book Without Answer
A multiplexer (or mux) is a device that selects one of several analog or digital input signals and forwards the
selected input into a single line. A multiplexer of 2n inputs has n select lines, which are used to select which
input line to send to the output. A multiplexer is also called a data selector
Block diagram of 2:1 MUX Truth Table K-Map & Gate Level Circuit:
Output
Expression:
Block diagram of 4:1 MUX Truth Table: K-Map & Gate Level Circuit:
Output
Expression:
Block diagram of 8:1 MUX Truth Table: K-Map & Gate Level Circuit:
Output
Expression:
Problem 1: Design 8:1 MUX using 4:1 MUX
Problem 2: Implement the Boolean function y(a, b, c) m 1,3,4,5,7 using 4:1 MUX
a) Keeping “a” as input & “b”, “c” as select line
Mapping and Simplification Logic Circuit
A Demultiplexer (or demux) is a device taking a single input signal and selecting one of many data-
output-lines, which is connected to the single input. A demultiplexer as a single-input, and multiple-
output switch.
A binary decoder is a combinational logic circuit that converts binary information from the n coded
inputs to a maximum of 2n unique outputs. They are used in a wide variety of applications, including
data demultiplexing, seven segment displays, and memory address decoding.
Problem 7: Implement the function using Decoder F1 m 1,2,3,4 , F 2 m 0,5,7 and F 3 m 1,6,7
Problem 8: Implement the divisible by 3 function with four variables using 4 x 16 Decoder
An encoder in digital electronics is a one-hot to binary converter. That is, if there are 2n input lines,
and at most only one of them will ever be high, the binary code of this 'hot' line is produced on the n-
bit output lines. For example, a 4-to-2 simple encoder takes 4 input bits and produces 2 output bits.
If two or more inputs are given at the same time, the input having the highest priority will
take precedence.
(a) with “I3” has highest priority and rest “I2”,”I1” and “I0”
(b) with “I0” has highest priority and rest “I1”,”I2” and “I3”
Problem 11: Draw the logic circuit of four bit adder with example
Problem 12: Draw the logic circuit of four bit subtractor with example
Problem 13: Draw the logic circuit of four bit adder/subtractor with example
Problem 12: When overflow will occur and how to find?
Problem 13: Draw the logic circuit of four bit adder/Subtractor with overflow
Binary Multiplier:
Problem 18: Design the logic circuit & write a Verilog code for four-bit by three-bit binary multiplier
4-bit Magnitude Comparator:
Problem: write a Verilog code for 4-bit magnitude comparator
Verilog HDL
Verilog means “Verification of Logic”. HDL is acronym of “Hardware Description Language”. There are so
many HDL, among them Verilog HDL and VHDL are familiar for digital circuit design.
Types of Modeling:
1.
2.
3.
module: It is a keyword, Verilog program starts with this keyword. Each module should have a separate
name & endmodule.
Ex: module and_gate(); // where “and_gate” is module name
………….;
endmodule
Port list: input & output signals/variables are declared in port list.
Ex: module and_gate(A,B,out); // where “and_gate” is module name
………….; // (A,B,out) is a port list of and_gate
endmodule
Note: There (In Port list) is a space between “input” and “A”. There is no space between “A” and “,”
Try this:
module and_gate(input A,input B,output out);
………….;
endmodule
17BEC0000
_12sa
S_$&ab
$sad
cAs$_123
Problem 1: Write a Verilog code for a 2-input AND gate in Structural modeling
Problem 2: Write a Verilog code for the following circuit in Gate level modeling
wire:
wire elements are used to connect input and output ports of a module instantiation together with some other
element in your design.
wire elements are used as inputs and outputs within an actual module declaration
wire elements must be driven by something, and cannot store a value without being driven.
wire elements cannot be used as the left-hand side of an = or <= sign in an always@ block.
wire elements are the only legal type on the left-hand side of an assign statement.
wire elements are a stateless way of connecting two pieces in a Verilog-based design.
wire elements can only be used to model combinational logic.
Try this:
module my_module (a, b, c); Draw the logic diagram If A = 01 & B = 11 then c?
input [1:0]a, b;
output [1:0]c;
and x[1:0](c,a,b);
endmodule
One module cannot be defined in another module: YES / NO
module ha(input a,b, output sum,carry); module ha(input a,b, output sum,carry);
xor x1(sum,a,b); xor x1(sum,a,b);
and x2(carry,a,b); and x2(carry,a,b);
endmodule endmodule
Note: Declaration port variable order can be Note: Declaration port variable order should not be
changed. changed.
Problem 4: Write a Verilog code for a 4-input binary adder gate in Structural modeling
// Half Adder
module ha(input a,b, output sum,carry);
xor x1(sum,a,b);
and x2(carry,a,b);
endmodule
//Full Adder
module fa(input a,b,cin,output sum,carry);
wire s1,c1,c2;
ha x1(a,b,s1,c1);
ha x2(s1,cin,sum,c2);
or x3(carry,c1,c2);
endmodule
//4-bit Adder
module fourbit_adder(a,b, sum,carry);
input [3:0]a,b;
input cin;
output [3:0]sum;
output carry;
wire Carryc1,Carryc2,Carryc3;
fa x1(a[0],b[0],cin,sum[0],Carryc1);
fa x2(a[1],b[1],Carryc1,sum[1],Carryc2);
fa x1(a[2],b[2],Carryc2,sum[2],Carryc3);
fa x2(a[3],b[3],Carryc3,sum[3],carry);
endmodule
Try this:
module my_module (a, b, c);
input a, b;
output c;
and x(c,a,b);
endmodule
Design the circuit & write the Verilog code (structural / Gate level modeling) for obtaining the above waveform
and give the truth table, Canonical and Standard form of Boolean expression for the above waveform with X,Y
and Z as inputs and P, C as outputs
Data flow modeling
Operators and assign (it is a keyword) are used (not gate primitives).
Arithmetic Operators:
There are two types of arithmetic operators: binary and unary
Binary operators(Arithmetic): +, -, *, /, %, **
Try this: a=4‟b0011, b=4‟b0100, c=6, d=4 and e=2.
a+b
b-a
a*b
c/d
d ** e
3%2
16 % 4
-7 % 2
7 % -2
A <= B
A>B
Y >= X
Y< Z
A = 4‟b1010, B = 4‟b0000
Function Operator name Result Comment (SIZE)
A|B
A || B
A^B
4'b0z01 ^ 4'bx001
4'b0z01 & 4'bx001
“assign” (Continuous assignment) : The continuous assignment is typically used with wires and other
structures that do not have memory. A continuous assignment is happening continuously and in
parallel to all other computational tasks. The order of continuous assignments, or their location in
the code do not matter.
“<=” (Non-blocking assignment): Non-blocking assignments are assignments that occur once, but
which can all happen at the same time. These assignments are typically used with registers and
integer data types, and other data types with memory.
“=” (Blocking assignment): Blocking assignments are also used with registers and integers (and
other memory data types). Blocking assignments occur sequentially, and the code after the
assignment will not execute until the assignment has occurred.
Problem 5: Write a Verilog code for a 2-input AND gate in dataflow modeling
Problem 6: Write a Verilog code for the following circuit in Dataflow modeling
Problem 7: Write a Verilog code for a 4-input binary adder gate in Dataflow modeling
a) without full adder module instantiation: b) with full adder module instantiation:
Output Comment
4'bx001 === 4'bx001
4'bx0x1 === 4'bx001
4'bz0x1 === 4'bz001
4'bx0x1 !== 4'bx001
4'bx0x1 !== 4'bx001
5 == 10
5 == 5
5 != 5
5 != 6
4’b1xxz == 4’b1xxx
Arithmetic right shift (>>>). Shift right specified number of bits, fill with value of sign bit if
expression is signed, othewise fill with zero.
Arithmetic right shift (<<<). Shift left specified number of bits, fill with value of sign bit if expression
is signed, othewise fill with zero.
Concatenation Operator: { , }
y = { 4{a} } 4‟b1111
y = { 4{a}, 2{b} } 8‟b11110000
y = { 4{a}, 2{b}, c } 10‟b1111000010
Conditional Operator: ( ? : )
module logic( input sel, d_in1, d_in0, output d_out ); Comment (Name of the Circuit):
assign d_out = sel ? d_in1 : d_in0;
endmodule
4:1 MUX using conditional Operator 8:1 MUX using conditional Operator
(Unary Operator)
A = 4‟b1010, B = 4‟b0000
Function Operator name Result Comment (SIZE)
&A Reduction AND 0 One bit size (i.e 1 & 0 & 1 & 0)
^A Reduction XOR 0
Problem: Write a Verilog code for 4 bit even parity checker and generator
With Reduction operator Without Reduction operator
Behavioural modeling:
Behavioral models in Verilog contain procedural statements, which control the simulation and
manipulate variables of the data types. These all statements are contained within the procedures. Each
of the procedure has an activity flow associated with it.
During simulation of behavioral model, all the flows defined by the „always‟ and „initial‟ statements
start together at simulation time „zero‟. The initial statements are executed once, and the always
statements are executed repetitively.
module behave;
reg a,b;
initial
begin
a = 1’b1;
b = 1’b0;
end
always
begin
#50 a = ~a;
end
always
begin
#100 b = ~b;
end
endmodule
Procedural assignments:
module block_nonblock();
reg a, b, c, d , e, f ;
// Blocking assignments
initial begin
a = #10 1'b1;// The simulator assigns 1 to a at time 10
b = #20 1'b0;// The simulator assigns 0 to b at time 30
c = #40 1'b1;// The simulator assigns 1 to c at time 70
end
// Nonblocking assignments
initial begin
d <= #10 1'b1;// The simulator assigns 1 to d at time 10
e <= #20 1'b0;// The simulator assigns 0 to e at time 20
f <= #40 1'b1;// The simulator assigns 1 to f at time 40
end
endmodule
Conditions:
if ( ) if ( ) if ( ) if ( )
……. begin begin begin
else if ( ) if ( ) ……
….. ….. ….. end
else( ) end
….. else else if ( )
end ….. begin
else …..
….. end
else if ( )
begin
…..
end
else( )
end
else
…..
input[7:0] a;
input[7:0] b;
input addnsub;
output[8:0] result;
reg[8:0] result;
2 x 4 Decoder 4 x 2 Encoder
reg y;
always @ (a or b or c or d or sel)
case (sel)
0 : y = a;
1 : y = b;
2 : y = c;
3 : y = d;
2'bxx,2'bx0,2'bx1,2'b0x,2'b1x,
2'bzz,2'bz0,2'bz1,2'b0z,2'b1z : $display("Error in SEL");
endcase
endmodule
Special versions of the case statement allow the x ad z logic values to be used as "don't care":
endmodule
endmodule
endmodule
Example : 8 x 3 priority encoder using casex
In Transcript window
Test bench for Four bit Adder: Output of Test bench of Four bit
adder:
module fourbit_ adder_tb;
reg [3:0]a,b;
reg cin;
wire cout;
wire [3:0]sum;
integer i,j,k;
fourbit_adder s1(a,b,cin,cout,sum);
initial
begin
a=0;b=0;cin=0;
for(i=0;i<16;i=i+1)
for(j=0;j<16;j=j+1)
for(k=0;k<2;k=k+1)
begin
a=i;
b=j;
cin=k;
$monitor("a=%d,b=%d,cout=%d,sum=%d",a,b,c4,sum);
#100;
end
end
endmodule
Problem: write a Verilog code for 2 x 4 decoder & test bench for 2 x 4 decoder
4 x 4 Binary Multiplier: