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

使用GHDL、Yosys及插件转VHDL到Verilog时,将std_logic_vector转为非打包数组端口

如何将GHDL+Yosys转换的Verilog打包数组改为非打包数组?

我正在用GHDL、Yosys及ghdl-yosys-plugin将VHDL设计转成Verilog,执行的命令如下:

$ yosys -m ghdl
yosys> ghdl --std=08 gol_cell.vhd gol_grid_array.vhd -e gol_grid_array
yosys> write_verilog gol_grid_array.v

我的顶层VHDL模块gol_grid_array端口声明:

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity gol_grid_array is
    generic (
        WSIZE : integer := 6;
        HSIZE : integer := 6
    );
    port (
        clk    : in  std_logic;
        rst    : in  std_logic;
        init   : in  std_logic_vector(WSIZE*HSIZE-1 downto 0);
        s_grid : out std_logic_vector(WSIZE*HSIZE-1 downto 0);
        load   : in  std_logic;
        en     : in  std_logic;
        s      : out std_logic
    );
end entity;

转换后的Verilog端口里,所有std_logic_vector都变成了打包数组:

module gol_grid_array(clk, rst, init, load, en, s_grid, s);
  input clk;
  wire clk;
  input rst;
  wire rst;
  input [35:0] init;
  wire [35:0] init;
  input load;
  wire load;
  input en;
  wire en;
  output [35:0] s_grid;
  wire [35:0] s_grid;
  output s;
  wire s;

请问有没有办法把这些打包数组转换成非打包数组?


两种实现思路

1. 在Yosys转换流程中拆分打包数组

可以直接用Yosys的splitnets命令,把打包向量拆成非打包的二维数组。具体操作如下:

在执行write_verilog前,添加拆分命令:

# 拆分init端口为HSIZE行、WSIZE列的非打包数组
splitnets -ports -init init -output init[%d][%d] -size {HSIZE, WSIZE}
# 对s_grid端口执行同样的拆分操作
splitnets -ports -init s_grid -output s_grid[%d][%d] -size {HSIZE, WSIZE}

完整命令流程变为:

$ yosys -m ghdl
yosys> ghdl --std=08 gol_cell.vhd gol_grid_array.vhd -e gol_grid_array
yosys> set hsize [getattr -generic gol_grid_array HSIZE]
yosys> set wsize [getattr -generic gol_grid_array WSIZE]
yosys> splitnets -ports -init init -output init[%d][%d] -size {$hsize, $wsize}
yosys> splitnets -ports -init s_grid -output s_grid[%d][%d] -size {$hsize, $wsize}
yosys> write_verilog gol_grid_array.v

执行后生成的Verilog端口会变成非打包数组形式:

module gol_grid_array(clk, rst, init, load, en, s_grid, s);
  input clk;
  wire clk;
  input rst;
  wire rst;
  input init[5:0][5:0];
  wire init[5:0][5:0];
  input load;
  wire load;
  input en;
  wire en;
  output s_grid[5:0][5:0];
  wire s_grid[5:0][5:0];
  output s;
  wire s;

2. 修改VHDL端口定义为二维数组

如果允许修改VHDL源码,直接把std_logic_vector换成二维std_logic数组,GHDL+Yosys会直接生成Verilog非打包数组。

修改后的VHDL端口声明:

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

-- 定义二维数组类型
type grid_type is array (HSIZE-1 downto 0, WSIZE-1 downto 0) of std_logic;

entity gol_grid_array is
    generic (
        WSIZE : integer := 6;
        HSIZE : integer := 6
    );
    port (
        clk    : in  std_logic;
        rst    : in  std_logic;
        init   : in  grid_type;
        s_grid : out grid_type;
        load   : in  std_logic;
        en     : in  std_logic;
        s      : out std_logic
    );
end entity;

重新执行转换命令后,生成的Verilog端口会直接是非打包数组格式,无需额外处理。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.11 22:46:02