REPORT Assignment 2 HDM
REPORT Assignment 2 HDM
Submitted By:
Shaily Garg
M.Tech MI
MEC2019010
IIIT Allahabad
Question 1: Design and implement a sequence detector to detect the sequence 1011. Use
behavioural modelling style and translate FSM exactly to its Verilog code. Perform the
same using different state coding schemes such as one hot assignment and binary
assignment (using gates/FF/Mux etc.) and compare results.
Solution 1:
//Verilog code using gates and D FF
module ques1(clk, reset, in, out);
input clk, reset, in;
output out;
wire [0:1] PS, NS;
wire t1, t2;
and D11(t1, ~in, PS[0]);
and D12(t2, in, PS[1], ~PS[0]);
or D1(NS[1], t1, t2);
and D0(NS[0], in, 1'b1);
and a1(out, in, PS[1], PS[0]);
dflop df1(reset, NS[1], PS[1], clk);
dflop df2(reset, NS[0], PS[0], clk);
endmodule
2
Indian Institute of Information Technology, Allahabad
//Verilog code using behavioural modeling
S0: begin
if(in==1)
NS<=S1;
else
NS<=S0;
end
S1: begin
if(in==0)
NS<=S2;
else
NS<=S1;
end
S2: begin
if(in==1)
NS<=S3;
else
NS<=S0;
end
S3: begin
if(in==1)
NS<=S1;
3
Indian Institute of Information Technology, Allahabad
else
NS<=S2;
end
default: NS<=S0;
endcase
end
always @(PS)
begin
case(PS)
S0: out<=0;
S1: out<=0;
S2: out<=0;
S3: out<=1;
default: out<=0;
endcase
end
endmodule
Solution 2:
//verilog code for three consecutive heads
module head(clk, reset, in, out);
input clk, reset;
input in; //INPUT=HEAD(1) or INPUT=TAIL(0)
output out;
reg [1:0]PS, NS;
parameter S0=0, S1=1, S2=2, S3=3;
parameter HEAD=1, TAIL=0;
initial
begin
clk=1'b1; reset=1;
end
always #5 clk=~clk;
initial
begin
#1 reset=0;
#2 in=1; #10 in=1;
#10 in=1; #10 in=0;
#10 in=0; #10 in=1; #10 in=1;
#10 in=1; #10 in=0;
end
endmodule
8
Indian Institute of Information Technology, Allahabad
9
Indian Institute of Information Technology, Allahabad
Question 3: Design a sequence detector if the in sequence “0110” (the leading ‘0’ can’t
use in more than one sequence). Implement the FSM using
1. Logic gates and D FF
2. MUX and D FF
Solution 3:
endmodule
//verilogcode for D FF
module dflop(reset, NS, PS, clk);
input clk, reset, NS;
output reg PS;
always @(posedge clk or posedge reset)
begin
if(reset) PS<=0;
else PS<=NS;
end
endmodule
mux2x1 m1(~PS[1], ~PS[0], 1'b1, t1); // Instantiation of mux2x1(sel, in1, in2, out)
mux2x1 m2(~PS[1], 1'b0, ~PS[0], t2);
mux2x1 m3(PS[1], 1'b0, ~PS[0], t3);
mux2x1 m4(PS[1], 1'b0, PS[0], t4);
mux2x1 m5(PS[1], PS[0], ~PS[0], t5);
mux4x1 m6({x,NS[2]}, t1, t2, t3, 1'b0, NS[0]); // Instantiation of mux4x1(sel0, sel1,
in1, in1, in2, in3, out)
mux4x1 m7({x,NS[2]}, 1'b0, 1'b0, t5, 1'b0, NS[1]);
mux4x1 m8({x,NS[2]}, t4, 1'b0, 1'b0, 1'b0, NS[2]);
mux4x1 m9({x,NS[2]}, 1'b0, t2, 1'b0, t2, y);
initial
begin
clk=1'b0; reset=1'b1; x=1'b0;
14
Indian Institute of Information Technology, Allahabad
#1 reset=1'b0;
#2 x=1'b1; #10 x=1'b0; #10 x=1'b1; #10 x=1'b1;
#10 x=1'b0; #10 x=1'b1; #10 x=1'b1; #10 x=1'b0;
#10 x=1'b1; #10 x=1'b1; #10 x=1'b1; #10 x=1'b0;
#10 x=1'b1; #10 x=1'b1; #10 x=1'b0; #10 x=1'b0;
#10 x=1'b1; #10 x=1'b1; #10 x=1'b0;
end
always #5 clk=~clk;
initial #200 $finish;
endmodule
15
Indian Institute of Information Technology, Allahabad
16
Indian Institute of Information Technology, Allahabad
17
Indian Institute of Information Technology, Allahabad
Question 4: Design a FSM to detect “10110” sequence, leading “10” can be taken in
more than one sequence. Develop Mealy Machine. Finally implement using D FF and
MUX. Develop a stimulus model for functional verification.
Solution 4:
initial
begin
clk=1'b0; reset=1'b1; x=1'b0;
#1 reset=1'b0;
#2 x=1'b1; #10 x=1'b0; #10 x=1'b1; #10 x=1'b1;
#10 x=1'b0; #10 x=1'b0;
#10 x=1'b1; #10 x=1'b1; #10 x=1'b0; #10 x=1'b1;
#10 x=1'b1; #10 x=1'b0; #10 x=1'b1;
#10 x=1'b1;
end
19
Indian Institute of Information Technology, Allahabad
always #5 clk=~clk;
initial #400 $finish;
endmodule
20
Indian Institute of Information Technology, Allahabad
21
Indian Institute of Information Technology, Allahabad
Question 5: Derive a Mealy type FSM that has an input x and an output y. The machine
is a sequence detector that produces y=1 when the previous two values of x were 00 or
11; otherwise y=0. Implement the FSM using the minimum number of gates and FF
(use don’t care). Develop a stimulus model for functional verification.
Solution 5:
S0: begin
out=0;
if(in==1)
NS<=S2;
else
NS<=S1;
end
S1: begin
out=in?0:1;
if(in==1)
NS<=S2;
22
Indian Institute of Information Technology, Allahabad
else
NS<=S1;
end
S2: begin
out=in?1:0;
if(in==1)
NS<=S2;
else
NS<=S1;
end
default: NS<=S0;
endcase
end
endmodule
endmodule
#1 reset=1'b0;
#2 in=1;
#10 in=0;
#10 in=1;
#10 in=0;
#10 in=1;
#10 in=0;
#10 in=0;
#10 in=1;
#10 in=0;
#10 in=0;
#10 in=1;
#10 in=1;
#10 in=0;
#10 in=1;
#10 in=1;
#10 $finish;
end
always #5 clk=~clk;
endmodule
24
Indian Institute of Information Technology, Allahabad
25
Indian Institute of Information Technology, Allahabad
26
Indian Institute of Information Technology, Allahabad
Question 6: A sequence of bits has odd parity if numbers of ‘1’ are odd. Design a circuit
that accepts string of bits and output a ‘1’, if parity thus far is odd; or else output a ‘0’.
Develop the FSM. Use structural level style to implement (MUX and FF). Develop a
stimulus model for functional verification.
Solution 6:
endmodule
29
Indian Institute of Information Technology, Allahabad
30
Indian Institute of Information Technology, Allahabad
31
Indian Institute of Information Technology, Allahabad
Question 7: Let us think about a special shifter circuit, which is capable of shifting by
more bit positions at a time; this circuit rotates the bit of input vector X[3:0] by a
specified number of bit positions, This circuit is known as Barrel shifter. Design a four
bit barrel shifter. Use structural level design style. Develop a stimulus model for
functional verification.
Solution 7:
33
Indian Institute of Information Technology, Allahabad
34
Indian Institute of Information Technology, Allahabad
35
Indian Institute of Information Technology, Allahabad