题解 | #自动售卖机#
自动售卖机
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
);
wire [1:0] total;
wire flag;
reg [1:0] tmp;
always @ (posedge clk or negedge rst_n) begin
if (~rst_n) begin
drinks_out <= 2'd0;
change_out <= 1'd0;
end
else begin
case({sel, total})
3'b001: begin drinks_out <= 2'b01; change_out <= 1'b0; end
3'b010: begin drinks_out <= 2'b01; change_out <= 1'b1; end
3'b110: begin drinks_out <= 2'b10; change_out <= 1'b0; end
3'b111: begin drinks_out <= 2'b10; change_out <= 1'b1; end
default: begin drinks_out <= 2'd0; change_out <= 1'd0; end
endcase
end
end
always @ (posedge clk or negedge rst_n) begin
if (~rst_n)
tmp <= 2'd0;
else if (flag)
tmp <= 2'd0; //售卖机输出饮料,寄存投币清零。
else if (|din)
tmp <= din; //投币时才能寄存。
end
assign total = din + tmp;
assign flag = (({sel, total} == 3'b001) || ({sel, total} == 3'b010) || ({sel, total} == 3'b110) || ({sel, total} == 3'b111));
endmodule
CVTE公司福利 677人发布
查看12道真题和解析
