在 Verilog 中,如何在实例化时定义端口宽度?
In Verilog, how can I define the width of a port at instantiation?
在 Verilog 2001 模块中,如何在实例化时定义端口宽度?
例如,乘法器和实例化它的模块可能如下所示:
module Multiplier #(parameter WIDTH = 8, parameter WIDTH1 = 8, WIDTH2 = 8) (
output [WIDTH-1:0] product,
input [WIDTH1-1:0] factor1,
input [WIDTH2-1:0] factor2
);
parameter WIDTHI = WIDTH1+WIDTH2;
wire [WIDTHI-1:0] intermediate_product;
assign intermediate_product = factor1 * factor2;
assign product = (intermediate_product >>> (WIDTHI-WIDTH));
endmodule
module User();
parameter WIDTH_OF_PRODUCT = 8;
parameter WIDTH_OF_FACTOR_1 = 8;
parameter WIDTH_OF_FACTOR_2 = 8;
wire [WIDTH_OF_PRODUCT] product;
reg [WIDTH_OF_FACTOR_1] factor1;
reg [WIDTH_OF_FACTOR_2] factor2;
Multiplier #(WIDTH_OF_PRODUCT,WIDTH_OF_FACTOR_1,WIDTH_OF_FACTOR_2)
m0(product, factor1, factor2);
endmodule
如果User中的网络宽度是在编译时定义的,有什么方法可以自动定义Multiplier实例化中的端口宽度?
我想要一个如下所示的实例化:
Multiplier m0(product, factor1, factor2);
这可能涉及更改 Multiplier 模块的定义。
请记住,这些只是示例。我的问题是关于这两个模块是如何根据它们的网络和端口定义的。
提前致谢!
更新:
这里是一个不适用于按原样定义的乘数的情况:
module Top();
parameter WIDE = 8;
parameter NARROW = 4;
wire [NARROW-1:0] narrow_product;
wire [WIDE-1:0] wide_product;
reg [NARROW-1:0] narrow_factor_1;
reg [WIDE-1:0] wide_factor_1;
reg [NARROW-1:0] narrow_factor_2;
reg [WIDE-1:0] wide_factor_1;
Multiplier mWide(wide_product, wide_factor_1, wide_factor_2);
Multiplier mNarrow(narrow_product, narrow_factor_1, narrow_factor_2); // PROBLEM
endmodule
我这样做的动机是网络代表 定点数 ,一种带有小数部分的数字表示。我将不得不为定点数指定一个精度。虽然这可能是一个全局常量,但我可能想使用不同的精度。所以,我可能需要参数来指定模块中端口的精度。
更新:
好吧,我不能做我想做的事。所以这就是我所做的,以防对任何人有帮助:
由于输入和输出的宽度相同,所以我这样定义它们:
module Multiplier #(parameter WIDTH = 8,
parameter WIDTH1 = WIDTH,
parameter WIDTH2 = WIDTH1) (
output [WIDTH-1:0] product,
input [WIDTH1-1:0] factor1,
input [WIDTH2-1:0] factor2
);
...
endmodule
那么我可以这样实例化:
Multiplier #(16) m0(product, factor1, factor2);
如果产品和因素都具有相同的长度。它比指定所有这些要简洁一些,尤其是当我使用实例化如下所示的定点数时:
Multiplier #(16,8) m0(product, factor1, factor2);
其中第二个数字是 PRECISION 参数,给出小数部分的宽度。 (这需要更新乘法器模块定义以包括 PRECISION、PRECISION1 和 PRECISION2,其定义类似于 WIDTH 定义。
参数的定义正是出于他的原因,您只需将参数传递给实例:
module Top();
localparam WIDE = 8;
localparam NARROW = 4;
wire [NARROW-1:0] narrow_product;
wire [WIDE-1:0] wide_product;
reg [NARROW-1:0] narrow_factor_1;
reg [WIDE-1:0] wide_factor_1;
reg [NARROW-1:0] narrow_factor_2;
reg [WIDE-1:0] wide_factor_1;
Multiplier #(
.WIDTH (WIDE),
.WIDTH1 (WIDE),
.WIDTH2 (WIDE)
) mWide (
.product (wide_product),
.factor1 (wide_factor_1),
.factor2 (wide_factor_2)
);
Multiplier #(
.WIDTH (NARROW),
.WIDTH1 (NARROW),
.WIDTH2 (NARROW)
) mNarrow(
.product (narrow_product),
.factor1( narrow_factor_1),
.factor2( narrow_factor_2)
); // NOT A PROBLEM
endmodule
乘数:
module Multiplier #(
parameter WIDTH = 8,
parameter WIDTH1 = 8,
parameter WIDTH2 = 8
) (
output [WIDTH-1:0] product,
input [WIDTH1-1:0] factor1,
input [WIDTH2-1:0] factor2
);
//...
在 Verilog 2001 模块中,如何在实例化时定义端口宽度?
例如,乘法器和实例化它的模块可能如下所示:
module Multiplier #(parameter WIDTH = 8, parameter WIDTH1 = 8, WIDTH2 = 8) (
output [WIDTH-1:0] product,
input [WIDTH1-1:0] factor1,
input [WIDTH2-1:0] factor2
);
parameter WIDTHI = WIDTH1+WIDTH2;
wire [WIDTHI-1:0] intermediate_product;
assign intermediate_product = factor1 * factor2;
assign product = (intermediate_product >>> (WIDTHI-WIDTH));
endmodule
module User();
parameter WIDTH_OF_PRODUCT = 8;
parameter WIDTH_OF_FACTOR_1 = 8;
parameter WIDTH_OF_FACTOR_2 = 8;
wire [WIDTH_OF_PRODUCT] product;
reg [WIDTH_OF_FACTOR_1] factor1;
reg [WIDTH_OF_FACTOR_2] factor2;
Multiplier #(WIDTH_OF_PRODUCT,WIDTH_OF_FACTOR_1,WIDTH_OF_FACTOR_2)
m0(product, factor1, factor2);
endmodule
如果User中的网络宽度是在编译时定义的,有什么方法可以自动定义Multiplier实例化中的端口宽度?
我想要一个如下所示的实例化:
Multiplier m0(product, factor1, factor2);
这可能涉及更改 Multiplier 模块的定义。
请记住,这些只是示例。我的问题是关于这两个模块是如何根据它们的网络和端口定义的。
提前致谢!
更新:
这里是一个不适用于按原样定义的乘数的情况:
module Top();
parameter WIDE = 8;
parameter NARROW = 4;
wire [NARROW-1:0] narrow_product;
wire [WIDE-1:0] wide_product;
reg [NARROW-1:0] narrow_factor_1;
reg [WIDE-1:0] wide_factor_1;
reg [NARROW-1:0] narrow_factor_2;
reg [WIDE-1:0] wide_factor_1;
Multiplier mWide(wide_product, wide_factor_1, wide_factor_2);
Multiplier mNarrow(narrow_product, narrow_factor_1, narrow_factor_2); // PROBLEM
endmodule
我这样做的动机是网络代表 定点数 ,一种带有小数部分的数字表示。我将不得不为定点数指定一个精度。虽然这可能是一个全局常量,但我可能想使用不同的精度。所以,我可能需要参数来指定模块中端口的精度。
更新: 好吧,我不能做我想做的事。所以这就是我所做的,以防对任何人有帮助:
由于输入和输出的宽度相同,所以我这样定义它们:
module Multiplier #(parameter WIDTH = 8,
parameter WIDTH1 = WIDTH,
parameter WIDTH2 = WIDTH1) (
output [WIDTH-1:0] product,
input [WIDTH1-1:0] factor1,
input [WIDTH2-1:0] factor2
);
...
endmodule
那么我可以这样实例化:
Multiplier #(16) m0(product, factor1, factor2);
如果产品和因素都具有相同的长度。它比指定所有这些要简洁一些,尤其是当我使用实例化如下所示的定点数时:
Multiplier #(16,8) m0(product, factor1, factor2);
其中第二个数字是 PRECISION 参数,给出小数部分的宽度。 (这需要更新乘法器模块定义以包括 PRECISION、PRECISION1 和 PRECISION2,其定义类似于 WIDTH 定义。
参数的定义正是出于他的原因,您只需将参数传递给实例:
module Top();
localparam WIDE = 8;
localparam NARROW = 4;
wire [NARROW-1:0] narrow_product;
wire [WIDE-1:0] wide_product;
reg [NARROW-1:0] narrow_factor_1;
reg [WIDE-1:0] wide_factor_1;
reg [NARROW-1:0] narrow_factor_2;
reg [WIDE-1:0] wide_factor_1;
Multiplier #(
.WIDTH (WIDE),
.WIDTH1 (WIDE),
.WIDTH2 (WIDE)
) mWide (
.product (wide_product),
.factor1 (wide_factor_1),
.factor2 (wide_factor_2)
);
Multiplier #(
.WIDTH (NARROW),
.WIDTH1 (NARROW),
.WIDTH2 (NARROW)
) mNarrow(
.product (narrow_product),
.factor1( narrow_factor_1),
.factor2( narrow_factor_2)
); // NOT A PROBLEM
endmodule
乘数:
module Multiplier #(
parameter WIDTH = 8,
parameter WIDTH1 = 8,
parameter WIDTH2 = 8
) (
output [WIDTH-1:0] product,
input [WIDTH1-1:0] factor1,
input [WIDTH2-1:0] factor2
);
//...