Verilog 状态机编写

  

Verilog状态机编写

一、状态机的编写分为3种

  1. 一段式:将状态转移、状态转移条件、输出全都放置在一个always模块中。

  2. 两段式:一个模块用时序逻辑描述状态转移,另一个模块利用组合逻辑描述状态转移条件及输出。

  3. 三段式:第一个模块利用时序逻辑编写状态转移(always、非阻塞原语)。

    第二个模块利用组合逻辑编写状态转移条件(always、阻塞原语)。

    第三个模块可以利用时序或组合逻辑来编写输出,但利用组合逻辑编写输出时,一般可能会产生毛刺。

二、Verilog HDL 数字设计与建模 ——Joseph Cavanagh

P285 例8.28 设计一个Mealy同步时序状态机,当检测到输入x端口出现了1001的序列时,输出z将被拉高。重叠的1001也要求被检测出来。


module mealy_ssm (x, clk, rst_n, state, z)
 input x, clk, rst_n;
 output state, z;
 
 wire z;
 reg [1:0] state, next_state;
 
 parameter state_a = 2'b00,
state_b = 2'b01,
state_c = 2'b11,
  state_d = 2'b10,
 
 //第一段用时序逻辑描述状态转移
 always @ (posedge clk or negedge rst_n)
  begin
         if (~rst_n)
          state <= state_a;
         else
          state <= next_state;
       end
 
 //第二段用组合逻辑描述状态转移条件
 always @ (x or state)
   begin
     case (state)
       state_a:  
         if (x==0)
        next_state = state_a;
         else
           next_state = state_b;
       state_b:
         if (x==0)
           next_state = state_b;
       else
          next_state = state_a;
       state_c:
         if (x==0)
           next_state = state_c;
         else
           next_state = state_a;
       state_d:
         if (x==0)
           next_state = state_a;
         else
           next_state = state_b;
       default: next_state = state_a;
     endcase
   end
 
 //输出采用了组合逻辑
 assign z1 = (state[1] && ~state[0] && x && clk);

endmodule

 

 

posted @ 2018-06-26 15:54  jtclm  阅读(333)  评论(0编辑  收藏  举报