Verilog之向量Vector1

36 篇文章 14 订阅

Vectors are used to group related signals using one name to make it more convenient to manipulate. For example, wire [7:0] w; declares an 8-bit vector named w that is equivalent to having 8 separate wires.

向量用于使用一个名称对相关信号进行分组,以便于操作。例如,wire [7:0] w;声明一个名为 w 的 8 位向量,相当于有 8 条单独的连线。

目录

1.Declaring Vectors声明向量

1.1Implicit nets隐式声明

1.2Unpacked vs. Packed Arrays

2.Accessing Vector Elements: Part-Select访问向量元素:部分选择

A Bit of Practice

3.Module Declaration


1.Declaring Vectors声明向量

Vectors must be declared:

必须声明向量:

type [upper:lower] vector_name;

type specifies the datatype of the vector. This is usually wire or reg. If you are declaring a input or output port, the type can additionally include the port type (e.g., input or output) as well.

type 指定向量的数据类型。这通常是线型或者寄存器。如果您要声明输入或输出端口,则类型还可以包括端口类型(例如,输入或输出)。

Some examples:

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.
4位输出线。除非另有规定,否则类型为“导线”。
wire [0:7] b;         // 8-bit wire where b[0] is the most-significant bit.其中b[0]是最高有效位。

The endianness (or, informally, "direction") of a vector is whether the the least significant bit has a lower index (little-endian, e.g., [3:0]) or a higher index (big-endian, e.g., [0:3]). In Verilog, once a vector is declared with a particular endianness, it must always be used the same way. e.g., writing vec[0:3] when vec is declared wire [3:0] vec; is illegal. Being consistent with endianness is good practice, as weird bugs occur if vectors of different endianness are assigned or used together.

向量的字节序(或非正式的“方向”)是最低有效位是否具有较低的索引(小端序,例如 [3:0])或更高的索引(大端序,例如 [0:3])。在 Verilog 中,一旦以特定的字节序声明向量,它必须始终以相同的方式使用。例如,当 vec 被声明为 wire [3:0] vec 时,写入 vec[0:3];是非法的。与字节序保持一致是一种很好的做法,因为如果将不同字节序的向量分配或一起使用,则会出现奇怪的错误。

1.1Implicit nets隐式声明

Implicit nets are often a source of hard-to-detect bugs. In Verilog, net-type signals can be implicitly created by an assign statement or by attaching something undeclared to a module port. Implicit nets are always one-bit wires and causes bugs if you had intended to use a vector. Disabling creation of implicit nets can be done using the `default_nettype none directive.

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

隐式网络通常是难以检测的错误的来源。在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.

如果未声明,d 和 e 隐含一位宽。
// This could be a bug if the port was intended to be a vector.

/如果端口是一个向量,这可能是一个错误。

Adding `default_nettype none would make the second line of code an error, which makes the bug more visible.

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

1.2Unpacked vs. Packed Arrays

You may have noticed that in declarations, the vector indices are written before the vector name. This declares the "packed" dimensions of the array, where the bits are "packed" together into a blob (this is relevant in a simulator, but not in hardware). The unpacked dimensions are declared after the name. They are generally used to declare memory arrays. 

您可能已经注意到,在声明中,向量索引写在向量名称之前。这声明了数组的“打包”维度,其中位被“打包”到一个 blob 中(这在模拟器中相关,但在硬件中不相关)。解压后的维度在名称之后声明。它们通常用于声明内存数组。由于 ECE253 没有涵盖内存数组,因此我们在本课程中没有使用打包数组。

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.

 256 个未打包元素,每个元素都是 reg 的 8 位打包向量。256个未压缩元素,每个元素是reg的8位压缩向量。

29 个未打包的元素,每个元素都是 1 位 reg。29个未封装元件,每个元件是1位reg。

2.Accessing Vector Elements: Part-Select访问向量元素:部分选择

Accessing an entire vector is done using the vector name.使用向量名称访问整个向量。

For example:

assign w = a;

向量名 a 代表了向量中的所有比特为信号。

takes the entire 4-bit vector a and assigns it to the entire 8-bit vector w (declarations are taken from above). If the lengths of the right and left sides don't match, it is zero-extended or truncated as appropriate.

获取整个 4 位向量 a 并将其分配给整个 8 位向量 w(声明取自上面)。如果右侧和左侧的长度不匹配,则将其零扩展或酌情截断。

The part-select operator can be used to access a portion of a vector:

部分选择运算符可用于访问向量的一部分:

w[3:0]      // Only the lower 4 bits of w    只有 w 的低 4 位
x[1]        // The lowest bit of x           x 的最低位
x[1:1]      // ...also the lowest bit of x   ...也是 x 的最低位
z[-1:-2]    // Two lowest bits of z          z 的两个最低位
b[3:0]      // Illegal. Vector part-select must match the direction of the declaration.
               非法。向量部分选择必须与声明的方向相匹配。
b[0:3]      // The *upper* 4 bits of b.       b 的高4 位。

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.将 b 的高 4 位分配给 w 的低 4 位。w[3]=b[0]、w[2]=b[1] 等。

A Bit of Practice

Build a combinational circuit that splits an input half-word (16 bits, [15:0] ) into lower [7:0] and upper [15:8] bytes.

构建一个组合电路,将输入半字(16 位, [15:0] )拆分为低 [7:0] 和高 [15:8] 字节。

分别输出 16 位输入信号的高 8 位 和低 8 位。

3.Module Declaration

`default_nettype none     // Disable implicit nets. Reduces some types of bugs.
module top_module( 
    input wire [15:0] in,
    output wire [7:0] out_hi,
    output wire [7:0] out_lo );
`default_nettype none     // Disable implicit nets. Reduces some types of bugs.
module top_module( 
    input wire [15:0] in,
    output wire [7:0] out_hi,
    output wire [7:0] out_lo );
    assign out_hi = in[15:8];
    assign out_lo = in[7:0];
// Concatenation operator also works: assign {out_hi, out_lo} = in;

endmodule

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Shining0596

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值