题解 | #自动贩售机2#

自动贩售机2

https://www.nowcoder.com/practice/298dec1c3dce45c881f3e53e02558828

`timescale 1ns/1ns

module seller2(
	input wire clk  ,
	input wire rst  ,
	input wire d1 ,
	input wire d2 ,
	input wire sel ,
	
	output reg out1,
	output reg out2,
	output reg out3
);
//*************code***********//
    
    localparam    IDEL_st = 7'b000_0001;
    localparam    Half_st = 7'b000_0010;
    localparam    One_st  = 7'b000_0100;
    localparam    OneHalf_st = 7'b000_1000;
    localparam    Two_st = 7'b001_0000;
    localparam    TwoHalf_st = 7'b010_0000;
    localparam    Thre_st = 7'b100_0000;
    
    reg    [6:0]    cur_state;
    reg    [6:0]    nex_state;
    
    
    always@(posedge clk or negedge rst)
    begin
        if(!rst)
            cur_state <= IDEL_st;
        else
            cur_state <= nex_state;
    end
    
    
    always@(*)
    begin
        if(!rst)
            nex_state = IDEL_st;
        else
            begin
                case(cur_state)  
                    IDEL_st       :    begin
                        if(d1)            //一般这里的条件判断要么用case,要么用ifelse,不能用?:语句,因为这个语句在输入为x或者z时,其输出也会为这个,从而导致输出出错,谨慎为好还是case或者if
                                nex_state = Half_st;
                            else if(d2)
                                nex_state = One_st;
                            else
                                nex_state = nex_state;
                        end
                    Half_st       :    begin
                            if(d1)
                                nex_state = One_st;
                            else if(d2)
                                nex_state = OneHalf_st;
                            else
                                nex_state = nex_state;
                        end
                    One_st        :    begin
                            if(d1)
                                nex_state = OneHalf_st;
                            else if(d2)
                                nex_state = Two_st;
                            else
                                nex_state = nex_state;
                        end
                    OneHalf_st    :    begin    //进入可以输出的状态时要判断时输出还是继续输入,若是输出则转初始状态,若不是则继续输入
                            if(sel)
                                begin
                                    if(d1)
                                        nex_state = Two_st;
                                    else if(d2)
                                        nex_state = TwoHalf_st;
                                    else
                                        nex_state = nex_state;
                                end
                            else
                                nex_state = IDEL_st;
                        end
                    Two_st        :    begin
                            if(sel)
                                begin
                                    if(d1)
                                        nex_state = TwoHalf_st;
                                    else if(d2)
                                        nex_state = Thre_st;
                                    else
                                        nex_state = nex_state;
                                end
                            else
                                nex_state = IDEL_st;
                        end
                    TwoHalf_st    :    nex_state = IDEL_st;    //这两种就只能输出一种情况了,不能继续进行输入了,因为最高的输入是1,则两次1最高到2,买1就是必出不能再高了,所以以下两个都是只能买2
                    Thre_st       :    nex_state = IDEL_st;
                    default       :    nex_state = IDEL_st;
                endcase
            end
    end
    
    always@(posedge clk or negedge rst)
    begin
        if(!rst)
            begin
                out1 <= 1'b0;
                out2 <= 1'b0;
                out3 <= 1'b0;
            end
        else
            begin
                case(nex_state)
                    IDEL_st , Half_st , One_st    :    begin
                            out1 <= 1'b0;
                            out2 <= 1'b0;
                            out3 <= 1'b0;
                        end
                    OneHalf_st    :    begin
                        if(sel)
                                begin
                                    out1 <= 1'b0;
                                    out2 <= 1'b0;
                                    out3 <= 1'b0;
                                end
                            else
                                begin
                                    out1 <= 1'b1;
                                    out2 <= 1'b0;
                                    out3 <= 1'b0;
                                end
                        end
                    Two_st    :    begin
                            if(sel)
                                begin
                                    out1 <= 1'b0;
                                    out2 <= 1'b0;
                                    out3 <= 1'b0;
                                end
                            else
                                begin
                                    out1 <= 1'b1;
                                    out2 <= 1'b0;
                                    out3 <= 1'b1;
                                end 
                        end
                    TwoHalf_st    :    begin    //以下两个只能买2不能买1,因为买1在2元及以下就可,不能在这里买1,遵循的原则就是在投币前就选好了饮料
                            out1 <= 1'b0;
                            out2 <= 1'b1;
                            out3 <= 1'b0; 
                        end
                    Thre_st    :    begin
                            out1 <= 1'b0;
                            out2 <= 1'b1;
                            out3 <= 1'b1;  
                        end
                    default    :    begin
                            out1 <= 1'b0;
                            out2 <= 1'b0;
                            out3 <= 1'b0;
                        end
                endcase
            end
    end

//*************code***********//
endmodule

全部评论

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务