题解 | #自动贩售机2#

自动贩售机2

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

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***********//
reg [2:0] count;

always @(posedge clk or negedge rst)
begin
if(!rst)
begin
count<=0;
end
else
begin
if(sel)
begin
count<= count>3'd4 ? 3'd0 :(d1 ? count+3'd1 :(d2 ? count+3'd2 : count));
end
else if(!sel)
begin
count<= count>3'd2 ? 3'd0 :(d1 ? count+3'd1 :(d2 ? count+3'd2 : count));
end
end
end
always @(posedge clk or negedge rst)
begin
if(!rst)
begin
out1<=0;
out2<=0;
out3<=0;
end
else
begin
if(sel)
begin
out1<=0;
out2<=count>3'd4 ? 1 :0;
out3<=count>3'd4 ? count-3'd5:0;
end
else if (!sel)
begin
out1<=count>3'd2 ? 1 :0;
out2<=0;
out3<=count>3'd2 ? count-3'd3:0;
end
end
end
//*************code***********//
endmodule

`timescale 1ns/1ns
module textbench();
reg clk ,rst,d1,d2,sel;
wire  out1,out2,out3;
 seller2 u0(
	.clk(clk)  ,
    .rst(rst)  ,
	.d1(d1) ,
	.d2(d2),
	.sel(sel),
	.out1(out1),
	.out2(out2),
	.out3(out3)
);
always #5 clk=~clk;
   initial
begin
clk=1;rst=0;d1=0;d2=0;sel=0;
# 5 rst=1;
# 5 d1=1;
# 5 d1=0;
# 15 d2=1;
# 5 d2=0; 
#25 sel=1;
#10 d2=1;
#5 d2=0;
#15 d2=1; 
#5  d2=0;
#15 d2=1; 
#5  d2=0;
#20
$finish;
end

endmodule

仿真结果图如下:

全部评论

相关推荐

不要停下啊:大二打开牛客,你有机会开卷了,卷起来,去找课程学习,在牛客上看看大家面试笔试都需要会什么,岗位有什么需求就去学什么,努力的人就一定会有收获,这句话从来都经得起考验,像我现在大三了啥也不会,被迫强行考研,炼狱难度开局,啥也不会,找工作没希望了,考研有丝丝机会
点赞 评论 收藏
分享
05-26 10:24
门头沟学院 Java
qq乃乃好喝到咩噗茶:其实是对的,线上面试容易被人当野怪刷了
找工作时遇到的神仙HR
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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