题解 | #自动贩售机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

全部评论

相关推荐

01-19 12:48
门头沟学院 C++
只想搞钱的鸽子很喜欢...:混账是很多的,还有那些在自己风华正茂的年纪说风凉话讥讽那些下岗前员工的。这些人都是现在职场环境这么烂的帮凶
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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