system-verilog - 根据参数 values 填充或截断向量分配

我有一个可以配置两个参数的模块。根据这些参数的 values ,我需要在将输出向量分配给输入向量时对其进行填充或截断。例如:

module my_mod(vector_in,
        vector_out);
    
    parameter IN_BITS = 10;
    parameter OUT_BITS = 8;
    // or
    // parameter IN_BITS = 8;
    // parameter OUT_BITS = 10;
    
    input [IN_BITS-1:0] vector_in;
    output[OUT_BITS-1:0] vector_out;
    
    // assign vector_out to vector_in
    // when OUT_BITS > IN_BITS => pad MSB's with zeors
    // when OUT_BITS < IN_BITS => truncate LSB's of in
    // else just assign
endmodule

做这个的最好方式是什么?

回答1

一种干净的方法是使用 generate if 构造。如果 out 位多于 in 位,则可以通过减去参数来实现填充。否则,使用 out 参数对输入进行位切片;这不应该是绝对必要的,但它可能会使设计意图更加明显。

module my_mod (vector_in, vector_out);
    parameter IN_BITS  = 10;
    parameter OUT_BITS =  8;

    input  [IN_BITS -1:0] vector_in;
    output [OUT_BITS-1:0] vector_out;

    if (OUT_BITS > IN_BITS) begin
        assign vector_out = { {(OUT_BITS-IN_BITS){1'b0}} , vector_in };
    end else begin
        assign vector_out = vector_in[OUT_BITS-1 : 0];
    end
endmodule

请参阅 IEEE Std 1800-2017,第 27 节。生成构造

相似文章

随机推荐

最新文章