题解 | #自动贩售机1#

自动贩售机1

http://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
);
//*************code***********//
    reg [3:0] curr_state;
    reg [3:0] next_state;
    
    always @ (posedge clk or negedge rst) begin
        if(~rst)
            curr_state<=4'b0;
        else
            curr_state<=next_state;
    end
    
    always @ (*) begin
        case (curr_state)
            4'd0:next_state<=d1?4'd1:d2?4'd2:d3?4'd3:next_state;
            4'd1:next_state<=d1?4'd2:d2?4'd4:d3?4'd5:next_state;//0.5
            4'd2:next_state<=d1?4'd4:d2?4'd3:d3?4'd6:next_state;//1
            4'd3:next_state<=4'd0;//2
            
            4'd4:next_state<=4'd0;//1.5
            4'd5:next_state<=4'd0;//2.5
            4'd6:next_state<=4'd0;//3
            default:next_state<=4'd0;
        endcase
    end
    
    always @ (*)begin
        if(~rst) begin
            out1=0;
            out2=2'b0;
        end
        else begin
            out1=(curr_state==4'd3||curr_state==4'd4||curr_state==4'd5||curr_state==4'd6);
            out2=(curr_state==4'd3)?1:(curr_state==4'd4)?0:(curr_state==4'd5)?2:(curr_state==4'd6)?3:0;                
        end
    end


//*************code***********//
endmodule
全部评论

相关推荐

10-15 10:57
已编辑
武昌理工学院 FPGA工程师
狠赚笔第一人:老哥学院本没实习还想拿13k学Java狠赚笔呢
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务