Generate constructs
本文最后更新于:Tuesday, November 10th 2020, 8:28 pm
Preface
Generate constructs are used to either conditionally or multiply instantiate generate blocks into a model
生成结构用于要么有条件地或成倍地实例化生成块到一个模型中。
有两种生成结构:loops和conditionals
loops: 用于实例生成块多次
conditionals: 包括if-generate和case-generate结构;最多从一系列generate blocks中选择一个.
Generate schemesare evaluated during elaboration of the model.生成方法:决定哪个生成块被实例化或多少生成块被实例化的方法。
Elaboration occurs after parsing the HDL and before simulation(生成发生在解析HDL之后仿真之前);包括如下五个过程:
- expanding module instantiations
- computing parameter values
- resolving hierarchical names
- establishing net connectivity
- preparing the model for simulation
Loop generate constructs
loop生成结构运行使用类似for loop 的语法把一个生成块实例多次。循环索引变量应该在使用前用genvar声明;genvar只在elaboration时有用,所以你不能在生成块任何地方引用它。
参数化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15module 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 :bit
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使用二维net把生成的实例连接起来
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
32module 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;
wire [SIZE-1:0] t [1:3];
genvar i;
assign c[0] = ci;
// Hierarchical gate instance names are:
// xor gates: bit[0].g1 bit[1].g1 bit[2].g1 bit[3].g1
// bit[0].g2 bit[1].g2 bit[2].g2 bit[3].g2
// and gates: bit[0].g3 bit[1].g3 bit[2].g3 bit[3].g3
// bit[0].g4 bit[1].g4 bit[2].g4 bit[3].g4
// or gates: bit[0].g5 bit[1].g5 bit[2].g5 bit[3].g5
// Generated instances are connected with
// multidimensional nets t[1][3:0] t[2][3:0] t[3][3:0]
// (12 nets total)
for (i=0; i<SIZE; i=i+1) begin :bit
xor g1 ( t[1][i], a[i], b[i]);
xor g2 ( sum[i], t[1][i], c[i]);
and g3 ( t[2][i], a[i], b[i]);
and g4 ( t[3][i], t[1][i], c[i]);
or g5 ( c[i+1], t[2][i], t[3][i]);
end
assign co = c[SIZE];
endmodulemultilevel generate loop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24parameter SIZE = 2;
genvar i, j, k, m;
generate
for (i=0; i<SIZE; i=i+1) begin :B1 // scope B1[i]
M1 N1(); // instantiates B1[i].N1
for (j=0; j<SIZE; j=j+1) begin :B2 // scope B1[i].B2[j]
M2 N2(); // instantiates B1[i].B2[j].N2
for (k=0; k<SIZE; k=k+1) begin :B3 // scope B1[i].B2[j].B3[k]
M3 N3(); // instantiates B1[i].B2[j].B3[k].N3
end
end
if (i>0) begin :B4 // scope B1[i].B4
for (m=0; m<SIZE; m=m+1) begin :B5 // scope B1[i].B4.B5[m]
M4 N4(); // instantiates B1[i].B4.B5[m].N4
end
end
end
endgenerate
// Some examples of hierarchical names for the module instances:
// B1[0].N1 B1[1].N1
// B1[0].B2[0].N2 B1[0].B2[1].N2
// B1[0].B2[0].B3[0].N3 B1[0].B2[0].B3[1].N3
// B1[0].B2[1].B3[0].N3
// B1[1].B4.B5[0].N4 B1[1].B4.B5[1].N4Conditional generate constructs
条件生成结构只能选择一个生成块
1 | |
External names for unnamed
尽管一个没有名字的生成块没有可以被使用的阶级名字(hierarchical name),但是它需要一个名字让外部接口可以访问它。因此出于该目的将会给他们分配名字。
所有没名字的生成块将被给予
genblk<n>名字,其中是被分配到它包括的生成结构的次数。(从1开始计数);如果有冲突,在 前面加0直到不冲突为止
1 | |
本博客所有文章除特别声明外,均采用 CC BY-SA 3.0协议 。转载请注明出处!