题解主体
有限状态机简写为FSM(Finite State Machine),主要分为2大类:
第一类,输出只和状态有关而与输入无关,则称为Moore状态机;
第二类,输出不仅和状态有关而且和输入有关系,则称为Mealy状态机。
Mealy型:输出信号不仅取决于当前状态,还取决于输入;
Moore型:输出信号只取决于当前状态;
实现相同的功能时,Mealy型比Moore型能节省一个状态(大部分情况下能够节省一个触发器资源),Mealy型比Moore型输出超前一个时钟周期。
本题要实现的是Moore状态机电路,根据题目要求,可画出状态转移图如下:
结合状态转换图,可得Verilog代码描述如下:
localparam IDLE = 4'b0000 ; localparam S0 = 4'b0001 ; localparam S1 = 4'b0010 ; localparam S2 = 4'b0100 ; localparam S3 = 4'b1000 ; reg [3:0] curr_state; reg [3:0] next_state; always @(posedge clk or negedge rst_n)begin if(!rst_n) curr_state <= IDLE; else curr_state <= next_state; end always @(*)begin case(curr_state) IDLE: begin if(din) next_state = S0; else next_state = IDLE; end S0: begin if(din) next_state = S1; else next_state = IDLE; end S1: begin if(~din) next_state = S2; else next_state = S1; end S2: begin if(din) next_state = S3; else next_state = IDLE; end S3: begin if(din) next_state = S0; else next_state = IDLE; end default: next_state = IDLE; endcase end always @(posedge clk or negedge rst_n)begin if(!rst_n)begin Y <= 1'b0; end else if(curr_state == S3) Y <= 1'b1; else Y <= 1'b0; end参考答案
`timescale 1ns/1ns module det_moore( input clk , input rst_n , input din , output reg Y ); localparam IDLE = 4'b0000 ; localparam S0 = 4'b0001 ; localparam S1 = 4'b0010 ; localparam S2 = 4'b0100 ; localparam S3 = 4'b1000 ; reg [3:0] curr_state; reg [3:0] next_state; always @(posedge clk or negedge rst_n)begin if(!rst_n) curr_state <= IDLE; else curr_state <= next_state; end always @(*)begin case(curr_state) IDLE: begin if(din) next_state = S0; else next_state = IDLE; end S0: begin if(din) next_state = S1; else next_state = IDLE; end S1: begin if(~din) next_state = S2; else next_state = S1; end S2: begin if(din) next_state = S3; else next_state = IDLE; end S3: begin if(din) next_state = S0; else next_state = IDLE; end default: next_state = IDLE; endcase end always @(posedge clk or negedge rst_n)begin if(!rst_n)begin Y <= 1'b0; end else if(curr_state == S3) Y <= 1'b1; else Y <= 1'b0; end endmodule
如果关于此次题单有问题或者反馈意见,欢迎加入牛客用户反馈群沟通~