Verilog学习笔记-hdlbits刷题练习(Vectors部分-Vector0,1)

basic部分:Verilog学习笔记-hdlbits刷题练习-CSDN博客

Vector0

矢量用于用一个名称将相关信号分组,以便于操作。例如,wire [7:0] w; 声明了一个名为 w 的 8 位矢量,在功能上等同于 8 条独立的导线

wire [99:0] my_vector; // Declare a 100-element vector

assign out = my_vector[10]; // Part-select one bit out of the vecto(取用某个值)

注意:向量定义时 长度 [7:0] 在名称 w 之前,前大后小。

但赋值时,数量在名称后面。

(左侧线上的3表示线的宽度)

module top_module ( 
    input wire [2:0] vec,
    output wire [2:0] outv,
    output wire o2,
    output wire o1,
    output wire o0  ); // Module body starts after module declaration
    assign outv[2:0]=vec[2:0];
    assign o2=vec[2];
    assign o1=vec[1];
    assign o0=vec[0];
endmodule

Vector1

Declare

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. 默认是wire类型
wire [0:7] b;         // 8-bit wire where b[0] is the most-significant bit.

在 Verilog 中,一旦以特定的字节序声明了矢量,就必须始终以相同的方式使用它。例如,当 vec 被声明为 wire [3:0] vec; 时,写入 vec[0:3] 是非法的。

Implicit nets(隐藏网络)

在 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 是一位没有声明的wire
                    // 使用向量的时候会出现bug

Unpacked vs. Packed Arrays(非打包阵列与打包阵列)

打包阵列声明在名称之前,非打包阵列声明在名称之后

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.

Accessing Vector Elements: Part-Select

访问整个矢量使用矢量名称。

assign w=a;

将整个 4 位向量 a 赋值给整个 8 位向量 w(声明取自上文)。如果左右两边的长度不匹配,就会根据情况进行零扩展或截断。

部分选取的例子:

有个疑问,上面声明的[0:7]b 那高四位是哪四位?

w[3:0]      // w的低四位
x[1]        // x的最低位
x[1:1]      // x的最低位
z[-1:-2]    // z的最低两位
b[3:0]      // 选不了;一定要和声明的维度对应
b[0:3]      // 最高的四位
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.

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.

别整啥最高位最低位,直接选就完了。

`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[7:0]=in[15:8];//最高8位
    assign out_lo[7:0]=in[7:0];//最di8位
endmodule

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值