题解 | #自动贩售机1#

自动贩售机1

https://www.nowcoder.com/practice/dcf59e6c51f6489093495acb1bc34dd8

`timescale 1ns/1ns
module seller1(
	input wire clk  ,
	input wire rst  ,
	input wire d1 ,
	input wire d2 ,
	input wire d3 ,
	
	output reg out1,
	output reg [1:0]out2
);
/*
//这是一个不完全正确的mealy状态机,输出取决于当前输入和状态。单状态图值得学习
localparam s0=0,s05=1,s1=2,s15=3,s2=4,s25=5,s3=6;
reg [2:0] state,next_state;

always@(posedge clk or negedge rst)
	if(~rst)
		state<=0;
	else
		state<=next_state;

always@(*)begin
	next_state=s0;
	case(state)
		s0:next_state=d1?s05:
						d2?s1:
						d3?s2:s0;
		s05:next_state=d1?s1:
						d2?s15:
						d3?s25:s05;
		s1:next_state=d1?s15:
						d2?s2:
						d3?s3:s1;
		s15, s2, s25, s3: next_state = s0;
	endcase
end

always@(*)
	if(~rst)
		out1=0;
	else
		out1=state==s15||state==s2||state==s25||state==s3;


//这个输出方法也值得学习,少写了很多状态
always@(*) begin
        if(~rst)
            out2 <= 0;
        else
            case(state)
                s2                : out2 <= 1;
                s25               : out2 <= 2;
                s3                : out2 <= 3;
                default           : out2 <= 0;
            endcase
    end
*/

    parameter S0=0, S0_5=1, S1=2, S1_5=3, S2=4, S2_5=5, S3=6;
    reg [2:0] state, nstate;
    
    always@(negedge clk or negedge rst) begin
        if(~rst)
            state <= S0;
        else
            state <= nstate;
    end
    
    always@(*) begin
        case(state)
            S0                : nstate = d1? S0_5:
                                         d2? S1  :
                                         d3? S2  :
                                         nstate;
            S0_5              : nstate = d1? S1  :
                                         d2? S1_5:
                                         d3? S2_5:
                                         nstate;
            S1                : nstate = d1? S1_5:
                                         d2? S2  :
                                         d3? S3  :
                                         nstate;
            S1_5, S2, S2_5, S3: nstate = S0;
            default           : nstate = S0;
        endcase
    end
    
    always@(posedge clk or negedge rst) begin
        if(~rst)
            out1 <= 0;
        else
            out1 <= state==S1_5||state==S2||state==S2_5||state==S3;
    end
    
    always@(posedge clk or negedge rst) begin
        if(~rst)
            out2 <= 0;
        else
            case(state)
                S0, S0_5, S1, S1_5: out2 <= 0;
                S2                : out2 <= 1;
                S2_5              : out2 <= 2;
                S3                : out2 <= 3;
                default           : out2 <= 0;
            endcase
    end
//*************code***********//


endmodule

关键是要有negedge来更新周期。

全部评论

相关推荐

07-20 11:20
门头沟学院 Java
Alan_01:看到都是黑马点评跟苍穹外卖我就放心了
无实习如何秋招上岸
点赞 评论 收藏
分享
08-27 21:03
已编辑
西南石油大学 Java
冷花幽露:大概率是了,京东面试就是这样。我上周一面也是20多分钟,面试官问的很刁钻的问题也答上来了,面完过了几天还是没推进,泡池子,昨天一看挂了。如果一面完第2天没有收到2面邀请,基本上不用抱希望了。如果你的bg是985,面试流程也是和我们一样,20多分钟,唯一区别就是面完他们会很快收到二面邮件,而不像我们泡池子然后挂掉
点赞 评论 收藏
分享
全A了,但是美团还有HC吗
投递美团等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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