2021-01-28

本文详细介绍了Verilog中的矢量概念,包括如何声明向量、隐式网络的注意事项、未包装与包装数组的区别,以及部分选择操作。通过实例演示了如何正确处理不同长度的向量,并强调了保持端性一致的重要性,以避免潜在的错误。
摘要由CSDN通过智能技术生成

Verilog中的矢量

矢量用于使用一个名称对相关信号进行分组,使其操作更方便。例如,线网类型 [7:0] w;声明一个名为 w 的 8 位矢量,相当于有 8 条单独的导线。
1 声明矢量
声明向量类型 [上:下] vector_name;

类型指定矢量的数据类型。这通常是电线或雷格。如果要声明输入或输出端口,则类型还可以包括端口类型(例如输入或输出)。一些示例:

wire [7:0] w;         // 8-bit wire
reg  [4:1] x;         // 4-bit reg
output reg [0:0] y;   // 1-bit reg that is also an output port (this is still a vector)
input wire [3:-2] z;  // 6-bit wire input (negative ranges are allowed)
output [3:0] a;       // 4-bit output wire. Type is 'wire' unless specified otherwise.
wire [0:7] b;         // 8-bit wire where b[0] is the most-significant bit.

向量的末位(或非正式地为"方向")是最不重要的位是否具有较低的索引(小端,例如 [3:0])或较高的索引(大端,例如 [0:3])。在 Verilog 中,一旦使用特定的端度声明向量,必须始终以相同方式使用。例如,宣布何时写入是非法的。与内端性保持一致是好的做法,因为如果将不同末位性向量分配或一起使用,则会发生奇怪的错误。vec[0:3]vecwire [3:0] vec;

2.隐式网
隐式网络通常是难以检测的 Bug 的来源。在 Verilog 中,网络类型信号可以通过语句或将未声明的附加到模块端口来隐式创建。隐式网络始终是一位导线,如果您打算使用矢量,则会导致错误。可以使用指令禁用隐式网络的创建。assign`default_nettype none

wire [2:0] a, c;   // Two vectors
assign a = 3'b101;  // a = 101
assign b = a;       // b =   1  implicitly-created wire
assign c = b;       // c = 001  <-- bug
my_module i1 (d,e); // d and e are implicitly one-bit wide if not declared.
                    // This could be a bug if the port was intended to be a vector.

添加会使第二行代码出错,使 Bug 更加明显。`default_nettype none

3.未包装与包装阵列
解包与打包阵列
可能已经注意到,在声明中,矢量索引写在矢量名称之前。这将声明数组的"打包"维度,其中位被"打包"到 Blob 中(这在模拟器中相关,但在硬件中不相关)。解包维度在名称后声明。它们通常用于声明内存数组。

reg [7:0] mem [255:0];   // 256 unpacked elements, each of which is a 8-bit packed vector of reg.
reg mem2 [28:0];         // 29 unpacked elements, each of which is a 1-bit reg.

2 访问矢量元素:部分选择
使用矢量名称访问整个矢量。例如:

assign w = a;
取整个 4 位向量a 并将其分配给整个 8 位矢量w(声明取自上方)。如果左右两边的长度不匹配,则酌情进行零扩展或截断。
零件选择运算符可用于访问矢量的一部分:

w[3:0]      // Only the lower 4 bits of w
x[1]        // The lowest bit of x
x[1:1]      // ...also the lowest bit of x
z[-1:-2]    // Two lowest bits of z
b[3:0]      // Illegal. Vector part-select must match the direction of the declaration.
b[0:3]      // The *upper* 4 bits of b.
assign w[3:0] = b[0:3];    // Assign upper 4 bits of b to lower 4 bits of w. w[3]=b[0], w[2]=b[1], etc.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值