Lab 2
Lab 2
LAB-2 (Tutorial)
IMPLEMENTATION OF MOORE AND MELAY
FSM MACHINE
2.1 AIM
This lab session will deal with the construction of synchronous sequential circuits using Hardware
Description Language (HDL) and Xilinx ISE 14.2i to analyse its behaviour. Sequential circuits are often
used to control the operation of physical systems. We will introduce the techniques for designing such
circuits by means of a simple example and will also learn to write he HDL code for the given example.
2.2 OBJECTIVE
After completing this lab, you will be able to:
• Design a Moore and Melay FSM machine.
• Analyze the Sequential behavior of digital circuits.
ICD & RFCS2 , FAST-NU, ISB (http://isb.nu.edu.pk/rfcs2/index.htm) 1/22
Very large scale integration(VLSI) Manual Fall 2021
2.3 Theory
2.3.1 Introduction
The general class of circuits in which the outputs depend on the past behaviour of the circuit, as well as on
the present values of inputs. They are called sequential circuits. In most cases a clock signal is used to control
the operation of a sequential circuit; such a circuit is called a synchronous sequential circuit. The alternative,
in which no clock signal is used, is called an asynchronous sequential circuit. Synchronous circuits are easier
to design and are used in a vast majority of practical applications.
Synchronous sequential circuits are realized using combinational logic and one or more flip-flops. The
general structure of such a circuit is shown in Figure 8.1. The circuit has a set of primary inputs, W, and
produces a set of outputs, Z. The stored values in the flip-flops are referred to as the state, Q, of the circuit.
Under control of the clock signal, the flip-flops change their state as determined by the combinational logic
that feeds the inputs of these flip-flops. Thus the circuit moves from one state to another. To ensure that only
one transition from one state to another takes place during one clock cycle, the flip-flops have to be of the
edge-triggered type. They can be triggered either by the positive (0 to 1 transition) or by the negative (1 to 0
transition) edge of the clock. We will use the term active clock edge to refer to the clock edge that causes the
change in state.
The combinational logic that provides the input signals to the flip-flops has two sources: the primary
inputs, W, and the present (current) state of the flip-flops, Q. Thus changes in state depend on both the
present state and the values of the primary inputs.
Figure 2.1 indicates that the outputs of the sequential circuit are generated by another combinational circuit,
such that the outputs are a function of the present state of the flip-flops and of the primary inputs.
Although the outputs always depend on the present state, they do not necessarily have to depend directly on
the primary inputs. Thus the connection shown in blue in the figure may or may not exist. To distinguish
between these two possibilities, it is customary to say that sequential circuits whose outputs depend only on
the state of the circuit are of Moore type, while those whose outputs depend on both the state and the primary
inputs are of Mealy type. These names are in honour of Edward Moore and George Mealy, who investigated
the behaviour of such circuits in the 1950s.
(a) (b)
The state table in Figure 9.3 defines the 2 states in terms of letters A and B. When implemented in a
logic circuit, each state is represented by a particular valuation (combination of values) of state
variables. Each state variable may be implemented in the form of a flip-flop. Since two states have
to be realized, it is sufficient to use 1 state variables. Let these variables be y .
PRE-LAB:
1. How would you decide the no of flip-flops needed to implement the machine?
Answer
2. Do we have choices of selecting different types of flip-flops?
Answer
Answer
Equipment:
Computer with ISE 14.7 Software Specifications:
HP Computer i7 Processor – 2.8 GHz, 2GB RAM, 160 GB Hard Disk
Software: ISE
2.6 Procedure:
1. Double click the project navigator and select the option File-New project.
ICD & RFCS2 , FAST-NU, ISB (http://isb.nu.edu.pk/rfcs2/index.htm) 7/22
Very large scale integration(VLSI) Manual Fall 2021
1. Write verilog code for Moore FSM which will detect the code 010 in order to unlock the
door.
Verilog Code
Test Bench
Waveform
2. Simulate the above code and verify your truth table by giving different inputs
Verilog Code
Test Bench
Waveform
Verilog code
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 08:59:53 11/05/2021
// Design Name:
// Module Name: mooremachine
// Project Name:
// Target Devices:
// Tool versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
ICD & RFCS2 , FAST-NU, ISB (http://isb.nu.edu.pk/rfcs2/index.htm) 13/22
Very large scale integration(VLSI) Manual Fall 2021
//
//////////////////////////////////////////////////////////////////////////////////
module mooremachine( clk, reset, seq_in, detector_out );
input clk;
input reset;
input seq_in;
output reg detector_out;
s0: begin
if (seq_in == 1)
next_state = s1;
else
next_state = s0;
end
s1: begin
if (seq_in == 1)
next_state = s2;
else
next_state = s0;
end
s2: begin
if (seq_in == 1)
next_state = s3;
else
next_state = s0;
end
s3: begin
if (seq_in == 1)
next_state = s1;
else
next_state = s0;
end
always @(current_state)
begin
case(current_state)
s0 : detector_out = 0;
s1 : detector_out = 0;
s2 : detector_out = 0;
s3 : detector_out = 1;
default : detector_out = 0;
endcase
end
endmodule
Test Bench
ICD & RFCS2 , FAST-NU, ISB (http://isb.nu.edu.pk/rfcs2/index.htm) 15/22
Very large scale integration(VLSI) Manual Fall 2021
////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 10:01:45 11/05/2021
// Design Name: mooremachine
// Module Name: C:/Users/ICD_Lab/newmoore/mooretestbench.v
// Project Name: newmoore
// Target Device:
// Tool versions:
// Description:
//
// Verilog Test Fixture created by ISE for module: mooremachine
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
////////////////////////////////////////////////////////////////////////////////
module mooretestbench;
// Inputs
reg clk;
reg reset;
reg seq_in;
// Outputs
wire detector_out;
.reset(reset),
.seq_in(seq_in),
.detector_out(detector_out)
);
initial begin
clk = 0;
forever #5 clk = ~clk;
end
initial begin
seq_in = 0;
reset = 1;
#30
reset = 0;
#40
seq_in = 1;
#10
seq_in = 1;
#10
seq_in = 1;
end
endmodule
Plot
2. Smiling Snail:
Alyssa P. Hacker has a snail that crawls down a paper tape with 1’s and 0’s on it. The snail
smiles whenever the last four digits it has crawled over are 1101. Draw the state transition
diagram, Draw the state transition diagram of Melay FSM and write their verilog also create
their testbench in order to verify their functionality and show the waveform with
explanation.
MELAY MACHINE
VERILOG CODE
);
reg[1:0] state;
ICD & RFCS2 , FAST-NU, ISB (http://isb.nu.edu.pk/rfcs2/index.htm) 19/22
Very large scale integration(VLSI) Manual Fall 2021
default : state = s0 ;
endcase
end
endmodule
TESTBENCH
////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 01:56:33 11/07/2021
// Design Name: Melaymachine
// Module Name: C:/Users/ICD_Lab/melay/TestMELAY.v
// Project Name: melay
// Target Device:
// Tool versions:
// Description:
//
// Verilog Test Fixture created by ISE for module: Melaymachine
//
// Dependencies:
//
// Revision:
ICD & RFCS2 , FAST-NU, ISB (http://isb.nu.edu.pk/rfcs2/index.htm) 20/22
Very large scale integration(VLSI) Manual Fall 2021
module TestMELAY;
// Inputs
reg clk;
reg reset;
reg in;
// Outputs
wire out;
initial begin
// Initialize Inputs
clk = 0;
forever #5 clk = ~clk;
end
initial begin
reset = 0;
in = 0;
reset = 1;
#20
in = 1;
#10
in = 1;
#10
in = 0;
#10
in = 1;
end
endmodule
PLOT