Lab 8
Lab 8
Computer Science
Semester: 6
th
Section: BEE 12C
Syeda Fatima
334379
Zahra
Objectives
The purpose of this lab was as follows:
Introduction
Finite state machines (FSMs) are conceptual representations of systems that can only exist in
a limited number of states and can transition between these states based on specific inputs
and outputs.
FSMs are commonly utilized in various fields such as computer science, engineering, and
mathematics to design and analyze algorithms, circuits, protocols, and languages. The
primary purpose of this lab report is to showcase our comprehension and proficiency in
utilizing FSMs as a potent tool for modeling and resolving computational problems.
Software
Quartus Prime is a design software developed by Intel for the design of digital circuits using
Field Programmable Gate Arrays (FPGAs). It supports various programming language
including Verilog and VHDL.
LAB EXERCISES
2. Write a Verilog file that instantiates the nine flip-flops in the circuit and which specifies
the logic expressions that drive the flip-flop input ports. Use only simple assign statements in
your Verilog code to specify the logic feeding the flip-flops. Note that the one-hot code
enables you to derive these expressions by inspection. Use the toggle switch SW0 as an
active-low synchronous reset input for the FSM, use SW1 as the w input, and the pushbutton
KEY0 as the clock input which is applied manually. Use the red light LEDR9 as the output z,
and assign the state flip-flop outputs to the red lights LEDR8 to LEDR0.
3. Include the Verilog file in your project, and assign the pins on the FPGA to connect to the
switches and the LEDs.
6. Finally, consider a modification of the one-hot code given in Table 1. It is often desirable
to set all flip-flop outputs to the value 0 in the reset state. Table 2 shows a modified one-hot
state assignment in which the reset state, A, uses all 0s. This is accomplished by inverting the
state variable y0. Create a modified version of your Verilog code that implements this state
assignment. (Hint: you should need to make very few changes to the logic expressions in
your circuit to implement the modified state assignment.)
Code:
module lab8 (
input [3:0] KEY,
input [9:0] SW,
output [9:0] LEDR
);
endmodule
RTL Viewer:
Hardware Implementation:
2. Include in the project your Verilog file that uses the style of code in Figure 3. Use the same
switches, pushbuttons, and lights that were used in Part I.
3. Before compiling your code it is necessary to explicitly tell the Synthesis tool in Quartus
that you wish to have the finite state machine implemented using the state assignment
specified in your Verilog code. If you do not explicitly give this setting to Quartus, the
Synthesis tool will automatically use a state assignment of its own choosing, and it will
ignore the state codes specified in your Verilog code. To make this setting, choose
Assignments > Settings in Quartus, and click on the Compiler Settings item on the left side of
the window, then click on the Advanced Settings (Synthesis) button. As indicated in Figure 4,
change the parameter State Machine Processing to the setting User-Encoded.
4. Compile your project. To examine the circuit produced by Quartus open the RTL Viewer
tool. Double-click on the box shown in the circuit that represents the finite state machine, and
determine whether the state diagram that it shows properly corresponds to the one in Figure2.
To see the state codes used for your FSM, open the Compilation Report, select the Analysis
and Synthesis section of the report, and click on State Machines.
5. Download the circuit into the FPGA chip and test its functionality.
Code:
module lab8(KEY,SW,LEDR);
FSM f1(KEY[0],SW[0],KEY[1],LEDR[0]);
endmodule
module FSM
( input clk, w, reset,
output reg z
);
// DECLARE STATE REGISTER
reg [3:0]state;
// DEFINED PARAMETERS
parameter A = 0, B = 1, C= 2, D = 3,E = 4, F = 5, G = 6, H =
7, I=8;
RTL Viewer:
Hardware Implementation:
Code:
module lab8(KEY,SW,LEDR,out0,out1);
input [9:0]SW;
input[3:0] KEY;
output reg [9:0] LEDR;
output [3:0] out0,out1;
wire [3:0] w0,w1;
assign out0=w0;
assign out1=w1;
always @(*)
begin
if(w0==4'b0000 || w1==4'b1111)
LEDR[0]=1;
else
LEDR[0]=0;
end
endmodule
module SHIFT_REGISTER0(clk,in,out,en);
input in,clk,en;
output reg [3:0] out;
always@(posedge clk)
begin
if(~en)
begin
out[0]<=in;
out[1]<=out[0];
out[2]<=out[1];
out[3]<=out[2];
end
else
out=4'bxxxx;
end
endmodule
module SHIFT_REGISTER1(clk,in,out,en);
input in,clk,en;
output reg [3:0] out;
always@(posedge clk)
begin
if(en)
begin
out[0]<=in;
out[1]<=out[0];
out[2]<=out[1];
out[3]<=out[2];
end
else
out=4'bxxxx;
end
endmodule
Hardware Implementation:
Exercise 4
Design and implement a Morse-code encoder circuit using an FSM. Your circuit should take
as input one of the first eight letters of the alphabet and display the Morse code for it on a red
LED. Use switches SW2−0 and pushbuttons KEY1−0 as inputs. When a user presses KEY1,
the circuit should display the Morse code for a letter specified by SW2−0 (000 for A, 001 for
Code:
module lab8
(
input clk,reset,input [2:0] sw,input[1:0] key,output
ledr,output reg [15:0] out
);
// STATE REGISTERS DECLARED
reg[3:0]state;
wire [15:0]w2;
// DEFINING STATES
parameter A = 0, B = 1, C = 2, D = 3,E= 4, F = 5, G = 6, H
=7,I=8;
shift_register s1(clk,reset,out,ledr,w2);
always @ (*) begin
case (state)
A:out=16'b0000000000000000;
B:out=16'b1011100000000000;
C:out=16'b1110001010100000;
D:out=16'b1110001011100010;
E:out=16'b1110001010000000;
F:out=16'b100000000000000;
G:out=16'b1010111000100000;
H:out=16'b0011100011100010;
I:out=16'b101010100000;
default:out=16'bxxxxxxxxxxxxxxxx;
endcase
end
B:
begin
if(sw[2:0]==000&& ~key[1])
state <= B;
else if(sw[2:0]==001&& ~key[1])
state <= C;
else if(sw[2:0]==010&& ~key[1])
state <= D;
else if(sw[2:0]==011&& ~key[1])
state <= E;
else if(sw[2:0]==100&& ~key[1])
state <= F;
else if(sw[2:0]==101&& ~key[1])
state <= G;
else if(sw[2:0]==110&& ~key[1])
state <= H;
else
state <=I;
end
C:
begin
if(sw[2:0]==000&& ~key[1])
state <= B;
else if(sw[2:0]==001&& ~key[1])
state <= C;
else if(sw[2:0]==010&& ~key[1])
state <= D;
else if(sw[2:0]==011&& ~key[1])
D:
begin
if(sw[2:0]==000&& ~key[1])
state <= B;
else if(sw[2:0]==001&& ~key[1])
state <= C;
else if(sw[2:0]==010&& ~key[1])
state <= D;
else if(sw[2:0]==011&& ~key[1])
state <= E;
else if(sw[2:0]==100&& ~key[1])
state <= F;
else if(sw[2:0]==101&& ~key[1])
state <= G;
else if(sw[2:0]==110&& ~key[1])
state <= H;
else
state <=I;
end
E:
begin
if(sw[2:0]==000&& ~key[1])
state <= B;
else if(sw[2:0]==001&& ~key[1])
state <= C;
else if(sw[2:0]==010&& ~key[1])
state <= D;
else if(sw[2:0]==011&& ~key[1])
state <= E;
else if(sw[2:0]==100&& ~key[1])
state <= F;
else if(sw[2:0]==101&& ~key[1])
state <= G;
else if(sw[2:0]==110&& ~key[1])
state <= H;
else
state <=I;
F:
begin
if(sw[2:0]==000&& ~key[1])
state <= B;
else if(sw[2:0]==001&& ~key[1])
state <= C;
else if(sw[2:0]==010&& ~key[1])
state <= D;
else if(sw[2:0]==011&& ~key[1])
state <= E;
else if(sw[2:0]==100&& ~key[1])
state <= F;
else if(sw[2:0]==101&& ~key[1])
state <= G;
else if(sw[2:0]==110&& ~key[1])
state <= H;
else
state <=I;
end
G:
begin
if(sw[2:0]==000&& ~key[1])
state <= B;
else if(sw[2:0]==001&& ~key[1])
state <= C;
else if(sw[2:0]==010&& ~key[1])
state <= D;
else if(sw[2:0]==011&& ~key[1])
state <= E;
else if(sw[2:0]==100&& ~key[1])
state <= F;
else if(sw[2:0]==101&& ~key[1])
state <= G;
else if(sw[2:0]==110&& ~key[1])
state <= H;
else
state <=I;
end
H:
begin
if(sw[2:0]==000&& ~key[1])
state <= B;
else if(sw[2:0]==001&& ~key[1])
state <= C;
else if(sw[2:0]==010&& ~key[1])
I:
begin
if(sw[2:0]==000&& ~key[1])
state <= B;
else if(sw[2:0]==001&& ~key[1])
state <= C;
else if(sw[2:0]==010&& ~key[1])
state <= D;
else if(sw[2:0]==011&& ~key[1])
state <= E;
else if(sw[2:0]==100&& ~key[1])
state <= F;
else if(sw[2:0]==101&& ~key[1])
state <= G;
else if(sw[2:0]==110&& ~key[1])
state <= H;
else
state <=I;
end
endcase
end
endmodule
module shift_register(clk,reset,in2,LEDR,out2);
input clk,reset;
input [15:0] in2;
output reg [15:0] out2;
initial
begin
out2=in2;
end
output reg LEDR;
integer i;
endmodule
RTL Viewer:
Hardware Implementation: