SV——generate constructs

0. 介紹

    generate構建是用來例化一個或者多個generate block;generate block可以是module item、一段語句或者嵌套的generate block。但不能有端口聲明。

    generate 調度是在elaboration階段,而不是在simulation。generate scheme的結果在simulation之前就確定了。所有在generate scheme階段的表達式都應該是常量表達式。

   可以用generate……endgenerate關鍵字,當然也可以不用。

   generate constructs有兩種loop generate constructs 和conditional generate constructs。

 

1. loop generate constructs

在循環生成中,一個generate block被重複生成多次。循環的索引變量應該被聲明爲genvar

The genvar is used as an integer during elaboration to evaluate the generate loop and create instances of the generate block, but it does not exist at simulation time. A genvar shall not be referenced anywhere other than in a loop generate scheme.

注意:

  1. 嵌套的循環生成中,不同的generate的genvar變量名不能相同。

  2. 如果generate block被命名,那麼名字不能和關鍵字、其他變量名重合,最好也不要和其他的block name重合。

// A parameterized gray-code–to–binary-code converter module using a loop to generate continuous assignments
module gray2bin1 (bin, gray);
parameter SIZE = 8; // this module is parameterizable
output [SIZE-1:0] bin;
input [SIZE-1:0] gray;
genvar i;
generate
for (i=0; i<SIZE; i=i+1) begin:bitnum
    assign bin[i] = ^gray[SIZE-1:i];
    // i refers to the implicitly defined localparam whose
    // value in each instance of the generate block is
    // the value of the genvar when it was elaborated.
end
endgenerate
endmodule
module addergen1 (co, sum, a, b, ci);
parameter SIZE = 4;
output [SIZE-1:0] sum;
output co;
input [SIZE-1:0] a, b;
input ci;
wire [SIZE :0] c;
genvar i;
assign c[0] = ci;
​
for(i=0; i<SIZE; i=i+1) begin:bitnum
    wire t1, t2, t3;
    xor g1 ( t1, a[i], b[i]);
    xor g2 ( sum[i], t1, c[i]);
    and g3 ( t2, a[i], b[i]);
    and g4 ( t3, t1, c[i]);
    or g5 ( c[i+1], t2, t3);
end
assign co = c[SIZE];
endmodule

 

2. conditional generate constructs

The conditional generate constructs, if-generate and case-generate, select at most one generate block from a set of alternative generate blocks based on constant expressions evaluated during elaboration.

 

可以通過if--else、case來進行條件判斷。

// 通過if--else判斷
module multiplier(a,b,product);
parameter a_width = 8, b_width = 8;
localparam product_width = a_width+b_width;
// cannot be modified directly with the defparam
// statement or the module instance statement #
input [a_width-1:0] a;
input [b_width-1:0] b;
output [product_width-1:0] product;
generate
    if((a_width < 8) || (b_width < 8)) begin: mult
    CLA_multiplier #(a_width,b_width) u1(a, b, product);
    // instantiate a CLA multiplier
    end
    else begin: mult
    WALLACE_multiplier #(a_width,b_width) u1(a, b, product);
    // instantiate a Wallace-tree multiplier
    end
endgenerate
// The hierarchical instance name is mult.u1
endmodule


// 通過case判斷
generate
    case (WIDTH)
        1: begin: adder // 1-bit adder implementation
            adder_1bit x1(co, sum, a, b, ci);
        end
        2: begin: adder // 2-bit adder implementation
            adder_2bit x1(co, sum, a, b, ci);
        end
        default:
            begin: adder // others - carry look-ahead adder
            adder_cla #(WIDTH) x1(co, sum, a, b, ci);
        end
    endcase
// The hierarchical instance name is adder.x1
endgenerate

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章