题解 | #非整数倍数据位宽转换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 [7:0] data_reg;
    always@(posedge clk or negedge rst_n) begin
        if(!rst_n) begin
            data_reg <= 'd0;
        end
        else begin
            data_reg <= valid_in?data_in:data_reg;
        end
    end
    reg [1:0] cnt;
    always@(posedge clk or negedge rst_n) begin
        if(!rst_n) begin
            cnt <= 'd0;
        end
        else begin
            cnt <= valid_in?((cnt=='d2)?'d0:(cnt+'d1)):cnt;
        end
    end
    always@(posedge clk or negedge rst_n) begin
        if(!rst_n) begin
            data_out <= 'd0;
            valid_out <= 1'b0;
        end
        else begin
            case(cnt)
                'd0: begin
                    data_out <= data_out;
                    valid_out <= 1'b0;
                end
                'd1: begin
                    data_out <= valid_in?{data_reg,data_in[7:4]}:data_out;
                    valid_out <= valid_in?1'b1:1'b0;
                end
                'd2: begin
                    data_out <= valid_in?{data_reg[3:0],data_in}:data_out;
                    valid_out <= valid_in?1'b1:1'b0;
                end
                default: begin
                    data_out <= 'd0;
                    valid_out <= 1'b0;
                end
            endcase
        end
    end
endmodule

全部评论

相关推荐

评论
点赞
收藏
分享
牛客网
牛客企业服务