You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Verilog 2011中打印数组值(非SystemVerilog)?

Verilog 2011环境下打印多维数组的解决方法

在Verilog 2011标准中,$display系统任务不支持直接打印整个多维内存数组(比如你代码里的hh和hh2),必须逐个访问数组元素才能完成打印。以下是两种可行的实现方式:

方法一:嵌套循环遍历打印(适合元素数量多的数组)

通过嵌套for循环遍历数组的每个维度索引,逐个打印元素。Verilog 2011要求循环变量必须在块内声明为integer类型。

修改后的代码示例:

`timescale 1ns/1ps   
module m_top 
(
input GCLK,//100MHz,Y9
input [7:0] i_in1,
output  o_out1,
output reg o_out2,
inout io_data
);

reg hh [2:0][1:0] ;
reg [2:0]hh2[1:0] ;

always @(negedge GCLK)begin
    hh[1][1] = 3'b1;
    // 打印hh数组所有元素
    integer i, j;
    $display("--- Printing hh array ---");
    for (i = 0; i <= 2; i = i + 1) begin
        for (j = 0; j <= 1; j = j + 1) begin
            $display("hh[%0d][%0d] = %b", i, j, hh[i][j]);
        end
    end

    hh2[1][1] = 3'b1;
    // 打印hh2数组所有元素
    $display("--- Printing hh2 array ---");
    for (i = 0; i <= 1; i = i + 1) begin
        $display("hh2[%0d] = %b", i, hh2[i]);
    end
end
endmodule

方法二:逐个元素直接打印(适合规模小的数组)

如果数组元素数量不多,可以直接通过索引逐个指定元素打印,代码更直观。

修改后的代码示例:

`timescale 1ns/1ps   
module m_top 
(
input GCLK,//100MHz,Y9
input [7:0] i_in1,
output  o_out1,
output reg o_out2,
inout io_data
);

reg hh [2:0][1:0] ;
reg [2:0]hh2[1:0] ;

always @(negedge GCLK)begin
    hh[1][1] = 3'b1;
    // 逐个打印hh数组元素
    $display("hh[0][0] = %b, hh[0][1] = %b", hh[0][0], hh[0][1]);
    $display("hh[1][0] = %b, hh[1][1] = %b", hh[1][0], hh[1][1]);
    $display("hh[2][0] = %b, hh[2][1] = %b", hh[2][0], hh[2][1]);

    hh2[1][1] = 3'b1;
    // 逐个打印hh2数组元素
    $display("hh2[0] = %b, hh2[1] = %b", hh2[0], hh2[1]);
end
endmodule

两种方法都能避开直接打印整个数组的限制,在Verilog 2011环境下正常输出数组内容。

内容的提问来源于stack exchange,提问作者kittygirl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 14:40:04