verilog中哪种数据类型相当于“变量”?在 VHDL 中?

发布于 2024-12-28 21:49:54 字数 4234 浏览 1 评论 0原文

如何为以下 VHDL 代码编写等效的 verilog 代码? 我在 VHDL 代码后面展示了我的 verilog 代码。 verilog代码确实可以编译,但aux在整个仿真过程中无效。

VHDL: (classic_multiplier_parameters.vhd 定义 m = 8)

library ieee; 
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
use work.classic_multiplier_parameters.all;

entity poly_multiplier is
port (
  a, b: in std_logic_vector(M-1 downto 0);
  d: out std_logic_vector(2*M-2 downto 0)
);
end poly_multiplier;


architecture simple of poly_multiplier is
  type matrix_ands is array (0 to 2*M-2) of STD_LOGIC_VECTOR(2*M-2 downto 0);
  signal a_by_b: matrix_ands;
  signal c: std_logic_vector(2*M-2 downto 0);
begin

  gen_ands: for k in 0 to M-1 generate
    l1: for i in 0 to k generate
       a_by_b(k)(i) <= A(i) and B(k-i);
    end generate;
  end generate;

  gen_ands2: for k in M to 2*M-2 generate
    l2: for i in k to 2*M-2 generate
       a_by_b(k)(i) <= A(k-i+(M-1)) and B(i-(M-1));
    end generate;
  end generate;

  d(0) <= a_by_b(0)(0);
  gen_xors: for k in 1 to 2*M-2 generate
    l3: process(a_by_b(k),c(k)) 
        variable aux: std_logic;
        begin
        if (k < M) then
          aux := a_by_b(k)(0);
          for i in 1 to k loop aux := a_by_b(k)(i) xor aux; end loop;
        else
          aux := a_by_b(k)(k);
          for i in k+1 to 2*M-2 loop aux := a_by_b(k)(i) xor aux; end loop;
        end if;
        d(k) <= aux;
    end process;
  end generate;

end simple;

Verilog:

module mul(
    a, b,
    d);
parameter M = 8;

input [M-1:0] a, b;
output [2*M-2:0] d;

wire [2*M-2:0] a_by_b [2*M-2:0];
wire aux;
//`UNPACK_ARRAY(2*M-2, 2*M-2, pack_a_by_b, a_by_b)

    //the first and
    genvar i, k;
    generate 
    for(k=0; k<=M-1; k=k+1) begin: for1_outer
        for(i=0; i<=k; i=i+1) begin: for1_inner
            assign a_by_b[k][i] = a[i] & b[k-i];
        end
    end
    endgenerate

    //second and
    generate 
    for(k=M; k<=2*M-2; k=k+1) begin: for2_outer
        for(i=k; i<=2*M-2; i=i+1) begin: for2_inner
            assign a_by_b[k][i] = a[k-i+(M-1)] & b[i-(M-1)];
        end 
    end
    endgenerate

    assign d[0] = a_by_b[0][0];
    // xors
    generate
    for(k=1; k<=2*M-2; k=k+1) begin: for3_outer
        if(k < M) begin
            assign aux = a_by_b[k][0];
            for(i=1; i<=k; i=i+1) begin: for3_inner1
                assign aux = a_by_b[k][i] ^ aux;
            end
        end
        else begin
            assign aux = a_by_b[k][k];
            for(i=k+1; i<=2*M-2; i=i+1) begin: for3_inner2
                assign aux = a_by_b[k][i] ^ aux;
            end
        end
        assign d[k] = aux;
    end
    endgenerate
endmodule

Verilog: (aux 作为 reg 类型)

module mul(
    a, b,
    d);
parameter M = 3;

input [M-1:0] a, b;
output [2*M-2:0] d;

wire [2*M-2:0] a_by_b [2*M-2:0];
reg aux = 1'b1;
//`UNPACK_ARRAY(2*M-2, 2*M-2, pack_a_by_b, a_by_b)

    //the first and
    genvar i, k;
    generate 
    for(k=0; k<=M-1; k=k+1) begin: for1_outer
        for(i=0; i<=k; i=i+1) begin: for1_inner
            assign a_by_b[k][i] = a[i] & b[k-i];
        end
    end
    endgenerate

    //second and
    generate 
    for(k=M; k<=2*M-2; k=k+1) begin: for2_outer
        for(i=k; i<=2*M-2; i=i+1) begin: for2_inner
            assign a_by_b[k][i] = a[k-i+(M-1)] & b[i-(M-1)];
        end 
    end
    endgenerate

    assign d[0] = a_by_b[0][0];
    // xors
    generate
    for(k=1; k<=2*M-2; k=k+1) begin: for3_outer
        if(k < M) begin
            always @(*) begin 
                aux = a_by_b[k][0];
            end
            for(i=1; i<=k; i=i+1) begin: for3_inner1

            always @(*) begin 
                aux <= a_by_b[k][i] ^ aux;
            end
            end
        end
        else begin

            always @(*) begin 
            aux <= a_by_b[k][k];
            end
            for(i=k+1; i<=2*M-2; i=i+1) begin: for3_inner2

            always @(*) begin 
                aux <= a_by_b[k][i] ^ aux;
            end
            end
        end
        assign d[k] = aux;
    end
    endgenerate
endmodule

How do I write equivalent verilog code for the below VHDL code?
I show my verilog code behind the VHDL code. The verilog code does compile, but aux is invalid during the entire simulation.

