题解 | #自动贩售机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***********//
parameter S0 = 3'd0;
parameter S0_5 = 3'd1;
parameter S1 = 3'd2;
parameter S1_5 = 3'd3;
parameter S2 = 3'd4;
parameter S2_5 = 3'd5;
parameter S3 = 3'd6;

reg [2:0] c_state;
reg [2:0] n_state;
always @(posedge clk or negedge rst) begin
	if(!rst) begin
		c_state <= S0;
	end
	else begin
		c_state <= n_state;
	end
end

always @(*) begin
	if(!rst) begin
		n_state <= S0;
	end
	else begin
		case(c_state)
			S0:
				case({d1,d2,d3})
                    3'b100: n_state <= S0_5;
                    3'b010: n_state <= S1;
                    3'b001: n_state <= S2;
                    default: n_state <= n_state;
				endcase
				/*n_state <= 	({d1,d2,d3} == 3'b100) ? S0_5:
							({d1,d2,d3} == 3'b010) ? S1:
							({d1,d2,d3} == 3'b001) ? S2:S0;*/
			S0_5:
			    case({d1,d2,d3})
                    3'b100: n_state <= S1;
                    3'b010: n_state <= S1_5;
                    3'b001: n_state <= S2_5;
                    default: n_state <= n_state;
				endcase
				/*n_state <= 	({d1,d2,d3} == 3'b100) ? S1:
							({d1,d2,d3} == 3'b010) ? S1_5:
							({d1,d2,d3} == 3'b001) ? S2_5:S0_5;*/
			S1:
				case({d1,d2,d3})
                    3'b100: n_state <= S1_5;
                    3'b010: n_state <= S2;
                    3'b001: n_state <= S3;
                    default: n_state <= n_state;
				endcase
				/*n_state <= 	({d1,d2,d3} == 3'b100) ? S1_5:
							({d1,d2,d3} == 3'b010) ? S2:
							({d1,d2,d3} == 3'b001) ? S3:S1;*/
			default: n_state <= S0;
		endcase
	end
end

always @(*) begin
	if(!rst) begin
		out1 <= 0;
	end
	else begin
		out1 <= ((c_state == S1_5 | c_state == S2 | c_state == S2_5 | c_state == S3));
	end
end

always @(*) begin
	if(!rst) begin
		out2 <= 0;
	end
	else begin
		out2 <= (c_state == S1_5)? 0:
				(c_state == S2)? 1:
				(c_state == S2_5)? 2:
				(c_state == S3)? 3:0;
	end
end

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

全部评论

相关推荐

缓解焦虑的最好方法是回家。鼠鼠昨天上午考完了本科阶段的最后一场考试,大概率考得稀烂,但是没多想,考完立马收拾行李,坐上了提前约好的顺风车飞奔回家。虽然家和学校很近,只有一百多公里的路程,但距离上次回家也已经有三四个月了。每次想回家,期间总有考试、毕业设计、面试、实习等等各种各样的原因,没办法回去,待在学校和公司的每一天也都充斥着无形的压力和焦虑。现在终于完成了答辩,考完了试,公司那边也请了假,是时候回去一趟了。没有提前通知爸妈,想给他们一个惊喜。下午提前到了家,他俩还在上班,只好让外公外婆来给我开门。因为我的回家,晚上外婆在厨房格外忙碌,做了满满一大桌子菜,填饱了我天天吃外卖的肚子。晚上也没空...
梦想是成为七海千秋:取决于家庭吧?其实回家更焦虑了,每天起床父母都问实习找好了没简历投递了没今天有没有面试,但是又没有什么结果,玩两下手机父母就会说你看你啥也没找到为什么天天就知道刷手机,怎么不去学习…我现在就希望我能永远在外面实习,报喜不报忧,等拿到一个好offer再回家
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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