题解 | #自动贩售机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
);
//*************状态机***********//
parameter   idle      =  7'b000_0001,
			half      =  7'b000_0010,
			one       =  7'b000_0100,
			one_half  =  7'b000_1000,
			two       =  7'b001_0000,
			two_half  =  7'b010_0000,
			three     =  7'b100_0000;


wire [2:0] pi_money={d3,d2,d1};
reg [6:0] cur_state, next_state;



 //第一段:时序逻辑--状态跳转
always@(posedge clk or negedge rst)begin
	if(!rst)
		cur_state  <= idle;
	else
		cur_state  <= next_state ;

end

//第二段:组合逻辑---状态转移(与输入的关系)
always@(*)begin
		case(cur_state) 

			idle: 
				case(pi_money)
						
				 	3'b001: next_state  = half ;
				  		
					3'b010: next_state  = one ;
						
					3'b100: next_state  = two ;

					default:next_state  = next_state ;
				endcase

			half: 
				case(pi_money)
						
				 	3'b001: next_state  = one ;
				  		
					3'b010: next_state  = one_half ;

					3'b100: next_state  = two_half ;
						
					default: next_state  = next_state ;
				endcase

			one : 
				case(pi_money)		
				 	3'b001: next_state  = one_half ;
				  		
					3'b010: next_state  = two ;

					3'b100: next_state  = three ;

					default: next_state  = next_state ;
				endcase

			one_half:next_state  = idle ;
			

			two:	next_state  = idle ;
			

			two_half:next_state  = idle ;
				

			three: next_state  = idle ;
					
			default: next_state  = idle ;
		endcase
	end


// 第三段:时序逻辑--输出(out1)

always@(posedge clk or negedge rst)begin
	if(!rst)
		out1  <= 1'b0;
	else if ((next_state==one_half)||(next_state==two)||(next_state==two_half)||(next_state==three))
		out1  <= 1'b1 ;
	else
		out1  <= 1'b0;

end

//第三段:时序逻辑--输出(out2)
always@(posedge clk or negedge rst)begin
	if(!rst)
		out2  <= 2'd0;
	else if(next_state==two)
		out2  <= 2'd1 ;
	else if(next_state==two_half)
		out2  <= 2'd2;
	else if(next_state==three)
		out2  <= 2'd3;
	else
		out2  <= 2'd0;
end
endmodule

全部评论

相关推荐

白火同学:先说结论,对于一份实习简历来说,整体还是挺不错的,技术深度和广度都到位,找到一份中小厂的实习没啥问题。 再说说能优化的点吧。 1、量化结果,项目中很多工作量化一下结果给面试官的感受会更直观一些,也能体现你对应用该项技术的理解(在众多技术为什么要用它,运行性能或者说开发效率往往是一大考虑指标;而不是说大家做这种功能都用它,所以我用它)。 2、突出亮点,项目中可以从“工作职责”择一些“个人亮点”另写一块,优先去写开发过程中遇到的xx问题,使用xx技术达到xx效果,针对性去写一些疑杂难的功能,能带出你个人思考和解决的过程。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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