lesson 18 fsm vhdl student

11
Lesson 18 Finite State Machine (FSM) with VHDL 1. Overview of FSM Recall that FSM is used to model a sequential logic circuit. A sequential logic circuit has three main parts: The states of the system are represented by flip-flops (or registers). Both next state logic and output logic are entirely combinational logic. A simple block diagram for a state machine is shown in Fig. 1 which contains a Present State Logic section, Present State section, and an Output Logic section. Next State Logic Present State (Register) Output Logic Inputs Outputs Present State Next State Fig. 1. Block diagram of a state machine. 2. FSM with VHDL We will now introduce a VHDL template to implement a state machine based on the model shown in Fig. 1. The VHDL file will include 3 main sections: Present State section: o The function of this section is to assign the next state to the present state at the active clock edge. o An asynchronous reset signal should be included to initialize the system to the default first state of the system. o This section is implemented with sequential (behavioral) VHDL code with a PROCESS. Next State Logic section: o The function of this section is to establish the next state of the system. o This section is implemented with sequential (behavioral) VHDL code with a PROCESS. 1

Upload: milus-cava

Post on 09-May-2017

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lesson 18 FSM VHDL Student

Lesson 18

Finite State Machine (FSM) with VHDL

1. Overview of FSM Recall that FSM is used to model a sequential logic circuit. A sequential logic circuit has three main parts:

1. Present state of the system 2. Next state logic 3. Output logic

The states of the system are represented by flip-flops (or registers). Both next state logic and output logic are entirely combinational logic. A simple block diagram for a state machine is shown in Fig. 1 which contains a Present State Logic section, Present State section, and an Output Logic section.

Next State Logic

Present State

(Register)

Output LogicInputs Outputs

Present State

Next State

Fig. 1. Block diagram of a state machine. 2. FSM with VHDL We will now introduce a VHDL template to implement a state machine based on the model shown in Fig. 1. The VHDL file will include 3 main sections:

• Present State section:

o The function of this section is to assign the next state to the present state at the active clock edge. o An asynchronous reset signal should be included to initialize the system to the default first state

of the system. o This section is implemented with sequential (behavioral) VHDL code with a PROCESS.

• Next State Logic section:

o The function of this section is to establish the next state of the system. o This section is implemented with sequential (behavioral) VHDL code with a PROCESS.

1

Page 2: Lesson 18 FSM VHDL Student

• Output Logic section:

o The function of this section is to generate the outputs of the system. o This section is implemented with concurrent VHDL code with conditional assignment

statements. Example: Implement the following ASM diagram using VHDL.

Fig. 2. ASM diagram example 1.

VHDL code:

Input: x Output: z

2

Page 3: Lesson 18 FSM VHDL Student

3

Page 4: Lesson 18 FSM VHDL Student

3. Implementing Digital Filters with VHDL When data is transmitted, a certain amount of noise is mixed with the actual digital signals. For applications that require accurate values from digital inputs (e.g. obstacle avoidance robot lab), it is necessary to incorporate digital filters into the circuits to remove noise from the input signals. One of the simplest ways to implement such a filter is to use a counter to determine if the input signals have been continuously active for a fixed period of time. An example of the noisy input x of the system is depicted in Fig. 3. A counter is incremented every clock cycle if the input x is active (‘1’ in this example); else it is reset to 0. When the counter reaches a fixed value, it is considered stable and a new signal called “filtered_x” is set to the active state (‘1’ in this case) as shown in Fig. 3. The “filtered_x” is reset to 0 if the actual input x signal is 0. The “filtered_x” signal is then used in the state machine instead of the actual input x signal. The digital filter is used to generate a more stable input before it is fed to the Next State Logic and the Output Logic section of the state machine as shown in Fig. 4.

4

Page 5: Lesson 18 FSM VHDL Student

Fig. 3. Noisy input and filtered input.

Next State Logic

Present State

Output LogicInputs OutputsDigital

Filter

Present State

Next State

Filtered Inputs

Fig. 4. Block diagram of a state machine with a digital filter. VHDL code (ASM shown in Fig. 2, with a digital filter):

5

Page 6: Lesson 18 FSM VHDL Student

The Present State section is the same. The Output Logic section is the same since this is a Moore state machine.

6

Page 7: Lesson 18 FSM VHDL Student

A digital filter is used to generate more stable signal (filtered_x) based on the input signal (x):

7

Page 8: Lesson 18 FSM VHDL Student

The filtered signal is then used in the Next State Logic section:

4. FSM with Time Delay Design a sequential system with one input, x, and a 2-bit output z(1:0). The circuit outputs the sequence:

1, 2, 3, 1, 2, …. when x = 0

1, 3, 2, 1, 3, …. when x = 1

In addition, the circuit should behave based on the following characteristics:

• Number 1 (012) should be outputted for 1 second. • Number 2 (102) should be outputted for 2 seconds.

8

Page 9: Lesson 18 FSM VHDL Student

• Number 3 (112) should be outputted for 3 seconds. • One asynchronous, active high reset signal that puts the system in the initial state (number 1). • The system is operated with a 5 Hz clock signal.

The state diagram of the system is (without timing information).

S001

S110

S211

0

1

Input: xOutput: z(1:0)

0

01 1

Fig. 5. State diagram example 2.

For timing consideration, we will use a counter to determine the delay periods. The clock frequency is 5 Hz, so the clock period is 200ms. The relationship between time delay and the corresponding count value is summarized in the table below.

State Delay Count Value 1 1 second 2 2 seconds 3 3 seconds

The time delay for each state now can be represented by CONSTANTs in VHDL code. VHDL code:

9

Page 10: Lesson 18 FSM VHDL Student

10

Page 11: Lesson 18 FSM VHDL Student

11