如何自动缩放 $display 列宽? [英] How can I automatically scale a $display column width?

查看:24
本文介绍了如何自动缩放 $display 列宽?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个列中$display 字符串,就像在一个固定宽度的表格中一样.但是,我不知道我的字符串的最大列宽是多少提前.

I want to $display strings in a column like in a fixed-width table. However, I don't know what the maximum column width of my strings is ahead of time.

假设我有一个 SystemVerilog 字符串数组(names).当我 $display 它们时,我猜测列的宽度 (10),但我的猜测太小了:

Lets say I have an array of SystemVerilog strings (names). When I $display them, I guess at a width for the column (10), but my guess is too small:

module tb;

string names [5];

initial begin
    names = '{
        "ALU"           ,
        "COMPARATOR_3"  ,
        "MEMORY"        ,
        "FLOP"          ,
        "ram_macro_with_a_long_name"
    };

    // Display all elements of the array
    foreach (names[i]) begin
        $display("| %10s |", names[i]);
    end
end

endmodule

这是输出:

|        ALU |
| COMPARATOR_3 |
|     MEMORY |
|       FLOP |
| ram_macro_with_a_long_name |

这是我想要的输出:

|                        ALU |
|                 COMPARATOR |
|                     MEMORY |
|                       FLOP |
| ram_macro_with_a_long_name |

我可以猜出一个非常大的数字(比如 100),但它可能要大得多比我需要的多.

I could guess a really big number (like 100), but it might be a lot bigger than I need.

如何自动缩放$display的宽度?

推荐答案

遍历数组计算最大字符串长度使用 len 数组方法.参考 IEEE Std 1800-2017,第 6.16 节 字符串数据类型.然后使用 $sformatf 创建格式字符串.

Loop through the array to calculate the maximum string length using the len array method. Refer to IEEE Std 1800-2017, section 6.16 String data type. Then create the format string using $sformatf.

module tb;

string names [5];
int maxlen = 0;
string fmt;

initial begin
    names = '{
        "ALU"           ,
        "COMPARATOR"    ,
        "MEMORY"        ,
        "FLOP"          ,
        "ram_macro_with_a_long_name"
    };

    // First, lets calculate the maximum string length
    foreach (names[i]) begin
        if (names[i].len() > maxlen) maxlen = names[i].len();
    end

    // Create the format which will be used by $display
    //      %%  ... double "%" is needed to create a literal "%"
    //      %0d ... this formats the maxlen number
    //      s   ... string format
    //      |   ... this is just the character I chose for the start/end of the field
    fmt = $sformatf("| %%%0ds |", maxlen);

    // Display all elements of the array
    foreach (names[i]) begin
        $display($sformatf(fmt, names[i]));
    end
end

endmodule

这是输出:

|                        ALU |
|                 COMPARATOR |
|                     MEMORY |
|                       FLOP |
| ram_macro_with_a_long_name |

这是一个关于 edaplayground 的可运行示例.

Here is a runnable example on edaplayground.

上面的输出是右对齐的.要获得左对齐的输出,请使用:

The output above is right-justified. To get left-justified output, use:

fmt = $sformatf("| %%-%0ds |", maxlen);

输出:

| ALU                        |
| COMPARATOR                 |
| MEMORY                     |
| FLOP                       |
| ram_macro_with_a_long_name |

这篇关于如何自动缩放 $display 列宽?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