题解 | #自动贩售机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_st,nxt_st;
	parameter zero = 3'd0,
			  half = 3'd1,
			  one = 3'd2,
			  onehalf = 3'd3,
			  two = 3'd4,
			  twohalf = 3'd5,
			  three = 3'd6;

	always @(posedge clk or negedge rst)begin
		if(rst == 1'b0)
			cur_st <= 3'd0;
		else
			cur_st <= nxt_st;
	end

	always @(*)(1444584) begin
		case(cur_st)
			zero:begin
					if(d1)
						nxt_st = half;
					else if(d2)
						nxt_st = one;
					else if(d3)
						nxt_st = two;
					else
						nxt_st = nxt_st;
			end
			half:begin
					if(d1)
						nxt_st = one;
					else if(d2)
						nxt_st = onehalf;
					else if(d3)
						nxt_st = twohalf;
					else
						nxt_st = nxt_st;
			end
			one:begin
					if(d1)
						nxt_st = onehalf;
					else if(d2)
						nxt_st = two;
					else if(d3)
						nxt_st = three;
					else
						nxt_st = nxt_st;
			end
			onehalf: nxt_st = zero;
			two: nxt_st = zero;
			twohalf: nxt_st = zero;
			three: nxt_st = zero;
			default: nxt_st = zero;
		endcase
	end

	/*always @(*)(1444584) begin
		if((cur_st == onehalf) || (cur_st == two) || (cur_st = twohalf) || (cur_st == three))
			out1 = 1'b1;
		else
			out1 = 1'b0;

	end*/

	always @(*)(1444584) begin
		case(cur_st)
			onehalf: out1 = 1'b1;
			two: out1 = 1'b1;
			twohalf: out1 = 1'b1;
			three: out1 = 1'b1;
			default: out1 = 1'b0;
		endcase
	end

	always @(*)(1444584) begin
		if(cur_st == onehalf)
			out2 = 2'd0;
		else if(cur_st == two)
			out2 = 2'd1;
		else if(cur_st == twohalf)
			out2 = 2'd2;
		else if(cur_st == three)
			out2 = 2'd3;
		else
			out2 = 2'd0;
	end

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

突破口:题目给定输出out2是2bit,找零最多找三次,因此最大币值是3元。

难点:输入信号半个周期有效,因此当不满足输入条件时,状态机的nxt_st需要保持,这一点与以往写的不一样。

全部评论

相关推荐

大佬们,在大厂实习的都是几百一天???
爱睡觉的冰箱哥:实习工资这个不都是公开的吗,a不了,字节400,快手350,有些有房补餐补这样
点赞 评论 收藏
分享
翱翔龙骑:耗材的幻想
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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