`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 save_five; //题目未说可以存钱 =1 存5块钱
always@(posedge clk or negedge rst_n)begin
    if(!rst_n)begin
        drinks_out <= 2'b0;
        change_out <= 1'b0;
        save_five <= 1'b0;
    end
    else if(sel)begin   //B饮料 10元
        if(din==2'd1)begin
            drinks_out <= 2'b0;
            change_out <= 1'b0;
            save_five <= 1'b1;
            if(save_five)begin
                drinks_out <= 2'd2;
                change_out <= 1'b0;
                save_five <= 1'b0;
            end
        end
        else if(din == 2'd2)begin
            drinks_out <= 2'd2;
            change_out <= 1'b0;
            if(save_five)begin
                drinks_out <= 2'd2;
                change_out <= 1'b1;
                save_five <= 1'b0;
            end
        end
        else begin
            drinks_out  <= 2'b0;
            change_out <= 1'b0;
        end
    end
    else begin  //A 5
        if(din == 2'd1)begin
            change_out<=1'b0;
            drinks_out <= 2'd1;
        end
        else if(din == 2'd2)begin
            change_out <= 1'b1;
            drinks_out <= 2'd1;
        end
        else begin
            change_out <= 1'b0;
            drinks_out <= 2'b0;
        end
    end
end
endmodule