Verilog学习3——向量

本文围绕Verilog学习中向量相关内容展开,介绍了向量的声明,使用前需指定数据类型;阐述了向量拆分,可访问整个向量或部分;说明了向量组合,用级联运算符将小部分组成大向量;还提及向量复制,可将向量多次连接,复制次数须为常量。

系列文章目录

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
### 关于 Verilog 向量时序图绘制的方法 在 Verilog 设计中,向量通常表示多比特宽的信号。为了更好地理解这些信号的行为及其相互作用,设计者经常需要借助时序图来可视化它们的时间特性。 #### 使用延迟语句构建基本时序逻辑 Verilog 提供了延迟语句的功能,允许开发者精确控制仿真的时间推进。这可以通过 `#` 符号实现特定时间段内的信号变化[^1]。例如: ```verilog initial begin reg_vector = 8'b0000_0000; // 初始化一个8位向量 #5 reg_vector = 8'b0000_1111; // 延迟5个时间单位后改变向量值 end ``` 上述代码展示了如何利用延迟语句创建简单的时序行为。这种技术对于定义输入激励波形特别有用,在测试平台开发中非常常见。 #### 动态数组的应用扩展到复杂数据结构建模 除了基础的单比特或固定宽度向量外,还可以采用动态数组来处理更复杂的场景。下面的例子说明了一个动态数组初始化并赋值的过程[^2]: ```verilog module test_dynamic_arrays(); int dynamic_vectors[]; initial begin dynamic_vectors = new[4]; // 创建长度为4的整型动态数组 foreach (dynamic_vectors[i]) begin dynamic_vectors[i] = $random % 16; // 随机生成范围内的数值填充至各元素位置 end #10; foreach (dynamic_vectors[i]) begin $display("Time=%t, Vector[%d]=%h", $time, i, dynamic_vectors[i]); end end endmodule ``` 此模块不仅演示了动态数组的操作方法,还结合 `$display` 和 `$time` 函数打印当前时刻各个索引对应的值,从而帮助观察其随时间演化的状态。 #### 工具支持与时序图展示 实际项目里,工程师一般依赖EDA工具(如ModelSim、Vivado Simulator等)自动生成详细的波形图表。这些软件能够解析RTL级描述文件,并依据内部设定好的触发条件自动渲染出相应的时序曲线。用户只需按照产品手册指导配置好必要的参数即可获得清晰直观的结果呈现形式。 #### 示例总结 综上所述,通过合理运用延迟机制以及高级的数据容器类型——比如这里提到过的动态数组,再配合强大的第三方仿真环境所提供的图形界面功能,完全可以满足大多数情况下有关分析研究数字电路工作原理的需求。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值