当有时输入和输出端口可以在 Verilog 中互换使用时,inout 端口的确切标准是什么?

What is the exact criteria for an inout port, when sometimes inout and output ports can be interchangeably used in Verilog?

在下面的模块中,理想的cnt、width和start应该是inout端口,而不是output端口。

但我尝试将这些端口作为输出端口,但我仍然能够运行它而没有任何错误。那么inout和output端口在Verilog中可以互换使用吗?

如果不是,那么确切的标准是什么,必须使用 inout 端口(在这种情况下不能使用输出端口)?

module (clk, rst, cnt, start, width, signal);
  input clk, rst, signal;
  output reg [11:0] cnt, width;
  output reg start;

  always @(posedge clk or negedge rst)
  begin
    if(~rst)
    begin
      cnt <= 0;
      start = 0;
      width <= 'h271;
    end
    else
    begin
      if(signal)
      begin

        width <= (start) ? width : 'h271;
        start = 1;
      end
      cnt <= (start) ? (cnt + 1) : cnt;
    end
  end
endmodule

注意 - 我知道,对于 inout 端口,我需要修改代码,因为 inout 端口不能是 reg 类型。但我在这里只询问端口类型。

outputinout 端口存在差异:

  1. 您不能同时读写inout端口,因此保留'Z用于读取。

  2. inout 端口可以 永远不会 是类型 reg。它必须是 wire 类型。

  3. 应该有一个信号决定给定端口是input还是output

在这个模块中,你必须有一个w_r信号,其功能如下:

// w_r = 1 => output port
// w_r = 0 => input port
input w_r;    
inout cnt;    
reg write_cnt;    
reg read_cnt;    
// wire read_cnt;    
assign cnt = w_r ? write_cnt : 1'bz;    // driving logic for cnt

// inside some always block
read_cnt <= cnt;
write_cnt <= some_data;

由于inout信号是线状的,必须由连续赋值语句驱动。

参考这部分:

So can inout and output ports be used interchangeably in Verilog?

答案是不鼓励使用它们可互换。由于对于输出端口,您可以单独使用 reg 类型。

从综合的角度来看,inout 端口可以有 三态驱动器或多路复用器 ,如下所示:(请注意,此结果可能因综合工具而异我不确定。):

有关详细信息,请参阅 Bidirectional port assignment and Bidirectional I/O Pins 论坛问题。

模拟器不关心端口方向。仿真工具总是将模块层次结构展平,重要的是它们可以将两个端口信号连接在一起。他们不检查数据流是否确实在指定的方向上流动。不幸的是,标准要求的内容和工具中实现的内容并不总是匹配。

有时,用户会在一个工具中拥有 "works" 的代码,但在另一个具有更强错误检查功能的工具中却没有。用户发现让他们的工具供应商删除错误检查比必须返回并修复他们的代码更容易。一些大多数模拟器已删除任何检查以强制执行端口方向。

关于不声明使用 inout 端口的变量数据类型的规则大部分保持不变,但许多工具允许将常量连接到 inout 端口。

端口方向在 Verilog 中基本上是建议性的,这在 SystemVerilog 中没有改变。这是一个历史问题,与 Verilog XL(第一个 Verilog 模拟器)如何进行端口崩溃有关;基本上,一切都是inout。当编写 '95 LRM 时,基本上记录了这种现有行为。在 1364-2005 中,这在 12.3.8 中显示为

A port that is declared as input (output) but used as an output (input) or inout may be coerced to inout. If not coerced to inout, a warning has to be issued.

实际上,所有内容都以 inout 结束,您可能会收到警告,也可能不会收到警告。因此,您几乎可以在模拟器中做任何事情,但 linter 或合成器应该为您找出任何逻辑连接错误(如果没有,那将毫无用处)。以下部分还有其他特定的非方向规则 - inputinout 端口必须是 net 类型,依此类推。