【问题标题】:VHDL to Verilog [duplicate]VHDL到Verilog [重复]
【发布时间】:2020-06-25 17:47:46
【问题描述】:

我有一些 VHDL 代码我正在尝试转换为 Verilog。

VHDL 代码运行良好

library ieee;                                
use ieee.std_logic_1164.all;                 
                                             
entity find_errors is port(                      
    a: bit_vector(0 to 3);                   
    b: out std_logic_vector(3 downto 0);         
    c: in bit_vector(5 downto 0));            
end find_errors;                            
                                             
architecture not_good of find_errors is        
  begin                                      
  my_label: process (a,c)                         
    begin                                    
    if c = "111111" then                                
      b <= To_StdLogicVector(a);                                
    else                                     
     b <= "0101";                            
    end if;                                   
  end process;                              
end not_good;                                 

我拥有的 Verilog 代码出现错误“非法引用网络“bw”。” 和 连续赋值左边的寄存器是非法的

module find_errors(                            
  input  [3:0]a,                             
  output [3:0]b,                             
  input [5:0]c                               
);                                            
  wire [0:3]aw;                              
  wire [3:0]bw;                             
  reg [5:0]creg;                              
                                       
  assign aw = a;                             
  assign b = bw;                             
  assign creg = c;                          
always @(a,c)                                      
  begin                                      
    if (creg == 4'b1111)   
       bw <= aw;                              
    else                                     
     bw <= 4'b0101;                            
    end                                                          
endmodule 

【问题讨论】:

  • 为了允许在always 块中分配给bw,它需要声明为reg。另一方面,creg 必须声明为 wire,而不是 reg,以便允许在连续赋值的左侧(always 之外)。
  • 另请注意c 是 6 位宽,您可以将其与 4 位值进行比较。

标签: vhdl verilog system-verilog hdl


【解决方案1】:

看起来很接近,但有一些问题/错误需要修复,请参阅固定代码中的内联 cmets:

module find_errors(                            
  input  wire [3:0] a, // Better to be explicit about the types even if 
                       // its not strictly necessary                            
  output reg  [3:0] b, // As mentioned in the comments, the error youre
                       // seeing is because b is a net type by default; when 
                       // describing logic in any always block, you need to 
                       // use variable types, like reg or logic (for 
                       // SystemVerilog); see the comment for a thread 
                       // describing the difference     
  input wire [5:0] c);                             
  
  // You dont really need any local signals as the logic is pretty simple                       
  
  always @(*) begin // Always use either always @(*), assign or 
                    // always_comb (if using SystemVerilog) for combinational logic                                                                 
    if (c == 6'b111111)   
      b = a; // For combinational logic, use blocking assignment ("=") 
             // instead of non-blocking assignment ("<="), NBA is used for 
             // registers/sequential logic                              
    else                                     
      b = 4'b0101;                            
  end                                                          
endmodule 

【讨论】:

  • 虽然是一个很好的经验法则,但值得注意的是这不是组合逻辑因为使用了阻塞赋值。在这种情况下,它没有任何区别。
  • 作为参考,该指南在sunburst-design.com/papers/CummingsSNUG2000SJ_NBA.pdf的第11节中进行了解释
【解决方案2】:

awcreg是不必要的,bw需要声明为reg

module find_errors(
  input  [3:0] a,
  output [3:0] b,
  input [5:0] c
);
                                                   
reg [3:0] bw;
assign b = bw;

always @(a,c)
begin
  if (c == 4'b1111)
    bw <= a;
  else
    bw <= 4'b0101;
end

endmodule

由于没有顺序逻辑,你甚至不需要always 块:

module find_errors(
  input  [3:0] a,
  output [3:0] b,
  input [5:0] c
);
                                                   
assign b = (c == 4'b1111) ? a : 4'b0101;

endmodule

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多