题解 | #自动售卖机#

自动售卖机

https://www.nowcoder.com/practice/487953e6d3e3434988e0dd6960b6c9f8

`timescale 1ns/1ns

module sale(
   input                clk   ,
   input                rst_n ,
   input                sel   ,//sel=0,5$dranks,sel=1,10&=$drinks
   input          [1:0] din   ,//din=1,input 5$,din=2,input 10$
 
   output   reg  [1:0] drinks_out,//drinks_out=1,output 5$ drinks,drinks_out=2,output 10$ drinks
   output	reg        change_out   
);

reg cs,ns;
always @(*) begin
    case(cs)
    1'b0: ns = (sel & (din == 2'b01))? 1'b1 : 1'b0;
    1'b1: ns = din? 1'b0 : 1'b1;
    endcase
end

always @(posedge clk or negedge rst_n) begin
    if(~rst_n) begin
        drinks_out <= 2'b00;
        change_out <= 1'b0;
        cs <= 1'b0;
    end
    else begin
        cs <= ns;
        if(~cs) begin
            case({sel,din})
            3'b010: begin
                drinks_out <= 2'b01;
                change_out <= 1'b1;
            end
            3'b001: begin
                drinks_out <= 2'b01;
                change_out <= 1'b0;
            end   
            3'b110: begin
                drinks_out <= 2'b10;
                change_out <= 1'b0;
            end
            default: begin
                drinks_out <= 2'b00;
                change_out <= 1'b0;
            end       
            endcase
        end
        else begin
            case(din)
            2'b01: begin
                drinks_out <= 2'b10;
                change_out <= 1'b0;
            end 
            2'b10: begin
                drinks_out <= 2'b10;
                change_out <= 1'b1;
            end
            endcase
        end
    end
end

endmodule 

这道题使用Mealy状态机实现是比较容易的,需要注意的是出饮料和找零的当拍就能再次购买,所以出饮料和找零不需要单独设置状态。只设置两种状态是是相对比较清楚的,我之前使用三四种状态,反而复杂了这个问题。

全部评论
下面的狀態欄畫錯了拉
1 回复 分享
发布于 2024-04-26 21:55 台湾

相关推荐

不愿透露姓名的神秘牛友
11-19 14:56
点赞 评论 收藏
分享
算法冲刺中:kpi面加一,面完完全没动静,感谢信都没有
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

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