题解 | #自动贩售机1#

自动贩售机1

https://www.nowcoder.com/practice/dcf59e6c51f6489093495acb1bc34dd8

真的要吐血...严格来讲上升沿上升,下降沿下降的,半周期脉冲,用上升沿去踩就很不对劲。结果这里就是要用上升沿去踩...用下降沿去踩反而还不对...

`timescale 1ns/1ns
module seller1(
	input wire clk  ,
	input wire rst  ,
	input wire d1 ,
	input wire d2 ,
	input wire d3 ,
	
	output reg out1,
	output reg [1:0]out2
);
//*************code***********//

reg [2:0] cur,nxt;
reg d1_tmp,d2_tmp,d3_tmp;

parameter S0=0,S1=1,S2=2,S3=3,S4=4;

always@(posedge clk or negedge rst)
begin 
	if(~rst)
	begin 
		d1_tmp <= 1'b0;
		d2_tmp <= 1'b0;
		d3_tmp <= 1'b0;
	end
	else 
	begin 
		d1_tmp <= d1;
		d2_tmp <= d2;
		d3_tmp <= d3;	
	end
end


always@(posedge clk or negedge rst)
begin 
	if(~rst)
	begin 
		cur <= S0;
	end
	else
	begin 
		cur <= nxt;
	end
end

always@(*)
begin 

		case(cur)
		S0: nxt = d1_tmp ? S1 : (d2_tmp ? S2 : (d3_tmp ? S4 : S0));
		S1: nxt = d1_tmp ? S2 : (d2_tmp ? S4 : (d3_tmp ? S4 : S1));
		S2: nxt = d1_tmp ? S4 : (d2_tmp ? S4 : (d3_tmp ? S4 : S2));
		S4: nxt = S0;
		default : nxt = S0;
		endcase
end

always@(posedge clk or negedge rst)
begin 
	if(~rst)
	begin 
		out1 <= 1'b0;
	end
	else
	begin 
		out1 <= (nxt == S4);
	end
end

always@(posedge clk or negedge rst)
begin 
	if((nxt == S4) && rst)
	begin 
		case(cur)
		S0: out2 <= 3'd1;
		S1: out2 <= d2_tmp ? 3'd0 : 3'd2;
		S2: 
		begin 
			if(d1_tmp) out2 <= 3'd0;
			else if(d2_tmp) out2 <= 3'd1;
			else out2 <= 3'd3;
		end	
		endcase
	end
	else
	begin 
		out2 <= 3'b000;
	end
	
end
endmodule

全部评论
别那么认真
点赞 回复 分享
发布于 06-20 19:44 陕西

相关推荐

Java抽象带篮子:难蚌,点进图片上面就是我的大头😆
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务