题解 | #根据状态转移写状态机-三段式#

根据状态转移写状态机-三段式

http://www.nowcoder.com/practice/d8394a6d31754e73ace8c394e9465e2a

`timescale 1ns/1ns

module fsm1(
	input wire clk  ,
	input wire rst  ,
	input wire data ,
	output reg flag
);
//*************code***********//
    parameter   S0 = 4'b0001,
                S1 = 4'b0010,
                S2 = 4'b0100,
                S3 = 4'b1000;
    
reg    [3:0]    st_cur;
reg    [3:0]    st_next;

always@(posedge clk or negedge rst)begin 
    if(!rst)begin 
       st_cur <= 4'b0; 
    end
    else    begin 
       st_cur <= st_next; 
    end
end

always@(*)begin 
    case(st_cur)
        S0 : st_next = data? S1:S0;
        S1 : st_next = data? S2:S1;
        S2 : st_next = data? S3:S2;
        S3 : st_next = data? S0:S3;
        default : st_next = S0;    
    endcase
end

always@(posedge clk or negedge rst)begin
    if(!rst)begin 
       flag <= 1'b0; 
    end
    else    if((st_cur == S3) && (data))begin 
        flag <= 1'b1;
    end
    else    begin 
       flag <= 1'b0; 
    end
end
//*************code***********//
endmodule
全部评论
s3状态下说明已经到来的101,在s3状态下直接就可以输出flag=1,为什么还需要(st_cur == S3) && (data))才输出 flag <= 1'b1??? 直接assign flag=(st_cur==s3)?1'b1:1'b0;不可以吗?还是必须要assign flag=(st_cur==s3&&data==1'b1)?1'b1:1'b0;???
1 回复 分享
发布于 2022-05-22 17:46

相关推荐

06-10 21:15
门头沟学院 Java
宁阿:好多这种没🧠的公司,他们估计都不知道毕业的人不能给安排实习岗
实习吐槽大会
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务