其他分享
首页 > 其他分享> > 【FPGA学习笔记】VL32 非整数倍数据位宽转换24to128

【FPGA学习笔记】VL32 非整数倍数据位宽转换24to128

作者:互联网

描述

实现数据位宽转换电路,实现24bit数据输入转换为128bit数据输出。其中,先到的数据应置于输出的高bit位。

电路的接口如下图所示。valid_in用来指示数据输入data_in的有效性,valid_out用来指示数据输出data_out的有效性;clk是时钟信号;rst_n是异步复位信号。

 

 

 

 

输入描述:

    input                 clk         ,   
    input                 rst_n        ,
    input                valid_in    ,
    input    [23:0]        data_in        

输出描述:

    output    reg            valid_out    ,
    output  reg [127:0]    data_out   输入:valid_in, data_in[23:0]
输出:valid_out,data_out[127:0]
输入数据是24bit,输出数据是128bit。因为<span class="katex"><span class="katex-mathml">128&times;3=24&times;16128\times3=24\times16<span class="katex-html"><span class="base"><span class="strut"><span class="mord">1<span class="mord">2<span class="mord">8<span class="mspace"><span class="mbin">&times;<span class="mspace"><span class="base"><span class="strut"><span class="mord">3<span class="mspace"><span class="mrel">=<span class="mspace"><span class="base"><span class="strut"><span class="mord">2<span class="mord">4<span class="mspace"><span class="mbin">&times;<span class="mspace"><span class="base"><span class="strut"><span class="mord">1<span class="mord">6,所以每输入16个有效数据,就可以产生三个完整的输出。因此设置一个仅在输入数据有效时工作的计数器<code>cnt</code>,计数范围是0-15。
1 2 3 4 5 6 7 8 9 reg [3:0]   cnt; always@(posedge clk or negedge rst_n) begin     if(~rst_n)         cnt <= 0;     else         cnt <= ~valid_in? cnt:                cnt==15  0  :                cnt+1; end

然后设置一个数据暂存器data_lock,每当输入有效时,将数据从低位移入。

 
1 2 3 4 5 6 7 reg [127:0] data_lock; always@(posedge clk or negedge rst_n) begin     if(~rst_n)         data_lock <= 0;     else         data_lock <= valid_in? {data_lock[103:0], data_in}: data_lock; end  

 

 

由上图易得,每当计数器cnt计数到5、10、15时,data_out要进行更新,并拉高valid_out一个周期。

 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 always@(posedge clk or negedge rst_n) begin     if(~rst_n)         valid_out <= 0;     else         valid_out <= (cnt==5 || cnt==10 || cnt==15)&&valid_in; end    always@(posedge clk or negedge rst_n) begin     if(~rst_n)         data_out <= 0;     else if(cnt==5)         data_out <= valid_in? {data_lock[119:0], data_in[23:16]}: data_out;     else if(cnt==10)         data_out <= valid_in? {data_lock[111:0], data_in[238]}: data_out;     else if(cnt==15)         data_out <= valid_in? {data_lock[103:0], data_in[230]}: data_out;     else         data_out <= data_out; end

代码

 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 `timescale 1ns/1ns   module width_24to128(     input               clk         ,       input               rst_n       ,     input               valid_in    ,     input   [23:0]      data_in     ,        output  reg         valid_out   ,     output  reg [127:0] data_out );     reg [3:0]   cnt;     reg [127:0] data_lock;           always@(posedge clk or negedge rst_n) begin         if(~rst_n)             cnt <= 0;         else             cnt <= ~valid_in? cnt:cnt+1;     end           always@(posedge clk or negedge rst_n) begin         if(~rst_n)             valid_out <= 0;         else             valid_out <= (cnt==5 || cnt==10 || cnt==15)&&valid_in;     end           always@(posedge clk or negedge rst_n) begin         if(~rst_n)             data_lock <= 0;         else             data_lock <= valid_in? {data_lock[103:0], data_in}: data_lock;     end           always@(posedge clk or negedge rst_n) begin         if(~rst_n)             data_out <= 0;         else if(cnt==5)             data_out <= valid_in? {data_lock[119:0], data_in[23:16]}: data_out;         else if(cnt==10)             data_out <= valid_in? {data_lock[111:0], data_in[238]}: data_out;         else if(cnt==15)             data_out <= valid_in? {data_lock[103:0], data_in[230]}: data_out;         else             data_out <= data_out;     end endmodule

标签:cnt,FPGA,24to128,else,valid,数据位,rst,data,out
来源: https://www.cnblogs.com/mahaidong/p/16442639.html