题解 | #非整数倍数据位宽转换8to12#

非整数倍数据位宽转换8to12

https://www.nowcoder.com/practice/11dfedff55fd4c24b7f696bed86190b1

`timescale 1ns/1ns

module width_8to12(
	input 				   clk 		,   
	input 			      rst_n		,
	input				      valid_in	,
	input	[7:0]			   data_in	,
 
 	output  reg			   valid_out,
	output  reg [11:0]   data_out
);
reg		[11:0]		data_pre;
reg		[2:0]		cnt;
always @(posedge clk or negedge rst_n) begin
	if(!rst_n) begin
		cnt <= 3'b0 ;
	end
	else if(valid_in) begin
	 	if(cnt == 3'd2 ) begin
		cnt <= 3'b0 ;
		end
		else  begin
		cnt <= cnt + 3'b1;
		end
	end
	else begin
		cnt <= cnt ;
	end

end
always @(posedge clk or negedge rst_n) begin
	if(!rst_n) begin
		data_pre <= 12'b0 ;
	end
	else if(valid_in) begin
		data_pre <= data_in;
	end
	else begin
		data_pre <= data_pre ;
	end
end
always @(posedge clk or negedge rst_n) begin
	if(!rst_n) begin
		data_out <= 12'b0 ;
	end
	else if(valid_in && cnt == 3'd1) begin
		data_out <= {data_pre[7:0],data_in[7:4]};
	end
	else if(valid_in && cnt == 3'd2) begin
		data_out <= {data_pre[3:0],data_in};
	end
	else begin
		data_out <= data_out ;
	end
end
always @(posedge clk or negedge rst_n) begin
	if(!rst_n) begin
		valid_out <= 1'b0 ;
	end
	else if(valid_in && cnt == 3'd1) begin
		valid_out <= 1'b1;
	end
	else if(valid_in && cnt == 3'd2) begin
		valid_out <= 1'b1;
	end
	else begin
		valid_out <= 1'b0;
	end
end
endmodule

全部评论

相关推荐

牛客969571862号:昨天捞我今天面这个,岗位一模一样,感觉就是面着玩
点赞 评论 收藏
分享
10-14 10:56
已编辑
长沙学院 嵌入式软件开发
痴心的00后拿到了ssp:hr面挂了,无所谓了反正不去😃
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务