如何自动缩放 $display 列宽?
How can I automatically scale a $display column width?
我想要 $display
列中的字符串,例如固定宽度 table。
但是,我不知道我的字符串的最大列宽是多少
提前。
假设我有一组 SystemVerilog 字符串 (names
)。
当我 $display
他们时,我猜测列 (10) 的宽度,
但我的猜测太小了:
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),但它可能会大很多
比我需要的。
如何自动缩放 $display
的宽度?
遍历数组计算最大字符串长度
使用 len
数组方法。请参阅 IEEE 标准 1800-2017,
6.16 节字符串数据类型。
然后使用 $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 上的可运行示例。
上面的输出是右对齐的。要获得左对齐输出,请使用:
fmt = $sformatf("| %%-%0ds |", maxlen);
输出:
| ALU |
| COMPARATOR |
| MEMORY |
| FLOP |
| ram_macro_with_a_long_name |
我想要 $display
列中的字符串,例如固定宽度 table。
但是,我不知道我的字符串的最大列宽是多少
提前。
假设我有一组 SystemVerilog 字符串 (names
)。
当我 $display
他们时,我猜测列 (10) 的宽度,
但我的猜测太小了:
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),但它可能会大很多 比我需要的。
如何自动缩放 $display
的宽度?
遍历数组计算最大字符串长度
使用 len
数组方法。请参阅 IEEE 标准 1800-2017,
6.16 节字符串数据类型。
然后使用 $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 上的可运行示例。
上面的输出是右对齐的。要获得左对齐输出,请使用:
fmt = $sformatf("| %%-%0ds |", maxlen);
输出:
| ALU |
| COMPARATOR |
| MEMORY |
| FLOP |
| ram_macro_with_a_long_name |