



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Finite State Machines (FSMs) have been introduced to aid in specifying the behavior of sequential circuits. For example, we want to design part of a laser surgery system such that a surgeon can activate a laser by pressing a button B (B=1). The laser should stay on (X=1) for exactly 3 clock cycles, then shut off (X=0). We want to ensure that the laser will only stay on for 3 clock cycles regardless of whether the button is pressed for multiple clock cycles or if the button is pressed again while the laser is activated. Figure 1(a) illustrates the FSM describing the behavior of the three cycle high laser controller. To implement the three cycles high laser controller we modify the standard controller architecture for our particular application. Because we have four states, we need a 2-bit state register. Additionally, the FSM accepts input B indicating a button press, and output X controlling the laser. The FSM inputs and outputs are connected to the combinational logic block as shown in Figure 1(b).
We begin modeling the FSM controller architecture by declaring a module, laser_timer, and its corresponding interface, as shown in Figure 2. The laser_timer module has three inputs (RST, CLK, B) and one output (X). It is important to include a reset signal in all sequential circuits. The reset signal is used to indicate the
Figure 1: Three cycle high laser controller’s (a) FSM and (b) corresponding controller architecture.
(a) (b)
Combinational Logic
X
State Register
s1 s
B
clk
n n Off
On1 On3 On
B’
B
X=
X=1 X=1 X=
Inputs: B Outputs: X
Figure 2: Verilog Module Declaration
module laser_timer(RST, CLK, B, X); // user interface input RST, CLK; input B; output X; reg X; // states parameter Off = 2'b00; parameter On1 = 2'b01; parameter On2 = 2'b10; parameter On3 = 2'b11;
// variables reg [1:0] State; reg [1:0] NextState;
circuit behavior when it is initially powered on. Additionally, we declare several constant values associating state names used in the FSM with their unique user-defined state encoding. The statement parameter constant_name = value; assigns the numerical value value to a textual name defined by constant_name. As we will see later, this step is intended to make it easier for a developer to read and understand the code. In this example, we assign state Off the encoding 00 , state On1 the encoding 01 , state On the encoding 10 , and state On3 the encoding 11. Lastly, we declare two variables to keep track of the current state of the FSM and the next state of the FSM, State and NextState respectively. The controller architecture is broken down into two subcomponents, a state register and combinational logic. The state register simply keeps track of the current state of the FSM. Figure 3 illustrates the process which models the functionality of the state register. When the circuit first starts up we must specify the initial state. In an FSM, we have a special arrow that indicates the start state. In Verilog we utilize a signal called RST indicating that the circuit has just powered on. If RST is equal to 1 , we indicate the desired starting behavior. In our case we want to set the initial state to Off as indicated by the FSM in Figure 1. Notice, instead of having to set the register to the numerical state encoding value 2’b00 we can simply utilize the state name Off. After powering on the circuit, the normal behavior of the state register is to simply hold the current state of the FSM, and update the state on the next clock cycle. We must ensure the state register is only active on reset or on the rising edge of the clock cycle. The sensitivity list in the always procedure specifies that the corresponding if/else code encompassed in the always procedure only occurs on the rising edge of the RST signal (posedge RST) or the rising edge of the CLK signal (posedge CLK). The second sub-component in the controller architecture is the combinational logic. Based on the current state State and input B, the combinational logic determines the next state NextState and the corresponding FSM output X. We again specify the always procedure’s sensitivity list to execute when State or B changes. Instead
Figure 3: State Register Process
// process modeling state register always @ (posedge RST or posedge CLK) begin if( RST == 1 ) State <= Off; else State <= NextState; end
Figure 4: Combinational Logic Process
// process modeling combinational circuit always @ (State or B) begin case( State ) Off: begin // determine next state if( B == 1 ) NextState <= On1; else NextState <= Off; // assign output value X <= 0; end On1: begin // determine next state NextState <= On2; // assign output value X <= 1; end
The initial procedure executes the code block associated with this procedure once upon execution of the testbench. Similar to previous labs, the input values are assigned various values to determine the corresponding FSM behavior. However, before the FSM’s functionality can be tested the FSM needs to have an initial state. The reset signal is utilized to indicate the circuit has been powered on, thus we first set the reset signal to 1. If reset if left as 1 , the circuit will remain in the reset state and will not perform its “normal” operation, thus after a short delay we then set the reset signal to 0. Afterwards, we can test the corresponding FSM behavior for different input scenarios by varying the value and duration of the button input B. When simulating FSMs it can be useful to trace internal signals such as the current and next state values. To trace internals signals,
Figure 6: Simulating internal signals.
Internal signals show up in simulation pane
The corresponding FSM and testbench code is available. Download and simulate the code to make sure you understand how the FSM model works. Consider the following scenarios:
Figure 7: Testbench simulation with internal state signals in default display setting changed to display as unsigned decimal values.