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

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

http://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[1:0]cnt;
reg[11:0]data_cache;

always @(posedge clk or negedge rst_n)begin
    if(~rst_n)
        cnt <= 0;
    else if(valid_in && cnt == 2)
        cnt <= 0;
    else if(valid_in)
        cnt <= cnt + 1'b1;
end

always @(posedge clk or negedge rst_n)begin
    if(~rst_n)
        data_cache <= 0;
    else if(valid_in)
        data_cache <= {data_cache[3:0],data_in};
end

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

endmodule

全部评论

相关推荐

totoroyyw:千年老妖😂
投递华为等公司10个岗位
点赞 评论 收藏
分享
11-08 17:36
诺瓦科技_HR
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务