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

	// 状态是已经投入的钱,激励信号显示一次只会投0.5/1/1.5中的一个

	parameter [2:0] EMPTY = 0;
	parameter [2:0] M0_5  = 1;
	parameter [2:0] M1_0  = 2;
	parameter [2:0] M1_5  = 3;
	parameter [2:0] M2_0  = 4;
	parameter [2:0] M2_5  = 5;
	parameter [2:0] M3_0  = 6;

	reg [2:0] curr_state, next_state;

	// state transfer
	always @(negedge clk, negedge rst) begin
		if(!rst) begin
			curr_state <= EMPTY;
		end
		else begin
			curr_state <= next_state;
		end
	end

	// state switch
	always @(*) begin
		case(curr_state)
			EMPTY: next_state = (d1 & ~d2 & ~d3)? M0_5 :
								(~d1 & d2 & ~d3)? M1_0 :
								(~d1 & ~d2 & d3)? M2_0 : EMPTY;

			M0_5 : next_state = (d1 & ~d2 & ~d3)? M1_0 :
								(~d1 & d2 & ~d3)? M1_5 :
								(~d1 & ~d2 & d3)? M2_5 : 
								(~d1 & ~d2 & ~d3)?M0_5 : EMPTY;

			M1_0 : next_state = (d1 & ~d2 & ~d3)? M1_5 :
								(~d1 & d2 & ~d3)? M2_0 :
								(~d1 & ~d2 & d3)? M3_0 : 
								(~d1 & ~d2 & ~d3)?M1_0 : EMPTY;

			M1_5 : next_state = (d1 & ~d2 & ~d3)? M0_5 :
								(~d1 & d2 & ~d3)? M1_0 :
								(~d1 & ~d2 & d3)? M2_0 : EMPTY;

			M2_0 : next_state = (d1 & ~d2 & ~d3)? M0_5 :
								(~d1 & d2 & ~d3)? M1_0 :
								(~d1 & ~d2 & d3)? M2_0 : EMPTY;

			M2_5 : next_state = (d1 & ~d2 & ~d3)? M0_5 :
								(~d1 & d2 & ~d3)? M1_0 :
								(~d1 & ~d2 & d3)? M2_0 : EMPTY;

			M3_0 : next_state = (d1 & ~d2 & ~d3)? M0_5 :
								(~d1 & d2 & ~d3)? M1_0 :
								(~d1 & ~d2 & d3)? M2_0 : EMPTY;		

		endcase
	end

	// output logic
	always @(posedge clk, negedge rst) begin
		if(!rst) begin
			out1 <= 0;
			out2 <= 0;
		end
		else begin
			if(curr_state == M1_5) begin
				out1 <= 1;
				out2 <= 0;
			end
			else if(curr_state == M2_0) begin
				out1 <= 1;
				out2 <= 2'b01;
			end
			else if(curr_state == M2_5) begin
				out1 <= 1;
				out2 <= 2'b10;
			end
			else if(curr_state == M3_0) begin
				out1 <= 1;
				out2 <= 2'b11;
			end
			else begin
				out1 <= 0;
				out2 <= 0;
			end
		end
	end

//*************code***********//
endmodule

全部评论

相关推荐

丿南烟丶:黑白模板吧,不要这样花哨的。 主要成就太空了,和获奖融在一起,写一两行就行了。 职业技能不要这样排,就传统的掌握精通什么什么然后举例补充的一些重要技术点。 自我介绍说实话也没啥用,可以删了。 把自己的两个项目方案细节补充上去,为什么这样设计,怎么设计,成果是什么按star法则来写 你要引导面试官来问你的技能和项目,你的获奖和自我介绍别人可能看都不看一眼或者不太在乎,重要的是展示你能干活的能力
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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