[ Chapter start ] [ Previous page ] [ Next page ] 12.7 Finite-State Machine SynthesisThere are three ways to synthesize a finite-state machine ( FSM ):
Most synthesis tools require that you write a state machine using a certain style—a special format or template. Synthesis tools may also require that you declare an FSM, the encoding, and the state register using a synthesis directive or special software command. Common FSM encoding options are:
You need to consider how the reset of the state register will be handled in the synthesized hardware. In a programmable ASIC there are often limitations on the polarity of the flip-flop resets. For example, in some FPGAs all flip-flop resets must all be of the same polarity (and this restriction may or may not be present or different for the internal flip-flops and the flip-flops in the I/O cells). Thus, for example, if you try to assign the reset state as '0101' , it may not be possible to set two flip-flops to '0' and two flip-flops to '1' at the same time in an FPGA. This may be handled by assigning the reset state, resSt , to '0000' or '1111' and inverting the appropriate two bits of the state register wherever they are used. You also need to consider the initial value of the state register in the synthesized hardware. In some reprogrammable FPGAs, after programming is complete the flip-flops may all be initialized to a value that may not correspond to the reset state. Thus if the flip-flops are all set to '1' at start-up and the reset state is '0000' , the initial state is '1111' and not the reset state. For this reason, and also to ensure fail-safe behavior, it is important that the behavior of the FSM is defined for every possible value of the state register. 12.7.1 FSM Synthesis in VerilogThe following FSM model uses paired processes . The first process synthesizes to sequential logic and the second process synthesizes to combinational logic: module StateMachine_1 (reset, clk, yOutReg); input reset, clk; output yOutReg; reg yOutReg, yOut; reg [1:0] curSt, nextSt; always @( posedge clk or posedge reset) begin :Seq //Compass statemachine oneHot curSt begin yOut = 0; yOutReg = yOut; curSt = `resSt; end `resSt:yOut = 0;`S1:yOut = 1;`S2:yOut = 1;`S3:yOut = 1; yOutReg = yOut; curSt = nextSt; // ... update the state. always @(curSt or yOut) // Assign the next state: `resSt:nextSt = `S3; `S1:nextSt = `S2; `S2:nextSt = `S1; `S3:nextSt = `S1; Synopsys uses separate pseudocomments to define the states and state vector as in the following example: module StateMachine_2 (reset, clk, yOutReg); input reset, clk; output yOutReg; reg yOutReg, yOut; parameter [1:0] //synopsys enum states resSt = 2'b00, S1 = 2'b01, S2 = 2'b10, S3 = 2'b11; reg [1:0] /* synopsys enum states */ curSt, nextSt; always @( posedge clk or posedge reset) begin begin yOut = 0; yOutReg = yOut; curSt = resSt; end case (curSt) resSt:yOut = 0;S1:yOut = 1;S2:yOut = 1;S3:yOut = 1; yOutReg = yOut; curSt = nextSt; end resSt:nextSt = S3; S1:nextSt = S2; S2:nextSt = S1; S3:nextSt = S1; To change encoding we can assign states explicitly by altering lines 3 – 4 to the following, for example: 12.7.2 FSM Synthesis in VHDLThe first architecture that follows is a template for a Moore state machine: library IEEE; use IEEE.STD_LOGIC_1164. all ; port (aIn, clk : in Std_logic; yOut: out Std_logic); type state is (s1, s2, s3, s4); when s1 => yOut <= '0'; nS <= s4; when s2 => yOut <= '1'; nS <= s3; when s3 => yOut <= '1'; nS <= s1; when s4 => yOut <= '1'; nS <= s2; wait until clk = '1'; pS <= nS; An example input, aIn , is included but not used in the next state assignments. A reset is also omitted to further simplify this example. An FSM compiler extracts the state machine. Some companies use FSM compilers that are separate from the logic synthesizers (and priced separately) because the algorithms for FSM optimization are different from those for optimizing combinational logic. We can see what is happening by asking the Compass synthesizer to write out intermediate results. The synthesizer extracts the FSM and produces the following output in a state-machine language used by the tools: inputs; outputs yout_smo; clock clk; STATE S1 { let yout_smo=0 ; } --> S4; STATE S2 { let yout_smo=1 ; } --> S3; STATE S3 { let yout_smo=1 ; } --> S1; STATE S4 { let yout_smo=1 ; } --> S2; You can use this language to modify the FSM and then use this modified code as an input to the synthesizer if you wish. In our case, it serves as documentation that explains the FSM behavior. Using one-hot encoding generates the following structural Verilog netlist ( dfntnb is positive-edge–triggered D flip-flop, and nd03d0 is a three-input NAND): dfntnb sm_ps4(.D(sm_ps1_Q),.CP(clk),.Q(sm_ps4_Q),.QN(sm_ps4_QN)); dfntnb sm_ps3(.D(sm_ps2_Q),.CP(clk),.Q(sm_ps3_Q),.QN(sm_ps3_QN)); dfntnb sm_ps2(.D(sm_ps4_Q),.CP(clk),.Q(sm_ps2_Q),.QN(sm_ps2_QN)); dfntnb sm_ps1(.D(sm_ps3_Q),.CP(clk),.Q(sm_ps1_Q),.QN(\sm_ps1.QN )); nd03d0 i_6(.A1(sm_ps4_QN),.A2(sm_ps3_QN),.A3(sm_ps2_QN), .ZN(yout_smo)); (Each example shows only the logic cells and their interconnection in the Verilog structural netlists.) The synthesizer has assigned one flip-flop to each of the four states to form a 4-bit state register. The FSM output (renamed from yOut to yout_smo by the software) is taken from the output of the three-input NAND gate that decodes the outputs from the flip-flops in the state register. Using adjacent encoding gives a simpler result, dfntnb sm_ps2(.D(i_4_ZN),.CP(clk), .Q(\sm_ps2.Q ),.QN(sm_ps2_QN)); dfntnb sm_ps1(.D(sm_ps1_QN),.CP(clk),.Q(\sm_ps1.Q ),.QN(sm_ps1_QN)); oa04d1 i_4(.A1(sm_ps1_QN),.A2(sm_ps2_QN),.B(yout_smo),.ZN(i_4_ZN)); nd02d0 i_5(.A1(sm_ps2_QN), .A2(sm_ps1_QN), .ZN(yout_smo)); ( oa04d1 is an OAI21 logic cell, nd02d0 is a two-input NAND). In this case binary encoding for the four states uses only two flip-flops. The two-input NAND gate decodes the states to produce the output. The OAI21 logic cell implements the logic that determines the next state. The combinational logic in this example is only slightly more complex than that for the one-hot encoding, but, in general, combinational logic for one-hot encoding is simpler than the other forms of encoding. Using the option 'moore' for Moore encoding, we receive the following message from the FSM compiler: The states were assigned these codes: 0?? : S1 100 : S2 101 : S3 110 : S4 The FSM compiler has assigned three bits to the state register. The first bit in the state register is used as the output. We can see more clearly what has happened by looking at the Verilog structural netlist: dfntnb sm_ps3(.D(i_6_ZN),.CP(clk),.Q(yout_smo),.QN(sm_ps3_QN)); dfntnb sm_ps2(.D(sm_ps3_QN),.CP(clk),.Q(sm_ps2_Q),.QN(\sm_ps2.QN )); dfntnb sm_ps1(.D(i_5_ZN),.CP(clk),.Q(sm_ps1_Q),.QN(\sm_ps1.QN )); nr02d0 i_5(.A1(sm_ps3_QN),.A2(sm_ps2_Q),.ZN(i_5_ZN)); nd02d0 i_6(.A1(sm_ps1_Q),.A2(yout_smo),.ZN(i_6_ZN)); The output, yout_smo , is now taken directly from a flip-flop. This means that the output appears after the clock edge with no combinational logic delay (only the clock-to-Q delay). This is useful for FSMs that are required to produce outputs as soon as possible after the active clock edge (in PCI bus controllers, for example). The following code is a template for a Mealy state machine: library IEEE; use IEEE.STD_LOGIC_1164. all ; port (aIn, clk : in Std_logic; yOut: out Std_logic); type state is (s1, s2, s3, s4); when s2 => yOut <= '1'; nS <= s3; when s3 => yOut <= '1'; nS <= s1; [ Chapter start ] [ Previous page ] [ Next page ] |
© 2024 Internet Business Systems, Inc. 670 Aberdeen Way, Milpitas, CA 95035 +1 (408) 882-6554 — Contact Us, or visit our other sites: |
|
Privacy PolicyAdvertise |