Verilog学习3——向量

系列文章目录

Verilog学习1——三目运算符
Verilog学习2——与门(按位与和逻辑与)
Verilog学习3——向量



一、向量声明

向量使用前必须声明:

type [upper:lower] vector_name;

type指定向量的数据类型,通常是wirereg。如果声明输入或输出端口,则该类型还可以包括端口类型(例如,输入或输出)。例如:

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.

二、向量拆分

使用向量名称可访问整个向量:

assign w = a; //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.

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

`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,out_lo} = in;
endmodule

三、向量组合

[ ] 用于选择向量的一部分。级联运算符 { } 用于将向量的较小部分级联在一起来创建较大的向量:

{3'b111, 3'b000} => 6'b111000
{1'b1, 1'b0, 3'b101} => 5'b10101
{4'ha, 4'd10} => 8'b10101010     // 4'ha and 4'd10 are both 4'b1010 in binary

向量组合需要知道每个组件的宽度(或者知道结果的宽度)。因此,{1, 2, 3}是非法的,向量组合中不允许使用未知宽度的常量。
串联运算符 { } 可以用于赋值的左侧和右侧。

input [15:0] in;
output [23:0] out;
assign {out[7:0], out[15:8]} = in;         // Swap two bytes. Right side and left side are both 16-bit vectors.
assign out[15:0] = {in[7:0], in[15:8]};    // This is the same thing.
assign out = {in[7:0], in[15:8]};       // This is different. The 16-bit vector on the right is extended to
                                        // match the 24-bit vector on the left, so out[23:16] are zero.
                                        // In the first two examples, out[23:16] are not assigned.

四、向量复制

串联运算符 { } 允许将向量串联在一起以形成更大的向量。有时想把同一个向量连接在一起很多次,如果做一些类似赋值 a={b, b, b, b, b}; 的事情就很乏味。而复制运算符可以将重复向量连接在一起:

{num{vector}}

这表示将向量复制num次。num必须是一个常量,并且两个 { } 都是必须的。例如:

{5{1'b1}}           // 5'b11111 (or 5'd31 or 5'h1f)
{2{a,b,c}}          // The same as {a,b,c,a,b,c}
{3'd5, {2{3'd6}}}   // 9'b101_110_110. It's a concatenation of 101 with
                    // the second vector, which is two copies of 3'b110.

当宽度较小的数字扩展到较大的数字同时保留其符号值时,可以使用向量复制。需要将较小宽度数字的符号位(最高有效位)复制到左侧来完成的。
Build a circuit that sign-extends an 8-bit number to 32 bits:

module top_module (
    input [7:0] in,
    output [31:0] out );//
    assign out = {{24{in[7]}}, in};  
endmodule
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值