2015-04-02 1289 views
0

因此,我有一個reg[7:0] corr_Output[0:63];,它在模塊中填充了值。如何在一個CLK週期中找到此陣列中的最大數量? 我寫了一個8位比較:如何在verilog數組中找到最大數量

module Comparator2D(
input [7:0] X1, 
input [7:0] indexX1, 
input [7:0] X2, 
input [7:0] indexX2, 
output [7:0] Y, 
output [7:0] indexY 
); 

always begin 
    if (X1 > X2) begin 
     Y = X1; 
     indexY = indexX1; 
    end 
    else begin 
     Y = X2; 
     indexY = indexX2; 
    end 
end 
endmodule 

但我不知道我應該如何在我的頂層設計實例化這個模塊?我想我應該使用「for循環」,或者甚至寫另一個模塊,它將以金字塔形式連接我的Comparator2D模塊,但是當我發現我不能將整個陣列傳遞到模塊的輸入端口時,所以我有點卡住了..

+0

[查找使用Verilog for Priority Queue實現的最小數字數組](http://stackoverflow.com/questions/5785351/find-minimum-在陣列-的號碼-使用-Verilog的換優先級的隊列的實現) – Qiu 2015-04-02 12:56:31

回答

0

你可以通過使用for/generate來完成,就像在這個代碼示例中一樣,我可以一次比較8個字節。

關鍵是我不能傳遞一個內存作爲輸入(一個寄存器數組),但我可以傳遞一個數組來保存內存中的當前值。

// This is just your compare module. 
module C2D (
    input wire [7:0] X1, 
    input wire [7:0] indexX1, 
    input wire [7:0] X2, 
    input wire [7:0] indexX2, 
    output reg [7:0] Y, 
    output reg [7:0] indexY 
    ); 

    always @* begin 
     if (X1 > X2) begin 
      Y = X1; 
      indexY = indexX1; 
     end 
     else begin 
      Y = X2; 
      indexY = indexX2; 
     end 
    end 
endmodule 

// Compare 8 bytes at a time 
module greatest8bytes (
    input wire [63:0] array, // 8 byte array 
    output wire [7:0] indexG, 
    output wire [7:0] valueG 
    ); 

    wire [7:0] value_l1[0:3]; 
    wire [7:0] index_l1[0:3]; 

    genvar i; 
    generate 
    for (i=0;i<8;i=i+2) begin :gen_comps_l1 
     C2D cl1 (array[i*8+7:i*8], 
       i, 
       array[(i+1)*8+7:(i+1)*8], 
       (i+1), 
       value_l1[i/2], 
       index_l1[i/2] 
       ); 
    end 
    endgenerate 

    wire [7:0] value_l2[0:1]; 
    wire [7:0] index_l2[0:1]; 

    generate 
    for (i=0;i<4;i=i+2) begin :gen_comps_l2 
     C2D cl2 (value_l1[i], 
       index_l1[i], 
       value_l1[i+1], 
       index_l1[i+1], 
       value_l2[i/2], 
       index_l2[i/2] 
       ); 
    end 
    endgenerate 

    wire [7:0] value_l3[0:0]; 
    wire [7:0] index_l3[0:0]; 

    generate 
    for (i=0;i<2;i=i+2) begin :gen_comps_l3 
     C2D cl3 (value_l2[i], 
       index_l2[i], 
       value_l2[i+1], 
       index_l2[i+1], 
       value_l3[i/2], 
       index_l3[i/2] 
       ); 
    end 
    endgenerate 

    assign indexG = index_l3[0]; 
    assign valueG = value_l3[0]; 
endmodule 

greatest8bytes模塊被合成你期望的方式:作爲比較器的一個棱錐狀排列:

Circuit synthesized for greatest8bytes 要連接的REG的陣列(存儲器)到這個模塊的輸入,創建(在本實施例64)位的期望數量的有線和串聯的存儲器中的所有元素,如本示例中模塊中:

module findgreatest (
    input wire clk, 
    input wire [2:0] addr, 
    input wire [7:0] data, 
    input wire we, 

    output wire [2:0] indexG, 
    output wire [7:0] valueG 
    ); 

    reg [7:0] memory[0:7]; // 8 bytes 
    // To load data from the outside so the synthesizer won't throw away memory 
    always @(posedge clk) begin 
     if (we) 
      memory[addr] <= data; 
    end 

    wire [63:0] array = {memory[7],memory[6],memory[5],memory[4], 
         memory[3],memory[2],memory[1],memory[0]}; 
    greatest8bytes compar (array, indexG, valueG); 
endmodule 
0

不知道這是合成的,但它是很好的瞭解, SystemVerilog的已建在MIN和MAX函數:

module maximum(); 
reg[7:0] corr_Output[0:63] = '{0:8'd112, 2:8'd250, 3:8'd37, 4:8'd15, default:8'd25}; 
reg[7:0] max_i[$]; 
reg[7:0] min_i[$]; 

initial begin 
    max_i = corr_Output.max; 
    min_i = corr_Output.min; 
    $display ("max=%d, min=%d", max_i[0], min_i[0]); 
end 
endmodule 

輸出:

# max=250, min= 15 

或者,它可能是更短的只使用這個經典和綜合的for循環比較:

always_comb begin 
    max = corr_Output[0]; 
    for (c = 0; c <= 63; c++) 
    begin 
    if (corr_Output[c] > max) 
    begin 
     max = array[c]; 
     index = c; 
    end 
    end 
相關問題