题解 | #自动贩售机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***********//
parameter idle=0,s0_half=1,s0_one=2,s0_one_half=3,s0_two=4;
parameter s1_half=5,s1_one=6,s1_one_half=7,s1_two=8,s1_two_half=9,s1_three=10;
reg [3:0]cs,ns;
always@(posedge clk or negedge rst)begin
	if(!rst)begin
		cs<=idle;
	end
	else begin
		cs<=ns;
	end
end
always@(*)begin
	if(!rst) ns=idle;
	else begin
		case(cs)
		idle:ns=sel?(d1?s1_half:d2?s1_one:ns):(d1?s0_half:d2?s0_one:ns);//和上题一样需要锁存ns值,因为激励d1,d2在上升沿和下降沿值都变了
		s0_half:ns=d1?s0_one:d2?s0_one_half:ns;
		s0_one: ns=d1?s0_one_half:d2?s0_two:ns;
		s0_one_half:ns=idle;
		s0_two:ns=idle;
		s1_half:ns=d1?s1_one:d2?s1_one_half:ns;
		s1_one:ns=d1?s1_one_half:d2?s1_two:ns;
		s1_one_half:ns=d1?s1_two:d2?s1_two_half:ns;
		s1_two:ns=d1?s1_two_half:d2?s1_three:ns;
		s1_two_half:ns=idle;
		s1_three:ns=idle;
		default:ns=idle;
		endcase
	end
end
always@(posedge clk or negedge rst)begin
	if(!rst)begin
		out1<=0;
		out2<=0;
		out3<=0;
	end
	else begin
		if(ns==s0_one_half)begin
			out1<=1;
			out2<=0;
			out3<=0;
		end
		else if(ns==s0_two)begin
			out1<=1;
			out2<=0;
			out3<=1;
		end
		else if(ns==s1_two_half)begin
			out1<=0;
			out2<=1;
			out3<=0;
		end
		else if(ns==s1_three)begin
			out1<=0;
			out2<=1;
			out3<=1;
		end
		else begin
			out1<=0;
			out2<=0;
			out3<=0;
		end
	end
end
//*************code***********//
endmodule

全部评论

相关推荐

06-13 12:13
已编辑
东北大学 射频工程师
26毕业的,日常实习还能找到吗
求实习的青提很想去大厂:目前应该还有hc吧,腾讯感觉还有hc,最近捞了我好几次,因为目前有offer,所以不准备面了,可以再找找,不行的话就找找中小厂试试,因为我之前也找了好久,准备放弃了,结果有个岗位流程特别顺利,然后就oc,只能说坚持下试试,万一呢💪
点赞 评论 收藏
分享
05-19 19:57
蚌埠学院 Python
2237:Gpa70不算高,建议只写排名,个人技能不在多而在精,缩到8条以内。项目留一个含金量高的,减少间距弄到一页,硕士简历也就一页,本科不要写很多
点赞 评论 收藏
分享
Rena1ssance_:对的,要是面评没太烂,勤更新简历等捞就行了,腾讯可以无限复活
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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