题解 | #自动贩售机2#

自动贩售机2

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

`timescale 1ns/1ns

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]	cnt ;
always @(posedge clk or negedge rst) begin
	if(!rst) begin
		cnt <= 3'b0 ;
	end
	else if(d1) begin
		cnt <= cnt +3'b1 ;
	end
	else if(d2) begin
		cnt <= cnt + 3'd2 ;
	end
	else if(cnt >= 3'd3 && !sel) begin
		cnt <= 3'd0 ;
	end
	else if(cnt >= 3'd5 && sel) begin
		cnt <= 3'd0 ;
	end
end
always @(posedge clk or negedge rst) begin
	if(!rst) begin
		out1<= 1'b0 ;
	end
	else if(cnt >= 3'd3 && !sel) begin
		out1 <= 1'd1 ;
	end
	else begin
		out1<= 1'b0 ;
	end 
end
always @(posedge clk or negedge rst) begin
	if(!rst) begin
		out2<= 1'b0 ;
	end
	else if(cnt >= 3'd5 && sel) begin
		out2 <= 1'd1 ;
	end
	else begin
		out2<= 1'b0 ;
	end 
end
always @(posedge clk or negedge rst) begin
	if(!rst) begin
		out3<= 1'b0 ;
	end
	else if(cnt == 3'd4 && !sel) begin
		out3 <= 1'd1 ;
	end
	else if(cnt == 3'd6 && sel) begin
		out3 <= 1'd1 ;
	end
	else begin
		out3<= 1'b0 ;
	end 
end
//*************code***********//
endmodule

全部评论

相关推荐

程序员小白条:你不是有一段实习了吗,现在找中大厂实习?过段时间要秋招了
我的简历长这样
点赞 评论 收藏
分享
07-03 11:02
中山大学 C++
字节刚oc,但距离九月秋招很近了有两段互联网实习,非腾讯字节。不敢赌转正,现在在纠结去还是不去如果实习俩月离职会有什么后果吗
阿城我会做到的:不去后悔一辈子,能否转正取决于ld的态度,只要他不卡,答辩就是走流程,个人觉得可以冲一把
投递字节跳动等公司9个岗位
点赞 评论 收藏
分享
06-25 09:33
厦门大学 Java
程序员饺子:现在日常估计没啥hc了,等到八月多估计就慢慢有了。双九✌🏻不用焦虑的
投递快手等公司9个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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