You are on page 1of 7

Lab 11: A Simple Digital

Combination Lock
Deanna Sessions
ECEN 248-511
TA: Priya Venkatas
Date: November 20, 2013

Objectives:
The objective of this lab is to design a circuit that mimics the actions of a rotary combination
lock on a circuit board. This means that the circuit will have to be able to detect when the proper
combination is entered in order to open the lock and know when the wrong combination is
entered in order to stay closed. This will be employing the usage of the Moore finite state
machine.

Design:
Below are the source codes for the two variations upon the rotary combination lock and the
up/down counter. One rotary combination lock has the combination described by the lab manual,
the other lock has an additional number in the combination that was implemented by me.
//rotary combination lock fsm
`timescale 1 ns/ 1 ps
`default_nettype none
module combination_lock_fsm(

//assigning each of the outputs and inputs

output reg [2:0] state,


output wire Locked,
input wire Right, Left,
input wire [4:0] Count,
input wire Center,
input wire Clk, South
);
parameter S0 = 3'b000,
S1 = 3'b001,
S2 = 3'b010,
S3 = 3'b011,
S4 = 3'b100;

//assigning each state to a binary number

reg [2:0] nextState;


always@(*)
case(state)
S0: begin
if(Right)
nextState = S1;
else
nextState = S0;
end

//what to do if in state 0
//if the knob is turned right go to state 1

S1: begin
if(Left)
if (Count == 5'b01101)
nextState = S2;
else
nextState = S0;
else
nextState = S1;
end

//what to do if in state 1
//if the knob is left and the count is 13; S2

S2: begin
if(Right)

//what to do if in state 2
//if knob is right and count is 7; S3

if(Count == 5'b00111)
nextState = S3;
else
nextState = S0;
else
nextState = S2;
end
S3: begin
if(Center)
if(Count == 5'b10001)
nextState = S4;
else
nextState = S0;
else
nextState = S3;
end

//what to do if in state 3
//if knob is center and count is 17; S4

S4: begin
nextState = S4;
end
default: begin
nextState = S0;
end

//what to do if in state 4

//S4 is unlocked

endcase
assign Locked = (state==S4)?0:1;
always@(posedge Clk)
if(South)
state<= S0;
else
state<= nextState;

//what to do if the clock is reset


//if south is pressed then reset to S0
//otherwise, continue forth

endmodule
//up down counter
`timescale 1 ns/ 1 ps
`default_nettype none
module up_down_counter(
output reg [4:0] Count,
input wire Up, Down,
input wire Clk, South
);

//assigning outputs and inputs

always@(posedge Clk)
if(South)
Count <= 0;
else if(Up)
begin
if(Count == 19)
Count <= 0;
else
Count <= Count + 1;

//Defines when the count should be occurring


//reset when the south button is pressed
//If the knob is turned to the right then then count goes up

end
else if(Down)
begin
if(Count == 0)
Count<=19;
else
Count <= Count - 1;
end

//if the knob is turned to the left then the count goes down

endmodule
//fsm rotary with my combination
`timescale 1 ns/ 1 ps
`default_nettype none
module combination_lock_fsm(

//assigning each of the outputs and inputs

output reg [2:0] state,


output wire Locked,
input wire Right, Left,
input wire [4:0] Count,
input wire Center,
input wire Clk, South
);
parameter S0 = 3'b000,
S1 = 3'b001,
S2 = 3'b010,
S3 = 3'b011,
S4 = 3'b100,
S5 = 3'b101;

//assigning each state to a binary number

reg [2:0] nextState;


always@(*)
case(state)
S0: begin
if(Right)
nextState = S1;
else
nextState = S0;
end

//what to do if in state 0
//if the knob is turned right go to state 1

S1: begin
//what to do if in state 1
if(Left)
//if the knob is left and the count is 13; S2
if (Count == 5'b01101)
nextState = S2;
else
nextState = S0;
else
nextState = S1;
end
S2: begin
//what to do if in state 2
if(Right)
//if knob is right and count is 7; S3
if(Count == 5'b00111)

nextState = S3;
else
nextState = S0;
else
nextState = S2;
end
S3: begin
//what to do if in state 3
if(Left)
//if knob is center and count is 17; S4
if(Count == 5'b10001)
nextState = S4;
//S4 is unlocked
else
nextState = S0;
else
nextState = S3;
end
S4: begin
//what to do if in state 4
if(Center)
//if knob is center and count is 1; S5
if(Count == 5'b00001)
nextState = S5;
else
nextState = S0;
else
nextState = S4;
end
S5: begin
nextState = S5;
end
default: begin
nextState = S0;
end

//what to do if in state 5

endcase
assign Locked = (state==S5)?0:1;
always@(posedge Clk)
if(South)
state<= S0;
else
state<= nextState;

//what to do if the clock is reset


//if south is pressed then reset to S0
//otherwise, continue forth

endmodule

Results:
Below are the waveforms for the Combination Lock and Up/Down Counter. Each of these
worked as planned and the combination lock was able to properly work in experiment two with
the help of the counter when it was programmed onto the FPGA board. The board locked and
unlocked with the right combination of numbers.

Figure 1: Combination Lock Finite State Machine Waveform

Figure 2: Up/Down Counter Waveform

Conclusion:
All of the code worked as I had expected it to work and the combination lock on the FPGA board
worked flawlessly. I was pleased with how the lab went and how easily implemented the
program was. I learned how to take the concept of a finite state machine and bring it full term
into a finished product that is useful in the real world. Going from state diagram to program to
implementation really taught the proper way to go about programming hardware in a methodical
way.

Questions:
1. Source code is included in the design section.
2. Waveforms are included in the results section.
3. Questions throughout lab:
a. Experiment 1:
i. Take a look at the simulation waveform and take note of the tests that the
test bench performs. Is this an exhaustive test? Why or why not?
1. Yes it is an exhaustive test even though it doesnt go through all of
the 8000 combinations. It uses a smart system in which it tests
certain combinations that would show whether other ones would
fail.
ii. Take a look at the simulation waveform and make a note in your lab writeup about how the test bench tests the operation of your Up/Down Counter.
1. The up down counter test bench just tests to make sure that it
counts up and down properly.
4. If every combination were to be tried with the three number combination lock then there
would be 203 possible combinations which is 8000 combinations. For the modified four
number combination lock there are 204 combinations which is 160000 combinations. This
would take quite some time to get through all of these combinations in a brute-force
attack.

Student Feedback:
1. This lab was so much fun! I loved being able to create something that is actually
plausible for implementation. It was great seeing the whole thing through to the end by
creating a state diagram and then creating a program to mimic the workings of a
combination lock. I didnt dislike anything about it.
2. Nothing was unclear.
3. Keep it as is, it was fun and full of information.

You might also like