简体   繁体   English

一个周期以verilog排序

[英]Sorting in verilog with one cycle

i was trying to sort 9 random numbers in verilog. 我试图在verilog中对9个随机数进行排序。 I use bubble sort(nested for loops) algorithm but i have a challenge. 我使用冒泡排序(嵌套循环)算法,但遇到了挑战。 I want to sort them in one clk cycle but it does not do what i want. 我想在一个clk周期中对它们进行排序,但是它并不能满足我的要求。 It needs at least 9 cycle to sort them. 对它们进行排序至少需要9个周期。

always @(posedge clk)
begin

if(m >= 68 && sort_valid == 0) begin
    pool_sort[0] <= pool_buffer[66];
    pool_sort[1] <= pool_buffer[65];
    pool_sort[2] <= pool_buffer[64];
    pool_sort[3] <= pool_buffer[34];
    pool_sort[4] <= pool_buffer[33];
    pool_sort[5] <= pool_buffer[32];
    pool_sort[6] <= pool_buffer[2];
    pool_sort[7] <= pool_buffer[1];
    pool_sort[8] <= pool_buffer[0];

sort_valid <= 1;

 end

if(sort_valid == 1) begin
        for(k=0;k<8;k=k+1) begin
            if(pool_sort[k] < pool_sort[k+1]) begin
                pool_sort[k] <= pool_sort[k+1];
                pool_sort[k+1] <= pool_sort[k];
            end
        end
    if(sort_counter == 0) begin
        sort_valid <= 0;
        pool_out <= pool_sort[0];
    end
end
end

always @(posedge clk)
begin

if(sort_valid == 1) begin
sort_counter <= sort_counter - 1;
end
if(sort_counter == 0) begin
    sort_counter <= 8;
end

end
endmodule

This is my sorting algorithm so far. 到目前为止,这是我的排序算法。

Think upfront if you will need to sort many rounds of 9 numbers. 如果需要对9个数字进行多轮排序,请提前考虑一下。

You can do sorting within 1 cycle, but the top frequency could be significantly limited. 您可以在1个周期内进行排序,但最高频率可能会受到很大限制。 But then, it doesn't really matter if you spent 1 clock cycle at low frequency, or 9 clocks at much higher frequency. 但是,如果您在低频下花费1个时钟周期,或者在更高的频率上花费9个时钟,则并没有什么关系。

If you will have many rounds, then you can pipeline sorting stages, getting new result on every clock cycle, with a few clock latency for a first result. 如果要进行很多回合,则可以进行流水线排序阶段,从而在每个时钟周期获得新结果,并为第一个结果提供一些时钟延迟。

See also https://en.wikipedia.org/wiki/Bitonic_sorter , as more suitable for parallel sorting in hw with small number of inputs. 另请参阅https://en.wikipedia.org/wiki/Bitonic_sorter ,因为它更适合于使用少量输入的硬件中的并行排序。

Here's a paramaterizable 1-clock cycle sorter for Verilog (inspired from here but I parameterized the module, reduced the number of cycles, and did some cleanup). 这是用于Verilog的可参数化的1时钟周期分类器(从此处得到启发,但我对模块进行了参数化,减少了周期数,并进行了一些清理)。

It'll do what you want in simulation but it'll probably be pretty big and ugly when you synthesize it. 它可以完成您在仿真中想要的操作,但是在合成时可能会变得非常笨拙。

module sort #(
    parameter NUM_VALS = 5,
    parameter SIZE     = 16
)(  input  wire clk,
    input  wire [NUM_VALS*SIZE-1:0] in,
    output reg  [NUM_VALS*SIZE-1:0] out
);
    reg [NUM_VALS*SIZE-1:0] sorted_bus;
    always @(posedge clk) begin
        out <= sorted_bus;
    end

    integer i, j;
    reg [SIZE-1:0] temp;
    reg [SIZE-1:0] array [1:NUM_VALS];
    always @* begin
        for (i = 0; i < NUM_VALS; i = i + 1) begin
            array[i+1] = in[i*SIZE +: SIZE];
        end

        for (i = NUM_VALS; i > 0; i = i - 1) begin
            for (j = 1 ; j < i; j = j + 1) begin
                if (array[j] < array[j + 1]) begin
                    temp         = array[j];
                    array[j]     = array[j + 1];
                    array[j + 1] = temp;
                end 
            end
        end

       for (i = 0; i < NUM_VALS; i = i + 1) begin
            sorted_bus[i*SIZE +: SIZE] = array[i+1];
       end
    end
endmodule

And a testbench: 和一个测试台:

module sort_tb;
    reg clk;
    reg  [16-1:0] in1,  in2,  in3,  in4,  in5;
    wire [16-1:0] out1, out2, out3, out4, out5;

    sort #(.NUM_VALS(5), .SIZE(16)) dut (
        .clk(clk),
        .in ({in1,  in2,  in3,  in4,  in5}),
        .out({out1, out2, out3, out4, out5})
    );

    always @(posedge clk) begin
        in1 <= $random;
        in2 <= $random;
        in3 <= $random;
        in4 <= $random;
        in5 <= $random;
    end

    always @(posedge clk) begin
        $display("In:  %0d %0d %0d %0d %0d", in1,  in2,  in3,  in4,  in5);
        $display("Out: %0d %0d %0d %0d %0d", out1, out2, out3, out4, out5);
    end

    initial begin
        #100;
        $finish;
    end

    always begin
        clk = 1'b0; #5;
        clk = 1'b1; #5;
    end
endmodule

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM