Finite state machine is in very basic Verilog is very important knowledge. In this paper, finite state machine to do a brief introduction.
1. The three elements of the state machine
Finite state machine has three elements: the state of the jump, jump is determined, the state of operation;
1) Status Jump: Jump to the secondary state, current state;
2) determination Jump: Jump state determination condition;
3) Operation Status: state corresponds to an operation;
2. The implementation of the state machine
1) one-segment: The state machine is integrated in a three elements always block.
1 always@(posedge clk or negedge rst_n) 2 begin 3 if(!rst_n)begin 4 state <= 2'b00; 5 Qout <= 1'b0; 6 end 7 else case(state) 8 2'b00: begin 9 if(A)begin 10 state <= 2'b01; 11 Qout <= 1'b1; 12 end 13 else begin 14 state <= 2'b00; 15 Qout <= 1'b0; 16 end 17 end 18 2'b01: begin 19 if(!A)begin 20 state <= 2'b00; 21 Qout <= 1'b0; 22 end 23 else begin 24 state <= 2'b01; 25 Qout <= 1'b1; 26 end 27 end 28 default:; 29 endcase 30 end
2) Two-stage: The state machine design in two of the three elements are always blocks.
1 always@(posedge clk or negedge rst_n) 2 begin 3 if(!rst_n) 4 state <= 2'b00; 5 else case(state) 6 2'b00: begin 7 if(A) 8 state <= 2'b01; 9 else 10 state <= 2'b00; 11 end 12 2'b01: begin 13 if(!A) 14 state <= 2'b00; 15 else 16 state <= 2'b01; 17 end 18 default:; 19 endcase 20 end 21 22 always@(posedge clk or negedge rst_n) 23 begin 24 if(!rst_n) 25 Qout <= 1'b0; 26 else case(state) 27 2'b00: Qout <= 1'b0; 28 2'b01: Qout <= 1'b1; 29 default:; 30 endcase 31 end
3 ) three-stage: a state machine with three elements, respectively, three always block.
. 1 Always @ ( posedge CLK or negedge RST_N) // state transition 2 the begin . 3 IF (! RST_N) . 4 the current_state <= 2 ' B00; // reset . 5 the else . 6 the current_state <= next_state // refresh clock rising edge in the current state . 7 End . 8 . 9 Always @ (the current_state) // jump judgment 10 the begin . 11 Case (the current_state) 12 is 2 'b00:begin 13 if(A) 14 next_state = 2'b01; 15 else 16 next_state = 2'b00; 17 end 18 2'b01:begin 19 if(!A) 20 next_state = 2'b00; 21 else 22 next_state = 2'b01; 23 end 24 default:; 25 end 26 is 27 Always @ (*) // state operation 28 the begin 29 Case (the current_state) 30 2 ' B00: Qout is <=. 1 ' B0; 31 is 2 ' B01: Qout is <=. 1 ' B1; 32 default :; 33 is ENDCASE 34 is End
3. The state machine design requirements
1) Select the appropriate style according to the design requirements;
2) case statement should be added to the default statement;
3) the clever DFF relay, improve reliability;
4) Class hot encoded using the Gray code design condition;
5) using global reset remember;