VHDL: (classic_multiplier_parameters.vhd defines m = 8)

library ieee; 
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
use work.classic_multiplier_parameters.all;

entity poly_multiplier is
port (
  a, b: in std_logic_vector(M-1 downto 0);
  d: out std_logic_vector(2*M-2 downto 0)
);
end poly_multiplier;


architecture simple of poly_multiplier is
  type matrix_ands is array (0 to 2*M-2) of STD_LOGIC_VECTOR(2*M-2 downto 0);
  signal a_by_b: matrix_ands;
  signal c: std_logic_vector(2*M-2 downto 0);
begin

  gen_ands: for k in 0 to M-1 generate
    l1: for i in 0 to k generate
       a_by_b(k)(i) <= A(i) and B(k-i);
    end generate;
  end generate;

  gen_ands2: for k in M to 2*M-2 generate
    l2: for i in k to 2*M-2 generate
       a_by_b(k)(i) <= A(k-i+(M-1)) and B(i-(M-1));
    end generate;
  end generate;

  d(0) <= a_by_b(0)(0);
  gen_xors: for k in 1 to 2*M-2 generate
    l3: process(a_by_b(k),c(k)) 
        variable aux: std_logic;
        begin
        if (k < M) then
          aux := a_by_b(k)(0);
          for i in 1 to k loop aux := a_by_b(k)(i) xor aux; end loop;
        else
          aux := a_by_b(k)(k);
          for i in k+1 to 2*M-2 loop aux := a_by_b(k)(i) xor aux; end loop;
        end if;
        d(k) <= aux;
    end process;
  end generate;

end simple;

Verilog:

module mul(
    a, b,
    d);
parameter M = 8;

input [M-1:0] a, b;
output [2*M-2:0] d;

wire [2*M-2:0] a_by_b [2*M-2:0];
wire aux;
//`UNPACK_ARRAY(2*M-2, 2*M-2, pack_a_by_b, a_by_b)

    //the first and
    genvar i, k;
    generate 
    for(k=0; k<=M-1; k=k+1) begin: for1_outer
        for(i=0; i<=k; i=i+1) begin: for1_inner
            assign a_by_b[k][i] = a[i] & b[k-i];
        end
    end
    endgenerate

    //second and
    generate 
    for(k=M; k<=2*M-2; k=k+1) begin: for2_outer
        for(i=k; i<=2*M-2; i=i+1) begin: for2_inner
            assign a_by_b[k][i] = a[k-i+(M-1)] & b[i-(M-1)];
        end 
    end
    endgenerate

    assign d[0] = a_by_b[0][0];
    // xors
    generate
    for(k=1; k<=2*M-2; k=k+1) begin: for3_outer
        if(k < M) begin
            assign aux = a_by_b[k][0];
            for(i=1; i<=k; i=i+1) begin: for3_inner1
                assign aux = a_by_b[k][i] ^ aux;
            end
        end
        else begin
            assign aux = a_by_b[k][k];
            for(i=k+1; i<=2*M-2; i=i+1) begin: for3_inner2
                assign aux = a_by_b[k][i] ^ aux;
            end
        end
        assign d[k] = aux;
    end
    endgenerate
endmodule

Verilog: (aux as reg type)

module mul(
    a, b,
    d);
parameter M = 3;

input [M-1:0] a, b;
output [2*M-2:0] d;

wire [2*M-2:0] a_by_b [2*M-2:0];
reg aux = 1'b1;
//`UNPACK_ARRAY(2*M-2, 2*M-2, pack_a_by_b, a_by_b)

    //the first and
    genvar i, k;
    generate 
    for(k=0; k<=M-1; k=k+1) begin: for1_outer
        for(i=0; i<=k; i=i+1) begin: for1_inner
            assign a_by_b[k][i] = a[i] & b[k-i];
        end
    end
    endgenerate

    //second and
    generate 
    for(k=M; k<=2*M-2; k=k+1) begin: for2_outer
        for(i=k; i<=2*M-2; i=i+1) begin: for2_inner
            assign a_by_b[k][i] = a[k-i+(M-1)] & b[i-(M-1)];
        end 
    end
    endgenerate

    assign d[0] = a_by_b[0][0];
    // xors
    generate
    for(k=1; k<=2*M-2; k=k+1) begin: for3_outer
        if(k < M) begin
            always @(*) begin 
                aux = a_by_b[k][0];
            end
            for(i=1; i<=k; i=i+1) begin: for3_inner1

            always @(*) begin 
                aux <= a_by_b[k][i] ^ aux;
            end
            end
        end
        else begin

            always @(*) begin 
            aux <= a_by_b[k][k];
            end
            for(i=k+1; i<=2*M-2; i=i+1) begin: for3_inner2

            always @(*) begin 
                aux <= a_by_b[k][i] ^ aux;
            end
            end
        end
        assign d[k] = aux;
    end
    endgenerate
endmodule

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

潜移默化 2025-01-04 21:49:54

听起来您正在寻找“reg”。例如,使用reg aux代替wire aux,并在分配时去掉“分配”。

(也就是说,这似乎并不是这段代码的唯一问题。)

Sounds like you are looking for "reg". So for instance, reg aux instead of wire aux, and get rid of the "assigns" when you assign it.

(That said, that doesn't seem to be the only problem with this code.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文