题解 | #自动贩售机2#

自动贩售机2

https://www.nowcoder.com/practice/298dec1c3dce45c881f3e53e02558828

`timescale 1ns/1ns

module seller2(
	input wire clk  ,
	input wire rst  ,
	input wire d1 ,
	input wire d2 ,
	input wire sel ,
	
	output reg out1,
	output reg out2,
	output reg out3
);
//*************code***********//

	parameter IDEL=0, S0=1,S1=2,S2=3,S3=4,S4=5,S5=6;

	reg [2:0] cstate, nstate;

	always@(posedge clk or negedge rst) begin
		if(!rst) begin
			cstate <= IDEL;
		end
		else begin
			cstate <= nstate;
		end
	end

	always@(*) begin
		case(cstate)
			IDEL:   nstate = d1?S0:(d2?S1:nstate);
			S0:		nstate = d1?S1:(d2?S2:nstate);
			S1:		nstate = d1?S2:(d2?S3:nstate);
			S2: begin
				if(sel) begin
					nstate = d1?S3:(d2?S4:nstate);
				end
				else begin
					nstate = d1?S0:(d2?S1:IDEL);
				end
			end
			S3: begin
				if(sel) begin
					nstate = d1?S4:(d2?S5:nstate);
				end
				else begin
					nstate = d1?S0:(d2?S1:IDEL);
				end
			end
			S4,		
			S5:		nstate = d1?S0:(d2?S1:IDEL);
			default:nstate = IDEL;
		endcase
	end

	always@(posedge clk or negedge rst) begin
		if(!rst) begin
			out1 <= 1'b0;
			out2 <= 1'b0;
			out3 <= 1'b0;
		end
		else begin
			out1 <= ((nstate==S2) | (nstate==S3)) & (~sel);
			out2 <= ((nstate==S4) | (nstate==S5)) & sel;
			out3 <= (((nstate==S3) & (~sel)) | ((nstate==S5) & sel))?1'b1:1'b0;
		end
	end

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

全部评论

相关推荐

06-17 00:26
门头沟学院 Java
程序员小白条:建议换下项目,智能 AI 旅游推荐平台:https://github.com/luoye6/vue3_tourism_frontend 智能 AI 校园二手交易平台:https://github.com/luoye6/vue3_trade_frontend GPT 智能图书馆:https://github.com/luoye6/Vue_BookManageSystem 选项目要选自己能掌握的,然后最好能自己拓展的,分布式这种尽量别去写,不然你只能背八股文了,另外实习的话要多投,尤其是学历不利的情况下,多找几段实习,最好公司title大一点的
无实习如何秋招上岸
点赞 评论 收藏
分享
程序员小白条:找的太晚,别人都是大三实习,然后大四秋招春招的,你大四下了才去实习,晚1年
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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