Laboratory Exercise 7 Finite State Machines This is an exercise in using finite state machines. PartI We wish to implement a finite state machine (EsM that recognizes two specific sequences of applied input sym bols,namely four consecutive Is or four consecutive 0s.There is an inputand an output=.Whenever1o eo four consecutive clock puseock pulses the output will be equal to I after the fo ck几几uuh几u几几几U Figure 1.Required timing for the output= Aed e dia the FSM use nine state flip-flops called ys.and the one-hot state assignment given in Table 1. State Code Name ysy7y6ysyysy2yo 000000001 <mU 000000100 00000100 EFG 00001000 01000000d 100000000 Table 1.One-hot codes for the FSM
Laboratory Exercise 7 Finite State Machines This is an exercise in using finite state machines. Part I We wish to implement a finite state machine (FSM) that recognizes two specific sequences of applied input symbols, namely four consecutive 1s or four consecutive 0s. There is an input w and an output z. Whenever w = 1 or w = 0 for four consecutive clock pulses the value of z has to be 1; otherwise, z = 0. Overlapping sequences are allowed, so that if w = 1 for five consecutive clock pulses the output z will be equal to 1 after the fourth and fifth pulses. Figure 1 illustrates the required relationship between w and z. Clock w z Figure 1. Required timing for the output z. A state diagram for this FSM is shown in Figure 2. For this part you are to manually derive an FSM circuit that implements this state diagram, including the logic expressions that feed each of the state flip-flops. To implement the FSM use nine state flip-flops called y8,...,y0 and the one-hot state assignment given in Table 1. State Code Name y8y7y6y5y4y3y2y1y0 A 000000001 B 000000010 C 000000100 D 000001000 E 000010000 F 000100000 G 001000000 H 010000000 I 100000000 Table 1. One-hot codes for the FSM. 1
A/0 B/0 F/O C/O G/O =0 D/0 Figure 2.A state diagram for the FSM. Design and implement your circuit on the DE2 board as follows. and which fies the t sta h sp VHDL logic feeding the fp-flops.Note that the one-hot code enables youto derive these expressions by inspection ut for the FSM the green LED LEDGo as the output=,and assign the state flip-flop outputs to the red LEDs LEDRsto LEDRo. 4.Simulate the behavior of your circuit 5.0 nt that works prope y as a result of you the output LEDs.Make sure that the FSM p and that it produces the correct output values on LEDGo. 6.Finally,consider a modification of the one-hot code given in Table 1.When an FSM is going to be im- plemented in a FPGA,the circuit cano n be simp ip-flop outputs are 0 whe n th FSM iS i port. 2
0 E/1 Reset w = 0 D/0 w = 0 C/0 w = 0 B/0 A/0 I/1 1 1 H/0 1 G/0 1 F/0 w = 0 w = 1 1 0 1 1 1 0 0 0 Figure 2. A state diagram for the FSM. Design and implement your circuit on the DE2 board as follows. 1. Create a new Quartus II project for the FSM circuit. Select as the target chip the Cyclone II EP2C35F672C6, which is the FPGA chip on the Altera DE2 board. 2. Write a VHDL 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 assignment statements in your VHDL 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 on the Altera DE2 board 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 green LED LEDG0 as the output z, and assign the state flip-flop outputs to the red LEDs LEDR8 to LEDR0. 3. Include the VHDL file in your project, and assign the pins on the FPGA to connect to the switches and the LEDs, as indicated in the User Manual for the DE2 board. Compile the circuit. 4. Simulate the behavior of your circuit. 5. Once you are confident that the circuit works properly as a result of your simulation, download the circuit into the FPGA chip. Test the functionality of your design by applying the input sequences and observing the output LEDs. Make sure that the FSM properly transitions between states as displayed on the red LEDs, and that it produces the correct output values on LEDG0. 6. Finally, consider a modification of the one-hot code given in Table 1. When an FSM is going to be implemented in an FPGA, the circuit can often be simplified if all flip-flop outputs are 0 when the FSM is in the reset state. This approach is preferable because the FPGA’s flip-flops usually include a clear input port, which can be conveniently used to realize the reset state, but the flip-flops often do not include a set input port. 2
Table 2 shows modified one-hot state assi ished bthetateCreatemofied ersion of your VHDL that implemnt this state assignment.Hint:you should need to make very few changes to the logic expressions in you d codes.Compile your new circuit and test it both through simulation and State code Name ysy7654s2h 0000 00000101 000001001 00001000 00I000 010000001 100000001 Table 2.Modified one-hot codes for the FSM. Part For this part you are to write another style of VHDL code for the FSM in Figure 2.In this version of the code you should not manually derive the logic expressions needed for each state flip-flop.Instead,describe the state table for the inantiate theaeYou c ROCESS block or imple ment statemenstopi the FSM b ESS block,and use another PROCESS block t Asuggested skeleton of the VHDL code is given in Figure 3.Observe that the present and next state vectors for the FSM are defined as an enumerated type with possible values given by the symbols A to I.The VHDL compiler determines how many state flip-flops to use for the circuit,and it automatically chooses the state assignment
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 VHDL 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 codes. Compile your new circuit and test it both through simulation and by downloading it onto the DE2 board. State Code Name y8y7y6y5y4y3y2y1y0 A 000000000 B 000000011 C 000000101 D 000001001 E 000010001 F 000100001 G 001000001 H 010000001 I 100000001 Table 2. Modified one-hot codes for the FSM. Part II For this part you are to write another style of VHDL code for the FSM in Figure 2. In this version of the code you should not manually derive the logic expressions needed for each state flip-flop. Instead, describe the state table for the FSM by using a VHDL CASE statement in a PROCESS block, and use another PROCESS block to instantiate the state flip-flops. You can use a third PROCESS block or simple assignment statements to specify the output z. A suggested skeleton of the VHDL code is given in Figure 3. Observe that the present and next state vectors for the FSM are defined as an enumerated type with possible values given by the symbols A to I. The VHDL compiler determines how many state flip-flops to use for the circuit, and it automatically chooses the state assignment. 3
ENTITY part2 IS PORT(...define input and output ports END part2: ARCHITECTURE Behavior OF part2 IS IS (A.B.C.D.E.F.G,H.D): SIGNALy Q.Y_D:State_type: --yQis present state,yD is next state BEGIN casey_QIS WHEN A IF (w='0')THEN Y_D<=B, YD<=F; other states END CASE: END PROCESS;--state table PROCESS(Clock)--state flip-flops BEGIN END PROCESS: assignments for outputz and the LEDs END Behavior. Figure 3.Skeleton VHDL code for the FSM Implement your circuit as follows. 1.Create a new project for the FSM.Select as the target chip the Cyelone lI EP2C35F672C6 2.Include in the project your VHDL file that uses the style of code in Figure3.Use the toggle switchW the Altera DE bod as an activ-ow ,and the the anually. e the green L o as th e outpu he switchesand the LEDs.s indicated n the User Manual for the DE2 board. 3.Before compilir your code it is possible to tell the Synthesis tool in Ouartus Il what stvle of state as shouldChoosegmentsngQuand then cik thnyss nd Synthesis item on the left side I the wi w.As indicated in Figure 4.change the parameter State Processing to the setting Minimal Bits y Ouartus I oroperly corresponds to the one in Figure 2.To see the state codes used for your FSM,o en the Compilation Report,select the Analysis and Synthesis section of the report,and click on State Machines. 5.Simulate the behavior of your cireuit
LIBRARY ieee; USE ieee.std_logic_1164.all; ENTITY part2 IS PORT ( ... define input and output ports ...); END part2; ARCHITECTURE Behavior OF part2 IS ... declare signals TYPE State_type IS (A, B, C, D, E, F, G, H, I); SIGNAL y_Q, Y_D : State_type; - - y_Q is present state, y_D is next state BEGIN ... PROCESS (w, y_Q) - - state table BEGIN case y_Q IS WHEN A IF (w = ’0’) THEN Y_D Settings in Quartus II, and then click on the Analysis and Synthesis item on the left side of the window. As indicated in Figure 4, change the parameter State Machine Processing to the setting Minimal Bits. 4. To examine the circuit produced by Quartus II 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 Figure 2. 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. Simulate the behavior of your circuit. 4
6.Once onfident that the download the circui the output LEDs.Make sure that the FSM properly transitions between states as displaved on the red LEDs and that it produces the correct output values on LEDGo. nts>Settinas and clict Machine Processing from Minimal Bits to One-Hot Recompile the circuit and then open the report file. Settines-pert2 Auto Open-Orain Pre o8aao8m o☐ce Figure 4.Specifying the state assignment method in Quartus II Part II For this part you are to implement the sequence-detector FSM by using shift registers,instead of using the more Poqaeneorihdaboe formal approach des Create VHDL code that instantiates two 4-bit shift registers.one is for recog- ur os, d the other for four Is. the switches and LEDs on the board n asimilar way as you did for Parts I and I and observe the behavior of your shift registers and the output=Answer the following question:could you use just one 4-bit shift register,rather than two?Explain your answer. 5
6. Once you are confident that the circuit works properly as a result of your simulation, download the circuit into the FPGA chip. Test the functionality of your design by applying the input sequences and observing the output LEDs. Make sure that the FSM properly transitions between states as displayed on the red LEDs, and that it produces the correct output values on LEDG0. 7. In step 3 you instructed the Quartus II Synthesis tool to use the state assignment given in your VHDL code. To see the result of changing this setting, open again the Quartus II settings window by choosing Assignments > Settings, and click on the Analysis and Synthesis item. Change the setting for State Machine Processing from Minimal Bits to One-Hot. Recompile the circuit and then open the report file, select the Analysis and Synthesis section of the report, and click on State Machines. Compare the state codes shown to those given in Table 2, and discuss any differences that you observe. Figure 4. Specifying the state assignment method in Quartus II. Part III For this part you are to implement the sequence-detector FSM by using shift registers, instead of using the more formal approach described above. Create VHDL code that instantiates two 4-bit shift registers; one is for recognizing a sequence of four 0s, and the other for four 1s. Include the appropriate logic expressions in your design to produce the output z. Make a Quartus II project for your design and implement the circuit on the DE2 board. Use the switches and LEDs on the board in a similar way as you did for Parts I and II and observe the behavior of your shift registers and the output z. Answer the following question: could you use just one 4-bit shift register, rather than two? Explain your answer. 5
Part IV We want to design a modulo-10 counter-like circuit that behaves as follows.It is reset to 0by the Reset input.It has two inputs,w and o which control its counting operation.Ifw00,the count remains the same.If cousW for e p nd ve edge of ac manual Dsp the dma ofe rhmentispa 1.Create a new project which will be used to implement the circuit on the DE2 board. 2.Write a VHDL file that defines the circuit.Use the style of code indicated in Figure3 for your FSM. 3.Include the VHDL file in your project and compile the circuit. 4.Simulate the behavior of your circuit. 5.Assign the pins on the FPGA to connect to the switches and the 7-segment display 6.Recompile the circuit and download it into the FPGA chip. 7.Test the functionality of your design by applying some inputs and observing the output display. Part V For this part you are to design a circuit for the DE2 board that scrolls the word"HELLOin ticker-tape fashion on the eight7-segment displays.The letters should move from right to efteach time you apply a manual en starts again or our circuit by using eight 7hit registers co ected in a o ue-like fashion.such that the outputs of the nuthe end fds the thrd This are to t controls the pipeline in two ways h the SM connects the last register back to ock the o SMthe ticker ape circuit an code in the style shown in Figurefor your Compile your VHDL code,download it onto the DE2 board and test the circuit PartVI For this part you are to modify your circuit from Part V so that it no longer requires manually-applied clock pulses Your circuit should s ll the word "HELLO"such that the lette off the left side of the displays it Write VHDLcode for the ticker-tape circuit and createa Quartus Iproject for your design.Use the 50-MHzclock signal,CLOCK_50,on the DE2 oard to clock the FSM and pipeline registers and use KEY as a synchronous Write VHDI e any oth ignals in Compile your VHDL code,download it onto the DE2 board and test the circuit 6
Part IV We want to design a modulo-10 counter-like circuit that behaves as follows. It is reset to 0 by the Reset input. It has two inputs, w1 and w0, which control its counting operation. If w1w0 = 00, the count remains the same. If w1w0 = 01, the count is incremented by 1. If w1w0 = 10, the count is incremented by 2. If w1w0 = 11, the count is decremented by 1. All changes take place on the active edge of a Clock input. Use toggle switches SW 2 and SW1 for inputs w1 and w0. Use toggle switch SW0 as an active-low synchronous reset, and use the pushbutton KEY0 as a manual clock. Display the decimal contents of the counter on the 7-segment display HEX0. 1. Create a new project which will be used to implement the circuit on the DE2 board. 2. Write a VHDL file that defines the circuit. Use the style of code indicated in Figure 3 for your FSM. 3. Include the VHDL file in your project and compile the circuit. 4. Simulate the behavior of your circuit. 5. Assign the pins on the FPGA to connect to the switches and the 7-segment display. 6. Recompile the circuit and download it into the FPGA chip. 7. Test the functionality of your design by applying some inputs and observing the output display. Part V For this part you are to design a circuit for the DE2 board that scrolls the word "HELLO" in ticker-tape fashion on the eight 7-segment displays HEX7 − 0. The letters should move from right to left each time you apply a manual clock pulse to the circuit. After the word "HELLO" scrolls off the left side of the displays it then starts again on the right side. Design your circuit by using eight 7-bit registers connected in a queue-like fashion, such that the outputs of the first register feed the inputs of the second, the second feeds the third, and so on. This type of connection between registers is often called a pipeline. Each register’s outputs should directly drive the seven segments of one display. You are to design a finite state machine that controls the pipeline in two ways: 1. For the first eight clock pulses after the system is reset, the FSM inserts the correct characters (H,E,L,L,0, , , ) into the first of the 7-bit registers in the pipeline. 2. After step 1 is complete, the FSM configures the pipeline into a loop that connects the last register back to the first one, so that the letters continue to scroll indefinitely. Write VHDL code for the ticker-tape circuit and create a Quartus II project for your design. Use KEY 0 on the DE2 board to clock the FSM and pipeline registers and use SW0 as a synchronous active-low reset input. Write VHDL code in the style shown in Figure 3 for your finite state machine. Compile your VHDL code, download it onto the DE2 board and test the circuit. Part VI For this part you are to modify your circuit from Part V so that it no longer requires manually-applied clock pulses. Your circuit should scroll the word "HELLO" such that the letters move from right to left in intervals of about one second. Scrolling should continue indefinitely; after the word "HELLO" scrolls off the left side of the displays it should start again on the right side. Write VHDL code for the ticker-tape circuit and create a Quartus II project for your design. Use the 50-MHz clock signal, CLOCK_50, on the DE2 board to clock the FSM and pipeline registers and use KEY 0 as a synchronous active-low reset input. Write VHDL code in the style shown in Figure 3 for your finite state machine, and ensure that all flip-flops in your circuit are clocked directly by the CLOCK_50 input. Do not derive or use any other clock signals in your circuit. Compile your VHDL code, download it onto the DE2 board and test the circuit. 6
Part VIl 1 is pressed,the letters sh a move twice as p Note that the KEY2 and KEY switches are debounced and will produce exactly one low pulse when pressed However,there is no way of knowing how long a switch may remain depressed,which means that the pulse a se your second FSM can be used as part of the scheme for creating a variable time interval in your circuit.Note that KEY2 ndre asynchronous inputs to your circut,so be sure to synchronize them to the clock signal before using hes When the circuit is s at about one second interrrpeauthe rinpeoublmimm four lettersper eon strate that it works properly Copyright 2006 Altera Corporation
Part VII Augment your design from Part VI so that under the control of pushbuttons KEY 2 and KEY1 the rate at which the letters move from right to left can be changed. If KEY 1 is pressed, the letters should move twice as fast. If KEY2 is pressed, the rate has to be reduced by a factor of 2. Note that the KEY2 and KEY1 switches are debounced and will produce exactly one low pulse when pressed. However, there is no way of knowing how long a switch may remain depressed, which means that the pulse duration can be arbitrarily long. A good approach for designing this circuit is to include a second FSM in your VHDL code that properly responds to the pressed keys. The outputs of this FSM can change appropriately when a key is pressed, and the FSM can wait for each key press to end before continuing. The outputs produced by this second FSM can be used as part of the scheme for creating a variable time interval in your circuit. Note that KEY 2 and KEY1 are asynchronous inputs to your circuit, so be sure to synchronize them to the clock signal before using these signals as inputs to your finite state machine. The ticker tape should operate as follows. When the circuit is reset, scrolling occurs at about one second intervals. Pressing KEY1 repeatedly causes the scrolling speed to double to a maximum of four letters per second. Pressing KEY2 repeatedly causes the scrolling speed to slow down to a minimum of one letter every four seconds. Implement your circuit on the DE2 board and demonstrate that it works properly. Copyright c 2006 Altera Corporation. 7