Fpga Programming Using Verilog HDL Language
Fpga Programming Using Verilog HDL Language
Conducted by:
BHUBANESWAR INSTITUTE OF TECHNOLOGY
Infovalley, Harapur, Bhubaneswar, Orissa - 752054, India
Ph - +91-674-2113498, Fax - +91-674-2113497, Email: info@bit.edu.in
Website: www.bit.edu.in
Course Objective
• Create and implement designs by using the ISE software design
environment and Basys-2 Spartan3E FPGA board.
• Verilog code for synthesis
• Functions and Tasks
• Creating Finite State Machine (FSM) by using Verilog
• Verilog test fixtures for simulation
• Introduction to FPGA
• Project Work
1
23-08-2011
AGENDA:
• Introduction to VLSI and its importance
• Getting Started with ISE 10.1 and Basys-2 Spartan 3E Kit
• Lab work
2
23-08-2011
3
23-08-2011
Why VLSI?
Integration improves the design:
• higher speed
• Lower power
• Physically smaller
• Integration reduces manufacturing cost
• higher reliability
• more functionality
• Design simulation
Initialize Timing
Simulating Design Functionality:
4
23-08-2011
LAB WORK
LAB WORK:
• Half Adder
• Full Adder
Programming and implementation on FPGA
5
23-08-2011
LAB WORK
AGENDA:
6
23-08-2011
•A primary use of HDLs is the simulation of designs before the designer commit
to fabrication.
7
23-08-2011
-Module Definition
-module
-endmodule
-Interface
-Add_on
-Module body
module Half_Adder(
input a,
input b,
output sum,
output carry
);
assign sum = a^b; // sum bit
assign carry = (a&b); //carry bit
endmodule
8
23-08-2011
9
23-08-2011
10
23-08-2011
• The designer is aware of how data flows between hardware registers and
how the data is processed in the design.
11
23-08-2011
12
23-08-2011
Switch level:
13
23-08-2011
Module instantiation
Syntax:
Module_name Instance_name (Port_Association_List)
Module instantiation
Example for Module Port Connection:
for DFF
DFF1 DFF2
14
23-08-2011
Module instantiation
Module port connection BY ORDER:
module SYNCHRO(ASYNC,SYNC,CLOCK);
input ASYNC;
input CLOCK;
output SYNC;
wire C1_ASYNC;
DFF DFF1 (C1_ASYNC, ASYNC, CLOCK);
DFF DFF2 (SYNC, C1_ASYNC, CLOCK);
Endmodule
• If the number of ports increased, then it is very difficult to do “module
ports connection by order”.
Module instantiation
Module Port Connection BY NAME:
endmodule;
15
23-08-2011
Module instantiation
• Inside the same module, instance names of particular module should be
different. In the same way, inside the same module, instance name of
different modules should be different.
For example:
module A();
DFF DFF1();
DFFE DFF1();
endmodule
IS NOT ALLOWED
LAB WORK
LAB WORK:
16
23-08-2011
AGENDA:
• Comments in Verilog
• Data Types in Verilog
• Expressions, Operands And Operators in Verilog
• Timing control in Verilog
Comments in Verilog
• Comments can be inserted in the code for readability and documentation.
17
23-08-2011
x Unknown/ Uninitialized
• Supply0 and supply1 define wires tied to logic 0 (ground) and logic 1 (power),
respectively.
18
23-08-2011
reg reset; // declare a variable reset that can hold its value
initial // this construct will be discussed later
begin
reset = 1'b1; //initialize reset to 1 to reset the digital circuit.
#100 reset = 1'b0; // after 100 time units reset is deasserted.
end
19
23-08-2011
Nets or reg data types can be declared as vectors (multiple bit widths). If bit
width is not specified, the default is scalar (1-bit).
Multi-dimensional arrays:
reg [4:0] port_id[0:7]; // Array of 8 port_ids; each port_id is 5 bits wide
20
23-08-2011
Expressions:
21
23-08-2011
Operators:
22
23-08-2011
| reduction or one
Reduction
~| reduction nor one
Unary operators:
-4 // Negative 4
+5 // Positive 5
Binary operators:
• If any operand bit has a value x, then the result of the entire expression is x.
• Modulus operators produce the remainder from the division of two numbers.
13 % 3 // Evaluates to 1
23
23-08-2011
• Logical operators are logical-and (&&), logical-or (||) and logical-not (!).
Operators && and || are binary operators. Operator ! is a unary operator.
• Logical operators always evaluate to a 1-bit value, 0 (false), 1 (true), or x
(ambiguous).
// Logical operations
A = 2’b10; B = 1’b0;
A && B // Evaluates to 0. Equivalent to (logical-1 && logical-0)
// X = 4'b1010, Y = 4'b1101,
Y >= X // Evaluates to a logical 1
24
23-08-2011
Equality Operators:
• Equality operators are logical equality (==), logical inequality (!=), case
equality (===), and case inequality (!==).
// X = 4'b1010, Y = 4'b1101
// Z = 4'b1xxz, M = 4'b1xxz
X != Y // Results in logical 1
X == Z // Results in x
Z === M // Results in logical 1 (all bits match, including x and z)
• Bitwise operators are negation (~), and (&), or (|), xor (^), xnor (^~, ~^).
• Bitwise operators perform a bit-by-bit operation on two operands.
• The exception is the unary negation operator (~), which takes only one
operand and operates on the bits of the single operand.
25
23-08-2011
• Reduction operators are and (&), nand (~&), or (|), nor (~|), xor (^), and
xnor (~^, ^~).
• Reduction operators take only one operand. Reduction operators perform a
bitwise operation on a single vector operand and yield a 1-bit result.
// X = 4'b1010
&X //Equivalent to 1 & 0 & 1 & 0. Results in 1'b0
^X //Equivalent to 1 ^ 0 ^ 1 ^ 0. Results in 1'b0
26
23-08-2011
// X = 4'b1100
Y = X >> 1; //Y is 4'b0110. Shift right 1 bit. 0 filled in MSB position.
Y = X << 2; //Y is 4'b0000. Shift left 2 bits.
27
23-08-2011
Conditional Operator:
28
23-08-2011
Syntax: #delay
Delayed assignment:
#Δt variable = expression;
Intra-assignment delay:
variable = #Δt expression;
29
23-08-2011
module delay_test1(
input a,
input b,
output reg c);
always @(a or b)
c = (a&b);
endmodule
/*Here the statement within the always block gets evaluated whenever there is
a transition on ‘a’ or ‘b’.*/
Syntax: wait(condition)
Module delay_test2(
input a,
input b,
output reg c);
initial
begin
c=0;
wait(a==1)
c=b;
end
endmodule
30
23-08-2011
LAB WORK
LAB WORK:
• Design a 2- to – 1 multiplexer using dataflow modeling (logic
equation), and Using Conditional Operators.
LAB WORK
2-to-1 Multiplexer, Using Logic 2-to-1 Multiplexer, Using Conditional
Equations Operators
/* Module 2-to-1 multiplexer using /* Module 2-to-1 multiplexer using
data flow. Logic equation*/ data flow. Conditional operator.*/
module mux2_to_1 (out, i0, i1, s); module multiplexer2_to_1 (out, i0,
// Port declarations from the I/O i1, s);
diagram // Port declarations from the I/O
output out; diagram
input i0, i1; output out;
input s; input i0, i1;
//Logic equation for out input s;
assign out = (~s & i0) | (s & i1); assign out = s ? i1 : i0;
endmodule endmodule
31
23-08-2011
LAB WORK
Verilog program for different operators:
Write down a verilog program which can show the use of following
operations
- logical-or (||)
- Relational operator greater-than (>)
- logical inequality (!=)
- case equality (===)
- Bitwise operator negation (~)
- Reduction operator xor (^)
- right shift ( >> )
- concatenation operator ( {, } )
- Replication Operator
Delay Problem:
Write down a Verilog program to implement both intra-assignment delay
and delayed assignment. Also implement a wait statement.
Weekend Assignment
32
23-08-2011
Continuous assignments:
• Continuous assignments can only be made to nets. The operands can be
of any data type. If one of the operands on the right hand side (RHS) of the
assignment change, as the name suggests, the net on the left hand side
(LHS) of the assignment is updated.
Procedural assignments:
• Procedural assignments are made to reg, integer, real or time, they need
updating constantly to reflect any change in the value of the operands on
the RHS.
33
23-08-2011
- Initial Block
- Always Block
Keywords: initial
• If there is more than one block they execute concurrently and independently.
34
23-08-2011
initial
clock = 1'b0; // variable initialization
initial
begin // multiple statements have to be grouped
alpha = 0;
#10 alpha = 1; // waveform generation
#20 alpha = 0;
#5 alpha = 1;
end;
endmodule
Keywords: always
• An always block is similar to the initial block, but the statements inside an
always block will repeated continuously, in a looping fashion, until stopped by
$finish.
reg clock;
initial clock = 1'b0; // start the clock at 0
always #10 clock = ~clock; // toggle every 10 time units
initial #5000 $finish // end the simulation after 5000 time units
endmodule
35
23-08-2011
Procedural Assignments:
Two types of Procedural Assignments:
• Blocking Assignments
• Nonblocking Assignments
36
23-08-2011
• The transfer to the left hand side is made according to the delays. An intra-
assignment delay in a non-blocking statement will not delay the start of any
subsequent statement blocking or non-blocking. However normal delays will
are cumulative and will delay the output.
Syntax
variable <= expression;
37
23-08-2011
initial
begin
a=1; b=0; c=1; x=0;
#50 a = b & c; // wait for 50 units, then grab b,c and execute a = b&c = 0.
d = a; // Time continues from last line, d=0 = b&c at t=50.
x <= #60 b | c;
// grab b|c now at t=50, don’t stop, make x=1 = b|c at t= 50+60 =110.
c <= #20 a; /* grab a at t=50 (end of last blocking statement).
Deliver c=0 at t= 50+20 = 70. Previous x is unaffected by c change. */
y <= #10 b | c; // grab b|c at t=50, don’t stop, make y=1 at t=50+10=60.
#30 z = b ~^ c;
// grab b~^c at t=80 (#50+#30), make z=1 (c =0 at t=70 and b = 0) at t=80.
w <= x // make w=1 at t=80. Starting at last blocking assign.
end
• “<=” best mimics what physical flip-flops do; use it for “always @ (posedge clk
..) type procedures.
• “=” best corresponds to what c/c++ code would do; use it for combinational
procedures.
38
23-08-2011
39
23-08-2011
If-else statement:
• It executes the first statement or statement_group if the condition evaluates as true and
executes the second statement or statement_group if the condition evaluates as false.
Syntax
if(condition)
statement or statement_group
else
statement or statement_group
Example:
if (alu_func == 2’b00)
aluout = a + b;
else if (alu_func == 2’b01)
aluout = a - b;
else if (alu_func == 2’b10)
aluout = a & b;
else // alu_func == 2’b11
aluout = a | b;
if (a == b)
begin
x = 1;
ot = 4’b1111;
end
40
23-08-2011
• It compares the expression with each of the case_item’s and executes the
statement or statement_group associated with the first matching case_item.
• It executes the default if none of the case_item’s match. Here the default
case is optional.
Syntax:
case(expression)
case_item1: statement or statement_group
case_item2: statement or statement_group
.
.
case_itemN: statement or statement_group
default: statement or statement_group
endcase
Example:
case (alu_ctr)
2’b00: aluout = a + b;
2’b01: aluout = a - b;
2’b10: aluout = a & b;
default: aluout = 1’bx; /* Treated as don’t cares for minimum logic generation. */
endcase
case (x, y, z)
2’b00: aluout = a + b; /*case if x or y or z is 2’b00.*/
2’b01: aluout = a - b;
2’b10: aluout = a & b;
default: aluout = a | b;
endcase
41
23-08-2011
Syntax:
forever
statement or statement_group
Example:
always
forever begin
@(posedge clk);
a = a + 1;
end
Syntax
repeat(expression)
statement or statement _group
Example:
always
repeat (2) begin
#50 a = 0;
#50 a = 1;
end
/* after 50, a = 0, after 100, a = 1, after 150, a = 0, after 200, a = 1 */
42
23-08-2011
Syntax
while(condition)
statement or statement_group
Example:
always
while (!a) begin
@(posedge clk);
c = b | 1;
end
• The for loop here uses three expressions separated by semicolons to control
the loop.
Syntax
for(initial_value; condition; step)
statement or statement_group
•The first expression (initial_value) is executed once before entering the loop
the first time.
• The second expression (condition) is evaluated to determine if the contents
of the loop (i.e statement or statement_group) should be executed. If the loop
condition expression is true, the loop is entered.
• The final expression (step) is evaluated at the end of the loop.
43
23-08-2011
LAB WORK:
•Generate a signal of 25MHz using initial and always block.
• Write a Verilog program to show the difference between begin-end
and fork-join block.
• Write a verilog program with delays to show the difference between
blocking and nonblocking assignment.
• Design a 4:1 MUX using if statement.
• Design a 3:8 Decoder using case statement.
• Generate a signal of 25MHz using forever loop.
• Design a 3:8 Decoder using while loop as well as for loop.
44
23-08-2011
• Function
• Task
• System Tasks
• Compiler Directives
• Functions and Tasks are useful for several reasons which are, they allow
often used behavioral sequences to be written once and called when needed,
they allow for a cleaner writing style and finally they allow data to be hidden
from other parts of the design.
45
23-08-2011
Functions
Functions are defined in the module in which they are used. It is possible to
define function in separate file and use compile directive 'include to include
the function in the file which instantiates the function. Function can call other
functions, but cannot call task.
• Functions must contain at least one input argument and cannot drive more
than one output.
Functions
• Function cannot include timing delays, like posedge, negedge, # delay. Which
means that function should be executed in "zero" time delay.
• Functions must contain a statement that assigns the return value to the
implicit function name register.
• The variables declared within the function are local to that function. The
order of declaration within the function defines how the variables passed to
the function by the caller are used.
46
23-08-2011
Functions
Syntax of Function:
Function begins with keyword function and ends with keyword endfunction.
Inputs are declared after the keyword function.
Functions
Example: Simple Function
function myfunction;
input a, b, c, d;
begin
myfunction = ((a|b) & (c|d));
end
endfunction
Calling a Function:
Let’s assume that function in above example is stored in a file called myfunction.v.
Advantage of coding function in separate file is that, it can be used in multiple module's.
module func_test(a, b, c, d, e, f);
input a, b, c, d, e ;
output f;
wire f;
`include "myfunction.v"
assign f = (myfunction (a,b,c,d)) ? e :0;
endmodule
47
23-08-2011
Tasks
• A task is similar to a function, but unlike a function it has any number input
and output ports. Therefore tasks do not return values.
• Included in the main body of code tasks can be called many times, reducing
code repetition.
Tasks
• Tasks are defined in the module in which they are used. It is possible to
define task in separate file and use compile directive 'include to include the
task in the file which instantiates the task.
• Task can include timing delays, like posedge, negedge, # delay. The variables
declared within the task are local to that task.
• The order of declaration within the task defines how the variables passed to
the task by the caller are used. Task can call another task or function.
48
23-08-2011
Tasks
Syntax of Task:
Task begins with keyword task and ends with keyword endtask.
Input and output are declared after the keyword task.
Local variables are declared after input and output declaration.
task task_name;
input [msb:lsb] input_port_list;
output [msb:lsb] output_port_list;
reg [msb:lsb] reg_variable_list;
parameter [msb:lsb] parameter_list;
integer [msb:lsb] integer_list;
... statements ...
endtask
Tasks
Example : Simple Task
task convert;
input [3:0] temp_in;
output [4:0] temp_out;
begin
temp_out = temp_in +5;
end
endtask
49
23-08-2011
Tasks
Calling a Task:
Let’s assume that task in example “Simple Task” is stored in a file called mytask.v.
Advantage of coding task in separate file is that, it can be used in multiple
modules.
System Tasks
Verilog provides standard system tasks for certain routine operations. All
system tasks appear in the form $<keyword>.
$display:
• Prints the formatted message once when the statement is executed during
simulation.
Example:
initial $display(“Hello World”);
50
23-08-2011
System Tasks
$strobe:
• It executes after all simulation events in the current time step have executed.
Prints the formatted message once when executed. This task guarantees that
the printed values for the signals/variables are the final values the signals/
variables can have at that time step.
Example:
initial $strobe(“Current values of A, B and C are A=%b, B=%b, C=%b”, A, B, C);
Prints A, B and C and prints their value in binary format after all simulation
events in the current time step have executed.
System Tasks
$monitor:
Example:
initial $monitor(“Current values of A, B and C are A=%b, B=%b, C=%b”, A, B, C);
Monitors A, B and C and prints their value in binary format whenever one of
the signals (i.e A or B or C) changes its value during simulation.
51
23-08-2011
System Tasks
$time:
Example:
initial $monitor(“time = %d, A = %d”, $time, A);
$finish:
Example:
initial $finish;
Compiler Directives
52
23-08-2011
Compiler Directives
‘include:
File inclusion. The contents of another Verilog source file is inserted where
the ‘include directive appears.
Compiler Directives
‘define:
Allows a text string to be defined as a macro name.
Example
`define WIDTH 2
// Allows ‘WIDTH’ to be substituted by 2 where ever it gets used.
module adder (a,b,c);
input [`WIDTH-1:0] a;
input [`WIDTH-1:0] b;
output [`WIDTH:0] c;
assign c=a+b;
endmodule
53
23-08-2011
Compiler Directives
Comparison between parameter and ‘define:
• ‘define can be used anywhere within the module or outside the module.
Compiler Directives
Example using parameter:
assign c=a+b;
endmodule
54
23-08-2011
Compiler Directives
`timescale:
timescale <time_unit> / <time_precision>
• For specifying time unit and time precision of the modules that follow it.
• the time unit is the unit of measurement for time values such as the
simulation time and delay values; the time precision specifies how
simulation tools rounds time values.
For example:
`timescale 1ns/100ps
module test ();
assign #10.1678 out = in; // out is delayed by 10.2 ns from in.
endmodule
LAB WORK
LAB WORK:
• Create a function which will take two 3 bit numbers and return the greater
one. Use that function in the module to find out the greatest one among
four 3 bit numbers.
• Create a Task which will be having 3 two bit inputs and 3 outputs among
which one will hold the addition of 3 inputs , another will hold the greatest
no. among them and the last one will hold the lowest no. among them. Use
that task in a module which will be having 6 inputs and 6 outputs. First 3
outputs will hold the addition, greatest no. and lowest no. of first 3 inputs
and last 3 outputs will hold the addition, greatest no. and lowest no. of last
3 inputs.
55
23-08-2011
LAB WORK
Create a function which will take two 3 bit numbers and return the greater
one. Use that function in the module to find out the greatest one among four
3 bit numbers.
module func_t1 (
// Function Body “myfunction.v”
input [2:0] a, b, c, d,
function [2:0]myfunction;
output [2:0] e
input [2:0]a,b;
);
begin
myfunction = (a>b)?a:b;
wire [2:0] f, g;
end
`include “myfunction.v”
endfunction
assign f = (myfunction (a,b));
assign g = (myfunction (c,d));
assign e = (myfunction (f,g));
endmodule
LAB WORK
Create a Task which will be having 3 two bit inputs and 3 outputs among which one
will hold the addition of 3 inputs , another will hold the greatest no. among them and
the last one will hold the lowest no. among them. Use that task in a module which
will be having 6 inputs and 6 outputs. First 3 outputs will hold the addition, greatest
no. and lowest no. of first 3 inputs and last 3 outputs will hold the addition, greatest
no. and lowest no. of last 3 inputs.
//Task Body “mytask.v”
task mytask;
input [1:0] a,b,c;
output [3:0] x;
output [1:0] y, z;
begin
// logic for addition
x = (a+b)+c;
// Rest of the task body refer next slide
56
23-08-2011
LAB WORK
//logic for greast no.
if (a>=b && a>=c)
y = a;
else if (b>=c && b>=a)
y = b;
else if (c>=a && c>=b)
y = c;
//logic for lowest no.
if (a<=b && a<=c)
y = a;
else if (b<=c && b<=a)
y = b;
else if (c<=a && c<=b)
y = c;
end
endtask
LAB WORK
//Program calling task “mytask.v”
module task_t ( a,b,c,d,e,f,g,h,I,j,k,l);
input [1:0] a,b,c,d,e,f;
output reg [1:0] h,i,k,l;
output reg [3:0] g,j;
`include “mytask.v”
always @(a,b,c)
mytask (a,b,c,g,h,i);
always @(d,e,f)
mytask (d,e,f,j,k,l);
endmodule
57
23-08-2011
• Writing Testbenches
• Creating Testbenches and simulating for some simple designs
Writing Testbenches
• A test bench is a HDL program used for applying stimulus to a HDL design
in order to test it and observe its response during simulation.
58
23-08-2011
Writing Testbenches
• A stimulus module is an HDL program that has the following form.
module testname
Declare local reg and wire identifiers
Instantiate the design module under test.
Generate stimulus using initial and always statements
Display the output response.
endmodule
• A test module typically has no inputs or outputs.
• The signals that are applied as inputs to the design module for simulation
are declared in the stimulus module as local reg data type.
• The outputs of the design module that are displayed for testing are
declared in the stimulus model as local wire data type.
• The module under test is then instantiated using the local identifiers.
Writing Testbenches
The stimulus model generates inputs for the design module by declaring
identifiers TA and TB as reg data type, and checks the output of the design unit
with the wire identifier TC. The local identifiers are then used to instantiate the
design module under test.
59
23-08-2011
Writing Testbenches
• The response to the stimulus generated by the initial and always blocks
will appear at the output of the simulator as timing diagrams.
Writing Testbenches
Example 1: Description Example 1: Description for the
for the design testbench
//Description of circuit with //Stimulus for simple circuit
delay module stimcrct;
module circuit_with_delay reg A,B,C;
(A,B,C,x,y,z); wire x,y,z;
input A,B,C; circuit_with_delay cwd(A,B,C,x,y,z);
output x,y,z; initial
wire e; begin
not #(10) g1(x,C); A = 1'b0; B = 1'b0; C = 1'b0;
and #(30) g2(y,A,B); #100 A = 1'b1; B = 1'b0; C = 1'b1;
or #(20) g3(z,A,B); #100 B = 1’b1;
endmodule #100 $finish;
end
endmodule
60
23-08-2011
Writing Testbenches
Example 2: Description for the design
//Dataflow description of 2-to-1-line multiplexer
module mux21_df (A,B,select,OUT);
input A,B,select;
output OUT;
assign OUT = select ? A : B;
endmodule
Writing Testbenches
Example 2: Description for the testbench
//Stimulus for mux2x1_df
module testmux;
reg TA,TB,TS; //inputs for mux
wire Y; //output from mux
mux2x1_df mx (TA,TB,TS,Y); // instantiate mux
initial begin
$display(“MUX TEST”);
$monitor(”select=%b A=%b B=%b OUT=%b T=%d",TS,TA,TB,Y,$time);
TS = 1; TA = 0; TB = 1;
#20 TA = 1;
#20 TS = 0;
#10 TB = 0;
end
endmodule
61
23-08-2011
LAB WORK
LAB WORK:
• Create a testbench for the greatest number design using function you have
created in the last class. Check all the possible stimulus for the design
within the testbench. Use $monitor, $strobe, $time, $display, $finish; the
system tasks within the testbench to check the outputs.
• Create a module having three inputs i0, i1, i2 each of 4 bits and having
three outputs y0, y1, y2 each of 4 bits. Design the module in such a way that
output y0, y1 and y2 will hold the value of inputs i0, i1, i2 in such a way that
y2>y1>y0. Create a testbench for the above design and give all possible
stimuli to the testbench to test the design.
AGENDA:
62
23-08-2011
endmodule
63
23-08-2011
reg temp;
integer i;
64
23-08-2011
always @(din)
begin
temp = 0;
for (i=0; i<SIZE; i=i+1)
temp = temp ^ din[i];
p_out = temp;
end
endmodule
endmodule
65
23-08-2011
Sel = 00 : Addition
= 01 : Subtraction
= 10 : Bitwise AND
= 11 : Bitwise XOR
66
23-08-2011
67
23-08-2011
assign p = ^res;
assign y = oe?res:2'b11;
endmodule
68
23-08-2011
69
23-08-2011
70
23-08-2011
D Q
output q; q
0 1 D
input data, rst, clk;
reg q; FLIP FLOP
rst
always @(posedge clk)
begin clk
if(rst == 1’b1)
q <= 1’b0;
else
q <= data;
endmodule
71
23-08-2011
endmodule
72
23-08-2011
73
23-08-2011
endmodule
74
23-08-2011
75
23-08-2011
module uni_shift_reg (d, clk, rst, l_in, r_in, s1, s0, q);
output [3:0] q;
input [3:0] d;
input clk, rst, l_in, r_in, s1, s0;
reg [3:0] q;
always @(posedge clk)
begin
if (rst)
q <= 4’b0000;
else
76
23-08-2011
Weekend Assignment
77
23-08-2011
Outputs are normally a function of the current state and possibly the FSM’s
primary inputs (in the case of a Mealy FSM). Often in a Moore FSM, we may
want to derive the outputs from the state memory and is independent of
primary inputs.
78
23-08-2011
2 Types of FSM
- Mealy
- Moore
Finite State Machine (FSM) is a means to model a digital system which has
some finite states in it. To show the transition between states,
state/transition diagram can be drawn. State/transition tables can be drawn
which indicates what is the present state and to which state there will be a
transition.
Moore machine output depends only on the state whereas, mealy machine
output depends on the input as well as the state.
(i) Determine the next-state and output functions which depend on current
state and input
(iii) Draw a state diagram that presents the information from the previous
step in graphical form.
79
23-08-2011
Excitation
State
Input Inputs Output
State
Combinational Combinational
Memory Mealy
Logic Logic
External Outputs
Inputs
A Mealy machine consists of three logic blocks, namely input combinational logic circuit,
state memory and an output combinational logic circuit.
Here the output combinational logic circuit has two inputs, namely, the output of the
sequential logic circuit as well as the external input signal. So output of Mealy FSM
depends on the present state of the circuit and also it depends on the external inputs
Excitation
State
Input Inputs Output
State
Combinational Combinational
Memory Moore
Logic Logic
External Outputs
Inputs
Control inputs connected to system
clock
A Moore machine consists of three logic blocks, namely input combinational logic circuit,
state memory and an output combinational logic circuit.
Here the output combinational logic circuit has one input, namely, the output of the
sequential logic circuit (state memory). So Moore FSM depends on the present state of the
circuit and is independent of the primary inputs
80
23-08-2011
Encoding Style:
Binary Encoding:
S0 = 00
S1 = 01
S2 = 10
S3 = 11
Gray Encoding:
S0 = 00
S1 = 01
S2 = 11
S3 = 10
81
23-08-2011
82
23-08-2011
83
23-08-2011
Identify the
sequence 1101,
regardless of
where it occurs in
a longer
sequence.
84
23-08-2011
85
23-08-2011
• Project Work
• Quiz Test
• Closing (Feedback) Meeting of Summer Training
86
23-08-2011
PROJECT WORK
Design of Traffic Signal Controller:
Road
Main Highway
Country
PROJECT WORK
Problem Statements:
• The traffic signal for the main highway gets highest priority because cars
are continuously present on the main highway. Thus the main highway signal
remains green by default.
• Occasionally, cars from the country road arrive at the traffic signal. The
traffic signal for the country road must turn green only long enough to let the
cars on the country road go.
• As soon as there are no cars on the country road, the country road traffic
signal turns yellow and then red and the traffic signal on the main highway
turns green again.
• There is a sensor to detect cars waiting on the country road. The sensor
sends a signal X as input to the controller. X=1 if there are cars on the country
road; otherwise, X=0.
87
23-08-2011
PROJECT WORK
State Definitions for Traffic Signal Controller:
State Signals
Hwy Cntry
S0 G R
S1 Y R
S2 R R
S3 R G
S4 R Y
PROJECT WORK
State Diagram for Traffic Signal Controller:
X=0
S0
Time Delay
GR
X=1
S4 S1
RY YR
There are delays on transitions from S1 to S2, from S2 to S3, and from S4 to
S0. The delays must be controllable.
88
23-08-2011
PROJECT WORK
89